sm3_generic.c 4.6 KB

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