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
7 * This code is placed in the public domain.
16 Add any additional defines you need
19 #define MODE_ECB 1 /* Are we ciphering in ECB mode? */
20 #define MODE_CBC 2 /* Are we ciphering in CBC mode? */
21 #define MODE_CFB1 3 /* Are we ciphering in 1-bit CFB mode? */
24 int aesMakeKey(MV_U8
*expandedKey
, MV_U8
*keyMaterial
, int keyLen
, int blockLen
)
26 MV_U8 W
[MAXROUNDS
+1][4][MAXBC
];
31 if (expandedKey
== NULL
)
33 return AES_BAD_KEY_INSTANCE
;
36 if (!((keyLen
== 128) || (keyLen
== 192) || (keyLen
== 256)))
38 return AES_BAD_KEY_MAT
;
41 if (keyMaterial
== NULL
)
43 return AES_BAD_KEY_MAT
;
46 /* initialize key schedule: */
47 for(i
=0; i
<keyLen
/8; i
++)
53 rijndaelKeySched (k
, keyLen
, blockLen
, W
);
56 MV_U8
* pW
= &W
[0][0][0];
59 mvOsPrintf("Expended Key: size = %d\n", sizeof(W
));
60 for(i
=0; i
<sizeof(W
); i
++)
62 mvOsPrintf("%02x ", pW
[i
]);
64 for(i
=0; i
<MAXROUNDS
+1; i
++)
66 mvOsPrintf("\n Round #%02d: ", i
);
67 for(x
=0; x
<MAXBC
; x
++)
69 mvOsPrintf("%02x%02x%02x%02x ",
70 W
[i
][0][x
], W
[i
][1][x
], W
[i
][2][x
], W
[i
][3][x
]);
75 #endif /* MV_AES_DEBUG */
94 for(i
=0; i
<MAXBC
; i
++)
98 expandedKey
[i
*4+j
] = W
[rounds
][j
][i
];
105 expandedKey
[i
*4+j
] = W
[rounds
-1][j
][i
+MAXBC
-KC
];
113 int aesBlockEncrypt128(MV_U8 mode
, MV_U8
*IV
, MV_U8
*expandedKey
, int keyLen
,
114 MV_U32
*plain
, int numBlocks
, MV_U32
*cipher
)
117 MV_U8 block
[4][MAXBC
];
119 char *input
, *outBuffer
;
121 input
= (char*)plain
;
122 outBuffer
= (char*)cipher
;
124 /* check parameter consistency: */
125 if( (expandedKey
== NULL
) || ((keyLen
!= 128) && (keyLen
!= 192) && (keyLen
!= 256)))
127 return AES_BAD_KEY_MAT
;
129 if ((mode
!= MODE_ECB
&& mode
!= MODE_CBC
))
131 return AES_BAD_CIPHER_STATE
;
136 case 128: rounds
= 10; break;
137 case 192: rounds
= 12; break;
138 case 256: rounds
= 14; break;
139 default : return (-3); /* this cannot happen */
146 for (i
= 0; i
< numBlocks
; i
++)
148 for (j
= 0; j
< 4; j
++)
150 for(t
= 0; t
< 4; t
++)
151 /* parse input stream into rectangular array */
152 block
[t
][j
] = input
[16*i
+4*j
+t
] & 0xFF;
154 rijndaelEncrypt128(block
, (MV_U8 (*)[4][MAXBC
])expandedKey
, rounds
);
155 for (j
= 0; j
< 4; j
++)
157 /* parse rectangular array into output ciphertext bytes */
158 for(t
= 0; t
< 4; t
++)
159 outBuffer
[16*i
+4*j
+t
] = (MV_U8
) block
[t
][j
];
166 for (j
= 0; j
< 4; j
++)
168 for(t
= 0; t
< 4; t
++)
169 /* parse initial value into rectangular array */
170 block
[t
][j
] = IV
[t
+4*j
] & 0xFF;
172 for (i
= 0; i
< numBlocks
; i
++)
174 for (j
= 0; j
< 4; j
++)
176 for(t
= 0; t
< 4; t
++)
177 /* parse input stream into rectangular array and exor with
178 IV or the previous ciphertext */
179 block
[t
][j
] ^= input
[16*i
+4*j
+t
] & 0xFF;
181 rijndaelEncrypt128(block
, (MV_U8 (*)[4][MAXBC
])expandedKey
, rounds
);
182 for (j
= 0; j
< 4; j
++)
184 /* parse rectangular array into output ciphertext bytes */
185 for(t
= 0; t
< 4; t
++)
186 outBuffer
[16*i
+4*j
+t
] = (MV_U8
) block
[t
][j
];
191 default: return AES_BAD_CIPHER_STATE
;
197 int aesBlockDecrypt128(MV_U8 mode
, MV_U8
*IV
, MV_U8
*expandedKey
, int keyLen
,
198 MV_U32
*srcData
, int numBlocks
, MV_U32
*dstData
)
201 MV_U8 block
[4][MAXBC
];
204 char *input
, *outBuffer
;
206 input
= (char*)srcData
;
207 outBuffer
= (char*)dstData
;
209 if (expandedKey
== NULL
)
211 return AES_BAD_KEY_MAT
;
214 /* check parameter consistency: */
215 if (keyLen
!= 128 && keyLen
!= 192 && keyLen
!= 256)
217 return AES_BAD_KEY_MAT
;
219 if ((mode
!= MODE_ECB
&& mode
!= MODE_CBC
))
221 return AES_BAD_CIPHER_STATE
;
226 case 128: rounds
= 10; break;
227 case 192: rounds
= 12; break;
228 case 256: rounds
= 14; break;
229 default : return (-3); /* this cannot happen */
236 for (i
= 0; i
< numBlocks
; i
++)
238 for (j
= 0; j
< 4; j
++)
240 for(t
= 0; t
< 4; t
++)
242 /* parse input stream into rectangular array */
243 block
[t
][j
] = input
[16*i
+4*j
+t
] & 0xFF;
246 rijndaelDecrypt128(block
, (MV_U8 (*)[4][MAXBC
])expandedKey
, rounds
);
247 for (j
= 0; j
< 4; j
++)
249 /* parse rectangular array into output ciphertext bytes */
250 for(t
= 0; t
< 4; t
++)
251 outBuffer
[16*i
+4*j
+t
] = (MV_U8
) block
[t
][j
];
258 for (j
= 0; j
< 4; j
++)
260 for(t
= 0; t
< 4; t
++)
262 /* parse input stream into rectangular array */
263 block
[t
][j
] = input
[4*j
+t
] & 0xFF;
264 iv
[t
][j
] = block
[t
][j
];
267 rijndaelDecrypt128(block
, (MV_U8 (*)[4][MAXBC
])expandedKey
, rounds
);
269 for (j
= 0; j
< 4; j
++)
271 /* exor the IV and parse rectangular array into output ciphertext bytes */
272 for(t
= 0; t
< 4; t
++)
274 outBuffer
[4*j
+t
] = (MV_U8
) (block
[t
][j
] ^ IV
[t
+4*j
]);
275 IV
[t
+4*j
] = iv
[t
][j
];
280 for (i
= 1; i
< numBlocks
; i
++)
282 for (j
= 0; j
< 4; j
++)
284 for(t
= 0; t
< 4; t
++)
286 /* parse input stream into rectangular array */
287 iv
[t
][j
] = input
[16*i
+4*j
+t
] & 0xFF;
288 block
[t
][j
] = iv
[t
][j
];
291 rijndaelDecrypt128(block
, (MV_U8 (*)[4][MAXBC
])expandedKey
, rounds
);
293 for (j
= 0; j
< 4; j
++)
295 /* exor previous ciphertext block and parse rectangular array
296 into output ciphertext bytes */
297 for(t
= 0; t
< 4; t
++)
299 outBuffer
[16*i
+4*j
+t
] = (MV_U8
) (block
[t
][j
] ^ IV
[t
+4*j
]);
300 IV
[t
+4*j
] = iv
[t
][j
];
306 default: return AES_BAD_CIPHER_STATE
;
This page took 0.065935 seconds and 5 git commands to generate.