AES_PKCS7.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "AES_PKCS7.h"
  2. //#include "malloc.h"
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include "_string.h"
  7. // 为了能针对C51进行优化,并且又使代码可用于ARM和PC等环境,
  8. // 在非C51环境(没有定义__C51__)下需要把C51特定的关键字定义为空
  9. #ifndef __C51__
  10. #define code
  11. #define data
  12. #define idata
  13. #define xdata
  14. #define pdata
  15. typedef unsigned char BOOL;
  16. #else
  17. typedef bit BOOL;
  18. #endif
  19. #define Nk (AES_KEY_LENGTH / 32) // 以“字”(4字节)为单位的密钥长度
  20. #define Nb 4 // 以“字”(4字节)为单位的加解密数据块大小,固定为4
  21. // Nr:加密的轮数
  22. #if AES_KEY_LENGTH == 128
  23. #define Nr 10
  24. #elif AES_KEY_LENGTH == 192
  25. #define Nr 12
  26. #elif AES_KEY_LENGTH == 256
  27. #define Nr 14
  28. #else
  29. #error AES_KEY_LENGTH must be 128, 192 or 256 BOOLs!
  30. #endif
  31. // GF(28) 多项式
  32. #define BPOLY 0x1B // Lower 8 BOOLs of (x^8 + x^4 + x^3 + x + 1), ie. (x^4 + x^3 + x + 1).
  33. // AES子密钥表,当密钥长度为128位时,占用176字节空间
  34. static xdata unsigned char g_roundKeyTable[4*Nb*(Nr+1)];
  35. // 加密用的SBox
  36. static code const unsigned char SBox[256] =
  37. {
  38. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  39. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  40. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  41. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  42. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  43. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  44. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  45. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  46. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  47. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  48. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  49. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  50. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  51. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  52. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  53. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  54. };
  55. // 解密用的SBox
  56. static code const unsigned char InvSBox[256] =
  57. {
  58. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  59. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  60. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  61. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  62. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x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14, 0x63, 0x55, 0x21, 0x0c, 0x7d
  74. };
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // 函数名: RotationWord
  77. // 描述: 对一个“字”数据进行循环右移。
  78. // 输入参数: pWord -- 要右移的4字节数据。
  79. // 输出参数: pWord -- 右移后的4字节数据。
  80. // 返回值: 无。
  81. ///////////////////////////////////////////////////////////////////////////////
  82. static void RotationWord(unsigned char *pWord)
  83. {
  84. unsigned char temp = pWord[0];
  85. pWord[0] = pWord[1];
  86. pWord[1] = pWord[2];
  87. pWord[2] = pWord[3];
  88. pWord[3] = temp;
  89. }
  90. ///////////////////////////////////////////////////////////////////////////////
  91. // 函数名: XorBytes
  92. // 描述: 批量异或两组数据。
  93. // 输入参数: pData1 -- 要异或的第一组数据。
  94. // pData1 -- 要异或的第二组数据。
  95. // nCount -- 要异或的数据长度。
  96. // 输出参数: pData1 -- 异或后的结果。
  97. // 返回值: 无。
  98. ///////////////////////////////////////////////////////////////////////////////
  99. static void XorBytes(unsigned char *pData1, const unsigned char *pData2, unsigned char nCount)
  100. {
  101. unsigned char i;
  102. for (i = 0; i < nCount; i++)
  103. {
  104. pData1[i] ^= pData2[i];
  105. }
  106. }
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // 函数名: AddRoundKey
  109. // 描述: 把 中间状态数据 加上(异或)子密钥,数据长度为16字节。
  110. // 输入参数: pState -- 状态数据。
  111. // pRoundKey -- 子密钥数据。
  112. // 输出参数: pState -- 加上子密钥后的状态数据。
  113. // 返回值: 无。
  114. ///////////////////////////////////////////////////////////////////////////////
  115. // static void AddRoundKey(unsigned char *pState, const unsigned char *pRoundKey)
  116. // {
  117. // XorBytes(pState, pRoundKey, 4*Nb);
  118. // }
  119. // AddRoundKey的宏形式,比函数形式可以节省4字节的data数据
  120. #define AddRoundKey(pState, pRoundKey) \
  121. XorBytes((pState), (pRoundKey), 4*Nb)
  122. ///////////////////////////////////////////////////////////////////////////////
  123. // 函数名: SubBytes
  124. // 描述: 通过S盒子置换状态数据。
  125. // 输入参数: pState -- 状态数据。
  126. // nCount -- 状态数据长度。
  127. // bInvert -- 是否使用反向S盒子(解密时使用)。
  128. // 输出参数: pState -- 置换后的状态数据。
  129. // 返回值: 无。
  130. ///////////////////////////////////////////////////////////////////////////////
  131. static void SubBytes(unsigned char *pState, unsigned char nCount, BOOL bInvert)
  132. {
  133. unsigned char i;
  134. const unsigned char code *pSBox = bInvert ? InvSBox : SBox;
  135. for (i = 0; i < nCount; i++)
  136. {
  137. pState[i] = pSBox[pState[i]];
  138. }
  139. }
  140. ///////////////////////////////////////////////////////////////////////////////
  141. // 函数名: ShiftRows
  142. // 描述: 把状态数据移行。
  143. // 输入参数: pState -- 状态数据。
  144. // bInvert -- 是否反向移行(解密时使用)。
  145. // 输出参数: pState -- 移行后的状态数据。
  146. // 返回值: 无。
  147. ///////////////////////////////////////////////////////////////////////////////
  148. static void ShiftRows(unsigned char *pState, BOOL bInvert)
  149. {
  150. // 注意:状态数据以列形式存放!
  151. unsigned char r; // row, 行
  152. unsigned char c; // column,列
  153. unsigned char temp;
  154. unsigned char rowData[4];
  155. for (r = 1; r < 4; r++)
  156. {
  157. // 备份一行数据
  158. for (c = 0; c < 4; c++)
  159. {
  160. rowData[c] = pState[r + 4*c];
  161. }
  162. temp = bInvert ? (4 - r) : r;
  163. for (c = 0; c < 4; c++)
  164. {
  165. pState[r + 4*c] = rowData[(c + temp) % 4];
  166. }
  167. }
  168. }
  169. ///////////////////////////////////////////////////////////////////////////////
  170. // 函数名: GfMultBy02
  171. // 描述: 在GF(28)域的 乘2 运算。
  172. // 输入参数: num -- 乘数。
  173. // 输出参数: 无。
  174. // 返回值: num乘以2的结果。
  175. ///////////////////////////////////////////////////////////////////////////////
  176. static unsigned char GfMultBy02(unsigned char num)
  177. {
  178. if ((num & 0x80) == 0)
  179. {
  180. num = num << 1;
  181. }
  182. else
  183. {
  184. num = (num << 1) ^ BPOLY;
  185. }
  186. return num;
  187. }
  188. ///////////////////////////////////////////////////////////////////////////////
  189. // 函数名: MixColumns
  190. // 描述: 混合状态各列数据。
  191. // 输入参数: pState -- 状态数据。
  192. // bInvert -- 是否反向混合(解密时使用)。
  193. // 输出参数: pState -- 混合列后的状态数据。
  194. // 返回值: 无。
  195. ///////////////////////////////////////////////////////////////////////////////
  196. static void MixColumns(unsigned char *pState, BOOL bInvert)
  197. {
  198. unsigned char i;
  199. unsigned char temp;
  200. unsigned char a0Pa2_M4; // 4(a0 + a2)
  201. unsigned char a1Pa3_M4; // 4(a1 + a3)
  202. unsigned char result[4];
  203. for (i = 0; i < 4; i++, pState += 4)
  204. {
  205. // b0 = 2a0 + 3a1 + a2 + a3
  206. // = (a0 + a1 + a2 + a3) + 2(a0 + a1) + a0
  207. temp = pState[0] ^ pState[1] ^ pState[2] ^ pState[3];
  208. result[0] = temp ^ pState[0] ^ GfMultBy02((unsigned char) (pState[0] ^ pState[1]));
  209. result[1] = temp ^ pState[1] ^ GfMultBy02((unsigned char) (pState[1] ^ pState[2]));
  210. result[2] = temp ^ pState[2] ^ GfMultBy02((unsigned char) (pState[2] ^ pState[3]));
  211. result[3] = temp ^ pState[3] ^ GfMultBy02((unsigned char) (pState[3] ^ pState[0]));
  212. if (bInvert)
  213. {
  214. // b0' = 14a0 + 11a1 + 13a2 + 9a3
  215. // = (a0 + a1 + a2 + a3) + 2(a0 + a1) + a0 (这部分为b0)
  216. // + 2(4(a0 + a2) + 4(a1 + a3))
  217. // + 4(a0 + a2)
  218. a0Pa2_M4 = GfMultBy02(GfMultBy02((unsigned char) (pState[0] ^ pState[2])));
  219. a1Pa3_M4 = GfMultBy02(GfMultBy02((unsigned char) (pState[1] ^ pState[3])));
  220. temp = GfMultBy02((unsigned char) (a0Pa2_M4 ^ a1Pa3_M4));
  221. result[0] ^= temp ^ a0Pa2_M4;
  222. result[1] ^= temp ^ a1Pa3_M4;
  223. result[2] ^= temp ^ a0Pa2_M4;
  224. result[3] ^= temp ^ a1Pa3_M4;
  225. }
  226. memcpy(pState, result, 4);
  227. }
  228. }
  229. ///////////////////////////////////////////////////////////////////////////////
  230. // 函数名: BlockEncrypt
  231. // 描述: 对单块数据加密。
  232. // 输入参数: pState -- 状态数据。
  233. // 输出参数: pState -- 加密后的状态数据。
  234. // 返回值: 无。
  235. ///////////////////////////////////////////////////////////////////////////////
  236. static void BlockEncrypt(unsigned char *pState)
  237. {
  238. unsigned char i;
  239. AddRoundKey(pState, g_roundKeyTable);
  240. for (i = 1; i <= Nr; i++) // i = [1, Nr]
  241. {
  242. SubBytes(pState, 4*Nb, 0);
  243. ShiftRows(pState, 0);
  244. if (i != Nr)
  245. {
  246. MixColumns(pState, 0);
  247. }
  248. AddRoundKey(pState, &g_roundKeyTable[4*Nb*i]);
  249. }
  250. // 为了节省代码,合并到循化执行
  251. // SubBytes(pState, 4*Nb);
  252. // ShiftRows(pState, 0);
  253. // AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
  254. }
  255. ///////////////////////////////////////////////////////////////////////////////
  256. // 函数名: BlockDecrypt
  257. // 描述: 对单块数据解密。
  258. // 输入参数: pState -- 状态数据。
  259. // 输出参数: pState -- 解密后的状态数据。
  260. // 返回值: 无。
  261. ///////////////////////////////////////////////////////////////////////////////
  262. static void BlockDecrypt(unsigned char *pState)
  263. {
  264. unsigned char i;
  265. AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
  266. for (i = Nr; i > 0; i--) // i = [Nr, 1]
  267. {
  268. ShiftRows(pState, 1);
  269. SubBytes(pState, 4*Nb, 1);
  270. AddRoundKey(pState, &g_roundKeyTable[4*Nb*(i-1)]);
  271. if (i != 1)
  272. {
  273. MixColumns(pState, 1);
  274. }
  275. }
  276. }
  277. ///////////////////////////////////////////////////////////////////////////////
  278. // 函数名: AES_Init
  279. // 描述: 初始化,在此执行扩展密钥操作。
  280. // 输入参数: pKey -- 原始密钥,其长度必须为 AES_KEY_LENGTH/8 字节。
  281. // 输出参数: 无。
  282. // 返回值: 无。
  283. ///////////////////////////////////////////////////////////////////////////////
  284. void AES_Init(const void *pKey)
  285. {
  286. // 扩展密钥
  287. unsigned char i;
  288. unsigned char *pRoundKey;
  289. unsigned char Rcon[4] = {0x01, 0x00, 0x00, 0x00};
  290. memcpy(g_roundKeyTable, pKey, 4*Nk);
  291. pRoundKey = &g_roundKeyTable[4*Nk];
  292. for (i = Nk; i < Nb*(Nr+1); pRoundKey += 4, i++)
  293. {
  294. memcpy(pRoundKey, pRoundKey - 4, 4);
  295. if (i % Nk == 0)
  296. {
  297. RotationWord(pRoundKey);
  298. SubBytes(pRoundKey, 4, 0);
  299. XorBytes(pRoundKey, Rcon, 4);
  300. Rcon[0] = GfMultBy02(Rcon[0]);
  301. }
  302. else if (Nk > 6 && i % Nk == Nb)
  303. {
  304. SubBytes(pRoundKey, 4, 0);
  305. }
  306. XorBytes(pRoundKey, pRoundKey - 4*Nk, 4);
  307. }
  308. }
  309. unsigned int AES_get_length(unsigned int length)
  310. {
  311. return ((length>>4) + 1)<<4;
  312. }
  313. //////////////////////////////////////////////////////////////////////////
  314. // 函数名: AES_Encrypt
  315. // 描述: 加密数据
  316. // 输入参数: pPlainText -- 明文,即需加密的数据,其长度为nDataLen字节。
  317. // nDataLen -- 数据长度,以字节为单位
  318. // pIV -- 初始化向量,如果使用ECB模式,可设为NULL。
  319. // 输出参数: pCipherText -- 密文,即由明文加密后的数据,可以与pPlainText相同。
  320. // 返回值: 无。
  321. //////////////////////////////////////////////////////////////////////////
  322. unsigned int AES_Encrypt_PKCS7(const unsigned char *pPlainText, unsigned char *pCipherText,
  323. unsigned int nDataLen, const unsigned char *pIV)
  324. {
  325. unsigned int i;
  326. //长度调整
  327. unsigned int length = nDataLen;
  328. nDataLen = ((nDataLen>>4) + 1)<<4;
  329. uint8_t p=(Nb<<2)-(length%(Nb<<2));
  330. if (pPlainText != pCipherText){memcpy(pCipherText,pPlainText,length);}
  331. //填充
  332. unsigned char temp[16];
  333. memset(temp,p,16);
  334. if(length<nDataLen)
  335. {
  336. memcpy(pCipherText + length,temp,nDataLen - length);
  337. }
  338. if(length == nDataLen)
  339. {
  340. memcpy(pCipherText + length,temp,16);
  341. }
  342. for (i = nDataLen/(4*Nb); i > 0 ; i--, pCipherText += 4*Nb)
  343. {
  344. #if AES_MODE == AES_MODE_CBC
  345. XorBytes(pCipherText, pIV, 4*Nb);
  346. #endif
  347. BlockEncrypt(pCipherText);
  348. pIV = pCipherText;
  349. }
  350. return(nDataLen);
  351. }
  352. //////////////////////////////////////////////////////////////////////////
  353. // 函数名: AES_Decrypt
  354. // 描述: 解密数据
  355. // 输入参数: pCipherText -- 密文,即需解密的数据,其长度为nDataLen字节。
  356. // nDataLen -- 数据长度,以字节为单位,必须为AES_KEY_LENGTH/8的整倍数。
  357. // pIV -- 初始化向量,如果使用ECB模式,可设为NULL。
  358. // 输出参数: pPlainText -- 明文,即由密文解密后的数据,可以与pCipherText相同。
  359. // 返回值: 无。
  360. //////////////////////////////////////////////////////////////////////////
  361. void AES_Decrypt(unsigned char *pPlainText, const unsigned char *pCipherText,
  362. unsigned int nDataLen, const unsigned char *pIV)
  363. {
  364. unsigned int i;
  365. if (pPlainText != pCipherText)
  366. {
  367. memcpy(pPlainText, pCipherText, nDataLen);
  368. }
  369. // 从最后一块数据开始解密,这样不用开辟空间来保存IV
  370. pPlainText += nDataLen - 4*Nb;
  371. for (i = nDataLen/(4*Nb); i > 0 ; i--, pPlainText -= 4*Nb)
  372. {
  373. BlockDecrypt(pPlainText);
  374. #if AES_MODE == AES_MODE_CBC
  375. if (i == 1)
  376. {// 最后一块数据
  377. XorBytes(pPlainText, pIV, 4*Nb);
  378. }
  379. else
  380. {
  381. XorBytes(pPlainText, pPlainText - 4*Nb, 4*Nb);
  382. }
  383. #endif
  384. }
  385. }