平安校园
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.
 
 
 
 
 
 

426 lines
12 KiB

  1. //
  2. namespace SafeCampus.Core.Utils;
  3. /// <summary>
  4. /// SM4工具类
  5. /// </summary>
  6. public class SM4CryptoUtil
  7. {
  8. /// <summary>
  9. /// 加密 非线性τ函数B=τ(A)
  10. /// </summary>
  11. /// <param name="b"></param>
  12. /// <param name="i"></param>
  13. /// <returns></returns>
  14. private static long GetULongByBe(byte[] b, int i)
  15. {
  16. var n = (long)(b[i] & 0xff) << 24 | (uint)((b[i + 1] & 0xff) << 16) | (uint)((b[i + 2] & 0xff) << 8) | b[i + 3] & 0xff & 0xffffffffL;
  17. return n;
  18. }
  19. /// <summary>
  20. /// 解密 非线性τ函数B=τ(A)
  21. /// </summary>
  22. /// <param name="n"></param>
  23. /// <param name="b"></param>
  24. /// <param name="i"></param>
  25. private static void PutULongToBe(long n, byte[] b, int i)
  26. {
  27. b[i] = (byte)(int)(0xFF & n >> 24);
  28. b[i + 1] = (byte)(int)(0xFF & n >> 16);
  29. b[i + 2] = (byte)(int)(0xFF & n >> 8);
  30. b[i + 3] = (byte)(int)(0xFF & n);
  31. }
  32. /// <summary>
  33. /// 循环移位,为32位的x循环左移n位
  34. /// </summary>
  35. /// <param name="x"></param>
  36. /// <param name="n"></param>
  37. /// <returns></returns>
  38. private static long Rotl(long x, int n)
  39. {
  40. return (x & 0xFFFFFFFF) << n | x >> 32 - n;
  41. }
  42. /// <summary>
  43. /// 将密钥逆序
  44. /// </summary>
  45. /// <param name="sk"></param>
  46. /// <param name="i"></param>
  47. private static void Swap(long[] sk, int i)
  48. {
  49. var t = sk[i];
  50. sk[i] = sk[31 - i];
  51. sk[31 - i] = t;
  52. }
  53. /// <summary>
  54. /// S盒
  55. /// </summary>
  56. public byte[] SboxTable = new byte[]
  57. {
  58. // 0 1 2 3 4 5 6 7 8 9 a b c d e f
  59. 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
  60. 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
  61. 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,
  62. 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,
  63. 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,
  64. 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,
  65. 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,
  66. 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,
  67. 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,
  68. 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
  69. 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
  70. 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
  71. 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
  72. 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
  73. 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
  74. 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48
  75. };
  76. /// <summary>
  77. /// 系统参数FK
  78. /// </summary>
  79. public uint[] FK = { 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc };
  80. /// <summary>
  81. /// 固定参数CK
  82. /// </summary>
  83. public uint[] CK =
  84. {
  85. 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
  86. 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
  87. 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
  88. 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
  89. 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
  90. 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
  91. 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
  92. 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279
  93. };
  94. /// <summary>
  95. /// Sm4的S盒取值
  96. /// </summary>
  97. /// <param name="inch"></param>
  98. /// <returns></returns>
  99. private byte Sm4Sbox(byte inch)
  100. {
  101. var i = inch & 0xFF;
  102. var retVal = SboxTable[i];
  103. return retVal;
  104. }
  105. /// <summary>
  106. /// 线性变换 L
  107. /// </summary>
  108. /// <param name="ka"></param>
  109. /// <returns></returns>
  110. private long Sm4Lt(long ka)
  111. {
  112. var a = new byte[4];
  113. var b = new byte[4];
  114. PutULongToBe(ka, a, 0);
  115. b[0] = Sm4Sbox(a[0]);
  116. b[1] = Sm4Sbox(a[1]);
  117. b[2] = Sm4Sbox(a[2]);
  118. b[3] = Sm4Sbox(a[3]);
  119. var bb = GetULongByBe(b, 0);
  120. var c = bb ^ Rotl(bb, 2) ^ Rotl(bb, 10) ^ Rotl(bb, 18) ^ Rotl(bb, 24);
  121. return c;
  122. }
  123. /// <summary>
  124. /// 轮函数 F
  125. /// </summary>
  126. /// <param name="x0"></param>
  127. /// <param name="x1"></param>
  128. /// <param name="x2"></param>
  129. /// <param name="x3"></param>
  130. /// <param name="rk"></param>
  131. /// <returns></returns>
  132. private long Sm4F(long x0, long x1, long x2,
  133. long x3, long rk)
  134. {
  135. return x0 ^ Sm4Lt(x1 ^ x2 ^ x3 ^ rk);
  136. }
  137. /// <summary>
  138. /// 轮密钥rk
  139. /// </summary>
  140. /// <param name="ka"></param>
  141. /// <returns></returns>
  142. private long Sm4CalciRk(long ka)
  143. {
  144. var a = new byte[4];
  145. var b = new byte[4];
  146. PutULongToBe(ka, a, 0);
  147. b[0] = Sm4Sbox(a[0]);
  148. b[1] = Sm4Sbox(a[1]);
  149. b[2] = Sm4Sbox(a[2]);
  150. b[3] = Sm4Sbox(a[3]);
  151. var bb = GetULongByBe(b, 0);
  152. var rk = bb ^ Rotl(bb, 13) ^ Rotl(bb, 23);
  153. return rk;
  154. }
  155. /// <summary>
  156. /// 加密密钥
  157. /// </summary>
  158. /// <param name="SK"></param>
  159. /// <param name="key"></param>
  160. private void SetKey(long[] SK, byte[] key)
  161. {
  162. //加密密钥长度为 128 比特
  163. var MK = new long[4];
  164. var i = 0;
  165. MK[0] = GetULongByBe(key, 0);
  166. MK[1] = GetULongByBe(key, 4);
  167. MK[2] = GetULongByBe(key, 8);
  168. MK[3] = GetULongByBe(key, 12);
  169. var k = new long[36];
  170. //轮密钥生成方法
  171. k[0] = MK[0] ^ FK[0];
  172. k[1] = MK[1] ^ FK[1];
  173. k[2] = MK[2] ^ FK[2];
  174. k[3] = MK[3] ^ FK[3];
  175. for (; i < 32; i++)
  176. {
  177. k[i + 4] = k[i] ^ Sm4CalciRk(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]);
  178. SK[i] = k[i + 4];
  179. }
  180. }
  181. /// <summary>
  182. /// 解密函数
  183. /// </summary>
  184. /// <param name="sk">轮密钥</param>
  185. /// <param name="input">输入分组的密文</param>
  186. /// <param name="output">输出的对应的分组明文</param>
  187. private void Sm4OneRound(long[] sk, byte[] input, byte[] output)
  188. {
  189. var i = 0;
  190. var ulbuf = new long[36];
  191. ulbuf[0] = GetULongByBe(input, 0);
  192. ulbuf[1] = GetULongByBe(input, 4);
  193. ulbuf[2] = GetULongByBe(input, 8);
  194. ulbuf[3] = GetULongByBe(input, 12);
  195. while (i < 32)//开始32轮解密 ,一次进行4轮,共计八次
  196. {
  197. ulbuf[i + 4] = Sm4F(ulbuf[i], ulbuf[i + 1], ulbuf[i + 2], ulbuf[i + 3],
  198. sk[i]);
  199. i++;
  200. }
  201. PutULongToBe(ulbuf[35], output, 0);
  202. PutULongToBe(ulbuf[34], output, 4);
  203. PutULongToBe(ulbuf[33], output, 8);
  204. PutULongToBe(ulbuf[32], output, 12);
  205. }
  206. /// <summary>
  207. /// 补足 16 进制字符串的 0 字符,返回不带 0x 的16进制字符串
  208. /// </summary>
  209. /// <param name="input"></param>
  210. /// <param name="mode">1表示加密,0表示解密</param>
  211. /// <returns></returns>
  212. private static byte[] Padding(byte[] input, int mode)
  213. {
  214. if (input == null)
  215. {
  216. return null;
  217. }
  218. byte[] ret;
  219. if (mode == 1)
  220. {
  221. var p = 16 - input.Length % 16;
  222. ret = new byte[input.Length + p];
  223. Array.Copy(input, 0, ret, 0,
  224. input.Length);
  225. for (var i = 0; i < p; i++)
  226. {
  227. ret[input.Length + i] = (byte)p;
  228. }
  229. }
  230. else
  231. {
  232. int p = input[input.Length - 1];
  233. ret = new byte[input.Length - p];
  234. Array.Copy(input, 0, ret, 0,
  235. input.Length - p);
  236. }
  237. return ret;
  238. }
  239. /// <summary>
  240. /// 设置加密的key
  241. /// </summary>
  242. /// <param name="ctx"></param>
  243. /// <param name="key"></param>
  244. public void SetKeyEnc(Sm4Context ctx, byte[] key)
  245. {
  246. ctx.Mode = 1;
  247. SetKey(ctx.Key, key);
  248. }
  249. /// <summary>
  250. /// 设置解密的key
  251. /// </summary>
  252. /// <param name="ctx"></param>
  253. /// <param name="key"></param>
  254. public void Sm4SetKeyDec(Sm4Context ctx, byte[] key)
  255. {
  256. ctx.Mode = 0;
  257. SetKey(ctx.Key, key);
  258. int i;
  259. for (i = 0; i < 16; i++)
  260. {
  261. Swap(ctx.Key, i);
  262. }
  263. }
  264. /// <summary>
  265. /// ECB
  266. /// </summary>
  267. /// <param name="ctx"></param>
  268. /// <param name="input"></param>
  269. /// <returns></returns>
  270. public byte[] Sm4CryptEcb(Sm4Context ctx, byte[] input)
  271. {
  272. if (ctx.IsPadding && ctx.Mode == 1)
  273. {
  274. input = Padding(input, 1);
  275. }
  276. var length = input.Length;
  277. var bins = new byte[length];
  278. Array.Copy(input, 0, bins, 0,
  279. length);
  280. var bous = new byte[length];
  281. for (var i = 0; length > 0; length -= 16, i++)
  282. {
  283. var inBytes = new byte[16];
  284. var outBytes = new byte[16];
  285. Array.Copy(bins, i * 16, inBytes, 0,
  286. length > 16 ? 16 : length);
  287. Sm4OneRound(ctx.Key, inBytes, outBytes);
  288. Array.Copy(outBytes, 0, bous, i * 16,
  289. length > 16 ? 16 : length);
  290. }
  291. if (ctx.IsPadding && ctx.Mode == 0)
  292. {
  293. bous = Padding(bous, 0);
  294. }
  295. return bous;
  296. }
  297. /// <summary>
  298. /// CBC
  299. /// </summary>
  300. /// <param name="ctx"></param>
  301. /// <param name="iv"></param>
  302. /// <param name="input"></param>
  303. /// <returns></returns>
  304. public byte[] Sm4CryptCbc(Sm4Context ctx, byte[] iv, byte[] input)
  305. {
  306. if (ctx.IsPadding && ctx.Mode == 1)
  307. {
  308. input = Padding(input, 1);
  309. }
  310. var length = input.Length;
  311. var bins = new byte[length];
  312. Array.Copy(input, 0, bins, 0,
  313. length);
  314. var bousList = new List<byte>();
  315. int i;
  316. if (ctx.Mode == 1)
  317. {
  318. for (var j = 0; length > 0; length -= 16, j++)
  319. {
  320. var inBytes = new byte[16];
  321. var outBytes = new byte[16];
  322. var out1 = new byte[16];
  323. Array.Copy(bins, j * 16, inBytes, 0,
  324. length > 16 ? 16 : length);
  325. for (i = 0; i < 16; i++)
  326. {
  327. outBytes[i] = (byte)(inBytes[i] ^ iv[i]);
  328. }
  329. Sm4OneRound(ctx.Key, outBytes, out1);
  330. Array.Copy(out1, 0, iv, 0,
  331. 16);
  332. for (var k = 0; k < 16; k++)
  333. {
  334. bousList.Add(out1[k]);
  335. }
  336. }
  337. }
  338. else
  339. {
  340. var temp = new byte[16];
  341. for (var j = 0; length > 0; length -= 16, j++)
  342. {
  343. var inBytes = new byte[16];
  344. var outBytes = new byte[16];
  345. var out1 = new byte[16];
  346. Array.Copy(bins, j * 16, inBytes, 0,
  347. length > 16 ? 16 : length);
  348. Array.Copy(inBytes, 0, temp, 0,
  349. 16);
  350. Sm4OneRound(ctx.Key, inBytes, outBytes);
  351. for (i = 0; i < 16; i++)
  352. {
  353. out1[i] = (byte)(outBytes[i] ^ iv[i]);
  354. }
  355. Array.Copy(temp, 0, iv, 0,
  356. 16);
  357. for (var k = 0; k < 16; k++)
  358. {
  359. bousList.Add(out1[k]);
  360. }
  361. }
  362. }
  363. if (ctx.IsPadding && ctx.Mode == 0)
  364. {
  365. var bous = Padding(bousList.ToArray(), 0);
  366. return bous;
  367. }
  368. return bousList.ToArray();
  369. }
  370. }
  371. /// <summary>
  372. /// SM4处理中心
  373. /// </summary>
  374. public class Sm4Context
  375. {
  376. public Sm4Context()
  377. {
  378. Mode = 1;
  379. IsPadding = true;
  380. Key = new long[32];
  381. }
  382. /// <summary>
  383. /// 1表示加密,0表示解密
  384. /// </summary>
  385. public int Mode;
  386. /// <summary>
  387. /// 密钥
  388. /// </summary>
  389. public long[] Key;
  390. /// <summary>
  391. /// 是否补足16进制字符串
  392. /// </summary>
  393. public bool IsPadding;
  394. }