xref: /netbsd-src/sys/crypto/blowfish/bf_skey.c (revision 95e1ffb15694e54f29f8baaa4232152b703c2a5a)
1*95e1ffb1Schristos /*	$NetBSD: bf_skey.c,v 1.6 2005/12/11 12:20:48 christos Exp $	*/
27236276cSitojun /*	$KAME: bf_skey.c,v 1.5 2000/11/06 13:58:08 itojun Exp $	*/
3599c8087Sthorpej 
4599c8087Sthorpej /* crypto/bf/bf_skey.c */
5599c8087Sthorpej /* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
6599c8087Sthorpej  * All rights reserved.
7599c8087Sthorpej  *
8599c8087Sthorpej  * This package is an SSL implementation written
9599c8087Sthorpej  * by Eric Young (eay@mincom.oz.au).
10599c8087Sthorpej  * The implementation was written so as to conform with Netscapes SSL.
11599c8087Sthorpej  *
12599c8087Sthorpej  * This library is free for commercial and non-commercial use as long as
13599c8087Sthorpej  * the following conditions are aheared to.  The following conditions
14599c8087Sthorpej  * apply to all code found in this distribution, be it the RC4, RSA,
15599c8087Sthorpej  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
16599c8087Sthorpej  * included with this distribution is covered by the same copyright terms
17599c8087Sthorpej  * except that the holder is Tim Hudson (tjh@mincom.oz.au).
18599c8087Sthorpej  *
19599c8087Sthorpej  * Copyright remains Eric Young's, and as such any Copyright notices in
20599c8087Sthorpej  * the code are not to be removed.
21599c8087Sthorpej  * If this package is used in a product, Eric Young should be given attribution
22599c8087Sthorpej  * as the author of the parts of the library used.
23599c8087Sthorpej  * This can be in the form of a textual message at program startup or
24599c8087Sthorpej  * in documentation (online or textual) provided with the package.
25599c8087Sthorpej  *
26599c8087Sthorpej  * Redistribution and use in source and binary forms, with or without
27599c8087Sthorpej  * modification, are permitted provided that the following conditions
28599c8087Sthorpej  * are met:
29599c8087Sthorpej  * 1. Redistributions of source code must retain the copyright
30599c8087Sthorpej  *    notice, this list of conditions and the following disclaimer.
31599c8087Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
32599c8087Sthorpej  *    notice, this list of conditions and the following disclaimer in the
33599c8087Sthorpej  *    documentation and/or other materials provided with the distribution.
34599c8087Sthorpej  * 3. All advertising materials mentioning features or use of this software
35599c8087Sthorpej  *    must display the following acknowledgement:
36599c8087Sthorpej  *    "This product includes cryptographic software written by
37599c8087Sthorpej  *     Eric Young (eay@mincom.oz.au)"
38599c8087Sthorpej  *    The word 'cryptographic' can be left out if the rouines from the library
39599c8087Sthorpej  *    being used are not cryptographic related :-).
40599c8087Sthorpej  * 4. If you include any Windows specific code (or a derivative thereof) from
41599c8087Sthorpej  *    the apps directory (application code) you must include an acknowledgement:
42599c8087Sthorpej  *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
43599c8087Sthorpej  *
44599c8087Sthorpej  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
45599c8087Sthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46599c8087Sthorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47599c8087Sthorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48599c8087Sthorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49599c8087Sthorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50599c8087Sthorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51599c8087Sthorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52599c8087Sthorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53599c8087Sthorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54599c8087Sthorpej  * SUCH DAMAGE.
55599c8087Sthorpej  *
56599c8087Sthorpej  * The licence and distribution terms for any publically available version or
57599c8087Sthorpej  * derivative of this code cannot be changed.  i.e. this code cannot simply be
58599c8087Sthorpej  * copied and put under another distribution licence
59599c8087Sthorpej  * [including the GNU Public Licence.]
60599c8087Sthorpej  */
61599c8087Sthorpej 
624f2ad952Slukem #include <sys/cdefs.h>
63*95e1ffb1Schristos __KERNEL_RCSID(0, "$NetBSD: bf_skey.c,v 1.6 2005/12/11 12:20:48 christos Exp $");
644f2ad952Slukem 
65599c8087Sthorpej #include <sys/types.h>
66599c8087Sthorpej #include <sys/time.h>
677236276cSitojun #ifdef _KERNEL
68599c8087Sthorpej #include <sys/systm.h>
697236276cSitojun #else
707236276cSitojun #include <string.h>
717236276cSitojun #endif
72599c8087Sthorpej #include <crypto/blowfish/blowfish.h>
73599c8087Sthorpej #include <crypto/blowfish/bf_locl.h>
74599c8087Sthorpej #include <crypto/blowfish/bf_pi.h>
75599c8087Sthorpej 
767236276cSitojun void
BF_set_key(BF_KEY * key,int len,const unsigned char * data)77e77423d9Sthorpej BF_set_key(BF_KEY *key, int len, const unsigned char *data)
78599c8087Sthorpej {
79599c8087Sthorpej 	int i;
80599c8087Sthorpej 	BF_LONG *p, ri, in[2];
81e77423d9Sthorpej 	const unsigned char *d, *end;
82599c8087Sthorpej 
83e77423d9Sthorpej 	memcpy(key, &bf_init, sizeof(BF_KEY));
84599c8087Sthorpej 	p = key->P;
85599c8087Sthorpej 
867236276cSitojun 	if (len > ((BF_ROUNDS + 2) * 4))
877236276cSitojun 		len = (BF_ROUNDS + 2) * 4;
88599c8087Sthorpej 
89599c8087Sthorpej 	d = data;
90599c8087Sthorpej 	end= &(data[len]);
917236276cSitojun 	for (i = 0; i < BF_ROUNDS + 2; i++) {
92599c8087Sthorpej 		ri = *(d++);
93599c8087Sthorpej 		if (d >= end) d = data;
94599c8087Sthorpej 
95599c8087Sthorpej 		ri <<= 8;
96599c8087Sthorpej 		ri |= *(d++);
97599c8087Sthorpej 		if (d >= end) d = data;
98599c8087Sthorpej 
99599c8087Sthorpej 		ri <<= 8;
100599c8087Sthorpej 		ri |= *(d++);
101599c8087Sthorpej 		if (d >= end) d = data;
102599c8087Sthorpej 
103599c8087Sthorpej 		ri <<= 8;
104599c8087Sthorpej 		ri |= *(d++);
105599c8087Sthorpej 		if (d >= end) d = data;
106599c8087Sthorpej 
107599c8087Sthorpej 		p[i] ^= ri;
108599c8087Sthorpej 	}
109599c8087Sthorpej 
110599c8087Sthorpej 	in[0] = 0L;
111599c8087Sthorpej 	in[1] = 0L;
1127236276cSitojun 	for (i = 0; i < BF_ROUNDS + 2; i += 2) {
1132ff9b437Sitojun 		BF_encrypt(in, key);
114599c8087Sthorpej 		p[i  ] = in[0];
115599c8087Sthorpej 		p[i+1] = in[1];
116599c8087Sthorpej 	}
117599c8087Sthorpej 
118599c8087Sthorpej 	p = key->S;
1197236276cSitojun 	for (i = 0; i < 4 * 256; i += 2) {
1202ff9b437Sitojun 		BF_encrypt(in, key);
121599c8087Sthorpej 		p[i  ] = in[0];
122599c8087Sthorpej 		p[i+1] = in[1];
123599c8087Sthorpej 	}
124599c8087Sthorpej }
125