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: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  23. *
  24. * See also:
  25. * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
  26. *
  27. * We use the algorithm described as Shoup's method with 4-bit tables in
  28. * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
  29. */
  30. #if !defined(MBEDTLS_CONFIG_FILE)
  31. #include "mbedtls/config.h"
  32. #else
  33. #include MBEDTLS_CONFIG_FILE
  34. #endif
  35. #if defined(MBEDTLS_GCM_C)
  36. #include "mbedtls/gcm.h"
  37. #include "mbedtls/platform_util.h"
  38. #include <string.h>
  39. #if defined(MBEDTLS_AESNI_C)
  40. #include "mbedtls/aesni.h"
  41. #endif
  42. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  43. #include "mbedtls/aes.h"
  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. #if !defined(MBEDTLS_GCM_ALT)
  52. /*
  53. * 32-bit integer manipulation macros (big endian)
  54. */
  55. #ifndef GET_UINT32_BE
  56. #define GET_UINT32_BE(n,b,i) \
  57. { \
  58. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  59. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  60. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  61. | ( (uint32_t) (b)[(i) + 3] ); \
  62. }
  63. #endif
  64. #ifndef PUT_UINT32_BE
  65. #define PUT_UINT32_BE(n,b,i) \
  66. { \
  67. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  68. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  69. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  70. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  71. }
  72. #endif
  73. /*
  74. * Initialize a context
  75. */
  76. void mbedtls_gcm_init( mbedtls_gcm_context *ctx )
  77. {
  78. memset( ctx, 0, sizeof( mbedtls_gcm_context ) );
  79. }
  80. /*
  81. * Precompute small multiples of H, that is set
  82. * HH[i] || HL[i] = H times i,
  83. * where i is seen as a field element as in [MGV], ie high-order bits
  84. * correspond to low powers of P. The result is stored in the same way, that
  85. * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
  86. * corresponds to P^127.
  87. */
  88. static int gcm_gen_table( mbedtls_gcm_context *ctx )
  89. {
  90. int ret, i, j;
  91. uint64_t hi, lo;
  92. uint64_t vl, vh;
  93. unsigned char h[16];
  94. size_t olen = 0;
  95. memset( h, 0, 16 );
  96. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
  97. return( ret );
  98. /* pack h as two 64-bits ints, big-endian */
  99. GET_UINT32_BE( hi, h, 0 );
  100. GET_UINT32_BE( lo, h, 4 );
  101. vh = (uint64_t) hi << 32 | lo;
  102. GET_UINT32_BE( hi, h, 8 );
  103. GET_UINT32_BE( lo, h, 12 );
  104. vl = (uint64_t) hi << 32 | lo;
  105. /* 8 = 1000 corresponds to 1 in GF(2^128) */
  106. ctx->HL[8] = vl;
  107. ctx->HH[8] = vh;
  108. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  109. /* With CLMUL support, we need only h, not the rest of the table */
  110. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) )
  111. return( 0 );
  112. #endif
  113. /* 0 corresponds to 0 in GF(2^128) */
  114. ctx->HH[0] = 0;
  115. ctx->HL[0] = 0;
  116. for( i = 4; i > 0; i >>= 1 )
  117. {
  118. uint32_t T = ( vl & 1 ) * 0xe1000000U;
  119. vl = ( vh << 63 ) | ( vl >> 1 );
  120. vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32);
  121. ctx->HL[i] = vl;
  122. ctx->HH[i] = vh;
  123. }
  124. for( i = 2; i <= 8; i *= 2 )
  125. {
  126. uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
  127. vh = *HiH;
  128. vl = *HiL;
  129. for( j = 1; j < i; j++ )
  130. {
  131. HiH[j] = vh ^ ctx->HH[j];
  132. HiL[j] = vl ^ ctx->HL[j];
  133. }
  134. }
  135. return( 0 );
  136. }
  137. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  138. mbedtls_cipher_id_t cipher,
  139. const unsigned char *key,
  140. unsigned int keybits )
  141. {
  142. int ret;
  143. const mbedtls_cipher_info_t *cipher_info;
  144. cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB );
  145. if( cipher_info == NULL )
  146. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  147. if( cipher_info->block_size != 16 )
  148. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  149. mbedtls_cipher_free( &ctx->cipher_ctx );
  150. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  151. return( ret );
  152. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  153. MBEDTLS_ENCRYPT ) ) != 0 )
  154. {
  155. return( ret );
  156. }
  157. if( ( ret = gcm_gen_table( ctx ) ) != 0 )
  158. return( ret );
  159. return( 0 );
  160. }
  161. /*
  162. * Shoup's method for multiplication use this table with
  163. * last4[x] = x times P^128
  164. * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
  165. */
  166. static const uint64_t last4[16] =
  167. {
  168. 0x0000, 0x1c20, 0x3840, 0x2460,
  169. 0x7080, 0x6ca0, 0x48c0, 0x54e0,
  170. 0xe100, 0xfd20, 0xd940, 0xc560,
  171. 0x9180, 0x8da0, 0xa9c0, 0xb5e0
  172. };
  173. /*
  174. * Sets output to x times H using the precomputed tables.
  175. * x and output are seen as elements of GF(2^128) as in [MGV].
  176. */
  177. static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16],
  178. unsigned char output[16] )
  179. {
  180. int i = 0;
  181. unsigned char lo, hi, rem;
  182. uint64_t zh, zl;
  183. #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
  184. if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) {
  185. unsigned char h[16];
  186. PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 );
  187. PUT_UINT32_BE( ctx->HH[8], h, 4 );
  188. PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 );
  189. PUT_UINT32_BE( ctx->HL[8], h, 12 );
  190. mbedtls_aesni_gcm_mult( output, x, h );
  191. return;
  192. }
  193. #endif /* MBEDTLS_AESNI_C && MBEDTLS_HAVE_X86_64 */
  194. lo = x[15] & 0xf;
  195. zh = ctx->HH[lo];
  196. zl = ctx->HL[lo];
  197. for( i = 15; i >= 0; i-- )
  198. {
  199. lo = x[i] & 0xf;
  200. hi = x[i] >> 4;
  201. if( i != 15 )
  202. {
  203. rem = (unsigned char) zl & 0xf;
  204. zl = ( zh << 60 ) | ( zl >> 4 );
  205. zh = ( zh >> 4 );
  206. zh ^= (uint64_t) last4[rem] << 48;
  207. zh ^= ctx->HH[lo];
  208. zl ^= ctx->HL[lo];
  209. }
  210. rem = (unsigned char) zl & 0xf;
  211. zl = ( zh << 60 ) | ( zl >> 4 );
  212. zh = ( zh >> 4 );
  213. zh ^= (uint64_t) last4[rem] << 48;
  214. zh ^= ctx->HH[hi];
  215. zl ^= ctx->HL[hi];
  216. }
  217. PUT_UINT32_BE( zh >> 32, output, 0 );
  218. PUT_UINT32_BE( zh, output, 4 );
  219. PUT_UINT32_BE( zl >> 32, output, 8 );
  220. PUT_UINT32_BE( zl, output, 12 );
  221. }
  222. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  223. int mode,
  224. const unsigned char *iv,
  225. size_t iv_len,
  226. const unsigned char *add,
  227. size_t add_len )
  228. {
  229. int ret;
  230. unsigned char work_buf[16];
  231. size_t i;
  232. const unsigned char *p;
  233. size_t use_len, olen = 0;
  234. /* IV and AD are limited to 2^64 bits, so 2^61 bytes */
  235. /* IV is not allowed to be zero length */
  236. if( iv_len == 0 ||
  237. ( (uint64_t) iv_len ) >> 61 != 0 ||
  238. ( (uint64_t) add_len ) >> 61 != 0 )
  239. {
  240. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  241. }
  242. memset( ctx->y, 0x00, sizeof(ctx->y) );
  243. memset( ctx->buf, 0x00, sizeof(ctx->buf) );
  244. ctx->mode = mode;
  245. ctx->len = 0;
  246. ctx->add_len = 0;
  247. if( iv_len == 12 )
  248. {
  249. memcpy( ctx->y, iv, iv_len );
  250. ctx->y[15] = 1;
  251. }
  252. else
  253. {
  254. memset( work_buf, 0x00, 16 );
  255. PUT_UINT32_BE( iv_len * 8, work_buf, 12 );
  256. p = iv;
  257. while( iv_len > 0 )
  258. {
  259. use_len = ( iv_len < 16 ) ? iv_len : 16;
  260. for( i = 0; i < use_len; i++ )
  261. ctx->y[i] ^= p[i];
  262. gcm_mult( ctx, ctx->y, ctx->y );
  263. iv_len -= use_len;
  264. p += use_len;
  265. }
  266. for( i = 0; i < 16; i++ )
  267. ctx->y[i] ^= work_buf[i];
  268. gcm_mult( ctx, ctx->y, ctx->y );
  269. }
  270. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ctx->base_ectr,
  271. &olen ) ) != 0 )
  272. {
  273. return( ret );
  274. }
  275. ctx->add_len = add_len;
  276. p = add;
  277. while( add_len > 0 )
  278. {
  279. use_len = ( add_len < 16 ) ? add_len : 16;
  280. for( i = 0; i < use_len; i++ )
  281. ctx->buf[i] ^= p[i];
  282. gcm_mult( ctx, ctx->buf, ctx->buf );
  283. add_len -= use_len;
  284. p += use_len;
  285. }
  286. return( 0 );
  287. }
  288. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  289. size_t length,
  290. const unsigned char *input,
  291. unsigned char *output )
  292. {
  293. int ret;
  294. unsigned char ectr[16];
  295. size_t i;
  296. const unsigned char *p;
  297. unsigned char *out_p = output;
  298. size_t use_len, olen = 0;
  299. if( output > input && (size_t) ( output - input ) < length )
  300. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  301. /* Total length is restricted to 2^39 - 256 bits, ie 2^36 - 2^5 bytes
  302. * Also check for possible overflow */
  303. if( ctx->len + length < ctx->len ||
  304. (uint64_t) ctx->len + length > 0xFFFFFFFE0ull )
  305. {
  306. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  307. }
  308. ctx->len += length;
  309. p = input;
  310. while( length > 0 )
  311. {
  312. use_len = ( length < 16 ) ? length : 16;
  313. for( i = 16; i > 12; i-- )
  314. if( ++ctx->y[i - 1] != 0 )
  315. break;
  316. if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctx->y, 16, ectr,
  317. &olen ) ) != 0 )
  318. {
  319. return( ret );
  320. }
  321. for( i = 0; i < use_len; i++ )
  322. {
  323. if( ctx->mode == MBEDTLS_GCM_DECRYPT )
  324. ctx->buf[i] ^= p[i];
  325. out_p[i] = ectr[i] ^ p[i];
  326. if( ctx->mode == MBEDTLS_GCM_ENCRYPT )
  327. ctx->buf[i] ^= out_p[i];
  328. }
  329. gcm_mult( ctx, ctx->buf, ctx->buf );
  330. length -= use_len;
  331. p += use_len;
  332. out_p += use_len;
  333. }
  334. return( 0 );
  335. }
  336. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  337. unsigned char *tag,
  338. size_t tag_len )
  339. {
  340. unsigned char work_buf[16];
  341. size_t i;
  342. uint64_t orig_len = ctx->len * 8;
  343. uint64_t orig_add_len = ctx->add_len * 8;
  344. if( tag_len > 16 || tag_len < 4 )
  345. return( MBEDTLS_ERR_GCM_BAD_INPUT );
  346. memcpy( tag, ctx->base_ectr, tag_len );
  347. if( orig_len || orig_add_len )
  348. {
  349. memset( work_buf, 0x00, 16 );
  350. PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 );
  351. PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 );
  352. PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 );
  353. PUT_UINT32_BE( ( orig_len ), work_buf, 12 );
  354. for( i = 0; i < 16; i++ )
  355. ctx->buf[i] ^= work_buf[i];
  356. gcm_mult( ctx, ctx->buf, ctx->buf );
  357. for( i = 0; i < tag_len; i++ )
  358. tag[i] ^= ctx->buf[i];
  359. }
  360. return( 0 );
  361. }
  362. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  363. int mode,
  364. size_t length,
  365. const unsigned char *iv,
  366. size_t iv_len,
  367. const unsigned char *add,
  368. size_t add_len,
  369. const unsigned char *input,
  370. unsigned char *output,
  371. size_t tag_len,
  372. unsigned char *tag )
  373. {
  374. int ret;
  375. if( ( ret = mbedtls_gcm_starts( ctx, mode, iv, iv_len, add, add_len ) ) != 0 )
  376. return( ret );
  377. if( ( ret = mbedtls_gcm_update( ctx, length, input, output ) ) != 0 )
  378. return( ret );
  379. if( ( ret = mbedtls_gcm_finish( ctx, tag, tag_len ) ) != 0 )
  380. return( ret );
  381. return( 0 );
  382. }
  383. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  384. size_t length,
  385. const unsigned char *iv,
  386. size_t iv_len,
  387. const unsigned char *add,
  388. size_t add_len,
  389. const unsigned char *tag,
  390. size_t tag_len,
  391. const unsigned char *input,
  392. unsigned char *output )
  393. {
  394. int ret;
  395. unsigned char check_tag[16];
  396. size_t i;
  397. int diff;
  398. if( ( ret = mbedtls_gcm_crypt_and_tag( ctx, MBEDTLS_GCM_DECRYPT, length,
  399. iv, iv_len, add, add_len,
  400. input, output, tag_len, check_tag ) ) != 0 )
  401. {
  402. return( ret );
  403. }
  404. /* Check tag in "constant-time" */
  405. for( diff = 0, i = 0; i < tag_len; i++ )
  406. diff |= tag[i] ^ check_tag[i];
  407. if( diff != 0 )
  408. {
  409. mbedtls_platform_zeroize( output, length );
  410. return( MBEDTLS_ERR_GCM_AUTH_FAILED );
  411. }
  412. return( 0 );
  413. }
  414. void mbedtls_gcm_free( mbedtls_gcm_context *ctx )
  415. {
  416. mbedtls_cipher_free( &ctx->cipher_ctx );
  417. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_gcm_context ) );
  418. }
  419. #endif /* !MBEDTLS_GCM_ALT */
  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. for( j = 0; j < 3; j++ )
  642. {
  643. int key_len = 128 + 64 * j;
  644. for( i = 0; i < MAX_TESTS; i++ )
  645. {
  646. mbedtls_gcm_init( &ctx );
  647. if( verbose != 0 )
  648. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  649. key_len, i, "enc" );
  650. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  651. key_len );
  652. /*
  653. * AES-192 is an optional feature that may be unavailable when
  654. * there is an alternative underlying implementation i.e. when
  655. * MBEDTLS_AES_ALT is defined.
  656. */
  657. if( ret == MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE && key_len == 192 )
  658. {
  659. mbedtls_printf( "skipped\n" );
  660. break;
  661. }
  662. else if( ret != 0 )
  663. {
  664. goto exit;
  665. }
  666. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT,
  667. pt_len[i],
  668. iv[iv_index[i]], iv_len[i],
  669. additional[add_index[i]], add_len[i],
  670. pt[pt_index[i]], buf, 16, tag_buf );
  671. if( ret != 0 )
  672. goto exit;
  673. if ( memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  674. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  675. {
  676. ret = 1;
  677. goto exit;
  678. }
  679. mbedtls_gcm_free( &ctx );
  680. if( verbose != 0 )
  681. mbedtls_printf( "passed\n" );
  682. mbedtls_gcm_init( &ctx );
  683. if( verbose != 0 )
  684. mbedtls_printf( " AES-GCM-%3d #%d (%s): ",
  685. key_len, i, "dec" );
  686. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  687. key_len );
  688. if( ret != 0 )
  689. goto exit;
  690. ret = mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_DECRYPT,
  691. pt_len[i],
  692. iv[iv_index[i]], iv_len[i],
  693. additional[add_index[i]], add_len[i],
  694. ct[j * 6 + i], buf, 16, tag_buf );
  695. if( ret != 0 )
  696. goto exit;
  697. if( memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  698. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  699. {
  700. ret = 1;
  701. goto exit;
  702. }
  703. mbedtls_gcm_free( &ctx );
  704. if( verbose != 0 )
  705. mbedtls_printf( "passed\n" );
  706. mbedtls_gcm_init( &ctx );
  707. if( verbose != 0 )
  708. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  709. key_len, i, "enc" );
  710. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  711. key_len );
  712. if( ret != 0 )
  713. goto exit;
  714. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_ENCRYPT,
  715. iv[iv_index[i]], iv_len[i],
  716. additional[add_index[i]], add_len[i] );
  717. if( ret != 0 )
  718. goto exit;
  719. if( pt_len[i] > 32 )
  720. {
  721. size_t rest_len = pt_len[i] - 32;
  722. ret = mbedtls_gcm_update( &ctx, 32, pt[pt_index[i]], buf );
  723. if( ret != 0 )
  724. goto exit;
  725. ret = mbedtls_gcm_update( &ctx, rest_len, pt[pt_index[i]] + 32,
  726. buf + 32 );
  727. if( ret != 0 )
  728. goto exit;
  729. }
  730. else
  731. {
  732. ret = mbedtls_gcm_update( &ctx, pt_len[i], pt[pt_index[i]], buf );
  733. if( ret != 0 )
  734. goto exit;
  735. }
  736. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  737. if( ret != 0 )
  738. goto exit;
  739. if( memcmp( buf, ct[j * 6 + i], pt_len[i] ) != 0 ||
  740. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  741. {
  742. ret = 1;
  743. goto exit;
  744. }
  745. mbedtls_gcm_free( &ctx );
  746. if( verbose != 0 )
  747. mbedtls_printf( "passed\n" );
  748. mbedtls_gcm_init( &ctx );
  749. if( verbose != 0 )
  750. mbedtls_printf( " AES-GCM-%3d #%d split (%s): ",
  751. key_len, i, "dec" );
  752. ret = mbedtls_gcm_setkey( &ctx, cipher, key[key_index[i]],
  753. key_len );
  754. if( ret != 0 )
  755. goto exit;
  756. ret = mbedtls_gcm_starts( &ctx, MBEDTLS_GCM_DECRYPT,
  757. iv[iv_index[i]], iv_len[i],
  758. additional[add_index[i]], add_len[i] );
  759. if( ret != 0 )
  760. goto exit;
  761. if( pt_len[i] > 32 )
  762. {
  763. size_t rest_len = pt_len[i] - 32;
  764. ret = mbedtls_gcm_update( &ctx, 32, ct[j * 6 + i], buf );
  765. if( ret != 0 )
  766. goto exit;
  767. ret = mbedtls_gcm_update( &ctx, rest_len, ct[j * 6 + i] + 32,
  768. buf + 32 );
  769. if( ret != 0 )
  770. goto exit;
  771. }
  772. else
  773. {
  774. ret = mbedtls_gcm_update( &ctx, pt_len[i], ct[j * 6 + i],
  775. buf );
  776. if( ret != 0 )
  777. goto exit;
  778. }
  779. ret = mbedtls_gcm_finish( &ctx, tag_buf, 16 );
  780. if( ret != 0 )
  781. goto exit;
  782. if( memcmp( buf, pt[pt_index[i]], pt_len[i] ) != 0 ||
  783. memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 )
  784. {
  785. ret = 1;
  786. goto exit;
  787. }
  788. mbedtls_gcm_free( &ctx );
  789. if( verbose != 0 )
  790. mbedtls_printf( "passed\n" );
  791. }
  792. }
  793. if( verbose != 0 )
  794. mbedtls_printf( "\n" );
  795. ret = 0;
  796. exit:
  797. if( ret != 0 )
  798. {
  799. if( verbose != 0 )
  800. mbedtls_printf( "failed\n" );
  801. mbedtls_gcm_free( &ctx );
  802. }
  803. return( ret );
  804. }
  805. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  806. #endif /* MBEDTLS_GCM_C */