147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* crypto/bf/bf_skey.c */ 247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * All rights reserved. 447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This package is an SSL implementation written 647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * by Eric Young (eay@cryptsoft.com). 747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The implementation was written so as to conform with Netscapes SSL. 847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This library is free for commercial and non-commercial use as long as 1047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the following conditions are aheared to. The following conditions 1147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * apply to all code found in this distribution, be it the RC4, RSA, 1247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * included with this distribution is covered by the same copyright terms 1447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 1647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Copyright remains Eric Young's, and as such any Copyright notices in 1747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the code are not to be removed. 1847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * If this package is used in a product, Eric Young should be given attribution 1947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * as the author of the parts of the library used. 2047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * This can be in the form of a textual message at program startup or 2147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * in documentation (online or textual) provided with the package. 2247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 2347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Redistribution and use in source and binary forms, with or without 2447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * modification, are permitted provided that the following conditions 2547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * are met: 2647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 1. Redistributions of source code must retain the copyright 2747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * notice, this list of conditions and the following disclaimer. 2847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright 2947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * notice, this list of conditions and the following disclaimer in the 3047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * documentation and/or other materials provided with the distribution. 3147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 3. All advertising materials mentioning features or use of this software 3247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * must display the following acknowledgement: 3347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * "This product includes cryptographic software written by 3447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * Eric Young (eay@cryptsoft.com)" 3547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The word 'cryptographic' can be left out if the rouines from the library 3647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * being used are not cryptographic related :-). 3747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 4. If you include any Windows specific code (or a derivative thereof) from 3847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * the apps directory (application code) you must include an acknowledgement: 3947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 4147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * SUCH DAMAGE. 5247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * 5347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * The licence and distribution terms for any publically available version or 5447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * derivative of this code cannot be changed. i.e. this code cannot simply be 5547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * copied and put under another distribution licence 5647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom * [including the GNU Public Licence.] 5747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom */ 5847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 5947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include <stdio.h> 6047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include <string.h> 61392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom#include <openssl/crypto.h> 6247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include <openssl/blowfish.h> 6347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include "bf_locl.h" 6447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom#include "bf_pi.h" 6547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 6647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstromvoid BF_set_key(BF_KEY *key, int len, const unsigned char *data) 67392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom#ifdef OPENSSL_FIPS 68392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom { 69392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom fips_cipher_abort(BLOWFISH); 70392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom private_BF_set_key(key, len, data); 71392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom } 72392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstromvoid private_BF_set_key(BF_KEY *key, int len, const unsigned char *data) 73392aa7cc7d2b122614c5393c3e357da07fd07af3Brian Carlstrom#endif 7447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 7547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom int i; 7647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_LONG *p,ri,in[2]; 7747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom const unsigned char *d,*end; 7847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 7947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 8047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom memcpy(key,&bf_init,sizeof(BF_KEY)); 8147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p=key->P; 8247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 8347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4; 8447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 8547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom d=data; 8647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom end= &(data[len]); 8747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom for (i=0; i<(BF_ROUNDS+2); i++) 8847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 8947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri= *(d++); 9047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (d >= end) d=data; 9147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 9247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri<<=8; 9347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri|= *(d++); 9447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (d >= end) d=data; 9547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 9647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri<<=8; 9747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri|= *(d++); 9847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (d >= end) d=data; 9947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 10047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri<<=8; 10147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom ri|= *(d++); 10247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom if (d >= end) d=data; 10347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 10447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p[i]^=ri; 10547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 10647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 10747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom in[0]=0L; 10847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom in[1]=0L; 10947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom for (i=0; i<(BF_ROUNDS+2); i+=2) 11047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 11147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_encrypt(in,key); 11247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p[i ]=in[0]; 11347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p[i+1]=in[1]; 11447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 11547e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 11647e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p=key->S; 11747e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom for (i=0; i<4*256; i+=2) 11847e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom { 11947e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom BF_encrypt(in,key); 12047e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p[i ]=in[0]; 12147e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom p[i+1]=in[1]; 12247e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 12347e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom } 12447e6a167f114f4c4b0bae4bce4ba1c4f539e0dc1Brian Carlstrom 125