aead.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * AEAD: Authenticated Encryption with Associated Data
  3. *
  4. * This file provides API support for AEAD algorithms.
  5. *
  6. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. */
  14. #include <crypto/algapi.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/seq_file.h>
  21. static int setkey_unaligned(struct crypto_aead *tfm, const u8 *key,
  22. unsigned int keylen)
  23. {
  24. struct aead_alg *aead = crypto_aead_alg(tfm);
  25. unsigned long alignmask = crypto_aead_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 = aead->setkey(tfm, alignbuffer, keylen);
  36. memset(alignbuffer, 0, keylen);
  37. kfree(buffer);
  38. return ret;
  39. }
  40. static int setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
  41. {
  42. struct aead_alg *aead = crypto_aead_alg(tfm);
  43. unsigned long alignmask = crypto_aead_alignmask(tfm);
  44. if ((unsigned long)key & alignmask)
  45. return setkey_unaligned(tfm, key, keylen);
  46. return aead->setkey(tfm, key, keylen);
  47. }
  48. int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  49. {
  50. int err;
  51. if (authsize > crypto_aead_alg(tfm)->maxauthsize)
  52. return -EINVAL;
  53. if (crypto_aead_alg(tfm)->setauthsize) {
  54. err = crypto_aead_alg(tfm)->setauthsize(tfm, authsize);
  55. if (err)
  56. return err;
  57. }
  58. crypto_aead_crt(tfm)->authsize = authsize;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(crypto_aead_setauthsize);
  62. static unsigned int crypto_aead_ctxsize(struct crypto_alg *alg, u32 type,
  63. u32 mask)
  64. {
  65. return alg->cra_ctxsize;
  66. }
  67. static int crypto_init_aead_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  68. {
  69. struct aead_alg *alg = &tfm->__crt_alg->cra_aead;
  70. struct aead_tfm *crt = &tfm->crt_aead;
  71. if (max(alg->maxauthsize, alg->ivsize) > PAGE_SIZE / 8)
  72. return -EINVAL;
  73. crt->setkey = setkey;
  74. crt->encrypt = alg->encrypt;
  75. crt->decrypt = alg->decrypt;
  76. crt->ivsize = alg->ivsize;
  77. crt->authsize = alg->maxauthsize;
  78. return 0;
  79. }
  80. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  81. __attribute__ ((unused));
  82. static void crypto_aead_show(struct seq_file *m, struct crypto_alg *alg)
  83. {
  84. struct aead_alg *aead = &alg->cra_aead;
  85. seq_printf(m, "type : aead\n");
  86. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  87. seq_printf(m, "ivsize : %u\n", aead->ivsize);
  88. seq_printf(m, "maxauthsize : %u\n", aead->maxauthsize);
  89. }
  90. const struct crypto_type crypto_aead_type = {
  91. .ctxsize = crypto_aead_ctxsize,
  92. .init = crypto_init_aead_ops,
  93. #ifdef CONFIG_PROC_FS
  94. .show = crypto_aead_show,
  95. #endif
  96. };
  97. EXPORT_SYMBOL_GPL(crypto_aead_type);
  98. MODULE_LICENSE("GPL");
  99. MODULE_DESCRIPTION("Authenticated Encryption with Associated Data (AEAD)");