00001 /* $OpenBSD: blf.h,v 1.7 2007/03/14 17:59:41 grunk Exp $ */ 00002 /* 00003 * Blowfish - a fast block cipher designed by Bruce Schneier 00004 * 00005 * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 1. Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in the 00015 * documentation and/or other materials provided with the distribution. 00016 * 3. All advertising materials mentioning features or use of this software 00017 * must display the following acknowledgement: 00018 * This product includes software developed by Niels Provos. 00019 * 4. The name of the author may not be used to endorse or promote products 00020 * derived from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00023 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00025 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 00027 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00028 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00029 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00030 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00031 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 #ifndef _BLF_H_ 00035 #define _BLF_H_ 00036 00037 //#include "includes.h" 00038 00039 #if !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) 00040 00041 /* Schneier specifies a maximum key length of 56 bytes. 00042 * This ensures that every key bit affects every cipher 00043 * bit. However, the subkeys can hold up to 72 bytes. 00044 * Warning: For normal blowfish encryption only 56 bytes 00045 * of the key affect all cipherbits. 00046 */ 00047 00048 #define BLF_N 16 /* Number of Subkeys */ 00049 #define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */ 00050 #define BLF_MAXUTILIZED ((BLF_N+2)*4) /* 576 bits */ 00051 00052 /* Blowfish context */ 00053 typedef struct BlowfishContext { 00054 uint32_t S[4][256]; /* S-Boxes */ 00055 uint32_t P[BLF_N + 2]; /* Subkeys */ 00056 } blf_ctx; 00057 00058 /* Raw access to customized Blowfish 00059 * blf_key is just: 00060 * Blowfish_initstate( state ) 00061 * Blowfish_expand0state( state, key, keylen ) 00062 */ 00063 00064 void Blowfish_encipher(blf_ctx *, uint32_t *, uint32_t *); 00065 void Blowfish_decipher(blf_ctx *, uint32_t *, uint32_t *); 00066 void Blowfish_initstate(blf_ctx *); 00067 void Blowfish_expand0state(blf_ctx *, const uint8_t *, uint16_t); 00068 void Blowfish_expandstate 00069 (blf_ctx *, const uint8_t *, uint16_t, const uint8_t *, uint16_t); 00070 00071 /* Standard Blowfish */ 00072 00073 void blf_key(blf_ctx *, const uint8_t *, uint16_t); 00074 void blf_enc(blf_ctx *, uint32_t *, uint16_t); 00075 void blf_dec(blf_ctx *, uint32_t *, uint16_t); 00076 00077 void blf_ecb_encrypt(blf_ctx *, uint8_t *, uint32_t); 00078 void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t); 00079 00080 void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); 00081 void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); 00082 00083 /* Converts uint8_t to uint32_t */ 00084 uint32_t Blowfish_stream2word(const uint8_t *, uint16_t , uint16_t *); 00085 00086 #endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */ 00087 #endif /* _BLF_H */