nist_kw.c 21 KB

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