source: trunk/src/rijndael-api-fst.c@ 590

Last change on this file since 590 was 590, checked in by katerina, 17 hours ago

Fix for ticket #478 (cppcheck warnings).

File size: 9.6 KB
Line 
1/* $NetBSD: rijndael-api-fst.c,v 1.24 2011/05/14 16:46:55 jmmv Exp $ */
2
3/**
4 * rijndael-api-fst.c
5 *
6 * @version 2.9 (December 2000)
7 *
8 * Optimised ANSI C code for the Rijndael cipher (now AES)
9 *
10 * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
11 * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
12 * @author Paulo Barreto <paulo.barreto@terra.com.br>
13 *
14 * This code is hereby placed in the public domain.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Acknowledgements:
29 *
30 * We are deeply indebted to the following people for their bug reports,
31 * fixes, and improvement suggestions to this implementation. Though we
32 * tried to list all contributions, we apologise in advance for any
33 * missing reference.
34 *
35 * Andrew Bales <Andrew.Bales@Honeywell.com>
36 * Markus Friedl <markus.friedl@informatik.uni-erlangen.de>
37 * John Skodon <skodonj@webquill.com>
38 */
39#include "config_xor.h"
40
41#include <stdlib.h>
42#include <string.h>
43
44
45#ifdef SH_ENCRYPT
46
47#include "rijndael-api-fst.h"
48
49static void xor16(u8 *d, const u8 *a, const u8* b)
50{
51 size_t i;
52
53 for (i = 0; i < 4; i++) {
54 *d++ = *a++ ^ *b++;
55 *d++ = *a++ ^ *b++;
56 *d++ = *a++ ^ *b++;
57 *d++ = *a++ ^ *b++;
58 }
59}
60
61int
62rijndael_makeKey(keyInstance *key, BYTE direction, int keyLen,
63 const char *keyMaterial)
64{
65 u8 cipherKey[RIJNDAEL_MAXKB];
66 int i;
67
68 if (key == NULL) {
69 return BAD_KEY_INSTANCE;
70 }
71
72 if ((direction == DIR_ENCRYPT) || (direction == DIR_DECRYPT)) {
73 key->direction = direction;
74 } else {
75 return BAD_KEY_DIR;
76 }
77
78 if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) {
79 key->keyLen = keyLen;
80 } else {
81 return BAD_KEY_MAT;
82 }
83
84 if (keyMaterial != NULL) {
85 char temp[RIJNDAEL_MAX_KEY_SIZE];
86 for (i = 0; i < key->keyLen/8; i++) {
87 int t, j;
88
89 t = *keyMaterial++;
90 if ((t >= '0') && (t <= '9')) j = (t - '0') << 4;
91 else if ((t >= 'a') && (t <= 'f')) j = (t - 'a' + 10) << 4;
92 else if ((t >= 'A') && (t <= 'F')) j = (t - 'A' + 10) << 4;
93 else return BAD_KEY_MAT;
94
95 t = *keyMaterial++;
96 if ((t >= '0') && (t <= '9')) j ^= (t - '0');
97 else if ((t >= 'a') && (t <= 'f')) j ^= (t - 'a' + 10);
98 else if ((t >= 'A') && (t <= 'F')) j ^= (t - 'A' + 10);
99 else return BAD_KEY_MAT;
100
101 temp[i] = (u8)j;
102 }
103
104 /* memcpy(key->keyMaterial, keyMaterial, keyLen/8); */
105 /* cppcheck-suppress uninitvar */
106 memcpy(key->keyMaterial, temp, keyLen/8);
107 }
108
109 /* initialize key schedule: */
110 memcpy(cipherKey, key->keyMaterial, keyLen/8);
111 if (direction == DIR_ENCRYPT) {
112 key->Nr = rijndaelKeySetupEnc(key->rk, cipherKey, keyLen);
113 } else {
114 key->Nr = rijndaelKeySetupDec(key->rk, cipherKey, keyLen);
115 }
116 rijndaelKeySetupEnc(key->ek, cipherKey, keyLen);
117 return TRUE;
118}
119
120int
121rijndael_cipherInit(cipherInstance *cipher, BYTE mode, const char *IV)
122{
123 if ((mode == MODE_ECB) || (mode == MODE_CBC) || (mode == MODE_CFB1)) {
124 cipher->mode = mode;
125 } else {
126 return BAD_CIPHER_MODE;
127 }
128 if (IV != NULL) {
129 memcpy(cipher->IV, IV, RIJNDAEL_MAX_IV_SIZE);
130 } else {
131 memset(cipher->IV, 0, RIJNDAEL_MAX_IV_SIZE);
132 }
133 return TRUE;
134}
135
136int
137rijndael_blockEncrypt(cipherInstance *cipher, keyInstance *key,
138 const BYTE *input, int inputLen, BYTE *outBuffer)
139{
140 int i, k, t, numBlocks;
141 u8 block[16], *iv;
142
143 if (cipher == NULL ||
144 key == NULL ||
145 key->direction == DIR_DECRYPT) {
146 return BAD_CIPHER_STATE;
147 }
148 if (input == NULL || inputLen <= 0) {
149 return 0; /* nothing to do */
150 }
151
152 numBlocks = inputLen/128;
153
154 switch (cipher->mode) {
155 case MODE_ECB:
156 for (i = numBlocks; i > 0; i--) {
157 rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
158 input += 16;
159 outBuffer += 16;
160 }
161 break;
162
163 case MODE_CBC:
164 iv = (u8 *)cipher->IV;
165 for (i = numBlocks; i > 0; i--) {
166 xor16(block, input, iv);
167 rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
168 iv = outBuffer;
169 input += 16;
170 outBuffer += 16;
171 }
172 break;
173
174 case MODE_CFB1:
175 iv = (u8 *)cipher->IV;
176 for (i = numBlocks; i > 0; i--) {
177 memcpy(outBuffer, input, 16);
178 for (k = 0; k < 128; k++) {
179 rijndaelEncrypt(key->ek, key->Nr, iv, block);
180 outBuffer[k >> 3] ^=
181 (block[0] & 0x80U) >> (k & 7);
182 for (t = 0; t < 15; t++) {
183 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
184 }
185 iv[15] = (iv[15] << 1) |
186 ((outBuffer[k >> 3] >> (7 - (k & 7))) & 1);
187 }
188 outBuffer += 16;
189 input += 16;
190 }
191 break;
192
193 default:
194 return BAD_CIPHER_STATE;
195 }
196
197 return 128 * numBlocks;
198}
199
200/**
201 * Encrypt data partitioned in octets, using RFC 2040-like padding.
202 *
203 * @param input data to be encrypted (octet sequence)
204 * @param inputOctets input length in octets (not bits)
205 * @param outBuffer encrypted output data
206 *
207 * @return length in octets (not bits) of the encrypted output buffer.
208 */
209int
210rijndael_padEncrypt(cipherInstance *cipher, keyInstance *key,
211 const BYTE *input, int inputOctets, BYTE *outBuffer)
212{
213 int i, numBlocks, padLen;
214 u8 block[16], *iv;
215
216 if (cipher == NULL ||
217 key == NULL ||
218 key->direction == DIR_DECRYPT) {
219 return BAD_CIPHER_STATE;
220 }
221 if (input == NULL || inputOctets <= 0) {
222 return 0; /* nothing to do */
223 }
224
225 numBlocks = inputOctets / 16;
226
227 switch (cipher->mode) {
228 case MODE_ECB:
229 for (i = numBlocks; i > 0; i--) {
230 rijndaelEncrypt(key->rk, key->Nr, input, outBuffer);
231 input += 16;
232 outBuffer += 16;
233 }
234 padLen = 16 - (inputOctets - 16*numBlocks);
235 memcpy(block, input, 16 - padLen);
236 memset(block + 16 - padLen, padLen, padLen);
237 rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
238 break;
239
240 case MODE_CBC:
241 iv = (u8 *)cipher->IV;
242 for (i = numBlocks; i > 0; i--) {
243 xor16(block, input, iv);
244 rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
245 iv = outBuffer;
246 input += 16;
247 outBuffer += 16;
248 }
249 padLen = 16 - (inputOctets - 16*numBlocks);
250 for (i = 0; i < 16 - padLen; i++) {
251 block[i] = input[i] ^ iv[i];
252 }
253 for (i = 16 - padLen; i < 16; i++) {
254 block[i] = (BYTE)padLen ^ iv[i];
255 }
256 rijndaelEncrypt(key->rk, key->Nr, block, outBuffer);
257 break;
258
259 default:
260 return BAD_CIPHER_STATE;
261 }
262
263 return 16 * (numBlocks + 1);
264}
265
266int
267rijndael_blockDecrypt(cipherInstance *cipher, keyInstance *key,
268 const BYTE *input, int inputLen, BYTE *outBuffer)
269{
270 int i, k, t, numBlocks;
271 u8 block[16], *iv;
272
273 if (cipher == NULL ||
274 key == NULL ||
275 (cipher->mode != MODE_CFB1 && key->direction == DIR_ENCRYPT)) {
276 return BAD_CIPHER_STATE;
277 }
278 if (input == NULL || inputLen <= 0) {
279 return 0; /* nothing to do */
280 }
281
282 numBlocks = inputLen/128;
283
284 switch (cipher->mode) {
285 case MODE_ECB:
286 for (i = numBlocks; i > 0; i--) {
287 rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
288 input += 16;
289 outBuffer += 16;
290 }
291 break;
292
293 case MODE_CBC:
294 iv = (u8 *)cipher->IV;
295 for (i = numBlocks; i > 0; i--) {
296 rijndaelDecrypt(key->rk, key->Nr, input, block);
297 xor16(block, block, iv);
298 if (numBlocks > 1)
299 memcpy(cipher->IV, input, 16);
300 memcpy(outBuffer, block, 16);
301 input += 16;
302 outBuffer += 16;
303 }
304 break;
305
306 case MODE_CFB1:
307 iv = (u8 *)cipher->IV;
308 for (i = numBlocks; i > 0; i--) {
309 memcpy(outBuffer, input, 16);
310 for (k = 0; k < 128; k++) {
311 rijndaelEncrypt(key->ek, key->Nr, iv, block);
312 for (t = 0; t < 15; t++) {
313 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
314 }
315 iv[15] = (iv[15] << 1) |
316 ((input[k >> 3] >> (7 - (k & 7))) & 1);
317 outBuffer[k >> 3] ^= (block[0] & 0x80U) >>
318 (k & 7);
319 }
320 outBuffer += 16;
321 input += 16;
322 }
323 break;
324
325 default:
326 return BAD_CIPHER_STATE;
327 }
328
329 return 128 * numBlocks;
330}
331
332int
333rijndael_padDecrypt(cipherInstance *cipher, keyInstance *key,
334 const BYTE *input, int inputOctets, BYTE *outBuffer)
335{
336 int i, numBlocks, padLen;
337 u8 block[16], *iv;
338
339 if (cipher == NULL ||
340 key == NULL ||
341 key->direction == DIR_ENCRYPT) {
342 return BAD_CIPHER_STATE;
343 }
344 if (input == NULL || inputOctets <= 0) {
345 return 0; /* nothing to do */
346 }
347 if (inputOctets % 16 != 0) {
348 return BAD_DATA;
349 }
350
351 numBlocks = inputOctets/16;
352
353 switch (cipher->mode) {
354 case MODE_ECB:
355 /* all blocks but last */
356 for (i = numBlocks - 1; i > 0; i--) {
357 rijndaelDecrypt(key->rk, key->Nr, input, outBuffer);
358 input += 16;
359 outBuffer += 16;
360 }
361 /* last block */
362 rijndaelDecrypt(key->rk, key->Nr, input, block);
363 padLen = block[15];
364 if (padLen >= 16) {
365 return BAD_DATA;
366 }
367 for (i = 16 - padLen; i < 16; i++) {
368 if (block[i] != padLen) {
369 return BAD_DATA;
370 }
371 }
372 memcpy(outBuffer, block, 16 - padLen);
373 break;
374
375 case MODE_CBC:
376 iv = (u8 *)cipher->IV;
377 /* all blocks but last */
378 for (i = numBlocks - 1; i > 0; i--) {
379 rijndaelDecrypt(key->rk, key->Nr, input, block);
380 xor16(block, block, iv);
381 memcpy(cipher->IV, input, 16);
382 memcpy(outBuffer, block, 16);
383 input += 16;
384 outBuffer += 16;
385 }
386 /* last block */
387 rijndaelDecrypt(key->rk, key->Nr, input, block);
388 xor16(block, block, iv);
389 padLen = block[15];
390 if (padLen <= 0 || padLen > 16) {
391 return BAD_DATA;
392 }
393 for (i = 16 - padLen; i < 16; i++) {
394 if (block[i] != padLen) {
395 return BAD_DATA;
396 }
397 }
398 memcpy(outBuffer, block, 16 - padLen);
399 break;
400
401 default:
402 return BAD_CIPHER_STATE;
403 }
404
405 return 16 * numBlocks - padLen;
406}
407
408#endif
Note: See TracBrowser for help on using the repository browser.