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