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