gcm.c 33 KB

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