]> git.cryptolib.org Git - arm-crypto-lib.git/blob - khazad/khazad.c
Adding Khazad
[arm-crypto-lib.git] / khazad / khazad.c
1 /* khazad.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2011 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
20 #include <stdint.h>
21 #include <string.h>
22 #include "gf256mul.h"
23 #include "memxor.h"
24 #include "khazad.h"
25
26 /*
27   | | | |     | | | |
28   V V V V     V V V V
29  +-------+   +-------+
30  |   P   |   |   Q   |
31  +-------+   +-------+
32   | | \ \     / / | |
33   | |  \ \   / /  | |
34   | |   \ \ / /   | |
35   | |    \ X /    | |
36   | |     X X     | |
37   | |    / X \    | |
38   | |   / / \ \   | |
39   | |  / /   \ \  | |
40   | | / /     \ \ | |
41   | | | |     | | | |
42   V V V V     V V V V
43  +-------+   +-------+
44  |   Q   |   |   P   |
45  +-------+   +-------+
46   | | \ \     / / | |
47   | |  \ \   / /  | |
48   | |   \ \ / /   | |
49   | |    \ X /    | |
50   | |     X X     | |
51   | |    / X \    | |
52   | |   / / \ \   | |
53   | |  / /   \ \  | |
54   | | / /     \ \ | |
55   | | | |     | | | |
56   V V V V     V V V V
57  +-------+   +-------+
58  |   P   |   |   Q   |
59  +-------+   +-------+
60   | | | |    | | | |
61   V V V V    V V V V
62
63
64 P:
65 3x Fx Ex 0x 5x 4x Bx Cx Dx Ax 9x 6x 7x 8x 2x 1x
66 Q:
67 9x Ex 5x 6x Ax 2x 3x Cx Fx 0x 4x Dx 7x Bx 1x 8x
68 */
69
70 static const uint8_t pq_lut[16] = {
71         0x39, 0xFE, 0xE5, 0x06, 0x5A, 0x42, 0xB3, 0xCC,
72         0xDF, 0xA0, 0x94, 0x6D, 0x77, 0x8B, 0x21, 0x18
73 };
74
75 uint8_t khazad_sbox(uint8_t a){
76         uint8_t b,c,d,e;
77         b = pq_lut[a>>4]  & 0xf0;
78         c = pq_lut[a&0xf] & 0x0f;
79         d = (b>>2)&0x0c;
80         e = (c<<2)&0x30;
81         b = (b&0xc0)|e;
82         c = (c&0x03)|d;
83         b = pq_lut[b>>4]  << 4;
84         c = pq_lut[c&0xf] >> 4;
85         d = (b>>2)&0x0c;
86         e = (c<<2)&0x30;
87         b = (b&0xc0)|e;
88         c = (c&0x03)|d;
89         b = pq_lut[b>>4]  & 0xf0;
90         c = pq_lut[c&0xf] & 0x0f;
91         return b|c;
92 }
93
94 static void gamma_x(uint8_t* a){
95         uint8_t i;
96         for(i=0; i<8; ++i){
97                 *a = khazad_sbox(*a);
98                 a++;
99         }
100 }
101
102 /******************************************************************************/
103 /* p8 (x) = x^8 + x^4 + x^3 + x^2 + 1 */
104 #define POLYNOM 0x1D
105
106 /*
107  * 01x 03x 04x 05x 06x 08x 0Bx 07x
108  * 03x 01x 05x 04x 08x 06x 07x 0Bx
109  * 04x 05x 01x 03x 0Bx 07x 06x 08x
110  * 05x 04x 03x 01x 07x 0Bx 08x 06x
111  * 06x 08x 0Bx 07x 01x 03x 04x 05x
112  * 08x 06x 07x 0Bx 03x 01x 05x 04x
113  * 0Bx 07x 06x 08x 04x 05x 01x 03x
114  * 07x 0Bx 08x 06x 05x 04x 03x 01x
115  */
116
117 static const uint8_t h[8][4] = {
118         { 0x13, 0x45, 0x68, 0xB7 },
119         { 0x31, 0x54, 0x86, 0x7B },
120         { 0x45, 0x13, 0xB7, 0x68 },
121         { 0x54, 0x31, 0x7B, 0x86 },
122         { 0x68, 0xB7, 0x13, 0x45 },
123         { 0x86, 0x7B, 0x31, 0x54 },
124         { 0xB7, 0x68, 0x45, 0x13 },
125         { 0x7B, 0x86, 0x54, 0x31 }
126 };
127
128 static void theta(uint8_t* a){
129         uint8_t i,j,x,accu;
130         uint8_t c[8];
131         uint8_t *hp;
132         hp = (uint8_t*)h;
133         for(i=0; i<8; ++i){
134                 accu = 0;
135                 for(j=0; j<4; ++j){
136                         x = *hp++;
137                         accu ^= gf256mul(*a++, x>>4, POLYNOM);
138                         accu ^= gf256mul(*a++, x&0xf, POLYNOM);
139                 }
140                 a -= 8;
141                 c[i] = accu;
142         }
143         memcpy(a, c, 8);
144 }
145
146 /******************************************************************************/
147
148 static void khazad_round(uint8_t* a, const uint8_t* k){
149         gamma_x(a);
150         theta(a);
151         memxor(a, k, 8);
152 }
153
154 /******************************************************************************/
155
156 void khazad_init(const void* key, khazad_ctx_t* ctx){
157         uint8_t c[8];
158         uint8_t i,r=0;
159         for(i=0; i<8; ++i){
160                 c[i] = khazad_sbox(r*8+i);
161         }
162         memcpy(ctx->k[r], (uint8_t*)key+8, 8);
163         khazad_round(ctx->k[r], c);
164         memxor(ctx->k[r], (uint8_t*)key, 8);
165         r=1;
166         for(i=0; i<8; ++i){
167                         c[i] = khazad_sbox(r*8+i);
168         }
169         memcpy(ctx->k[r], ctx->k[r-1], 8);
170         khazad_round(ctx->k[r], c);
171         memxor(ctx->k[r], (uint8_t*)key+8, 8);
172         for(r=2; r<9; ++r){
173                 for(i=0; i<8; ++i){
174                                 c[i] = khazad_sbox(r*8+i);
175                 }
176                 memcpy(ctx->k[r], ctx->k[r-1], 8);
177                 khazad_round(ctx->k[r], c);
178                 memxor(ctx->k[r], ctx->k[r-2], 8);
179         }
180 }
181
182 /******************************************************************************/
183
184 void khazad_enc(void* buffer, const khazad_ctx_t* ctx){
185         uint8_t r;
186         memxor(buffer, ctx->k[0], 8);
187         for(r=1; r<8; ++r){
188                 khazad_round(buffer, ctx->k[r]);
189         }
190         gamma_x(buffer);
191         memxor(buffer, ctx->k[8], 8);
192 }
193
194 /******************************************************************************/
195
196 void khazad_dec(void* buffer, const khazad_ctx_t* ctx){
197         uint8_t r=7;
198         memxor(buffer, ctx->k[8], 8);
199         gamma_x(buffer);
200         do{
201                 memxor(buffer, ctx->k[r--], 8);
202                 theta(buffer);
203                 gamma_x(buffer);
204         }while(r);
205         memxor(buffer, ctx->k[0], 8);
206 }
207
208
209
210