cipher.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Cipher operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/crypto.h>
  17. #include <linux/errno.h>
  18. #include <linux/scatterlist.h>
  19. #include <linux/string.h>
  20. #include "internal.h"
  21. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  22. {
  23. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  24. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  25. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  26. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  27. return -EINVAL;
  28. } else
  29. return cia->cia_setkey(tfm, key, keylen);
  30. }
  31. static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
  32. const u8 *),
  33. struct crypto_tfm *tfm,
  34. u8 *dst, const u8 *src)
  35. {
  36. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  37. unsigned int size = crypto_tfm_alg_blocksize(tfm);
  38. u8 buffer[size + alignmask];
  39. u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  40. memcpy(tmp, src, size);
  41. fn(tfm, tmp, tmp);
  42. memcpy(dst, tmp, size);
  43. }
  44. static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
  45. u8 *dst, const u8 *src)
  46. {
  47. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  48. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  49. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  50. cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
  51. return;
  52. }
  53. cipher->cia_encrypt(tfm, dst, src);
  54. }
  55. static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
  56. u8 *dst, const u8 *src)
  57. {
  58. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  59. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  60. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  61. cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
  62. return;
  63. }
  64. cipher->cia_decrypt(tfm, dst, src);
  65. }
  66. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  67. {
  68. struct cipher_tfm *ops = &tfm->crt_cipher;
  69. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  70. ops->cit_setkey = setkey;
  71. ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  72. cipher_encrypt_unaligned : cipher->cia_encrypt;
  73. ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  74. cipher_decrypt_unaligned : cipher->cia_decrypt;
  75. return 0;
  76. }
  77. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  78. {
  79. }