191671Sume/* $KAME: bf_skey.c,v 1.7 2002/02/27 01:33:59 itojun Exp $ */ 262587Sitojun 355009Sshin/* crypto/bf/bf_skey.c */ 4116174Sobrien 555009Sshin/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au) 655009Sshin * All rights reserved. 755009Sshin * 855009Sshin * This package is an SSL implementation written 955009Sshin * by Eric Young (eay@mincom.oz.au). 1055009Sshin * The implementation was written so as to conform with Netscapes SSL. 1155009Sshin * 1255009Sshin * This library is free for commercial and non-commercial use as long as 1355009Sshin * the following conditions are aheared to. The following conditions 1455009Sshin * apply to all code found in this distribution, be it the RC4, RSA, 1555009Sshin * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1655009Sshin * included with this distribution is covered by the same copyright terms 1755009Sshin * except that the holder is Tim Hudson (tjh@mincom.oz.au). 1855009Sshin * 1955009Sshin * Copyright remains Eric Young's, and as such any Copyright notices in 2055009Sshin * the code are not to be removed. 2155009Sshin * If this package is used in a product, Eric Young should be given attribution 2255009Sshin * as the author of the parts of the library used. 2355009Sshin * This can be in the form of a textual message at program startup or 2455009Sshin * in documentation (online or textual) provided with the package. 2555009Sshin * 2655009Sshin * Redistribution and use in source and binary forms, with or without 2755009Sshin * modification, are permitted provided that the following conditions 2855009Sshin * are met: 2955009Sshin * 1. Redistributions of source code must retain the copyright 3055009Sshin * notice, this list of conditions and the following disclaimer. 3155009Sshin * 2. Redistributions in binary form must reproduce the above copyright 3255009Sshin * notice, this list of conditions and the following disclaimer in the 3355009Sshin * documentation and/or other materials provided with the distribution. 3455009Sshin * 3. All advertising materials mentioning features or use of this software 3555009Sshin * must display the following acknowledgement: 3655009Sshin * "This product includes cryptographic software written by 3755009Sshin * Eric Young (eay@mincom.oz.au)" 3855009Sshin * The word 'cryptographic' can be left out if the rouines from the library 3955009Sshin * being used are not cryptographic related :-). 4055009Sshin * 4. If you include any Windows specific code (or a derivative thereof) from 4155009Sshin * the apps directory (application code) you must include an acknowledgement: 4255009Sshin * "This product includes software written by Tim Hudson (tjh@mincom.oz.au)" 4355009Sshin * 4455009Sshin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4555009Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4655009Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4755009Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4855009Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4955009Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5055009Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5155009Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5255009Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5355009Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5455009Sshin * SUCH DAMAGE. 5555009Sshin * 5655009Sshin * The licence and distribution terms for any publically available version or 5755009Sshin * derivative of this code cannot be changed. i.e. this code cannot simply be 5855009Sshin * copied and put under another distribution licence 5955009Sshin * [including the GNU Public Licence.] 6055009Sshin */ 6155009Sshin 62116174Sobrien#include <sys/cdefs.h> 63116174Sobrien__FBSDID("$FreeBSD$"); 64116174Sobrien 6555009Sshin#include <sys/types.h> 6655009Sshin#include <sys/time.h> 6755009Sshin#include <sys/systm.h> 6855009Sshin#include <crypto/blowfish/blowfish.h> 6955009Sshin#include <crypto/blowfish/bf_locl.h> 7055009Sshin#include <crypto/blowfish/bf_pi.h> 7155009Sshin 7278064Sumevoid 7378064SumeBF_set_key(key, len, data) 7478064Sume BF_KEY *key; 7578064Sume int len; 7678064Sume unsigned char *data; 7778064Sume{ 7855009Sshin int i; 7978064Sume BF_LONG *p, ri, in[2]; 8078064Sume unsigned char *d, *end; 8155009Sshin 8291671Sume memcpy((char *)key, (const char *)&bf_init, sizeof(BF_KEY)); 8378064Sume p = key->P; 8455009Sshin 8578064Sume if (len > ((BF_ROUNDS + 2) * 4)) 8678064Sume len = (BF_ROUNDS + 2) * 4; 8755009Sshin 8878064Sume d = data; 8955009Sshin end= &(data[len]); 9078064Sume for (i = 0; i < BF_ROUNDS + 2; i++) { 9178064Sume ri = *(d++); 9278064Sume if (d >= end) d = data; 9355009Sshin 9478064Sume ri <<= 8; 9578064Sume ri |= *(d++); 9678064Sume if (d >= end) d = data; 9755009Sshin 9878064Sume ri <<= 8; 9978064Sume ri |= *(d++); 10078064Sume if (d >= end) d = data; 10155009Sshin 10278064Sume ri <<= 8; 10378064Sume ri |= *(d++); 10478064Sume if (d >= end) d = data; 10555009Sshin 10678064Sume p[i] ^= ri; 10778064Sume } 10855009Sshin 10978064Sume in[0] = 0L; 11078064Sume in[1] = 0L; 11178064Sume for (i = 0; i < BF_ROUNDS + 2; i += 2) { 11291671Sume BF_encrypt(in, key); 11378064Sume p[i ] = in[0]; 11478064Sume p[i+1] = in[1]; 11578064Sume } 11655009Sshin 11778064Sume p = key->S; 11878064Sume for (i = 0; i < 4 * 256; i += 2) { 11991671Sume BF_encrypt(in, key); 12078064Sume p[i ] = in[0]; 12178064Sume p[i+1] = in[1]; 12255009Sshin } 12378064Sume} 124