3 * $Id: blowfish-mktab.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
5 * Build Blowfish key table
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
37 /*----- Main code ---------------------------------------------------------*/
41 * Arguments: @uint32 *buf@ = pointer to the output buffer
42 * @size_t n@ = number of output digits wanted
46 * Use: Writes digits of %$\pi$% to the given array. The algorithm
47 * is based on the Spigot algorithm by Stanley Rabinowitz and
48 * Stan Wagon, published in Amer.Math.Monthly, March 1995, with
49 * bug fixes by C. Haenel. I then bodged it to output hex
50 * digits rather than decimal ones, and to leave off the initial
53 * I've not analysed the algorithm very much.
56 #define SPIGOT_WORDS (18 + 4 * 256ul)
58 #define SPIGOT_RADIX (1ul << SPIGOT_BITS)
59 #define SPIGOT_BUFLEN (SPIGOT_WORDS * 32)
67 static void spigot(uint32 *buf, size_t n)
71 unsigned a[SPIGOT_BUFLEN] = { 0 };
74 unsigned max = 32 * n;
75 Q( size_t step = n / 60; )
80 #define EMIT(z) do { \
84 acc = (acc << SPIGOT_BITS) | (z); \
93 Q( if (n % step == 0) \
94 fputc('.', stderr); ) \
103 uint32 k = max * 2 - 1;
105 for (i = max; i; i--) {
106 x = (b == -1 ? SPIGOT_RADIX * 2 : a[i - 1] << SPIGOT_BITS) + q * i;
108 a[i - 1] = x - q * k;
112 k = x & (SPIGOT_RADIX - 1);
113 if (k == SPIGOT_RADIX - 1)
116 EMIT(p + (x >> SPIGOT_BITS));
118 unsigned d = (x >= SPIGOT_RADIX ? 0 : SPIGOT_RADIX - 1);
129 Q( fputc('\n', stderr); )
138 uint32 dbuf[SPIGOT_WORDS];
142 spigot(d, SPIGOT_WORDS);
147 * Blowfish initial key table [generated]\n\
150 #ifndef CATACOMB_BLOWFISH_TAB_H\n\
151 #define CATACOMB_BLOWFISH_TAB_H\n\
153 #define BLOWFISH_IKEY { \\\n\
156 for (i = 0; i < 18; i++) {
157 printf("0x%08x", *d++);
163 fputs(", \\\n ", stdout);
168 for (j = 0; j < 4; j++) {
169 for (i = 0; i < 256; i++) {
170 printf("0x%08x", *d++);
173 fputs(" } \\\n}\n\n#endif\n", stdout);
178 } else if (i % 4 == 3)
179 fputs(", \\\n ", stdout);
185 if (fclose(stdout)) {
186 fprintf(stderr, "error writing data\n");
192 /*----- That's all, folks -------------------------------------------------*/