| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Generate tables for Whirlpool hash function |
| 6 | * |
| 7 | * (c) 2005 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 | /*----- Header files ------------------------------------------------------*/ |
| 31 | |
| 32 | #include <assert.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include <mLib/bits.h> |
| 37 | |
| 38 | /*----- Static variables --------------------------------------------------*/ |
| 39 | |
| 40 | static const octet E[] = { |
| 41 | 0x1, 0xb, 0x9, 0xc, 0xd, 0x6, 0xf, 0x3, |
| 42 | 0xe, 0x8, 0x7, 0x4, 0xa, 0x2, 0x5, 0x0 |
| 43 | }, R[] = { |
| 44 | 0x7, 0xc, 0xb, 0xd, 0xe, 0x4, 0x9, 0xf, |
| 45 | 0x6, 0x3, 0x8, 0xa, 0x2, 0x5, 0x1, 0x0 |
| 46 | }, C[] = { |
| 47 | 0x1, 0x1, 0x4, 0x1, 0x8, 0x5, 0x2, 0x9 |
| 48 | }; |
| 49 | |
| 50 | static octet S[256], T[256][8], log[256], alog[256]; |
| 51 | |
| 52 | /*----- Main code ---------------------------------------------------------*/ |
| 53 | |
| 54 | #define S_MOD 0x11d |
| 55 | |
| 56 | static void logtable(void) |
| 57 | { |
| 58 | unsigned i, x; |
| 59 | |
| 60 | for (i = 0, x = 1; i < 255; i++) { |
| 61 | log[x] = i; |
| 62 | alog[i] = x; |
| 63 | x <<= 1; |
| 64 | if (x & 0x100) x ^= S_MOD; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static octet mul(octet x, octet y) |
| 69 | { if (!x || !y) return (0); return (alog[(log[x] + log[y]) % 255]); } |
| 70 | |
| 71 | static void sbox(void) |
| 72 | { |
| 73 | unsigned i, j; |
| 74 | octet EI[16]; |
| 75 | octet l, r, y; |
| 76 | |
| 77 | for (i = 0; i < 16; i++) EI[E[i]] = i; |
| 78 | for (i = 0; i < 256; i++) { |
| 79 | l = (i >> 4) & 0xf; |
| 80 | r = (i >> 0) & 0xf; |
| 81 | l = E[l]; r = EI[r]; |
| 82 | y = R[l ^ r]; |
| 83 | l = E[l ^ y]; r = EI[r ^ y]; |
| 84 | S[i] = (l << 4) | r; |
| 85 | } |
| 86 | |
| 87 | for (i = 0; i < 256; i++) { |
| 88 | for (j = 0; j < 8; j++) |
| 89 | T[i][j] = mul(S[i], C[j]); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static unsigned long w32(int i, int j, int k) |
| 94 | { |
| 95 | kludge64 x; |
| 96 | LOAD64_L_(x, T[j]); |
| 97 | ROL64_(x, x, i * 8); |
| 98 | return (k ? LO64(x) : HI64(x)); |
| 99 | } |
| 100 | |
| 101 | int main(void) |
| 102 | { |
| 103 | int i, j; |
| 104 | |
| 105 | puts("\ |
| 106 | /* -*-c-*-\n\ |
| 107 | *\n\ |
| 108 | * Whirlpool tables [generated]\n\ |
| 109 | */\n\ |
| 110 | \n\ |
| 111 | #ifndef CATACOMB_WHIRLPOOL_TAB_H\n\ |
| 112 | #define CATACOMB_WHIRLPOOL_TAB_H\n\ |
| 113 | "); |
| 114 | |
| 115 | /* --- Write out the S-box --- */ |
| 116 | |
| 117 | logtable(); |
| 118 | sbox(); |
| 119 | fputs("\ |
| 120 | /* --- The byte substitution --- */\n\ |
| 121 | \n\ |
| 122 | #define WHIRLPOOL_S { \\\n\ |
| 123 | ", stdout); |
| 124 | for (i = 0; i < 256; i++) { |
| 125 | printf("0x%02x", S[i]); |
| 126 | if (i == 255) |
| 127 | fputs(" \\\n}\n\n", stdout); |
| 128 | else if (i % 8 == 7) |
| 129 | fputs(", \\\n ", stdout); |
| 130 | else |
| 131 | fputs(", ", stdout); |
| 132 | } |
| 133 | |
| 134 | /* --- Write out the key constant tables --- */ |
| 135 | |
| 136 | fputs("\ |
| 137 | /* --- The key generation constants --- */\n\ |
| 138 | \n\ |
| 139 | #define WHIRLPOOL_C { \\\n\ |
| 140 | ", stdout); |
| 141 | for (i = 0; i < 10; i++) { |
| 142 | printf("X64(%08lx, %08lx)", |
| 143 | (unsigned long)LOAD32_L(&S[i * 8 + 4]), |
| 144 | (unsigned long)LOAD32_L(&S[i * 8 + 0])); |
| 145 | if (i == 9) |
| 146 | fputs(" \\\n}\n\n", stdout); |
| 147 | else if (i % 2 == 1) |
| 148 | fputs(", \\\n ", stdout); |
| 149 | else |
| 150 | fputs(", ", stdout); |
| 151 | } |
| 152 | |
| 153 | /* --- Write out the big T tables --- */ |
| 154 | |
| 155 | fputs("\ |
| 156 | /* --- The 64-bit big round tables --- */\n\ |
| 157 | \n\ |
| 158 | #define WHIRLPOOL_T { \\\n\ |
| 159 | { ", stdout); |
| 160 | for (j = 0; j < 8; j++) { |
| 161 | for (i = 0; i < 256; i++) { |
| 162 | printf("X64(%08lx, %08lx)", w32(j, i, 0), w32(j, i, 1)); |
| 163 | if (i == 255) { |
| 164 | if (j == 7) |
| 165 | fputs(" } \\\n}\n\n", stdout); |
| 166 | else |
| 167 | fputs(" }, \\\n\ |
| 168 | \\\n\ |
| 169 | { ", stdout); |
| 170 | } else if (i % 2 == 1) |
| 171 | fputs(", \\\n ", stdout); |
| 172 | else |
| 173 | fputs(", ", stdout); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* --- Write out the smaller U and V tables --- */ |
| 178 | |
| 179 | fputs("\ |
| 180 | /* --- The 32-bit round tables --- */\n\ |
| 181 | \n\ |
| 182 | #define WHIRLPOOL_U { \\\n\ |
| 183 | { ", stdout); |
| 184 | for (j = 0; j < 4; j++) { |
| 185 | for (i = 0; i < 256; i++) { |
| 186 | printf("0x%08lx", w32(j, i, 1)); |
| 187 | if (i == 255) { |
| 188 | if (j == 3) |
| 189 | fputs(" } \\\n}\n\n", stdout); |
| 190 | else |
| 191 | fputs(" }, \\\n\ |
| 192 | \\\n\ |
| 193 | { ", stdout); |
| 194 | } else if (i % 4 == 3) |
| 195 | fputs(", \\\n ", stdout); |
| 196 | else |
| 197 | fputs(", ", stdout); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | fputs("\ |
| 202 | #define WHIRLPOOL_V { \\\n\ |
| 203 | { ", stdout); |
| 204 | for (j = 0; j < 4; j++) { |
| 205 | for (i = 0; i < 256; i++) { |
| 206 | printf("0x%08lx", w32(j, i, 0)); |
| 207 | if (i == 255) { |
| 208 | if (j == 3) |
| 209 | fputs(" } \\\n}\n\n", stdout); |
| 210 | else |
| 211 | fputs(" }, \\\n\ |
| 212 | \\\n\ |
| 213 | { ", stdout); |
| 214 | } else if (i % 4 == 3) |
| 215 | fputs(", \\\n ", stdout); |
| 216 | else |
| 217 | fputs(", ", stdout); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* --- Done --- */ |
| 222 | |
| 223 | puts("#endif"); |
| 224 | |
| 225 | if (fclose(stdout)) { |
| 226 | fprintf(stderr, "error writing data\n"); |
| 227 | exit(EXIT_FAILURE); |
| 228 | } |
| 229 | |
| 230 | return (0); |
| 231 | } |
| 232 | |
| 233 | /*----- That's all, folks -------------------------------------------------*/ |