]> git.cryptolib.org Git - avr-crypto-lib.git/blob - cscipher/cscipher_small.c
b15c723963835ad1a2550d6ac0396530dfae20ec
[avr-crypto-lib.git] / cscipher / cscipher_small.c
1 /* cscipher_small_core.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
20 #include <stdint.h>
21 #include <avr/pgmspace.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "memxor.h"
25 #include "cscipher.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "cli.h"
31 #endif
32
33 #define ROTL(a) (((a)<<1)|((a)>>7))
34
35 #ifndef SBOX_PROG
36 #define SBOX_PROG 0
37 #endif
38
39 #if SBOX_PROG
40 static const uint8_t fg_table[] PROGMEM = {
41     0xfa, 0xd6, 0xb0, 0xb2, 0x7b, 0x5e, 0x71, 0x78,
42     0xed, 0xd4, 0xa5, 0xb3, 0xef, 0xdc, 0xe7, 0xf9
43 };
44
45 static
46 uint8_t p(uint8_t a) {
47     a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
48     a ^= pgm_read_byte(fg_table+(a>>4)) &0x0f;
49     a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
50     return a;
51 }
52 #define P(a) p(a)
53
54 #else
55
56 #include "cscipher_sbox.h"
57 #define P(a) pgm_read_byte(cscipher_sbox+(a))
58 #endif
59
60 static const uint8_t round_const[] PROGMEM = {
61         0xb7, 0xe1, 0x51, 0x62, 0x8a, 0xed, 0x2a, 0x6a,
62         0xbf, 0x71, 0x58, 0x80, 0x9c, 0xf4, 0xf3, 0xc7 };
63
64 static const uint8_t ks_const[] PROGMEM = {
65         0x29, 0x0d, 0x61, 0x40, 0x9c, 0xeb, 0x9e, 0x8f,
66         0x1f, 0x85, 0x5f, 0x58, 0x5b, 0x01, 0x39, 0x86,
67         0x97, 0x2e, 0xd7, 0xd6, 0x35, 0xae, 0x17, 0x16,
68         0x21, 0xb6, 0x69, 0x4e, 0xa5, 0x72, 0x87, 0x08,
69         0x3c, 0x18, 0xe6, 0xe7, 0xfa, 0xad, 0xb8, 0x89,
70         0xb7, 0x00, 0xf7, 0x6f, 0x73, 0x84, 0x11, 0x63,
71         0x3f, 0x96, 0x7f, 0x6e, 0xbf, 0x14, 0x9d, 0xac,
72         0xa4, 0x0e, 0x7e, 0xf6, 0x20, 0x4a, 0x62, 0x30,
73         0x03, 0xc5, 0x4b, 0x5a, 0x46, 0xa3, 0x44, 0x65
74 };
75
76 static uint16_t m(uint16_t a)
77 {
78     uint8_t xl, xr, yl, yr;
79     uint16_t ret;
80     xr = a >> 8;
81     xl = a & 0xff;
82     yl = (ROTL(xl) & 0x55) ^ xl ^ xr;
83     yr = ROTL(xl) ^ xr;
84     ret = (P(yr) << 8) | P(yl);
85     return ret;
86 }
87
88 static uint16_t m_inv(uint16_t a)
89 {
90     uint8_t xl, xr;
91     xr = P(a >> 8);
92     xl = P(a & 0xff);
93     xl ^= xr;
94     xl ^= (ROTL(xl) & 0xaa);
95     xr ^= ROTL(xl);
96     return (xr << 8) | xl;
97 }
98
99 void cscipher_enc(void *buffer, const cscipher_ctx_t *ctx)
100 {
101     uint8_t i, j, k;
102     uint8_t tmp[8];
103     for (i = 0; i < 8; ++i) {
104 #if DEBUG
105         cli_putstr_P(PSTR("\r\nDBG: round "));
106         cli_hexdump(&i, 1);
107         cli_putstr_P(PSTR(" buffer:"));
108         cli_hexdump(buffer, 8);
109 #endif
110         for (j = 0; j < 3; ++j) {
111             if (j == 0) {
112                 memxor(buffer, ctx->keys[i], 8);
113             } else {
114                 memxor_P(buffer, round_const + ((j == 1) ? 0 : 8), 8);
115             }
116             for (k = 0; k < 4; ++k) {
117                 ((uint16_t*) tmp)[k] = m(((uint16_t*) buffer)[k]);
118             }
119             for (k = 0; k < 4; ++k) {
120                 ((uint8_t*) buffer)[k] = tmp[2 * k];
121                 ((uint8_t*) buffer)[k + 4] = tmp[2 * k + 1];
122             }
123         }
124     }
125     memxor(buffer, ctx->keys[8], 8);
126 }
127
128 void cscipher_dec(void *buffer, const cscipher_ctx_t *ctx)
129 {
130     uint8_t i = 7, j, k;
131     uint8_t tmp[8];
132     memxor(buffer, ctx->keys[8], 8);
133     do {
134         for (j = 0; j < 3; ++j) {
135             for (k = 0; k < 4; ++k) {
136                 tmp[2 * k] = ((uint8_t*) buffer)[k];
137                 tmp[2 * k + 1] = ((uint8_t*) buffer)[4 + k];
138             }
139             for (k = 0; k < 4; ++k) {
140                 ((uint16_t*) buffer)[k] = m_inv(((uint16_t*) tmp)[k]);
141             }
142             if (j == 2) {
143                 memxor(buffer, ctx->keys[i], 8);
144             } else {
145                 memxor_P(buffer, round_const + ((j == 1) ? 0 : 8), 8);
146             }
147
148         }
149     } while (i--);
150 }
151
152 void cscipher_init(const void *key, cscipher_ctx_t *ctx)
153 {
154     uint8_t tmp_key[16], tmp[8];
155     uint8_t i, j, k, t = 0;
156     memcpy(tmp_key, key, 16);
157     for (i = 0; i < 9; ++i) {
158 #if DEBUG
159         cli_putstr_P(PSTR("\r\nDBG: round "));
160         cli_hexdump(&i, 1);
161         cli_putstr_P(PSTR(" key state:"));
162         cli_hexdump(tmp_key, 16);
163 #endif
164         memcpy(tmp, tmp_key + (((i & 1) == 0) ? 0 : 8), 8);
165         memxor_P(tmp, ks_const + 8 * i, 8);
166         for (j = 0; j < 8; ++j) {
167             tmp[j] = P(tmp[j]);
168         }
169         for (j = 0; j < 8; ++j) {
170             for (k = 0; k < 8; ++k) {
171                 t <<= 1;
172                 t |= tmp[k] >> 7;
173                 tmp[k] <<= 1;
174             }
175             tmp_key[j + (((i & 1) == 0) ? 8 : 0)] ^= t;
176         }
177         memcpy(ctx->keys[i], tmp_key + (((i & 1) == 0) ? 8 : 0), 8);
178     }
179 }