2
0

mvAesApi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* rijndael-api-ref.c v2.1 April 2000
  2. * Reference ANSI C code
  3. * authors: v2.0 Paulo Barreto
  4. * Vincent Rijmen, K.U.Leuven
  5. * v2.1 Vincent Rijmen, K.U.Leuven
  6. *
  7. * This code is placed in the public domain.
  8. */
  9. #include "mvOs.h"
  10. #include "mvAes.h"
  11. #include "mvAesAlg.h"
  12. /* Defines:
  13. Add any additional defines you need
  14. */
  15. #define MODE_ECB 1 /* Are we ciphering in ECB mode? */
  16. #define MODE_CBC 2 /* Are we ciphering in CBC mode? */
  17. #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
  18. int aesMakeKey(MV_U8 *expandedKey, MV_U8 *keyMaterial, int keyLen, int blockLen)
  19. {
  20. MV_U8 W[MAXROUNDS+1][4][MAXBC];
  21. MV_U8 k[4][MAXKC];
  22. MV_U8 j;
  23. int i, rounds, KC;
  24. if (expandedKey == NULL)
  25. {
  26. return AES_BAD_KEY_INSTANCE;
  27. }
  28. if (!((keyLen == 128) || (keyLen == 192) || (keyLen == 256)))
  29. {
  30. return AES_BAD_KEY_MAT;
  31. }
  32. if (keyMaterial == NULL)
  33. {
  34. return AES_BAD_KEY_MAT;
  35. }
  36. /* initialize key schedule: */
  37. for(i=0; i<keyLen/8; i++)
  38. {
  39. j = keyMaterial[i];
  40. k[i % 4][i / 4] = j;
  41. }
  42. rijndaelKeySched (k, keyLen, blockLen, W);
  43. #ifdef MV_AES_DEBUG
  44. {
  45. MV_U8* pW = &W[0][0][0];
  46. int x;
  47. mvOsPrintf("Expended Key: size = %d\n", sizeof(W));
  48. for(i=0; i<sizeof(W); i++)
  49. {
  50. mvOsPrintf("%02x ", pW[i]);
  51. }
  52. for(i=0; i<MAXROUNDS+1; i++)
  53. {
  54. mvOsPrintf("\n Round #%02d: ", i);
  55. for(x=0; x<MAXBC; x++)
  56. {
  57. mvOsPrintf("%02x%02x%02x%02x ",
  58. W[i][0][x], W[i][1][x], W[i][2][x], W[i][3][x]);
  59. }
  60. mvOsPrintf("\n");
  61. }
  62. }
  63. #endif /* MV_AES_DEBUG */
  64. switch (keyLen)
  65. {
  66. case 128:
  67. rounds = 10;
  68. KC = 4;
  69. break;
  70. case 192:
  71. rounds = 12;
  72. KC = 6;
  73. break;
  74. case 256:
  75. rounds = 14;
  76. KC = 8;
  77. break;
  78. default :
  79. return (-1);
  80. }
  81. for(i=0; i<MAXBC; i++)
  82. {
  83. for(j=0; j<4; j++)
  84. {
  85. expandedKey[i*4+j] = W[rounds][j][i];
  86. }
  87. }
  88. for(; i<KC; i++)
  89. {
  90. for(j=0; j<4; j++)
  91. {
  92. expandedKey[i*4+j] = W[rounds-1][j][i+MAXBC-KC];
  93. }
  94. }
  95. return 0;
  96. }
  97. int aesBlockEncrypt128(MV_U8 mode, MV_U8 *IV, MV_U8 *expandedKey, int keyLen,
  98. MV_U32 *plain, int numBlocks, MV_U32 *cipher)
  99. {
  100. int i, j, t;
  101. MV_U8 block[4][MAXBC];
  102. int rounds;
  103. char *input, *outBuffer;
  104. input = (char*)plain;
  105. outBuffer = (char*)cipher;
  106. /* check parameter consistency: */
  107. if( (expandedKey == NULL) || ((keyLen != 128) && (keyLen != 192) && (keyLen != 256)))
  108. {
  109. return AES_BAD_KEY_MAT;
  110. }
  111. if ((mode != MODE_ECB && mode != MODE_CBC))
  112. {
  113. return AES_BAD_CIPHER_STATE;
  114. }
  115. switch (keyLen)
  116. {
  117. case 128: rounds = 10; break;
  118. case 192: rounds = 12; break;
  119. case 256: rounds = 14; break;
  120. default : return (-3); /* this cannot happen */
  121. }
  122. switch (mode)
  123. {
  124. case MODE_ECB:
  125. for (i = 0; i < numBlocks; i++)
  126. {
  127. for (j = 0; j < 4; j++)
  128. {
  129. for(t = 0; t < 4; t++)
  130. /* parse input stream into rectangular array */
  131. block[t][j] = input[16*i+4*j+t] & 0xFF;
  132. }
  133. rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
  134. for (j = 0; j < 4; j++)
  135. {
  136. /* parse rectangular array into output ciphertext bytes */
  137. for(t = 0; t < 4; t++)
  138. outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
  139. }
  140. }
  141. break;
  142. case MODE_CBC:
  143. for (j = 0; j < 4; j++)
  144. {
  145. for(t = 0; t < 4; t++)
  146. /* parse initial value into rectangular array */
  147. block[t][j] = IV[t+4*j] & 0xFF;
  148. }
  149. for (i = 0; i < numBlocks; i++)
  150. {
  151. for (j = 0; j < 4; j++)
  152. {
  153. for(t = 0; t < 4; t++)
  154. /* parse input stream into rectangular array and exor with
  155. IV or the previous ciphertext */
  156. block[t][j] ^= input[16*i+4*j+t] & 0xFF;
  157. }
  158. rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
  159. for (j = 0; j < 4; j++)
  160. {
  161. /* parse rectangular array into output ciphertext bytes */
  162. for(t = 0; t < 4; t++)
  163. outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
  164. }
  165. }
  166. break;
  167. default: return AES_BAD_CIPHER_STATE;
  168. }
  169. return 0;
  170. }
  171. int aesBlockDecrypt128(MV_U8 mode, MV_U8 *IV, MV_U8 *expandedKey, int keyLen,
  172. MV_U32 *srcData, int numBlocks, MV_U32 *dstData)
  173. {
  174. int i, j, t;
  175. MV_U8 block[4][MAXBC];
  176. MV_U8 iv[4][MAXBC];
  177. int rounds;
  178. char *input, *outBuffer;
  179. input = (char*)srcData;
  180. outBuffer = (char*)dstData;
  181. if (expandedKey == NULL)
  182. {
  183. return AES_BAD_KEY_MAT;
  184. }
  185. /* check parameter consistency: */
  186. if (keyLen != 128 && keyLen != 192 && keyLen != 256)
  187. {
  188. return AES_BAD_KEY_MAT;
  189. }
  190. if ((mode != MODE_ECB && mode != MODE_CBC))
  191. {
  192. return AES_BAD_CIPHER_STATE;
  193. }
  194. switch (keyLen)
  195. {
  196. case 128: rounds = 10; break;
  197. case 192: rounds = 12; break;
  198. case 256: rounds = 14; break;
  199. default : return (-3); /* this cannot happen */
  200. }
  201. switch (mode)
  202. {
  203. case MODE_ECB:
  204. for (i = 0; i < numBlocks; i++)
  205. {
  206. for (j = 0; j < 4; j++)
  207. {
  208. for(t = 0; t < 4; t++)
  209. {
  210. /* parse input stream into rectangular array */
  211. block[t][j] = input[16*i+4*j+t] & 0xFF;
  212. }
  213. }
  214. rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
  215. for (j = 0; j < 4; j++)
  216. {
  217. /* parse rectangular array into output ciphertext bytes */
  218. for(t = 0; t < 4; t++)
  219. outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
  220. }
  221. }
  222. break;
  223. case MODE_CBC:
  224. /* first block */
  225. for (j = 0; j < 4; j++)
  226. {
  227. for(t = 0; t < 4; t++)
  228. {
  229. /* parse input stream into rectangular array */
  230. block[t][j] = input[4*j+t] & 0xFF;
  231. iv[t][j] = block[t][j];
  232. }
  233. }
  234. rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
  235. for (j = 0; j < 4; j++)
  236. {
  237. /* exor the IV and parse rectangular array into output ciphertext bytes */
  238. for(t = 0; t < 4; t++)
  239. {
  240. outBuffer[4*j+t] = (MV_U8) (block[t][j] ^ IV[t+4*j]);
  241. IV[t+4*j] = iv[t][j];
  242. }
  243. }
  244. /* next blocks */
  245. for (i = 1; i < numBlocks; i++)
  246. {
  247. for (j = 0; j < 4; j++)
  248. {
  249. for(t = 0; t < 4; t++)
  250. {
  251. /* parse input stream into rectangular array */
  252. iv[t][j] = input[16*i+4*j+t] & 0xFF;
  253. block[t][j] = iv[t][j];
  254. }
  255. }
  256. rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
  257. for (j = 0; j < 4; j++)
  258. {
  259. /* exor previous ciphertext block and parse rectangular array
  260. into output ciphertext bytes */
  261. for(t = 0; t < 4; t++)
  262. {
  263. outBuffer[16*i+4*j+t] = (MV_U8) (block[t][j] ^ IV[t+4*j]);
  264. IV[t+4*j] = iv[t][j];
  265. }
  266. }
  267. }
  268. break;
  269. default: return AES_BAD_CIPHER_STATE;
  270. }
  271. return 0;
  272. }