ablkcipher.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Asynchronous block chaining cipher operations.
  3. *
  4. * This is the asynchronous version of blkcipher.c indicating completion
  5. * via a callback.
  6. *
  7. * Copyright (c) 2006 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 <crypto/algapi.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/seq_file.h>
  22. static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  23. unsigned int keylen)
  24. {
  25. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  26. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  27. int ret;
  28. u8 *buffer, *alignbuffer;
  29. unsigned long absize;
  30. absize = keylen + alignmask;
  31. buffer = kmalloc(absize, GFP_ATOMIC);
  32. if (!buffer)
  33. return -ENOMEM;
  34. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  35. memcpy(alignbuffer, key, keylen);
  36. ret = cipher->setkey(tfm, alignbuffer, keylen);
  37. memset(alignbuffer, 0, keylen);
  38. kfree(buffer);
  39. return ret;
  40. }
  41. static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  42. unsigned int keylen)
  43. {
  44. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  45. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  46. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  47. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  48. return -EINVAL;
  49. }
  50. if ((unsigned long)key & alignmask)
  51. return setkey_unaligned(tfm, key, keylen);
  52. return cipher->setkey(tfm, key, keylen);
  53. }
  54. static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  55. u32 mask)
  56. {
  57. return alg->cra_ctxsize;
  58. }
  59. static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  60. u32 mask)
  61. {
  62. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  63. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  64. if (alg->ivsize > PAGE_SIZE / 8)
  65. return -EINVAL;
  66. crt->setkey = setkey;
  67. crt->encrypt = alg->encrypt;
  68. crt->decrypt = alg->decrypt;
  69. crt->ivsize = alg->ivsize;
  70. return 0;
  71. }
  72. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  73. __attribute__ ((unused));
  74. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  75. {
  76. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  77. seq_printf(m, "type : ablkcipher\n");
  78. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  79. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  80. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  81. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  82. }
  83. const struct crypto_type crypto_ablkcipher_type = {
  84. .ctxsize = crypto_ablkcipher_ctxsize,
  85. .init = crypto_init_ablkcipher_ops,
  86. #ifdef CONFIG_PROC_FS
  87. .show = crypto_ablkcipher_show,
  88. #endif
  89. };
  90. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  91. MODULE_LICENSE("GPL");
  92. MODULE_DESCRIPTION("Asynchronous block chaining cipher type");