| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * The Blowfish block cipher |
| 4 | * |
| 5 | * (c) 1998 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <mLib/bits.h> |
| 31 | |
| 32 | #include "blowfish.h" |
| 33 | #include "blkc.h" |
| 34 | #include "gcipher.h" |
| 35 | #include "paranoia.h" |
| 36 | |
| 37 | /*----- Global variables --------------------------------------------------*/ |
| 38 | |
| 39 | extern const blowfish_ctx blowfish_ikey; |
| 40 | |
| 41 | const octet blowfish_keysz[] = { KSZ_RANGE, BLOWFISH_KEYSZ, 1, 56, 1 }; |
| 42 | |
| 43 | /*----- Macros ------------------------------------------------------------*/ |
| 44 | |
| 45 | #define ROUND(k, x, y, r) do { \ |
| 46 | x ^= *r; \ |
| 47 | y ^= ((k->s0[U8(x >> 24)] + \ |
| 48 | k->s1[U8(x >> 16)]) ^ \ |
| 49 | k->s2[U8(x >> 8)]) + \ |
| 50 | k->s3[U8(x >> 0)]; \ |
| 51 | } while (0) |
| 52 | |
| 53 | #define EBLK(k, a, b, c, d) do { \ |
| 54 | const uint32 *_r = k->p; \ |
| 55 | uint32 _x = a; \ |
| 56 | uint32 _y = b; \ |
| 57 | ROUND(k, _x, _y, _r++); \ |
| 58 | ROUND(k, _y, _x, _r++); \ |
| 59 | ROUND(k, _x, _y, _r++); \ |
| 60 | ROUND(k, _y, _x, _r++); \ |
| 61 | ROUND(k, _x, _y, _r++); \ |
| 62 | ROUND(k, _y, _x, _r++); \ |
| 63 | ROUND(k, _x, _y, _r++); \ |
| 64 | ROUND(k, _y, _x, _r++); \ |
| 65 | ROUND(k, _x, _y, _r++); \ |
| 66 | ROUND(k, _y, _x, _r++); \ |
| 67 | ROUND(k, _x, _y, _r++); \ |
| 68 | ROUND(k, _y, _x, _r++); \ |
| 69 | ROUND(k, _x, _y, _r++); \ |
| 70 | ROUND(k, _y, _x, _r++); \ |
| 71 | ROUND(k, _x, _y, _r++); \ |
| 72 | ROUND(k, _y, _x, _r++); \ |
| 73 | c = _y ^ k->p[17]; \ |
| 74 | d = _x ^ k->p[16]; \ |
| 75 | } while (0) |
| 76 | |
| 77 | #define DBLK(k, a, b, c, d) do { \ |
| 78 | const uint32 *_r = k->p + 18; \ |
| 79 | uint32 _x = a; \ |
| 80 | uint32 _y = b; \ |
| 81 | ROUND(k, _x, _y, --_r); \ |
| 82 | ROUND(k, _y, _x, --_r); \ |
| 83 | ROUND(k, _x, _y, --_r); \ |
| 84 | ROUND(k, _y, _x, --_r); \ |
| 85 | ROUND(k, _x, _y, --_r); \ |
| 86 | ROUND(k, _y, _x, --_r); \ |
| 87 | ROUND(k, _x, _y, --_r); \ |
| 88 | ROUND(k, _y, _x, --_r); \ |
| 89 | ROUND(k, _x, _y, --_r); \ |
| 90 | ROUND(k, _y, _x, --_r); \ |
| 91 | ROUND(k, _x, _y, --_r); \ |
| 92 | ROUND(k, _y, _x, --_r); \ |
| 93 | ROUND(k, _x, _y, --_r); \ |
| 94 | ROUND(k, _y, _x, --_r); \ |
| 95 | ROUND(k, _x, _y, --_r); \ |
| 96 | ROUND(k, _y, _x, --_r); \ |
| 97 | c = _y ^ k->p[0]; \ |
| 98 | d = _x ^ k->p[1]; \ |
| 99 | } while (0) |
| 100 | |
| 101 | /*----- Low-level encryption interface ------------------------------------*/ |
| 102 | |
| 103 | /* --- @blowfish_init@ --- * |
| 104 | * |
| 105 | * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in |
| 106 | * @const void *buf@ = pointer to buffer of key material |
| 107 | * @size_t sz@ = size of key material |
| 108 | * |
| 109 | * Returns: --- |
| 110 | * |
| 111 | * Use: Initializes a Blowfish key buffer. Blowfish accepts |
| 112 | * a more-or-less arbitrary size key. |
| 113 | */ |
| 114 | |
| 115 | void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz) |
| 116 | { |
| 117 | KSZ_ASSERT(blowfish, sz); |
| 118 | |
| 119 | /* --- Copy the initial value over --- */ |
| 120 | |
| 121 | *k = blowfish_ikey; |
| 122 | |
| 123 | /* --- Initialize the %$P$% array --- */ |
| 124 | |
| 125 | { |
| 126 | const octet *p = buf; |
| 127 | const octet *q = p + sz; |
| 128 | int i = 0, j = 0; |
| 129 | uint32 x = 0; |
| 130 | |
| 131 | while (i < 18) { |
| 132 | x = (x << 8) | U8(*p++); |
| 133 | if (p >= q) |
| 134 | p = buf; |
| 135 | if (++j >= 4) { |
| 136 | k->p[i++] ^= x; |
| 137 | x = 0; |
| 138 | j = 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | x = 0; |
| 143 | } |
| 144 | |
| 145 | /* --- Now mangle the complete array of keys --- */ |
| 146 | |
| 147 | { |
| 148 | uint32 b[2]; |
| 149 | int i; |
| 150 | |
| 151 | b[0] = b[1] = 0; |
| 152 | |
| 153 | for (i = 0; i < 18; i += 2) { |
| 154 | blowfish_eblk(k, b, b); |
| 155 | k->p[i] = b[0]; k->p[i + 1] = b[1]; |
| 156 | } |
| 157 | |
| 158 | for (i = 0; i < 256; i += 2) { |
| 159 | blowfish_eblk(k, b, b); |
| 160 | k->s0[i] = b[0]; k->s0[i + 1] = b[1]; |
| 161 | } |
| 162 | |
| 163 | for (i = 0; i < 256; i += 2) { |
| 164 | blowfish_eblk(k, b, b); |
| 165 | k->s1[i] = b[0]; k->s1[i + 1] = b[1]; |
| 166 | } |
| 167 | |
| 168 | for (i = 0; i < 256; i += 2) { |
| 169 | blowfish_eblk(k, b, b); |
| 170 | k->s2[i] = b[0]; k->s2[i + 1] = b[1]; |
| 171 | } |
| 172 | |
| 173 | for (i = 0; i < 256; i += 2) { |
| 174 | blowfish_eblk(k, b, b); |
| 175 | k->s3[i] = b[0]; k->s3[i + 1] = b[1]; |
| 176 | } |
| 177 | |
| 178 | BURN(b); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* --- @blowfish_eblk@, @blowfish_dblk@ --- * |
| 183 | * |
| 184 | * Arguments: @const blowfish_ctx *k@ = pointer to key block |
| 185 | * @const uint32 s[2]@ = pointer to source block |
| 186 | * @uint32 d[2]@ = pointer to destination block |
| 187 | * |
| 188 | * Returns: --- |
| 189 | * |
| 190 | * Use: Low-level block encryption and decryption. |
| 191 | */ |
| 192 | |
| 193 | void blowfish_eblk(const blowfish_ctx *k, const uint32 *s, uint32 *d) |
| 194 | { |
| 195 | EBLK(k, s[0], s[1], d[0], d[1]); |
| 196 | } |
| 197 | |
| 198 | void blowfish_dblk(const blowfish_ctx *k, const uint32 *s, uint32 *d) |
| 199 | { |
| 200 | DBLK(k, s[0], s[1], d[0], d[1]); |
| 201 | } |
| 202 | |
| 203 | BLKC_TEST(BLOWFISH, blowfish) |
| 204 | |
| 205 | /*----- That's all, folks -------------------------------------------------*/ |