| 1 |
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 |
/* SPDX-License-Identifier: Unlicense */ |
| 3 |
#include "tomcrypt_private.h" |
| 4 |
|
| 5 |
/** |
| 6 |
@file gcm_siv.c |
| 7 |
RFC 8452 - AES-GCM-SIV: Nonce Misuse-Resistant AEAD |
| 8 |
*/ |
| 9 |
|
| 10 |
#ifdef LTC_GCM_SIV_MODE |
| 11 |
|
| 12 |
/* Multiply by x in the GHASH field GF(2^128) defined by |
| 13 |
x^128 + x^7 + x^2 + x + 1, using GHASH's bit-byte ordering |
| 14 |
(bit 7 of byte 0 is x^0, bit 0 of byte 15 is x^127). This is used |
| 15 |
once during key derivation per RFC 8452 Appendix A: the POLYVAL |
| 16 |
authentication key is computed as mulX_GHASH(ByteReverse(H_polyval)) |
| 17 |
so that GHASH multiplication can be reused for POLYVAL. |
| 18 |
*/ |
| 19 |
static void s_mulx_ghash(unsigned char *b) |
| 20 |
{ |
| 21 |
unsigned int carry = b[15] & 1; |
| 22 |
int x; |
| 23 |
|
| 24 |
for (x = 15; x > 0; x--) { |
| 25 |
b[x] = (unsigned char)((b[x] >> 1) | ((b[x-1] & 1) << 7)); |
| 26 |
} |
| 27 |
b[0] >>= 1; |
| 28 |
if (carry) b[0] ^= 0xe1; |
| 29 |
} |
| 30 |
|
| 31 |
typedef struct { |
| 32 |
unsigned char H[16]; /* GHASH-form authentication key */ |
| 33 |
unsigned char S[16]; /* GHASH-form accumulator */ |
| 34 |
} polyval_state; |
| 35 |
|
| 36 |
static void s_polyval_init(polyval_state *st, const unsigned char *auth_key) |
| 37 |
{ |
| 38 |
int i; |
| 39 |
|
| 40 |
for (i = 0; i < 16; i++) st->H[i] = auth_key[15 - i]; |
| 41 |
s_mulx_ghash(st->H); |
| 42 |
zeromem(st->S, 16); |
| 43 |
} |
| 44 |
|
| 45 |
static void s_polyval_block(polyval_state *st, const unsigned char *X) |
| 46 |
{ |
| 47 |
unsigned char T[16]; |
| 48 |
int i; |
| 49 |
|
| 50 |
for (i = 0; i < 16; i++) st->S[i] ^= X[15 - i]; |
| 51 |
gcm_gf_mult(st->S, st->H, T); |
| 52 |
XMEMCPY(st->S, T, 16); |
| 53 |
} |
| 54 |
|
| 55 |
static void s_polyval_data(polyval_state *st, const unsigned char *data, unsigned long len) |
| 56 |
{ |
| 57 |
unsigned long full = len / 16; |
| 58 |
unsigned long rem = len % 16; |
| 59 |
unsigned long i; |
| 60 |
unsigned char block[16]; |
| 61 |
|
| 62 |
for (i = 0; i < full; i++) s_polyval_block(st, data + i * 16); |
| 63 |
if (rem) { |
| 64 |
zeromem(block, 16); |
| 65 |
XMEMCPY(block, data + full * 16, rem); |
| 66 |
s_polyval_block(st, block); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static void s_polyval_done(polyval_state *st, unsigned char *out) |
| 71 |
{ |
| 72 |
int i; |
| 73 |
for (i = 0; i < 16; i++) out[i] = st->S[15 - i]; |
| 74 |
} |
| 75 |
|
| 76 |
/* Derive the POLYVAL authentication key and the AES message-encryption key from K and N as specified |
| 77 |
in RFC 8452 Section 4. The cipher is scheduled with the message-encryption key in *enc_ecb on success |
| 78 |
*/ |
| 79 |
static int s_gcm_siv_derive_keys(int cipher, |
| 80 |
const unsigned char *K, unsigned long keylen, |
| 81 |
const unsigned char *N, |
| 82 |
unsigned char *auth_key, |
| 83 |
unsigned char *enc_key, |
| 84 |
symmetric_ECB *enc_ecb) |
| 85 |
{ |
| 86 |
int err; |
| 87 |
unsigned char input[16], block[16]; |
| 88 |
unsigned long ctr, num_blocks; |
| 89 |
symmetric_ECB ecb; |
| 90 |
|
| 91 |
if ((err = ecb_start(cipher, K, (int)keylen, 0, &ecb)) != CRYPT_OK) return err; |
| 92 |
|
| 93 |
XMEMCPY(input + 4, N, 12); |
| 94 |
|
| 95 |
/* counters 0..1 yield the 16-byte authentication key, counters 2.. yield the encryption key (16 or 32 bytes) */ |
| 96 |
num_blocks = (keylen == 16) ? 4 : 6; |
| 97 |
for (ctr = 0; ctr < num_blocks; ctr++) { |
| 98 |
STORE32L((ulong32)ctr, input); |
| 99 |
if ((err = ecb_encrypt_block(input, block, &ecb)) != CRYPT_OK) goto cleanup; |
| 100 |
if (ctr < 2) { |
| 101 |
XMEMCPY(auth_key + 8 * ctr, block, 8); |
| 102 |
} else { |
| 103 |
XMEMCPY(enc_key + 8 * (ctr - 2), block, 8); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
err = ecb_start(cipher, enc_key, (int)keylen, 0, enc_ecb); |
| 108 |
|
| 109 |
cleanup: |
| 110 |
ecb_done(&ecb); |
| 111 |
#ifdef LTC_CLEAN_STACK |
| 112 |
zeromem(input, sizeof(input)); |
| 113 |
zeromem(block, sizeof(block)); |
| 114 |
#endif |
| 115 |
return err; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
AES-GCM-SIV one-shot encryption/decryption (RFC 8452). |
| 120 |
|
| 121 |
@param cipher The index of the cipher (must be a 128-bit block cipher; AES per RFC) |
| 122 |
@param key The 16- or 32-byte key |
| 123 |
@param keylen The length of the key (16 or 32) |
| 124 |
@param nonce The 12-byte nonce |
| 125 |
@param noncelen The length of the nonce (must be 12) |
| 126 |
@param aad The associated data |
| 127 |
@param aadlen The length of the associated data |
| 128 |
@param in The input (plaintext on encrypt, ciphertext on decrypt) |
| 129 |
@param inlen The length of the input |
| 130 |
@param out The output (ciphertext on encrypt, plaintext on decrypt) |
| 131 |
@param tag [in/out] The 16-byte tag (output on encrypt, input on decrypt) |
| 132 |
@param taglen [in/out] The length of the tag (must be >= 16; set to 16 on output) |
| 133 |
@param direction LTC_ENCRYPT or LTC_DECRYPT |
| 134 |
@return CRYPT_OK on success, CRYPT_ERROR on tag mismatch during decrypt |
| 135 |
*/ |
| 136 |
int gcm_siv_memory( int cipher, |
| 137 |
const unsigned char *key, unsigned long keylen, |
| 138 |
const unsigned char *nonce, unsigned long noncelen, |
| 139 |
const unsigned char *aad, unsigned long aadlen, |
| 140 |
unsigned char *in, unsigned long inlen, |
| 141 |
unsigned char *out, |
| 142 |
unsigned char *tag, unsigned long *taglen, |
| 143 |
int direction) |
| 144 |
{ |
| 145 |
int err, ecb_started = 0, ctr_started = 0; |
| 146 |
unsigned char auth_key[16]; |
| 147 |
unsigned char enc_key[32]; |
| 148 |
unsigned char S[16], IC[16], expected_tag[16]; |
| 149 |
unsigned char lenblk[16]; |
| 150 |
const unsigned char *plaintext; |
| 151 |
symmetric_ECB enc_ecb; |
| 152 |
symmetric_CTR ctr; |
| 153 |
polyval_state pv; |
| 154 |
unsigned long i; |
| 155 |
|
| 156 |
LTC_ARGCHK(key != NULL); |
| 157 |
LTC_ARGCHK(nonce != NULL); |
| 158 |
LTC_ARGCHK(aad != NULL || aadlen == 0); |
| 159 |
LTC_ARGCHK(in != NULL || inlen == 0); |
| 160 |
LTC_ARGCHK(out != NULL || inlen == 0); |
| 161 |
LTC_ARGCHK(tag != NULL); |
| 162 |
LTC_ARGCHK(taglen != NULL); |
| 163 |
|
| 164 |
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) return err; |
| 165 |
if (cipher_descriptor[cipher].block_length != 16) return CRYPT_INVALID_CIPHER; |
| 166 |
if (keylen != 16 && keylen != 32) return CRYPT_INVALID_KEYSIZE; |
| 167 |
if (noncelen != 12) return CRYPT_INVALID_ARG; |
| 168 |
if (*taglen < 16) { |
| 169 |
*taglen = 16; |
| 170 |
return CRYPT_BUFFER_OVERFLOW; |
| 171 |
} |
| 172 |
|
| 173 |
if ((err = s_gcm_siv_derive_keys(cipher, key, keylen, nonce, auth_key, enc_key, &enc_ecb)) != CRYPT_OK) goto cleanup; |
| 174 |
ecb_started = 1; |
| 175 |
|
| 176 |
if (direction == LTC_DECRYPT) { |
| 177 |
/* CTR mode in RFC 8452: the supplied tag is the initial counter block with the MSB of the last |
| 178 |
byte forced to 1; only the first 4 bytes (interpreted as little-endian uint32) advance |
| 179 |
*/ |
| 180 |
XMEMCPY(IC, tag, 16); |
| 181 |
IC[15] |= 0x80; |
| 182 |
if ((err = ctr_start(cipher, IC, enc_key, (int)keylen, 0, CTR_COUNTER_LITTLE_ENDIAN | 4, &ctr)) != CRYPT_OK) goto cleanup; |
| 183 |
ctr_started = 1; |
| 184 |
if (inlen) { |
| 185 |
if ((err = ctr_decrypt(in, out, inlen, &ctr)) != CRYPT_OK) goto cleanup; |
| 186 |
} |
| 187 |
ctr_done(&ctr); |
| 188 |
ctr_started = 0; |
| 189 |
plaintext = out; |
| 190 |
} |
| 191 |
else { |
| 192 |
plaintext = in; |
| 193 |
} |
| 194 |
|
| 195 |
s_polyval_init(&pv, auth_key); |
| 196 |
s_polyval_data(&pv, aad, aadlen); |
| 197 |
s_polyval_data(&pv, plaintext, inlen); |
| 198 |
STORE64L((ulong64)aadlen * 8, lenblk); |
| 199 |
STORE64L((ulong64)inlen * 8, lenblk + 8); |
| 200 |
s_polyval_block(&pv, lenblk); |
| 201 |
s_polyval_done(&pv, S); |
| 202 |
|
| 203 |
for (i = 0; i < 12; i++) S[i] ^= nonce[i]; |
| 204 |
S[15] &= 0x7f; |
| 205 |
|
| 206 |
if ((err = ecb_encrypt_block(S, expected_tag, &enc_ecb)) != CRYPT_OK) goto cleanup; |
| 207 |
|
| 208 |
if (direction == LTC_DECRYPT) { |
| 209 |
err = XMEM_NEQ(expected_tag, tag, 16); |
| 210 |
if (err != CRYPT_OK) { |
| 211 |
if (inlen) zeromem(out, inlen); /* tag mismatch: do not release plaintext */ |
| 212 |
goto cleanup; |
| 213 |
} |
| 214 |
} |
| 215 |
else { |
| 216 |
XMEMCPY(IC, expected_tag, 16); |
| 217 |
IC[15] |= 0x80; |
| 218 |
if ((err = ctr_start(cipher, IC, enc_key, (int)keylen, 0, CTR_COUNTER_LITTLE_ENDIAN | 4, &ctr)) != CRYPT_OK) { |
| 219 |
goto cleanup; |
| 220 |
} |
| 221 |
ctr_started = 1; |
| 222 |
if (inlen) { |
| 223 |
if ((err = ctr_encrypt(in, out, inlen, &ctr)) != CRYPT_OK) goto cleanup; |
| 224 |
} |
| 225 |
XMEMCPY(tag, expected_tag, 16); |
| 226 |
*taglen = 16; |
| 227 |
} |
| 228 |
|
| 229 |
cleanup: |
| 230 |
if (ctr_started) ctr_done(&ctr); |
| 231 |
if (ecb_started) ecb_done(&enc_ecb); |
| 232 |
#ifdef LTC_CLEAN_STACK |
| 233 |
zeromem(auth_key, sizeof(auth_key)); |
| 234 |
zeromem(enc_key, sizeof(enc_key)); |
| 235 |
zeromem(S, sizeof(S)); |
| 236 |
zeromem(IC, sizeof(IC)); |
| 237 |
zeromem(expected_tag, sizeof(expected_tag)); |
| 238 |
zeromem(lenblk, sizeof(lenblk)); |
| 239 |
zeromem(&pv, sizeof(pv)); |
| 240 |
#endif |
| 241 |
return err; |
| 242 |
} |
| 243 |
|
| 244 |
#endif |