aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/files/crypto/ocf/kirkwood/cesa/AES/mvAesApi.c
blob: b432dc6e66ed548a752f6b4e899c9c0f25dc4783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* rijndael-api-ref.c   v2.1   April 2000
 * Reference ANSI C code
 * authors: v2.0 Paulo Barreto
 *               Vincent Rijmen, K.U.Leuven
 *          v2.1 Vincent Rijmen, K.U.Leuven
 *
 * This code is placed in the public domain.
 */
#include "mvOs.h"

#include "mvAes.h"
#include "mvAesAlg.h"


/*  Defines:
	Add any additional defines you need
*/

#define     MODE_ECB        1    /*  Are we ciphering in ECB mode?   */
#define     MODE_CBC        2    /*  Are we ciphering in CBC mode?   */
#define     MODE_CFB1       3    /*  Are we ciphering in 1-bit CFB mode? */


int     aesMakeKey(MV_U8 *expandedKey, MV_U8 *keyMaterial, int keyLen, int blockLen)
{
    MV_U8   W[MAXROUNDS+1][4][MAXBC];
	MV_U8   k[4][MAXKC];
    MV_U8   j;
	int     i, rounds, KC;
	
	if (expandedKey == NULL) 
    {
		return AES_BAD_KEY_INSTANCE;
	}

	if (!((keyLen == 128) || (keyLen == 192) || (keyLen == 256))) 
    { 
		return AES_BAD_KEY_MAT;
	}

	if (keyMaterial == NULL) 
    {
		return AES_BAD_KEY_MAT;
	}

	/* initialize key schedule: */ 
 	for(i=0; i<keyLen/8; i++) 
    {
		j = keyMaterial[i];
		k[i % 4][i / 4] = j; 
	}	
	
	rijndaelKeySched (k, keyLen, blockLen, W);
#ifdef MV_AES_DEBUG
    {
        MV_U8*  pW = &W[0][0][0];
        int     x;

        mvOsPrintf("Expended Key: size = %d\n", sizeof(W));
        for(i=0; i<sizeof(W); i++)
        {
            mvOsPrintf("%02x ", pW[i]);
        }
        for(i=0; i<MAXROUNDS+1; i++)
        {
            mvOsPrintf("\n Round #%02d: ", i);
            for(x=0; x<MAXBC; x++)
            {
                mvOsPrintf("%02x%02x%02x%02x ", 
                    W[i][0][x], W[i][1][x], W[i][2][x], W[i][3][x]);
            }
            mvOsPrintf("\n");
        }
    }
#endif /* MV_AES_DEBUG */
  	switch (keyLen) 
    {
	    case 128: 
            rounds = 10;
            KC = 4; 
            break;
	    case 192: 
            rounds = 12;
            KC = 6; 
            break;
	    case 256: 
            rounds = 14;
            KC = 8; 
            break;
	    default : 
            return (-1);
	}

    for(i=0; i<MAXBC; i++)
    {
        for(j=0; j<4; j++)
        {
            expandedKey[i*4+j] = W[rounds][j][i];
        }
    }
    for(; i<KC; i++)
    {
        for(j=0; j<4; j++)
        {
            expandedKey[i*4+j] = W[rounds-1][j][i+MAXBC-KC];
        }
    }

	
	return 0;
}

int     aesBlockEncrypt128(MV_U8 mode, MV_U8 *IV, MV_U8 *expandedKey, int  keyLen, 
                        MV_U32 *plain, int numBlocks, MV_U32 *cipher)
{
	int     i, j, t;
	MV_U8   block[4][MAXBC];
    int     rounds;
    char    *input, *outBuffer;

    input = (char*)plain;
    outBuffer = (char*)cipher;

        /* check parameter consistency: */
    if( (expandedKey == NULL) || ((keyLen != 128) && (keyLen != 192) && (keyLen != 256))) 
    {
        return AES_BAD_KEY_MAT;
    }
    if ((mode != MODE_ECB && mode != MODE_CBC))
    {
        return AES_BAD_CIPHER_STATE;
    }

	switch (keyLen) 
    {
	    case 128: rounds = 10; break;
	    case 192: rounds = 12; break;
	    case 256: rounds = 14; break;
	    default : return (-3); /* this cannot happen */
	}

	
	switch (mode) 
    {
	    case MODE_ECB: 
		    for (i = 0; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
				        /* parse input stream into rectangular array */
					    block[t][j] = input[16*i+4*j+t] & 0xFF;
			    }                           
			    rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			    for (j = 0; j < 4; j++) 
                {           
				    /* parse rectangular array into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
                        outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];

			    }
		    }
		    break;
		
	    case MODE_CBC:
		    for (j = 0; j < 4; j++) 
            {
			    for(t = 0; t < 4; t++)
			    /* parse initial value into rectangular array */
					block[t][j] = IV[t+4*j] & 0xFF;
			}
		    for (i = 0; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
				        /* parse input stream into rectangular array and exor with 
				        IV or the previous ciphertext */
					    block[t][j] ^= input[16*i+4*j+t] & 0xFF;
			    }
			    rijndaelEncrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			    for (j = 0; j < 4; j++) 
                {
				    /* parse rectangular array into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
					    outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
			    }
		    }
		    break;
	
	    default: return AES_BAD_CIPHER_STATE;
	}
	
	return 0;
}

int     aesBlockDecrypt128(MV_U8 mode, MV_U8 *IV, MV_U8 *expandedKey, int  keyLen, 
                            MV_U32 *srcData, int numBlocks, MV_U32 *dstData)
{
	int     i, j, t;
	MV_U8   block[4][MAXBC];
    MV_U8   iv[4][MAXBC];    
    int     rounds;
    char    *input, *outBuffer;

    input = (char*)srcData;
    outBuffer = (char*)dstData;

    if (expandedKey == NULL) 
    {
		return AES_BAD_KEY_MAT;
	}

    /* check parameter consistency: */
    if (keyLen != 128 && keyLen != 192 && keyLen != 256)
    {
        return AES_BAD_KEY_MAT;
    }
    if ((mode != MODE_ECB && mode != MODE_CBC)) 
    {
        return AES_BAD_CIPHER_STATE;
    }

	switch (keyLen) 
    {
	    case 128: rounds = 10; break;
	    case 192: rounds = 12; break;
	    case 256: rounds = 14; break;
	    default : return (-3); /* this cannot happen */
	}

	
	switch (mode) 
    {
	    case MODE_ECB: 
		    for (i = 0; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
                    {
				        /* parse input stream into rectangular array */
					    block[t][j] = input[16*i+4*j+t] & 0xFF;
                    }
			    }
			    rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			    for (j = 0; j < 4; j++) 
                {
				    /* parse rectangular array into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
					    outBuffer[16*i+4*j+t] = (MV_U8) block[t][j];
			    }
		    }
		    break;
		
	    case MODE_CBC:
		    /* first block */
		    for (j = 0; j < 4; j++) 
            {
			    for(t = 0; t < 4; t++)
                {
			        /* parse input stream into rectangular array */
				    block[t][j] = input[4*j+t] & 0xFF;
                    iv[t][j] = block[t][j];
                }
		    }
		    rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
		
		    for (j = 0; j < 4; j++) 
            {
			    /* exor the IV and parse rectangular array into output ciphertext bytes */
			    for(t = 0; t < 4; t++)
                {
				    outBuffer[4*j+t] = (MV_U8) (block[t][j] ^ IV[t+4*j]);
                    IV[t+4*j] = iv[t][j];
                }
		    }
		
		    /* next blocks */
		    for (i = 1; i < numBlocks; i++) 
            {
			    for (j = 0; j < 4; j++) 
                {
				    for(t = 0; t < 4; t++)
                    {
				        /* parse input stream into rectangular array */
                        iv[t][j] = input[16*i+4*j+t] & 0xFF;
					    block[t][j] = iv[t][j];
                    }
			    }
			    rijndaelDecrypt128(block, (MV_U8 (*)[4][MAXBC])expandedKey, rounds);
			
			    for (j = 0; j < 4; j++) 
                {
				    /* exor previous ciphertext block and parse rectangular array 
				       into output ciphertext bytes */
				    for(t = 0; t < 4; t++)
                    {
					    outBuffer[16*i+4*j+t] = (MV_U8) (block[t][j] ^ IV[t+4*j]);
                        IV[t+4*j] = iv[t][j];
                    }
			    }
		    }
		    break;
	
	    default: return AES_BAD_CIPHER_STATE;
	}
	
	return 0;
}