2
0

nist_kw.c 25 KB

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