OpenCores
URL https://opencores.org/ocsvn/apbtoaes128/apbtoaes128/trunk

Subversion Repositories apbtoaes128

[/] [apbtoaes128/] [trunk/] [doc/] [openssl_software_proof.cpp] - Blame information for rev 16

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 redbear
//////////////////////////////////////////////////////////////////
2
////
3
////
4
////    AES CORE BLOCK
5
////
6
////
7
////
8
//// This file is part of the APB to AES128 project
9
////
10
//// http://www.opencores.org/cores/apbtoaes128/
11
////
12
////
13
////
14
//// Description
15
////
16
//// Implementation of APB IP core according to
17
////
18
//// aes128_spec IP core specification document.
19
////
20
////
21
////
22
//// To Do: Things are right here but always all block can suffer changes
23
////
24
////
25
////
26
////
27
////
28
//// Author(s): - Felipe Fernandes Da Costa, fefe2560@gmail.com
29
////
30
///////////////////////////////////////////////////////////////// 
31
////
32
////
33
//// Copyright (C) 2009 Authors and OPENCORES.ORG
34
////
35
////
36
////
37
//// This source file may be used and distributed without
38
////
39
//// restriction provided that this copyright statement is not
40
////
41
//// removed from the file and that any derivative work contains
42
//// the original copyright notice and the associated disclaimer.
43
////
44
////
45
//// This source file is free software; you can redistribute it
46
////
47
//// and/or modify it under the terms of the GNU Lesser General
48
////
49
//// Public License as published by the Free Software Foundation;
50
//// either version 2.1 of the License, or (at your option) any
51
////
52
//// later version.
53
////
54
////
55
////
56
//// This source is distributed in the hope that it will be
57
////
58
//// useful, but WITHOUT ANY WARRANTY; without even the implied
59
////
60
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
61
////
62
//// PURPOSE. See the GNU Lesser General Public License for more
63
//// details.
64
////
65
////
66
////
67
//// You should have received a copy of the GNU Lesser General
68
////
69
//// Public License along with this source; if not, download it
70
////
71
//// from http://www.opencores.org/lgpl.shtml
72
////
73
////
74
///////////////////////////////////////////////////////////////////
75
#include <iostream>
76
#include <openssl/aes.h>  
77
#include <stdio.h>
78
#include <string.h>
79
 
80
using namespace std;
81
 
82
 
83
//g++ teste.cpp -o teste -lm -m64  -lcrypto
84
 
85
 
86
 
87
unsigned char TEXT_FIPS_NOT_DERIVATED[]     = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
88
unsigned char KEY_FIPS_NOT_DERIVATED[]      = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
89
 
90
unsigned char TEXT_FIPS_DERIVATED[]         = {0x69,0xC4,0xE0,0xD8,0x6A,0x7B,0x04,0x30,0xD8,0xCD,0xB7,0x80,0x70,0xB4,0xC5,0x5A};
91
unsigned char KEY_FIPS_DERIVATED[]          = {0x13,0x11,0x1D,0x7F,0xE3,0x94,0x4A,0x17,0xF3,0x07,0xA7,0x8B,0x4D,0x2B,0x30,0xC5};
92
 
93
 
94
unsigned char KEY_FIPS_CBC_NOT_DERIVATED[]  = {0x2B,0x7E,0x15,0x16,0x28,0xAE,0xD2,0xA6,0xAB,0xF7,0x15,0x88,0x09,0xCF,0x4F,0x3C};
95
 
96
unsigned char IV_FIPS_CBC_NOT_DERIVATED[]   = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
97
unsigned char IV_FIPS_CBC_NOT_DERIVATEDD[]   = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
98
 
99
unsigned char TEXT_FIPS_CBC_NOT_DERIVATED[] = {0x6B,0xC1,0xBE,0xE2,0x2E,0x40,0x9F,0x96,0xE9,0x3D,0x7E,0x11,0x73,0x93,0x17,0x2A};
100
 
101
unsigned char TEXT_CBC_FIPS_DERIVATED[]     = {0x76,0x49,0xAB,0xAC,0x81,0x19,0xB2,0x46,0xCE,0xE9,0x8E,0x9B,0x12,0xE9,0x19,0x7D};
102
 
103
 
104
unsigned char KEY_FIPS_CTR_NOT_DERIVATED[]  = {0x2B,0x7E,0x15,0x16,0x28,0xAE,0xD2,0xA6,0xAB,0xF7,0x15,0x88,0x09,0xCF,0x4F,0x3C};
105
unsigned char IV_FIPS_CTR_NOT_DERIVATED[]   = {0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF};
106
unsigned char TEXT_FIPS_CTR_NOT_DERIVATED[] = {0x6B,0xC1,0xBE,0xE2,0x2E,0x40,0x9F,0x96,0xE9,0x3D,0x7E,0x11,0x73,0x93,0x17,0x2A};
107
 
108
unsigned char TEXT_CTR_FIPS_DERIVATED[]     = {0x87,0x4D,0x61,0x91,0xB6,0x20,0xE3,0x26,0x1B,0xEF,0x68,0x64,0x99,0x0D,0xB6,0xCE};
109
 
110
unsigned char TEXT_NULL[]                   = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
111
 
112
unsigned char CBLOCK[16];
113
unsigned char BLOCK[16];
114
 
115
 
116
struct ctr_state {
117
    unsigned char ivec[16];
118
    unsigned int num;
119
    unsigned char ecount[16];
120
};
121
 
122
struct ctr_state state;
123
 
124
 
125
 
126
int main()
127
{
128
 
129
        unsigned long int result[4];
130
        unsigned char result_to_compare[16];
131
        unsigned char *ptr;
132
 
133
        int i,j,n;
134
 
135
        for(i=0;i<=16;i++)
136
        {
137
                CBLOCK[i]='0';
138
        }
139
 
140
        AES_KEY wctx;
141
 
142
        printf(" ECB SAMPLE \n");
143
 
144
        AES_set_encrypt_key(KEY_FIPS_NOT_DERIVATED, 128, &wctx);
145
        AES_encrypt(TEXT_FIPS_NOT_DERIVATED, CBLOCK, &wctx);
146
 
147
 
148
        result[0]=wctx.rd_key[40];
149
        result[1]=wctx.rd_key[41];
150
        result[2]=wctx.rd_key[42];
151
        result[3]=wctx.rd_key[43];
152
 
153
 
154
        for(i=0,n=0;i<4;i++)
155
        {
156
                ptr = (unsigned char *)&result[i];
157
 
158
                for(j=0;j<4;j++)
159
                {
160
                        result_to_compare[j+n] = ptr[j];
161
                }
162
                n=n+4;
163
 
164
        }
165
 
166
 
167
        printf("TEXT ECB ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
168
                                                                            CBLOCK[1],
169
                                                                            CBLOCK[2],
170
                                                                            CBLOCK[3],
171
                                                                            CBLOCK[4],
172
                                                                            CBLOCK[5],
173
                                                                            CBLOCK[6],
174
                                                                            CBLOCK[7],
175
                                                                            CBLOCK[8],
176
                                                                            CBLOCK[9],
177
                                                                            CBLOCK[10],
178
                                                                            CBLOCK[11],
179
                                                                            CBLOCK[12],
180
                                                                            CBLOCK[13],
181
                                                                            CBLOCK[14],
182
                                                                            CBLOCK[15]);
183
 
184
        printf("KEY  ECB ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n", result_to_compare[0],
185
                                                                        result_to_compare[1],
186
                                                                        result_to_compare[2],
187
                                                                        result_to_compare[3],
188
                                                                        result_to_compare[4],
189
                                                                        result_to_compare[5],
190
                                                                        result_to_compare[6],
191
                                                                        result_to_compare[7],
192
                                                                        result_to_compare[8],
193
                                                                        result_to_compare[9],
194
                                                                        result_to_compare[10],
195
                                                                        result_to_compare[11],
196
                                                                        result_to_compare[12],
197
                                                                        result_to_compare[13],
198
                                                                        result_to_compare[14],
199
                                                                        result_to_compare[15]);
200
 
201
 
202
        AES_set_decrypt_key(KEY_FIPS_NOT_DERIVATED, 128, &wctx);
203
 
204
 
205
        result[0]=wctx.rd_key[4];
206
        result[1]=wctx.rd_key[5];
207
        result[2]=wctx.rd_key[6];
208
        result[3]=wctx.rd_key[7];
209
 
210
 
211
        //AES_set_encrypt_key(result_to_compare, 128, &wctx);   
212
        AES_decrypt(TEXT_FIPS_DERIVATED, CBLOCK, &wctx);
213
 
214
        printf("TEXT ECB DECRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
215
                                                                        CBLOCK[1],
216
                                                                        CBLOCK[2],
217
                                                                        CBLOCK[3],
218
                                                                        CBLOCK[4],
219
                                                                        CBLOCK[5],
220
                                                                        CBLOCK[6],
221
                                                                        CBLOCK[7],
222
                                                                        CBLOCK[8],
223
                                                                        CBLOCK[9],
224
                                                                        CBLOCK[10],
225
                                                                        CBLOCK[11],
226
                                                                        CBLOCK[12],
227
                                                                        CBLOCK[13],
228
                                                                        CBLOCK[14],
229
                                                                        CBLOCK[15]);
230
 
231
        printf(" CBC SAMPLE \n");
232
 
233
        //CBC
234
        for(i=0;i<=16;i++)
235
        {
236
                CBLOCK[i]='0';
237
        }
238
 
239
        AES_set_encrypt_key(KEY_FIPS_CBC_NOT_DERIVATED, 128, &wctx);
240
        AES_cbc_encrypt(TEXT_FIPS_CBC_NOT_DERIVATED, CBLOCK, 16, &wctx ,IV_FIPS_CBC_NOT_DERIVATED, AES_ENCRYPT);
241
 
242
        result[0]=wctx.rd_key[40];
243
        result[1]=wctx.rd_key[41];
244
        result[2]=wctx.rd_key[42];
245
        result[3]=wctx.rd_key[43];
246
 
247
 
248
        for(i=0,n=0;i<4;i++)
249
        {
250
                ptr = (unsigned char *)&result[i];
251
 
252
                for(j=0;j<4;j++)
253
                {
254
                        result_to_compare[j+n] = ptr[j];
255
                }
256
                n=n+4;
257
 
258
        }
259
 
260
 
261
        printf("TEXT CBC ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
262
                                                                            CBLOCK[1],
263
                                                                            CBLOCK[2],
264
                                                                            CBLOCK[3],
265
                                                                            CBLOCK[4],
266
                                                                            CBLOCK[5],
267
                                                                            CBLOCK[6],
268
                                                                            CBLOCK[7],
269
                                                                            CBLOCK[8],
270
                                                                            CBLOCK[9],
271
                                                                            CBLOCK[10],
272
                                                                            CBLOCK[11],
273
                                                                            CBLOCK[12],
274
                                                                            CBLOCK[13],
275
                                                                            CBLOCK[14],
276
                                                                            CBLOCK[15]);
277
 
278
        printf("KEY  CBC ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n", result_to_compare[0],
279
                                                                        result_to_compare[1],
280
                                                                        result_to_compare[2],
281
                                                                        result_to_compare[3],
282
                                                                        result_to_compare[4],
283
                                                                        result_to_compare[5],
284
                                                                        result_to_compare[6],
285
                                                                        result_to_compare[7],
286
                                                                        result_to_compare[8],
287
                                                                        result_to_compare[9],
288
                                                                        result_to_compare[10],
289
                                                                        result_to_compare[11],
290
                                                                        result_to_compare[12],
291
                                                                        result_to_compare[13],
292
                                                                        result_to_compare[14],
293
                                                                        result_to_compare[15]);
294
 
295
 
296
        AES_set_decrypt_key(KEY_FIPS_CBC_NOT_DERIVATED, 128, &wctx);
297
        for(i=0;i<=16;i++)
298
        {
299
                CBLOCK[i]='0';
300
        }
301
        //AES_decrypt(TEXT_CBC_FIPS_DERIVATED, CBLOCK, &wctx);
302
        AES_cbc_encrypt(TEXT_CBC_FIPS_DERIVATED, CBLOCK, 16, &wctx ,IV_FIPS_CBC_NOT_DERIVATEDD, AES_DECRYPT);
303
 
304
 
305
 
306
 
307
        printf("TEXT CBC DECRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
308
                                                                            CBLOCK[1],
309
                                                                            CBLOCK[2],
310
                                                                            CBLOCK[3],
311
                                                                            CBLOCK[4],
312
                                                                            CBLOCK[5],
313
                                                                            CBLOCK[6],
314
                                                                            CBLOCK[7],
315
                                                                            CBLOCK[8],
316
                                                                            CBLOCK[9],
317
                                                                            CBLOCK[10],
318
                                                                            CBLOCK[11],
319
                                                                            CBLOCK[12],
320
                                                                            CBLOCK[13],
321
                                                                            CBLOCK[14],
322
                                                                            CBLOCK[15]);
323
 
324
        printf(" CTR SAMPLE \n");
325
 
326
        //CTR
327
        memset(CBLOCK , 0, 16);
328
        state.num=0;
329
        memset(state.ecount , 0, 16);
330
        memset(state.ivec , 0, 16);
331
        memcpy(state.ivec, IV_FIPS_CTR_NOT_DERIVATED, 16);
332
 
333
        AES_set_encrypt_key(KEY_FIPS_CTR_NOT_DERIVATED, 128, &wctx);
334
        AES_ctr128_encrypt(TEXT_FIPS_CTR_NOT_DERIVATED, CBLOCK , 16 ,  &wctx,  state.ivec , state.ecount , &state.num);
335
 
336
 
337
        result[0]=wctx.rd_key[40];
338
        result[1]=wctx.rd_key[41];
339
        result[2]=wctx.rd_key[42];
340
        result[3]=wctx.rd_key[43];
341
 
342
 
343
        for(i=0,n=0;i<4;i++)
344
        {
345
                ptr = (unsigned char *)&result[i];
346
 
347
                for(j=0;j<4;j++)
348
                {
349
                        result_to_compare[j+n] = ptr[j];
350
                }
351
                n=n+4;
352
 
353
        }
354
 
355
 
356
 
357
 
358
        printf("TEXT CTR ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
359
                                                                            CBLOCK[1],
360
                                                                            CBLOCK[2],
361
                                                                            CBLOCK[3],
362
                                                                            CBLOCK[4],
363
                                                                            CBLOCK[5],
364
                                                                            CBLOCK[6],
365
                                                                            CBLOCK[7],
366
                                                                            CBLOCK[8],
367
                                                                            CBLOCK[9],
368
                                                                            CBLOCK[10],
369
                                                                            CBLOCK[11],
370
                                                                            CBLOCK[12],
371
                                                                            CBLOCK[13],
372
                                                                            CBLOCK[14],
373
                                                                            CBLOCK[15]);
374
 
375
        printf("KEY  CTR ENCRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n", result_to_compare[0],
376
                                                                        result_to_compare[1],
377
                                                                        result_to_compare[2],
378
                                                                        result_to_compare[3],
379
                                                                        result_to_compare[4],
380
                                                                        result_to_compare[5],
381
                                                                        result_to_compare[6],
382
                                                                        result_to_compare[7],
383
                                                                        result_to_compare[8],
384
                                                                        result_to_compare[9],
385
                                                                        result_to_compare[10],
386
                                                                        result_to_compare[11],
387
                                                                        result_to_compare[12],
388
                                                                        result_to_compare[13],
389
                                                                        result_to_compare[14],
390
                                                                        result_to_compare[15]);
391
 
392
        memset(CBLOCK , 0, 16);
393
        state.num=0;
394
        memset(state.ecount , 0, 16);
395
        memset(state.ivec , 0, 16);
396
        memcpy(state.ivec, IV_FIPS_CTR_NOT_DERIVATED, 16);
397
 
398
 
399
        AES_set_encrypt_key(KEY_FIPS_CTR_NOT_DERIVATED, 128, &wctx);
400
        AES_ctr128_encrypt(TEXT_CTR_FIPS_DERIVATED, CBLOCK , 16 ,  &wctx,  state.ivec , state.ecount , &state.num);
401
 
402
        printf("TEXT CTR DECRYPTED: %x%x%x%x %x%x%x%x %x%x%x%x %x%x%x%x \n",CBLOCK[0],
403
                                                                            CBLOCK[1],
404
                                                                            CBLOCK[2],
405
                                                                            CBLOCK[3],
406
                                                                            CBLOCK[4],
407
                                                                            CBLOCK[5],
408
                                                                            CBLOCK[6],
409
                                                                            CBLOCK[7],
410
                                                                            CBLOCK[8],
411
                                                                            CBLOCK[9],
412
                                                                            CBLOCK[10],
413
                                                                            CBLOCK[11],
414
                                                                            CBLOCK[12],
415
                                                                            CBLOCK[13],
416
                                                                            CBLOCK[14],
417
                                                                            CBLOCK[15]);
418
 
419
 return 0;
420
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.