]> git.cryptolib.org Git - avr-crypto-lib.git/blob - serpent-sboxes.c
rcfour optimized++; memxor optimized++
[avr-crypto-lib.git] / serpent-sboxes.c
1 /* serpent-sboxes.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 /* 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 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 ip(uint8_t *o, uint32_t *i){
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
77 static void fp(uint32_t *o, uint32_t *i){
78         uint8_t c; // carry 
79         uint8_t n,m;
80         memset(o, 0, 16);
81         for(n=0; n<4; ++n){
82                 for(m=0; m<8; ++m){
83                 SHR_O(i[n]);
84                 SHR_I(o[0]);
85                 SHR_O(i[n]);
86                 SHR_I(o[1]);
87                 SHR_O(i[n]);
88                 SHR_I(o[2]);
89                 SHR_O(i[n]);
90                 SHR_I(o[3]);
91                 }
92         }
93 }
94
95 /******************************************************************************/
96
97 static void sbox128x(uint8_t box, void* w){
98         uint8_t sb[16];
99         uint8_t i,t,x;
100         box &= 0x0f;
101         /* load sbox */
102         for(i=0; i<8; ++i){
103                 t = pgm_read_byte(sbox + box*8 + i);
104                 sb[2*i+0]=t>>4;
105                 sb[2*i+1]=t&0xf;
106         }
107         uint8_t o[16];
108         ip(o, w);
109         
110         for(i=0; i<16; ++i){
111                 t = ((uint8_t*)o)[i];
112                 x = sb[t>>4];
113                 x <<= 4;
114                 x |= sb[t&0xf];
115                 ((uint8_t*)o)[i] = x;
116         }
117         fp(w, (uint32_t*)o);
118 }
119
120 void sbox128(void * w, uint8_t box){
121         sbox128x(box&0x7, w);
122 }
123
124 void inv_sbox128(void * w, uint8_t box){
125         sbox128x(((box&0x7)|0x8), w);
126 }