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