xref: /openbsd-src/sys/crypto/blf.h (revision 832bedbc19acba9d33aa4d33e3d47fe2719559fb)
1*832bedbcSdjm /*	$OpenBSD: blf.h,v 1.7 2021/11/29 01:04:45 djm Exp $	*/
29b0b114fSderaadt 
321f2d90fSderaadt /*
421f2d90fSderaadt  * Blowfish - a fast block cipher designed by Bruce Schneier
521f2d90fSderaadt  *
621f2d90fSderaadt  * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
721f2d90fSderaadt  * All rights reserved.
821f2d90fSderaadt  *
921f2d90fSderaadt  * Redistribution and use in source and binary forms, with or without
1021f2d90fSderaadt  * modification, are permitted provided that the following conditions
1121f2d90fSderaadt  * are met:
1221f2d90fSderaadt  * 1. Redistributions of source code must retain the above copyright
1321f2d90fSderaadt  *    notice, this list of conditions and the following disclaimer.
1421f2d90fSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1521f2d90fSderaadt  *    notice, this list of conditions and the following disclaimer in the
1621f2d90fSderaadt  *    documentation and/or other materials provided with the distribution.
17*832bedbcSdjm  * 3. The name of the author may not be used to endorse or promote products
1821f2d90fSderaadt  *    derived from this software without specific prior written permission.
1921f2d90fSderaadt  *
2021f2d90fSderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2121f2d90fSderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2221f2d90fSderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2321f2d90fSderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2421f2d90fSderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2521f2d90fSderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2621f2d90fSderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2721f2d90fSderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2821f2d90fSderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2921f2d90fSderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3021f2d90fSderaadt  */
3121f2d90fSderaadt 
3221f2d90fSderaadt #ifndef _BLF_H_
3321f2d90fSderaadt #define _BLF_H_
3421f2d90fSderaadt 
3521f2d90fSderaadt /* Schneier states the maximum key length to be 56 bytes.
36f92e66c2Snate  * The way how the subkeys are initialized by the key up
3721f2d90fSderaadt  * to (N+2)*4 i.e. 72 bytes are utilized.
3821f2d90fSderaadt  * Warning: For normal blowfish encryption only 56 bytes
3921f2d90fSderaadt  * of the key affect all cipherbits.
4021f2d90fSderaadt  */
4121f2d90fSderaadt 
4221f2d90fSderaadt #define BLF_N	16			/* Number of Subkeys */
4321f2d90fSderaadt #define BLF_MAXKEYLEN ((BLF_N-2)*4)	/* 448 bits */
44549b4f6eSgrunk #define BLF_MAXUTILIZED ((BLF_N+2)*4)	/* 576 bits */
4521f2d90fSderaadt 
4621f2d90fSderaadt /* Blowfish context */
4721f2d90fSderaadt typedef struct BlowfishContext {
4821f2d90fSderaadt 	u_int32_t S[4][256];	/* S-Boxes */
4921f2d90fSderaadt 	u_int32_t P[BLF_N + 2];	/* Subkeys */
5021f2d90fSderaadt } blf_ctx;
5121f2d90fSderaadt 
5221f2d90fSderaadt /* Raw access to customized Blowfish
5321f2d90fSderaadt  *	blf_key is just:
5421f2d90fSderaadt  *	Blowfish_initstate( state )
5521f2d90fSderaadt  *	Blowfish_expand0state( state, key, keylen )
5621f2d90fSderaadt  */
5721f2d90fSderaadt 
58c4071fd1Smillert void Blowfish_encipher(blf_ctx *, u_int32_t *);
59c4071fd1Smillert void Blowfish_decipher(blf_ctx *, u_int32_t *);
60c4071fd1Smillert void Blowfish_initstate(blf_ctx *);
61c4071fd1Smillert void Blowfish_expand0state(blf_ctx *, const u_int8_t *, u_int16_t);
62c4071fd1Smillert void Blowfish_expandstate(blf_ctx *, const u_int8_t *, u_int16_t, const u_int8_t *, u_int16_t);
6321f2d90fSderaadt 
6421f2d90fSderaadt /* Standard Blowfish */
6521f2d90fSderaadt 
66c4071fd1Smillert void blf_key(blf_ctx *, const u_int8_t *, u_int16_t);
67c4071fd1Smillert void blf_enc(blf_ctx *, u_int32_t *, u_int16_t);
68c4071fd1Smillert void blf_dec(blf_ctx *, u_int32_t *, u_int16_t);
6921f2d90fSderaadt 
7021f2d90fSderaadt /* Converts u_int8_t to u_int32_t */
71c4071fd1Smillert u_int32_t Blowfish_stream2word(const u_int8_t *, u_int16_t ,
72c4071fd1Smillert 				    u_int16_t *);
7321f2d90fSderaadt 
74c4071fd1Smillert void blf_ecb_encrypt(blf_ctx *, u_int8_t *, u_int32_t);
75c4071fd1Smillert void blf_ecb_decrypt(blf_ctx *, u_int8_t *, u_int32_t);
765fc5d121Sprovos 
77c4071fd1Smillert void blf_cbc_encrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
78c4071fd1Smillert void blf_cbc_decrypt(blf_ctx *, u_int8_t *, u_int8_t *, u_int32_t);
7921f2d90fSderaadt #endif
80