The NSA's Skipjack block cipher.
[u/mdw/catacomb] / skipjack.c
1 /* -*-c-*-
2 *
3 * $Id: skipjack.c,v 1.1 2000/07/15 15:39:33 mdw Exp $
4 *
5 * The Skipjack block cipher
6 *
7 * (c) 2000 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: skipjack.c,v $
33 * Revision 1.1 2000/07/15 15:39:33 mdw
34 * The NSA's Skipjack block cipher.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <mLib/bits.h>
41
42 #include "blkc.h"
43 #include "gcipher.h"
44 #include "skipjack.h"
45 #include "skipjack-tab.h"
46
47 /*----- Global variables --------------------------------------------------*/
48
49 const octet skipjack_keysz[] = { KSZ_SET, 10, 0 };
50
51 /*----- The Skipjack S-box ------------------------------------------------*/
52
53 static octet f[256] = SKIPJACK_S;
54
55 /*----- Main code ---------------------------------------------------------*/
56
57 /* --- @skipjack_init@ --- *
58 *
59 * Arguments: @skipjack_ctx *k@ = pointer to key block
60 * @const void *buf@ = pointer to key buffer
61 * @size_t sz@ = size of key material
62 *
63 * Returns: ---
64 *
65 * Use: Initializes a Skipjack key buffer. The key buffer must be
66 * exactly 10 bytes long.
67 */
68
69 void skipjack_init(skipjack_ctx *k, const void *buf, size_t sz)
70 {
71 KSZ_ASSERT(skipjack, sz);
72 memcpy(k->k, buf, sz);
73 }
74
75 /* --- @skipjack_eblk@, @skipjack_dblk@ --- *
76 *
77 * Arguments: @const skipjack_ctx *k@ = pointer to key block
78 * @const uint32 s[2]@ = pointer to source block
79 * @uint32 d[2]@ = pointer to skipjacktination block
80 *
81 * Returns: ---
82 *
83 * Use: Low-level block encryption and decryption.
84 */
85
86 #define G(x, i) do { \
87 octet _x = U8(x >> 8), _y = U8(x); \
88 _x ^= f[_y ^ k->k[i++]]; if (i >= 10) i = 0; \
89 _y ^= f[_x ^ k->k[i++]]; if (i >= 10) i = 0; \
90 _x ^= f[_y ^ k->k[i++]]; if (i >= 10) i = 0; \
91 _y ^= f[_x ^ k->k[i++]]; if (i >= 10) i = 0; \
92 x = U16((_x << 8) | _y); \
93 } while (0)
94
95 #define RULE_A(w, x, y, z, n, i) do { \
96 G(w, i); z ^= w ^ n++; \
97 } while (0)
98
99 #define RULE_B(w, x, y, z, n, i) do { \
100 x ^= w ^ n++; G(w, i); \
101 } while (0)
102
103 void skipjack_eblk(const skipjack_ctx *k, const uint32 *s, uint32 *d)
104 {
105 unsigned i = 0;
106 unsigned n = 1;
107 uint16 w = U16(s[0] >> 16), x = U16(s[0]);
108 uint16 y = U16(s[1] >> 16), z = U16(s[1]);
109
110 RULE_A(w, x, y, z, n, i); RULE_A(z, w, x, y, n, i);
111 RULE_A(y, z, w, x, n, i); RULE_A(x, y, z, w, n, i);
112 RULE_A(w, x, y, z, n, i); RULE_A(z, w, x, y, n, i);
113 RULE_A(y, z, w, x, n, i); RULE_A(x, y, z, w, n, i);
114 RULE_B(w, x, y, z, n, i); RULE_B(z, w, x, y, n, i);
115 RULE_B(y, z, w, x, n, i); RULE_B(x, y, z, w, n, i);
116 RULE_B(w, x, y, z, n, i); RULE_B(z, w, x, y, n, i);
117 RULE_B(y, z, w, x, n, i); RULE_B(x, y, z, w, n, i);
118 RULE_A(w, x, y, z, n, i); RULE_A(z, w, x, y, n, i);
119 RULE_A(y, z, w, x, n, i); RULE_A(x, y, z, w, n, i);
120 RULE_A(w, x, y, z, n, i); RULE_A(z, w, x, y, n, i);
121 RULE_A(y, z, w, x, n, i); RULE_A(x, y, z, w, n, i);
122 RULE_B(w, x, y, z, n, i); RULE_B(z, w, x, y, n, i);
123 RULE_B(y, z, w, x, n, i); RULE_B(x, y, z, w, n, i);
124 RULE_B(w, x, y, z, n, i); RULE_B(z, w, x, y, n, i);
125 RULE_B(y, z, w, x, n, i); RULE_B(x, y, z, w, n, i);
126
127 d[0] = ((uint32)w << 16) | (uint32)x;
128 d[1] = ((uint32)y << 16) | (uint32)z;
129 }
130
131 #define G_INV(x, i) do { \
132 octet _x = U8(x >> 8), _y = U8(x); \
133 _y ^= f[_x ^ k->k[--i]]; if (i == 0) i = 10; \
134 _x ^= f[_y ^ k->k[--i]]; if (i == 0) i = 10; \
135 _y ^= f[_x ^ k->k[--i]]; if (i == 0) i = 10; \
136 _x ^= f[_y ^ k->k[--i]]; if (i == 0) i = 10; \
137 x = U16((_x << 8) | _y); \
138 } while (0)
139
140 #define RULE_A_INV(w, x, y, z, n, i) do { \
141 w ^= x ^ --n; G_INV(x, i); \
142 } while (0)
143
144 #define RULE_B_INV(w, x, y, z, n, i) do { \
145 G_INV(x, i); y ^= x ^ --n; \
146 } while (0)
147
148 void skipjack_dblk(const skipjack_ctx *k, const uint32 *s, uint32 *d)
149 {
150 unsigned i = 8;
151 unsigned n = 33;
152 uint16 w = U16(s[0] >> 16), x = U16(s[0]);
153 uint16 y = U16(s[1] >> 16), z = U16(s[1]);
154
155 RULE_B_INV(w, x, y, z, n, i); RULE_B_INV(x, y, z, w, n, i);
156 RULE_B_INV(y, z, w, x, n, i); RULE_B_INV(z, w, x, y, n, i);
157 RULE_B_INV(w, x, y, z, n, i); RULE_B_INV(x, y, z, w, n, i);
158 RULE_B_INV(y, z, w, x, n, i); RULE_B_INV(z, w, x, y, n, i);
159 RULE_A_INV(w, x, y, z, n, i); RULE_A_INV(x, y, z, w, n, i);
160 RULE_A_INV(y, z, w, x, n, i); RULE_A_INV(z, w, x, y, n, i);
161 RULE_A_INV(w, x, y, z, n, i); RULE_A_INV(x, y, z, w, n, i);
162 RULE_A_INV(y, z, w, x, n, i); RULE_A_INV(z, w, x, y, n, i);
163 RULE_B_INV(w, x, y, z, n, i); RULE_B_INV(x, y, z, w, n, i);
164 RULE_B_INV(y, z, w, x, n, i); RULE_B_INV(z, w, x, y, n, i);
165 RULE_B_INV(w, x, y, z, n, i); RULE_B_INV(x, y, z, w, n, i);
166 RULE_B_INV(y, z, w, x, n, i); RULE_B_INV(z, w, x, y, n, i);
167 RULE_A_INV(w, x, y, z, n, i); RULE_A_INV(x, y, z, w, n, i);
168 RULE_A_INV(y, z, w, x, n, i); RULE_A_INV(z, w, x, y, n, i);
169 RULE_A_INV(w, x, y, z, n, i); RULE_A_INV(x, y, z, w, n, i);
170 RULE_A_INV(y, z, w, x, n, i); RULE_A_INV(z, w, x, y, n, i);
171
172 d[0] = ((uint32)w << 16) | (uint32)x;
173 d[1] = ((uint32)y << 16) | (uint32)z;
174 }
175
176 BLKC_TEST(SKIPJACK, skipjack)
177
178 /*----- That's all, folks -------------------------------------------------*/