]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serpent/serpent-sboxes_c.c
fixing style, typos and uart
[avr-crypto-lib.git] / serpent / serpent-sboxes_c.c
1 /* serpent-sboxes.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 /* serpent-sboxes.c
20  * a non-bitsliced implementation of the serpent sboxes
21  * author: Daniel Otte 
22  * email:  daniel.otte@rub.de
23  * license: GPLv3
24  */
25
26 #include <stdint.h>
27 #include <string.h> /* memset() */
28 #include <avr/pgmspace.h>
29 #include "serpent-sboxes.h"
30
31 const uint8_t sbox[] PROGMEM = {
32  0x38, 0xF1, 0xA6, 0x5B, 0xED, 0x42, 0x70, 0x9C,
33  0xFC, 0x27, 0x90, 0x5A, 0x1B, 0xE8, 0x6D, 0x34, 
34  0x86, 0x79, 0x3C, 0xAF, 0xD1, 0xE4, 0x0B, 0x52,
35  0x0F, 0xB8, 0xC9, 0x63, 0xD1, 0x24, 0xA7, 0x5E,
36  0x1F, 0x83, 0xC0, 0xB6, 0x25, 0x4A, 0x9E, 0x7D,
37  0xF5, 0x2B, 0x4A, 0x9C, 0x03, 0xE8, 0xD6, 0x71,
38  0x72, 0xC5, 0x84, 0x6B, 0xE9, 0x1F, 0xD3, 0xA0,
39  0x1D, 0xF0, 0xE8, 0x2B, 0x74, 0xCA, 0x93, 0x56,
40 /* now the inverted sboxes */
41  0xD3, 0xB0, 0xA6, 0x5C, 0x1E, 0x47, 0xF9, 0x82,
42  0x58, 0x2E, 0xF6, 0xC3, 0xB4, 0x79, 0x1D, 0xA0,
43  0xC9, 0xF4, 0xBE, 0x12, 0x03, 0x6D, 0x58, 0xA7,
44  0x09, 0xA7, 0xBE, 0x6D, 0x35, 0xC2, 0x48, 0xF1,
45  0x50, 0x83, 0xA9, 0x7E, 0x2C, 0xB6, 0x4F, 0xD1,
46  0x8F, 0x29, 0x41, 0xDE, 0xB6, 0x53, 0x7C, 0xA0,
47  0xFA, 0x1D, 0x53, 0x60, 0x49, 0xE7, 0x2C, 0x8B,
48  0x30, 0x6D, 0x9E, 0xF8, 0x5C, 0xB7, 0xA1, 0x42
49 };        
50          
51
52 #define SHR_O(a) c=(a)&1; ((a) = (a)>>1)
53 #define SHR_I(a) ((a) = (c?0x80:0x00)| ((a)>>1))
54
55 static void serpent_ip(uint32_t *i, uint8_t *o){
56         uint8_t c; // carry 
57         uint8_t n,m;
58         memset(o, 0, 16);
59         for(n=0; n<16; ++n){
60                 for(m=0; m<2; ++m){
61                 SHR_O(i[0]);
62                 SHR_I(o[n]);
63                 SHR_O(i[1]);
64                 SHR_I(o[n]);
65                 SHR_O(i[2]);
66                 SHR_I(o[n]);
67                 SHR_O(i[3]);
68                 SHR_I(o[n]);
69                 }
70         }
71 }
72
73 #undef SHR_I
74 #define SHR_I(a) ((a) = (c?0x80000000L:0x00L)| ((a)>>1)) /* we use 32-bit words here */
75
76 static void serpent_fp(uint32_t *i, uint32_t *o){
77         uint8_t c; // carry 
78         uint8_t n,m;
79         memset(o, 0, 16);
80         for(n=0; n<4; ++n){
81                 for(m=0; m<8; ++m){
82                 SHR_O(i[n]);
83                 SHR_I(o[0]);
84                 SHR_O(i[n]);
85                 SHR_I(o[1]);
86                 SHR_O(i[n]);
87                 SHR_I(o[2]);
88                 SHR_O(i[n]);
89                 SHR_I(o[3]);
90                 }
91         }
92 }
93
94 /******************************************************************************/
95 static void sbox128x(uint8_t box, void *w){
96         uint8_t sb[16];
97         uint8_t i,t,x;
98         box &= 0x0f;
99         /* load sbox */
100         for(i=0; i<8; ++i){
101                 t = pgm_read_byte(sbox + box*8 + i);
102                 sb[2*i+0]=t>>4;
103                 sb[2*i+1]=t&0xf;
104         }
105         uint8_t o[16];
106         serpent_ip(w,o);
107         
108         for(i=0; i<16; ++i){
109                 t = ((uint8_t*)o)[i];
110                 x = sb[t>>4];
111                 x <<= 4;
112                 x |= sb[t&0xf];
113                 ((uint8_t*)o)[i] = x;
114         }
115         serpent_fp((uint32_t*)o, w);
116 }
117
118 void sbox128(void * w, uint8_t box){
119         sbox128x(box&0x7, w);
120 }
121
122
123 void inv_sbox128(void * w, uint8_t box){
124         sbox128x(((box&0x7)|0x8), w);
125 }
126
127
128