aes-ce-ccm-glue.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * aes-ccm-glue.c - AES-CCM transform for ARMv8 with Crypto Extensions
  3. *
  4. * Copyright (C) 2013 - 2014 Linaro Ltd <ard.biesheuvel@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <asm/neon.h>
  11. #include <asm/unaligned.h>
  12. #include <crypto/aes.h>
  13. #include <crypto/algapi.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/crypto.h>
  16. #include <linux/module.h>
  17. static int num_rounds(struct crypto_aes_ctx *ctx)
  18. {
  19. /*
  20. * # of rounds specified by AES:
  21. * 128 bit key 10 rounds
  22. * 192 bit key 12 rounds
  23. * 256 bit key 14 rounds
  24. * => n byte key => 6 + (n/4) rounds
  25. */
  26. return 6 + ctx->key_length / 4;
  27. }
  28. asmlinkage void ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes,
  29. u32 *macp, u32 const rk[], u32 rounds);
  30. asmlinkage void ce_aes_ccm_encrypt(u8 out[], u8 const in[], u32 cbytes,
  31. u32 const rk[], u32 rounds, u8 mac[],
  32. u8 ctr[]);
  33. asmlinkage void ce_aes_ccm_decrypt(u8 out[], u8 const in[], u32 cbytes,
  34. u32 const rk[], u32 rounds, u8 mac[],
  35. u8 ctr[]);
  36. asmlinkage void ce_aes_ccm_final(u8 mac[], u8 const ctr[], u32 const rk[],
  37. u32 rounds);
  38. static int ccm_setkey(struct crypto_aead *tfm, const u8 *in_key,
  39. unsigned int key_len)
  40. {
  41. struct crypto_aes_ctx *ctx = crypto_aead_ctx(tfm);
  42. int ret;
  43. ret = crypto_aes_expand_key(ctx, in_key, key_len);
  44. if (!ret)
  45. return 0;
  46. tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  47. return -EINVAL;
  48. }
  49. static int ccm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
  50. {
  51. if ((authsize & 1) || authsize < 4)
  52. return -EINVAL;
  53. return 0;
  54. }
  55. static int ccm_init_mac(struct aead_request *req, u8 maciv[], u32 msglen)
  56. {
  57. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  58. __be32 *n = (__be32 *)&maciv[AES_BLOCK_SIZE - 8];
  59. u32 l = req->iv[0] + 1;
  60. /* verify that CCM dimension 'L' is set correctly in the IV */
  61. if (l < 2 || l > 8)
  62. return -EINVAL;
  63. /* verify that msglen can in fact be represented in L bytes */
  64. if (l < 4 && msglen >> (8 * l))
  65. return -EOVERFLOW;
  66. /*
  67. * Even if the CCM spec allows L values of up to 8, the Linux cryptoapi
  68. * uses a u32 type to represent msglen so the top 4 bytes are always 0.
  69. */
  70. n[0] = 0;
  71. n[1] = cpu_to_be32(msglen);
  72. memcpy(maciv, req->iv, AES_BLOCK_SIZE - l);
  73. /*
  74. * Meaning of byte 0 according to CCM spec (RFC 3610/NIST 800-38C)
  75. * - bits 0..2 : max # of bytes required to represent msglen, minus 1
  76. * (already set by caller)
  77. * - bits 3..5 : size of auth tag (1 => 4 bytes, 2 => 6 bytes, etc)
  78. * - bit 6 : indicates presence of authenticate-only data
  79. */
  80. maciv[0] |= (crypto_aead_authsize(aead) - 2) << 2;
  81. if (req->assoclen)
  82. maciv[0] |= 0x40;
  83. memset(&req->iv[AES_BLOCK_SIZE - l], 0, l);
  84. return 0;
  85. }
  86. static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[])
  87. {
  88. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  89. struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
  90. struct __packed { __be16 l; __be32 h; u16 len; } ltag;
  91. struct scatter_walk walk;
  92. u32 len = req->assoclen;
  93. u32 macp = 0;
  94. /* prepend the AAD with a length tag */
  95. if (len < 0xff00) {
  96. ltag.l = cpu_to_be16(len);
  97. ltag.len = 2;
  98. } else {
  99. ltag.l = cpu_to_be16(0xfffe);
  100. put_unaligned_be32(len, &ltag.h);
  101. ltag.len = 6;
  102. }
  103. ce_aes_ccm_auth_data(mac, (u8 *)&ltag, ltag.len, &macp, ctx->key_enc,
  104. num_rounds(ctx));
  105. scatterwalk_start(&walk, req->assoc);
  106. do {
  107. u32 n = scatterwalk_clamp(&walk, len);
  108. u8 *p;
  109. if (!n) {
  110. scatterwalk_start(&walk, sg_next(walk.sg));
  111. n = scatterwalk_clamp(&walk, len);
  112. }
  113. p = scatterwalk_map(&walk);
  114. ce_aes_ccm_auth_data(mac, p, n, &macp, ctx->key_enc,
  115. num_rounds(ctx));
  116. len -= n;
  117. scatterwalk_unmap(p);
  118. scatterwalk_advance(&walk, n);
  119. scatterwalk_done(&walk, 0, len);
  120. } while (len);
  121. }
  122. static int ccm_encrypt(struct aead_request *req)
  123. {
  124. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  125. struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
  126. struct blkcipher_desc desc = { .info = req->iv };
  127. struct blkcipher_walk walk;
  128. u8 __aligned(8) mac[AES_BLOCK_SIZE];
  129. u8 buf[AES_BLOCK_SIZE];
  130. u32 len = req->cryptlen;
  131. int err;
  132. err = ccm_init_mac(req, mac, len);
  133. if (err)
  134. return err;
  135. kernel_neon_begin_partial(6);
  136. if (req->assoclen)
  137. ccm_calculate_auth_mac(req, mac);
  138. /* preserve the original iv for the final round */
  139. memcpy(buf, req->iv, AES_BLOCK_SIZE);
  140. blkcipher_walk_init(&walk, req->dst, req->src, len);
  141. err = blkcipher_aead_walk_virt_block(&desc, &walk, aead,
  142. AES_BLOCK_SIZE);
  143. while (walk.nbytes) {
  144. u32 tail = walk.nbytes % AES_BLOCK_SIZE;
  145. if (walk.nbytes == len)
  146. tail = 0;
  147. ce_aes_ccm_encrypt(walk.dst.virt.addr, walk.src.virt.addr,
  148. walk.nbytes - tail, ctx->key_enc,
  149. num_rounds(ctx), mac, walk.iv);
  150. len -= walk.nbytes - tail;
  151. err = blkcipher_walk_done(&desc, &walk, tail);
  152. }
  153. if (!err)
  154. ce_aes_ccm_final(mac, buf, ctx->key_enc, num_rounds(ctx));
  155. kernel_neon_end();
  156. if (err)
  157. return err;
  158. /* copy authtag to end of dst */
  159. scatterwalk_map_and_copy(mac, req->dst, req->cryptlen,
  160. crypto_aead_authsize(aead), 1);
  161. return 0;
  162. }
  163. static int ccm_decrypt(struct aead_request *req)
  164. {
  165. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  166. struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
  167. unsigned int authsize = crypto_aead_authsize(aead);
  168. struct blkcipher_desc desc = { .info = req->iv };
  169. struct blkcipher_walk walk;
  170. u8 __aligned(8) mac[AES_BLOCK_SIZE];
  171. u8 buf[AES_BLOCK_SIZE];
  172. u32 len = req->cryptlen - authsize;
  173. int err;
  174. err = ccm_init_mac(req, mac, len);
  175. if (err)
  176. return err;
  177. kernel_neon_begin_partial(6);
  178. if (req->assoclen)
  179. ccm_calculate_auth_mac(req, mac);
  180. /* preserve the original iv for the final round */
  181. memcpy(buf, req->iv, AES_BLOCK_SIZE);
  182. blkcipher_walk_init(&walk, req->dst, req->src, len);
  183. err = blkcipher_aead_walk_virt_block(&desc, &walk, aead,
  184. AES_BLOCK_SIZE);
  185. while (walk.nbytes) {
  186. u32 tail = walk.nbytes % AES_BLOCK_SIZE;
  187. if (walk.nbytes == len)
  188. tail = 0;
  189. ce_aes_ccm_decrypt(walk.dst.virt.addr, walk.src.virt.addr,
  190. walk.nbytes - tail, ctx->key_enc,
  191. num_rounds(ctx), mac, walk.iv);
  192. len -= walk.nbytes - tail;
  193. err = blkcipher_walk_done(&desc, &walk, tail);
  194. }
  195. if (!err)
  196. ce_aes_ccm_final(mac, buf, ctx->key_enc, num_rounds(ctx));
  197. kernel_neon_end();
  198. if (err)
  199. return err;
  200. /* compare calculated auth tag with the stored one */
  201. scatterwalk_map_and_copy(buf, req->src, req->cryptlen - authsize,
  202. authsize, 0);
  203. if (memcmp(mac, buf, authsize))
  204. return -EBADMSG;
  205. return 0;
  206. }
  207. static struct crypto_alg ccm_aes_alg = {
  208. .cra_name = "ccm(aes)",
  209. .cra_driver_name = "ccm-aes-ce",
  210. .cra_priority = 300,
  211. .cra_flags = CRYPTO_ALG_TYPE_AEAD,
  212. .cra_blocksize = 1,
  213. .cra_ctxsize = sizeof(struct crypto_aes_ctx),
  214. .cra_alignmask = 7,
  215. .cra_type = &crypto_aead_type,
  216. .cra_module = THIS_MODULE,
  217. .cra_aead = {
  218. .ivsize = AES_BLOCK_SIZE,
  219. .maxauthsize = AES_BLOCK_SIZE,
  220. .setkey = ccm_setkey,
  221. .setauthsize = ccm_setauthsize,
  222. .encrypt = ccm_encrypt,
  223. .decrypt = ccm_decrypt,
  224. }
  225. };
  226. static int __init aes_mod_init(void)
  227. {
  228. if (!(elf_hwcap & HWCAP_AES))
  229. return -ENODEV;
  230. return crypto_register_alg(&ccm_aes_alg);
  231. }
  232. static void __exit aes_mod_exit(void)
  233. {
  234. crypto_unregister_alg(&ccm_aes_alg);
  235. }
  236. module_init(aes_mod_init);
  237. module_exit(aes_mod_exit);
  238. MODULE_DESCRIPTION("Synchronous AES in CCM mode using ARMv8 Crypto Extensions");
  239. MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
  240. MODULE_LICENSE("GPL v2");
  241. MODULE_ALIAS("ccm(aes)");