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