2 * aes-ccm-glue.c - AES-CCM transform for ARMv8 with Crypto Extensions
4 * Copyright (C) 2013 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
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.
13 #include <asm/unaligned.h>
14 #include <crypto/aes.h>
15 #include <crypto/scatterwalk.h>
16 #include <crypto/internal/aead.h>
17 #include <crypto/internal/simd.h>
18 #include <crypto/internal/skcipher.h>
19 #include <linux/module.h>
21 #include "aes-ce-setkey.h"
23 static int num_rounds(struct crypto_aes_ctx *ctx)
26 * # of rounds specified by AES:
27 * 128 bit key 10 rounds
28 * 192 bit key 12 rounds
29 * 256 bit key 14 rounds
30 * => n byte key => 6 + (n/4) rounds
32 return 6 + ctx->key_length / 4;
35 asmlinkage void ce_aes_ccm_auth_data(u8 mac[], u8 const in[], u32 abytes,
36 u32 *macp, u32 const rk[], u32 rounds);
38 asmlinkage void ce_aes_ccm_encrypt(u8 out[], u8 const in[], u32 cbytes,
39 u32 const rk[], u32 rounds, u8 mac[],
42 asmlinkage void ce_aes_ccm_decrypt(u8 out[], u8 const in[], u32 cbytes,
43 u32 const rk[], u32 rounds, u8 mac[],
46 asmlinkage void ce_aes_ccm_final(u8 mac[], u8 const ctr[], u32 const rk[],
49 asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int rounds);
51 static int ccm_setkey(struct crypto_aead *tfm, const u8 *in_key,
54 struct crypto_aes_ctx *ctx = crypto_aead_ctx(tfm);
57 ret = ce_aes_expandkey(ctx, in_key, key_len);
61 tfm->base.crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
65 static int ccm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
67 if ((authsize & 1) || authsize < 4)
72 static int ccm_init_mac(struct aead_request *req, u8 maciv[], u32 msglen)
74 struct crypto_aead *aead = crypto_aead_reqtfm(req);
75 __be32 *n = (__be32 *)&maciv[AES_BLOCK_SIZE - 8];
76 u32 l = req->iv[0] + 1;
78 /* verify that CCM dimension 'L' is set correctly in the IV */
82 /* verify that msglen can in fact be represented in L bytes */
83 if (l < 4 && msglen >> (8 * l))
87 * Even if the CCM spec allows L values of up to 8, the Linux cryptoapi
88 * uses a u32 type to represent msglen so the top 4 bytes are always 0.
91 n[1] = cpu_to_be32(msglen);
93 memcpy(maciv, req->iv, AES_BLOCK_SIZE - l);
96 * Meaning of byte 0 according to CCM spec (RFC 3610/NIST 800-38C)
97 * - bits 0..2 : max # of bytes required to represent msglen, minus 1
98 * (already set by caller)
99 * - bits 3..5 : size of auth tag (1 => 4 bytes, 2 => 6 bytes, etc)
100 * - bit 6 : indicates presence of authenticate-only data
102 maciv[0] |= (crypto_aead_authsize(aead) - 2) << 2;
106 memset(&req->iv[AES_BLOCK_SIZE - l], 0, l);
110 static void ccm_update_mac(struct crypto_aes_ctx *key, u8 mac[], u8 const in[],
111 u32 abytes, u32 *macp)
113 if (crypto_simd_usable()) {
115 ce_aes_ccm_auth_data(mac, in, abytes, macp, key->key_enc,
119 if (*macp > 0 && *macp < AES_BLOCK_SIZE) {
120 int added = min(abytes, AES_BLOCK_SIZE - *macp);
122 crypto_xor(&mac[*macp], in, added);
129 while (abytes >= AES_BLOCK_SIZE) {
130 __aes_arm64_encrypt(key->key_enc, mac, mac,
132 crypto_xor(mac, in, AES_BLOCK_SIZE);
134 in += AES_BLOCK_SIZE;
135 abytes -= AES_BLOCK_SIZE;
139 __aes_arm64_encrypt(key->key_enc, mac, mac,
141 crypto_xor(mac, in, abytes);
147 static void ccm_calculate_auth_mac(struct aead_request *req, u8 mac[])
149 struct crypto_aead *aead = crypto_aead_reqtfm(req);
150 struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
151 struct __packed { __be16 l; __be32 h; u16 len; } ltag;
152 struct scatter_walk walk;
153 u32 len = req->assoclen;
156 /* prepend the AAD with a length tag */
158 ltag.l = cpu_to_be16(len);
161 ltag.l = cpu_to_be16(0xfffe);
162 put_unaligned_be32(len, <ag.h);
166 ccm_update_mac(ctx, mac, (u8 *)<ag, ltag.len, &macp);
167 scatterwalk_start(&walk, req->src);
170 u32 n = scatterwalk_clamp(&walk, len);
174 scatterwalk_start(&walk, sg_next(walk.sg));
175 n = scatterwalk_clamp(&walk, len);
177 p = scatterwalk_map(&walk);
178 ccm_update_mac(ctx, mac, p, n, &macp);
181 scatterwalk_unmap(p);
182 scatterwalk_advance(&walk, n);
183 scatterwalk_done(&walk, 0, len);
187 static int ccm_crypt_fallback(struct skcipher_walk *walk, u8 mac[], u8 iv0[],
188 struct crypto_aes_ctx *ctx, bool enc)
190 u8 buf[AES_BLOCK_SIZE];
193 while (walk->nbytes) {
194 int blocks = walk->nbytes / AES_BLOCK_SIZE;
195 u32 tail = walk->nbytes % AES_BLOCK_SIZE;
196 u8 *dst = walk->dst.virt.addr;
197 u8 *src = walk->src.virt.addr;
198 u32 nbytes = walk->nbytes;
200 if (nbytes == walk->total && tail > 0) {
206 u32 bsize = AES_BLOCK_SIZE;
208 if (nbytes < AES_BLOCK_SIZE)
211 crypto_inc(walk->iv, AES_BLOCK_SIZE);
212 __aes_arm64_encrypt(ctx->key_enc, buf, walk->iv,
214 __aes_arm64_encrypt(ctx->key_enc, mac, mac,
217 crypto_xor(mac, src, bsize);
218 crypto_xor_cpy(dst, src, buf, bsize);
220 crypto_xor(mac, dst, bsize);
226 err = skcipher_walk_done(walk, tail);
230 __aes_arm64_encrypt(ctx->key_enc, buf, iv0, num_rounds(ctx));
231 __aes_arm64_encrypt(ctx->key_enc, mac, mac, num_rounds(ctx));
232 crypto_xor(mac, buf, AES_BLOCK_SIZE);
237 static int ccm_encrypt(struct aead_request *req)
239 struct crypto_aead *aead = crypto_aead_reqtfm(req);
240 struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
241 struct skcipher_walk walk;
242 u8 __aligned(8) mac[AES_BLOCK_SIZE];
243 u8 buf[AES_BLOCK_SIZE];
244 u32 len = req->cryptlen;
247 err = ccm_init_mac(req, mac, len);
252 ccm_calculate_auth_mac(req, mac);
254 /* preserve the original iv for the final round */
255 memcpy(buf, req->iv, AES_BLOCK_SIZE);
257 err = skcipher_walk_aead_encrypt(&walk, req, false);
259 if (crypto_simd_usable()) {
260 while (walk.nbytes) {
261 u32 tail = walk.nbytes % AES_BLOCK_SIZE;
263 if (walk.nbytes == walk.total)
267 ce_aes_ccm_encrypt(walk.dst.virt.addr,
269 walk.nbytes - tail, ctx->key_enc,
270 num_rounds(ctx), mac, walk.iv);
273 err = skcipher_walk_done(&walk, tail);
277 ce_aes_ccm_final(mac, buf, ctx->key_enc,
282 err = ccm_crypt_fallback(&walk, mac, buf, ctx, true);
287 /* copy authtag to end of dst */
288 scatterwalk_map_and_copy(mac, req->dst, req->assoclen + req->cryptlen,
289 crypto_aead_authsize(aead), 1);
294 static int ccm_decrypt(struct aead_request *req)
296 struct crypto_aead *aead = crypto_aead_reqtfm(req);
297 struct crypto_aes_ctx *ctx = crypto_aead_ctx(aead);
298 unsigned int authsize = crypto_aead_authsize(aead);
299 struct skcipher_walk walk;
300 u8 __aligned(8) mac[AES_BLOCK_SIZE];
301 u8 buf[AES_BLOCK_SIZE];
302 u32 len = req->cryptlen - authsize;
305 err = ccm_init_mac(req, mac, len);
310 ccm_calculate_auth_mac(req, mac);
312 /* preserve the original iv for the final round */
313 memcpy(buf, req->iv, AES_BLOCK_SIZE);
315 err = skcipher_walk_aead_decrypt(&walk, req, false);
317 if (crypto_simd_usable()) {
318 while (walk.nbytes) {
319 u32 tail = walk.nbytes % AES_BLOCK_SIZE;
321 if (walk.nbytes == walk.total)
325 ce_aes_ccm_decrypt(walk.dst.virt.addr,
327 walk.nbytes - tail, ctx->key_enc,
328 num_rounds(ctx), mac, walk.iv);
331 err = skcipher_walk_done(&walk, tail);
335 ce_aes_ccm_final(mac, buf, ctx->key_enc,
340 err = ccm_crypt_fallback(&walk, mac, buf, ctx, false);
346 /* compare calculated auth tag with the stored one */
347 scatterwalk_map_and_copy(buf, req->src,
348 req->assoclen + req->cryptlen - authsize,
351 if (crypto_memneq(mac, buf, authsize))
356 static struct aead_alg ccm_aes_alg = {
358 .cra_name = "ccm(aes)",
359 .cra_driver_name = "ccm-aes-ce",
362 .cra_ctxsize = sizeof(struct crypto_aes_ctx),
363 .cra_module = THIS_MODULE,
365 .ivsize = AES_BLOCK_SIZE,
366 .chunksize = AES_BLOCK_SIZE,
367 .maxauthsize = AES_BLOCK_SIZE,
368 .setkey = ccm_setkey,
369 .setauthsize = ccm_setauthsize,
370 .encrypt = ccm_encrypt,
371 .decrypt = ccm_decrypt,
374 static int __init aes_mod_init(void)
376 if (!cpu_have_named_feature(AES))
378 return crypto_register_aead(&ccm_aes_alg);
381 static void __exit aes_mod_exit(void)
383 crypto_unregister_aead(&ccm_aes_alg);
386 module_init(aes_mod_init);
387 module_exit(aes_mod_exit);
389 MODULE_DESCRIPTION("Synchronous AES in CCM mode using ARMv8 Crypto Extensions");
390 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
391 MODULE_LICENSE("GPL v2");
392 MODULE_ALIAS_CRYPTO("ccm(aes)");