sha3.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * FIPS-202 compliant SHA3 implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The SHA-3 Secure Hash Standard was published by NIST in 2015.
  9. *
  10. * https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.202.pdf
  11. */
  12. #include "common.h"
  13. #if defined(MBEDTLS_SHA3_C)
  14. /*
  15. * These macros select manually unrolled implementations of parts of the main permutation function.
  16. *
  17. * Unrolling has a major impact on both performance and code size. gcc performance benefits a lot
  18. * from manually unrolling at higher optimisation levels.
  19. *
  20. * Depending on your size/perf priorities, compiler and target, it may be beneficial to adjust
  21. * these; the defaults here should give sensible trade-offs for gcc and clang on aarch64 and
  22. * x86-64.
  23. */
  24. #if !defined(MBEDTLS_SHA3_THETA_UNROLL)
  25. #define MBEDTLS_SHA3_THETA_UNROLL 0 //no-check-names
  26. #endif
  27. #if !defined(MBEDTLS_SHA3_CHI_UNROLL)
  28. #if defined(__OPTIMIZE_SIZE__)
  29. #define MBEDTLS_SHA3_CHI_UNROLL 0 //no-check-names
  30. #else
  31. #define MBEDTLS_SHA3_CHI_UNROLL 1 //no-check-names
  32. #endif
  33. #endif
  34. #if !defined(MBEDTLS_SHA3_PI_UNROLL)
  35. #define MBEDTLS_SHA3_PI_UNROLL 1 //no-check-names
  36. #endif
  37. #if !defined(MBEDTLS_SHA3_RHO_UNROLL)
  38. #define MBEDTLS_SHA3_RHO_UNROLL 1 //no-check-names
  39. #endif
  40. #include "mbedtls/sha3.h"
  41. #include "mbedtls/platform_util.h"
  42. #include "mbedtls/error.h"
  43. #include <string.h>
  44. #if defined(MBEDTLS_SELF_TEST)
  45. #include "mbedtls/platform.h"
  46. #endif /* MBEDTLS_SELF_TEST */
  47. #define XOR_BYTE 0x6
  48. /* Precomputed masks for the iota transform.
  49. *
  50. * Each round uses a 64-bit mask value. In each mask values, only
  51. * bits whose position is of the form 2^k-1 can be set, thus only
  52. * 7 of 64 bits of the mask need to be known for each mask value.
  53. *
  54. * We use a compressed encoding of the mask where bits 63, 31 and 15
  55. * are moved to bits 4-6. This allows us to make each mask value
  56. * 1 byte rather than 8 bytes, saving 7*24 = 168 bytes of data (with
  57. * perhaps a little variation due to alignment). Decompressing this
  58. * requires a little code, but much less than the savings on the table.
  59. *
  60. * The impact on performance depends on the platform and compiler.
  61. * There's a bit more computation, but less memory bandwidth. A quick
  62. * benchmark on x86_64 shows a 7% speed improvement with GCC and a
  63. * 5% speed penalty with Clang, compared to the naive uint64_t[24] table.
  64. * YMMV.
  65. */
  66. /* Helper macro to set the values of the higher bits in unused low positions */
  67. #define H(b63, b31, b15) (b63 << 6 | b31 << 5 | b15 << 4)
  68. static const uint8_t iota_r_packed[24] = {
  69. H(0, 0, 0) | 0x01, H(0, 0, 1) | 0x82, H(1, 0, 1) | 0x8a, H(1, 1, 1) | 0x00,
  70. H(0, 0, 1) | 0x8b, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x09,
  71. H(0, 0, 0) | 0x8a, H(0, 0, 0) | 0x88, H(0, 1, 1) | 0x09, H(0, 1, 0) | 0x0a,
  72. H(0, 1, 1) | 0x8b, H(1, 0, 0) | 0x8b, H(1, 0, 1) | 0x89, H(1, 0, 1) | 0x03,
  73. H(1, 0, 1) | 0x02, H(1, 0, 0) | 0x80, H(0, 0, 1) | 0x0a, H(1, 1, 0) | 0x0a,
  74. H(1, 1, 1) | 0x81, H(1, 0, 1) | 0x80, H(0, 1, 0) | 0x01, H(1, 1, 1) | 0x08,
  75. };
  76. #undef H
  77. static const uint32_t rho[6] = {
  78. 0x3f022425, 0x1c143a09, 0x2c3d3615, 0x27191713, 0x312b382e, 0x3e030832
  79. };
  80. static const uint32_t pi[6] = {
  81. 0x110b070a, 0x10050312, 0x04181508, 0x0d13170f, 0x0e14020c, 0x01060916
  82. };
  83. #define ROTR64(x, y) (((x) << (64U - (y))) | ((x) >> (y))) // 64-bit rotate right
  84. #define ABSORB(ctx, idx, v) do { ctx->state[(idx) >> 3] ^= ((uint64_t) (v)) << (((idx) & 0x7) << 3); \
  85. } while (0)
  86. #define SQUEEZE(ctx, idx) ((uint8_t) (ctx->state[(idx) >> 3] >> (((idx) & 0x7) << 3)))
  87. #define SWAP(x, y) do { uint64_t tmp = (x); (x) = (y); (y) = tmp; } while (0)
  88. /* The permutation function. */
  89. static void keccak_f1600(mbedtls_sha3_context *ctx)
  90. {
  91. uint64_t lane[5];
  92. uint64_t *s = ctx->state;
  93. int i;
  94. for (int round = 0; round < 24; round++) {
  95. uint64_t t;
  96. /* Theta */
  97. #if MBEDTLS_SHA3_THETA_UNROLL == 0 //no-check-names
  98. for (i = 0; i < 5; i++) {
  99. lane[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20];
  100. }
  101. for (i = 0; i < 5; i++) {
  102. t = lane[(i + 4) % 5] ^ ROTR64(lane[(i + 1) % 5], 63);
  103. s[i] ^= t; s[i + 5] ^= t; s[i + 10] ^= t; s[i + 15] ^= t; s[i + 20] ^= t;
  104. }
  105. #else
  106. lane[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];
  107. lane[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];
  108. lane[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];
  109. lane[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];
  110. lane[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];
  111. t = lane[4] ^ ROTR64(lane[1], 63);
  112. s[0] ^= t; s[5] ^= t; s[10] ^= t; s[15] ^= t; s[20] ^= t;
  113. t = lane[0] ^ ROTR64(lane[2], 63);
  114. s[1] ^= t; s[6] ^= t; s[11] ^= t; s[16] ^= t; s[21] ^= t;
  115. t = lane[1] ^ ROTR64(lane[3], 63);
  116. s[2] ^= t; s[7] ^= t; s[12] ^= t; s[17] ^= t; s[22] ^= t;
  117. t = lane[2] ^ ROTR64(lane[4], 63);
  118. s[3] ^= t; s[8] ^= t; s[13] ^= t; s[18] ^= t; s[23] ^= t;
  119. t = lane[3] ^ ROTR64(lane[0], 63);
  120. s[4] ^= t; s[9] ^= t; s[14] ^= t; s[19] ^= t; s[24] ^= t;
  121. #endif
  122. /* Rho */
  123. for (i = 1; i < 25; i += 4) {
  124. uint32_t r = rho[(i - 1) >> 2];
  125. #if MBEDTLS_SHA3_RHO_UNROLL == 0
  126. for (int j = i; j < i + 4; j++) {
  127. uint8_t r8 = (uint8_t) (r >> 24);
  128. r <<= 8;
  129. s[j] = ROTR64(s[j], r8);
  130. }
  131. #else
  132. s[i + 0] = ROTR64(s[i + 0], MBEDTLS_BYTE_3(r));
  133. s[i + 1] = ROTR64(s[i + 1], MBEDTLS_BYTE_2(r));
  134. s[i + 2] = ROTR64(s[i + 2], MBEDTLS_BYTE_1(r));
  135. s[i + 3] = ROTR64(s[i + 3], MBEDTLS_BYTE_0(r));
  136. #endif
  137. }
  138. /* Pi */
  139. t = s[1];
  140. #if MBEDTLS_SHA3_PI_UNROLL == 0
  141. for (i = 0; i < 24; i += 4) {
  142. uint32_t p = pi[i >> 2];
  143. for (unsigned j = 0; j < 4; j++) {
  144. SWAP(s[p & 0xff], t);
  145. p >>= 8;
  146. }
  147. }
  148. #else
  149. uint32_t p = pi[0];
  150. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  151. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  152. p = pi[1];
  153. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  154. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  155. p = pi[2];
  156. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  157. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  158. p = pi[3];
  159. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  160. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  161. p = pi[4];
  162. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  163. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  164. p = pi[5];
  165. SWAP(s[MBEDTLS_BYTE_0(p)], t); SWAP(s[MBEDTLS_BYTE_1(p)], t);
  166. SWAP(s[MBEDTLS_BYTE_2(p)], t); SWAP(s[MBEDTLS_BYTE_3(p)], t);
  167. #endif
  168. /* Chi */
  169. #if MBEDTLS_SHA3_CHI_UNROLL == 0 //no-check-names
  170. for (i = 0; i <= 20; i += 5) {
  171. lane[0] = s[i]; lane[1] = s[i + 1]; lane[2] = s[i + 2];
  172. lane[3] = s[i + 3]; lane[4] = s[i + 4];
  173. s[i + 0] ^= (~lane[1]) & lane[2];
  174. s[i + 1] ^= (~lane[2]) & lane[3];
  175. s[i + 2] ^= (~lane[3]) & lane[4];
  176. s[i + 3] ^= (~lane[4]) & lane[0];
  177. s[i + 4] ^= (~lane[0]) & lane[1];
  178. }
  179. #else
  180. lane[0] = s[0]; lane[1] = s[1]; lane[2] = s[2]; lane[3] = s[3]; lane[4] = s[4];
  181. s[0] ^= (~lane[1]) & lane[2];
  182. s[1] ^= (~lane[2]) & lane[3];
  183. s[2] ^= (~lane[3]) & lane[4];
  184. s[3] ^= (~lane[4]) & lane[0];
  185. s[4] ^= (~lane[0]) & lane[1];
  186. lane[0] = s[5]; lane[1] = s[6]; lane[2] = s[7]; lane[3] = s[8]; lane[4] = s[9];
  187. s[5] ^= (~lane[1]) & lane[2];
  188. s[6] ^= (~lane[2]) & lane[3];
  189. s[7] ^= (~lane[3]) & lane[4];
  190. s[8] ^= (~lane[4]) & lane[0];
  191. s[9] ^= (~lane[0]) & lane[1];
  192. lane[0] = s[10]; lane[1] = s[11]; lane[2] = s[12]; lane[3] = s[13]; lane[4] = s[14];
  193. s[10] ^= (~lane[1]) & lane[2];
  194. s[11] ^= (~lane[2]) & lane[3];
  195. s[12] ^= (~lane[3]) & lane[4];
  196. s[13] ^= (~lane[4]) & lane[0];
  197. s[14] ^= (~lane[0]) & lane[1];
  198. lane[0] = s[15]; lane[1] = s[16]; lane[2] = s[17]; lane[3] = s[18]; lane[4] = s[19];
  199. s[15] ^= (~lane[1]) & lane[2];
  200. s[16] ^= (~lane[2]) & lane[3];
  201. s[17] ^= (~lane[3]) & lane[4];
  202. s[18] ^= (~lane[4]) & lane[0];
  203. s[19] ^= (~lane[0]) & lane[1];
  204. lane[0] = s[20]; lane[1] = s[21]; lane[2] = s[22]; lane[3] = s[23]; lane[4] = s[24];
  205. s[20] ^= (~lane[1]) & lane[2];
  206. s[21] ^= (~lane[2]) & lane[3];
  207. s[22] ^= (~lane[3]) & lane[4];
  208. s[23] ^= (~lane[4]) & lane[0];
  209. s[24] ^= (~lane[0]) & lane[1];
  210. #endif
  211. /* Iota */
  212. /* Decompress the round masks (see definition of rc) */
  213. s[0] ^= ((iota_r_packed[round] & 0x40ull) << 57 |
  214. (iota_r_packed[round] & 0x20ull) << 26 |
  215. (iota_r_packed[round] & 0x10ull) << 11 |
  216. (iota_r_packed[round] & 0x8f));
  217. }
  218. }
  219. void mbedtls_sha3_init(mbedtls_sha3_context *ctx)
  220. {
  221. memset(ctx, 0, sizeof(mbedtls_sha3_context));
  222. }
  223. void mbedtls_sha3_free(mbedtls_sha3_context *ctx)
  224. {
  225. if (ctx == NULL) {
  226. return;
  227. }
  228. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha3_context));
  229. }
  230. void mbedtls_sha3_clone(mbedtls_sha3_context *dst,
  231. const mbedtls_sha3_context *src)
  232. {
  233. *dst = *src;
  234. }
  235. /*
  236. * SHA-3 context setup
  237. */
  238. int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id)
  239. {
  240. switch (id) {
  241. case MBEDTLS_SHA3_224:
  242. ctx->olen = 224 / 8;
  243. ctx->max_block_size = 1152 / 8;
  244. break;
  245. case MBEDTLS_SHA3_256:
  246. ctx->olen = 256 / 8;
  247. ctx->max_block_size = 1088 / 8;
  248. break;
  249. case MBEDTLS_SHA3_384:
  250. ctx->olen = 384 / 8;
  251. ctx->max_block_size = 832 / 8;
  252. break;
  253. case MBEDTLS_SHA3_512:
  254. ctx->olen = 512 / 8;
  255. ctx->max_block_size = 576 / 8;
  256. break;
  257. default:
  258. return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA;
  259. }
  260. memset(ctx->state, 0, sizeof(ctx->state));
  261. ctx->index = 0;
  262. return 0;
  263. }
  264. /*
  265. * SHA-3 process buffer
  266. */
  267. int mbedtls_sha3_update(mbedtls_sha3_context *ctx,
  268. const uint8_t *input,
  269. size_t ilen)
  270. {
  271. if (ilen >= 8) {
  272. // 8-byte align index
  273. int align_bytes = 8 - (ctx->index % 8);
  274. if (align_bytes) {
  275. for (; align_bytes > 0; align_bytes--) {
  276. ABSORB(ctx, ctx->index, *input++);
  277. ilen--;
  278. ctx->index++;
  279. }
  280. if ((ctx->index = ctx->index % ctx->max_block_size) == 0) {
  281. keccak_f1600(ctx);
  282. }
  283. }
  284. // process input in 8-byte chunks
  285. while (ilen >= 8) {
  286. ABSORB(ctx, ctx->index, MBEDTLS_GET_UINT64_LE(input, 0));
  287. input += 8;
  288. ilen -= 8;
  289. if ((ctx->index = (ctx->index + 8) % ctx->max_block_size) == 0) {
  290. keccak_f1600(ctx);
  291. }
  292. }
  293. }
  294. // handle remaining bytes
  295. while (ilen-- > 0) {
  296. ABSORB(ctx, ctx->index, *input++);
  297. if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) {
  298. keccak_f1600(ctx);
  299. }
  300. }
  301. return 0;
  302. }
  303. int mbedtls_sha3_finish(mbedtls_sha3_context *ctx,
  304. uint8_t *output, size_t olen)
  305. {
  306. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  307. /* Catch SHA-3 families, with fixed output length */
  308. if (ctx->olen > 0) {
  309. if (ctx->olen > olen) {
  310. ret = MBEDTLS_ERR_SHA3_BAD_INPUT_DATA;
  311. goto exit;
  312. }
  313. olen = ctx->olen;
  314. }
  315. ABSORB(ctx, ctx->index, XOR_BYTE);
  316. ABSORB(ctx, ctx->max_block_size - 1, 0x80);
  317. keccak_f1600(ctx);
  318. ctx->index = 0;
  319. while (olen-- > 0) {
  320. *output++ = SQUEEZE(ctx, ctx->index);
  321. if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) {
  322. keccak_f1600(ctx);
  323. }
  324. }
  325. ret = 0;
  326. exit:
  327. mbedtls_sha3_free(ctx);
  328. return ret;
  329. }
  330. /*
  331. * output = SHA-3( input buffer )
  332. */
  333. int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input,
  334. size_t ilen, uint8_t *output, size_t olen)
  335. {
  336. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  337. mbedtls_sha3_context ctx;
  338. mbedtls_sha3_init(&ctx);
  339. /* Sanity checks are performed in every mbedtls_sha3_xxx() */
  340. if ((ret = mbedtls_sha3_starts(&ctx, id)) != 0) {
  341. goto exit;
  342. }
  343. if ((ret = mbedtls_sha3_update(&ctx, input, ilen)) != 0) {
  344. goto exit;
  345. }
  346. if ((ret = mbedtls_sha3_finish(&ctx, output, olen)) != 0) {
  347. goto exit;
  348. }
  349. exit:
  350. mbedtls_sha3_free(&ctx);
  351. return ret;
  352. }
  353. /**************** Self-tests ****************/
  354. #if defined(MBEDTLS_SELF_TEST)
  355. static const unsigned char test_data[2][4] =
  356. {
  357. "",
  358. "abc",
  359. };
  360. static const size_t test_data_len[2] =
  361. {
  362. 0, /* "" */
  363. 3 /* "abc" */
  364. };
  365. static const unsigned char test_hash_sha3_224[2][28] =
  366. {
  367. { /* "" */
  368. 0x6B, 0x4E, 0x03, 0x42, 0x36, 0x67, 0xDB, 0xB7,
  369. 0x3B, 0x6E, 0x15, 0x45, 0x4F, 0x0E, 0xB1, 0xAB,
  370. 0xD4, 0x59, 0x7F, 0x9A, 0x1B, 0x07, 0x8E, 0x3F,
  371. 0x5B, 0x5A, 0x6B, 0xC7
  372. },
  373. { /* "abc" */
  374. 0xE6, 0x42, 0x82, 0x4C, 0x3F, 0x8C, 0xF2, 0x4A,
  375. 0xD0, 0x92, 0x34, 0xEE, 0x7D, 0x3C, 0x76, 0x6F,
  376. 0xC9, 0xA3, 0xA5, 0x16, 0x8D, 0x0C, 0x94, 0xAD,
  377. 0x73, 0xB4, 0x6F, 0xDF
  378. }
  379. };
  380. static const unsigned char test_hash_sha3_256[2][32] =
  381. {
  382. { /* "" */
  383. 0xA7, 0xFF, 0xC6, 0xF8, 0xBF, 0x1E, 0xD7, 0x66,
  384. 0x51, 0xC1, 0x47, 0x56, 0xA0, 0x61, 0xD6, 0x62,
  385. 0xF5, 0x80, 0xFF, 0x4D, 0xE4, 0x3B, 0x49, 0xFA,
  386. 0x82, 0xD8, 0x0A, 0x4B, 0x80, 0xF8, 0x43, 0x4A
  387. },
  388. { /* "abc" */
  389. 0x3A, 0x98, 0x5D, 0xA7, 0x4F, 0xE2, 0x25, 0xB2,
  390. 0x04, 0x5C, 0x17, 0x2D, 0x6B, 0xD3, 0x90, 0xBD,
  391. 0x85, 0x5F, 0x08, 0x6E, 0x3E, 0x9D, 0x52, 0x5B,
  392. 0x46, 0xBF, 0xE2, 0x45, 0x11, 0x43, 0x15, 0x32
  393. }
  394. };
  395. static const unsigned char test_hash_sha3_384[2][48] =
  396. {
  397. { /* "" */
  398. 0x0C, 0x63, 0xA7, 0x5B, 0x84, 0x5E, 0x4F, 0x7D,
  399. 0x01, 0x10, 0x7D, 0x85, 0x2E, 0x4C, 0x24, 0x85,
  400. 0xC5, 0x1A, 0x50, 0xAA, 0xAA, 0x94, 0xFC, 0x61,
  401. 0x99, 0x5E, 0x71, 0xBB, 0xEE, 0x98, 0x3A, 0x2A,
  402. 0xC3, 0x71, 0x38, 0x31, 0x26, 0x4A, 0xDB, 0x47,
  403. 0xFB, 0x6B, 0xD1, 0xE0, 0x58, 0xD5, 0xF0, 0x04
  404. },
  405. { /* "abc" */
  406. 0xEC, 0x01, 0x49, 0x82, 0x88, 0x51, 0x6F, 0xC9,
  407. 0x26, 0x45, 0x9F, 0x58, 0xE2, 0xC6, 0xAD, 0x8D,
  408. 0xF9, 0xB4, 0x73, 0xCB, 0x0F, 0xC0, 0x8C, 0x25,
  409. 0x96, 0xDA, 0x7C, 0xF0, 0xE4, 0x9B, 0xE4, 0xB2,
  410. 0x98, 0xD8, 0x8C, 0xEA, 0x92, 0x7A, 0xC7, 0xF5,
  411. 0x39, 0xF1, 0xED, 0xF2, 0x28, 0x37, 0x6D, 0x25
  412. }
  413. };
  414. static const unsigned char test_hash_sha3_512[2][64] =
  415. {
  416. { /* "" */
  417. 0xA6, 0x9F, 0x73, 0xCC, 0xA2, 0x3A, 0x9A, 0xC5,
  418. 0xC8, 0xB5, 0x67, 0xDC, 0x18, 0x5A, 0x75, 0x6E,
  419. 0x97, 0xC9, 0x82, 0x16, 0x4F, 0xE2, 0x58, 0x59,
  420. 0xE0, 0xD1, 0xDC, 0xC1, 0x47, 0x5C, 0x80, 0xA6,
  421. 0x15, 0xB2, 0x12, 0x3A, 0xF1, 0xF5, 0xF9, 0x4C,
  422. 0x11, 0xE3, 0xE9, 0x40, 0x2C, 0x3A, 0xC5, 0x58,
  423. 0xF5, 0x00, 0x19, 0x9D, 0x95, 0xB6, 0xD3, 0xE3,
  424. 0x01, 0x75, 0x85, 0x86, 0x28, 0x1D, 0xCD, 0x26
  425. },
  426. { /* "abc" */
  427. 0xB7, 0x51, 0x85, 0x0B, 0x1A, 0x57, 0x16, 0x8A,
  428. 0x56, 0x93, 0xCD, 0x92, 0x4B, 0x6B, 0x09, 0x6E,
  429. 0x08, 0xF6, 0x21, 0x82, 0x74, 0x44, 0xF7, 0x0D,
  430. 0x88, 0x4F, 0x5D, 0x02, 0x40, 0xD2, 0x71, 0x2E,
  431. 0x10, 0xE1, 0x16, 0xE9, 0x19, 0x2A, 0xF3, 0xC9,
  432. 0x1A, 0x7E, 0xC5, 0x76, 0x47, 0xE3, 0x93, 0x40,
  433. 0x57, 0x34, 0x0B, 0x4C, 0xF4, 0x08, 0xD5, 0xA5,
  434. 0x65, 0x92, 0xF8, 0x27, 0x4E, 0xEC, 0x53, 0xF0
  435. }
  436. };
  437. static const unsigned char long_kat_hash_sha3_224[28] =
  438. {
  439. 0xD6, 0x93, 0x35, 0xB9, 0x33, 0x25, 0x19, 0x2E,
  440. 0x51, 0x6A, 0x91, 0x2E, 0x6D, 0x19, 0xA1, 0x5C,
  441. 0xB5, 0x1C, 0x6E, 0xD5, 0xC1, 0x52, 0x43, 0xE7,
  442. 0xA7, 0xFD, 0x65, 0x3C
  443. };
  444. static const unsigned char long_kat_hash_sha3_256[32] =
  445. {
  446. 0x5C, 0x88, 0x75, 0xAE, 0x47, 0x4A, 0x36, 0x34,
  447. 0xBA, 0x4F, 0xD5, 0x5E, 0xC8, 0x5B, 0xFF, 0xD6,
  448. 0x61, 0xF3, 0x2A, 0xCA, 0x75, 0xC6, 0xD6, 0x99,
  449. 0xD0, 0xCD, 0xCB, 0x6C, 0x11, 0x58, 0x91, 0xC1
  450. };
  451. static const unsigned char long_kat_hash_sha3_384[48] =
  452. {
  453. 0xEE, 0xE9, 0xE2, 0x4D, 0x78, 0xC1, 0x85, 0x53,
  454. 0x37, 0x98, 0x34, 0x51, 0xDF, 0x97, 0xC8, 0xAD,
  455. 0x9E, 0xED, 0xF2, 0x56, 0xC6, 0x33, 0x4F, 0x8E,
  456. 0x94, 0x8D, 0x25, 0x2D, 0x5E, 0x0E, 0x76, 0x84,
  457. 0x7A, 0xA0, 0x77, 0x4D, 0xDB, 0x90, 0xA8, 0x42,
  458. 0x19, 0x0D, 0x2C, 0x55, 0x8B, 0x4B, 0x83, 0x40
  459. };
  460. static const unsigned char long_kat_hash_sha3_512[64] =
  461. {
  462. 0x3C, 0x3A, 0x87, 0x6D, 0xA1, 0x40, 0x34, 0xAB,
  463. 0x60, 0x62, 0x7C, 0x07, 0x7B, 0xB9, 0x8F, 0x7E,
  464. 0x12, 0x0A, 0x2A, 0x53, 0x70, 0x21, 0x2D, 0xFF,
  465. 0xB3, 0x38, 0x5A, 0x18, 0xD4, 0xF3, 0x88, 0x59,
  466. 0xED, 0x31, 0x1D, 0x0A, 0x9D, 0x51, 0x41, 0xCE,
  467. 0x9C, 0xC5, 0xC6, 0x6E, 0xE6, 0x89, 0xB2, 0x66,
  468. 0xA8, 0xAA, 0x18, 0xAC, 0xE8, 0x28, 0x2A, 0x0E,
  469. 0x0D, 0xB5, 0x96, 0xC9, 0x0B, 0x0A, 0x7B, 0x87
  470. };
  471. static int mbedtls_sha3_kat_test(int verbose,
  472. const char *type_name,
  473. mbedtls_sha3_id id,
  474. int test_num)
  475. {
  476. uint8_t hash[64];
  477. int result;
  478. result = mbedtls_sha3(id,
  479. test_data[test_num], test_data_len[test_num],
  480. hash, sizeof(hash));
  481. if (result != 0) {
  482. if (verbose != 0) {
  483. mbedtls_printf(" %s test %d error code: %d\n",
  484. type_name, test_num, result);
  485. }
  486. return result;
  487. }
  488. switch (id) {
  489. case MBEDTLS_SHA3_224:
  490. result = memcmp(hash, test_hash_sha3_224[test_num], 28);
  491. break;
  492. case MBEDTLS_SHA3_256:
  493. result = memcmp(hash, test_hash_sha3_256[test_num], 32);
  494. break;
  495. case MBEDTLS_SHA3_384:
  496. result = memcmp(hash, test_hash_sha3_384[test_num], 48);
  497. break;
  498. case MBEDTLS_SHA3_512:
  499. result = memcmp(hash, test_hash_sha3_512[test_num], 64);
  500. break;
  501. default:
  502. break;
  503. }
  504. if (0 != result) {
  505. if (verbose != 0) {
  506. mbedtls_printf(" %s test %d failed\n", type_name, test_num);
  507. }
  508. return -1;
  509. }
  510. if (verbose != 0) {
  511. mbedtls_printf(" %s test %d passed\n", type_name, test_num);
  512. }
  513. return 0;
  514. }
  515. static int mbedtls_sha3_long_kat_test(int verbose,
  516. const char *type_name,
  517. mbedtls_sha3_id id)
  518. {
  519. mbedtls_sha3_context ctx;
  520. unsigned char buffer[1000];
  521. unsigned char hash[64];
  522. int result = 0;
  523. memset(buffer, 'a', 1000);
  524. if (verbose != 0) {
  525. mbedtls_printf(" %s long KAT test ", type_name);
  526. }
  527. mbedtls_sha3_init(&ctx);
  528. result = mbedtls_sha3_starts(&ctx, id);
  529. if (result != 0) {
  530. if (verbose != 0) {
  531. mbedtls_printf("setup failed\n ");
  532. }
  533. }
  534. /* Process 1,000,000 (one million) 'a' characters */
  535. for (int i = 0; i < 1000; i++) {
  536. result = mbedtls_sha3_update(&ctx, buffer, 1000);
  537. if (result != 0) {
  538. if (verbose != 0) {
  539. mbedtls_printf("update error code: %i\n", result);
  540. }
  541. goto cleanup;
  542. }
  543. }
  544. result = mbedtls_sha3_finish(&ctx, hash, sizeof(hash));
  545. if (result != 0) {
  546. if (verbose != 0) {
  547. mbedtls_printf("finish error code: %d\n", result);
  548. }
  549. goto cleanup;
  550. }
  551. switch (id) {
  552. case MBEDTLS_SHA3_224:
  553. result = memcmp(hash, long_kat_hash_sha3_224, 28);
  554. break;
  555. case MBEDTLS_SHA3_256:
  556. result = memcmp(hash, long_kat_hash_sha3_256, 32);
  557. break;
  558. case MBEDTLS_SHA3_384:
  559. result = memcmp(hash, long_kat_hash_sha3_384, 48);
  560. break;
  561. case MBEDTLS_SHA3_512:
  562. result = memcmp(hash, long_kat_hash_sha3_512, 64);
  563. break;
  564. default:
  565. break;
  566. }
  567. if (result != 0) {
  568. if (verbose != 0) {
  569. mbedtls_printf("failed\n");
  570. }
  571. }
  572. if (verbose != 0) {
  573. mbedtls_printf("passed\n");
  574. }
  575. cleanup:
  576. mbedtls_sha3_free(&ctx);
  577. return result;
  578. }
  579. int mbedtls_sha3_self_test(int verbose)
  580. {
  581. int i;
  582. /* SHA-3 Known Answer Tests (KAT) */
  583. for (i = 0; i < 2; i++) {
  584. if (0 != mbedtls_sha3_kat_test(verbose,
  585. "SHA3-224", MBEDTLS_SHA3_224, i)) {
  586. return 1;
  587. }
  588. if (0 != mbedtls_sha3_kat_test(verbose,
  589. "SHA3-256", MBEDTLS_SHA3_256, i)) {
  590. return 1;
  591. }
  592. if (0 != mbedtls_sha3_kat_test(verbose,
  593. "SHA3-384", MBEDTLS_SHA3_384, i)) {
  594. return 1;
  595. }
  596. if (0 != mbedtls_sha3_kat_test(verbose,
  597. "SHA3-512", MBEDTLS_SHA3_512, i)) {
  598. return 1;
  599. }
  600. }
  601. /* SHA-3 long KAT tests */
  602. if (0 != mbedtls_sha3_long_kat_test(verbose,
  603. "SHA3-224", MBEDTLS_SHA3_224)) {
  604. return 1;
  605. }
  606. if (0 != mbedtls_sha3_long_kat_test(verbose,
  607. "SHA3-256", MBEDTLS_SHA3_256)) {
  608. return 1;
  609. }
  610. if (0 != mbedtls_sha3_long_kat_test(verbose,
  611. "SHA3-384", MBEDTLS_SHA3_384)) {
  612. return 1;
  613. }
  614. if (0 != mbedtls_sha3_long_kat_test(verbose,
  615. "SHA3-512", MBEDTLS_SHA3_512)) {
  616. return 1;
  617. }
  618. if (verbose != 0) {
  619. mbedtls_printf("\n");
  620. }
  621. return 0;
  622. }
  623. #endif /* MBEDTLS_SELF_TEST */
  624. #endif /* MBEDTLS_SHA3_C */