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