bf_skey.c revision 238405
1254721Semaste/* crypto/bf/bf_skey.c */ 2254721Semaste/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3254721Semaste * All rights reserved. 4254721Semaste * 5254721Semaste * This package is an SSL implementation written 6254721Semaste * by Eric Young (eay@cryptsoft.com). 7254721Semaste * The implementation was written so as to conform with Netscapes SSL. 8254721Semaste * 9254721Semaste * This library is free for commercial and non-commercial use as long as 10254721Semaste * the following conditions are aheared to. The following conditions 11254721Semaste * apply to all code found in this distribution, be it the RC4, RSA, 12254721Semaste * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13254721Semaste * included with this distribution is covered by the same copyright terms 14254721Semaste * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15254721Semaste * 16254721Semaste * Copyright remains Eric Young's, and as such any Copyright notices in 17254721Semaste * the code are not to be removed. 18254721Semaste * If this package is used in a product, Eric Young should be given attribution 19254721Semaste * as the author of the parts of the library used. 20254721Semaste * This can be in the form of a textual message at program startup or 21254721Semaste * in documentation (online or textual) provided with the package. 22254721Semaste * 23254721Semaste * Redistribution and use in source and binary forms, with or without 24254721Semaste * modification, are permitted provided that the following conditions 25254721Semaste * are met: 26254721Semaste * 1. Redistributions of source code must retain the copyright 27254721Semaste * notice, this list of conditions and the following disclaimer. 28254721Semaste * 2. Redistributions in binary form must reproduce the above copyright 29254721Semaste * notice, this list of conditions and the following disclaimer in the 30254721Semaste * documentation and/or other materials provided with the distribution. 31254721Semaste * 3. All advertising materials mentioning features or use of this software 32254721Semaste * must display the following acknowledgement: 33254721Semaste * "This product includes cryptographic software written by 34254721Semaste * Eric Young (eay@cryptsoft.com)" 35254721Semaste * The word 'cryptographic' can be left out if the rouines from the library 36254721Semaste * being used are not cryptographic related :-). 37254721Semaste * 4. If you include any Windows specific code (or a derivative thereof) from 38254721Semaste * the apps directory (application code) you must include an acknowledgement: 39254721Semaste * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40254721Semaste * 41254721Semaste * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42254721Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43254721Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44254721Semaste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46254721Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48254721Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51254721Semaste * SUCH DAMAGE. 52254721Semaste * 53254721Semaste * The licence and distribution terms for any publically available version or 54254721Semaste * derivative of this code cannot be changed. i.e. this code cannot simply be 55254721Semaste * copied and put under another distribution licence 56254721Semaste * [including the GNU Public Licence.] 57254721Semaste */ 58254721Semaste 59254721Semaste#include <stdio.h> 60254721Semaste#include <string.h> 61254721Semaste#include <openssl/crypto.h> 62254721Semaste#include <openssl/blowfish.h> 63254721Semaste#include "bf_locl.h" 64254721Semaste#include "bf_pi.h" 65254721Semaste 66254721Semastevoid BF_set_key(BF_KEY *key, int len, const unsigned char *data) 67254721Semaste#ifdef OPENSSL_FIPS 68254721Semaste { 69254721Semaste fips_cipher_abort(BLOWFISH); 70254721Semaste private_BF_set_key(key, len, data); 71254721Semaste } 72254721Semastevoid private_BF_set_key(BF_KEY *key, int len, const unsigned char *data) 73254721Semaste#endif 74254721Semaste { 75254721Semaste int i; 76254721Semaste BF_LONG *p,ri,in[2]; 77254721Semaste const unsigned char *d,*end; 78254721Semaste 79254721Semaste 80254721Semaste memcpy(key,&bf_init,sizeof(BF_KEY)); 81254721Semaste p=key->P; 82254721Semaste 83254721Semaste if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4; 84254721Semaste 85254721Semaste d=data; 86254721Semaste end= &(data[len]); 87254721Semaste for (i=0; i<(BF_ROUNDS+2); i++) 88254721Semaste { 89254721Semaste ri= *(d++); 90254721Semaste if (d >= end) d=data; 91254721Semaste 92254721Semaste ri<<=8; 93254721Semaste ri|= *(d++); 94254721Semaste if (d >= end) d=data; 95254721Semaste 96254721Semaste ri<<=8; 97254721Semaste ri|= *(d++); 98254721Semaste if (d >= end) d=data; 99254721Semaste 100254721Semaste ri<<=8; 101254721Semaste ri|= *(d++); 102254721Semaste if (d >= end) d=data; 103254721Semaste 104254721Semaste p[i]^=ri; 105254721Semaste } 106254721Semaste 107254721Semaste in[0]=0L; 108254721Semaste in[1]=0L; 109254721Semaste for (i=0; i<(BF_ROUNDS+2); i+=2) 110254721Semaste { 111254721Semaste BF_encrypt(in,key); 112254721Semaste p[i ]=in[0]; 113254721Semaste p[i+1]=in[1]; 114254721Semaste } 115254721Semaste 116254721Semaste p=key->S; 117254721Semaste for (i=0; i<4*256; i+=2) 118254721Semaste { 119254721Semaste BF_encrypt(in,key); 120254721Semaste p[i ]=in[0]; 121254721Semaste p[i+1]=in[1]; 122254721Semaste } 123254721Semaste } 124254721Semaste 125254721Semaste