|
| 1 | +/* LibTomCrypt, modular cryptographic library -- Tom St Denis */ |
| 2 | +/* SPDX-License-Identifier: Unlicense */ |
| 3 | +#include "tomcrypt_private.h" |
| 4 | + |
| 5 | +/** |
| 6 | + @file siv.c |
| 7 | + RFC 5297 SIV - Synthetic Initialization Vector, Steffen Jaeckel |
| 8 | +*/ |
| 9 | + |
| 10 | +#ifdef LTC_SIV_MODE |
| 11 | + |
| 12 | +static void s_siv_dbl(unsigned char *inout) |
| 13 | +{ |
| 14 | + int y, mask, msb, len; |
| 15 | + |
| 16 | + /* setup the system */ |
| 17 | + mask = 0x87; |
| 18 | + len = 16; |
| 19 | + |
| 20 | + /* if msb(L * u^(x+1)) = 0 then just shift, otherwise shift and xor constant mask */ |
| 21 | + msb = inout[0] >> 7; |
| 22 | + |
| 23 | + /* shift left */ |
| 24 | + for (y = 0; y < (len - 1); y++) { |
| 25 | + inout[y] = ((inout[y] << 1) | (inout[y + 1] >> 7)) & 255; |
| 26 | + } |
| 27 | + inout[len - 1] = ((inout[len - 1] << 1) ^ (msb ? mask : 0)) & 255; |
| 28 | +} |
| 29 | + |
| 30 | +static int s_siv_S2V(int cipher, |
| 31 | + const unsigned char *key, unsigned long keylen, |
| 32 | + const unsigned char **ad, unsigned long *adlen, |
| 33 | + const unsigned char *in, unsigned long inlen, |
| 34 | + unsigned char *V, unsigned long *Vlen) |
| 35 | +{ |
| 36 | + int err, n; |
| 37 | + unsigned long Dlen, TMPlen, Tlen, i, j; |
| 38 | + unsigned char D[16], TMP[16], *T; |
| 39 | + unsigned char zero_or_one[16] = {0}; |
| 40 | + |
| 41 | + if(ad == NULL || adlen == NULL || ad[0] == NULL || adlen[0] == 0) { |
| 42 | + /* if n = 0 then |
| 43 | + * return V = AES-CMAC(K, <one>) |
| 44 | + */ |
| 45 | + zero_or_one[0] = 1; |
| 46 | + err = omac_memory(cipher, key, keylen, zero_or_one, sizeof(zero_or_one), V, Vlen); |
| 47 | + } else { |
| 48 | + /* D = AES-CMAC(K, <zero>) */ |
| 49 | + Dlen = sizeof(D); |
| 50 | + if ((err = omac_memory(cipher, key, keylen, zero_or_one, sizeof(zero_or_one), D, &Dlen)) != CRYPT_OK) { |
| 51 | + return err; |
| 52 | + } |
| 53 | + /* for i = 1 to n-1 do |
| 54 | + * D = dbl(D) xor AES-CMAC(K, Si) |
| 55 | + * done |
| 56 | + */ |
| 57 | + n = 0; |
| 58 | + while(ad[n] != NULL && adlen[n] != 0) { |
| 59 | + s_siv_dbl(D); |
| 60 | + TMPlen = sizeof(TMP); |
| 61 | + if ((err = omac_memory(cipher, key, keylen, ad[n], adlen[n], TMP, &TMPlen)) != CRYPT_OK) { |
| 62 | + return err; |
| 63 | + } |
| 64 | + for (i = 0; i < sizeof(D); ++i) { |
| 65 | + D[i] ^= TMP[i]; |
| 66 | + } |
| 67 | + n++; |
| 68 | + } |
| 69 | + /* if len(Sn) >= 128 then |
| 70 | + * T = Sn xorend D |
| 71 | + * else |
| 72 | + * T = dbl(D) xor pad(Sn) |
| 73 | + * fi |
| 74 | + */ |
| 75 | + Tlen = inlen >= 16 ? inlen : 16; |
| 76 | + T = XMALLOC(Tlen); |
| 77 | + if (T == NULL) { |
| 78 | + return CRYPT_MEM; |
| 79 | + } |
| 80 | + XMEMCPY(T, in, inlen); |
| 81 | + if (inlen >= 16) { |
| 82 | + for(i = inlen - 16, j = 0; i < inlen; ++i, ++j) { |
| 83 | + T[i] = D[j] ^ T[i]; |
| 84 | + } |
| 85 | + } else { |
| 86 | + s_siv_dbl(D); |
| 87 | + T[inlen] = 0x80; |
| 88 | + for (i = inlen + 1; i < 16; ++i) { |
| 89 | + T[i] = 0x0; |
| 90 | + } |
| 91 | + for(i = 0; i < Tlen; ++i) { |
| 92 | + T[i] ^= D[i]; |
| 93 | + } |
| 94 | + } |
| 95 | + err = omac_memory(cipher, key, keylen, T, Tlen, V, Vlen); |
| 96 | +#ifdef LTC_CLEAN_STACK |
| 97 | + zeromem(T, Tlen); |
| 98 | +#endif |
| 99 | + XFREE(T); |
| 100 | + } |
| 101 | + |
| 102 | + return err; |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +static void s_siv_bitand(const unsigned char* V, unsigned char* Q) |
| 107 | +{ |
| 108 | + int n; |
| 109 | + XMEMSET(Q, 0xff, 16); |
| 110 | + Q[8] = Q[12] = 0x7f; |
| 111 | + for (n = 0; n < 16; ++n) { |
| 112 | + Q[n] &= V[n]; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +typedef struct { |
| 118 | + unsigned char V[16]; |
| 119 | + symmetric_CTR ctr; |
| 120 | +} siv_state; |
| 121 | + |
| 122 | +/** |
| 123 | + SIV encrypt |
| 124 | +
|
| 125 | + @param cipher The index of the cipher desired |
| 126 | + @param key The secret key to use |
| 127 | + @param keylen The length of the secret key (octets) |
| 128 | + @param ad An array of Associated Data pointers (must be NULL terminated) |
| 129 | + @param adlen An array with the lengths of the Associated Data |
| 130 | + @param pt The plaintext |
| 131 | + @param ptlen The length of the plaintext |
| 132 | + @param ct The ciphertext |
| 133 | + @param ctlen [in/out] The length of the ciphertext |
| 134 | + @return CRYPT_OK if successful |
| 135 | +*/ |
| 136 | +int siv_encrypt(int cipher, |
| 137 | + const unsigned char *key, unsigned long keylen, |
| 138 | + const unsigned char **ad, unsigned long *adlen, |
| 139 | + const unsigned char *pt, unsigned long ptlen, |
| 140 | + unsigned char *ct, unsigned long *ctlen) |
| 141 | +{ |
| 142 | + int err; |
| 143 | + unsigned char Q[16]; |
| 144 | + const unsigned char *K1, *K2; |
| 145 | + unsigned long Vlen; |
| 146 | + siv_state siv; |
| 147 | + |
| 148 | + LTC_ARGCHK(key != NULL); |
| 149 | + LTC_ARGCHK(ad != NULL); |
| 150 | + LTC_ARGCHK(adlen != NULL); |
| 151 | + LTC_ARGCHK(pt != NULL); |
| 152 | + LTC_ARGCHK(ct != NULL); |
| 153 | + LTC_ARGCHK(ctlen != NULL); |
| 154 | + |
| 155 | + if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { |
| 156 | + return err; |
| 157 | + } |
| 158 | + if (*ctlen < ptlen + 16) { |
| 159 | + return CRYPT_BUFFER_OVERFLOW; |
| 160 | + } |
| 161 | + |
| 162 | + K1 = key; |
| 163 | + K2 = &key[keylen/2]; |
| 164 | + |
| 165 | + Vlen = sizeof(siv.V); |
| 166 | + err = s_siv_S2V(cipher, K1, keylen/2, ad, adlen, pt, ptlen, siv.V, &Vlen); |
| 167 | +#ifdef LTC_CLEAN_STACK |
| 168 | + burn_stack(3 * 16 + 7 * sizeof(unsigned long) + 1 * sizeof(void*)); |
| 169 | +#endif |
| 170 | + if (err != CRYPT_OK) { |
| 171 | + return err; |
| 172 | + } |
| 173 | + s_siv_bitand(siv.V, Q); |
| 174 | + err = ctr_start(cipher, Q, K2, keylen/2, 0, CTR_COUNTER_BIG_ENDIAN | 16, &siv.ctr); |
| 175 | + if (err != CRYPT_OK) { |
| 176 | + goto out; |
| 177 | + } |
| 178 | + XMEMCPY(ct, siv.V, 16); |
| 179 | + ct += 16; |
| 180 | + err = ctr_encrypt(pt, ct, ptlen, &siv.ctr); |
| 181 | + if (err != CRYPT_OK) { |
| 182 | + zeromem(ct, ptlen + 16); |
| 183 | + } else { |
| 184 | + *ctlen = ptlen + 16; |
| 185 | + } |
| 186 | + ctr_done(&siv.ctr); |
| 187 | + |
| 188 | +out: |
| 189 | +#ifdef LTC_CLEAN_STACK |
| 190 | + zeromem(Q, sizeof(Q)); |
| 191 | + zeromem(&siv, sizeof(siv)); |
| 192 | +#endif |
| 193 | + |
| 194 | + return err; |
| 195 | +} |
| 196 | + |
| 197 | +/** |
| 198 | + SIV decrypt |
| 199 | +
|
| 200 | + @param cipher The index of the cipher desired |
| 201 | + @param key The secret key to use |
| 202 | + @param keylen The length of the secret key (octets) |
| 203 | + @param ad An array of Associated Data pointers (must be NULL terminated) |
| 204 | + @param adlen An array with the lengths of the Associated Data |
| 205 | + @param ct The ciphertext |
| 206 | + @param ctlen The length of the ciphertext |
| 207 | + @param pt The plaintext |
| 208 | + @param ptlen [in/out] The length of the plaintext |
| 209 | + @return CRYPT_OK if successful |
| 210 | +*/ |
| 211 | +int siv_decrypt(int cipher, |
| 212 | + const unsigned char *key, unsigned long keylen, |
| 213 | + const unsigned char **ad, unsigned long *adlen, |
| 214 | + const unsigned char *ct, unsigned long ctlen, |
| 215 | + unsigned char *pt, unsigned long *ptlen) |
| 216 | +{ |
| 217 | + int err; |
| 218 | + unsigned char Q[16], *pt_work; |
| 219 | + const unsigned char *K1, *K2, *V; |
| 220 | + unsigned long Vlen; |
| 221 | + siv_state siv; |
| 222 | + |
| 223 | + LTC_ARGCHK(key != NULL); |
| 224 | + LTC_ARGCHK(ad != NULL); |
| 225 | + LTC_ARGCHK(adlen != NULL); |
| 226 | + LTC_ARGCHK(ct != NULL); |
| 227 | + LTC_ARGCHK(pt != NULL); |
| 228 | + LTC_ARGCHK(ptlen != NULL); |
| 229 | + |
| 230 | + if ((err = cipher_is_valid(cipher)) != CRYPT_OK) { |
| 231 | + return err; |
| 232 | + } |
| 233 | + if (*ptlen < ctlen || ctlen < 16) { |
| 234 | + return CRYPT_BUFFER_OVERFLOW; |
| 235 | + } |
| 236 | + |
| 237 | + *ptlen = ctlen - 16; |
| 238 | + pt_work = XMALLOC(*ptlen); |
| 239 | + if (pt_work == NULL) { |
| 240 | + return CRYPT_MEM; |
| 241 | + } |
| 242 | + |
| 243 | + K1 = key; |
| 244 | + K2 = &key[keylen/2]; |
| 245 | + |
| 246 | + V = ct; |
| 247 | + s_siv_bitand(V, Q); |
| 248 | + ct += 16; |
| 249 | + |
| 250 | + err = ctr_start(cipher, Q, K2, keylen/2, 0, CTR_COUNTER_BIG_ENDIAN | 16, &siv.ctr); |
| 251 | + if (err != CRYPT_OK) { |
| 252 | + goto out; |
| 253 | + } |
| 254 | + err = ctr_decrypt(ct, pt_work, *ptlen, &siv.ctr); |
| 255 | + if (err != CRYPT_OK) { |
| 256 | + goto out; |
| 257 | + } |
| 258 | + Vlen = sizeof(siv.V); |
| 259 | + err = s_siv_S2V(cipher, K1, keylen/2, ad, adlen, pt_work, *ptlen, siv.V, &Vlen); |
| 260 | +#ifdef LTC_CLEAN_STACK |
| 261 | + burn_stack(3 * 16 + 7 * sizeof(unsigned long) + 1 * sizeof(void*)); |
| 262 | +#endif |
| 263 | + if (err != CRYPT_OK) { |
| 264 | + goto out; |
| 265 | + } |
| 266 | + |
| 267 | + err = XMEM_NEQ(siv.V, V, Vlen); |
| 268 | + copy_or_zeromem(pt_work, pt, *ptlen, err); |
| 269 | +out: |
| 270 | +#ifdef LTC_CLEAN_STACK |
| 271 | + zeromem(Q, sizeof(Q)); |
| 272 | + zeromem(&siv, sizeof(siv)); |
| 273 | + zeromem(pt_work, *ptlen); |
| 274 | +#endif |
| 275 | + XFREE(pt_work); |
| 276 | + |
| 277 | + return err; |
| 278 | +} |
| 279 | + |
| 280 | +int siv_test(void) |
| 281 | +{ |
| 282 | + /* |
| 283 | + * RFC5297 - A.1. Deterministic Authenticated Encryption Example |
| 284 | + */ |
| 285 | + const unsigned char Key_A1[] = |
| 286 | + { 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, |
| 287 | + 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, |
| 288 | + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, |
| 289 | + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; |
| 290 | + const unsigned char AD_A1[] = |
| 291 | + { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 292 | + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 293 | + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 }; |
| 294 | + const unsigned char Plaintext_A1[] = |
| 295 | + { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, |
| 296 | + 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee }; |
| 297 | + const unsigned char output_A1[] = |
| 298 | + { 0x85, 0x63, 0x2d, 0x07, 0xc6, 0xe8, 0xf3, 0x7f, |
| 299 | + 0x95, 0x0a, 0xcd, 0x32, 0x0a, 0x2e, 0xcc, 0x93, |
| 300 | + 0x40, 0xc0, 0x2b, 0x96, 0x90, 0xc4, 0xdc, 0x04, |
| 301 | + 0xda, 0xef, 0x7f, 0x6a, 0xfe, 0x5c }; |
| 302 | + const unsigned char *ad_A1[] = |
| 303 | + { AD_A1, NULL }; |
| 304 | + unsigned long adlen_A1[] = |
| 305 | + { sizeof(AD_A1), 0 }; |
| 306 | + |
| 307 | + const unsigned char Key_A2[] = |
| 308 | + { 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, |
| 309 | + 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70, |
| 310 | + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, |
| 311 | + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f }; |
| 312 | + const unsigned char AD1_A2[] = |
| 313 | + { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, |
| 314 | + 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
| 315 | + 0xde, 0xad, 0xda, 0xda, 0xde, 0xad, 0xda, 0xda, |
| 316 | + 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, |
| 317 | + 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 }; |
| 318 | + const unsigned char AD2_A2[] = |
| 319 | + { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, |
| 320 | + 0x90, 0xa0 }; |
| 321 | + const unsigned char AD3_A2[] = |
| 322 | + { 0x09, 0xf9, 0x11, 0x02, 0x9d, 0x74, 0xe3, 0x5b, |
| 323 | + 0xd8, 0x41, 0x56, 0xc5, 0x63, 0x56, 0x88, 0xc0 }; |
| 324 | + const unsigned char Plaintext_A2[] = |
| 325 | + { 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, |
| 326 | + 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x6c, 0x61, |
| 327 | + 0x69, 0x6e, 0x74, 0x65, 0x78, 0x74, 0x20, 0x74, |
| 328 | + 0x6f, 0x20, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, |
| 329 | + 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, |
| 330 | + 0x53, 0x49, 0x56, 0x2d, 0x41, 0x45, 0x53 }; |
| 331 | + const unsigned char output_A2[] = |
| 332 | + { 0x7b, 0xdb, 0x6e, 0x3b, 0x43, 0x26, 0x67, 0xeb, |
| 333 | + 0x06, 0xf4, 0xd1, 0x4b, 0xff, 0x2f, 0xbd, 0x0f, |
| 334 | + 0xcb, 0x90, 0x0f, 0x2f, 0xdd, 0xbe, 0x40, 0x43, |
| 335 | + 0x26, 0x60, 0x19, 0x65, 0xc8, 0x89, 0xbf, 0x17, |
| 336 | + 0xdb, 0xa7, 0x7c, 0xeb, 0x09, 0x4f, 0xa6, 0x63, |
| 337 | + 0xb7, 0xa3, 0xf7, 0x48, 0xba, 0x8a, 0xf8, 0x29, |
| 338 | + 0xea, 0x64, 0xad, 0x54, 0x4a, 0x27, 0x2e, 0x9c, |
| 339 | + 0x48, 0x5b, 0x62, 0xa3, 0xfd, 0x5c, 0x0d }; |
| 340 | + const unsigned char *ad_A2[] = |
| 341 | + { AD1_A2, AD2_A2, AD3_A2, NULL }; |
| 342 | + unsigned long adlen_A2[] = |
| 343 | + { sizeof(AD1_A2), sizeof(AD2_A2), sizeof(AD3_A2), 0 }; |
| 344 | + |
| 345 | +#define PL_PAIR(n) n, sizeof(n) |
| 346 | + struct { |
| 347 | + const unsigned char* Key; |
| 348 | + unsigned long Keylen; |
| 349 | + const unsigned char* Plaintext; |
| 350 | + unsigned long Plaintextlen; |
| 351 | + const void* ADs; |
| 352 | + void* ADlens; |
| 353 | + const unsigned char* output; |
| 354 | + unsigned long outputlen; |
| 355 | + const char* name; |
| 356 | + } siv_tests[] = { |
| 357 | + { PL_PAIR(Key_A1), PL_PAIR(Plaintext_A1), &ad_A1, &adlen_A1, PL_PAIR(output_A1), "RFC5297 - A.1. Deterministic Authenticated Encryption Example" }, |
| 358 | + { PL_PAIR(Key_A2), PL_PAIR(Plaintext_A2), &ad_A2, &adlen_A2, PL_PAIR(output_A2), "RFC5297 - A.2. Nonce-Based Authenticated Encryption Example" } |
| 359 | + }; |
| 360 | +#undef PL_PAIR |
| 361 | + |
| 362 | + int err; |
| 363 | + unsigned n; |
| 364 | + unsigned char buf[MAX(sizeof(output_A1), sizeof(output_A2))]; |
| 365 | + |
| 366 | + for (n = 0; n < sizeof(siv_tests)/sizeof(siv_tests[0]); ++n) { |
| 367 | + unsigned long buflen = sizeof(buf); |
| 368 | + if ((err = siv_encrypt(find_cipher("aes"), |
| 369 | + siv_tests[n].Key, siv_tests[n].Keylen, |
| 370 | + (const unsigned char **)siv_tests[n].ADs, siv_tests[n].ADlens, |
| 371 | + siv_tests[n].Plaintext, siv_tests[n].Plaintextlen, |
| 372 | + buf, &buflen)) != CRYPT_OK) { |
| 373 | + return err; |
| 374 | + } |
| 375 | + if (compare_testvector(buf, buflen, siv_tests[n].output, siv_tests[n].outputlen, siv_tests[n].name, n) != 0) { |
| 376 | + return CRYPT_FAIL_TESTVECTOR; |
| 377 | + } |
| 378 | + buflen = sizeof(buf); |
| 379 | + if ((err = siv_decrypt(find_cipher("aes"), |
| 380 | + siv_tests[n].Key, siv_tests[n].Keylen, |
| 381 | + (const unsigned char **)siv_tests[n].ADs, siv_tests[n].ADlens, |
| 382 | + siv_tests[n].output, siv_tests[n].outputlen, |
| 383 | + buf, &buflen)) != CRYPT_OK) { |
| 384 | + return err; |
| 385 | + } |
| 386 | + if (compare_testvector(buf, buflen, siv_tests[n].Plaintext, siv_tests[n].Plaintextlen, siv_tests[n].name, n + 1000) != 0) { |
| 387 | + return CRYPT_FAIL_TESTVECTOR; |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + return CRYPT_OK; |
| 392 | +} |
| 393 | +#endif |
| 394 | + |
| 395 | +/* ref: $Format:%D$ */ |
| 396 | +/* git commit: $Format:%H$ */ |
| 397 | +/* commit time: $Format:%ai$ */ |
0 commit comments