| 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 | /*----- Notes on the Blowfish block cipher --------------------------------* |
| 29 | * |
| 30 | * Blowfish was invented by Bruce Schneier. The algorithm is unpatented and |
| 31 | * free for anyone to use. It's fast, simple, offers a big key, and is |
| 32 | * looking relatively bulletproof. It's also this author's block cipher of |
| 33 | * choice, for what little that's worth. The disadvantage is that Blowfish |
| 34 | * has a particularly heavyweight key schedule. |
| 35 | */ |
| 36 | |
| 37 | #ifndef CATACOMB_BLOWFISH_H |
| 38 | #define CATACOMB_BLOWFISH_H |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /*----- Header files ------------------------------------------------------*/ |
| 45 | |
| 46 | #include <stddef.h> |
| 47 | |
| 48 | #include <mLib/bits.h> |
| 49 | |
| 50 | /*----- Magical numbers ---------------------------------------------------*/ |
| 51 | |
| 52 | #define BLOWFISH_BLKSZ 8 |
| 53 | #define BLOWFISH_KEYSZ 32 |
| 54 | #define BLOWFISH_CLASS (N, B, 64) |
| 55 | |
| 56 | extern const octet blowfish_keysz[]; |
| 57 | |
| 58 | /*----- Data structures ---------------------------------------------------*/ |
| 59 | |
| 60 | typedef struct blowfish_ctx { |
| 61 | uint32 p[18]; |
| 62 | uint32 s0[256], s1[256], s2[256], s3[256]; |
| 63 | } blowfish_ctx; |
| 64 | |
| 65 | /*----- Functions provided ------------------------------------------------*/ |
| 66 | |
| 67 | /* --- @blowfish_init@ --- * |
| 68 | * |
| 69 | * Arguments: @blowfish_ctx *k@ = pointer to key block to fill in |
| 70 | * @const void *buf@ = pointer to buffer of key material |
| 71 | * @size_t sz@ = size of key material |
| 72 | * |
| 73 | * Returns: --- |
| 74 | * |
| 75 | * Use: Initializes a Blowfish key buffer. Blowfish accepts |
| 76 | * a more-or-less arbitrary size key. |
| 77 | */ |
| 78 | |
| 79 | extern void blowfish_init(blowfish_ctx */*k*/, |
| 80 | const void */*buf*/, size_t /*sz*/); |
| 81 | |
| 82 | /* --- @blowfish_eblk@, @blowfish_dblk@ --- * |
| 83 | * |
| 84 | * Arguments: @const blowfish_ctx *k@ = pointer to key block |
| 85 | * @const uint32 s[2]@ = pointer to source block |
| 86 | * @uint32 d[2]@ = pointer to destination block |
| 87 | * |
| 88 | * Returns: --- |
| 89 | * |
| 90 | * Use: Low-level block encryption and decryption. |
| 91 | */ |
| 92 | |
| 93 | extern void blowfish_eblk(const blowfish_ctx */*k*/, |
| 94 | const uint32 */*s*/, uint32 */*d*/); |
| 95 | |
| 96 | extern void blowfish_dblk(const blowfish_ctx */*k*/, |
| 97 | const uint32 */*s*/, uint32 */*d*/); |
| 98 | |
| 99 | /*----- That's all, folks -------------------------------------------------*/ |
| 100 | |
| 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | |
| 105 | #endif |