You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
16 KiB

1 year ago
  1. /*
  2. * Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the GmSSL Project.
  19. * (http://gmssl.org/)"
  20. *
  21. * 4. The name "GmSSL Project" must not be used to endorse or promote
  22. * products derived from this software without prior written
  23. * permission. For written permission, please contact
  24. * guanzhi1980@gmail.com.
  25. *
  26. * 5. Products derived from this software may not be called "GmSSL"
  27. * nor may "GmSSL" appear in their names without prior written
  28. * permission of the GmSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the GmSSL Project
  33. * (http://gmssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. */
  48. function sm4_memcpy(dst, dst_offset, src, src_offset, len) {
  49. while (len--) {
  50. dst[dst_offset++] = src[src_offset++];
  51. }
  52. }
  53. function SM4_GETU32(data, offset) {
  54. return ((data[offset] << 24)
  55. | (data[offset + 1] << 16)
  56. | (data[offset + 2] << 8)
  57. | data[offset + 3]) >>> 0;
  58. }
  59. function SM4_PUTU32(data, offset, value) {
  60. data[offset + 3] = (value & 0xff) >>> 0;
  61. value >>>= 8;
  62. data[offset + 2] = (value & 0xff) >>> 0;
  63. value >>>= 8;
  64. data[offset + 1] = (value & 0xff) >>> 0;
  65. value >>>= 8;
  66. data[offset] = (value & 0xff) >>> 0;
  67. }
  68. const SM4_KEY_LENGTH = 16
  69. const SM4_BLOCK_SIZE = 16
  70. const SM4_IV_LENGTH = SM4_BLOCK_SIZE
  71. const SM4_NUM_ROUNDS = 32
  72. const SM4_S = [
  73. 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7,
  74. 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
  75. 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3,
  76. 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
  77. 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a,
  78. 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,
  79. 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95,
  80. 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,
  81. 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba,
  82. 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,
  83. 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b,
  84. 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,
  85. 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2,
  86. 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,
  87. 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52,
  88. 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,
  89. 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5,
  90. 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,
  91. 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55,
  92. 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
  93. 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60,
  94. 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
  95. 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f,
  96. 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
  97. 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f,
  98. 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
  99. 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd,
  100. 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
  101. 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e,
  102. 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
  103. 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20,
  104. 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48,
  105. ];
  106. const SM4_FK = [
  107. 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc,
  108. ];
  109. const SM4_CK = [
  110. 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
  111. 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
  112. 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
  113. 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
  114. 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
  115. 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
  116. 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
  117. 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279,
  118. ];
  119. function SM4_ROL32(x, n) {
  120. return ((x << n) | (x >>> (32 - n))) >>> 0;
  121. }
  122. function SM4_S32(A) {
  123. return (
  124. (SM4_S[A >>> 24] << 24) ^
  125. (SM4_S[(A >>> 16) & 0xff] << 16) ^
  126. (SM4_S[(A >>> 8) & 0xff] << 8) ^
  127. (SM4_S[A & 0xff])) >>> 0;
  128. }
  129. function SM4_L32(x) {
  130. return (
  131. x ^
  132. SM4_ROL32(x, 2) ^
  133. SM4_ROL32(x, 10) ^
  134. SM4_ROL32(x, 18) ^
  135. SM4_ROL32(x, 24)) >>> 0;
  136. }
  137. function SM4_L32_(x) {
  138. return (
  139. x ^
  140. SM4_ROL32(x, 13) ^
  141. SM4_ROL32(x, 23)) >>> 0;
  142. }
  143. function sm4_key_new() {
  144. var key = {
  145. rk: new Array(SM4_NUM_ROUNDS),
  146. };
  147. return key;
  148. }
  149. function sm4_key_free(key) {
  150. for (var i = 0; i < SM4_NUM_ROUNDS; i++) {
  151. key.rk[i] = 0;
  152. }
  153. key = null;
  154. }
  155. function sm4_set_encrypt_key(key, user_key) {
  156. var x0, x1, x2, x3, x4;
  157. x0 = SM4_GETU32(user_key, 0) ^ SM4_FK[0];
  158. x1 = SM4_GETU32(user_key, 4) ^ SM4_FK[1];
  159. x2 = SM4_GETU32(user_key, 8) ^ SM4_FK[2];
  160. x3 = SM4_GETU32(user_key, 12) ^ SM4_FK[3];
  161. key.rk[0] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[0]))) >>> 0;
  162. key.rk[1] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[1]))) >>> 0;
  163. key.rk[2] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[2]))) >>> 0;
  164. key.rk[3] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[3]))) >>> 0;
  165. key.rk[4] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[4]))) >>> 0;
  166. key.rk[5] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[5]))) >>> 0;
  167. key.rk[6] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[6]))) >>> 0;
  168. key.rk[7] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[7]))) >>> 0;
  169. key.rk[8] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[8]))) >>> 0;
  170. key.rk[9] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[9]))) >>> 0;
  171. key.rk[10] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[10]))) >>> 0;
  172. key.rk[11] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[11]))) >>> 0;
  173. key.rk[12] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[12]))) >>> 0;
  174. key.rk[13] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[13]))) >>> 0;
  175. key.rk[14] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[14]))) >>> 0;
  176. key.rk[15] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[15]))) >>> 0;
  177. key.rk[16] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[16]))) >>> 0;
  178. key.rk[17] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[17]))) >>> 0;
  179. key.rk[18] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[18]))) >>> 0;
  180. key.rk[19] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[19]))) >>> 0;
  181. key.rk[20] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[20]))) >>> 0;
  182. key.rk[21] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[21]))) >>> 0;
  183. key.rk[22] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[22]))) >>> 0;
  184. key.rk[23] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[23]))) >>> 0;
  185. key.rk[24] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[24]))) >>> 0;
  186. key.rk[25] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[25]))) >>> 0;
  187. key.rk[26] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[26]))) >>> 0;
  188. key.rk[27] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[27]))) >>> 0;
  189. key.rk[28] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[28]))) >>> 0;
  190. key.rk[29] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[29]))) >>> 0;
  191. key.rk[30] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[30]))) >>> 0;
  192. key.rk[31] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[31]))) >>> 0;
  193. x0 = x1 = x3 = x3 = x4 = 0;
  194. }
  195. function sm4_set_decrypt_key(key, user_key) {
  196. var x0, x1, x2, x3, x4;
  197. x0 = SM4_GETU32(user_key, 0) ^ SM4_FK[0];
  198. x1 = SM4_GETU32(user_key, 4) ^ SM4_FK[1];
  199. x2 = SM4_GETU32(user_key, 8) ^ SM4_FK[2];
  200. x3 = SM4_GETU32(user_key, 12) ^ SM4_FK[3];
  201. key.rk[31] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[0]))) >>> 0;
  202. key.rk[30] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[1]))) >>> 0;
  203. key.rk[29] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[2]))) >>> 0;
  204. key.rk[28] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[3]))) >>> 0;
  205. key.rk[27] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[4]))) >>> 0;
  206. key.rk[26] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[5]))) >>> 0;
  207. key.rk[25] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[6]))) >>> 0;
  208. key.rk[24] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[7]))) >>> 0;
  209. key.rk[23] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[8]))) >>> 0;
  210. key.rk[22] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[9]))) >>> 0;
  211. key.rk[21] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[10]))) >>> 0;
  212. key.rk[20] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[11]))) >>> 0;
  213. key.rk[19] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[12]))) >>> 0;
  214. key.rk[18] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[13]))) >>> 0;
  215. key.rk[17] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[14]))) >>> 0;
  216. key.rk[16] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[15]))) >>> 0;
  217. key.rk[15] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[16]))) >>> 0;
  218. key.rk[14] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[17]))) >>> 0;
  219. key.rk[13] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[18]))) >>> 0;
  220. key.rk[12] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[19]))) >>> 0;
  221. key.rk[11] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[20]))) >>> 0;
  222. key.rk[10] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[21]))) >>> 0;
  223. key.rk[9] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[22]))) >>> 0;
  224. key.rk[8] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[23]))) >>> 0;
  225. key.rk[7] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[24]))) >>> 0;
  226. key.rk[6] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[25]))) >>> 0;
  227. key.rk[5] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[26]))) >>> 0;
  228. key.rk[4] = x1 = (x2 ^ SM4_L32_(SM4_S32(x3 ^ x4 ^ x0 ^ SM4_CK[27]))) >>> 0;
  229. key.rk[3] = x2 = (x3 ^ SM4_L32_(SM4_S32(x4 ^ x0 ^ x1 ^ SM4_CK[28]))) >>> 0;
  230. key.rk[2] = x3 = (x4 ^ SM4_L32_(SM4_S32(x0 ^ x1 ^ x2 ^ SM4_CK[29]))) >>> 0;
  231. key.rk[1] = x4 = (x0 ^ SM4_L32_(SM4_S32(x1 ^ x2 ^ x3 ^ SM4_CK[30]))) >>> 0;
  232. key.rk[0] = x0 = (x1 ^ SM4_L32_(SM4_S32(x2 ^ x3 ^ x4 ^ SM4_CK[31]))) >>> 0;
  233. x0 = x1 = x3 = x3 = x4 = 0;
  234. }
  235. function sm4_encrypt(inbuf, in_offset, outbuf, out_offset, key) {
  236. var x0, x1, x2, x3, x4;
  237. x0 = SM4_GETU32(inbuf, in_offset);
  238. x1 = SM4_GETU32(inbuf, in_offset + 4);
  239. x2 = SM4_GETU32(inbuf, in_offset + 8);
  240. x3 = SM4_GETU32(inbuf, in_offset + 12);
  241. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[0]));
  242. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[1]));
  243. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[2]));
  244. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[3]));
  245. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[4]));
  246. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[5]));
  247. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[6]));
  248. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[7]));
  249. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[8]));
  250. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[9]));
  251. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[10]));
  252. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[11]));
  253. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[12]));
  254. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[13]));
  255. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[14]));
  256. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[15]));
  257. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[16]));
  258. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[17]));
  259. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[18]));
  260. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[19]));
  261. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[20]));
  262. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[21]));
  263. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[22]));
  264. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[23]));
  265. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[24]));
  266. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[25]));
  267. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[26]));
  268. x1 = x2 ^ SM4_L32(SM4_S32(x3 ^ x4 ^ x0 ^ key.rk[27]));
  269. x2 = x3 ^ SM4_L32(SM4_S32(x4 ^ x0 ^ x1 ^ key.rk[28]));
  270. x3 = x4 ^ SM4_L32(SM4_S32(x0 ^ x1 ^ x2 ^ key.rk[29]));
  271. x4 = x0 ^ SM4_L32(SM4_S32(x1 ^ x2 ^ x3 ^ key.rk[30]));
  272. x0 = x1 ^ SM4_L32(SM4_S32(x2 ^ x3 ^ x4 ^ key.rk[31]));
  273. SM4_PUTU32(outbuf, out_offset, x0);
  274. SM4_PUTU32(outbuf, out_offset + 4, x4);
  275. SM4_PUTU32(outbuf, out_offset + 8, x3);
  276. SM4_PUTU32(outbuf, out_offset + 12, x2);
  277. }
  278. function sm4_decrypt(inbuf, in_offset, outbuf, out_offset, key) {
  279. return sm4_encrypt(inbuf, in_offset, outbuf, out_offset, key);
  280. }
  281. function sm4_test() {
  282. const user_key = [
  283. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  284. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  285. ];
  286. const rk = [
  287. 0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077,
  288. 0x367360f4, 0x776a0c61, 0xb6bb89b3, 0x24763151,
  289. 0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57,
  290. 0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1,
  291. 0xd120b428, 0x73b55fa3, 0xcc874966, 0x92244439,
  292. 0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e,
  293. 0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81,
  294. 0x428d3654, 0x62293496, 0x01cf72e5, 0x9124a012,
  295. ];
  296. const plaintext = [
  297. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  298. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  299. ];
  300. const ciphertext = [
  301. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  302. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  303. ];
  304. const ciphertext2 = [
  305. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  306. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  307. ];
  308. let key = sm4_key_new();
  309. let buf = new Array(SM4_BLOCK_SIZE);
  310. sm4_set_encrypt_key(key, user_key);
  311. for (let i = 0; i < SM4_NUM_ROUNDS; i++) {
  312. if (key.rk[i] !== rk[i]) {
  313. console.log("sm4_set_encrypt_key failed");
  314. return 0;
  315. }
  316. }
  317. sm4_encrypt(plaintext, 0, buf, 0, key);
  318. console.log("sm4 test1");
  319. for (let i = 0; i < SM4_BLOCK_SIZE; i++) {
  320. if (buf[i] !== ciphertext[i]) {
  321. console.log("sm4_encrypt failed");
  322. return 0;
  323. }
  324. }
  325. sm4_memcpy(buf, 0, plaintext, 0, SM4_BLOCK_SIZE);
  326. console.log("sm4 test2");
  327. for (let i = 0; i < 1000000; i++) {
  328. sm4_encrypt(buf, 0, buf, 0, key);
  329. }
  330. for (let i = 0; i < SM4_BLOCK_SIZE; i++) {
  331. if (buf[i] !== ciphertext2[i]) {
  332. console.log("sm4_encrypt 1000000 failed");
  333. return 0;
  334. }
  335. }
  336. sm4_set_decrypt_key(key, user_key);
  337. sm4_encrypt(ciphertext, 0, buf, 0, key);
  338. for (let i = 0; i < SM4_BLOCK_SIZE; i++) {
  339. if (buf[i] !== plaintext[i]) {
  340. console.log("sm4_decrypt failed");
  341. return 0;
  342. }
  343. }
  344. sm4_key_free(key);
  345. return 1;
  346. }
  347. export default class SM4 {
  348. /**
  349. * sm4加密
  350. * @param input 输入字节数组
  351. * @param length 输入长度(整形)
  352. * @param user_key 密钥字节数组
  353. */
  354. static sm4_enc(input, length, user_key) {
  355. let padding = SM4_BLOCK_SIZE - length % SM4_BLOCK_SIZE;
  356. let block = parseInt(length / SM4_BLOCK_SIZE);
  357. let end_len = SM4_BLOCK_SIZE - padding;
  358. let end = new Uint8Array(SM4_BLOCK_SIZE);
  359. //input末尾不足16个字节时补0拷贝到end中
  360. sm4_memcpy(end, 0, input, block * SM4_BLOCK_SIZE, end_len);
  361. let key = sm4_key_new();
  362. sm4_set_encrypt_key(key, user_key);
  363. let output = new Uint8Array(length + padding);
  364. for (let i = 0; i < block; i++) {
  365. sm4_encrypt(input, i * SM4_BLOCK_SIZE, output, i * SM4_BLOCK_SIZE, key);
  366. }
  367. sm4_encrypt(end, 0, output, block * SM4_BLOCK_SIZE, key);
  368. sm4_key_free(key);
  369. return output;
  370. }
  371. /**
  372. *
  373. * @param input 输入字节数组
  374. * @param length 输入长度整形)
  375. * @param user_key 密钥字节数组
  376. */
  377. static sm4_dec(input, length, user_key) {
  378. let key = sm4_key_new();
  379. sm4_set_decrypt_key(key, user_key);
  380. let output = new Uint8Array(length);
  381. for (let i = 0; i < length / SM4_BLOCK_SIZE; i++) {
  382. sm4_decrypt(input, i * SM4_BLOCK_SIZE, output, i * SM4_BLOCK_SIZE, key);
  383. }
  384. //TODO 去补位,固件以8000...结尾
  385. sm4_key_free(key);
  386. return output;
  387. }
  388. }