]> git.cryptolib.org Git - avr-crypto-lib.git/blob - twister/twister.c
adjusting test system uart reference
[avr-crypto-lib.git] / twister / twister.c
1 /* twister.c */
2 /*
3     This file is part of the AVR-Crypto-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 #include "config.h"
21 #include <stdint.h>
22 #include <string.h>
23 #include <avr/pgmspace.h>
24 #include "twister.h"
25 #include "twister_tables.h"
26 #include "memxor.h"
27
28 //#ifndef TWISTER_MUL_TABLE
29 # include "gf256mul/gf256mul.h"
30 //#endif
31
32 #define MDS(a,b)  pgm_read_byte(&(twister_mds[(a)][(b)]))
33
34 //#ifdef TWISTER_MUL_TABLE
35 //# define MULT(a,b) pgm_read_byte(&(twister_multab[(a)][(b)]))
36 //#else
37 # define MULT(a,b) gf256mul((a),(b), 0x4D)
38 //#endif
39
40 void twister_blank_round(twister_state_t *ctx){
41         uint8_t i,j,k;
42         uint8_t tmp[8][8];
43         /* add twist counter */
44         for(i=0; i<8; ++i){
45                 ctx->s[i][1] ^= ((uint8_t*)&(ctx->counter))[7-i];
46         }
47         ctx->counter--;
48         /* sub bytes */
49         for(i=0; i<8; ++i){
50                 for(j=0;j<8;++j){
51                         tmp[i][j] = pgm_read_byte(twister_sbox+ctx->s[i][j]);
52                 }
53         }
54         /* mix columns with integrates shift rows */
55         for( i=0; i<8; i++ ){
56                 // multiply with mds matrix
57                 for( j=0; j<8; j++ ){
58                         k=(i+1)&7;
59                         ctx->s[j][i] =
60                                 MULT( MDS(j,0), (tmp[0][i]) );
61                         ctx->s[j][i] ^=  
62                                 MULT( MDS(j,1), (tmp[1][k]) );
63                         ctx->s[j][i] ^=  
64                                 MULT( MDS(j,2), (tmp[2][((++k)&7)]) );
65                         ctx->s[j][i] ^=  
66                                 MULT( MDS(j,3), (tmp[3][((++k)&7)]) );
67                         ctx->s[j][i] ^=  
68                                 MULT( MDS(j,4), (tmp[4][((++k)&7)]) );
69                         ctx->s[j][i] ^=  
70                                 MULT( MDS(j,5), (tmp[5][((++k)&7)]) );
71                         ctx->s[j][i] ^=  
72                                 MULT( MDS(j,6), (tmp[6][((++k)&7)]) );
73                         ctx->s[j][i] ^=  
74                                 MULT( MDS(j,7), (tmp[7][((++k)&7)]) );
75                                 
76                 }       
77         }
78 }
79 void twister_mini_round(twister_state_t *ctx, const void *msg){
80         /* inject message */
81         uint8_t i;
82         for(i=0; i<8; ++i){
83                 ctx->s[7][7-i] ^= *((uint8_t*)msg);
84                 msg = (uint8_t*)msg +1; 
85         }
86         twister_blank_round(ctx);
87 }
88
89 void twister_ctx2hash(void *dest, twister_state_t *ctx, uint16_t hashsize_b){
90         uint8_t tmp[8][8];
91         uint8_t j;
92         uint16_t i=hashsize_b;
93         while(i>=64){
94                 i-=64;
95                 memcpy(tmp,ctx->s, 64);
96                 twister_blank_round(ctx);
97                 memxor(ctx->s, tmp, 64);
98                 twister_blank_round(ctx);
99                 for(j=0; j<8; ++j){
100                         *((uint8_t*)dest) = ctx->s[7-j][0] ^ tmp[7-j][0];
101                         dest = (uint8_t*)dest + 1;
102                 }
103         }
104         if(i>=32){
105                 memcpy(tmp,ctx->s, 64);
106                 twister_blank_round(ctx);
107                 memxor(ctx->s, tmp, 64);
108                 twister_blank_round(ctx);
109                 for(j=0; j<4; ++j){
110                         *((uint8_t*)dest) = ctx->s[7-j][0] ^ tmp[7-j][0];
111                         dest = (uint8_t*)dest + 1;
112                 }
113         }
114 }
115
116