gcm.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. /*
  2. * NIST SP800-38D compliant GCM implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  25. *
  26. * See also:
  27. * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
  28. *
  29. * We use the algorithm described as Shoup's method with 4-bit tables in
  30. * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
  31. */
  32. #if !defined(MBEDTLS_CONFIG_FILE)
  33. #include "mbedtls/config.h"
  34. #else
  35. #include MBEDTLS_CONFIG_FILE
  36. #endif
  37. #if defined(MBEDTLS_GCM_C)
  38. #include "mbedtls/gcm.h"
  39. #include <string.h>
  40. #if defined(MBEDTLS_AESNI_C)
  41. #include "mbedtls/aesni.h"
  42. #endif
  43. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  44. #if defined(MBEDTLS_PLATFORM_C)
  45. #include "mbedtls/platform.h"
  46. #else
  47. #include <stdio.h>
  48. #define mbedtls_printf printf
  49. #endif /* MBEDTLS_PLATFORM_C */
  50. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  51. /*
  52. * 32-bit integer manipulation macros (big endian)
  53. */
  54. #ifndef GET_UINT32_BE
  55. #define GET_UINT32_BE(n,b,i) \
  56. { \
  57. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  58. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  59. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  60. | ( (uint32_t) (b)[(i) + 3] ); \
  61. }
  62. #endif
  63. #ifndef PUT_UINT32_BE
  64. #define PUT_UINT32_BE(n,b,i) \
  65. { \
  66. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  67. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  68. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  69. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  70. }
  71. #endif
  72. /* Implementation that should never be optimized out by the compiler */
  73. static void mbedtls_zeroize( void *v, size_t n ) {
  74. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  75. }
  76. /*
  77. * Initialize a context
  78. */
  79. void mbedtls_gcm_init( mbedtls_gcm_context *ctx )
  80. {
  81. memset( ctx, 0, sizeof( mbedtls_gcm_context ) );
  82. }
  83. /*
  84. * Precompute small multiples of H, that is set
  85. * HH[i] || HL[i] = H times i,
  86. * where i is seen as a field element as in [MGV], ie high-order bits
  87. * correspond to low powers of P. The result is stored in the same way, that
  88. * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
  89. * corresponds to P^127.
  90. */
  91. static int gcm_gen_table( mbedtls_gcm_context *ctx )
  92. {
  93. int ret, i, j;
  94. uint64_t hi, lo;
  95. uint64_t vl, vh;
  96. unsigned char h[16];
  97. size_t olen = 0;
  98. memset( h, 0, 16 );
  99. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
  100. return( ret );
  101. /* pack h as two 64-bits ints, big-endian */
  102. GET_UINT32_BE( hi, h, 0 );
  103. GET_UINT32_BE( lo, h, 4 );
  104. vh = (uint64_t) hi << 32 | lo;
  105. GET_UINT32_BE( hi, h, 8 );
  106. GET_UINT32_BE( lo, h, 12 );
  107. vl = (uint64_t) hi << 32 | lo;
  108. /* 8 = 1000 corresponds to 1 in GF(2^128) */
  109. ctx->HL[8] = vl;
  110. ctx->HH[8] = vh;
  111. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  112. /* With CLMUL support, we need only h, not the rest of the table */
  113. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) )
  114. return( 0 );
  115. #endif
  116. /* 0 corresponds to 0 in GF(2^128) */
  117. ctx->HH[0] = 0;
  118. ctx->HL[0] = 0;
  119. for( i = 4; i > 0; i >>= 1 )
  120. {
  121. uint32_t T = ( vl & 1 ) * 0xe1000000U;
  122. vl = ( vh << 63 ) | ( vl >> 1 );
  123. vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32);
  124. ctx->HL[i] = vl;
  125. ctx->HH[i] = vh;
  126. }
  127. for( i = 2; i <= 8; i *= 2 )
  128. {
  129. uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
  130. vh = *HiH;
  131. vl = *HiL;
  132. for( j = 1; j < i; j++ )
  133. {
  134. HiH[j] = vh ^ ctx->HH[j];
  135. HiL[j] = vl ^ ctx->HL[j];
  136. }
  137. }
  138. return( 0 );
  139. }
  140. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  141. mbedtls_cipher_id_t cipher,
  142. const unsigned char *key,
  143. unsigned int keybits )
  144. {
  145. int ret;
  146. const mbedtls_cipher_info_t *cipher_info;
  147. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  148. if( cipher_info == NULL )
  149. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  150. if( cipher_info->block_size != 16 )
  151. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  152. mbedtls_cipher_free( &ctx->cipher_ctx );
  153. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  154. return( ret );
  155. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  156. MBEDTLS_ENCRYPT ) ) != 0 )
  157. {
  158. return( ret );
  159. }
  160. if( ( ret = gcm_gen_table( ctx ) ) != 0 )
  161. return( ret );
  162. return( 0 );
  163. }
  164. /*
  165. * Shoup's method for multiplication use this table with
  166. * last4[x] = x times P^128
  167. * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
  168. */
  169. static const uint64_t last4[16] =
  170. {
  171. 0x0000, 0x1c20, 0x3840, 0x2460,
  172. 0x7080, 0x6ca0, 0x48c0, 0x54e0,
  173. 0xe100, 0xfd20, 0xd940, 0xc560,
  174. 0x9180, 0x8da0, 0xa9c0, 0xb5e0
  175. };
  176. /*
  177. * Sets output to x times H using the precomputed tables.
  178. * x and output are seen as elements of GF(2^128) as in [MGV].
  179. */
  180. static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16],
  181. unsigned char output[16] )
  182. {
  183. int i = 0;
  184. unsigned char lo, hi, rem;
  185. uint64_t zh, zl;
  186. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  187. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) {
  188. unsigned char h[16];
  189. PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 );
  190. PUT_UINT32_BE( ctx->HH[8], h, 4 );
  191. PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 );
  192. PUT_UINT32_BE( ctx->HL[8], h, 12 );
  193. mbedtls_aesni_gcm_mult( output, x, h );
  194. return;
  195. }
  196. #endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */
  197. lo = x[15] & 0xf;
  198. zh = ctx->HH[lo];
  199. zl = ctx->HL[lo];
  200. for( i = 15; i >= 0; i-- )
  201. {
  202. lo = x[i] & 0xf;
  203. hi = x[i] >> 4;
  204. if( i != 15 )
  205. {
  206. rem = (unsigned char) zl & 0xf;
  207. zl = ( zh << 60 ) | ( zl >> 4 );
  208. zh = ( zh >> 4 );
  209. zh ^= (uint64_t) last4[rem] << 48;
  210. zh ^= ctx->HH[lo];
  211. zl ^= ctx->HL[lo];
  212. }
  213. rem = (unsigned char) zl & 0xf;
  214. zl = ( zh << 60 ) | ( zl >> 4 );
  215. zh = ( zh >> 4 );
  216. zh ^= (uint64_t) last4[rem] << 48;
  217. zh ^= ctx->HH[hi];
  218. zl ^= ctx->HL[hi];
  219. }
  220. PUT_UINT32_BE( zh >> 32, output, 0 );
  221. PUT_UINT32_BE( zh, output, 4 );
  222. PUT_UINT32_BE( zl >> 32, output, 8 );
  223. PUT_UINT32_BE( zl, output, 12 );
  224. }
  225. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  226. int mode,
  227. const unsigned char *iv,
  228. size_t iv_len,
  229. const unsigned char *add,
  230. size_t add_len )
  231. {
  232. int ret;
  233. unsigned char work_buf[16];
  234. size_t i;
  235. const unsigned char *p;
  236. size_t use_len, olen = 0;
  237. /* IV and AD are limited to 2^64 bits, so 2^61 bytes */
  238. if( ( (uint64_t) iv_len ) >> 61 != 0 ||
  239. ( (uint64_t) add_len ) >> 61 != 0 )
  240. {
  241. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  242. }
  243. memset( ctx->y, 0x00, sizeof(ctx->y) );
  244. memset( ctx->buf, 0x00, sizeof(ctx->buf) );
  245. ctx->mode = mode;
  246. ctx->len = 0;
  247. ctx->add_len = 0;
  248. if( iv_len == 12 )
  249. {
  250. memcpy( ctx->y, iv, iv_len );
  251. ctx->y[15] = 1;
  252. }
  253. else
  254. {
  255. memset( work_buf, 0x00, 16 );
  256. PUT_UINT32_BE( iv_len * 8, work_buf, 12 );
  257. p = iv;
  258. while( iv_len > 0 )
  259. {
  260. use_len = ( iv_len < 16 ) ? iv_len : 16;
  261. for( i = 0; i < use_len; i++ )
  262. ctx->y[i] ^= p[i];
  263. gcm_mult( ctx, ctx->y, ctx->y );
  264. iv_len -= use_len;
  265. p += use_len;
  266. }
  267. for( i = 0; i < 16; i++ )
  268. ctx->y[i] ^= work_buf[i];
  269. gcm_mult( ctx, ctx->y, ctx->y );
  270. }
  271. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr,
  272. &olen ) ) != 0 )
  273. {
  274. return( ret );
  275. }
  276. ctx->add_len = add_len;
  277. p = add;
  278. while( add_len > 0 )
  279. {
  280. use_len = ( add_len < 16 ) ? add_len : 16;
  281. for( i = 0; i < use_len; i++ )
  282. ctx->buf[i] ^= p[i];
  283. gcm_mult( ctx, ctx->buf, ctx->buf );
  284. add_len -= use_len;
  285. p += use_len;
  286. }
  287. return( 0 );
  288. }
  289. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  290. size_t length,
  291. const unsigned char *input,
  292. unsigned char *output )
  293. {
  294. int ret;
  295. unsigned char ectr[16];
  296. size_t i;
  297. const unsigned char *p;
  298. unsigned char *out_p = output;
  299. size_t use_len, olen = 0;
  300. if( output > input && (size_t) ( output - input ) < length )
  301. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  302. /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes
  303. * Also check for possible overflow */
  304. if( ctx->len + length < ctx->len ||
  305. (uint64_t) ctx->len + length > 0xFFFFFFFE0ull )
  306. {
  307. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  308. }
  309. ctx->len += length;
  310. p = input;
  311. while( length > 0 )
  312. {
  313. use_len = ( length < 16 ) ? length : 16;
  314. for( i = 16; i > 12; i-- )
  315. if( ++ctx->y[i - 1] != 0 )
  316. break;
  317. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr,
  318. &olen ) ) != 0 )
  319. {
  320. return( ret );
  321. }
  322. for( i = 0; i < use_len; i++ )
  323. {
  324. if( ctx->mode == MBEDTLS_GCM_DECRYPT )
  325. ctx->buf[i] ^= p[i];
  326. out_p[i] = ectr[i] ^ p[i];
  327. if( ctx->mode == MBEDTLS_GCM_ENCRYPT )
  328. ctx->buf[i] ^= out_p[i];
  329. }
  330. gcm_mult( ctx, ctx->buf, ctx->buf );
  331. length -= use_len;
  332. p += use_len;
  333. out_p += use_len;
  334. }
  335. return( 0 );
  336. }
  337. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  338. unsigned char *tag,
  339. size_t tag_len )
  340. {
  341. unsigned char work_buf[16];
  342. size_t i;
  343. uint64_t orig_len = ctx->len * 8;
  344. uint64_t orig_add_len = ctx->add_len * 8;
  345. if( tag_len > 16 || tag_len < 4 )
  346. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  347. memcpy( tag, ctx->base_ectr, tag_len );
  348. if( orig_len || orig_add_len )
  349. {
  350. memset( work_buf, 0x00, 16 );
  351. PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 );
  352. PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 );
  353. PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 );
  354. PUT_UINT32_BE( ( orig_len ), work_buf, 12 );
  355. for( i = 0; i < 16; i++ )
  356. ctx->buf[i] ^= work_buf[i];
  357. gcm_mult( ctx, ctx->buf, ctx->buf );
  358. for( i = 0; i < tag_len; i++ )
  359. tag[i] ^= ctx->buf[i];
  360. }
  361. return( 0 );
  362. }
  363. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  364. int mode,
  365. size_t length,
  366. const unsigned char *iv,
  367. size_t iv_len,
  368. const unsigned char *add,
  369. size_t add_len,
  370. const unsigned char *input,
  371. unsigned char *output,
  372. size_t tag_len,
  373. unsigned char *tag )
  374. {
  375. int ret;
  376. if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 )
  377. return( ret );
  378. if( ( ret = mbedtls_gcm_update( ctx, length, input, output ) ) != 0 )
  379. return( ret );
  380. if( ( ret = mbedtls_gcm_finish( ctx, tag, tag_len ) ) != 0 )
  381. return( ret );
  382. return( 0 );
  383. }
  384. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  385. size_t length,
  386. const unsigned char *iv,
  387. size_t iv_len,
  388. const unsigned char *add,
  389. size_t add_len,
  390. const unsigned char *tag,
  391. size_t tag_len,
  392. const unsigned char *input,
  393. unsigned char *output )
  394. {
  395. int ret;
  396. unsigned char check_tag[16];
  397. size_t i;
  398. int diff;
  399. if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length,
  400. iv, iv_len, add, add_len,
  401. input, output, tag_len, check_tag ) ) != 0 )
  402. {
  403. return( ret );
  404. }
  405. /* Check tag in "constant-time" */
  406. for( diff = 0, i = 0; i < tag_len; i++ )
  407. diff |= tag[i] ^ check_tag[i];
  408. if( diff != 0 )
  409. {
  410. mbedtls_zeroize( output, length );
  411. return( MBEDTLS_ERR_GCM_AUTH_FAILED );
  412. }
  413. return( 0 );
  414. }
  415. void mbedtls_gcm_free( mbedtls_gcm_context *ctx )
  416. {
  417. mbedtls_cipher_free( &ctx->cipher_ctx );
  418. mbedtls_zeroize( ctx, sizeof( mbedtls_gcm_context ) );
  419. }
  420. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  421. /*
  422. * AES-GCM test vectors from:
  423. *
  424. * http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip
  425. */
  426. #define MAX_TESTS 6
  427. static const int key_index[MAX_TESTS] =
  428. { 0, 0, 1, 1, 1, 1 };
  429. static const unsigned char key[MAX_TESTS][32] =
  430. {
  431. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  432. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  433. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  434. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  435. { 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  436. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  437. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  438. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08 },
  439. };
  440. static const size_t iv_len[MAX_TESTS] =
  441. { 12, 12, 12, 12, 8, 60 };
  442. static const int iv_index[MAX_TESTS] =
  443. { 0, 0, 1, 1, 1, 2 };
  444. static const unsigned char iv[MAX_TESTS][64] =
  445. {
  446. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  447. 0x00, 0x00, 0x00, 0x00 },
  448. { 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  449. 0xde, 0xca, 0xf8, 0x88 },
  450. { 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  451. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  452. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  453. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  454. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  455. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  456. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  457. 0xa6, 0x37, 0xb3, 0x9b },
  458. };
  459. static const size_t add_len[MAX_TESTS] =
  460. { 0, 0, 0, 20, 20, 20 };
  461. static const int add_index[MAX_TESTS] =
  462. { 0, 0, 0, 1, 1, 1 };
  463. static const unsigned char additional[MAX_TESTS][64] =
  464. {
  465. { 0x00 },
  466. { 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  467. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  468. 0xab, 0xad, 0xda, 0xd2 },
  469. };
  470. static const size_t pt_len[MAX_TESTS] =
  471. { 0, 16, 64, 60, 60, 60 };
  472. static const int pt_index[MAX_TESTS] =
  473. { 0, 0, 1, 1, 1, 1 };
  474. static const unsigned char pt[MAX_TESTS][64] =
  475. {
  476. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  477. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  478. { 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  479. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  480. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  481. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  482. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  483. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  484. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  485. 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55 },
  486. };
  487. static const unsigned char ct[MAX_TESTS * 3][64] =
  488. {
  489. { 0x00 },
  490. { 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
  491. 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78 },
  492. { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  493. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  494. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  495. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  496. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  497. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  498. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  499. 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85 },
  500. { 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
  501. 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
  502. 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
  503. 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
  504. 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
  505. 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
  506. 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
  507. 0x3d, 0x58, 0xe0, 0x91 },
  508. { 0x61, 0x35, 0x3b, 0x4c, 0x28, 0x06, 0x93, 0x4a,
  509. 0x77, 0x7f, 0xf5, 0x1f, 0xa2, 0x2a, 0x47, 0x55,
  510. 0x69, 0x9b, 0x2a, 0x71, 0x4f, 0xcd, 0xc6, 0xf8,
  511. 0x37, 0x66, 0xe5, 0xf9, 0x7b, 0x6c, 0x74, 0x23,
  512. 0x73, 0x80, 0x69, 0x00, 0xe4, 0x9f, 0x24, 0xb2,
  513. 0x2b, 0x09, 0x75, 0x44, 0xd4, 0x89, 0x6b, 0x42,
  514. 0x49, 0x89, 0xb5, 0xe1, 0xeb, 0xac, 0x0f, 0x07,
  515. 0xc2, 0x3f, 0x45, 0x98 },
  516. { 0x8c, 0xe2, 0x49, 0x98, 0x62, 0x56, 0x15, 0xb6,
  517. 0x03, 0xa0, 0x33, 0xac, 0xa1, 0x3f, 0xb8, 0x94,
  518. 0xbe, 0x91, 0x12, 0xa5, 0xc3, 0xa2, 0x11, 0xa8,
  519. 0xba, 0x26, 0x2a, 0x3c, 0xca, 0x7e, 0x2c, 0xa7,
  520. 0x01, 0xe4, 0xa9, 0xa4, 0xfb, 0xa4, 0x3c, 0x90,
  521. 0xcc, 0xdc, 0xb2, 0x81, 0xd4, 0x8c, 0x7c, 0x6f,
  522. 0xd6, 0x28, 0x75, 0xd2, 0xac, 0xa4, 0x17, 0x03,
  523. 0x4c, 0x34, 0xae, 0xe5 },
  524. { 0x00 },
  525. { 0x98, 0xe7, 0x24, 0x7c, 0x07, 0xf0, 0xfe, 0x41,
  526. 0x1c, 0x26, 0x7e, 0x43, 0x84, 0xb0, 0xf6, 0x00 },
  527. { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  528. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  529. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  530. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  531. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  532. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  533. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  534. 0xcc, 0xda, 0x27, 0x10, 0xac, 0xad, 0xe2, 0x56 },
  535. { 0x39, 0x80, 0xca, 0x0b, 0x3c, 0x00, 0xe8, 0x41,
  536. 0xeb, 0x06, 0xfa, 0xc4, 0x87, 0x2a, 0x27, 0x57,
  537. 0x85, 0x9e, 0x1c, 0xea, 0xa6, 0xef, 0xd9, 0x84,
  538. 0x62, 0x85, 0x93, 0xb4, 0x0c, 0xa1, 0xe1, 0x9c,
  539. 0x7d, 0x77, 0x3d, 0x00, 0xc1, 0x44, 0xc5, 0x25,
  540. 0xac, 0x61, 0x9d, 0x18, 0xc8, 0x4a, 0x3f, 0x47,
  541. 0x18, 0xe2, 0x44, 0x8b, 0x2f, 0xe3, 0x24, 0xd9,
  542. 0xcc, 0xda, 0x27, 0x10 },
  543. { 0x0f, 0x10, 0xf5, 0x99, 0xae, 0x14, 0xa1, 0x54,
  544. 0xed, 0x24, 0xb3, 0x6e, 0x25, 0x32, 0x4d, 0xb8,
  545. 0xc5, 0x66, 0x63, 0x2e, 0xf2, 0xbb, 0xb3, 0x4f,
  546. 0x83, 0x47, 0x28, 0x0f, 0xc4, 0x50, 0x70, 0x57,
  547. 0xfd, 0xdc, 0x29, 0xdf, 0x9a, 0x47, 0x1f, 0x75,
  548. 0xc6, 0x65, 0x41, 0xd4, 0xd4, 0xda, 0xd1, 0xc9,
  549. 0xe9, 0x3a, 0x19, 0xa5, 0x8e, 0x8b, 0x47, 0x3f,
  550. 0xa0, 0xf0, 0x62, 0xf7 },
  551. { 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  552. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  553. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  554. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  555. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  556. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  557. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  558. 0xe9, 0xb7, 0x37, 0x3b },
  559. { 0x00 },
  560. { 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
  561. 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18 },
  562. { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  563. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  564. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  565. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  566. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  567. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  568. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  569. 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad },
  570. { 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  571. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  572. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  573. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  574. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  575. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  576. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  577. 0xbc, 0xc9, 0xf6, 0x62 },
  578. { 0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32,
  579. 0xae, 0x47, 0xc1, 0x3b, 0xf1, 0x98, 0x44, 0xcb,
  580. 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
  581. 0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0,
  582. 0xfe, 0xb5, 0x82, 0xd3, 0x39, 0x34, 0xa4, 0xf0,
  583. 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
  584. 0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99,
  585. 0xf4, 0x7c, 0x9b, 0x1f },
  586. { 0x5a, 0x8d, 0xef, 0x2f, 0x0c, 0x9e, 0x53, 0xf1,
  587. 0xf7, 0x5d, 0x78, 0x53, 0x65, 0x9e, 0x2a, 0x20,
  588. 0xee, 0xb2, 0xb2, 0x2a, 0xaf, 0xde, 0x64, 0x19,
  589. 0xa0, 0x58, 0xab, 0x4f, 0x6f, 0x74, 0x6b, 0xf4,
  590. 0x0f, 0xc0, 0xc3, 0xb7, 0x80, 0xf2, 0x44, 0x45,
  591. 0x2d, 0xa3, 0xeb, 0xf1, 0xc5, 0xd8, 0x2c, 0xde,
  592. 0xa2, 0x41, 0x89, 0x97, 0x20, 0x0e, 0xf8, 0x2e,
  593. 0x44, 0xae, 0x7e, 0x3f },
  594. };
  595. static const unsigned char tag[MAX_TESTS * 3][16] =
  596. {
  597. { 0x58, 0xe2, 0xfc, 0xce, 0xfa, 0x7e, 0x30, 0x61,
  598. 0x36, 0x7f, 0x1d, 0x57, 0xa4, 0xe7, 0x45, 0x5a },
  599. { 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
  600. 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf },
  601. { 0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd, 0x64, 0xa6,
  602. 0x2c, 0xf3, 0x5a, 0xbd, 0x2b, 0xa6, 0xfa, 0xb4 },
  603. { 0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21, 0xa5, 0xdb,
  604. 0x94, 0xfa, 0xe9, 0x5a, 0xe7, 0x12, 0x1a, 0x47 },
  605. { 0x36, 0x12, 0xd2, 0xe7, 0x9e, 0x3b, 0x07, 0x85,
  606. 0x56, 0x1b, 0xe1, 0x4a, 0xac, 0xa2, 0xfc, 0xcb },
  607. { 0x61, 0x9c, 0xc5, 0xae, 0xff, 0xfe, 0x0b, 0xfa,
  608. 0x46, 0x2a, 0xf4, 0x3c, 0x16, 0x99, 0xd0, 0x50 },
  609. { 0xcd, 0x33, 0xb2, 0x8a, 0xc7, 0x73, 0xf7, 0x4b,
  610. 0xa0, 0x0e, 0xd1, 0xf3, 0x12, 0x57, 0x24, 0x35 },
  611. { 0x2f, 0xf5, 0x8d, 0x80, 0x03, 0x39, 0x27, 0xab,
  612. 0x8e, 0xf4, 0xd4, 0x58, 0x75, 0x14, 0xf0, 0xfb },
  613. { 0x99, 0x24, 0xa7, 0xc8, 0x58, 0x73, 0x36, 0xbf,
  614. 0xb1, 0x18, 0x02, 0x4d, 0xb8, 0x67, 0x4a, 0x14 },
  615. { 0x25, 0x19, 0x49, 0x8e, 0x80, 0xf1, 0x47, 0x8f,
  616. 0x37, 0xba, 0x55, 0xbd, 0x6d, 0x27, 0x61, 0x8c },
  617. { 0x65, 0xdc, 0xc5, 0x7f, 0xcf, 0x62, 0x3a, 0x24,
  618. 0x09, 0x4f, 0xcc, 0xa4, 0x0d, 0x35, 0x33, 0xf8 },
  619. { 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  620. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9 },
  621. { 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9,
  622. 0xa9, 0x63, 0xb4, 0xf1, 0xc4, 0xcb, 0x73, 0x8b },
  623. { 0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0,
  624. 0x26, 0x5b, 0x98, 0xb5, 0xd4, 0x8a, 0xb9, 0x19 },
  625. { 0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34, 0x71, 0xbd,
  626. 0xec, 0x1a, 0x50, 0x22, 0x70, 0xe3, 0xcc, 0x6c },
  627. { 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  628. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b },
  629. { 0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4,
  630. 0x5e, 0x45, 0x49, 0x13, 0xfe, 0x2e, 0xa8, 0xf2 },
  631. { 0xa4, 0x4a, 0x82, 0x66, 0xee, 0x1c, 0x8e, 0xb0,
  632. 0xc8, 0xb5, 0xd4, 0xcf, 0x5a, 0xe9, 0xf1, 0x9a },
  633. };
  634. int mbedtls_gcm_self_test( int verbose )
  635. {
  636. mbedtls_gcm_context ctx;
  637. unsigned char buf[64];
  638. unsigned char tag_buf[16];
  639. int i, j, ret;
  640. mbedtls_cipher_id_t cipher = MBEDTLS_CIPHER_ID_AES;
  641. mbedtls_gcm_init( &ctx );
  642. for( j = 0; j < 3; j++ )
  643. {
  644. int key_len = 128 + 64 * j;
  645. for( i = 0; i < MAX_TESTS; i++ )
  646. {
  647. if( verbose != 0 )
  648. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  649. key_len, i, "enc" );
  650. mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
  651. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT,
  652. pt_len[i],
  653. iv[iv_index[i]], iv_len[i],
  654. additional[add_index[i]], add_len[i],
  655. pt[pt_index[i]], buf, 16, tag_buf );
  656. if( ret != 0 ||
  657. memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  658. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  659. {
  660. if( verbose != 0 )
  661. mbedtls_printf( "failed\n" );
  662. return( 1 );
  663. }
  664. mbedtls_gcm_free( &ctx );
  665. if( verbose != 0 )
  666. mbedtls_printf( "passed\n" );
  667. if( verbose != 0 )
  668. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  669. key_len, i, "dec" );
  670. mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
  671. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_DECRYPT,
  672. pt_len[i],
  673. iv[iv_index[i]], iv_len[i],
  674. additional[add_index[i]], add_len[i],
  675. ct[j * 6 + i], buf, 16, tag_buf );
  676. if( ret != 0 ||
  677. memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  678. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  679. {
  680. if( verbose != 0 )
  681. mbedtls_printf( "failed\n" );
  682. return( 1 );
  683. }
  684. mbedtls_gcm_free( &ctx );
  685. if( verbose != 0 )
  686. mbedtls_printf( "passed\n" );
  687. if( verbose != 0 )
  688. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  689. key_len, i, "enc" );
  690. mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
  691. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_ENCRYPT,
  692. iv[iv_index[i]], iv_len[i],
  693. additional[add_index[i]], add_len[i] );
  694. if( ret != 0 )
  695. {
  696. if( verbose != 0 )
  697. mbedtls_printf( "failed\n" );
  698. return( 1 );
  699. }
  700. if( pt_len[i] > 32 )
  701. {
  702. size_t rest_len = pt_len[i] - 32;
  703. ret = mbedtls_gcm_update( &ctx, 32, pt[pt_index[i]], buf );
  704. if( ret != 0 )
  705. {
  706. if( verbose != 0 )
  707. mbedtls_printf( "failed\n" );
  708. return( 1 );
  709. }
  710. ret = mbedtls_gcm_update( &ctx, rest_len, pt[pt_index[i]] + 32,
  711. buf + 32 );
  712. if( ret != 0 )
  713. {
  714. if( verbose != 0 )
  715. mbedtls_printf( "failed\n" );
  716. return( 1 );
  717. }
  718. }
  719. else
  720. {
  721. ret = mbedtls_gcm_update( &ctx, pt_len[i], pt[pt_index[i]], buf );
  722. if( ret != 0 )
  723. {
  724. if( verbose != 0 )
  725. mbedtls_printf( "failed\n" );
  726. return( 1 );
  727. }
  728. }
  729. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  730. if( ret != 0 ||
  731. memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  732. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  733. {
  734. if( verbose != 0 )
  735. mbedtls_printf( "failed\n" );
  736. return( 1 );
  737. }
  738. mbedtls_gcm_free( &ctx );
  739. if( verbose != 0 )
  740. mbedtls_printf( "passed\n" );
  741. if( verbose != 0 )
  742. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  743. key_len, i, "dec" );
  744. mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]], key_len );
  745. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_DECRYPT,
  746. iv[iv_index[i]], iv_len[i],
  747. additional[add_index[i]], add_len[i] );
  748. if( ret != 0 )
  749. {
  750. if( verbose != 0 )
  751. mbedtls_printf( "failed\n" );
  752. return( 1 );
  753. }
  754. if( pt_len[i] > 32 )
  755. {
  756. size_t rest_len = pt_len[i] - 32;
  757. ret = mbedtls_gcm_update( &ctx, 32, ct[j * 6 + i], buf );
  758. if( ret != 0 )
  759. {
  760. if( verbose != 0 )
  761. mbedtls_printf( "failed\n" );
  762. return( 1 );
  763. }
  764. ret = mbedtls_gcm_update( &ctx, rest_len, ct[j * 6 + i] + 32,
  765. buf + 32 );
  766. if( ret != 0 )
  767. {
  768. if( verbose != 0 )
  769. mbedtls_printf( "failed\n" );
  770. return( 1 );
  771. }
  772. }
  773. else
  774. {
  775. ret = mbedtls_gcm_update( &ctx, pt_len[i], ct[j * 6 + i], buf );
  776. if( ret != 0 )
  777. {
  778. if( verbose != 0 )
  779. mbedtls_printf( "failed\n" );
  780. return( 1 );
  781. }
  782. }
  783. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  784. if( ret != 0 ||
  785. memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  786. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  787. {
  788. if( verbose != 0 )
  789. mbedtls_printf( "failed\n" );
  790. return( 1 );
  791. }
  792. mbedtls_gcm_free( &ctx );
  793. if( verbose != 0 )
  794. mbedtls_printf( "passed\n" );
  795. }
  796. }
  797. if( verbose != 0 )
  798. mbedtls_printf( "\n" );
  799. return( 0 );
  800. }
  801. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  802. #endif /* MBEDTLS_GCM_C */