3 * Build Blowfish key table
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
35 /*----- Main code ---------------------------------------------------------*/
39 * Arguments: @uint32 *buf@ = pointer to the output buffer
40 * @size_t n@ = number of output digits wanted
44 * Use: Writes digits of %$\pi$% to the given array. The algorithm
45 * is based on the Spigot algorithm by Stanley Rabinowitz and
46 * Stan Wagon, published in Amer.Math.Monthly, March 1995, with
47 * bug fixes by C. Haenel. I then bodged it to output hex
48 * digits rather than decimal ones, and to leave off the initial
51 * I've not analysed the algorithm very much.
54 #define SPIGOT_WORDS (18 + 4 * 256ul)
56 #define SPIGOT_RADIX (1ul << SPIGOT_BITS)
57 #define SPIGOT_BUFLEN (SPIGOT_WORDS * 32)
65 static void spigot(uint32 *buf, size_t n)
69 unsigned a[SPIGOT_BUFLEN] = { 0 };
72 unsigned max = 32 * n;
73 Q( size_t step = n / 60; )
78 #define EMIT(z) do { \
82 acc = (acc << SPIGOT_BITS) | (z); \
91 Q( if (n % step == 0) \
92 fputc('.', stderr); ) \
101 uint32 k = max * 2 - 1;
103 for (i = max; i; i--) {
104 x = (b == -1 ? SPIGOT_RADIX * 2 : a[i - 1] << SPIGOT_BITS) + q * i;
106 a[i - 1] = x - q * k;
110 k = x & (SPIGOT_RADIX - 1);
111 if (k == SPIGOT_RADIX - 1)
114 EMIT(p + (x >> SPIGOT_BITS));
116 unsigned d = (x >= SPIGOT_RADIX ? 0 : SPIGOT_RADIX - 1);
127 Q( fputc('\n', stderr); )
136 uint32 dbuf[SPIGOT_WORDS];
140 spigot(d, SPIGOT_WORDS);
145 * Blowfish initial key table [generated]\n\
148 #ifndef CATACOMB_BLOWFISH_TAB_H\n\
149 #define CATACOMB_BLOWFISH_TAB_H\n\
151 #define BLOWFISH_IKEY { \\\n\
154 for (i = 0; i < 18; i++) {
155 printf("0x%08x", *d++);
161 fputs(", \\\n ", stdout);
166 for (j = 0; j < 4; j++) {
167 for (i = 0; i < 256; i++) {
168 printf("0x%08x", *d++);
171 fputs(" } \\\n}\n\n#endif\n", stdout);
176 } else if (i % 4 == 3)
177 fputs(", \\\n ", stdout);
183 if (fclose(stdout)) {
184 fprintf(stderr, "error writing data\n");
190 /*----- That's all, folks -------------------------------------------------*/