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