xref: /linux/include/crypto/aes-cbc-macs.h (revision 370c3883195566ee3e7d79e0146c3d735a406573)
1*309a7e51SEric Biggers /* SPDX-License-Identifier: GPL-2.0 */
2*309a7e51SEric Biggers /*
3*309a7e51SEric Biggers  * Support for AES-CMAC, AES-XCBC-MAC, and AES-CBC-MAC
4*309a7e51SEric Biggers  *
5*309a7e51SEric Biggers  * Copyright 2026 Google LLC
6*309a7e51SEric Biggers  */
7*309a7e51SEric Biggers #ifndef _CRYPTO_AES_CBC_MACS_H
8*309a7e51SEric Biggers #define _CRYPTO_AES_CBC_MACS_H
9*309a7e51SEric Biggers 
10*309a7e51SEric Biggers #include <crypto/aes.h>
11*309a7e51SEric Biggers 
12*309a7e51SEric Biggers /**
13*309a7e51SEric Biggers  * struct aes_cmac_key - Prepared key for AES-CMAC or AES-XCBC-MAC
14*309a7e51SEric Biggers  * @aes: The AES key for cipher block chaining
15*309a7e51SEric Biggers  * @k_final: Finalization subkeys for the final block.
16*309a7e51SEric Biggers  *	     k_final[0] (CMAC K1, XCBC-MAC K2) is used if it's a full block.
17*309a7e51SEric Biggers  *	     k_final[1] (CMAC K2, XCBC-MAC K3) is used if it's a partial block.
18*309a7e51SEric Biggers  */
19*309a7e51SEric Biggers struct aes_cmac_key {
20*309a7e51SEric Biggers 	struct aes_enckey aes;
21*309a7e51SEric Biggers 	union {
22*309a7e51SEric Biggers 		u8 b[AES_BLOCK_SIZE];
23*309a7e51SEric Biggers 		__be64 w[2];
24*309a7e51SEric Biggers 	} k_final[2];
25*309a7e51SEric Biggers };
26*309a7e51SEric Biggers 
27*309a7e51SEric Biggers /**
28*309a7e51SEric Biggers  * struct aes_cmac_ctx - Context for computing an AES-CMAC or AES-XCBC-MAC value
29*309a7e51SEric Biggers  * @key: Pointer to the key struct.  A pointer is used rather than a copy of the
30*309a7e51SEric Biggers  *	 struct, since the key struct size may be large.  It is assumed that the
31*309a7e51SEric Biggers  *	 key lives at least as long as the context.
32*309a7e51SEric Biggers  * @partial_len: Number of bytes that have been XOR'ed into @h since the last
33*309a7e51SEric Biggers  *		 AES encryption.  This is 0 if no data has been processed yet,
34*309a7e51SEric Biggers  *		 or between 1 and AES_BLOCK_SIZE inclusive otherwise.
35*309a7e51SEric Biggers  * @h: The current chaining value
36*309a7e51SEric Biggers  */
37*309a7e51SEric Biggers struct aes_cmac_ctx {
38*309a7e51SEric Biggers 	const struct aes_cmac_key *key;
39*309a7e51SEric Biggers 	size_t partial_len;
40*309a7e51SEric Biggers 	u8 h[AES_BLOCK_SIZE];
41*309a7e51SEric Biggers };
42*309a7e51SEric Biggers 
43*309a7e51SEric Biggers /**
44*309a7e51SEric Biggers  * aes_cmac_preparekey() - Prepare a key for AES-CMAC
45*309a7e51SEric Biggers  * @key: (output) The key struct to initialize
46*309a7e51SEric Biggers  * @in_key: The raw AES key
47*309a7e51SEric Biggers  * @key_len: Length of the raw key in bytes.  The supported values are
48*309a7e51SEric Biggers  *	     AES_KEYSIZE_128, AES_KEYSIZE_192, and AES_KEYSIZE_256.
49*309a7e51SEric Biggers  *
50*309a7e51SEric Biggers  * Context: Any context.
51*309a7e51SEric Biggers  * Return: 0 on success or -EINVAL if the given key length is invalid.  No other
52*309a7e51SEric Biggers  *	   errors are possible, so callers that always pass a valid key length
53*309a7e51SEric Biggers  *	   don't need to check for errors.
54*309a7e51SEric Biggers  */
55*309a7e51SEric Biggers int aes_cmac_preparekey(struct aes_cmac_key *key, const u8 *in_key,
56*309a7e51SEric Biggers 			size_t key_len);
57*309a7e51SEric Biggers 
58*309a7e51SEric Biggers /**
59*309a7e51SEric Biggers  * aes_xcbcmac_preparekey() - Prepare a key for AES-XCBC-MAC
60*309a7e51SEric Biggers  * @key: (output) The key struct to initialize
61*309a7e51SEric Biggers  * @in_key: The raw key.  As per the AES-XCBC-MAC specification (RFC 3566), this
62*309a7e51SEric Biggers  *	    is 128 bits, matching the internal use of AES-128.
63*309a7e51SEric Biggers  *
64*309a7e51SEric Biggers  * AES-XCBC-MAC and AES-CMAC are the same except for the key preparation.  After
65*309a7e51SEric Biggers  * that step, AES-XCBC-MAC is supported via the aes_cmac_* functions.
66*309a7e51SEric Biggers  *
67*309a7e51SEric Biggers  * New users should use AES-CMAC instead of AES-XCBC-MAC.
68*309a7e51SEric Biggers  *
69*309a7e51SEric Biggers  * Context: Any context.
70*309a7e51SEric Biggers  */
71*309a7e51SEric Biggers void aes_xcbcmac_preparekey(struct aes_cmac_key *key,
72*309a7e51SEric Biggers 			    const u8 in_key[at_least AES_KEYSIZE_128]);
73*309a7e51SEric Biggers 
74*309a7e51SEric Biggers /**
75*309a7e51SEric Biggers  * aes_cmac_init() - Start computing an AES-CMAC or AES-XCBC-MAC value
76*309a7e51SEric Biggers  * @ctx: (output) The context to initialize
77*309a7e51SEric Biggers  * @key: The key to use.  Note that a pointer to the key is saved in the
78*309a7e51SEric Biggers  *	 context, so the key must live at least as long as the context.
79*309a7e51SEric Biggers  *
80*309a7e51SEric Biggers  * This supports both AES-CMAC and AES-XCBC-MAC.  Which one is done depends on
81*309a7e51SEric Biggers  * whether aes_cmac_preparekey() or aes_xcbcmac_preparekey() was called.
82*309a7e51SEric Biggers  */
83*309a7e51SEric Biggers static inline void aes_cmac_init(struct aes_cmac_ctx *ctx,
84*309a7e51SEric Biggers 				 const struct aes_cmac_key *key)
85*309a7e51SEric Biggers {
86*309a7e51SEric Biggers 	*ctx = (struct aes_cmac_ctx){ .key = key };
87*309a7e51SEric Biggers }
88*309a7e51SEric Biggers 
89*309a7e51SEric Biggers /**
90*309a7e51SEric Biggers  * aes_cmac_update() - Update an AES-CMAC or AES-XCBC-MAC context with more data
91*309a7e51SEric Biggers  * @ctx: The context to update; must have been initialized
92*309a7e51SEric Biggers  * @data: The message data
93*309a7e51SEric Biggers  * @data_len: The data length in bytes.  Doesn't need to be block-aligned.
94*309a7e51SEric Biggers  *
95*309a7e51SEric Biggers  * This can be called any number of times.
96*309a7e51SEric Biggers  *
97*309a7e51SEric Biggers  * Context: Any context.
98*309a7e51SEric Biggers  */
99*309a7e51SEric Biggers void aes_cmac_update(struct aes_cmac_ctx *ctx, const u8 *data, size_t data_len);
100*309a7e51SEric Biggers 
101*309a7e51SEric Biggers /**
102*309a7e51SEric Biggers  * aes_cmac_final() - Finish computing an AES-CMAC or AES-XCBC-MAC value
103*309a7e51SEric Biggers  * @ctx: The context to finalize; must have been initialized
104*309a7e51SEric Biggers  * @out: (output) The resulting MAC
105*309a7e51SEric Biggers  *
106*309a7e51SEric Biggers  * After finishing, this zeroizes @ctx.  So the caller does not need to do it.
107*309a7e51SEric Biggers  *
108*309a7e51SEric Biggers  * Context: Any context.
109*309a7e51SEric Biggers  */
110*309a7e51SEric Biggers void aes_cmac_final(struct aes_cmac_ctx *ctx, u8 out[at_least AES_BLOCK_SIZE]);
111*309a7e51SEric Biggers 
112*309a7e51SEric Biggers /**
113*309a7e51SEric Biggers  * aes_cmac() - Compute AES-CMAC or AES-XCBC-MAC in one shot
114*309a7e51SEric Biggers  * @key: The key to use
115*309a7e51SEric Biggers  * @data: The message data
116*309a7e51SEric Biggers  * @data_len: The data length in bytes
117*309a7e51SEric Biggers  * @out: (output) The resulting AES-CMAC or AES-XCBC-MAC value
118*309a7e51SEric Biggers  *
119*309a7e51SEric Biggers  * This supports both AES-CMAC and AES-XCBC-MAC.  Which one is done depends on
120*309a7e51SEric Biggers  * whether aes_cmac_preparekey() or aes_xcbcmac_preparekey() was called.
121*309a7e51SEric Biggers  *
122*309a7e51SEric Biggers  * Context: Any context.
123*309a7e51SEric Biggers  */
124*309a7e51SEric Biggers static inline void aes_cmac(const struct aes_cmac_key *key, const u8 *data,
125*309a7e51SEric Biggers 			    size_t data_len, u8 out[at_least AES_BLOCK_SIZE])
126*309a7e51SEric Biggers {
127*309a7e51SEric Biggers 	struct aes_cmac_ctx ctx;
128*309a7e51SEric Biggers 
129*309a7e51SEric Biggers 	aes_cmac_init(&ctx, key);
130*309a7e51SEric Biggers 	aes_cmac_update(&ctx, data, data_len);
131*309a7e51SEric Biggers 	aes_cmac_final(&ctx, out);
132*309a7e51SEric Biggers }
133*309a7e51SEric Biggers 
134*309a7e51SEric Biggers /*
135*309a7e51SEric Biggers  * AES-CBC-MAC support.  This is provided only for use by the implementation of
136*309a7e51SEric Biggers  * AES-CCM.  It should have no other users.  Warning: unlike AES-CMAC and
137*309a7e51SEric Biggers  * AES-XCBC-MAC, AES-CBC-MAC isn't a secure MAC for variable-length messages.
138*309a7e51SEric Biggers  */
139*309a7e51SEric Biggers struct aes_cbcmac_ctx {
140*309a7e51SEric Biggers 	const struct aes_enckey *key;
141*309a7e51SEric Biggers 	size_t partial_len;
142*309a7e51SEric Biggers 	u8 h[AES_BLOCK_SIZE];
143*309a7e51SEric Biggers };
144*309a7e51SEric Biggers static inline void aes_cbcmac_init(struct aes_cbcmac_ctx *ctx,
145*309a7e51SEric Biggers 				   const struct aes_enckey *key)
146*309a7e51SEric Biggers {
147*309a7e51SEric Biggers 	*ctx = (struct aes_cbcmac_ctx){ .key = key };
148*309a7e51SEric Biggers }
149*309a7e51SEric Biggers void aes_cbcmac_update(struct aes_cbcmac_ctx *ctx, const u8 *data,
150*309a7e51SEric Biggers 		       size_t data_len);
151*309a7e51SEric Biggers void aes_cbcmac_final(struct aes_cbcmac_ctx *ctx,
152*309a7e51SEric Biggers 		      u8 out[at_least AES_BLOCK_SIZE]);
153*309a7e51SEric Biggers 
154*309a7e51SEric Biggers #endif /* _CRYPTO_AES_CBC_MACS_H */
155