sms4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* sms4.c
  2. ** SMS4 Encryption algorithm for wireless networks
  3. **
  4. ** $Id: sms4.c 2009-12-31 14:41:57 tao.tang <$">[email protected]>$
  5. **
  6. **
  7. ** This program is free software; you can redistribute it and/or
  8. ** modify it under the terms of the GNU General Public License
  9. ** as published by the Free Software Foundation; either version 2
  10. ** of the License, or (at your option) any later version.
  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, write to the Free Software
  19. ** Foundation, Inc.
  20. **/
  21. #include <string.h>
  22. #include <stdio.h>
  23. /*#include "sms4.h"*/
  24. #ifndef unlong
  25. typedef unsigned long unlong;
  26. #endif /* unlong */
  27. #ifndef unchar
  28. typedef unsigned char unchar;
  29. #endif /* unchar */
  30. /* define SMS4CROL for rotating left */
  31. #define SMS4CROL(uval, bits) ((uval << bits) | (uval >> (0x20 - bits)))
  32. /* define MASK code for selecting expected bits from a 32 bits value */
  33. #define SMS4MASK3 0xFF000000
  34. #define SMS4MASK2 0x00FF0000
  35. #define SMS4MASK1 0x0000FF00
  36. #define SMS4MASK0 0x000000FF
  37. /* Sbox table: 8bits input convert to 8 bits output*/
  38. static unchar SboxTable[16][16] =
  39. {
  40. {0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05},
  41. {0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99},
  42. {0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62},
  43. {0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6},
  44. {0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8},
  45. {0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35},
  46. {0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87},
  47. {0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e},
  48. {0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1},
  49. {0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3},
  50. {0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f},
  51. {0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51},
  52. {0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8},
  53. {0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0},
  54. {0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84},
  55. {0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48}
  56. };
  57. /* Encryption key: 128bits */
  58. static unlong MK[4] = {0x01234567,0x89abcdef,0xfedcba98,0x76543210};
  59. /* System parameter */
  60. static unlong FK[4] = {0xa3b1bac6,0x56aa3350,0x677d9197,0xb27022dc};
  61. /* fixed parameter */
  62. static unlong CK[32] =
  63. {
  64. 0x00070e15,0x1c232a31,0x383f464d,0x545b6269,
  65. 0x70777e85,0x8c939aa1,0xa8afb6bd,0xc4cbd2d9,
  66. 0xe0e7eef5,0xfc030a11,0x181f262d,0x343b4249,
  67. 0x50575e65,0x6c737a81,0x888f969d,0xa4abb2b9,
  68. 0xc0c7ced5,0xdce3eaf1,0xf8ff060d,0x141b2229,
  69. 0x30373e45,0x4c535a61,0x686f767d,0x848b9299,
  70. 0xa0a7aeb5,0xbcc3cad1,0xd8dfe6ed,0xf4fb0209,
  71. 0x10171e25,0x2c333a41,0x484f565d,0x646b7279
  72. };
  73. /* buffer for round encryption key */
  74. static unlong ENRK[32];
  75. static unlong DERK[32];
  76. /* original contents for debugging */
  77. unlong pData[4] =
  78. {
  79. 0x01234567,
  80. 0x89abcdef,
  81. 0xfedcba98,
  82. 0x76543210
  83. };
  84. /* original contents for debugging */
  85. unlong pData2[9] =
  86. {
  87. 0x01234567,
  88. 0x89abcdef,
  89. 0xfedcba98,
  90. 0x76543210,
  91. 0x12121212,
  92. 0x34343434,
  93. 0x56565656,
  94. 0x78787878,
  95. 0x12341234
  96. };
  97. /*=============================================================================
  98. ** private function:
  99. ** look up in SboxTable and get the related value.
  100. ** args: [in] inch: 0x00~0xFF (8 bits unsigned value).
  101. **============================================================================*/
  102. static unchar SMS4Sbox(unchar inch)
  103. {
  104. unchar *pTable = (unchar *)SboxTable;
  105. unchar retVal = (unchar)(pTable[inch]);
  106. return retVal;
  107. }
  108. /*=============================================================================
  109. ** private function:
  110. ** "T algorithm" == "L algorithm" + "t algorithm".
  111. ** args: [in] a: a is a 32 bits unsigned value;
  112. ** return: c: c is calculated with line algorithm "L" and nonline algorithm "t"
  113. **============================================================================*/
  114. static unlong SMS4Lt(unlong a)
  115. {
  116. unlong b = 0;
  117. unlong c = 0;
  118. unchar a0 = (unchar)(a & SMS4MASK0);
  119. unchar a1 = (unchar)((a & SMS4MASK1) >> 8);
  120. unchar a2 = (unchar)((a & SMS4MASK2) >> 16);
  121. unchar a3 = (unchar)((a & SMS4MASK3) >> 24);
  122. unchar b0 = SMS4Sbox(a0);
  123. unchar b1 = SMS4Sbox(a1);
  124. unchar b2 = SMS4Sbox(a2);
  125. unchar b3 = SMS4Sbox(a3);
  126. b =b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
  127. c =b^(SMS4CROL(b, 2))^(SMS4CROL(b, 10))^(SMS4CROL(b, 18))^(SMS4CROL(b, 24));
  128. return c;
  129. }
  130. /*=============================================================================
  131. ** private function:
  132. ** Calculating round encryption key.
  133. ** args: [in] a: a is a 32 bits unsigned value;
  134. ** return: ENRK[i]: i{0,1,2,3,...31}.
  135. **============================================================================*/
  136. static unlong SMS4CalciRK(unlong a)
  137. {
  138. unlong b = 0;
  139. unlong rk = 0;
  140. unchar a0 = (unchar)(a & SMS4MASK0);
  141. unchar a1 = (unchar)((a & SMS4MASK1) >> 8);
  142. unchar a2 = (unchar)((a & SMS4MASK2) >> 16);
  143. unchar a3 = (unchar)((a & SMS4MASK3) >> 24);
  144. unchar b0 = SMS4Sbox(a0);
  145. unchar b1 = SMS4Sbox(a1);
  146. unchar b2 = SMS4Sbox(a2);
  147. unchar b3 = SMS4Sbox(a3);
  148. b = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
  149. rk = b^(SMS4CROL(b, 13))^(SMS4CROL(b, 23));
  150. return rk;
  151. }
  152. /*=============================================================================
  153. ** private function:
  154. ** Calculating round encryption key.
  155. ** args: [in] ulflag: if 0: not calculate DERK , else calculate;
  156. ** return: NONE.
  157. **============================================================================*/
  158. static void SMS4CalcRK(unlong ulflag)
  159. {
  160. unlong k[36];
  161. unlong i = 0;
  162. k[0] = MK[0]^FK[0];
  163. k[1] = MK[1]^FK[1];
  164. k[2] = MK[2]^FK[2];
  165. k[3] = MK[3]^FK[3];
  166. for(; i<32; i++)
  167. {
  168. k[i+4] = k[i] ^ (SMS4CalciRK(k[i+1]^k[i+2]^k[i+3]^CK[i]));
  169. ENRK[i] = k[i+4];
  170. }
  171. if (ulflag != 0x00)
  172. {
  173. for (i=0; i<32; i++)
  174. {
  175. DERK[i] = ENRK[31-i];
  176. }
  177. }
  178. }
  179. /*=============================================================================
  180. ** private function:
  181. ** "T algorithm" == "L algorithm" + "t algorithm".
  182. ** args: [in] a: a is a 32 bits unsigned value.
  183. **============================================================================*/
  184. static unlong SMS4T(unlong a)
  185. {
  186. return (SMS4Lt(a));
  187. }
  188. /*=============================================================================
  189. ** private function:
  190. ** Calculating and getting encryption/decryption contents.
  191. ** args: [in] x0: original contents;
  192. ** args: [in] x1: original contents;
  193. ** args: [in] x2: original contents;
  194. ** args: [in] x3: original contents;
  195. ** args: [in] rk: encryption/decryption key;
  196. ** return the contents of encryption/decryption contents.
  197. **============================================================================*/
  198. static unlong SMS4F(unlong x0, unlong x1, unlong x2, unlong x3, unlong rk)
  199. {
  200. return (x0^SMS4Lt(x1^x2^x3^rk));
  201. }
  202. /*=============================================================================
  203. ** public function:
  204. ** "T algorithm" == "L algorithm" + "t algorithm".
  205. ** args: [in] ulkey: password defined by user(NULL: default encryption key);
  206. ** args: [in] flag: if 0: not calculate DERK , else calculate;
  207. ** return ulkey: NULL for default encryption key.
  208. **============================================================================*/
  209. unlong *SMS4SetKey(unlong *ulkey, unlong flag)
  210. {
  211. if (ulkey != NULL)
  212. {
  213. memcpy(MK, ulkey, sizeof(MK));
  214. }
  215. SMS4CalcRK(flag);
  216. return ulkey;
  217. }
  218. /*=============================================================================
  219. ** public function:
  220. ** sms4 encryption algorithm.
  221. ** args: [in/out] psrc: a pointer point to original contents;
  222. ** args: [in] lgsrc: the length of original contents;
  223. ** args: [in] derk: a pointer point to encryption/decryption key;
  224. ** return: pRet: a pointer point to encrypted contents.
  225. **============================================================================*/
  226. unlong *SMS4Encrypt(unlong *psrc, unlong lgsrc, unlong rk[])
  227. {
  228. unlong *pRet = NULL;
  229. unlong i = 0;
  230. unlong ulbuf[36];
  231. unlong ulCnter = 0;
  232. unlong ulTotal = (lgsrc >> 4);
  233. if(psrc != NULL)
  234. {
  235. pRet = psrc;
  236. /* !!!It's a temporary scheme: start!!! */
  237. /*========================================
  238. ** 16 bytes(128 bits) is deemed as an unit.
  239. **======================================*/
  240. while (ulCnter<ulTotal)
  241. {
  242. /* reset number counter */
  243. i = 0;
  244. /* filled up with 0*/
  245. memset(ulbuf, 0, sizeof(ulbuf));
  246. memcpy(ulbuf, psrc, 16);
  247. #ifdef SMS4DBG0
  248. printf("0x%08x, 0x%08x, 0x%08x, 0x%08x, \n",
  249. ulbuf[0], ulbuf[1], ulbuf[2], ulbuf[3]);
  250. #endif /* SMS4DBG0 */
  251. while(i<32)
  252. {
  253. ulbuf[i+4] = SMS4F(ulbuf[i], ulbuf[i+1],
  254. ulbuf[i+2], ulbuf[i+3], rk[i]);
  255. #ifdef SMS4DBG0
  256. printf("0x%08x, \n", ulbuf[i+4]);
  257. #endif /* SMS4DBG0 */
  258. i++;
  259. }
  260. /* save encrypted contents to original area */
  261. psrc[0] = ulbuf[35];
  262. psrc[1] = ulbuf[34];
  263. psrc[2] = ulbuf[33];
  264. psrc[3] = ulbuf[32];
  265. ulCnter++;
  266. psrc += 4;
  267. }
  268. /* !!!It's a temporary scheme: end!!! */
  269. }
  270. return pRet;
  271. }
  272. /*=============================================================================
  273. ** public function:
  274. ** sms4 decryption algorithm.
  275. ** args: [in/out] psrc: a pointer point to encrypted contents;
  276. ** args: [in] lgsrc: the length of encrypted contents;
  277. ** args: [in] derk: a pointer point to decryption key;
  278. ** return: pRet: a pointer point to decrypted contents.
  279. **============================================================================*/
  280. unlong *SMS4Decrypt(unlong *psrc, unlong lgsrc, unlong derk[])
  281. {
  282. unlong *pRet = NULL;
  283. unlong i = 0;
  284. if(psrc != NULL)
  285. {
  286. pRet = psrc;
  287. /* the same arithmetic, different encryption key sequence. */
  288. SMS4Encrypt(psrc, lgsrc, derk);
  289. }
  290. return pRet;
  291. }
  292. void SMS4Encrypt1M()
  293. {
  294. unlong i = 0;
  295. while (i<1000000)
  296. {
  297. SMS4Encrypt(pData, sizeof(pData), ENRK);
  298. i++;
  299. // if (0 == i%10000)
  300. // {
  301. // printf("encrypted times: %d\n", i);
  302. // }
  303. }
  304. printf("0x%08x, 0x%08x, 0x%08x, 0x%08x. \n",
  305. pData[0], pData[1], pData[2], pData[3]);
  306. }
  307. /* entry-point for debugging */
  308. int main()
  309. {
  310. SMS4SetKey(NULL, 1);
  311. /* cycle1: common test */
  312. printf("0x%08x, 0x%08x, 0x%08x, 0x%08x. \n",
  313. pData[0], pData[1], pData[2], pData[3]);
  314. SMS4Encrypt(pData, sizeof(pData), ENRK);
  315. printf("0x%08x, 0x%08x, 0x%08x, 0x%08x. \n",
  316. pData[0], pData[1], pData[2], pData[3]);
  317. SMS4Decrypt(pData, sizeof(pData), DERK);
  318. printf("0x%08x, 0x%08x, 0x%08x, 0x%08x. \n",
  319. pData[0], pData[1], pData[2], pData[3]);
  320. /* cycle2: encrypted 1000000 times */
  321. SMS4Encrypt1M();
  322. /* cycle3: longer contents */
  323. SMS4Encrypt(pData2, sizeof(pData2), ENRK);
  324. SMS4Decrypt(pData2, sizeof(pData2), DERK);
  325. return 0;
  326. }