sm3_generic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SM3 secure hash, as specified by OSCCA GM/T 0004-2012 SM3 and
  4. * described at https://tools.ietf.org/html/draft-shen-sm3-hash-01
  5. *
  6. * Copyright (C) 2017 ARM Limited or its affiliates.
  7. * Written by Gilad Ben-Yossef <gilad@benyossef.com>
  8. */
  9. #include <crypto/internal/hash.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/mm.h>
  13. #include <linux/types.h>
  14. #include <crypto/sm3.h>
  15. #include <crypto/sm3_base.h>
  16. #include <linux/bitops.h>
  17. #include <asm/byteorder.h>
  18. #include <asm/unaligned.h>
  19. const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
  20. 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
  21. 0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
  22. 0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
  23. 0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B
  24. };
  25. EXPORT_SYMBOL_GPL(sm3_zero_message_hash);
  26. static inline u32 p0(u32 x)
  27. {
  28. return x ^ rol32(x, 9) ^ rol32(x, 17);
  29. }
  30. static inline u32 p1(u32 x)
  31. {
  32. return x ^ rol32(x, 15) ^ rol32(x, 23);
  33. }
  34. static inline u32 ff(unsigned int n, u32 a, u32 b, u32 c)
  35. {
  36. return (n < 16) ? (a ^ b ^ c) : ((a & b) | (a & c) | (b & c));
  37. }
  38. static inline u32 gg(unsigned int n, u32 e, u32 f, u32 g)
  39. {
  40. return (n < 16) ? (e ^ f ^ g) : ((e & f) | ((~e) & g));
  41. }
  42. static inline u32 t(unsigned int n)
  43. {
  44. return (n < 16) ? SM3_T1 : SM3_T2;
  45. }
  46. static void sm3_expand(u32 *t, u32 *w, u32 *wt)
  47. {
  48. int i;
  49. unsigned int tmp;
  50. /* load the input */
  51. for (i = 0; i <= 15; i++)
  52. w[i] = get_unaligned_be32((__u32 *)t + i);
  53. for (i = 16; i <= 67; i++) {
  54. tmp = w[i - 16] ^ w[i - 9] ^ rol32(w[i - 3], 15);
  55. w[i] = p1(tmp) ^ (rol32(w[i - 13], 7)) ^ w[i - 6];
  56. }
  57. for (i = 0; i <= 63; i++)
  58. wt[i] = w[i] ^ w[i + 4];
  59. }
  60. static void sm3_compress(u32 *w, u32 *wt, u32 *m)
  61. {
  62. u32 ss1;
  63. u32 ss2;
  64. u32 tt1;
  65. u32 tt2;
  66. u32 a, b, c, d, e, f, g, h;
  67. int i;
  68. a = m[0];
  69. b = m[1];
  70. c = m[2];
  71. d = m[3];
  72. e = m[4];
  73. f = m[5];
  74. g = m[6];
  75. h = m[7];
  76. for (i = 0; i <= 63; i++) {
  77. ss1 = rol32((rol32(a, 12) + e + rol32(t(i), i & 31)), 7);
  78. ss2 = ss1 ^ rol32(a, 12);
  79. tt1 = ff(i, a, b, c) + d + ss2 + *wt;
  80. wt++;
  81. tt2 = gg(i, e, f, g) + h + ss1 + *w;
  82. w++;
  83. d = c;
  84. c = rol32(b, 9);
  85. b = a;
  86. a = tt1;
  87. h = g;
  88. g = rol32(f, 19);
  89. f = e;
  90. e = p0(tt2);
  91. }
  92. m[0] = a ^ m[0];
  93. m[1] = b ^ m[1];
  94. m[2] = c ^ m[2];
  95. m[3] = d ^ m[3];
  96. m[4] = e ^ m[4];
  97. m[5] = f ^ m[5];
  98. m[6] = g ^ m[6];
  99. m[7] = h ^ m[7];
  100. a = b = c = d = e = f = g = h = ss1 = ss2 = tt1 = tt2 = 0;
  101. }
  102. static void sm3_transform(struct sm3_state *sst, u8 const *src)
  103. {
  104. unsigned int w[68];
  105. unsigned int wt[64];
  106. sm3_expand((u32 *)src, w, wt);
  107. sm3_compress(w, wt, sst->state);
  108. memzero_explicit(w, sizeof(w));
  109. memzero_explicit(wt, sizeof(wt));
  110. }
  111. static void sm3_generic_block_fn(struct sm3_state *sst, u8 const *src,
  112. int blocks)
  113. {
  114. while (blocks--) {
  115. sm3_transform(sst, src);
  116. src += SM3_BLOCK_SIZE;
  117. }
  118. }
  119. int crypto_sm3_update(struct shash_desc *desc, const u8 *data,
  120. unsigned int len)
  121. {
  122. return sm3_base_do_update(desc, data, len, sm3_generic_block_fn);
  123. }
  124. EXPORT_SYMBOL(crypto_sm3_update);
  125. int crypto_sm3_final(struct shash_desc *desc, u8 *out)
  126. {
  127. sm3_base_do_finalize(desc, sm3_generic_block_fn);
  128. return sm3_base_finish(desc, out);
  129. }
  130. EXPORT_SYMBOL(crypto_sm3_final);
  131. int crypto_sm3_finup(struct shash_desc *desc, const u8 *data,
  132. unsigned int len, u8 *hash)
  133. {
  134. sm3_base_do_update(desc, data, len, sm3_generic_block_fn);
  135. return crypto_sm3_final(desc, hash);
  136. }
  137. EXPORT_SYMBOL(crypto_sm3_finup);
  138. static struct shash_alg sm3_alg = {
  139. .digestsize = SM3_DIGEST_SIZE,
  140. .init = sm3_base_init,
  141. .update = crypto_sm3_update,
  142. .final = crypto_sm3_final,
  143. .finup = crypto_sm3_finup,
  144. .descsize = sizeof(struct sm3_state),
  145. .base = {
  146. .cra_name = "sm3",
  147. .cra_driver_name = "sm3-generic",
  148. .cra_blocksize = SM3_BLOCK_SIZE,
  149. .cra_module = THIS_MODULE,
  150. }
  151. };
  152. static int __init sm3_generic_mod_init(void)
  153. {
  154. return crypto_register_shash(&sm3_alg);
  155. }
  156. static void __exit sm3_generic_mod_fini(void)
  157. {
  158. crypto_unregister_shash(&sm3_alg);
  159. }
  160. subsys_initcall(sm3_generic_mod_init);
  161. module_exit(sm3_generic_mod_fini);
  162. MODULE_LICENSE("GPL v2");
  163. MODULE_DESCRIPTION("SM3 Secure Hash Algorithm");
  164. MODULE_ALIAS_CRYPTO("sm3");
  165. MODULE_ALIAS_CRYPTO("sm3-generic");