1261287Sdes/* $OpenBSD: blf.h,v 1.7 2007/03/14 17:59:41 grunk Exp $ */ 2261287Sdes/* 3261287Sdes * Blowfish - a fast block cipher designed by Bruce Schneier 4261287Sdes * 5261287Sdes * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> 6261287Sdes * All rights reserved. 7261287Sdes * 8261287Sdes * Redistribution and use in source and binary forms, with or without 9261287Sdes * modification, are permitted provided that the following conditions 10261287Sdes * are met: 11261287Sdes * 1. Redistributions of source code must retain the above copyright 12261287Sdes * notice, this list of conditions and the following disclaimer. 13261287Sdes * 2. Redistributions in binary form must reproduce the above copyright 14261287Sdes * notice, this list of conditions and the following disclaimer in the 15261287Sdes * documentation and/or other materials provided with the distribution. 16261287Sdes * 3. All advertising materials mentioning features or use of this software 17261287Sdes * must display the following acknowledgement: 18261287Sdes * This product includes software developed by Niels Provos. 19261287Sdes * 4. The name of the author may not be used to endorse or promote products 20261287Sdes * derived from this software without specific prior written permission. 21261287Sdes * 22261287Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23261287Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24261287Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25261287Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26261287Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27261287Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28261287Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29261287Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30261287Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31261287Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32261287Sdes */ 33261287Sdes 34261287Sdes#ifndef _BLF_H_ 35261287Sdes#define _BLF_H_ 36261287Sdes 37261287Sdes#include "includes.h" 38261287Sdes 39261287Sdes#if !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) 40261287Sdes 41261287Sdes/* Schneier specifies a maximum key length of 56 bytes. 42261287Sdes * This ensures that every key bit affects every cipher 43261287Sdes * bit. However, the subkeys can hold up to 72 bytes. 44261287Sdes * Warning: For normal blowfish encryption only 56 bytes 45261287Sdes * of the key affect all cipherbits. 46261287Sdes */ 47261287Sdes 48261287Sdes#define BLF_N 16 /* Number of Subkeys */ 49261287Sdes#define BLF_MAXKEYLEN ((BLF_N-2)*4) /* 448 bits */ 50261287Sdes#define BLF_MAXUTILIZED ((BLF_N+2)*4) /* 576 bits */ 51261287Sdes 52261287Sdes/* Blowfish context */ 53261287Sdestypedef struct BlowfishContext { 54261287Sdes u_int32_t S[4][256]; /* S-Boxes */ 55261287Sdes u_int32_t P[BLF_N + 2]; /* Subkeys */ 56261287Sdes} blf_ctx; 57261287Sdes 58261287Sdes/* Raw access to customized Blowfish 59261287Sdes * blf_key is just: 60261287Sdes * Blowfish_initstate( state ) 61261287Sdes * Blowfish_expand0state( state, key, keylen ) 62261287Sdes */ 63261287Sdes 64261287Sdesvoid Blowfish_encipher(blf_ctx *, u_int32_t *, u_int32_t *); 65261287Sdesvoid Blowfish_decipher(blf_ctx *, u_int32_t *, u_int32_t *); 66261287Sdesvoid Blowfish_initstate(blf_ctx *); 67261287Sdesvoid Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t); 68261287Sdesvoid Blowfish_expandstate 69261287Sdes(blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t); 70261287Sdes 71261287Sdes/* Standard Blowfish */ 72261287Sdes 73261287Sdesvoid blf_key(blf_ctx *, const u_int8_t *, u_int16_t); 74261287Sdesvoid blf_enc(blf_ctx *, u_int32_t *, u_int16_t); 75261287Sdesvoid blf_dec(blf_ctx *, u_int32_t *, u_int16_t); 76261287Sdes 77261287Sdesvoid blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t); 78261287Sdesvoid blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t); 79261287Sdes 80261287Sdesvoid blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); 81261287Sdesvoid blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t); 82261287Sdes 83261287Sdes/* Converts u_int8_t to u_int32_t */ 84261287Sdesu_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t , u_int16_t *); 85261287Sdes 86261287Sdes#endif /* !defined(HAVE_BCRYPT_PBKDF) && !defined(HAVE_BLH_H) */ 87261287Sdes#endif /* _BLF_H */ 88261287Sdes 89