]> git.cryptolib.org Git - arm-crypto-lib.git/blob - serpent/serpent.c
Adding Serpent
[arm-crypto-lib.git] / serpent / serpent.c
1 /* serpent.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.c
20  * a bitsliced implementation of the serpent cipher for avr microcontrollers
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 "memxor.h"
29 #include "serpent.h"
30 #include "serpent-sboxes.h"
31
32 /******************************************************************************/
33
34 uint32_t rotl32(uint32_t a, uint8_t n){
35         return ((a<<n) | (a>>(32-n)));
36 }
37
38
39 uint32_t rotr32(uint32_t a, uint8_t n){
40         return ((a>>n) | (a<<(32-n)));
41 }
42
43
44 #define X0 (((uint32_t*)b)[0])
45 #define X1 (((uint32_t*)b)[1])
46 #define X2 (((uint32_t*)b)[2])
47 #define X3 (((uint32_t*)b)[3])
48
49 static void serpent_lt(uint8_t *b){
50         X0 = rotl32(X0, 13);
51         X2 = rotl32(X2,  3);
52         X1 ^= X0 ^ X2;
53         X3 ^= X2 ^ (X0 << 3);
54         X1 = rotl32(X1, 1);
55         X3 = rotl32(X3, 7);
56         X0 ^= X1 ^ X3;
57         X2 ^= X3 ^ (X1 << 7);
58         X0 = rotl32(X0, 5);
59         X2 = rotr32(X2, 10);
60 }
61
62 static void serpent_inv_lt(uint8_t *b){
63         X2 = rotl32(X2, 10);
64         X0 = rotr32(X0, 5);
65         X2 ^= X3 ^ (X1 << 7);
66         X0 ^= X1 ^ X3;
67         X3 = rotr32(X3, 7);
68         X1 = rotr32(X1, 1);
69         X3 ^= X2 ^ (X0 << 3);
70         X1 ^= X0 ^ X2;
71         X2 = rotr32(X2,  3);
72         X0 = rotr32(X0, 13);
73 }
74
75 #define GOLDEN_RATIO 0x9e3779b9l
76
77 static uint32_t serpent_gen_w(uint32_t * b, uint8_t i){
78         uint32_t ret;
79         ret = b[0] ^ b[3] ^ b[5] ^ b[7] ^ GOLDEN_RATIO ^ (uint32_t)i;
80         ret = rotl32(ret, 11);
81         return ret;
82
83
84 void serpent_init(const void* key, uint16_t keysize_b, serpent_ctx_t* ctx){
85         uint32_t buffer[8];
86         uint8_t i,j;
87         if(keysize_b<256){
88                 /* keysize is less than 256 bit, padding needed */
89                 memset(buffer, 0, 32);
90                 memcpy(buffer, key, (keysize_b+7)/8);
91                 ((uint8_t*)buffer)[keysize_b/8] |= 1<<(keysize_b%8);
92         } else {
93                 /* keysize is 256 bit */
94                 memcpy((void*)buffer, key, 32);
95         }
96         for(i=0; i<33; ++i){
97                 for(j=0; j<4; ++j){
98                         ctx->k[i][j] = serpent_gen_w(buffer, i*4+j);
99                         memmove(buffer, &(buffer[1]), 7*4); /* shift buffer one to the "left" */
100                         buffer[7] = ctx->k[i][j];
101                 }
102         }
103         for(i=0; i<33; ++i){
104                 sbox128(ctx->k[i],3-i);
105         }
106 }
107
108 void serpent_enc(void* buffer, const serpent_ctx_t* ctx){
109         uint8_t i;
110         for(i=0; i<31; ++i){
111                 memxor(buffer, ctx->k[i], 16);
112                 sbox128(buffer, i);
113                 serpent_lt((uint8_t*)buffer);
114         }
115         memxor(buffer, ctx->k[i], 16);
116         sbox128(buffer, i);
117         ++i;
118         memxor(buffer, ctx->k[i], 16);
119 }
120
121 void serpent_dec(void* buffer, const serpent_ctx_t* ctx){
122         int8_t i=32;
123         
124         memxor(buffer, ctx->k[i], 16);
125         --i;
126         inv_sbox128(buffer, i);
127         memxor((uint8_t*)buffer, ctx->k[i], 16);
128         --i;
129         for(; i>=0; --i){
130                 serpent_inv_lt(buffer);
131                 inv_sbox128(buffer, i);
132                 memxor(buffer, ctx->k[i], 16);
133         }
134 }
135
136
137
138
139