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