Commit | Line | Data |
---|---|---|
8875ece6 | 1 | /* -*-c-*- |
2 | * | |
8875ece6 | 3 | * The Square block cipher |
4 | * | |
5 | * (c) 2000 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
8875ece6 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
8875ece6 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
8875ece6 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
8875ece6 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
30 | #include <assert.h> | |
31 | #include <stdio.h> | |
32 | ||
33 | #include <mLib/bits.h> | |
34 | ||
35 | #include "blkc.h" | |
36 | #include "gcipher.h" | |
37 | #include "paranoia.h" | |
38 | #include "square.h" | |
8875ece6 | 39 | |
40 | /*----- Global variables --------------------------------------------------*/ | |
41 | ||
42 | const octet square_keysz[] = { KSZ_RANGE, SQUARE_KEYSZ, 4, 16, 4 }; | |
43 | ||
44 | /*----- Constant tables ---------------------------------------------------*/ | |
45 | ||
e5b61a8d MW |
46 | extern const octet square_s[256], square_si[256]; |
47 | extern const uint32 square_t[4][256], square_ti[4][256]; | |
48 | extern const uint32 square_u[4][256]; | |
49 | extern const octet square_rcon[32]; | |
50 | ||
51 | #define S square_s | |
52 | #define SI square_si | |
53 | #define T square_t | |
54 | #define TI square_ti | |
55 | #define U square_u | |
56 | #define RCON square_rcon | |
8875ece6 | 57 | |
58 | /*----- Main code ---------------------------------------------------------*/ | |
59 | ||
8875ece6 | 60 | /* --- @square_init@ --- * |
61 | * | |
62 | * Arguments: @square_ctx *k@ = pointer to context to initialize | |
63 | * @const void *buf@ = pointer to buffer of key material | |
64 | * @size_t sz@ = size of the key material | |
65 | * | |
66 | * Returns: --- | |
67 | * | |
68 | * Use: Initializes a Square context with a particular key. Square | |
69 | * keys must be a multiple of 32 bits long, and may be at most | |
70 | * 128 bits. | |
71 | */ | |
72 | ||
73 | void square_init(square_ctx *k, const void *buf, size_t sz) | |
74 | { | |
75 | unsigned nk, nr, nw; | |
76 | unsigned i, j, jj; | |
77 | const octet *p; | |
78 | uint32 ww; | |
79 | uint32 kk[SQUARE_KWORDS]; | |
80 | ||
81 | /* --- Sort out the key size --- */ | |
82 | ||
83 | KSZ_ASSERT(square, sz); | |
84 | nk = sz / 4; | |
85 | ||
86 | /* --- Fetch the first key words out --- */ | |
87 | ||
88 | p = buf; | |
89 | for (i = 0; i < nk; i++) { | |
90 | kk[i] = LOAD32_L(p); | |
91 | p += 4; | |
92 | } | |
93 | nr = 8; | |
94 | ||
95 | /* --- Expand this material to fill the rest of the table --- */ | |
96 | ||
97 | nw = (nr + 1) * 4; | |
98 | ww = kk[i - 1]; | |
e5b61a8d | 99 | p = RCON; |
8875ece6 | 100 | for (; i < nw; i++) { |
101 | uint32 w = kk[i - nk]; | |
102 | if (i % nk == 0) { | |
103 | ww = ROR32(ww, 8); | |
104 | w ^= ww ^ *p++; | |
105 | } else | |
106 | w ^= ww; | |
107 | kk[i] = ww = w; | |
108 | } | |
109 | ||
110 | /* --- Make the encryption and decryption keys --- */ | |
111 | ||
112 | for (i = 0; i < nr * 4; i++) { | |
113 | uint32 w = kk[i]; | |
45c0fd36 | 114 | k->w[i] = (U[0][U8(w >> 0)] ^ U[1][U8(w >> 8)] ^ |
8875ece6 | 115 | U[2][U8(w >> 16)] ^ U[3][U8(w >> 24)]); |
116 | } | |
117 | for (; i < nw; i++) | |
118 | k->w[i] = kk[i]; | |
119 | ||
120 | jj = nw; | |
121 | for (i = 0; i < nr * 4; i += 4) { | |
122 | jj -= 4; | |
123 | for (j = 0; j < 4; j++) | |
124 | k->wi[i + j] = kk[jj + j]; | |
125 | } | |
126 | for (j = 0; j < 4; j++) | |
127 | k->wi[i + j] = k->w[j]; | |
128 | ||
129 | BURN(kk); | |
130 | } | |
131 | ||
132 | /* --- @square_eblk@, @square_dblk@ --- * | |
133 | * | |
134 | * Arguments: @const square_ctx *k@ = pointer to Square context | |
135 | * @const uint32 s[4]@ = pointer to source block | |
136 | * @uint32 d[4]@ = pointer to destination block | |
137 | * | |
138 | * Returns: --- | |
139 | * | |
140 | * Use: Low-level block encryption and decryption. | |
141 | */ | |
142 | ||
b14a0768 | 143 | #define SUB(s, sh, a, b, c, d) \ |
45c0fd36 | 144 | (s[U8((a) >> sh)] << 0 | s[U8((b) >> sh)] << 8 | \ |
b14a0768 | 145 | s[U8((c) >> sh)] << 16 | s[U8((d) >> sh)] << 24) |
146 | ||
147 | #define MIX(t, sh, a, b, c, d) \ | |
148 | (t[0][U8((a) >> sh)] ^ t[1][U8((b) >> sh)] ^ \ | |
149 | t[2][U8((c) >> sh)] ^ t[3][U8((d) >> sh)]) | |
8875ece6 | 150 | |
b14a0768 | 151 | #define DO(what, t, aa, bb, cc, dd, a, b, c, d, w) do { \ |
45c0fd36 MW |
152 | aa = what(t, 0, a, b, c, d) ^ *w++; \ |
153 | bb = what(t, 8, a, b, c, d) ^ *w++; \ | |
b14a0768 | 154 | cc = what(t, 16, a, b, c, d) ^ *w++; \ |
155 | dd = what(t, 24, a, b, c, d) ^ *w++; \ | |
8875ece6 | 156 | } while (0) |
157 | ||
158 | void square_eblk(const square_ctx *k, const uint32 *s, uint32 *dst) | |
159 | { | |
160 | uint32 a = s[0], b = s[1], c = s[2], d = s[3]; | |
161 | uint32 aa, bb, cc, dd; | |
78ec50fa | 162 | const uint32 *w = k->w; |
8875ece6 | 163 | |
164 | a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; | |
165 | ||
b14a0768 | 166 | DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w); |
167 | DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w); | |
168 | DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w); | |
169 | DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w); | |
170 | DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w); | |
171 | DO(MIX, T, a, b, c, d, aa, bb, cc, dd, w); | |
172 | DO(MIX, T, aa, bb, cc, dd, a, b, c, d, w); | |
173 | DO(SUB, S, a, b, c, d, aa, bb, cc, dd, w); | |
8875ece6 | 174 | |
175 | dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; | |
176 | } | |
177 | ||
178 | void square_dblk(const square_ctx *k, const uint32 *s, uint32 *dst) | |
179 | { | |
180 | uint32 a = s[0], b = s[1], c = s[2], d = s[3]; | |
181 | uint32 aa, bb, cc, dd; | |
78ec50fa | 182 | const uint32 *w = k->wi; |
8875ece6 | 183 | |
184 | a ^= *w++; b ^= *w++; c ^= *w++; d ^= *w++; | |
185 | ||
b14a0768 | 186 | DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w); |
187 | DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w); | |
188 | DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w); | |
189 | DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w); | |
190 | DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w); | |
191 | DO(MIX, TI, a, b, c, d, aa, bb, cc, dd, w); | |
192 | DO(MIX, TI, aa, bb, cc, dd, a, b, c, d, w); | |
193 | DO(SUB, SI, a, b, c, d, aa, bb, cc, dd, w); | |
8875ece6 | 194 | |
195 | dst[0] = a; dst[1] = b; dst[2] = c; dst[3] = d; | |
196 | } | |
197 | ||
198 | BLKC_TEST(SQUARE, square) | |
199 | ||
200 | /*----- That's all, folks -------------------------------------------------*/ |