3 * $Id: storin.c,v 1.1 2000/05/21 11:28:30 mdw Exp $
5 * Block cipher optimized for DSPs
7 * (c) 2000 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * Copyright (c) 2000 Mark Wooding
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
22 * 2, Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the authors may not be used to endorse or promote
27 * products derived from this software without specific prior written
30 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
33 * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
42 * Instead of accepting the above terms, you may redistribute and/or modify
43 * this software under the terms of either the GNU General Public License,
44 * or the GNU Library General Public License, published by the Free
45 * Software Foundation; either version 2 of the License, or (at your
46 * option) any later version.
49 /*----- Revision history --------------------------------------------------*
52 * Revision 1.1 2000/05/21 11:28:30 mdw
57 /*----- Header files ------------------------------------------------------*/
60 #include "storin-tab.h"
64 /*----- Debugging output --------------------------------------------------*/
76 /*----- The constant matrix -----------------------------------------------*/
78 static const uint24 m[] = STORIN_M, mi[] = STORIN_MI;
80 /*----- Main code ---------------------------------------------------------*/
82 /* --- @storin_init24@ --- *
84 * Arguments: @storin_ctx *k@ = pointer to cipher context to initialize
85 * @const uint24 *buf@ = pointer to buffer of key material
86 * @size_t sz@ = size of the key material
90 * Use: Initializes the storin for use.
93 void storin_init24(storin_ctx *k, const uint24 *buf, size_t sz)
100 #define KEYS (4 * (STORIN_ROUNDS + 1))
102 D( puts("Key schedule...\n"); )
104 /* --- Seed the subkey array --- */
110 matmul(dd, d, m, 4, 4, 4);
115 matmul(mm, d, m, 4, 4, 4);
116 for (i = 0; i < n; i++)
119 D( puts("Constant initial array contents:");
120 for (i = 0; i < KEYS; i++) {
121 printf("%06x ", k->k[i]);
125 fputc('\n', stdout); )
127 /* --- Mix in the real key material --- */
132 for (i = 0; i < KEYS; i++) {
141 D( puts("Array after mixing in key material:");
142 for (i = 0; i < KEYS; i++) {
143 printf("%06x ", k->k[i]);
147 fputc('\n', stdout); )
149 /* --- Now mangle the key material horribly --- */
151 for (i = 0; i < 4; i++)
155 for (i = 0; i < KEYS; i += 4) {
156 storin_eblk24(k, mm, mm);
157 for (n = 0; n < 4; n++)
162 D( puts("Final round subkeys:");
163 for (i = 0; i < KEYS; i++) {
164 printf("%06x ", k->k[i]);
168 fputc('\n', stdout); )
171 /* --- @storin_eblk24@, @storin_dblk24@ --- *
173 * Arguments: @const storin_ctx *k@ = pointer to cipher context
174 * @const uint24 s[4]@ = pointer to source block
175 * @uint24 d[4]@ = pointer to destination block
179 * Use: Low-level block encryption and decryption.
182 void storin_eblk24(const storin_ctx *k, const uint24 *s, uint24 *d)
186 const uint24 *kk = k->k;
188 D( puts("Encryption...");
189 printf(" plaintext: %06x %06x %06x %06x\n", s[0], s[1], s[2], s[3]); )
191 for (j = 0; j < 4; j++)
194 /* --- Main cipher guts --- */
196 for (i = 0; i < STORIN_ROUNDS; i++) {
197 D( printf("round %2i\n", i); )
198 for (j = 0; j < 4; j++)
200 D( printf(" mix key: %06x %06x %06x %06x\n", q[0], q[1], q[2], q[3]); )
201 matmul(p, m, q, 4, 4, 1);
202 D( printf(" matrix: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
203 for (j = 0; j < 4; j++)
205 D( printf(" lin trans: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
208 /* --- Postwhitening and output --- */
210 for (j = 0; j < 4; j++)
213 D( printf("ciphertext: %06x %06x %06x %06x\n", d[0], d[1], d[2], d[3]); )
217 void storin_dblk24(const storin_ctx *k, const uint24 *s, uint24 *d)
221 const uint24 *kk = k->k + KEYS;
223 D( puts("Decryption...");
224 printf("ciphertext: %06x %06x %06x %06x\n", s[0], s[1], s[2], s[3]); )
226 for (j = 0; j < 4; j++)
229 /* --- Main cipher guts --- */
231 for (i = 0; i < STORIN_ROUNDS; i++) {
232 D( printf("round %2i\n", i); )
233 for (j = 0; j < 4; j++)
234 q[3 - j] = p[3 - j] ^ *--kk;
235 D( printf(" mix key: %06x %06x %06x %06x\n", q[0], q[1], q[2], q[3]); )
236 for (j = 0; j < 4; j++)
238 D( printf(" lin trans: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
239 matmul(p, mi, q, 4, 4, 1);
240 D( printf(" matrix: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
243 /* --- Postwhitening and output --- */
245 for (j = 0; j < 4; j++)
246 d[3 - j] = p[3 - j] ^ *--kk;
248 D( printf(" plaintext: %06x %06x %06x %06x\n", d[0], d[1], d[2], d[3]); )
251 /*----- Test rig ----------------------------------------------------------*/
253 #if defined(DEBUG) || defined(TIMER)
259 uint24 kk[] = { 1, 2, 3, 4, 5 };
260 uint24 p[4] = { 6, 7, 8, 9 };
264 storin_init24(&c, kk, 5);
267 storin_eblk24(&c, p, q);
268 storin_dblk24(&c, q, q);
278 storin_eblk24(&c, p, q);
281 if (difftime(now, then) > 10.0)
284 printf("%g blocks/s = %g bits/s\n", n / 10.0, n * 96.0 / 10.0);
292 /*----- That's all, folks -------------------------------------------------*/