POK
bf_skey.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Dec 11 16:32:31 2009
15  */
16 
17 /* crypto/bf/bf_skey.c */
18 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
19  * All rights reserved.
20  *
21  * This package is an SSL implementation written
22  * by Eric Young (eay@cryptsoft.com).
23  * The implementation was written so as to conform with Netscapes SSL.
24  *
25  * This library is free for commercial and non-commercial use as long as
26  * the following conditions are aheared to. The following conditions
27  * apply to all code found in this distribution, be it the RC4, RSA,
28  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
29  * included with this distribution is covered by the same copyright terms
30  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
31  *
32  * Copyright remains Eric Young's, and as such any Copyright notices in
33  * the code are not to be removed.
34  * If this package is used in a product, Eric Young should be given attribution
35  * as the author of the parts of the library used.
36  * This can be in the form of a textual message at program startup or
37  * in documentation (online or textual) provided with the package.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in the
46  * documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  * "This product includes cryptographic software written by
50  * Eric Young (eay@cryptsoft.com)"
51  * The word 'cryptographic' can be left out if the rouines from the library
52  * being used are not cryptographic related :-).
53  * 4. If you include any Windows specific code (or a derivative thereof) from
54  * the apps directory (application code) you must include an acknowledgement:
55  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
56  *
57  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  * The licence and distribution terms for any publically available version or
70  * derivative of this code cannot be changed. i.e. this code cannot simply be
71  * copied and put under another distribution licence
72  * [including the GNU Public Licence.]
73  */
74 
75 #ifdef POK_NEEDS_PROTOCOLS_BLOWFISH
76 
77 #include <libc/stdio.h>
78 #include <libc/string.h>
79 #include "blowfish.h"
80 
81 #include "bf_locl.h"
82 #include "bf_pi.h"
83 
84 #define FIPS_NON_FIPS_VCIPHER_Init(alg) \
85  void alg##_set_key(alg##_KEY *key, int len, const unsigned char *data)
86 
87 FIPS_NON_FIPS_VCIPHER_Init(BF)
88  {
89  int i;
90  BF_LONG *p,ri,in[2];
91  const unsigned char *d,*end;
92 
93 
94  memcpy(key,&bf_init,sizeof(BF_KEY));
95  p=key->P;
96 
97  if (len > ((BF_ROUNDS+2)*4)) len=(BF_ROUNDS+2)*4;
98 
99  d=data;
100  end= &(data[len]);
101  for (i=0; i<(BF_ROUNDS+2); i++)
102  {
103  ri= *(d++);
104  if (d >= end) d=data;
105 
106  ri<<=8;
107  ri|= *(d++);
108  if (d >= end) d=data;
109 
110  ri<<=8;
111  ri|= *(d++);
112  if (d >= end) d=data;
113 
114  ri<<=8;
115  ri|= *(d++);
116  if (d >= end) d=data;
117 
118  p[i]^=ri;
119  }
120 
121  in[0]=0L;
122  in[1]=0L;
123  for (i=0; i<(BF_ROUNDS+2); i+=2)
124  {
125  BF_encrypt(in,key);
126  p[i ]=in[0];
127  p[i+1]=in[1];
128  }
129 
130  p=key->S;
131  for (i=0; i<4*256; i+=2)
132  {
133  BF_encrypt(in,key);
134  p[i ]=in[0];
135  p[i+1]=in[1];
136  }
137  }
138 
139 
140 #endif /* POK_NEEDS_PROTOCOLS */