cipher.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifdef CONFIG_CRYPTO_FIPS
  63. if (unlikely(in_fips_err()))
  64. return;
  65. #endif
  66. memcpy(tmp, src, size);
  67. fn(tfm, tmp, tmp);
  68. memcpy(dst, tmp, size);
  69. }
  70. static void cipher_encrypt_unaligned(struct crypto_tfm *tfm,
  71. u8 *dst, const u8 *src)
  72. {
  73. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  74. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  75. #ifdef CONFIG_CRYPTO_FIPS
  76. if (unlikely(in_fips_err()))
  77. return;
  78. #endif
  79. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  80. cipher_crypt_unaligned(cipher->cia_encrypt, tfm, dst, src);
  81. return;
  82. }
  83. cipher->cia_encrypt(tfm, dst, src);
  84. }
  85. static void cipher_decrypt_unaligned(struct crypto_tfm *tfm,
  86. u8 *dst, const u8 *src)
  87. {
  88. unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
  89. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  90. #ifdef CONFIG_CRYPTO_FIPS
  91. if (unlikely(in_fips_err()))
  92. return;
  93. #endif
  94. if (unlikely(((unsigned long)dst | (unsigned long)src) & alignmask)) {
  95. cipher_crypt_unaligned(cipher->cia_decrypt, tfm, dst, src);
  96. return;
  97. }
  98. cipher->cia_decrypt(tfm, dst, src);
  99. }
  100. int crypto_init_cipher_ops(struct crypto_tfm *tfm)
  101. {
  102. struct cipher_tfm *ops = &tfm->crt_cipher;
  103. struct cipher_alg *cipher = &tfm->__crt_alg->cra_cipher;
  104. ops->cit_setkey = setkey;
  105. ops->cit_encrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  106. cipher_encrypt_unaligned : cipher->cia_encrypt;
  107. ops->cit_decrypt_one = crypto_tfm_alg_alignmask(tfm) ?
  108. cipher_decrypt_unaligned : cipher->cia_decrypt;
  109. return 0;
  110. }
  111. void crypto_exit_cipher_ops(struct crypto_tfm *tfm)
  112. {
  113. }