1*1323ec57SEd Maste /* $OpenBSD: blf.h,v 1.8 2021/11/29 01:04:45 djm Exp $ */ 2f7167e0eSDag-Erling Smørgrav /* 3f7167e0eSDag-Erling Smørgrav * Blowfish - a fast block cipher designed by Bruce Schneier 4f7167e0eSDag-Erling Smørgrav * 5f7167e0eSDag-Erling Smørgrav * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 6f7167e0eSDag-Erling Smørgrav * All rights reserved. 7f7167e0eSDag-Erling Smørgrav * 8f7167e0eSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 9f7167e0eSDag-Erling Smørgrav * modification, are permitted provided that the following conditions 10f7167e0eSDag-Erling Smørgrav * are met: 11f7167e0eSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 12f7167e0eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 13f7167e0eSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 14f7167e0eSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 15f7167e0eSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 16*1323ec57SEd Maste * 3. The name of the author may not be used to endorse or promote products 17f7167e0eSDag-Erling Smørgrav * derived from this software without specific prior written permission. 18f7167e0eSDag-Erling Smørgrav * 19f7167e0eSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20f7167e0eSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21f7167e0eSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22f7167e0eSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23f7167e0eSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24f7167e0eSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25f7167e0eSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26f7167e0eSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27f7167e0eSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28f7167e0eSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29f7167e0eSDag-Erling Smørgrav */ 30f7167e0eSDag-Erling Smørgrav 31f7167e0eSDag-Erling Smørgrav #ifndef _BLF_H_ 32f7167e0eSDag-Erling Smørgrav #define _BLF_H_ 33f7167e0eSDag-Erling Smørgrav 34f7167e0eSDag-Erling Smørgrav #include "includes.h" 35f7167e0eSDag-Erling Smørgrav 36f7167e0eSDag-Erling Smørgrav #if !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) 37f7167e0eSDag-Erling Smørgrav 38f7167e0eSDag-Erling Smørgrav /* Schneier specifies a maximum key length of 56 bytes. 39f7167e0eSDag-Erling Smørgrav * This ensures that every key bit affects every cipher 40f7167e0eSDag-Erling Smørgrav * bit. However, the subkeys can hold up to 72 bytes. 41f7167e0eSDag-Erling Smørgrav * Warning: For normal blowfish encryption only 56 bytes 42f7167e0eSDag-Erling Smørgrav * of the key affect all cipherbits. 43f7167e0eSDag-Erling Smørgrav */ 44f7167e0eSDag-Erling Smørgrav 45f7167e0eSDag-Erling Smørgrav #define BLF_N 16 /* Number of Subkeys */ 46f7167e0eSDag-Erling Smørgrav #define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */ 47f7167e0eSDag-Erling Smørgrav #define BLF_MAXUTILIZED ((BLF_N+2)*4) /* 576 bits */ 48f7167e0eSDag-Erling Smørgrav 49f7167e0eSDag-Erling Smørgrav /* Blowfish context */ 50f7167e0eSDag-Erling Smørgrav typedef struct BlowfishContext { 51f7167e0eSDag-Erling Smørgrav u_int32_t S[4][256]; /* S-Boxes */ 52f7167e0eSDag-Erling Smørgrav u_int32_t P[BLF_N + 2]; /* Subkeys */ 53f7167e0eSDag-Erling Smørgrav } blf_ctx; 54f7167e0eSDag-Erling Smørgrav 55f7167e0eSDag-Erling Smørgrav /* Raw access to customized Blowfish 56f7167e0eSDag-Erling Smørgrav * blf_key is just: 57f7167e0eSDag-Erling Smørgrav * Blowfish_initstate( state ) 58f7167e0eSDag-Erling Smørgrav * Blowfish_expand0state( state, key, keylen ) 59f7167e0eSDag-Erling Smørgrav */ 60f7167e0eSDag-Erling Smørgrav 61f7167e0eSDag-Erling Smørgrav void Blowfish_encipher(blf_ctx *, u_int32_t *, u_int32_t *); 62f7167e0eSDag-Erling Smørgrav void Blowfish_decipher(blf_ctx *, u_int32_t *, u_int32_t *); 63f7167e0eSDag-Erling Smørgrav void Blowfish_initstate(blf_ctx *); 64f7167e0eSDag-Erling Smørgrav void Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t); 65f7167e0eSDag-Erling Smørgrav void Blowfish_expandstate 66f7167e0eSDag-Erling Smørgrav (blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t); 67f7167e0eSDag-Erling Smørgrav 68f7167e0eSDag-Erling Smørgrav /* Standard Blowfish */ 69f7167e0eSDag-Erling Smørgrav 70f7167e0eSDag-Erling Smørgrav void blf_key(blf_ctx *, const u_int8_t *, u_int16_t); 71f7167e0eSDag-Erling Smørgrav void blf_enc(blf_ctx *, u_int32_t *, u_int16_t); 72f7167e0eSDag-Erling Smørgrav void blf_dec(blf_ctx *, u_int32_t *, u_int16_t); 73f7167e0eSDag-Erling Smørgrav 74f7167e0eSDag-Erling Smørgrav void blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t); 75f7167e0eSDag-Erling Smørgrav void blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t); 76f7167e0eSDag-Erling Smørgrav 77f7167e0eSDag-Erling Smørgrav void blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); 78f7167e0eSDag-Erling Smørgrav void blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); 79f7167e0eSDag-Erling Smørgrav 80f7167e0eSDag-Erling Smørgrav /* Converts u_int8_t to u_int32_t */ 81f7167e0eSDag-Erling Smørgrav u_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t , u_int16_t *); 82f7167e0eSDag-Erling Smørgrav 83f7167e0eSDag-Erling Smørgrav #endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */ 84f7167e0eSDag-Erling Smørgrav #endif /* _BLF_H */ 85f7167e0eSDag-Erling Smørgrav 86