]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-present-test.c
a40d00b2af6d953d604a675adfa68156e0fa0fa3
[avr-crypto-lib.git] / main-present-test.c
1 /*
2  * present test-suit
3  * 
4 */
5
6 #include "config.h"
7 #include "serial-tools.h"
8 #include "uart.h"
9 #include "debug.h"
10
11 #include "present.h"
12 #include "nessie_bc_test.h"
13 #include "cli.h"
14 #include "performance_test.h"
15
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <string.h>
19
20 char* cipher_name = "Present";
21
22 /*****************************************************************************
23  *  additional validation-functions                                                                                      *
24  *****************************************************************************/
25 void present_genctx_dummy(uint8_t* key, uint16_t keysize_b, present_ctx_t* ctx){
26         present_init(key, keysize_b, ctx);
27 }
28
29 void testrun_nessie_present(void){
30         nessie_bc_ctx.blocksize_B =   8;
31         nessie_bc_ctx.keysize_b   =  80;
32         nessie_bc_ctx.name        = cipher_name;
33         nessie_bc_ctx.ctx_size_B  = sizeof(present_ctx_t);
34         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)present_enc;
35         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)present_dec;
36         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)present_genctx_dummy;
37         
38         nessie_bc_run();        
39 }
40
41 void testrun_selfenc(uint8_t* key, uint8_t* buffer){
42         present_ctx_t ctx;
43         uart_putstr_P(PSTR("\r\nkey   : "));
44         uart_hexdump(key, 10);
45         uart_putstr_P(PSTR("\r\nplain : "));
46         uart_hexdump(buffer, 8);
47         present_init(key, 80, &ctx);
48         present_enc(buffer, &ctx);
49         uart_putstr_P(PSTR("\r\ncipher: "));
50         uart_hexdump(buffer, 8);
51         present_dec(buffer, &ctx);
52         uart_putstr_P(PSTR("\r\nplain : "));
53         uart_hexdump(buffer, 8);
54         uart_putstr_P(PSTR("\r\n"));
55 }
56
57 void testrun_self_present(void){
58         uint8_t buffer[8], key[10];
59         uart_putstr_P(PSTR("\r\n\r\n=== Testvectors from the paper ===\r\n"));
60         
61         memset(buffer, 0, 8);
62         memset(key, 0, 10);
63         testrun_selfenc(key, buffer);
64         
65         memset(buffer, 0, 8);
66         memset(key, 0xFF, 10);
67         testrun_selfenc(key, buffer);
68         
69         memset(buffer, 0xFF, 8);
70         memset(key, 0, 10);
71         testrun_selfenc(key, buffer);
72         
73         memset(buffer, 0xFF, 8);
74         memset(key, 0xFF, 10);
75         testrun_selfenc(key, buffer);
76         
77 }
78
79 void testrun_performance_present(void){
80         uint64_t t;
81         uint8_t key[10], data[8];
82         present_ctx_t ctx;
83         
84         calibrateTimer();
85         print_overhead();
86         
87         memset(key,  0, 10);
88         memset(data, 0,  8);
89         
90         startTimer(1);
91         present_init(key, 80, &ctx);
92         t = stopTimer();
93         print_time_P(PSTR("\tctx-gen time: "),t);
94         
95         startTimer(1);
96         present_enc(data, &ctx);
97         t = stopTimer();
98         print_time_P(PSTR("\tencrypt time: "), t);
99         
100         startTimer(1);
101         present_dec(data, &ctx);
102         t = stopTimer();
103         print_time_P(PSTR("\tdecrypt time: "), t);
104         
105         uart_putstr_P(PSTR("\r\n"));
106 }
107
108 /*****************************************************************************
109  *  main                                                                                                                                         *
110  *****************************************************************************/
111
112 int main (void){
113         char  str[20];
114         DEBUG_INIT();
115         uart_putstr("\r\n");
116
117         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
118         uart_putstr(cipher_name);
119         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
120
121         PGM_P    u   = PSTR("nessie\0test\0performance\0");
122         void_fpt v[] = {testrun_nessie_present, testrun_self_present, testrun_performance_present};
123
124         while(1){ 
125                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
126                 if(execcommand_d0_P(str, u, v)<0){
127                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
128                 }
129                 continue;
130         error:
131                 uart_putstr("ERROR\r\n");
132         }
133         
134 }