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