nist_kw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Implementation of NIST SP 800-38F key wrapping, supporting KW and KWP modes
  3. * only
  4. *
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. /*
  21. * Definition of Key Wrapping:
  22. * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38F.pdf
  23. * RFC 3394 "Advanced Encryption Standard (AES) Key Wrap Algorithm"
  24. * RFC 5649 "Advanced Encryption Standard (AES) Key Wrap with Padding Algorithm"
  25. *
  26. * Note: RFC 3394 defines different methodology for intermediate operations for
  27. * the wrapping and unwrapping operation than the definition in NIST SP 800-38F.
  28. */
  29. #include "common.h"
  30. #if defined(MBEDTLS_NIST_KW_C)
  31. #include "mbedtls/nist_kw.h"
  32. #include "mbedtls/platform_util.h"
  33. #include "mbedtls/error.h"
  34. #include <stdint.h>
  35. #include <string.h>
  36. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  37. #if defined(MBEDTLS_PLATFORM_C)
  38. #include "mbedtls/platform.h"
  39. #else
  40. #include <stdio.h>
  41. #define mbedtls_printf printf
  42. #endif /* MBEDTLS_PLATFORM_C */
  43. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  44. #if !defined(MBEDTLS_NIST_KW_ALT)
  45. #define KW_SEMIBLOCK_LENGTH 8
  46. #define MIN_SEMIBLOCKS_COUNT 3
  47. /* constant-time buffer comparison */
  48. static inline unsigned char mbedtls_nist_kw_safer_memcmp( const void *a, const void *b, size_t n )
  49. {
  50. size_t i;
  51. volatile const unsigned char *A = (volatile const unsigned char *) a;
  52. volatile const unsigned char *B = (volatile const unsigned char *) b;
  53. volatile unsigned char diff = 0;
  54. for( i = 0; i < n; i++ )
  55. {
  56. /* Read volatile data in order before computing diff.
  57. * This avoids IAR compiler warning:
  58. * 'the order of volatile accesses is undefined ..' */
  59. unsigned char x = A[i], y = B[i];
  60. diff |= x ^ y;
  61. }
  62. return( diff );
  63. }
  64. /*! The 64-bit default integrity check value (ICV) for KW mode. */
  65. static const unsigned char NIST_KW_ICV1[] = {0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6};
  66. /*! The 32-bit default integrity check value (ICV) for KWP mode. */
  67. static const unsigned char NIST_KW_ICV2[] = {0xA6, 0x59, 0x59, 0xA6};
  68. #ifndef GET_UINT32_BE
  69. #define GET_UINT32_BE(n,b,i) \
  70. do { \
  71. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  72. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  73. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  74. | ( (uint32_t) (b)[(i) + 3] ); \
  75. } while( 0 )
  76. #endif
  77. #ifndef PUT_UINT32_BE
  78. #define PUT_UINT32_BE(n,b,i) \
  79. do { \
  80. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  81. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  82. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  83. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  84. } while( 0 )
  85. #endif
  86. /*
  87. * Initialize context
  88. */
  89. void mbedtls_nist_kw_init( mbedtls_nist_kw_context *ctx )
  90. {
  91. memset( ctx, 0, sizeof( mbedtls_nist_kw_context ) );
  92. }
  93. int mbedtls_nist_kw_setkey( mbedtls_nist_kw_context *ctx,
  94. mbedtls_cipher_id_t cipher,
  95. const unsigned char *key,
  96. unsigned int keybits,
  97. const int is_wrap )
  98. {
  99. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  100. const mbedtls_cipher_info_t *cipher_info;
  101. cipher_info = mbedtls_cipher_info_from_values( cipher,
  102. keybits,
  103. MBEDTLS_MODE_ECB );
  104. if( cipher_info == NULL )
  105. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  106. if( cipher_info->block_size != 16 )
  107. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  108. /*
  109. * SP 800-38F currently defines AES cipher as the only block cipher allowed:
  110. * "For KW and KWP, the underlying block cipher shall be approved, and the
  111. * block size shall be 128 bits. Currently, the AES block cipher, with key
  112. * lengths of 128, 192, or 256 bits, is the only block cipher that fits
  113. * this profile."
  114. * Currently we don't support other 128 bit block ciphers for key wrapping,
  115. * such as Camellia and Aria.
  116. */
  117. if( cipher != MBEDTLS_CIPHER_ID_AES )
  118. return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
  119. mbedtls_cipher_free( &ctx->cipher_ctx );
  120. if( ( ret = mbedtls_cipher_setup( &ctx->cipher_ctx, cipher_info ) ) != 0 )
  121. return( ret );
  122. if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keybits,
  123. is_wrap ? MBEDTLS_ENCRYPT :
  124. MBEDTLS_DECRYPT )
  125. ) != 0 )
  126. {
  127. return( ret );
  128. }
  129. return( 0 );
  130. }
  131. /*
  132. * Free context
  133. */
  134. void mbedtls_nist_kw_free( mbedtls_nist_kw_context *ctx )
  135. {
  136. mbedtls_cipher_free( &ctx->cipher_ctx );
  137. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_nist_kw_context ) );
  138. }
  139. /*
  140. * Helper function for Xoring the uint64_t "t" with the encrypted A.
  141. * Defined in NIST SP 800-38F section 6.1
  142. */
  143. static void calc_a_xor_t( unsigned char A[KW_SEMIBLOCK_LENGTH], uint64_t t )
  144. {
  145. size_t i = 0;
  146. for( i = 0; i < sizeof( t ); i++ )
  147. {
  148. A[i] ^= ( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ) & 0xff;
  149. }
  150. }
  151. /*
  152. * KW-AE as defined in SP 800-38F section 6.2
  153. * KWP-AE as defined in SP 800-38F section 6.3
  154. */
  155. int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx,
  156. mbedtls_nist_kw_mode_t mode,
  157. const unsigned char *input, size_t in_len,
  158. unsigned char *output, size_t *out_len, size_t out_size )
  159. {
  160. int ret = 0;
  161. size_t semiblocks = 0;
  162. size_t s;
  163. size_t olen, padlen = 0;
  164. uint64_t t = 0;
  165. unsigned char outbuff[KW_SEMIBLOCK_LENGTH * 2];
  166. unsigned char inbuff[KW_SEMIBLOCK_LENGTH * 2];
  167. *out_len = 0;
  168. /*
  169. * Generate the String to work on
  170. */
  171. if( mode == MBEDTLS_KW_MODE_KW )
  172. {
  173. if( out_size < in_len + KW_SEMIBLOCK_LENGTH )
  174. {
  175. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  176. }
  177. /*
  178. * According to SP 800-38F Table 1, the plaintext length for KW
  179. * must be between 2 to 2^54-1 semiblocks inclusive.
  180. */
  181. if( in_len < 16 ||
  182. #if SIZE_MAX > 0x1FFFFFFFFFFFFF8
  183. in_len > 0x1FFFFFFFFFFFFF8 ||
  184. #endif
  185. in_len % KW_SEMIBLOCK_LENGTH != 0 )
  186. {
  187. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  188. }
  189. memcpy( output, NIST_KW_ICV1, KW_SEMIBLOCK_LENGTH );
  190. memmove( output + KW_SEMIBLOCK_LENGTH, input, in_len );
  191. }
  192. else
  193. {
  194. if( in_len % 8 != 0 )
  195. {
  196. padlen = ( 8 - ( in_len % 8 ) );
  197. }
  198. if( out_size < in_len + KW_SEMIBLOCK_LENGTH + padlen )
  199. {
  200. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  201. }
  202. /*
  203. * According to SP 800-38F Table 1, the plaintext length for KWP
  204. * must be between 1 and 2^32-1 octets inclusive.
  205. */
  206. if( in_len < 1
  207. #if SIZE_MAX > 0xFFFFFFFF
  208. || in_len > 0xFFFFFFFF
  209. #endif
  210. )
  211. {
  212. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  213. }
  214. memcpy( output, NIST_KW_ICV2, KW_SEMIBLOCK_LENGTH / 2 );
  215. PUT_UINT32_BE( ( in_len & 0xffffffff ), output,
  216. KW_SEMIBLOCK_LENGTH / 2 );
  217. memcpy( output + KW_SEMIBLOCK_LENGTH, input, in_len );
  218. memset( output + KW_SEMIBLOCK_LENGTH + in_len, 0, padlen );
  219. }
  220. semiblocks = ( ( in_len + padlen ) / KW_SEMIBLOCK_LENGTH ) + 1;
  221. s = 6 * ( semiblocks - 1 );
  222. if( mode == MBEDTLS_KW_MODE_KWP
  223. && in_len <= KW_SEMIBLOCK_LENGTH )
  224. {
  225. memcpy( inbuff, output, 16 );
  226. ret = mbedtls_cipher_update( &ctx->cipher_ctx,
  227. inbuff, 16, output, &olen );
  228. if( ret != 0 )
  229. goto cleanup;
  230. }
  231. else
  232. {
  233. unsigned char *R2 = output + KW_SEMIBLOCK_LENGTH;
  234. unsigned char *A = output;
  235. /*
  236. * Do the wrapping function W, as defined in RFC 3394 section 2.2.1
  237. */
  238. if( semiblocks < MIN_SEMIBLOCKS_COUNT )
  239. {
  240. ret = MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
  241. goto cleanup;
  242. }
  243. /* Calculate intermediate values */
  244. for( t = 1; t <= s; t++ )
  245. {
  246. memcpy( inbuff, A, KW_SEMIBLOCK_LENGTH );
  247. memcpy( inbuff + KW_SEMIBLOCK_LENGTH, R2, KW_SEMIBLOCK_LENGTH );
  248. ret = mbedtls_cipher_update( &ctx->cipher_ctx,
  249. inbuff, 16, outbuff, &olen );
  250. if( ret != 0 )
  251. goto cleanup;
  252. memcpy( A, outbuff, KW_SEMIBLOCK_LENGTH );
  253. calc_a_xor_t( A, t );
  254. memcpy( R2, outbuff + KW_SEMIBLOCK_LENGTH, KW_SEMIBLOCK_LENGTH );
  255. R2 += KW_SEMIBLOCK_LENGTH;
  256. if( R2 >= output + ( semiblocks * KW_SEMIBLOCK_LENGTH ) )
  257. R2 = output + KW_SEMIBLOCK_LENGTH;
  258. }
  259. }
  260. *out_len = semiblocks * KW_SEMIBLOCK_LENGTH;
  261. cleanup:
  262. if( ret != 0)
  263. {
  264. memset( output, 0, semiblocks * KW_SEMIBLOCK_LENGTH );
  265. }
  266. mbedtls_platform_zeroize( inbuff, KW_SEMIBLOCK_LENGTH * 2 );
  267. mbedtls_platform_zeroize( outbuff, KW_SEMIBLOCK_LENGTH * 2 );
  268. return( ret );
  269. }
  270. /*
  271. * W-1 function as defined in RFC 3394 section 2.2.2
  272. * This function assumes the following:
  273. * 1. Output buffer is at least of size ( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH.
  274. * 2. The input buffer is of size semiblocks * KW_SEMIBLOCK_LENGTH.
  275. * 3. Minimal number of semiblocks is 3.
  276. * 4. A is a buffer to hold the first semiblock of the input buffer.
  277. */
  278. static int unwrap( mbedtls_nist_kw_context *ctx,
  279. const unsigned char *input, size_t semiblocks,
  280. unsigned char A[KW_SEMIBLOCK_LENGTH],
  281. unsigned char *output, size_t* out_len )
  282. {
  283. int ret = 0;
  284. const size_t s = 6 * ( semiblocks - 1 );
  285. size_t olen;
  286. uint64_t t = 0;
  287. unsigned char outbuff[KW_SEMIBLOCK_LENGTH * 2];
  288. unsigned char inbuff[KW_SEMIBLOCK_LENGTH * 2];
  289. unsigned char *R = NULL;
  290. *out_len = 0;
  291. if( semiblocks < MIN_SEMIBLOCKS_COUNT )
  292. {
  293. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  294. }
  295. memcpy( A, input, KW_SEMIBLOCK_LENGTH );
  296. memmove( output, input + KW_SEMIBLOCK_LENGTH, ( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH );
  297. R = output + ( semiblocks - 2 ) * KW_SEMIBLOCK_LENGTH;
  298. /* Calculate intermediate values */
  299. for( t = s; t >= 1; t-- )
  300. {
  301. calc_a_xor_t( A, t );
  302. memcpy( inbuff, A, KW_SEMIBLOCK_LENGTH );
  303. memcpy( inbuff + KW_SEMIBLOCK_LENGTH, R, KW_SEMIBLOCK_LENGTH );
  304. ret = mbedtls_cipher_update( &ctx->cipher_ctx,
  305. inbuff, 16, outbuff, &olen );
  306. if( ret != 0 )
  307. goto cleanup;
  308. memcpy( A, outbuff, KW_SEMIBLOCK_LENGTH );
  309. /* Set R as LSB64 of outbuff */
  310. memcpy( R, outbuff + KW_SEMIBLOCK_LENGTH, KW_SEMIBLOCK_LENGTH );
  311. if( R == output )
  312. R = output + ( semiblocks - 2 ) * KW_SEMIBLOCK_LENGTH;
  313. else
  314. R -= KW_SEMIBLOCK_LENGTH;
  315. }
  316. *out_len = ( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH;
  317. cleanup:
  318. if( ret != 0)
  319. memset( output, 0, ( semiblocks - 1 ) * KW_SEMIBLOCK_LENGTH );
  320. mbedtls_platform_zeroize( inbuff, sizeof( inbuff ) );
  321. mbedtls_platform_zeroize( outbuff, sizeof( outbuff ) );
  322. return( ret );
  323. }
  324. /*
  325. * KW-AD as defined in SP 800-38F section 6.2
  326. * KWP-AD as defined in SP 800-38F section 6.3
  327. */
  328. int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx,
  329. mbedtls_nist_kw_mode_t mode,
  330. const unsigned char *input, size_t in_len,
  331. unsigned char *output, size_t *out_len, size_t out_size )
  332. {
  333. int ret = 0;
  334. size_t i, olen;
  335. unsigned char A[KW_SEMIBLOCK_LENGTH];
  336. unsigned char diff, bad_padding = 0;
  337. *out_len = 0;
  338. if( out_size < in_len - KW_SEMIBLOCK_LENGTH )
  339. {
  340. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  341. }
  342. if( mode == MBEDTLS_KW_MODE_KW )
  343. {
  344. /*
  345. * According to SP 800-38F Table 1, the ciphertext length for KW
  346. * must be between 3 to 2^54 semiblocks inclusive.
  347. */
  348. if( in_len < 24 ||
  349. #if SIZE_MAX > 0x200000000000000
  350. in_len > 0x200000000000000 ||
  351. #endif
  352. in_len % KW_SEMIBLOCK_LENGTH != 0 )
  353. {
  354. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  355. }
  356. ret = unwrap( ctx, input, in_len / KW_SEMIBLOCK_LENGTH,
  357. A, output, out_len );
  358. if( ret != 0 )
  359. goto cleanup;
  360. /* Check ICV in "constant-time" */
  361. diff = mbedtls_nist_kw_safer_memcmp( NIST_KW_ICV1, A, KW_SEMIBLOCK_LENGTH );
  362. if( diff != 0 )
  363. {
  364. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  365. goto cleanup;
  366. }
  367. }
  368. else if( mode == MBEDTLS_KW_MODE_KWP )
  369. {
  370. size_t padlen = 0;
  371. uint32_t Plen;
  372. /*
  373. * According to SP 800-38F Table 1, the ciphertext length for KWP
  374. * must be between 2 to 2^29 semiblocks inclusive.
  375. */
  376. if( in_len < KW_SEMIBLOCK_LENGTH * 2 ||
  377. #if SIZE_MAX > 0x100000000
  378. in_len > 0x100000000 ||
  379. #endif
  380. in_len % KW_SEMIBLOCK_LENGTH != 0 )
  381. {
  382. return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
  383. }
  384. if( in_len == KW_SEMIBLOCK_LENGTH * 2 )
  385. {
  386. unsigned char outbuff[KW_SEMIBLOCK_LENGTH * 2];
  387. ret = mbedtls_cipher_update( &ctx->cipher_ctx,
  388. input, 16, outbuff, &olen );
  389. if( ret != 0 )
  390. goto cleanup;
  391. memcpy( A, outbuff, KW_SEMIBLOCK_LENGTH );
  392. memcpy( output, outbuff + KW_SEMIBLOCK_LENGTH, KW_SEMIBLOCK_LENGTH );
  393. mbedtls_platform_zeroize( outbuff, sizeof( outbuff ) );
  394. *out_len = KW_SEMIBLOCK_LENGTH;
  395. }
  396. else
  397. {
  398. /* in_len >= KW_SEMIBLOCK_LENGTH * 3 */
  399. ret = unwrap( ctx, input, in_len / KW_SEMIBLOCK_LENGTH,
  400. A, output, out_len );
  401. if( ret != 0 )
  402. goto cleanup;
  403. }
  404. /* Check ICV in "constant-time" */
  405. diff = mbedtls_nist_kw_safer_memcmp( NIST_KW_ICV2, A, KW_SEMIBLOCK_LENGTH / 2 );
  406. if( diff != 0 )
  407. {
  408. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  409. }
  410. GET_UINT32_BE( Plen, A, KW_SEMIBLOCK_LENGTH / 2 );
  411. /*
  412. * Plen is the length of the plaintext, when the input is valid.
  413. * If Plen is larger than the plaintext and padding, padlen will be
  414. * larger than 8, because of the type wrap around.
  415. */
  416. padlen = in_len - KW_SEMIBLOCK_LENGTH - Plen;
  417. if ( padlen > 7 )
  418. {
  419. padlen &= 7;
  420. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  421. }
  422. /* Check padding in "constant-time" */
  423. for( diff = 0, i = 0; i < KW_SEMIBLOCK_LENGTH; i++ )
  424. {
  425. if( i >= KW_SEMIBLOCK_LENGTH - padlen )
  426. diff |= output[*out_len - KW_SEMIBLOCK_LENGTH + i];
  427. else
  428. bad_padding |= output[*out_len - KW_SEMIBLOCK_LENGTH + i];
  429. }
  430. if( diff != 0 )
  431. {
  432. ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
  433. }
  434. if( ret != 0 )
  435. {
  436. goto cleanup;
  437. }
  438. memset( output + Plen, 0, padlen );
  439. *out_len = Plen;
  440. }
  441. else
  442. {
  443. ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
  444. goto cleanup;
  445. }
  446. cleanup:
  447. if( ret != 0 )
  448. {
  449. memset( output, 0, *out_len );
  450. *out_len = 0;
  451. }
  452. mbedtls_platform_zeroize( &bad_padding, sizeof( bad_padding) );
  453. mbedtls_platform_zeroize( &diff, sizeof( diff ) );
  454. mbedtls_platform_zeroize( A, sizeof( A ) );
  455. return( ret );
  456. }
  457. #endif /* !MBEDTLS_NIST_KW_ALT */
  458. #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
  459. #define KW_TESTS 3
  460. /*
  461. * Test vectors taken from NIST
  462. * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/CAVP-TESTING-BLOCK-CIPHER-MODES#KW
  463. */
  464. static const unsigned int key_len[KW_TESTS] = { 16, 24, 32 };
  465. static const unsigned char kw_key[KW_TESTS][32] = {
  466. { 0x75, 0x75, 0xda, 0x3a, 0x93, 0x60, 0x7c, 0xc2,
  467. 0xbf, 0xd8, 0xce, 0xc7, 0xaa, 0xdf, 0xd9, 0xa6 },
  468. { 0x2d, 0x85, 0x26, 0x08, 0x1d, 0x02, 0xfb, 0x5b,
  469. 0x85, 0xf6, 0x9a, 0xc2, 0x86, 0xec, 0xd5, 0x7d,
  470. 0x40, 0xdf, 0x5d, 0xf3, 0x49, 0x47, 0x44, 0xd3 },
  471. { 0x11, 0x2a, 0xd4, 0x1b, 0x48, 0x56, 0xc7, 0x25,
  472. 0x4a, 0x98, 0x48, 0xd3, 0x0f, 0xdd, 0x78, 0x33,
  473. 0x5b, 0x03, 0x9a, 0x48, 0xa8, 0x96, 0x2c, 0x4d,
  474. 0x1c, 0xb7, 0x8e, 0xab, 0xd5, 0xda, 0xd7, 0x88 }
  475. };
  476. static const unsigned char kw_msg[KW_TESTS][40] = {
  477. { 0x42, 0x13, 0x6d, 0x3c, 0x38, 0x4a, 0x3e, 0xea,
  478. 0xc9, 0x5a, 0x06, 0x6f, 0xd2, 0x8f, 0xed, 0x3f },
  479. { 0x95, 0xc1, 0x1b, 0xf5, 0x35, 0x3a, 0xfe, 0xdb,
  480. 0x98, 0xfd, 0xd6, 0xc8, 0xca, 0x6f, 0xdb, 0x6d,
  481. 0xa5, 0x4b, 0x74, 0xb4, 0x99, 0x0f, 0xdc, 0x45,
  482. 0xc0, 0x9d, 0x15, 0x8f, 0x51, 0xce, 0x62, 0x9d,
  483. 0xe2, 0xaf, 0x26, 0xe3, 0x25, 0x0e, 0x6b, 0x4c },
  484. { 0x1b, 0x20, 0xbf, 0x19, 0x90, 0xb0, 0x65, 0xd7,
  485. 0x98, 0xe1, 0xb3, 0x22, 0x64, 0xad, 0x50, 0xa8,
  486. 0x74, 0x74, 0x92, 0xba, 0x09, 0xa0, 0x4d, 0xd1 }
  487. };
  488. static const size_t kw_msg_len[KW_TESTS] = { 16, 40, 24 };
  489. static const size_t kw_out_len[KW_TESTS] = { 24, 48, 32 };
  490. static const unsigned char kw_res[KW_TESTS][48] = {
  491. { 0x03, 0x1f, 0x6b, 0xd7, 0xe6, 0x1e, 0x64, 0x3d,
  492. 0xf6, 0x85, 0x94, 0x81, 0x6f, 0x64, 0xca, 0xa3,
  493. 0xf5, 0x6f, 0xab, 0xea, 0x25, 0x48, 0xf5, 0xfb },
  494. { 0x44, 0x3c, 0x6f, 0x15, 0x09, 0x83, 0x71, 0x91,
  495. 0x3e, 0x5c, 0x81, 0x4c, 0xa1, 0xa0, 0x42, 0xec,
  496. 0x68, 0x2f, 0x7b, 0x13, 0x6d, 0x24, 0x3a, 0x4d,
  497. 0x6c, 0x42, 0x6f, 0xc6, 0x97, 0x15, 0x63, 0xe8,
  498. 0xa1, 0x4a, 0x55, 0x8e, 0x09, 0x64, 0x16, 0x19,
  499. 0xbf, 0x03, 0xfc, 0xaf, 0x90, 0xb1, 0xfc, 0x2d },
  500. { 0xba, 0x8a, 0x25, 0x9a, 0x47, 0x1b, 0x78, 0x7d,
  501. 0xd5, 0xd5, 0x40, 0xec, 0x25, 0xd4, 0x3d, 0x87,
  502. 0x20, 0x0f, 0xda, 0xdc, 0x6d, 0x1f, 0x05, 0xd9,
  503. 0x16, 0x58, 0x4f, 0xa9, 0xf6, 0xcb, 0xf5, 0x12 }
  504. };
  505. static const unsigned char kwp_key[KW_TESTS][32] = {
  506. { 0x78, 0x65, 0xe2, 0x0f, 0x3c, 0x21, 0x65, 0x9a,
  507. 0xb4, 0x69, 0x0b, 0x62, 0x9c, 0xdf, 0x3c, 0xc4 },
  508. { 0xf5, 0xf8, 0x96, 0xa3, 0xbd, 0x2f, 0x4a, 0x98,
  509. 0x23, 0xef, 0x16, 0x2b, 0x00, 0xb8, 0x05, 0xd7,
  510. 0xde, 0x1e, 0xa4, 0x66, 0x26, 0x96, 0xa2, 0x58 },
  511. { 0x95, 0xda, 0x27, 0x00, 0xca, 0x6f, 0xd9, 0xa5,
  512. 0x25, 0x54, 0xee, 0x2a, 0x8d, 0xf1, 0x38, 0x6f,
  513. 0x5b, 0x94, 0xa1, 0xa6, 0x0e, 0xd8, 0xa4, 0xae,
  514. 0xf6, 0x0a, 0x8d, 0x61, 0xab, 0x5f, 0x22, 0x5a }
  515. };
  516. static const unsigned char kwp_msg[KW_TESTS][31] = {
  517. { 0xbd, 0x68, 0x43, 0xd4, 0x20, 0x37, 0x8d, 0xc8,
  518. 0x96 },
  519. { 0x6c, 0xcd, 0xd5, 0x85, 0x18, 0x40, 0x97, 0xeb,
  520. 0xd5, 0xc3, 0xaf, 0x3e, 0x47, 0xd0, 0x2c, 0x19,
  521. 0x14, 0x7b, 0x4d, 0x99, 0x5f, 0x96, 0x43, 0x66,
  522. 0x91, 0x56, 0x75, 0x8c, 0x13, 0x16, 0x8f },
  523. { 0xd1 }
  524. };
  525. static const size_t kwp_msg_len[KW_TESTS] = { 9, 31, 1 };
  526. static const unsigned char kwp_res[KW_TESTS][48] = {
  527. { 0x41, 0xec, 0xa9, 0x56, 0xd4, 0xaa, 0x04, 0x7e,
  528. 0xb5, 0xcf, 0x4e, 0xfe, 0x65, 0x96, 0x61, 0xe7,
  529. 0x4d, 0xb6, 0xf8, 0xc5, 0x64, 0xe2, 0x35, 0x00 },
  530. { 0x4e, 0x9b, 0xc2, 0xbc, 0xbc, 0x6c, 0x1e, 0x13,
  531. 0xd3, 0x35, 0xbc, 0xc0, 0xf7, 0x73, 0x6a, 0x88,
  532. 0xfa, 0x87, 0x53, 0x66, 0x15, 0xbb, 0x8e, 0x63,
  533. 0x8b, 0xcc, 0x81, 0x66, 0x84, 0x68, 0x17, 0x90,
  534. 0x67, 0xcf, 0xa9, 0x8a, 0x9d, 0x0e, 0x33, 0x26 },
  535. { 0x06, 0xba, 0x7a, 0xe6, 0xf3, 0x24, 0x8c, 0xfd,
  536. 0xcf, 0x26, 0x75, 0x07, 0xfa, 0x00, 0x1b, 0xc4 }
  537. };
  538. static const size_t kwp_out_len[KW_TESTS] = { 24, 40, 16 };
  539. int mbedtls_nist_kw_self_test( int verbose )
  540. {
  541. mbedtls_nist_kw_context ctx;
  542. unsigned char out[48];
  543. size_t olen;
  544. int i;
  545. int ret = 0;
  546. mbedtls_nist_kw_init( &ctx );
  547. for( i = 0; i < KW_TESTS; i++ )
  548. {
  549. if( verbose != 0 )
  550. mbedtls_printf( " KW-AES-%u ", (unsigned int) key_len[i] * 8 );
  551. ret = mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
  552. kw_key[i], key_len[i] * 8, 1 );
  553. if( ret != 0 )
  554. {
  555. if( verbose != 0 )
  556. mbedtls_printf( " KW: setup failed " );
  557. goto end;
  558. }
  559. ret = mbedtls_nist_kw_wrap( &ctx, MBEDTLS_KW_MODE_KW, kw_msg[i],
  560. kw_msg_len[i], out, &olen, sizeof( out ) );
  561. if( ret != 0 || kw_out_len[i] != olen ||
  562. memcmp( out, kw_res[i], kw_out_len[i] ) != 0 )
  563. {
  564. if( verbose != 0 )
  565. mbedtls_printf( "failed. ");
  566. ret = 1;
  567. goto end;
  568. }
  569. if( ( ret = mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
  570. kw_key[i], key_len[i] * 8, 0 ) )
  571. != 0 )
  572. {
  573. if( verbose != 0 )
  574. mbedtls_printf( " KW: setup failed ");
  575. goto end;
  576. }
  577. ret = mbedtls_nist_kw_unwrap( &ctx, MBEDTLS_KW_MODE_KW,
  578. out, olen, out, &olen, sizeof( out ) );
  579. if( ret != 0 || olen != kw_msg_len[i] ||
  580. memcmp( out, kw_msg[i], kw_msg_len[i] ) != 0 )
  581. {
  582. if( verbose != 0 )
  583. mbedtls_printf( "failed\n" );
  584. ret = 1;
  585. goto end;
  586. }
  587. if( verbose != 0 )
  588. mbedtls_printf( " passed\n" );
  589. }
  590. for( i = 0; i < KW_TESTS; i++ )
  591. {
  592. olen = sizeof( out );
  593. if( verbose != 0 )
  594. mbedtls_printf( " KWP-AES-%u ", (unsigned int) key_len[i] * 8 );
  595. ret = mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES, kwp_key[i],
  596. key_len[i] * 8, 1 );
  597. if( ret != 0 )
  598. {
  599. if( verbose != 0 )
  600. mbedtls_printf( " KWP: setup failed " );
  601. goto end;
  602. }
  603. ret = mbedtls_nist_kw_wrap( &ctx, MBEDTLS_KW_MODE_KWP, kwp_msg[i],
  604. kwp_msg_len[i], out, &olen, sizeof( out ) );
  605. if( ret != 0 || kwp_out_len[i] != olen ||
  606. memcmp( out, kwp_res[i], kwp_out_len[i] ) != 0 )
  607. {
  608. if( verbose != 0 )
  609. mbedtls_printf( "failed. ");
  610. ret = 1;
  611. goto end;
  612. }
  613. if( ( ret = mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
  614. kwp_key[i], key_len[i] * 8, 0 ) )
  615. != 0 )
  616. {
  617. if( verbose != 0 )
  618. mbedtls_printf( " KWP: setup failed ");
  619. goto end;
  620. }
  621. ret = mbedtls_nist_kw_unwrap( &ctx, MBEDTLS_KW_MODE_KWP, out,
  622. olen, out, &olen, sizeof( out ) );
  623. if( ret != 0 || olen != kwp_msg_len[i] ||
  624. memcmp( out, kwp_msg[i], kwp_msg_len[i] ) != 0 )
  625. {
  626. if( verbose != 0 )
  627. mbedtls_printf( "failed. ");
  628. ret = 1;
  629. goto end;
  630. }
  631. if( verbose != 0 )
  632. mbedtls_printf( " passed\n" );
  633. }
  634. end:
  635. mbedtls_nist_kw_free( &ctx );
  636. if( verbose != 0 )
  637. mbedtls_printf( "\n" );
  638. return( ret );
  639. }
  640. #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
  641. #endif /* MBEDTLS_NIST_KW_C */