sm3.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* sm3.c - SM3 hash function
  2. * Copyright (C) 2017 Jia Zhang
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /* Test vectors:
  20. "abc"
  21. SM3: 66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0
  22. "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"
  23. SM3: debe9ff9 2275b8a1 38604889 c18e5a4d 6fdb70e5 387e5765 293dcba3 9c0c5732
  24. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  25. SM3: 639b6cc5 e64d9e37 a390b192 df4fa1ea 0720ab74 7ff692b9 f38c4e66 ad7b8c05
  26. "a" one million times
  27. SM3: c8aaf894 29554029 e231941a 2acc0ad6 1ff2a5ac d8fadd25 847a3a73 2b3b02c3
  28. */
  29. #include <config.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "g10lib.h"
  34. #include "bithelp.h"
  35. #include "bufhelp.h"
  36. #include "cipher.h"
  37. #include "hash-common.h"
  38. /* USE_AVX_BMI2 indicates whether to compile with Intel AVX/BMI2 code. */
  39. #undef USE_AVX_BMI2
  40. #if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_AVX) && \
  41. defined(HAVE_GCC_INLINE_ASM_BMI2) && \
  42. (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \
  43. defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS))
  44. # define USE_AVX_BMI2 1
  45. #endif
  46. /* USE_AARCH64_SIMD indicates whether to enable ARMv8 SIMD assembly
  47. * code. */
  48. #undef USE_AARCH64_SIMD
  49. #ifdef ENABLE_NEON_SUPPORT
  50. # if defined(__AARCH64EL__) \
  51. && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \
  52. && defined(HAVE_GCC_INLINE_ASM_AARCH64_NEON)
  53. # define USE_AARCH64_SIMD 1
  54. # endif
  55. #endif
  56. /* USE_ARM_CE indicates whether to enable ARMv8 Crypto Extension code. */
  57. #undef USE_ARM_CE
  58. #ifdef ENABLE_ARM_CRYPTO_SUPPORT
  59. # if defined(__AARCH64EL__) && \
  60. defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) && \
  61. defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO)
  62. # define USE_ARM_CE 1
  63. # endif
  64. #endif
  65. typedef struct {
  66. gcry_md_block_ctx_t bctx;
  67. u32 h[8];
  68. } SM3_CONTEXT;
  69. /* AMD64 assembly implementations use SystemV ABI, ABI conversion and additional
  70. * stack to store XMM6-XMM15 needed on Win64. */
  71. #undef ASM_FUNC_ABI
  72. #undef ASM_EXTRA_STACK
  73. #if defined(USE_AVX_BMI2)
  74. # ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
  75. # define ASM_FUNC_ABI __attribute__((sysv_abi))
  76. # define ASM_EXTRA_STACK (10 * 16 + 4 * sizeof(void *))
  77. # else
  78. # define ASM_FUNC_ABI
  79. # define ASM_EXTRA_STACK 0
  80. # endif
  81. #endif
  82. #ifdef USE_AVX_BMI2
  83. unsigned int _gcry_sm3_transform_amd64_avx_bmi2(void *state,
  84. const void *input_data,
  85. size_t num_blks) ASM_FUNC_ABI;
  86. static unsigned int
  87. do_sm3_transform_amd64_avx_bmi2(void *context, const unsigned char *data,
  88. size_t nblks)
  89. {
  90. SM3_CONTEXT *hd = context;
  91. unsigned int nburn = _gcry_sm3_transform_amd64_avx_bmi2 (hd->h, data, nblks);
  92. nburn += nburn ? ASM_EXTRA_STACK : 0;
  93. return nburn;
  94. }
  95. #endif /* USE_AVX_BMI2 */
  96. #ifdef USE_AARCH64_SIMD
  97. unsigned int _gcry_sm3_transform_aarch64(void *state, const void *input_data,
  98. size_t num_blks);
  99. static unsigned int
  100. do_sm3_transform_aarch64(void *context, const unsigned char *data, size_t nblks)
  101. {
  102. SM3_CONTEXT *hd = context;
  103. return _gcry_sm3_transform_aarch64 (hd->h, data, nblks);
  104. }
  105. #endif /* USE_AARCH64_SIMD */
  106. #ifdef USE_ARM_CE
  107. void _gcry_sm3_transform_armv8_ce(void *state, const void *input_data,
  108. size_t num_blks);
  109. static unsigned int
  110. do_sm3_transform_armv8_ce(void *context, const unsigned char *data,
  111. size_t nblks)
  112. {
  113. SM3_CONTEXT *hd = context;
  114. _gcry_sm3_transform_armv8_ce (hd->h, data, nblks);
  115. return 0;
  116. }
  117. #endif /* USE_ARM_CE */
  118. static unsigned int
  119. transform (void *c, const unsigned char *data, size_t nblks);
  120. static void
  121. sm3_init (void *context, unsigned int flags)
  122. {
  123. SM3_CONTEXT *hd = context;
  124. unsigned int features = _gcry_get_hw_features ();
  125. (void)flags;
  126. hd->h[0] = 0x7380166f;
  127. hd->h[1] = 0x4914b2b9;
  128. hd->h[2] = 0x172442d7;
  129. hd->h[3] = 0xda8a0600;
  130. hd->h[4] = 0xa96f30bc;
  131. hd->h[5] = 0x163138aa;
  132. hd->h[6] = 0xe38dee4d;
  133. hd->h[7] = 0xb0fb0e4e;
  134. hd->bctx.nblocks = 0;
  135. hd->bctx.nblocks_high = 0;
  136. hd->bctx.count = 0;
  137. hd->bctx.blocksize_shift = _gcry_ctz(64);
  138. hd->bctx.bwrite = transform;
  139. #ifdef USE_AVX_BMI2
  140. if ((features & HWF_INTEL_AVX2) && (features & HWF_INTEL_BMI2))
  141. hd->bctx.bwrite = do_sm3_transform_amd64_avx_bmi2;
  142. #endif
  143. #ifdef USE_AARCH64_SIMD
  144. if (features & HWF_ARM_NEON)
  145. hd->bctx.bwrite = do_sm3_transform_aarch64;
  146. #endif
  147. #ifdef USE_ARM_CE
  148. if (features & HWF_ARM_SM3)
  149. hd->bctx.bwrite = do_sm3_transform_armv8_ce;
  150. #endif
  151. (void)features;
  152. }
  153. /*
  154. Transform the message X which consists of 16 32-bit-words. See
  155. GM/T 004-2012 for details. */
  156. #define R(i,a,b,c,d,e,f,g,h,t,w1,w2) do \
  157. { \
  158. ss1 = rol ((rol ((a), 12) + (e) + (t)), 7); \
  159. ss2 = ss1 ^ rol ((a), 12); \
  160. d += FF##i(a,b,c) + ss2 + ((w1) ^ (w2)); \
  161. h += GG##i(e,f,g) + ss1 + (w1); \
  162. b = rol ((b), 9); \
  163. f = rol ((f), 19); \
  164. h = P0 ((h)); \
  165. } while (0)
  166. #define R1(a,b,c,d,e,f,g,h,t,w1,w2) R(1,a,b,c,d,e,f,g,h,t,w1,w2)
  167. #define R2(a,b,c,d,e,f,g,h,t,w1,w2) R(2,a,b,c,d,e,f,g,h,t,w1,w2)
  168. #define FF1(x, y, z) (x ^ y ^ z)
  169. #define FF2(x, y, z) ((x & y) | (x & z) | (y & z))
  170. #define GG1(x, y, z) (x ^ y ^ z)
  171. #define GG2(x, y, z) ((x & y) | ( ~x & z))
  172. /* Message expansion */
  173. #define P0(x) ((x) ^ rol ((x), 9) ^ rol ((x), 17))
  174. #define P1(x) ((x) ^ rol ((x), 15) ^ rol ((x), 23))
  175. #define I(i) ( w[i] = buf_get_be32(data + i * 4) )
  176. #define W1(i) ( w[i&0x0f] )
  177. #define W2(i) ( w[i&0x0f] = P1(w[i &0x0f] \
  178. ^ w[(i-9)&0x0f] \
  179. ^ rol (w[(i-3)&0x0f], 15)) \
  180. ^ rol (w[(i-13)&0x0f], 7) \
  181. ^ w[(i-6)&0x0f] )
  182. static unsigned int
  183. transform_blk (void *ctx, const unsigned char *data)
  184. {
  185. SM3_CONTEXT *hd = ctx;
  186. static const u32 K[64] = {
  187. 0x79cc4519, 0xf3988a32, 0xe7311465, 0xce6228cb,
  188. 0x9cc45197, 0x3988a32f, 0x7311465e, 0xe6228cbc,
  189. 0xcc451979, 0x988a32f3, 0x311465e7, 0x6228cbce,
  190. 0xc451979c, 0x88a32f39, 0x11465e73, 0x228cbce6,
  191. 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c,
  192. 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce,
  193. 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec,
  194. 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5,
  195. 0x7a879d8a, 0xf50f3b14, 0xea1e7629, 0xd43cec53,
  196. 0xa879d8a7, 0x50f3b14f, 0xa1e7629e, 0x43cec53d,
  197. 0x879d8a7a, 0x0f3b14f5, 0x1e7629ea, 0x3cec53d4,
  198. 0x79d8a7a8, 0xf3b14f50, 0xe7629ea1, 0xcec53d43,
  199. 0x9d8a7a87, 0x3b14f50f, 0x7629ea1e, 0xec53d43c,
  200. 0xd8a7a879, 0xb14f50f3, 0x629ea1e7, 0xc53d43ce,
  201. 0x8a7a879d, 0x14f50f3b, 0x29ea1e76, 0x53d43cec,
  202. 0xa7a879d8, 0x4f50f3b1, 0x9ea1e762, 0x3d43cec5
  203. };
  204. u32 a,b,c,d,e,f,g,h,ss1,ss2;
  205. u32 w[16];
  206. a = hd->h[0];
  207. b = hd->h[1];
  208. c = hd->h[2];
  209. d = hd->h[3];
  210. e = hd->h[4];
  211. f = hd->h[5];
  212. g = hd->h[6];
  213. h = hd->h[7];
  214. R1(a, b, c, d, e, f, g, h, K[0], I(0), I(4));
  215. R1(d, a, b, c, h, e, f, g, K[1], I(1), I(5));
  216. R1(c, d, a, b, g, h, e, f, K[2], I(2), I(6));
  217. R1(b, c, d, a, f, g, h, e, K[3], I(3), I(7));
  218. R1(a, b, c, d, e, f, g, h, K[4], W1(4), I(8));
  219. R1(d, a, b, c, h, e, f, g, K[5], W1(5), I(9));
  220. R1(c, d, a, b, g, h, e, f, K[6], W1(6), I(10));
  221. R1(b, c, d, a, f, g, h, e, K[7], W1(7), I(11));
  222. R1(a, b, c, d, e, f, g, h, K[8], W1(8), I(12));
  223. R1(d, a, b, c, h, e, f, g, K[9], W1(9), I(13));
  224. R1(c, d, a, b, g, h, e, f, K[10], W1(10), I(14));
  225. R1(b, c, d, a, f, g, h, e, K[11], W1(11), I(15));
  226. R1(a, b, c, d, e, f, g, h, K[12], W1(12), W2(16));
  227. R1(d, a, b, c, h, e, f, g, K[13], W1(13), W2(17));
  228. R1(c, d, a, b, g, h, e, f, K[14], W1(14), W2(18));
  229. R1(b, c, d, a, f, g, h, e, K[15], W1(15), W2(19));
  230. R2(a, b, c, d, e, f, g, h, K[16], W1(16), W2(20));
  231. R2(d, a, b, c, h, e, f, g, K[17], W1(17), W2(21));
  232. R2(c, d, a, b, g, h, e, f, K[18], W1(18), W2(22));
  233. R2(b, c, d, a, f, g, h, e, K[19], W1(19), W2(23));
  234. R2(a, b, c, d, e, f, g, h, K[20], W1(20), W2(24));
  235. R2(d, a, b, c, h, e, f, g, K[21], W1(21), W2(25));
  236. R2(c, d, a, b, g, h, e, f, K[22], W1(22), W2(26));
  237. R2(b, c, d, a, f, g, h, e, K[23], W1(23), W2(27));
  238. R2(a, b, c, d, e, f, g, h, K[24], W1(24), W2(28));
  239. R2(d, a, b, c, h, e, f, g, K[25], W1(25), W2(29));
  240. R2(c, d, a, b, g, h, e, f, K[26], W1(26), W2(30));
  241. R2(b, c, d, a, f, g, h, e, K[27], W1(27), W2(31));
  242. R2(a, b, c, d, e, f, g, h, K[28], W1(28), W2(32));
  243. R2(d, a, b, c, h, e, f, g, K[29], W1(29), W2(33));
  244. R2(c, d, a, b, g, h, e, f, K[30], W1(30), W2(34));
  245. R2(b, c, d, a, f, g, h, e, K[31], W1(31), W2(35));
  246. R2(a, b, c, d, e, f, g, h, K[32], W1(32), W2(36));
  247. R2(d, a, b, c, h, e, f, g, K[33], W1(33), W2(37));
  248. R2(c, d, a, b, g, h, e, f, K[34], W1(34), W2(38));
  249. R2(b, c, d, a, f, g, h, e, K[35], W1(35), W2(39));
  250. R2(a, b, c, d, e, f, g, h, K[36], W1(36), W2(40));
  251. R2(d, a, b, c, h, e, f, g, K[37], W1(37), W2(41));
  252. R2(c, d, a, b, g, h, e, f, K[38], W1(38), W2(42));
  253. R2(b, c, d, a, f, g, h, e, K[39], W1(39), W2(43));
  254. R2(a, b, c, d, e, f, g, h, K[40], W1(40), W2(44));
  255. R2(d, a, b, c, h, e, f, g, K[41], W1(41), W2(45));
  256. R2(c, d, a, b, g, h, e, f, K[42], W1(42), W2(46));
  257. R2(b, c, d, a, f, g, h, e, K[43], W1(43), W2(47));
  258. R2(a, b, c, d, e, f, g, h, K[44], W1(44), W2(48));
  259. R2(d, a, b, c, h, e, f, g, K[45], W1(45), W2(49));
  260. R2(c, d, a, b, g, h, e, f, K[46], W1(46), W2(50));
  261. R2(b, c, d, a, f, g, h, e, K[47], W1(47), W2(51));
  262. R2(a, b, c, d, e, f, g, h, K[48], W1(48), W2(52));
  263. R2(d, a, b, c, h, e, f, g, K[49], W1(49), W2(53));
  264. R2(c, d, a, b, g, h, e, f, K[50], W1(50), W2(54));
  265. R2(b, c, d, a, f, g, h, e, K[51], W1(51), W2(55));
  266. R2(a, b, c, d, e, f, g, h, K[52], W1(52), W2(56));
  267. R2(d, a, b, c, h, e, f, g, K[53], W1(53), W2(57));
  268. R2(c, d, a, b, g, h, e, f, K[54], W1(54), W2(58));
  269. R2(b, c, d, a, f, g, h, e, K[55], W1(55), W2(59));
  270. R2(a, b, c, d, e, f, g, h, K[56], W1(56), W2(60));
  271. R2(d, a, b, c, h, e, f, g, K[57], W1(57), W2(61));
  272. R2(c, d, a, b, g, h, e, f, K[58], W1(58), W2(62));
  273. R2(b, c, d, a, f, g, h, e, K[59], W1(59), W2(63));
  274. R2(a, b, c, d, e, f, g, h, K[60], W1(60), W2(64));
  275. R2(d, a, b, c, h, e, f, g, K[61], W1(61), W2(65));
  276. R2(c, d, a, b, g, h, e, f, K[62], W1(62), W2(66));
  277. R2(b, c, d, a, f, g, h, e, K[63], W1(63), W2(67));
  278. hd->h[0] ^= a;
  279. hd->h[1] ^= b;
  280. hd->h[2] ^= c;
  281. hd->h[3] ^= d;
  282. hd->h[4] ^= e;
  283. hd->h[5] ^= f;
  284. hd->h[6] ^= g;
  285. hd->h[7] ^= h;
  286. return /*burn_stack*/ 26*4+32;
  287. }
  288. #undef P0
  289. #undef P1
  290. #undef R
  291. #undef R1
  292. #undef R2
  293. static unsigned int
  294. transform (void *ctx, const unsigned char *data, size_t nblks)
  295. {
  296. SM3_CONTEXT *hd = ctx;
  297. unsigned int burn;
  298. do
  299. {
  300. burn = transform_blk (hd, data);
  301. data += 64;
  302. }
  303. while (--nblks);
  304. return burn;
  305. }
  306. /*
  307. The routine finally terminates the computation and returns the
  308. digest. The handle is prepared for a new cycle, but adding bytes
  309. to the handle will the destroy the returned buffer. Returns: 32
  310. bytes with the message the digest. */
  311. static void
  312. sm3_final(void *context)
  313. {
  314. SM3_CONTEXT *hd = context;
  315. u32 t, th, msb, lsb;
  316. byte *p;
  317. unsigned int burn;
  318. t = hd->bctx.nblocks;
  319. if (sizeof t == sizeof hd->bctx.nblocks)
  320. th = hd->bctx.nblocks_high;
  321. else
  322. th = hd->bctx.nblocks >> 32;
  323. /* multiply by 64 to make a byte count */
  324. lsb = t << 6;
  325. msb = (th << 6) | (t >> 26);
  326. /* add the count */
  327. t = lsb;
  328. if ((lsb += hd->bctx.count) < t)
  329. msb++;
  330. /* multiply by 8 to make a bit count */
  331. t = lsb;
  332. lsb <<= 3;
  333. msb <<= 3;
  334. msb |= t >> 29;
  335. if (hd->bctx.count < 56) /* enough room */
  336. {
  337. hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
  338. if (hd->bctx.count < 56)
  339. memset (&hd->bctx.buf[hd->bctx.count], 0, 56 - hd->bctx.count);
  340. /* append the 64 bit count */
  341. buf_put_be32(hd->bctx.buf + 56, msb);
  342. buf_put_be32(hd->bctx.buf + 60, lsb);
  343. burn = (*hd->bctx.bwrite) ( hd, hd->bctx.buf, 1 );
  344. }
  345. else /* need one extra block */
  346. {
  347. hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
  348. /* fill pad and next block with zeroes */
  349. memset (&hd->bctx.buf[hd->bctx.count], 0, 64 - hd->bctx.count + 56);
  350. /* append the 64 bit count */
  351. buf_put_be32(hd->bctx.buf + 64 + 56, msb);
  352. buf_put_be32(hd->bctx.buf + 64 + 60, lsb);
  353. burn = (*hd->bctx.bwrite) ( hd, hd->bctx.buf, 2 );
  354. }
  355. p = hd->bctx.buf;
  356. #define X(a) do { buf_put_be32(p, hd->h[a]); p += 4; } while(0)
  357. X(0);
  358. X(1);
  359. X(2);
  360. X(3);
  361. X(4);
  362. X(5);
  363. X(6);
  364. X(7);
  365. #undef X
  366. hd->bctx.count = 0;
  367. _gcry_burn_stack (burn);
  368. }
  369. static byte *
  370. sm3_read (void *context)
  371. {
  372. SM3_CONTEXT *hd = context;
  373. return hd->bctx.buf;
  374. }
  375. /* Shortcut functions which puts the hash value of the supplied buffer iov
  376. * into outbuf which must have a size of 32 bytes. */
  377. static void
  378. _gcry_sm3_hash_buffers (void *outbuf, size_t nbytes,
  379. const gcry_buffer_t *iov, int iovcnt)
  380. {
  381. SM3_CONTEXT hd;
  382. (void)nbytes;
  383. sm3_init (&hd, 0);
  384. for (;iovcnt > 0; iov++, iovcnt--)
  385. _gcry_md_block_write (&hd,
  386. (const char*)iov[0].data + iov[0].off, iov[0].len);
  387. sm3_final (&hd);
  388. memcpy (outbuf, hd.bctx.buf, 32);
  389. }
  390. /*
  391. Self-test section.
  392. */
  393. static gpg_err_code_t
  394. selftests_sm3 (int extended, selftest_report_func_t report)
  395. {
  396. const char *what;
  397. const char *errtxt;
  398. what = "short string (spec example 1)";
  399. errtxt = _gcry_hash_selftest_check_one
  400. (GCRY_MD_SM3, 0,
  401. "abc", 3,
  402. "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2"
  403. "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0", 32);
  404. if (errtxt)
  405. goto failed;
  406. if (extended)
  407. {
  408. what = "long string (spec example 2)";
  409. errtxt = _gcry_hash_selftest_check_one
  410. (GCRY_MD_SM3, 0,
  411. "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", 64,
  412. "\xde\xbe\x9f\xf9\x22\x75\xb8\xa1\x38\x60\x48\x89\xc1\x8e\x5a\x4d"
  413. "\x6f\xdb\x70\xe5\x38\x7e\x57\x65\x29\x3d\xcb\xa3\x9c\x0c\x57\x32",
  414. 32);
  415. if (errtxt)
  416. goto failed;
  417. what = "long string";
  418. errtxt = _gcry_hash_selftest_check_one
  419. (GCRY_MD_SM3, 0,
  420. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
  421. "\x63\x9b\x6c\xc5\xe6\x4d\x9e\x37\xa3\x90\xb1\x92\xdf\x4f\xa1\xea"
  422. "\x07\x20\xab\x74\x7f\xf6\x92\xb9\xf3\x8c\x4e\x66\xad\x7b\x8c\x05",
  423. 32);
  424. if (errtxt)
  425. goto failed;
  426. what = "one million \"a\"";
  427. errtxt = _gcry_hash_selftest_check_one
  428. (GCRY_MD_SM3, 1,
  429. NULL, 0,
  430. "\xc8\xaa\xf8\x94\x29\x55\x40\x29\xe2\x31\x94\x1a\x2a\xcc\x0a\xd6"
  431. "\x1f\xf2\xa5\xac\xd8\xfa\xdd\x25\x84\x7a\x3a\x73\x2b\x3b\x02\xc3",
  432. 32);
  433. if (errtxt)
  434. goto failed;
  435. }
  436. return 0; /* Succeeded. */
  437. failed:
  438. if (report)
  439. report ("digest", GCRY_MD_SM3, what, errtxt);
  440. return GPG_ERR_SELFTEST_FAILED;
  441. }
  442. /* Run a full self-test for ALGO and return 0 on success. */
  443. static gpg_err_code_t
  444. run_selftests (int algo, int extended, selftest_report_func_t report)
  445. {
  446. gpg_err_code_t ec;
  447. switch (algo)
  448. {
  449. case GCRY_MD_SM3:
  450. ec = selftests_sm3 (extended, report);
  451. break;
  452. default:
  453. ec = GPG_ERR_DIGEST_ALGO;
  454. break;
  455. }
  456. return ec;
  457. }
  458. static const byte asn_sm3[] = /* Object ID is 1.2.156.10197.401 */
  459. { 0x30, 0x2F, 0x30, 0x0B, 0x06, 0x07, 0x2A, 0x81,
  460. 0x1C, 0xCF, 0x55, 0x83, 0x11, 0x05, 0x00, 0x04,
  461. 0x20 };
  462. static const gcry_md_oid_spec_t oid_spec_sm3[] =
  463. {
  464. /* China Electronics Standardization Instutute,
  465. OID White paper (2015), Table 6 */
  466. { "1.2.156.10197.401" },
  467. { NULL },
  468. };
  469. const gcry_md_spec_t _gcry_digest_spec_sm3 =
  470. {
  471. GCRY_MD_SM3, {0, 0},
  472. "SM3", asn_sm3, DIM (asn_sm3), oid_spec_sm3, 32,
  473. sm3_init, _gcry_md_block_write, sm3_final, sm3_read, NULL,
  474. _gcry_sm3_hash_buffers,
  475. sizeof (SM3_CONTEXT),
  476. run_selftests
  477. };