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 AUTHORS 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.2 2000/07/02 15:21:20 mdw
55 * Revision 1.1 2000/05/21 11:28:30 mdw
60 /*----- Header files ------------------------------------------------------*/
63 #include "storin-tab.h"
67 /*----- Debugging output --------------------------------------------------*/
79 /*----- The constant matrix -----------------------------------------------*/
81 static const uint24 m[] = STORIN_M, mi[] = STORIN_MI;
83 /*----- Main code ---------------------------------------------------------*/
85 /* --- @storin_init24@ --- *
87 * Arguments: @storin_ctx *k@ = pointer to cipher context to initialize
88 * @const uint24 *buf@ = pointer to buffer of key material
89 * @size_t sz@ = size of the key material
93 * Use: Initializes the storin for use.
96 void storin_init24(storin_ctx *k, const uint24 *buf, size_t sz)
103 #define KEYS (4 * (STORIN_ROUNDS + 1))
105 D( puts("Key schedule...\n"); )
107 /* --- Seed the subkey array --- */
113 matmul(dd, d, m, 4, 4, 4);
118 matmul(mm, d, m, 4, 4, 4);
119 for (i = 0; i < n; i++)
122 D( puts("Constant initial array contents:");
123 for (i = 0; i < KEYS; i++) {
124 printf("%06x ", k->k[i]);
128 fputc('\n', stdout); )
130 /* --- Mix in the real key material --- */
135 for (i = 0; i < KEYS; i++) {
144 D( puts("Array after mixing in key material:");
145 for (i = 0; i < KEYS; i++) {
146 printf("%06x ", k->k[i]);
150 fputc('\n', stdout); )
152 /* --- Now mangle the key material horribly --- */
154 for (i = 0; i < 4; i++)
158 for (i = 0; i < KEYS; i += 4) {
159 storin_eblk24(k, mm, mm);
160 for (n = 0; n < 4; n++)
165 D( puts("Final round subkeys:");
166 for (i = 0; i < KEYS; i++) {
167 printf("%06x ", k->k[i]);
171 fputc('\n', stdout); )
174 /* --- @storin_eblk24@, @storin_dblk24@ --- *
176 * Arguments: @const storin_ctx *k@ = pointer to cipher context
177 * @const uint24 s[4]@ = pointer to source block
178 * @uint24 d[4]@ = pointer to destination block
182 * Use: Low-level block encryption and decryption.
185 void storin_eblk24(const storin_ctx *k, const uint24 *s, uint24 *d)
189 const uint24 *kk = k->k;
191 D( puts("Encryption...");
192 printf(" plaintext: %06x %06x %06x %06x\n", s[0], s[1], s[2], s[3]); )
194 for (j = 0; j < 4; j++)
197 /* --- Main cipher guts --- */
199 for (i = 0; i < STORIN_ROUNDS; i++) {
200 D( printf("round %2i\n", i); )
201 for (j = 0; j < 4; j++)
203 D( printf(" mix key: %06x %06x %06x %06x\n", q[0], q[1], q[2], q[3]); )
204 matmul(p, m, q, 4, 4, 1);
205 D( printf(" matrix: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
206 for (j = 0; j < 4; j++)
208 D( printf(" lin trans: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
211 /* --- Postwhitening and output --- */
213 for (j = 0; j < 4; j++)
216 D( printf("ciphertext: %06x %06x %06x %06x\n", d[0], d[1], d[2], d[3]); )
220 void storin_dblk24(const storin_ctx *k, const uint24 *s, uint24 *d)
224 const uint24 *kk = k->k + KEYS;
226 D( puts("Decryption...");
227 printf("ciphertext: %06x %06x %06x %06x\n", s[0], s[1], s[2], s[3]); )
229 for (j = 0; j < 4; j++)
232 /* --- Main cipher guts --- */
234 for (i = 0; i < STORIN_ROUNDS; i++) {
235 D( printf("round %2i\n", i); )
236 for (j = 0; j < 4; j++)
237 q[3 - j] = p[3 - j] ^ *--kk;
238 D( printf(" mix key: %06x %06x %06x %06x\n", q[0], q[1], q[2], q[3]); )
239 for (j = 0; j < 4; j++)
241 D( printf(" lin trans: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
242 matmul(p, mi, q, 4, 4, 1);
243 D( printf(" matrix: %06x %06x %06x %06x\n", p[0], p[1], p[2], p[3]); )
246 /* --- Postwhitening and output --- */
248 for (j = 0; j < 4; j++)
249 d[3 - j] = p[3 - j] ^ *--kk;
251 D( printf(" plaintext: %06x %06x %06x %06x\n", d[0], d[1], d[2], d[3]); )
254 /*----- Test rig ----------------------------------------------------------*/
256 #if defined(DEBUG) || defined(TIMER)
262 uint24 kk[] = { 1, 2, 3, 4, 5 };
263 uint24 p[4] = { 6, 7, 8, 9 };
267 storin_init24(&c, kk, 5);
270 storin_eblk24(&c, p, q);
271 storin_dblk24(&c, q, q);
281 storin_eblk24(&c, p, q);
284 if (difftime(now, then) > 10.0)
287 printf("%g blocks/s = %g bits/s\n", n / 10.0, n * 96.0 / 10.0);
295 /*----- That's all, folks -------------------------------------------------*/