POK
/home/jaouen/pok_official/pok/trunk/libpok/protocols/blowfish/bf_skey.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Dec 11 16:32:31 2009 
00015  */
00016 
00017 /* crypto/bf/bf_skey.c */
00018 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00019  * All rights reserved.
00020  *
00021  * This package is an SSL implementation written
00022  * by Eric Young (eay@cryptsoft.com).
00023  * The implementation was written so as to conform with Netscapes SSL.
00024  * 
00025  * This library is free for commercial and non-commercial use as long as
00026  * the following conditions are aheared to.  The following conditions
00027  * apply to all code found in this distribution, be it the RC4, RSA,
00028  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00029  * included with this distribution is covered by the same copyright terms
00030  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00031  * 
00032  * Copyright remains Eric Young's, and as such any Copyright notices in
00033  * the code are not to be removed.
00034  * If this package is used in a product, Eric Young should be given attribution
00035  * as the author of the parts of the library used.
00036  * This can be in the form of a textual message at program startup or
00037  * in documentation (online or textual) provided with the package.
00038  * 
00039  * Redistribution and use in source and binary forms, with or without
00040  * modification, are permitted provided that the following conditions
00041  * are met:
00042  * 1. Redistributions of source code must retain the copyright
00043  *    notice, this list of conditions and the following disclaimer.
00044  * 2. Redistributions in binary form must reproduce the above copyright
00045  *    notice, this list of conditions and the following disclaimer in the
00046  *    documentation and/or other materials provided with the distribution.
00047  * 3. All advertising materials mentioning features or use of this software
00048  *    must display the following acknowledgement:
00049  *    "This product includes cryptographic software written by
00050  *     Eric Young (eay@cryptsoft.com)"
00051  *    The word 'cryptographic' can be left out if the rouines from the library
00052  *    being used are not cryptographic related :-).
00053  * 4. If you include any Windows specific code (or a derivative thereof) from 
00054  *    the apps directory (application code) you must include an acknowledgement:
00055  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00056  * 
00057  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00058  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00059  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00060  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00061  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00062  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00063  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00064  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00065  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00066  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00067  * SUCH DAMAGE.
00068  * 
00069  * The licence and distribution terms for any publically available version or
00070  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00071  * copied and put under another distribution licence
00072  * [including the GNU Public Licence.]
00073  */
00074 
00075 #ifdef POK_NEEDS_PROTOCOLS_BLOWFISH
00076 
00077 #include <libc/stdio.h>
00078 #include <libc/string.h>
00079 #include "blowfish.h"
00080 
00081 #include "bf_locl.h"
00082 #include "bf_pi.h"
00083 
00084 #define FIPS_NON_FIPS_VCIPHER_Init(alg) \
00085         void alg##_set_key(alg##_KEY *key, int len, const unsigned char *data)
00086 
00087 FIPS_NON_FIPS_VCIPHER_Init(BF)
00088         {
00089         int i;
00090         BF_LONG *p,ri,in[2];
00091         const unsigned char *d,*end;
00092 
00093 
00094         memcpy(key,&bf_init,sizeof(BF_KEY));
00095         p=key->P;
00096 
00097         if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
00098 
00099         d=data;
00100         end= &(data[len]);
00101         for (i=0; i<(BF_ROUNDS+2); i++)
00102                 {
00103                 ri= *(d++);
00104                 if (d >= end) d=data;
00105 
00106                 ri<<=8;
00107                 ri|= *(d++);
00108                 if (d >= end) d=data;
00109 
00110                 ri<<=8;
00111                 ri|= *(d++);
00112                 if (d >= end) d=data;
00113 
00114                 ri<<=8;
00115                 ri|= *(d++);
00116                 if (d >= end) d=data;
00117 
00118                 p[i]^=ri;
00119                 }
00120 
00121         in[0]=0L;
00122         in[1]=0L;
00123         for (i=0; i<(BF_ROUNDS+2); i+=2)
00124                 {
00125                 BF_encrypt(in,key);
00126                 p[i  ]=in[0];
00127                 p[i+1]=in[1];
00128                 }
00129 
00130         p=key->S;
00131         for (i=0; i<4*256; i+=2)
00132                 {
00133                 BF_encrypt(in,key);
00134                 p[i  ]=in[0];
00135                 p[i+1]=in[1];
00136                 }
00137         }
00138 
00139 
00140 #endif /* POK_NEEDS_PROTOCOLS */