New cipher Noekeon added.
[u/mdw/catacomb] / noekeon.c
1 /* -*-c-*-
2 *
3 * $Id: noekeon.c,v 1.1 2001/05/08 22:17:41 mdw Exp $
4 *
5 * The Noekeon block cipher
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: noekeon.c,v $
33 * Revision 1.1 2001/05/08 22:17:41 mdw
34 * New cipher Noekeon added.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <assert.h>
41 #include <stdio.h>
42
43 #include <mLib/bits.h>
44
45 #include "blkc.h"
46 #include "gcipher.h"
47 #include "noekeon.h"
48
49 /*----- Global variables --------------------------------------------------*/
50
51 const octet noekeon_keysz[] = { KSZ_SET, NOEKEON_KEYSZ, 0 };
52
53 /*----- Magic constants ---------------------------------------------------*/
54
55 /* --- To generate the magic --- *
56 *
57 * perl -e'@r=();$x=0x80;for(0..16){push(@r,$x);$x<<=1;$x^=0x11b if$x&0x100;}
58 * i;print join(", ",map{sprintf"0x%02x",$_}@r),"\n";'
59 */
60
61 static const octet rcon[17] = {
62 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a,
63 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
64 0xd4
65 };
66
67 /*----- Main code ---------------------------------------------------------*/
68
69 /* --- @noekeon_init@--- *
70 *
71 * Arguments: @noekeon_ctx *k@ = pointer to key block to fill in
72 * @const void *buf@ = pointer to buffer of key material
73 * @size_t sz@ = size of key material
74 *
75 * Returns: ---
76 *
77 * Use: Initializes a Noekeon key buffer. Noekeon accepts a 128-bit
78 * key.
79 */
80
81 void noekeon_init(noekeon_ctx *k, const void *buf, size_t sz)
82 {
83 const octet *p = buf;
84 static const noekeon_ctx nullkey = { { 0, 0, 0, 0 } };
85
86 KSZ_ASSERT(noekeon, sz);
87 k->k[0] = LOAD32(p + 0);
88 k->k[1] = LOAD32(p + 4);
89 k->k[2] = LOAD32(p + 8);
90 k->k[3] = LOAD32(p + 12);
91 noekeon_eblk(&nullkey, k->k, k->k);
92 }
93
94 /* --- @noekeon_eblk@, @noekeon_dblk@ --- *
95 *
96 * Arguments: @const noekeon_ctx *k@ = pointer to key block
97 * @const uint32 s[2]@ = pointer to source block
98 * @uint32 d[2]@ = pointer to destination block
99 *
100 * Returns: ---
101 *
102 * Use: Low-level block encryption and decryption.
103 */
104
105 #define GAMMA(a, b, c, d) do { \
106 uint32 _x; \
107 b ^= ~(c | d); a ^= b & c; \
108 _x = d; d = a; a = _x; \
109 c ^= a ^ b ^ d; \
110 b ^= ~(c | d); a ^= b & c; \
111 } while (0)
112
113 #define THETA(ka, kb, kc, kd, a, b, c, d) do { \
114 uint32 _x; \
115 _x = a ^ c; _x ^= ROR32(_x, 8) ^ ROL32(_x, 8); b ^= _x; d ^= _x; \
116 a ^= ka; b ^= kb; c ^= kc; d ^= kd; \
117 _x = b ^ d; _x ^= ROR32(_x, 8) ^ ROL32(_x, 8); a ^= _x; c ^= _x; \
118 } while (0)
119
120 #define ITHETA(ka, kb, kc, kd, a, b, c, d) do { \
121 uint32 _x; \
122 _x = b ^ d; _x ^= ROR32(_x, 8) ^ ROL32(_x, 8); a ^= _x; c ^= _x; \
123 a ^= ka; b ^= kb; c ^= kc; d ^= kd; \
124 _x = a ^ c; _x ^= ROR32(_x, 8) ^ ROL32(_x, 8); b ^= _x; d ^= _x; \
125 } while (0)
126
127 #define PI1(a, b, c, d) do { \
128 b = ROL32(b, 1); c = ROL32(c, 5); d = ROL32(d, 2); \
129 } while (0)
130
131 #define PI2(a, b, c, d) do { \
132 b = ROR32(b, 1); c = ROR32(c, 5); d = ROR32(d, 2); \
133 } while (0)
134
135 #define ROUND(r, ka, kb, kc, kd, a, b, c, d) do { \
136 a ^= *r++; THETA(ka, kb, kc, kd, a, b, c, d); \
137 PI1(a, b, c, d); GAMMA(a, b, c, d); PI2(a, b, c, d); \
138 } while (0)
139
140 #define IROUND(r, ka, kb, kc, kd, a, b, c, d) do { \
141 ITHETA(ka, kb, kc, kd, a, b, c, d); a ^= *--r; \
142 PI1(a, b, c, d); GAMMA(a, b, c, d); PI2(a, b, c, d); \
143 } while (0)
144
145 void noekeon_eblk(const noekeon_ctx *k, const uint32 *src, uint32 *dst)
146 {
147 uint32 ka = k->k[0], kb = k->k[1], kc = k->k[2], kd = k->k[3];
148 uint32 a = src[0], b = src[1], c = src[2], d = src[3];
149 const octet *r = rcon;
150
151 ROUND(r, ka, kb, kc, kd, a, b, c, d);
152 ROUND(r, ka, kb, kc, kd, a, b, c, d);
153 ROUND(r, ka, kb, kc, kd, a, b, c, d);
154 ROUND(r, ka, kb, kc, kd, a, b, c, d);
155 ROUND(r, ka, kb, kc, kd, a, b, c, d);
156 ROUND(r, ka, kb, kc, kd, a, b, c, d);
157 ROUND(r, ka, kb, kc, kd, a, b, c, d);
158 ROUND(r, ka, kb, kc, kd, a, b, c, d);
159 ROUND(r, ka, kb, kc, kd, a, b, c, d);
160 ROUND(r, ka, kb, kc, kd, a, b, c, d);
161 ROUND(r, ka, kb, kc, kd, a, b, c, d);
162 ROUND(r, ka, kb, kc, kd, a, b, c, d);
163 ROUND(r, ka, kb, kc, kd, a, b, c, d);
164 ROUND(r, ka, kb, kc, kd, a, b, c, d);
165 ROUND(r, ka, kb, kc, kd, a, b, c, d);
166 ROUND(r, ka, kb, kc, kd, a, b, c, d);
167
168 a ^= *r++; THETA(ka, kb, kc, kd, a, b, c, d);
169
170 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
171 }
172
173 void noekeon_dblk(const noekeon_ctx *k, const uint32 *src, uint32 *dst)
174 {
175 uint32 ka = k->k[0], kb = k->k[1], kc = k->k[2], kd = k->k[3];
176 uint32 a = src[0], b = src[1], c = src[2], d = src[3];
177 const octet *r = rcon + sizeof(rcon);
178
179 IROUND(r, ka, kb, kc, kd, a, b, c, d);
180 IROUND(r, ka, kb, kc, kd, a, b, c, d);
181 IROUND(r, ka, kb, kc, kd, a, b, c, d);
182 IROUND(r, ka, kb, kc, kd, a, b, c, d);
183 IROUND(r, ka, kb, kc, kd, a, b, c, d);
184 IROUND(r, ka, kb, kc, kd, a, b, c, d);
185 IROUND(r, ka, kb, kc, kd, a, b, c, d);
186 IROUND(r, ka, kb, kc, kd, a, b, c, d);
187 IROUND(r, ka, kb, kc, kd, a, b, c, d);
188 IROUND(r, ka, kb, kc, kd, a, b, c, d);
189 IROUND(r, ka, kb, kc, kd, a, b, c, d);
190 IROUND(r, ka, kb, kc, kd, a, b, c, d);
191 IROUND(r, ka, kb, kc, kd, a, b, c, d);
192 IROUND(r, ka, kb, kc, kd, a, b, c, d);
193 IROUND(r, ka, kb, kc, kd, a, b, c, d);
194 IROUND(r, ka, kb, kc, kd, a, b, c, d);
195
196 ITHETA(ka, kb, kc, kd, a, b, c, d); a ^= *--r;
197
198 dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d;
199 }
200
201 BLKC_TEST(NOEKEON, noekeon)
202
203 /*----- That's all, folks -------------------------------------------------*/