]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-present-test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / main-present-test.c
1 /* main-present-test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20  * present test-suit
21  * 
22 */
23
24 #include "config.h"
25 #include "serial-tools.h"
26 #include "uart.h"
27 #include "debug.h"
28
29 #include "present.h"
30 #include "nessie_bc_test.h"
31 #include "cli.h"
32 #include "performance_test.h"
33
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37
38 char* cipher_name = "Present";
39
40 /*****************************************************************************
41  *  additional validation-functions                                                                                      *
42  *****************************************************************************/
43 void present_genctx_dummy(uint8_t* key, uint16_t keysize_b, present_ctx_t* ctx){
44         present_init(key, keysize_b, ctx);
45 }
46
47 void testrun_nessie_present(void){
48         nessie_bc_ctx.blocksize_B =   8;
49         nessie_bc_ctx.keysize_b   =  80;
50         nessie_bc_ctx.name        = cipher_name;
51         nessie_bc_ctx.ctx_size_B  = sizeof(present_ctx_t);
52         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)present_enc;
53         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)present_dec;
54         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)present_genctx_dummy;
55         
56         nessie_bc_run();        
57 }
58
59 void testrun_selfenc(uint8_t* key, uint8_t* buffer){
60         present_ctx_t ctx;
61         uart_putstr_P(PSTR("\r\nkey   : "));
62         uart_hexdump(key, 10);
63         uart_putstr_P(PSTR("\r\nplain : "));
64         uart_hexdump(buffer, 8);
65         present_init(key, 80, &ctx);
66         present_enc(buffer, &ctx);
67         uart_putstr_P(PSTR("\r\ncipher: "));
68         uart_hexdump(buffer, 8);
69         present_dec(buffer, &ctx);
70         uart_putstr_P(PSTR("\r\nplain : "));
71         uart_hexdump(buffer, 8);
72         uart_putstr_P(PSTR("\r\n"));
73 }
74
75 void testrun_self_present(void){
76         uint8_t buffer[8], key[10];
77         uart_putstr_P(PSTR("\r\n\r\n=== Testvectors from the paper ===\r\n"));
78         
79         memset(buffer, 0, 8);
80         memset(key, 0, 10);
81         testrun_selfenc(key, buffer);
82         
83         memset(buffer, 0, 8);
84         memset(key, 0xFF, 10);
85         testrun_selfenc(key, buffer);
86         
87         memset(buffer, 0xFF, 8);
88         memset(key, 0, 10);
89         testrun_selfenc(key, buffer);
90         
91         memset(buffer, 0xFF, 8);
92         memset(key, 0xFF, 10);
93         testrun_selfenc(key, buffer);
94         
95 }
96
97 void testrun_performance_present(void){
98         uint64_t t;
99         uint8_t key[10], data[8];
100         present_ctx_t ctx;
101         
102         calibrateTimer();
103         print_overhead();
104         
105         memset(key,  0, 10);
106         memset(data, 0,  8);
107         
108         startTimer(1);
109         present_init(key, 80, &ctx);
110         t = stopTimer();
111         print_time_P(PSTR("\tctx-gen time: "),t);
112         
113         startTimer(1);
114         present_enc(data, &ctx);
115         t = stopTimer();
116         print_time_P(PSTR("\tencrypt time: "), t);
117         
118         startTimer(1);
119         present_dec(data, &ctx);
120         t = stopTimer();
121         print_time_P(PSTR("\tdecrypt time: "), t);
122         
123         uart_putstr_P(PSTR("\r\n"));
124 }
125
126 /*****************************************************************************
127  *  main                                                                                                                                         *
128  *****************************************************************************/
129
130 int main (void){
131         char  str[20];
132         DEBUG_INIT();
133         uart_putstr("\r\n");
134
135         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
136         uart_putstr(cipher_name);
137         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
138
139         PGM_P    u   = PSTR("nessie\0test\0performance\0");
140         void_fpt v[] = {testrun_nessie_present, testrun_self_present, testrun_performance_present};
141
142         while(1){ 
143                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
144                 if(execcommand_d0_P(str, u, v)<0){
145                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
146                 }
147                 continue;
148         error:
149                 uart_putstr("ERROR\r\n");
150         }
151         
152 }