xref: /openbsd/sys/lib/libsa/blowfish.h (revision 832bedbc)
1*832bedbcSdjm /* $OpenBSD: blowfish.h,v 1.2 2021/11/29 01:04:45 djm Exp $ */
2695e0c08Sjsing /*
3695e0c08Sjsing  * Blowfish - a fast block cipher designed by Bruce Schneier
4695e0c08Sjsing  *
5695e0c08Sjsing  * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
6695e0c08Sjsing  * All rights reserved.
7695e0c08Sjsing  *
8695e0c08Sjsing  * Redistribution and use in source and binary forms, with or without
9695e0c08Sjsing  * modification, are permitted provided that the following conditions
10695e0c08Sjsing  * are met:
11695e0c08Sjsing  * 1. Redistributions of source code must retain the above copyright
12695e0c08Sjsing  *    notice, this list of conditions and the following disclaimer.
13695e0c08Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
14695e0c08Sjsing  *    notice, this list of conditions and the following disclaimer in the
15695e0c08Sjsing  *    documentation and/or other materials provided with the distribution.
16*832bedbcSdjm  * 3. The name of the author may not be used to endorse or promote products
17695e0c08Sjsing  *    derived from this software without specific prior written permission.
18695e0c08Sjsing  *
19695e0c08Sjsing  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20695e0c08Sjsing  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21695e0c08Sjsing  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22695e0c08Sjsing  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23695e0c08Sjsing  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24695e0c08Sjsing  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25695e0c08Sjsing  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26695e0c08Sjsing  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27695e0c08Sjsing  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28695e0c08Sjsing  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29695e0c08Sjsing  */
30695e0c08Sjsing 
31695e0c08Sjsing #ifndef _BLF_H_
32695e0c08Sjsing #define _BLF_H_
33695e0c08Sjsing 
34695e0c08Sjsing /* Schneier specifies a maximum key length of 56 bytes.
35695e0c08Sjsing  * This ensures that every key bit affects every cipher
36695e0c08Sjsing  * bit.  However, the subkeys can hold up to 72 bytes.
37695e0c08Sjsing  * Warning: For normal blowfish encryption only 56 bytes
38695e0c08Sjsing  * of the key affect all cipherbits.
39695e0c08Sjsing  */
40695e0c08Sjsing 
41695e0c08Sjsing #define BLF_N	16			/* Number of Subkeys */
42695e0c08Sjsing #define BLF_MAXKEYLEN ((BLF_N-2)*4)	/* 448 bits */
43695e0c08Sjsing #define BLF_MAXUTILIZED ((BLF_N+2)*4)	/* 576 bits */
44695e0c08Sjsing 
45695e0c08Sjsing /* Blowfish context */
46695e0c08Sjsing typedef struct BlowfishContext {
47695e0c08Sjsing 	u_int32_t S[4][256];	/* S-Boxes */
48695e0c08Sjsing 	u_int32_t P[BLF_N + 2];	/* Subkeys */
49695e0c08Sjsing } blf_ctx;
50695e0c08Sjsing 
51695e0c08Sjsing /* Raw access to customized Blowfish
52695e0c08Sjsing  *	blf_key is just:
53695e0c08Sjsing  *	Blowfish_initstate( state )
54695e0c08Sjsing  *	Blowfish_expand0state( state, key, keylen )
55695e0c08Sjsing  */
56695e0c08Sjsing 
57695e0c08Sjsing void Blowfish_encipher(blf_ctx *, u_int32_t *, u_int32_t *);
58695e0c08Sjsing void Blowfish_decipher(blf_ctx *, u_int32_t *, u_int32_t *);
59695e0c08Sjsing void Blowfish_initstate(blf_ctx *);
60695e0c08Sjsing void Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t);
61695e0c08Sjsing void Blowfish_expandstate
62695e0c08Sjsing (blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t);
63695e0c08Sjsing 
64695e0c08Sjsing /* Standard Blowfish */
65695e0c08Sjsing 
66695e0c08Sjsing void blf_key(blf_ctx *, const u_int8_t *, u_int16_t);
67695e0c08Sjsing void blf_enc(blf_ctx *, u_int32_t *, u_int16_t);
68695e0c08Sjsing void blf_dec(blf_ctx *, u_int32_t *, u_int16_t);
69695e0c08Sjsing 
70695e0c08Sjsing void blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t);
71695e0c08Sjsing void blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t);
72695e0c08Sjsing 
73695e0c08Sjsing void blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
74695e0c08Sjsing void blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
75695e0c08Sjsing 
76695e0c08Sjsing /* Converts u_int8_t to u_int32_t */
77695e0c08Sjsing u_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t , u_int16_t *);
78695e0c08Sjsing 
79695e0c08Sjsing #endif
80