1*2a87486bSEric Biggers /* SPDX-License-Identifier: GPL-2.0 */ 2*2a87486bSEric Biggers /* 3*2a87486bSEric Biggers * AES-GCM authenticated encryption and decryption 4*2a87486bSEric Biggers * 5*2a87486bSEric Biggers * Copyright 2026 Google LLC 6*2a87486bSEric Biggers */ 7*2a87486bSEric Biggers #ifndef _CRYPTO_AES_GCM_H 8*2a87486bSEric Biggers #define _CRYPTO_AES_GCM_H 9*2a87486bSEric Biggers 10*2a87486bSEric Biggers #include <crypto/aes.h> 11*2a87486bSEric Biggers #include <crypto/gcm.h> 12*2a87486bSEric Biggers #include <crypto/gf128hash.h> 13*2a87486bSEric Biggers 14*2a87486bSEric Biggers /** 15*2a87486bSEric Biggers * struct aes_gcm_key - A key prepared for AES-GCM encryption and decryption 16*2a87486bSEric Biggers */ 17*2a87486bSEric Biggers struct aes_gcm_key { 18*2a87486bSEric Biggers /* private: */ 19*2a87486bSEric Biggers struct aes_enckey aes; 20*2a87486bSEric Biggers struct ghash_key ghash; 21*2a87486bSEric Biggers size_t authtag_len; /* Length of authentication tags in bytes */ 22*2a87486bSEric Biggers }; 23*2a87486bSEric Biggers 24*2a87486bSEric Biggers /** 25*2a87486bSEric Biggers * struct aes_gcm_ctx - Context for incrementally en/decrypting a message 26*2a87486bSEric Biggers */ 27*2a87486bSEric Biggers struct aes_gcm_ctx { 28*2a87486bSEric Biggers /* private: */ 29*2a87486bSEric Biggers /* 30*2a87486bSEric Biggers * Pointer to the key, which is assumed to live at least as long as this 31*2a87486bSEric Biggers * struct. 32*2a87486bSEric Biggers */ 33*2a87486bSEric Biggers const struct aes_gcm_key *key; 34*2a87486bSEric Biggers /* The current GHASH context */ 35*2a87486bSEric Biggers struct ghash_ctx ghash; 36*2a87486bSEric Biggers /* 37*2a87486bSEric Biggers * The current counter. This can be viewed as either a 128-bit big 38*2a87486bSEric Biggers * endian counter, or as a 96-bit nonce followed by a 32-bit big endian 39*2a87486bSEric Biggers * counter; it doesn't matter, since the last 32-bit word starts at 1, 40*2a87486bSEric Biggers * and AES-GCM is undefined for messages that would overflow that part. 41*2a87486bSEric Biggers * In practice this means that code optimized for AES-GCM can just 42*2a87486bSEric Biggers * increment the last 32-bit word (wrapping at 2^32), but when needed it 43*2a87486bSEric Biggers * can still call AES-CTR code that does a 128-bit increment. 44*2a87486bSEric Biggers * 45*2a87486bSEric Biggers * 'long' alignment is for crypto_xor() to work more efficiently. 46*2a87486bSEric Biggers */ 47*2a87486bSEric Biggers union { 48*2a87486bSEric Biggers u8 ctr[AES_BLOCK_SIZE]; 49*2a87486bSEric Biggers __be32 ctr32[AES_BLOCK_SIZE / 4]; 50*2a87486bSEric Biggers } __aligned(__alignof__(long)); 51*2a87486bSEric Biggers /* Buffered keystream for partial block updates */ 52*2a87486bSEric Biggers u8 keystream[AES_BLOCK_SIZE] __aligned(__alignof__(long)); 53*2a87486bSEric Biggers /* Encrypted counter of 1. This gets XOR'ed with the tag at the end. */ 54*2a87486bSEric Biggers u8 j0_enc[AES_BLOCK_SIZE] __aligned(__alignof__(long)); 55*2a87486bSEric Biggers /* Number of associated data bytes processed so far */ 56*2a87486bSEric Biggers u64 ad_len; 57*2a87486bSEric Biggers /* Number of en/decrypted bytes processed so far */ 58*2a87486bSEric Biggers u64 data_len; 59*2a87486bSEric Biggers }; 60*2a87486bSEric Biggers 61*2a87486bSEric Biggers /** 62*2a87486bSEric Biggers * aes_gcm_preparekey() - Prepare a key for AES-GCM encryption and decryption 63*2a87486bSEric Biggers * @key: (output) The key structure to initialize 64*2a87486bSEric Biggers * @in_key: The raw AES-GCM key 65*2a87486bSEric Biggers * @key_len: Length of the raw key in bytes: 16, 24, or 32 66*2a87486bSEric Biggers * @authtag_len: Length of the authentication tag in bytes: 67*2a87486bSEric Biggers * 4, 8, 12, 13, 14, 15, or 16. 16 is recommended. 68*2a87486bSEric Biggers * 69*2a87486bSEric Biggers * Users should use memzero_explicit() to zeroize the key struct at the end of 70*2a87486bSEric Biggers * its lifetime. (But if this function fails, zeroization is unnecessary.) 71*2a87486bSEric Biggers * 72*2a87486bSEric Biggers * Context: Any context. 73*2a87486bSEric Biggers * Return: 74*2a87486bSEric Biggers * * 0 on success 75*2a87486bSEric Biggers * * -EINVAL if either of the lengths is invalid 76*2a87486bSEric Biggers */ 77*2a87486bSEric Biggers int __must_check aes_gcm_preparekey(struct aes_gcm_key *key, const u8 *in_key, 78*2a87486bSEric Biggers size_t key_len, size_t authtag_len); 79*2a87486bSEric Biggers 80*2a87486bSEric Biggers /** 81*2a87486bSEric Biggers * aes_gcm_encrypt() - Encrypt a message with AES-GCM 82*2a87486bSEric Biggers * @dst: The destination ciphertext data. Can be in-place or out-of-place. 83*2a87486bSEric Biggers * For other overlaps the behavior is unspecified. 84*2a87486bSEric Biggers * @src: The source plaintext data 85*2a87486bSEric Biggers * @data_len: Length of plaintext in bytes (and ciphertext excluding the tag): 86*2a87486bSEric Biggers * at most 2^36 - 32 87*2a87486bSEric Biggers * @authtag: The output authentication tag. Length is the authtag_len that was 88*2a87486bSEric Biggers * passed to aes_gcm_preparekey(). Usually protocols using AES-GCM 89*2a87486bSEric Biggers * put the tag at the end of the ciphertext, in which case this should 90*2a87486bSEric Biggers * be set to @dst + @data_len and @dst must have room for the tag. 91*2a87486bSEric Biggers * @ad: The associated data 92*2a87486bSEric Biggers * @ad_len: Length of associated data in bytes: at most 2^61 - 1 93*2a87486bSEric Biggers * @nonce: The 12-byte nonce. All (key, nonce) pairs used MUST be distinct. 94*2a87486bSEric Biggers * @key: The key, already prepared using aes_gcm_preparekey() 95*2a87486bSEric Biggers * 96*2a87486bSEric Biggers * For AES-GMAC (i.e., AES-GCM without any data en/decrypted), use dst=NULL, 97*2a87486bSEric Biggers * src=NULL, and data_len=0 to generate the AES-GMAC value. 98*2a87486bSEric Biggers * 99*2a87486bSEric Biggers * Context: Any context. 100*2a87486bSEric Biggers */ 101*2a87486bSEric Biggers void aes_gcm_encrypt(u8 *dst, const u8 *src, size_t data_len, u8 *authtag, 102*2a87486bSEric Biggers const u8 *ad, size_t ad_len, const u8 nonce[at_least 12], 103*2a87486bSEric Biggers const struct aes_gcm_key *key); 104*2a87486bSEric Biggers 105*2a87486bSEric Biggers /** 106*2a87486bSEric Biggers * aes_gcm_decrypt() - Decrypt a message with AES-GCM 107*2a87486bSEric Biggers * @dst: The destination plaintext data. Can be in-place or out-of-place. 108*2a87486bSEric Biggers * For other overlaps the behavior is unspecified. 109*2a87486bSEric Biggers * @src: The source ciphertext data 110*2a87486bSEric Biggers * @data_len: Length of plaintext in bytes (and ciphertext excluding the tag): 111*2a87486bSEric Biggers * at most 2^36 - 32 112*2a87486bSEric Biggers * @authtag: The stored authentication tag. Length is the authtag_len that was 113*2a87486bSEric Biggers * passed to aes_gcm_preparekey(). Usually protocols using AES-GCM 114*2a87486bSEric Biggers * put the tag at the end of the ciphertext, in which case this should 115*2a87486bSEric Biggers * be set to @src + @data_len and @src must have room for the tag. 116*2a87486bSEric Biggers * @ad: The associated data 117*2a87486bSEric Biggers * @ad_len: Length of associated data in bytes: at most 2^61 - 1 118*2a87486bSEric Biggers * @nonce: The 12-byte nonce 119*2a87486bSEric Biggers * @key: The key, already prepared using aes_gcm_preparekey() 120*2a87486bSEric Biggers * 121*2a87486bSEric Biggers * For AES-GMAC (i.e., AES-GCM without any data en/decrypted), use dst=NULL, 122*2a87486bSEric Biggers * src=NULL, and data_len=0 to verify the AES-GMAC value. 123*2a87486bSEric Biggers * 124*2a87486bSEric Biggers * Context: Any context. 125*2a87486bSEric Biggers * Return: 126*2a87486bSEric Biggers * * 0 on success. This is the only case where any decrypted or associated data 127*2a87486bSEric Biggers * can be used. 128*2a87486bSEric Biggers * * -EBADMSG if the message is inauthentic 129*2a87486bSEric Biggers */ 130*2a87486bSEric Biggers int __must_check aes_gcm_decrypt(u8 *dst, const u8 *src, size_t data_len, 131*2a87486bSEric Biggers const u8 *authtag, const u8 *ad, size_t ad_len, 132*2a87486bSEric Biggers const u8 nonce[at_least 12], 133*2a87486bSEric Biggers const struct aes_gcm_key *key); 134*2a87486bSEric Biggers 135*2a87486bSEric Biggers /** 136*2a87486bSEric Biggers * aes_gcm_init() - Initialize context for incremental AES-GCM encryption or 137*2a87486bSEric Biggers * decryption, or for AES-GMAC computation 138*2a87486bSEric Biggers * @ctx: The context to initialize 139*2a87486bSEric Biggers * @nonce: The 12-byte nonce. All (key, nonce) pairs used for encryption or MAC 140*2a87486bSEric Biggers * generation MUST be distinct. 141*2a87486bSEric Biggers * @key: The key, already prepared using aes_gcm_preparekey(). Note that a 142*2a87486bSEric Biggers * pointer to the key is saved in the context, so the key must live at 143*2a87486bSEric Biggers * least as long as the context. 144*2a87486bSEric Biggers * 145*2a87486bSEric Biggers * The context should be zeroized at the end of its lifetime. Normally that 146*2a87486bSEric Biggers * happens in aes_gcm_encrypt_final() or aes_gcm_decrypt_final(), but callers 147*2a87486bSEric Biggers * that abandon a context without finalizing it should explicitly zeroize it. 148*2a87486bSEric Biggers * 149*2a87486bSEric Biggers * IMPORTANT: Callers that are decrypting data or computing a GMAC value for 150*2a87486bSEric Biggers * verification MUST NOT assume that any decrypted or associated data is 151*2a87486bSEric Biggers * authentic until the authentication tag has been verified. This incremental 152*2a87486bSEric Biggers * API is provided solely to support callers that can't efficiently use the 153*2a87486bSEric Biggers * one-shot functions due to using a nonlinear data layout. 154*2a87486bSEric Biggers * 155*2a87486bSEric Biggers * For incremental AES-GCM encryption, use: 156*2a87486bSEric Biggers * 157*2a87486bSEric Biggers * 1. aes_gcm_init() 158*2a87486bSEric Biggers * 2. aes_gcm_auth_update() (any number of times) 159*2a87486bSEric Biggers * 3. aes_gcm_encrypt_update() (any number of times) 160*2a87486bSEric Biggers * 4. aes_gcm_encrypt_final() 161*2a87486bSEric Biggers * 162*2a87486bSEric Biggers * For incremental AES-GCM decryption, use: 163*2a87486bSEric Biggers * 164*2a87486bSEric Biggers * 1. aes_gcm_init() 165*2a87486bSEric Biggers * 2. aes_gcm_auth_update() (any number of times) 166*2a87486bSEric Biggers * 3. aes_gcm_decrypt_update() (any number of times) 167*2a87486bSEric Biggers * 4. aes_gcm_decrypt_final() 168*2a87486bSEric Biggers * 169*2a87486bSEric Biggers * AES-GMAC is just AES-GCM with zero bytes en/decrypted. For incremental 170*2a87486bSEric Biggers * AES-GMAC computation, use: 171*2a87486bSEric Biggers * 172*2a87486bSEric Biggers * 1. aes_gcm_init() 173*2a87486bSEric Biggers * 2. aes_gcm_auth_update() (any number of times) 174*2a87486bSEric Biggers * 3. aes_gcm_encrypt_final() to return the computed tag to the caller, or 175*2a87486bSEric Biggers * aes_gcm_decrypt_final() to directly verify the computed tag 176*2a87486bSEric Biggers * 177*2a87486bSEric Biggers * Context: Any context. 178*2a87486bSEric Biggers */ 179*2a87486bSEric Biggers void aes_gcm_init(struct aes_gcm_ctx *ctx, const u8 nonce[at_least 12], 180*2a87486bSEric Biggers const struct aes_gcm_key *key); 181*2a87486bSEric Biggers 182*2a87486bSEric Biggers /** 183*2a87486bSEric Biggers * aes_gcm_auth_update() - Incrementally process AES-GCM associated data 184*2a87486bSEric Biggers * @ctx: An AES-GCM context 185*2a87486bSEric Biggers * @ad: The associated data 186*2a87486bSEric Biggers * @len: Number of bytes provided. The caller must ensure that the total 187*2a87486bSEric Biggers * associated data length doesn't exceed GCM's limit of 2^61 - 1. 188*2a87486bSEric Biggers * 189*2a87486bSEric Biggers * IMPORTANT: Callers MUST NOT assume that any decrypted or associated data is 190*2a87486bSEric Biggers * authentic until the authentication tag has been verified. 191*2a87486bSEric Biggers * 192*2a87486bSEric Biggers * Context: Any context. 193*2a87486bSEric Biggers */ 194*2a87486bSEric Biggers void aes_gcm_auth_update(struct aes_gcm_ctx *ctx, const u8 *ad, size_t len); 195*2a87486bSEric Biggers 196*2a87486bSEric Biggers /** 197*2a87486bSEric Biggers * aes_gcm_encrypt_update() - Incrementally encrypt data with AES-GCM 198*2a87486bSEric Biggers * @ctx: An AES-GCM context 199*2a87486bSEric Biggers * @dst: The destination buffer. Can be in-place or out-of-place. For other 200*2a87486bSEric Biggers * overlaps the behavior is unspecified. 201*2a87486bSEric Biggers * @src: The source plaintext data 202*2a87486bSEric Biggers * @len: Number of bytes to encrypt. The caller must ensure that the total 203*2a87486bSEric Biggers * number of bytes encrypted doesn't exceed GCM's limit of 2^36 - 32. 204*2a87486bSEric Biggers * 205*2a87486bSEric Biggers * This can be called only after all associated data has been processed. 206*2a87486bSEric Biggers * 207*2a87486bSEric Biggers * Context: Any context. 208*2a87486bSEric Biggers */ 209*2a87486bSEric Biggers void aes_gcm_encrypt_update(struct aes_gcm_ctx *ctx, u8 *dst, const u8 *src, 210*2a87486bSEric Biggers size_t len); 211*2a87486bSEric Biggers 212*2a87486bSEric Biggers /** 213*2a87486bSEric Biggers * aes_gcm_decrypt_update() - Incrementally decrypt data with AES-GCM 214*2a87486bSEric Biggers * @ctx: An AES-GCM context 215*2a87486bSEric Biggers * @dst: The destination buffer. Can be in-place or out-of-place. For other 216*2a87486bSEric Biggers * overlaps the behavior is unspecified. 217*2a87486bSEric Biggers * @src: The source ciphertext data (not including auth tag) 218*2a87486bSEric Biggers * @len: Number of bytes to decrypt. The caller must ensure that the total 219*2a87486bSEric Biggers * number of bytes decrypted doesn't exceed GCM's limit of 2^36 - 32. 220*2a87486bSEric Biggers * 221*2a87486bSEric Biggers * This can be called only after all associated data has been processed. 222*2a87486bSEric Biggers * 223*2a87486bSEric Biggers * IMPORTANT: Callers MUST NOT assume that any decrypted or associated data is 224*2a87486bSEric Biggers * authentic until the authentication tag has been verified. 225*2a87486bSEric Biggers * 226*2a87486bSEric Biggers * Context: Any context. 227*2a87486bSEric Biggers */ 228*2a87486bSEric Biggers void aes_gcm_decrypt_update(struct aes_gcm_ctx *ctx, u8 *dst, const u8 *src, 229*2a87486bSEric Biggers size_t len); 230*2a87486bSEric Biggers 231*2a87486bSEric Biggers /** 232*2a87486bSEric Biggers * aes_gcm_encrypt_final() - Finish encrypting a message with AES-GCM 233*2a87486bSEric Biggers * @ctx: An AES-GCM context 234*2a87486bSEric Biggers * @authtag: The output authentication tag. Length is the authtag_len that was 235*2a87486bSEric Biggers * passed to aes_gcm_preparekey(). 236*2a87486bSEric Biggers * 237*2a87486bSEric Biggers * This also zeroizes @ctx, so the caller doesn't need to do it. 238*2a87486bSEric Biggers * 239*2a87486bSEric Biggers * Context: Any context. 240*2a87486bSEric Biggers */ 241*2a87486bSEric Biggers void aes_gcm_encrypt_final(struct aes_gcm_ctx *ctx, u8 *authtag); 242*2a87486bSEric Biggers 243*2a87486bSEric Biggers /** 244*2a87486bSEric Biggers * aes_gcm_decrypt_final() - Finish decrypting a message with AES-GCM 245*2a87486bSEric Biggers * @ctx: An AES-GCM context 246*2a87486bSEric Biggers * @authtag: The stored authentication tag. Length is the authtag_len that was 247*2a87486bSEric Biggers * passed to aes_gcm_preparekey(). 248*2a87486bSEric Biggers * 249*2a87486bSEric Biggers * This also zeroizes @ctx, so the caller doesn't need to do it. 250*2a87486bSEric Biggers * 251*2a87486bSEric Biggers * Context: Any context. 252*2a87486bSEric Biggers * Return: 253*2a87486bSEric Biggers * * 0 on success. This is the only case where any decrypted or associated data 254*2a87486bSEric Biggers * can be used. 255*2a87486bSEric Biggers * * -EBADMSG if the message is inauthentic 256*2a87486bSEric Biggers */ 257*2a87486bSEric Biggers int __must_check aes_gcm_decrypt_final(struct aes_gcm_ctx *ctx, 258*2a87486bSEric Biggers const u8 *authtag); 259*2a87486bSEric Biggers 260*2a87486bSEric Biggers #endif /* _CRYPTO_AES_GCM_H */ 261