nist_kw.c 22 KB

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