cipher.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/slab.h>
  19. #include <linux/string.h>
  20. #include "internal.h"
  21. static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key,
  22. unsigned int keylen)
  23. {
  24. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  25. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  26. int ret;
  27. u8 *buffer, *alignbuffer;
  28. unsigned long absize;
  29. absize = keylen + alignmask;
  30. buffer = kmalloc(absize, GFP_ATOMIC);
  31. if (!buffer)
  32. return -ENOMEM;
  33. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  34. memcpy(alignbuffer, key, keylen);
  35. ret = cia->cia_setkey(tfm, alignbuffer, keylen);
  36. memset(alignbuffer, 0, keylen);
  37. kfree(buffer);
  38. return ret;
  39. }
  40. static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
  41. {
  42. struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
  43. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  44. tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
  45. if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
  46. tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  47. return -EINVAL;
  48. }
  49. if ((unsigned long)key & alignmask)
  50. return setkey_unaligned(tfm, key, keylen);
  51. return cia->cia_setkey(tfm, key, keylen);
  52. }
  53. static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,
  54. const u8 *),
  55. struct crypto_tfm *tfm,
  56. u8 *dst, const u8 *src)
  57. {
  58. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  59. unsigned int size = crypto_tfm_alg_blocksize(tfm);
  60. u8 buffer[size + alignmask];
  61. u8 *tmp = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  62. memcpy(tmp, src, size);
  63. fn(tfm, tmp, tmp);
  64. memcpy(dst, tmp, size);
  65. }
  66. static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
  67. u8 *dst, const u8 *src)
  68. {
  69. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  70. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  71. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  72. cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
  73. return;
  74. }
  75. cipher->cia_encrypt(tfm, dst, src);
  76. }
  77. static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
  78. u8 *dst, const u8 *src)
  79. {
  80. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  81. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  82. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  83. cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
  84. return;
  85. }
  86. cipher->cia_decrypt(tfm, dst, src);
  87. }
  88. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  89. {
  90. struct cipher_tfm *ops = &tfm->crt_cipher;
  91. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  92. ops->cit_setkey = setkey;
  93. ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  94. cipher_encrypt_unaligned : cipher->cia_encrypt;
  95. ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  96. cipher_decrypt_unaligned : cipher->cia_decrypt;
  97. return 0;
  98. }
  99. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  100. {
  101. }