bf_skey.c revision 116174
1228753Smm/*	$KAME: bf_skey.c,v 1.7 2002/02/27 01:33:59 itojun Exp $	*/
2228753Smm
3228753Smm/* crypto/bf/bf_skey.c */
4228753Smm
5228753Smm/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
6228753Smm * All rights reserved.
7228753Smm *
8228753Smm * This package is an SSL implementation written
9228753Smm * by Eric Young (eay@mincom.oz.au).
10228753Smm * The implementation was written so as to conform with Netscapes SSL.
11228753Smm *
12228753Smm * This library is free for commercial and non-commercial use as long as
13228753Smm * the following conditions are aheared to.  The following conditions
14228753Smm * apply to all code found in this distribution, be it the RC4, RSA,
15228753Smm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
16228753Smm * included with this distribution is covered by the same copyright terms
17228753Smm * except that the holder is Tim Hudson (tjh@mincom.oz.au).
18228753Smm *
19228753Smm * Copyright remains Eric Young's, and as such any Copyright notices in
20228753Smm * the code are not to be removed.
21228753Smm * If this package is used in a product, Eric Young should be given attribution
22228753Smm * as the author of the parts of the library used.
23228753Smm * This can be in the form of a textual message at program startup or
24228753Smm * in documentation (online or textual) provided with the package.
25228753Smm *
26228763Smm * Redistribution and use in source and binary forms, with or without
27228753Smm * modification, are permitted provided that the following conditions
28228753Smm * are met:
29228753Smm * 1. Redistributions of source code must retain the copyright
30228753Smm *    notice, this list of conditions and the following disclaimer.
31228753Smm * 2. Redistributions in binary form must reproduce the above copyright
32228776Smm *    notice, this list of conditions and the following disclaimer in the
33228753Smm *    documentation and/or other materials provided with the distribution.
34228753Smm * 3. All advertising materials mentioning features or use of this software
35232153Smm *    must display the following acknowledgement:
36232153Smm *    "This product includes cryptographic software written by
37232153Smm *     Eric Young (eay@mincom.oz.au)"
38232153Smm *    The word 'cryptographic' can be left out if the rouines from the library
39348608Smm *    being used are not cryptographic related :-).
40232153Smm * 4. If you include any Windows specific code (or a derivative thereof) from
41232153Smm *    the apps directory (application code) you must include an acknowledgement:
42232153Smm *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
43228753Smm *
44232153Smm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
45228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47228753Smm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48228753Smm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52228753Smm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53228753Smm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54228753Smm * SUCH DAMAGE.
55228753Smm *
56228753Smm * The licence and distribution terms for any publically available version or
57228753Smm * derivative of this code cannot be changed.  i.e. this code cannot simply be
58232153Smm * copied and put under another distribution licence
59232153Smm * [including the GNU Public Licence.]
60228753Smm */
61232153Smm
62232153Smm#include <sys/cdefs.h>
63228753Smm__FBSDID("$FreeBSD: head/sys/crypto/blowfish/bf_skey.c 116174 2003-06-10 21:44:29Z obrien $");
64228753Smm
65228753Smm#include <sys/types.h>
66228753Smm#include <sys/time.h>
67228753Smm#include <sys/systm.h>
68228753Smm#include <crypto/blowfish/blowfish.h>
69232153Smm#include <crypto/blowfish/bf_locl.h>
70232153Smm#include <crypto/blowfish/bf_pi.h>
71228753Smm
72228753Smmvoid
73228753SmmBF_set_key(key, len, data)
74228753Smm	BF_KEY *key;
75228753Smm	int len;
76232153Smm	unsigned char *data;
77232153Smm{
78228753Smm	int i;
79228753Smm	BF_LONG *p, ri, in[2];
80228753Smm	unsigned char *d, *end;
81228753Smm
82228753Smm	memcpy((char *)key, (const char *)&bf_init, sizeof(BF_KEY));
83228753Smm	p = key->P;
84232153Smm
85302001Smm	if (len > ((BF_ROUNDS + 2) * 4))
86232153Smm		len = (BF_ROUNDS + 2) * 4;
87302001Smm
88228753Smm	d = data;
89228753Smm	end= &(data[len]);
90228753Smm	for (i = 0; i < BF_ROUNDS + 2; i++) {
91228753Smm		ri = *(d++);
92228753Smm		if (d >= end) d = data;
93228753Smm
94228753Smm		ri <<= 8;
95358090Smm		ri |= *(d++);
96358090Smm		if (d >= end) d = data;
97228753Smm
98228753Smm		ri <<= 8;
99232153Smm		ri |= *(d++);
100232153Smm		if (d >= end) d = data;
101232153Smm
102232153Smm		ri <<= 8;
103232153Smm		ri |= *(d++);
104232153Smm		if (d >= end) d = data;
105232153Smm
106232153Smm		p[i] ^= ri;
107232153Smm	}
108232153Smm
109232153Smm	in[0] = 0L;
110348608Smm	in[1] = 0L;
111232153Smm	for (i = 0; i < BF_ROUNDS + 2; i += 2) {
112232153Smm		BF_encrypt(in, key);
113232153Smm		p[i  ] = in[0];
114232153Smm		p[i+1] = in[1];
115232153Smm	}
116232153Smm
117348608Smm	p = key->S;
118232153Smm	for (i = 0; i < 4 * 256; i += 2) {
119232153Smm		BF_encrypt(in, key);
120232153Smm		p[i  ] = in[0];
121232153Smm		p[i+1] = in[1];
122232153Smm	}
123232153Smm}
124232153Smm