summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiod <>2014-04-15 18:08:42 +0000
committermiod <>2014-04-15 18:08:42 +0000
commit226b8a984011488eb6966f9b8b7659feae55dda8 (patch)
treeef0c273cc879fee9e9a2be5375a3a24eaa333958
parent21dfaf13d16be4bcbf44d8e58c3a37439b8ca822 (diff)
downloadopenbsd-226b8a984011488eb6966f9b8b7659feae55dda8.tar.gz
openbsd-226b8a984011488eb6966f9b8b7659feae55dda8.tar.bz2
openbsd-226b8a984011488eb6966f9b8b7659feae55dda8.zip
Moved to regress/lib/libcrypto.
-rw-r--r--src/lib/libcrypto/bf/bftest.c529
-rw-r--r--src/lib/libcrypto/bn/bntest.c2013
-rw-r--r--src/lib/libcrypto/bn/exptest.c204
-rw-r--r--src/lib/libcrypto/cast/casttest.c233
-rw-r--r--src/lib/libcrypto/des/destest.c952
-rw-r--r--src/lib/libcrypto/dh/dhtest.c226
-rw-r--r--src/lib/libcrypto/dsa/dsatest.c259
-rw-r--r--src/lib/libcrypto/ec/ectest.c1489
-rw-r--r--src/lib/libcrypto/ecdh/ecdhtest.c374
-rw-r--r--src/lib/libcrypto/ecdsa/ecdsatest.c572
-rw-r--r--src/lib/libcrypto/evp/evp_test.c450
-rw-r--r--src/lib/libcrypto/hmac/hmactest.c164
-rw-r--r--src/lib/libcrypto/idea/ideatest.c235
-rw-r--r--src/lib/libcrypto/md2/md2test.c139
-rw-r--r--src/lib/libcrypto/md4/md4test.c136
-rw-r--r--src/lib/libcrypto/md5/md5test.c140
-rw-r--r--src/lib/libcrypto/mdc2/mdc2test.c141
-rw-r--r--src/lib/libcrypto/rand/randtest.c219
-rw-r--r--src/lib/libcrypto/rc2/rc2test.c274
-rw-r--r--src/lib/libcrypto/rc4/rc4test.c242
-rw-r--r--src/lib/libcrypto/ripemd/rmdtest.c138
-rw-r--r--src/lib/libcrypto/sha/sha1test.c166
-rw-r--r--src/lib/libcrypto/sha/shatest.c166
-rw-r--r--src/lib/libssl/src/crypto/bf/bftest.c529
-rw-r--r--src/lib/libssl/src/crypto/bn/bntest.c2013
-rw-r--r--src/lib/libssl/src/crypto/bn/exptest.c204
-rw-r--r--src/lib/libssl/src/crypto/cast/casttest.c233
-rw-r--r--src/lib/libssl/src/crypto/des/destest.c952
-rw-r--r--src/lib/libssl/src/crypto/dh/dhtest.c226
-rw-r--r--src/lib/libssl/src/crypto/dsa/dsatest.c259
-rw-r--r--src/lib/libssl/src/crypto/ec/ectest.c1489
-rw-r--r--src/lib/libssl/src/crypto/ecdh/ecdhtest.c374
-rw-r--r--src/lib/libssl/src/crypto/ecdsa/ecdsatest.c572
-rw-r--r--src/lib/libssl/src/crypto/evp/evp_test.c450
-rw-r--r--src/lib/libssl/src/crypto/hmac/hmactest.c164
-rw-r--r--src/lib/libssl/src/crypto/idea/ideatest.c235
-rw-r--r--src/lib/libssl/src/crypto/md2/md2test.c139
-rw-r--r--src/lib/libssl/src/crypto/md4/md4test.c136
-rw-r--r--src/lib/libssl/src/crypto/md5/md5test.c140
-rw-r--r--src/lib/libssl/src/crypto/mdc2/mdc2test.c141
-rw-r--r--src/lib/libssl/src/crypto/rand/randtest.c219
-rw-r--r--src/lib/libssl/src/crypto/rc2/rc2test.c274
-rw-r--r--src/lib/libssl/src/crypto/rc4/rc4test.c242
-rw-r--r--src/lib/libssl/src/crypto/ripemd/rmdtest.c138
-rw-r--r--src/lib/libssl/src/crypto/sha/sha1test.c166
-rw-r--r--src/lib/libssl/src/crypto/sha/shatest.c166
46 files changed, 0 insertions, 18922 deletions
diff --git a/src/lib/libcrypto/bf/bftest.c b/src/lib/libcrypto/bf/bftest.c
deleted file mode 100644
index 20caf0f3f8..0000000000
--- a/src/lib/libcrypto/bf/bftest.c
+++ /dev/null
@@ -1,529 +0,0 @@
1/* crypto/bf/bftest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* This has been a quickly hacked 'ideatest.c'. When I add tests for other
60 * RC2 modes, more of the code will be uncommented. */
61
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_BF is defined */
66
67#include "../e_os.h"
68
69#ifdef OPENSSL_NO_BF
70int main(int argc, char *argv[])
71{
72 printf("No BF support\n");
73 return(0);
74}
75#else
76#include <openssl/blowfish.h>
77
78static char *bf_key[2]={
79 "abcdefghijklmnopqrstuvwxyz",
80 "Who is John Galt?"
81 };
82
83/* big endian */
84static BF_LONG bf_plain[2][2]={
85 {0x424c4f57L,0x46495348L},
86 {0xfedcba98L,0x76543210L}
87 };
88
89static BF_LONG bf_cipher[2][2]={
90 {0x324ed0feL,0xf413a203L},
91 {0xcc91732bL,0x8022f684L}
92 };
93/************/
94
95/* Lets use the DES test vectors :-) */
96#define NUM_TESTS 34
97static unsigned char ecb_data[NUM_TESTS][8]={
98 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
99 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
100 {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
101 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
102 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
103 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
104 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
105 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
106 {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
107 {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
108 {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
109 {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
110 {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
111 {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
112 {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
113 {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
114 {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
115 {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
116 {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
117 {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
118 {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
119 {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
120 {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
121 {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
122 {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
123 {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
124 {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
125 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
126 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
127 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
128 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
129 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
130 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
131 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
132
133static unsigned char plain_data[NUM_TESTS][8]={
134 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
135 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
136 {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
137 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
138 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
139 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
140 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
141 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
142 {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
143 {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
144 {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
145 {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
146 {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
147 {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
148 {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
149 {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
150 {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
151 {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
152 {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
153 {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
154 {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
155 {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
156 {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
157 {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
158 {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
159 {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
160 {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
161 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
162 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
163 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
164 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
165 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
166 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
167 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
168
169static unsigned char cipher_data[NUM_TESTS][8]={
170 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
171 {0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A},
172 {0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2},
173 {0x24,0x66,0xDD,0x87,0x8B,0x96,0x3C,0x9D},
174 {0x61,0xF9,0xC3,0x80,0x22,0x81,0xB0,0x96},
175 {0x7D,0x0C,0xC6,0x30,0xAF,0xDA,0x1E,0xC7},
176 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
177 {0x0A,0xCE,0xAB,0x0F,0xC6,0xA0,0xA2,0x8D},
178 {0x59,0xC6,0x82,0x45,0xEB,0x05,0x28,0x2B},
179 {0xB1,0xB8,0xCC,0x0B,0x25,0x0F,0x09,0xA0},
180 {0x17,0x30,0xE5,0x77,0x8B,0xEA,0x1D,0xA4},
181 {0xA2,0x5E,0x78,0x56,0xCF,0x26,0x51,0xEB},
182 {0x35,0x38,0x82,0xB1,0x09,0xCE,0x8F,0x1A},
183 {0x48,0xF4,0xD0,0x88,0x4C,0x37,0x99,0x18},
184 {0x43,0x21,0x93,0xB7,0x89,0x51,0xFC,0x98},
185 {0x13,0xF0,0x41,0x54,0xD6,0x9D,0x1A,0xE5},
186 {0x2E,0xED,0xDA,0x93,0xFF,0xD3,0x9C,0x79},
187 {0xD8,0x87,0xE0,0x39,0x3C,0x2D,0xA6,0xE3},
188 {0x5F,0x99,0xD0,0x4F,0x5B,0x16,0x39,0x69},
189 {0x4A,0x05,0x7A,0x3B,0x24,0xD3,0x97,0x7B},
190 {0x45,0x20,0x31,0xC1,0xE4,0xFA,0xDA,0x8E},
191 {0x75,0x55,0xAE,0x39,0xF5,0x9B,0x87,0xBD},
192 {0x53,0xC5,0x5F,0x9C,0xB4,0x9F,0xC0,0x19},
193 {0x7A,0x8E,0x7B,0xFA,0x93,0x7E,0x89,0xA3},
194 {0xCF,0x9C,0x5D,0x7A,0x49,0x86,0xAD,0xB5},
195 {0xD1,0xAB,0xB2,0x90,0x65,0x8B,0xC7,0x78},
196 {0x55,0xCB,0x37,0x74,0xD1,0x3E,0xF2,0x01},
197 {0xFA,0x34,0xEC,0x48,0x47,0xB2,0x68,0xB2},
198 {0xA7,0x90,0x79,0x51,0x08,0xEA,0x3C,0xAE},
199 {0xC3,0x9E,0x07,0x2D,0x9F,0xAC,0x63,0x1D},
200 {0x01,0x49,0x33,0xE0,0xCD,0xAF,0xF6,0xE4},
201 {0xF2,0x1E,0x9A,0x77,0xB7,0x1C,0x49,0xBC},
202 {0x24,0x59,0x46,0x88,0x57,0x54,0x36,0x9A},
203 {0x6B,0x5C,0x5A,0x9C,0x5D,0x9E,0x0A,0x5A},
204 };
205
206static unsigned char cbc_key [16]={
207 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
208 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
209static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
210static char cbc_data[40]="7654321 Now is the time for ";
211static unsigned char cbc_ok[32]={
212 0x6B,0x77,0xB4,0xD6,0x30,0x06,0xDE,0xE6,
213 0x05,0xB1,0x56,0xE2,0x74,0x03,0x97,0x93,
214 0x58,0xDE,0xB9,0xE7,0x15,0x46,0x16,0xD9,
215 0x59,0xF1,0x65,0x2B,0xD5,0xFF,0x92,0xCC};
216
217static unsigned char cfb64_ok[]={
218 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
219 0xF2,0x6E,0xCF,0x6D,0x2E,0xB9,0xE7,0x6E,
220 0x3D,0xA3,0xDE,0x04,0xD1,0x51,0x72,0x00,
221 0x51,0x9D,0x57,0xA6,0xC3};
222
223static unsigned char ofb64_ok[]={
224 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
225 0x62,0xB3,0x43,0xCC,0x5B,0x65,0x58,0x73,
226 0x10,0xDD,0x90,0x8D,0x0C,0x24,0x1B,0x22,
227 0x63,0xC2,0xCF,0x80,0xDA};
228
229#define KEY_TEST_NUM 25
230static unsigned char key_test[KEY_TEST_NUM]={
231 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87,
232 0x78,0x69,0x5a,0x4b,0x3c,0x2d,0x1e,0x0f,
233 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
234 0x88};
235
236static unsigned char key_data[8]=
237 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10};
238
239static unsigned char key_out[KEY_TEST_NUM][8]={
240 {0xF9,0xAD,0x59,0x7C,0x49,0xDB,0x00,0x5E},
241 {0xE9,0x1D,0x21,0xC1,0xD9,0x61,0xA6,0xD6},
242 {0xE9,0xC2,0xB7,0x0A,0x1B,0xC6,0x5C,0xF3},
243 {0xBE,0x1E,0x63,0x94,0x08,0x64,0x0F,0x05},
244 {0xB3,0x9E,0x44,0x48,0x1B,0xDB,0x1E,0x6E},
245 {0x94,0x57,0xAA,0x83,0xB1,0x92,0x8C,0x0D},
246 {0x8B,0xB7,0x70,0x32,0xF9,0x60,0x62,0x9D},
247 {0xE8,0x7A,0x24,0x4E,0x2C,0xC8,0x5E,0x82},
248 {0x15,0x75,0x0E,0x7A,0x4F,0x4E,0xC5,0x77},
249 {0x12,0x2B,0xA7,0x0B,0x3A,0xB6,0x4A,0xE0},
250 {0x3A,0x83,0x3C,0x9A,0xFF,0xC5,0x37,0xF6},
251 {0x94,0x09,0xDA,0x87,0xA9,0x0F,0x6B,0xF2},
252 {0x88,0x4F,0x80,0x62,0x50,0x60,0xB8,0xB4},
253 {0x1F,0x85,0x03,0x1C,0x19,0xE1,0x19,0x68},
254 {0x79,0xD9,0x37,0x3A,0x71,0x4C,0xA3,0x4F},
255 {0x93,0x14,0x28,0x87,0xEE,0x3B,0xE1,0x5C},
256 {0x03,0x42,0x9E,0x83,0x8C,0xE2,0xD1,0x4B},
257 {0xA4,0x29,0x9E,0x27,0x46,0x9F,0xF6,0x7B},
258 {0xAF,0xD5,0xAE,0xD1,0xC1,0xBC,0x96,0xA8},
259 {0x10,0x85,0x1C,0x0E,0x38,0x58,0xDA,0x9F},
260 {0xE6,0xF5,0x1E,0xD7,0x9B,0x9D,0xB2,0x1F},
261 {0x64,0xA6,0xE1,0x4A,0xFD,0x36,0xB4,0x6F},
262 {0x80,0xC7,0xD7,0xD4,0x5A,0x54,0x79,0xAD},
263 {0x05,0x04,0x4B,0x62,0xFA,0x52,0xD0,0x80},
264 };
265
266static int test(void );
267static int print_test_data(void );
268int main(int argc, char *argv[])
269 {
270 int ret;
271
272 if (argc > 1)
273 ret=print_test_data();
274 else
275 ret=test();
276
277#ifdef OPENSSL_SYS_NETWARE
278 if (ret) printf("ERROR: %d\n", ret);
279#endif
280 EXIT(ret);
281 return(0);
282 }
283
284static int print_test_data(void)
285 {
286 unsigned int i,j;
287
288 printf("ecb test data\n");
289 printf("key bytes\t\tclear bytes\t\tcipher bytes\n");
290 for (i=0; i<NUM_TESTS; i++)
291 {
292 for (j=0; j<8; j++)
293 printf("%02X",ecb_data[i][j]);
294 printf("\t");
295 for (j=0; j<8; j++)
296 printf("%02X",plain_data[i][j]);
297 printf("\t");
298 for (j=0; j<8; j++)
299 printf("%02X",cipher_data[i][j]);
300 printf("\n");
301 }
302
303 printf("set_key test data\n");
304 printf("data[8]= ");
305 for (j=0; j<8; j++)
306 printf("%02X",key_data[j]);
307 printf("\n");
308 for (i=0; i<KEY_TEST_NUM-1; i++)
309 {
310 printf("c=");
311 for (j=0; j<8; j++)
312 printf("%02X",key_out[i][j]);
313 printf(" k[%2u]=",i+1);
314 for (j=0; j<i+1; j++)
315 printf("%02X",key_test[j]);
316 printf("\n");
317 }
318
319 printf("\nchaining mode test data\n");
320 printf("key[16] = ");
321 for (j=0; j<16; j++)
322 printf("%02X",cbc_key[j]);
323 printf("\niv[8] = ");
324 for (j=0; j<8; j++)
325 printf("%02X",cbc_iv[j]);
326 printf("\ndata[%d] = '%s'",(int)strlen(cbc_data)+1,cbc_data);
327 printf("\ndata[%d] = ",(int)strlen(cbc_data)+1);
328 for (j=0; j<strlen(cbc_data)+1; j++)
329 printf("%02X",cbc_data[j]);
330 printf("\n");
331 printf("cbc cipher text\n");
332 printf("cipher[%d]= ",32);
333 for (j=0; j<32; j++)
334 printf("%02X",cbc_ok[j]);
335 printf("\n");
336
337 printf("cfb64 cipher text\n");
338 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
339 for (j=0; j<strlen(cbc_data)+1; j++)
340 printf("%02X",cfb64_ok[j]);
341 printf("\n");
342
343 printf("ofb64 cipher text\n");
344 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
345 for (j=0; j<strlen(cbc_data)+1; j++)
346 printf("%02X",ofb64_ok[j]);
347 printf("\n");
348 return(0);
349 }
350
351static int test(void)
352 {
353 unsigned char cbc_in[40],cbc_out[40],iv[8];
354 int i,n,err=0;
355 BF_KEY key;
356 BF_LONG data[2];
357 unsigned char out[8];
358 BF_LONG len;
359
360 printf("testing blowfish in raw ecb mode\n");
361 for (n=0; n<2; n++)
362 {
363 BF_set_key(&key,strlen(bf_key[n]),(unsigned char *)bf_key[n]);
364
365 data[0]=bf_plain[n][0];
366 data[1]=bf_plain[n][1];
367 BF_encrypt(data,&key);
368 if (memcmp(&(bf_cipher[n][0]),&(data[0]),8) != 0)
369 {
370 printf("BF_encrypt error encrypting\n");
371 printf("got :");
372 for (i=0; i<2; i++)
373 printf("%08lX ",(unsigned long)data[i]);
374 printf("\n");
375 printf("expected:");
376 for (i=0; i<2; i++)
377 printf("%08lX ",(unsigned long)bf_cipher[n][i]);
378 err=1;
379 printf("\n");
380 }
381
382 BF_decrypt(&(data[0]),&key);
383 if (memcmp(&(bf_plain[n][0]),&(data[0]),8) != 0)
384 {
385 printf("BF_encrypt error decrypting\n");
386 printf("got :");
387 for (i=0; i<2; i++)
388 printf("%08lX ",(unsigned long)data[i]);
389 printf("\n");
390 printf("expected:");
391 for (i=0; i<2; i++)
392 printf("%08lX ",(unsigned long)bf_plain[n][i]);
393 printf("\n");
394 err=1;
395 }
396 }
397
398 printf("testing blowfish in ecb mode\n");
399
400 for (n=0; n<NUM_TESTS; n++)
401 {
402 BF_set_key(&key,8,ecb_data[n]);
403
404 BF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT);
405 if (memcmp(&(cipher_data[n][0]),out,8) != 0)
406 {
407 printf("BF_ecb_encrypt blowfish error encrypting\n");
408 printf("got :");
409 for (i=0; i<8; i++)
410 printf("%02X ",out[i]);
411 printf("\n");
412 printf("expected:");
413 for (i=0; i<8; i++)
414 printf("%02X ",cipher_data[n][i]);
415 err=1;
416 printf("\n");
417 }
418
419 BF_ecb_encrypt(out,out,&key,BF_DECRYPT);
420 if (memcmp(&(plain_data[n][0]),out,8) != 0)
421 {
422 printf("BF_ecb_encrypt error decrypting\n");
423 printf("got :");
424 for (i=0; i<8; i++)
425 printf("%02X ",out[i]);
426 printf("\n");
427 printf("expected:");
428 for (i=0; i<8; i++)
429 printf("%02X ",plain_data[n][i]);
430 printf("\n");
431 err=1;
432 }
433 }
434
435 printf("testing blowfish set_key\n");
436 for (n=1; n<KEY_TEST_NUM; n++)
437 {
438 BF_set_key(&key,n,key_test);
439 BF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT);
440 /* mips-sgi-irix6.5-gcc vv -mabi=64 bug workaround */
441 if (memcmp(out,&(key_out[i=n-1][0]),8) != 0)
442 {
443 printf("blowfish setkey error\n");
444 err=1;
445 }
446 }
447
448 printf("testing blowfish in cbc mode\n");
449 len=strlen(cbc_data)+1;
450
451 BF_set_key(&key,16,cbc_key);
452 memset(cbc_in,0,sizeof cbc_in);
453 memset(cbc_out,0,sizeof cbc_out);
454 memcpy(iv,cbc_iv,sizeof iv);
455 BF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len,
456 &key,iv,BF_ENCRYPT);
457 if (memcmp(cbc_out,cbc_ok,32) != 0)
458 {
459 err=1;
460 printf("BF_cbc_encrypt encrypt error\n");
461 for (i=0; i<32; i++) printf("0x%02X,",cbc_out[i]);
462 }
463 memcpy(iv,cbc_iv,8);
464 BF_cbc_encrypt(cbc_out,cbc_in,len,
465 &key,iv,BF_DECRYPT);
466 if (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0)
467 {
468 printf("BF_cbc_encrypt decrypt error\n");
469 err=1;
470 }
471
472 printf("testing blowfish in cfb64 mode\n");
473
474 BF_set_key(&key,16,cbc_key);
475 memset(cbc_in,0,40);
476 memset(cbc_out,0,40);
477 memcpy(iv,cbc_iv,8);
478 n=0;
479 BF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,
480 &key,iv,&n,BF_ENCRYPT);
481 BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13,
482 &key,iv,&n,BF_ENCRYPT);
483 if (memcmp(cbc_out,cfb64_ok,(int)len) != 0)
484 {
485 err=1;
486 printf("BF_cfb64_encrypt encrypt error\n");
487 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
488 }
489 n=0;
490 memcpy(iv,cbc_iv,8);
491 BF_cfb64_encrypt(cbc_out,cbc_in,17,
492 &key,iv,&n,BF_DECRYPT);
493 BF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,
494 &key,iv,&n,BF_DECRYPT);
495 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
496 {
497 printf("BF_cfb64_encrypt decrypt error\n");
498 err=1;
499 }
500
501 printf("testing blowfish in ofb64\n");
502
503 BF_set_key(&key,16,cbc_key);
504 memset(cbc_in,0,40);
505 memset(cbc_out,0,40);
506 memcpy(iv,cbc_iv,8);
507 n=0;
508 BF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n);
509 BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]),
510 &(cbc_out[13]),len-13,&key,iv,&n);
511 if (memcmp(cbc_out,ofb64_ok,(int)len) != 0)
512 {
513 err=1;
514 printf("BF_ofb64_encrypt encrypt error\n");
515 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
516 }
517 n=0;
518 memcpy(iv,cbc_iv,8);
519 BF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n);
520 BF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n);
521 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
522 {
523 printf("BF_ofb64_encrypt decrypt error\n");
524 err=1;
525 }
526
527 return(err);
528 }
529#endif
diff --git a/src/lib/libcrypto/bn/bntest.c b/src/lib/libcrypto/bn/bntest.c
deleted file mode 100644
index 06f5954acc..0000000000
--- a/src/lib/libcrypto/bn/bntest.c
+++ /dev/null
@@ -1,2013 +0,0 @@
1/* crypto/bn/bntest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the Eric Young open source
65 * license provided above.
66 *
67 * The binary polynomial arithmetic software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72/* Until the key-gen callbacks are modified to use newer prototypes, we allow
73 * deprecated functions for openssl-internal code */
74#ifdef OPENSSL_NO_DEPRECATED
75#undef OPENSSL_NO_DEPRECATED
76#endif
77
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81
82#include "e_os.h"
83
84#include <openssl/bio.h>
85#include <openssl/bn.h>
86#include <openssl/rand.h>
87#include <openssl/x509.h>
88#include <openssl/err.h>
89
90const int num0 = 100; /* number of tests */
91const int num1 = 50; /* additional tests for some functions */
92const int num2 = 5; /* number of tests for slow functions */
93
94int test_add(BIO *bp);
95int test_sub(BIO *bp);
96int test_lshift1(BIO *bp);
97int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
98int test_rshift1(BIO *bp);
99int test_rshift(BIO *bp,BN_CTX *ctx);
100int test_div(BIO *bp,BN_CTX *ctx);
101int test_div_word(BIO *bp);
102int test_div_recp(BIO *bp,BN_CTX *ctx);
103int test_mul(BIO *bp);
104int test_sqr(BIO *bp,BN_CTX *ctx);
105int test_mont(BIO *bp,BN_CTX *ctx);
106int test_mod(BIO *bp,BN_CTX *ctx);
107int test_mod_mul(BIO *bp,BN_CTX *ctx);
108int test_mod_exp(BIO *bp,BN_CTX *ctx);
109int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
110int test_exp(BIO *bp,BN_CTX *ctx);
111int test_gf2m_add(BIO *bp);
112int test_gf2m_mod(BIO *bp);
113int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
114int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
115int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
116int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
117int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
118int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
119int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
120int test_kron(BIO *bp,BN_CTX *ctx);
121int test_sqrt(BIO *bp,BN_CTX *ctx);
122int rand_neg(void);
123static int results=0;
124
125static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
126"\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
127
128static const char rnd_seed[] = "string to make the random number generator think it has entropy";
129
130static void message(BIO *out, char *m)
131 {
132 fprintf(stderr, "test %s\n", m);
133 BIO_puts(out, "print \"test ");
134 BIO_puts(out, m);
135 BIO_puts(out, "\\n\"\n");
136 }
137
138int main(int argc, char *argv[])
139 {
140 BN_CTX *ctx;
141 BIO *out;
142 char *outfile=NULL;
143
144 results = 0;
145
146 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
147
148 argc--;
149 argv++;
150 while (argc >= 1)
151 {
152 if (strcmp(*argv,"-results") == 0)
153 results=1;
154 else if (strcmp(*argv,"-out") == 0)
155 {
156 if (--argc < 1) break;
157 outfile= *(++argv);
158 }
159 argc--;
160 argv++;
161 }
162
163
164 ctx=BN_CTX_new();
165 if (ctx == NULL) EXIT(1);
166
167 out=BIO_new(BIO_s_file());
168 if (out == NULL) EXIT(1);
169 if (outfile == NULL)
170 {
171 BIO_set_fp(out,stdout,BIO_NOCLOSE);
172 }
173 else
174 {
175 if (!BIO_write_filename(out,outfile))
176 {
177 perror(outfile);
178 EXIT(1);
179 }
180 }
181
182 if (!results)
183 BIO_puts(out,"obase=16\nibase=16\n");
184
185 message(out,"BN_add");
186 if (!test_add(out)) goto err;
187 (void)BIO_flush(out);
188
189 message(out,"BN_sub");
190 if (!test_sub(out)) goto err;
191 (void)BIO_flush(out);
192
193 message(out,"BN_lshift1");
194 if (!test_lshift1(out)) goto err;
195 (void)BIO_flush(out);
196
197 message(out,"BN_lshift (fixed)");
198 if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
199 goto err;
200 (void)BIO_flush(out);
201
202 message(out,"BN_lshift");
203 if (!test_lshift(out,ctx,NULL)) goto err;
204 (void)BIO_flush(out);
205
206 message(out,"BN_rshift1");
207 if (!test_rshift1(out)) goto err;
208 (void)BIO_flush(out);
209
210 message(out,"BN_rshift");
211 if (!test_rshift(out,ctx)) goto err;
212 (void)BIO_flush(out);
213
214 message(out,"BN_sqr");
215 if (!test_sqr(out,ctx)) goto err;
216 (void)BIO_flush(out);
217
218 message(out,"BN_mul");
219 if (!test_mul(out)) goto err;
220 (void)BIO_flush(out);
221
222 message(out,"BN_div");
223 if (!test_div(out,ctx)) goto err;
224 (void)BIO_flush(out);
225
226 message(out,"BN_div_word");
227 if (!test_div_word(out)) goto err;
228 (void)BIO_flush(out);
229
230 message(out,"BN_div_recp");
231 if (!test_div_recp(out,ctx)) goto err;
232 (void)BIO_flush(out);
233
234 message(out,"BN_mod");
235 if (!test_mod(out,ctx)) goto err;
236 (void)BIO_flush(out);
237
238 message(out,"BN_mod_mul");
239 if (!test_mod_mul(out,ctx)) goto err;
240 (void)BIO_flush(out);
241
242 message(out,"BN_mont");
243 if (!test_mont(out,ctx)) goto err;
244 (void)BIO_flush(out);
245
246 message(out,"BN_mod_exp");
247 if (!test_mod_exp(out,ctx)) goto err;
248 (void)BIO_flush(out);
249
250 message(out,"BN_mod_exp_mont_consttime");
251 if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
252 (void)BIO_flush(out);
253
254 message(out,"BN_exp");
255 if (!test_exp(out,ctx)) goto err;
256 (void)BIO_flush(out);
257
258 message(out,"BN_kronecker");
259 if (!test_kron(out,ctx)) goto err;
260 (void)BIO_flush(out);
261
262 message(out,"BN_mod_sqrt");
263 if (!test_sqrt(out,ctx)) goto err;
264 (void)BIO_flush(out);
265#ifndef OPENSSL_NO_EC2M
266 message(out,"BN_GF2m_add");
267 if (!test_gf2m_add(out)) goto err;
268 (void)BIO_flush(out);
269
270 message(out,"BN_GF2m_mod");
271 if (!test_gf2m_mod(out)) goto err;
272 (void)BIO_flush(out);
273
274 message(out,"BN_GF2m_mod_mul");
275 if (!test_gf2m_mod_mul(out,ctx)) goto err;
276 (void)BIO_flush(out);
277
278 message(out,"BN_GF2m_mod_sqr");
279 if (!test_gf2m_mod_sqr(out,ctx)) goto err;
280 (void)BIO_flush(out);
281
282 message(out,"BN_GF2m_mod_inv");
283 if (!test_gf2m_mod_inv(out,ctx)) goto err;
284 (void)BIO_flush(out);
285
286 message(out,"BN_GF2m_mod_div");
287 if (!test_gf2m_mod_div(out,ctx)) goto err;
288 (void)BIO_flush(out);
289
290 message(out,"BN_GF2m_mod_exp");
291 if (!test_gf2m_mod_exp(out,ctx)) goto err;
292 (void)BIO_flush(out);
293
294 message(out,"BN_GF2m_mod_sqrt");
295 if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
296 (void)BIO_flush(out);
297
298 message(out,"BN_GF2m_mod_solve_quad");
299 if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
300 (void)BIO_flush(out);
301#endif
302 BN_CTX_free(ctx);
303 BIO_free(out);
304
305/**/
306 EXIT(0);
307err:
308 BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
309 * the failure, see test_bn in test/Makefile.ssl*/
310 (void)BIO_flush(out);
311 ERR_load_crypto_strings();
312 ERR_print_errors_fp(stderr);
313 EXIT(1);
314 return(1);
315 }
316
317int test_add(BIO *bp)
318 {
319 BIGNUM a,b,c;
320 int i;
321
322 BN_init(&a);
323 BN_init(&b);
324 BN_init(&c);
325
326 BN_bntest_rand(&a,512,0,0);
327 for (i=0; i<num0; i++)
328 {
329 BN_bntest_rand(&b,450+i,0,0);
330 a.neg=rand_neg();
331 b.neg=rand_neg();
332 BN_add(&c,&a,&b);
333 if (bp != NULL)
334 {
335 if (!results)
336 {
337 BN_print(bp,&a);
338 BIO_puts(bp," + ");
339 BN_print(bp,&b);
340 BIO_puts(bp," - ");
341 }
342 BN_print(bp,&c);
343 BIO_puts(bp,"\n");
344 }
345 a.neg=!a.neg;
346 b.neg=!b.neg;
347 BN_add(&c,&c,&b);
348 BN_add(&c,&c,&a);
349 if(!BN_is_zero(&c))
350 {
351 fprintf(stderr,"Add test failed!\n");
352 return 0;
353 }
354 }
355 BN_free(&a);
356 BN_free(&b);
357 BN_free(&c);
358 return(1);
359 }
360
361int test_sub(BIO *bp)
362 {
363 BIGNUM a,b,c;
364 int i;
365
366 BN_init(&a);
367 BN_init(&b);
368 BN_init(&c);
369
370 for (i=0; i<num0+num1; i++)
371 {
372 if (i < num1)
373 {
374 BN_bntest_rand(&a,512,0,0);
375 BN_copy(&b,&a);
376 if (BN_set_bit(&a,i)==0) return(0);
377 BN_add_word(&b,i);
378 }
379 else
380 {
381 BN_bntest_rand(&b,400+i-num1,0,0);
382 a.neg=rand_neg();
383 b.neg=rand_neg();
384 }
385 BN_sub(&c,&a,&b);
386 if (bp != NULL)
387 {
388 if (!results)
389 {
390 BN_print(bp,&a);
391 BIO_puts(bp," - ");
392 BN_print(bp,&b);
393 BIO_puts(bp," - ");
394 }
395 BN_print(bp,&c);
396 BIO_puts(bp,"\n");
397 }
398 BN_add(&c,&c,&b);
399 BN_sub(&c,&c,&a);
400 if(!BN_is_zero(&c))
401 {
402 fprintf(stderr,"Subtract test failed!\n");
403 return 0;
404 }
405 }
406 BN_free(&a);
407 BN_free(&b);
408 BN_free(&c);
409 return(1);
410 }
411
412int test_div(BIO *bp, BN_CTX *ctx)
413 {
414 BIGNUM a,b,c,d,e;
415 int i;
416
417 BN_init(&a);
418 BN_init(&b);
419 BN_init(&c);
420 BN_init(&d);
421 BN_init(&e);
422
423 for (i=0; i<num0+num1; i++)
424 {
425 if (i < num1)
426 {
427 BN_bntest_rand(&a,400,0,0);
428 BN_copy(&b,&a);
429 BN_lshift(&a,&a,i);
430 BN_add_word(&a,i);
431 }
432 else
433 BN_bntest_rand(&b,50+3*(i-num1),0,0);
434 a.neg=rand_neg();
435 b.neg=rand_neg();
436 BN_div(&d,&c,&a,&b,ctx);
437 if (bp != NULL)
438 {
439 if (!results)
440 {
441 BN_print(bp,&a);
442 BIO_puts(bp," / ");
443 BN_print(bp,&b);
444 BIO_puts(bp," - ");
445 }
446 BN_print(bp,&d);
447 BIO_puts(bp,"\n");
448
449 if (!results)
450 {
451 BN_print(bp,&a);
452 BIO_puts(bp," % ");
453 BN_print(bp,&b);
454 BIO_puts(bp," - ");
455 }
456 BN_print(bp,&c);
457 BIO_puts(bp,"\n");
458 }
459 BN_mul(&e,&d,&b,ctx);
460 BN_add(&d,&e,&c);
461 BN_sub(&d,&d,&a);
462 if(!BN_is_zero(&d))
463 {
464 fprintf(stderr,"Division test failed!\n");
465 return 0;
466 }
467 }
468 BN_free(&a);
469 BN_free(&b);
470 BN_free(&c);
471 BN_free(&d);
472 BN_free(&e);
473 return(1);
474 }
475
476static void print_word(BIO *bp,BN_ULONG w)
477 {
478#ifdef SIXTY_FOUR_BIT
479 if (sizeof(w) > sizeof(unsigned long))
480 {
481 unsigned long h=(unsigned long)(w>>32),
482 l=(unsigned long)(w);
483
484 if (h) BIO_printf(bp,"%lX%08lX",h,l);
485 else BIO_printf(bp,"%lX",l);
486 return;
487 }
488#endif
489 BIO_printf(bp,BN_HEX_FMT1,w);
490 }
491
492int test_div_word(BIO *bp)
493 {
494 BIGNUM a,b;
495 BN_ULONG r,s;
496 int i;
497
498 BN_init(&a);
499 BN_init(&b);
500
501 for (i=0; i<num0; i++)
502 {
503 do {
504 BN_bntest_rand(&a,512,-1,0);
505 BN_bntest_rand(&b,BN_BITS2,-1,0);
506 s = b.d[0];
507 } while (!s);
508
509 BN_copy(&b, &a);
510 r = BN_div_word(&b, s);
511
512 if (bp != NULL)
513 {
514 if (!results)
515 {
516 BN_print(bp,&a);
517 BIO_puts(bp," / ");
518 print_word(bp,s);
519 BIO_puts(bp," - ");
520 }
521 BN_print(bp,&b);
522 BIO_puts(bp,"\n");
523
524 if (!results)
525 {
526 BN_print(bp,&a);
527 BIO_puts(bp," % ");
528 print_word(bp,s);
529 BIO_puts(bp," - ");
530 }
531 print_word(bp,r);
532 BIO_puts(bp,"\n");
533 }
534 BN_mul_word(&b,s);
535 BN_add_word(&b,r);
536 BN_sub(&b,&a,&b);
537 if(!BN_is_zero(&b))
538 {
539 fprintf(stderr,"Division (word) test failed!\n");
540 return 0;
541 }
542 }
543 BN_free(&a);
544 BN_free(&b);
545 return(1);
546 }
547
548int test_div_recp(BIO *bp, BN_CTX *ctx)
549 {
550 BIGNUM a,b,c,d,e;
551 BN_RECP_CTX recp;
552 int i;
553
554 BN_RECP_CTX_init(&recp);
555 BN_init(&a);
556 BN_init(&b);
557 BN_init(&c);
558 BN_init(&d);
559 BN_init(&e);
560
561 for (i=0; i<num0+num1; i++)
562 {
563 if (i < num1)
564 {
565 BN_bntest_rand(&a,400,0,0);
566 BN_copy(&b,&a);
567 BN_lshift(&a,&a,i);
568 BN_add_word(&a,i);
569 }
570 else
571 BN_bntest_rand(&b,50+3*(i-num1),0,0);
572 a.neg=rand_neg();
573 b.neg=rand_neg();
574 BN_RECP_CTX_set(&recp,&b,ctx);
575 BN_div_recp(&d,&c,&a,&recp,ctx);
576 if (bp != NULL)
577 {
578 if (!results)
579 {
580 BN_print(bp,&a);
581 BIO_puts(bp," / ");
582 BN_print(bp,&b);
583 BIO_puts(bp," - ");
584 }
585 BN_print(bp,&d);
586 BIO_puts(bp,"\n");
587
588 if (!results)
589 {
590 BN_print(bp,&a);
591 BIO_puts(bp," % ");
592 BN_print(bp,&b);
593 BIO_puts(bp," - ");
594 }
595 BN_print(bp,&c);
596 BIO_puts(bp,"\n");
597 }
598 BN_mul(&e,&d,&b,ctx);
599 BN_add(&d,&e,&c);
600 BN_sub(&d,&d,&a);
601 if(!BN_is_zero(&d))
602 {
603 fprintf(stderr,"Reciprocal division test failed!\n");
604 fprintf(stderr,"a=");
605 BN_print_fp(stderr,&a);
606 fprintf(stderr,"\nb=");
607 BN_print_fp(stderr,&b);
608 fprintf(stderr,"\n");
609 return 0;
610 }
611 }
612 BN_free(&a);
613 BN_free(&b);
614 BN_free(&c);
615 BN_free(&d);
616 BN_free(&e);
617 BN_RECP_CTX_free(&recp);
618 return(1);
619 }
620
621int test_mul(BIO *bp)
622 {
623 BIGNUM a,b,c,d,e;
624 int i;
625 BN_CTX *ctx;
626
627 ctx = BN_CTX_new();
628 if (ctx == NULL) EXIT(1);
629
630 BN_init(&a);
631 BN_init(&b);
632 BN_init(&c);
633 BN_init(&d);
634 BN_init(&e);
635
636 for (i=0; i<num0+num1; i++)
637 {
638 if (i <= num1)
639 {
640 BN_bntest_rand(&a,100,0,0);
641 BN_bntest_rand(&b,100,0,0);
642 }
643 else
644 BN_bntest_rand(&b,i-num1,0,0);
645 a.neg=rand_neg();
646 b.neg=rand_neg();
647 BN_mul(&c,&a,&b,ctx);
648 if (bp != NULL)
649 {
650 if (!results)
651 {
652 BN_print(bp,&a);
653 BIO_puts(bp," * ");
654 BN_print(bp,&b);
655 BIO_puts(bp," - ");
656 }
657 BN_print(bp,&c);
658 BIO_puts(bp,"\n");
659 }
660 BN_div(&d,&e,&c,&a,ctx);
661 BN_sub(&d,&d,&b);
662 if(!BN_is_zero(&d) || !BN_is_zero(&e))
663 {
664 fprintf(stderr,"Multiplication test failed!\n");
665 return 0;
666 }
667 }
668 BN_free(&a);
669 BN_free(&b);
670 BN_free(&c);
671 BN_free(&d);
672 BN_free(&e);
673 BN_CTX_free(ctx);
674 return(1);
675 }
676
677int test_sqr(BIO *bp, BN_CTX *ctx)
678 {
679 BIGNUM a,c,d,e;
680 int i;
681
682 BN_init(&a);
683 BN_init(&c);
684 BN_init(&d);
685 BN_init(&e);
686
687 for (i=0; i<num0; i++)
688 {
689 BN_bntest_rand(&a,40+i*10,0,0);
690 a.neg=rand_neg();
691 BN_sqr(&c,&a,ctx);
692 if (bp != NULL)
693 {
694 if (!results)
695 {
696 BN_print(bp,&a);
697 BIO_puts(bp," * ");
698 BN_print(bp,&a);
699 BIO_puts(bp," - ");
700 }
701 BN_print(bp,&c);
702 BIO_puts(bp,"\n");
703 }
704 BN_div(&d,&e,&c,&a,ctx);
705 BN_sub(&d,&d,&a);
706 if(!BN_is_zero(&d) || !BN_is_zero(&e))
707 {
708 fprintf(stderr,"Square test failed!\n");
709 return 0;
710 }
711 }
712 BN_free(&a);
713 BN_free(&c);
714 BN_free(&d);
715 BN_free(&e);
716 return(1);
717 }
718
719int test_mont(BIO *bp, BN_CTX *ctx)
720 {
721 BIGNUM a,b,c,d,A,B;
722 BIGNUM n;
723 int i;
724 BN_MONT_CTX *mont;
725
726 BN_init(&a);
727 BN_init(&b);
728 BN_init(&c);
729 BN_init(&d);
730 BN_init(&A);
731 BN_init(&B);
732 BN_init(&n);
733
734 mont=BN_MONT_CTX_new();
735 if (mont == NULL)
736 return 0;
737
738 BN_bntest_rand(&a,100,0,0); /**/
739 BN_bntest_rand(&b,100,0,0); /**/
740 for (i=0; i<num2; i++)
741 {
742 int bits = (200*(i+1))/num2;
743
744 if (bits == 0)
745 continue;
746 BN_bntest_rand(&n,bits,0,1);
747 BN_MONT_CTX_set(mont,&n,ctx);
748
749 BN_nnmod(&a,&a,&n,ctx);
750 BN_nnmod(&b,&b,&n,ctx);
751
752 BN_to_montgomery(&A,&a,mont,ctx);
753 BN_to_montgomery(&B,&b,mont,ctx);
754
755 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
756 BN_from_montgomery(&A,&c,mont,ctx);/**/
757 if (bp != NULL)
758 {
759 if (!results)
760 {
761#ifdef undef
762fprintf(stderr,"%d * %d %% %d\n",
763BN_num_bits(&a),
764BN_num_bits(&b),
765BN_num_bits(mont->N));
766#endif
767 BN_print(bp,&a);
768 BIO_puts(bp," * ");
769 BN_print(bp,&b);
770 BIO_puts(bp," % ");
771 BN_print(bp,&(mont->N));
772 BIO_puts(bp," - ");
773 }
774 BN_print(bp,&A);
775 BIO_puts(bp,"\n");
776 }
777 BN_mod_mul(&d,&a,&b,&n,ctx);
778 BN_sub(&d,&d,&A);
779 if(!BN_is_zero(&d))
780 {
781 fprintf(stderr,"Montgomery multiplication test failed!\n");
782 return 0;
783 }
784 }
785 BN_MONT_CTX_free(mont);
786 BN_free(&a);
787 BN_free(&b);
788 BN_free(&c);
789 BN_free(&d);
790 BN_free(&A);
791 BN_free(&B);
792 BN_free(&n);
793 return(1);
794 }
795
796int test_mod(BIO *bp, BN_CTX *ctx)
797 {
798 BIGNUM *a,*b,*c,*d,*e;
799 int i;
800
801 a=BN_new();
802 b=BN_new();
803 c=BN_new();
804 d=BN_new();
805 e=BN_new();
806
807 BN_bntest_rand(a,1024,0,0); /**/
808 for (i=0; i<num0; i++)
809 {
810 BN_bntest_rand(b,450+i*10,0,0); /**/
811 a->neg=rand_neg();
812 b->neg=rand_neg();
813 BN_mod(c,a,b,ctx);/**/
814 if (bp != NULL)
815 {
816 if (!results)
817 {
818 BN_print(bp,a);
819 BIO_puts(bp," % ");
820 BN_print(bp,b);
821 BIO_puts(bp," - ");
822 }
823 BN_print(bp,c);
824 BIO_puts(bp,"\n");
825 }
826 BN_div(d,e,a,b,ctx);
827 BN_sub(e,e,c);
828 if(!BN_is_zero(e))
829 {
830 fprintf(stderr,"Modulo test failed!\n");
831 return 0;
832 }
833 }
834 BN_free(a);
835 BN_free(b);
836 BN_free(c);
837 BN_free(d);
838 BN_free(e);
839 return(1);
840 }
841
842int test_mod_mul(BIO *bp, BN_CTX *ctx)
843 {
844 BIGNUM *a,*b,*c,*d,*e;
845 int i,j;
846
847 a=BN_new();
848 b=BN_new();
849 c=BN_new();
850 d=BN_new();
851 e=BN_new();
852
853 for (j=0; j<3; j++) {
854 BN_bntest_rand(c,1024,0,0); /**/
855 for (i=0; i<num0; i++)
856 {
857 BN_bntest_rand(a,475+i*10,0,0); /**/
858 BN_bntest_rand(b,425+i*11,0,0); /**/
859 a->neg=rand_neg();
860 b->neg=rand_neg();
861 if (!BN_mod_mul(e,a,b,c,ctx))
862 {
863 unsigned long l;
864
865 while ((l=ERR_get_error()))
866 fprintf(stderr,"ERROR:%s\n",
867 ERR_error_string(l,NULL));
868 EXIT(1);
869 }
870 if (bp != NULL)
871 {
872 if (!results)
873 {
874 BN_print(bp,a);
875 BIO_puts(bp," * ");
876 BN_print(bp,b);
877 BIO_puts(bp," % ");
878 BN_print(bp,c);
879 if ((a->neg ^ b->neg) && !BN_is_zero(e))
880 {
881 /* If (a*b) % c is negative, c must be added
882 * in order to obtain the normalized remainder
883 * (new with OpenSSL 0.9.7, previous versions of
884 * BN_mod_mul could generate negative results)
885 */
886 BIO_puts(bp," + ");
887 BN_print(bp,c);
888 }
889 BIO_puts(bp," - ");
890 }
891 BN_print(bp,e);
892 BIO_puts(bp,"\n");
893 }
894 BN_mul(d,a,b,ctx);
895 BN_sub(d,d,e);
896 BN_div(a,b,d,c,ctx);
897 if(!BN_is_zero(b))
898 {
899 fprintf(stderr,"Modulo multiply test failed!\n");
900 ERR_print_errors_fp(stderr);
901 return 0;
902 }
903 }
904 }
905 BN_free(a);
906 BN_free(b);
907 BN_free(c);
908 BN_free(d);
909 BN_free(e);
910 return(1);
911 }
912
913int test_mod_exp(BIO *bp, BN_CTX *ctx)
914 {
915 BIGNUM *a,*b,*c,*d,*e;
916 int i;
917
918 a=BN_new();
919 b=BN_new();
920 c=BN_new();
921 d=BN_new();
922 e=BN_new();
923
924 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
925 for (i=0; i<num2; i++)
926 {
927 BN_bntest_rand(a,20+i*5,0,0); /**/
928 BN_bntest_rand(b,2+i,0,0); /**/
929
930 if (!BN_mod_exp(d,a,b,c,ctx))
931 return(0);
932
933 if (bp != NULL)
934 {
935 if (!results)
936 {
937 BN_print(bp,a);
938 BIO_puts(bp," ^ ");
939 BN_print(bp,b);
940 BIO_puts(bp," % ");
941 BN_print(bp,c);
942 BIO_puts(bp," - ");
943 }
944 BN_print(bp,d);
945 BIO_puts(bp,"\n");
946 }
947 BN_exp(e,a,b,ctx);
948 BN_sub(e,e,d);
949 BN_div(a,b,e,c,ctx);
950 if(!BN_is_zero(b))
951 {
952 fprintf(stderr,"Modulo exponentiation test failed!\n");
953 return 0;
954 }
955 }
956 BN_free(a);
957 BN_free(b);
958 BN_free(c);
959 BN_free(d);
960 BN_free(e);
961 return(1);
962 }
963
964int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
965 {
966 BIGNUM *a,*b,*c,*d,*e;
967 int i;
968
969 a=BN_new();
970 b=BN_new();
971 c=BN_new();
972 d=BN_new();
973 e=BN_new();
974
975 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
976 for (i=0; i<num2; i++)
977 {
978 BN_bntest_rand(a,20+i*5,0,0); /**/
979 BN_bntest_rand(b,2+i,0,0); /**/
980
981 if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
982 return(00);
983
984 if (bp != NULL)
985 {
986 if (!results)
987 {
988 BN_print(bp,a);
989 BIO_puts(bp," ^ ");
990 BN_print(bp,b);
991 BIO_puts(bp," % ");
992 BN_print(bp,c);
993 BIO_puts(bp," - ");
994 }
995 BN_print(bp,d);
996 BIO_puts(bp,"\n");
997 }
998 BN_exp(e,a,b,ctx);
999 BN_sub(e,e,d);
1000 BN_div(a,b,e,c,ctx);
1001 if(!BN_is_zero(b))
1002 {
1003 fprintf(stderr,"Modulo exponentiation test failed!\n");
1004 return 0;
1005 }
1006 }
1007 BN_free(a);
1008 BN_free(b);
1009 BN_free(c);
1010 BN_free(d);
1011 BN_free(e);
1012 return(1);
1013 }
1014
1015int test_exp(BIO *bp, BN_CTX *ctx)
1016 {
1017 BIGNUM *a,*b,*d,*e,*one;
1018 int i;
1019
1020 a=BN_new();
1021 b=BN_new();
1022 d=BN_new();
1023 e=BN_new();
1024 one=BN_new();
1025 BN_one(one);
1026
1027 for (i=0; i<num2; i++)
1028 {
1029 BN_bntest_rand(a,20+i*5,0,0); /**/
1030 BN_bntest_rand(b,2+i,0,0); /**/
1031
1032 if (BN_exp(d,a,b,ctx) <= 0)
1033 return(0);
1034
1035 if (bp != NULL)
1036 {
1037 if (!results)
1038 {
1039 BN_print(bp,a);
1040 BIO_puts(bp," ^ ");
1041 BN_print(bp,b);
1042 BIO_puts(bp," - ");
1043 }
1044 BN_print(bp,d);
1045 BIO_puts(bp,"\n");
1046 }
1047 BN_one(e);
1048 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
1049 BN_mul(e,e,a,ctx);
1050 BN_sub(e,e,d);
1051 if(!BN_is_zero(e))
1052 {
1053 fprintf(stderr,"Exponentiation test failed!\n");
1054 return 0;
1055 }
1056 }
1057 BN_free(a);
1058 BN_free(b);
1059 BN_free(d);
1060 BN_free(e);
1061 BN_free(one);
1062 return(1);
1063 }
1064#ifndef OPENSSL_NO_EC2M
1065int test_gf2m_add(BIO *bp)
1066 {
1067 BIGNUM a,b,c;
1068 int i, ret = 0;
1069
1070 BN_init(&a);
1071 BN_init(&b);
1072 BN_init(&c);
1073
1074 for (i=0; i<num0; i++)
1075 {
1076 BN_rand(&a,512,0,0);
1077 BN_copy(&b, BN_value_one());
1078 a.neg=rand_neg();
1079 b.neg=rand_neg();
1080 BN_GF2m_add(&c,&a,&b);
1081#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1082 if (bp != NULL)
1083 {
1084 if (!results)
1085 {
1086 BN_print(bp,&a);
1087 BIO_puts(bp," ^ ");
1088 BN_print(bp,&b);
1089 BIO_puts(bp," = ");
1090 }
1091 BN_print(bp,&c);
1092 BIO_puts(bp,"\n");
1093 }
1094#endif
1095 /* Test that two added values have the correct parity. */
1096 if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
1097 {
1098 fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
1099 goto err;
1100 }
1101 BN_GF2m_add(&c,&c,&c);
1102 /* Test that c + c = 0. */
1103 if(!BN_is_zero(&c))
1104 {
1105 fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
1106 goto err;
1107 }
1108 }
1109 ret = 1;
1110 err:
1111 BN_free(&a);
1112 BN_free(&b);
1113 BN_free(&c);
1114 return ret;
1115 }
1116
1117int test_gf2m_mod(BIO *bp)
1118 {
1119 BIGNUM *a,*b[2],*c,*d,*e;
1120 int i, j, ret = 0;
1121 int p0[] = {163,7,6,3,0,-1};
1122 int p1[] = {193,15,0,-1};
1123
1124 a=BN_new();
1125 b[0]=BN_new();
1126 b[1]=BN_new();
1127 c=BN_new();
1128 d=BN_new();
1129 e=BN_new();
1130
1131 BN_GF2m_arr2poly(p0, b[0]);
1132 BN_GF2m_arr2poly(p1, b[1]);
1133
1134 for (i=0; i<num0; i++)
1135 {
1136 BN_bntest_rand(a, 1024, 0, 0);
1137 for (j=0; j < 2; j++)
1138 {
1139 BN_GF2m_mod(c, a, b[j]);
1140#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1141 if (bp != NULL)
1142 {
1143 if (!results)
1144 {
1145 BN_print(bp,a);
1146 BIO_puts(bp," % ");
1147 BN_print(bp,b[j]);
1148 BIO_puts(bp," - ");
1149 BN_print(bp,c);
1150 BIO_puts(bp,"\n");
1151 }
1152 }
1153#endif
1154 BN_GF2m_add(d, a, c);
1155 BN_GF2m_mod(e, d, b[j]);
1156 /* Test that a + (a mod p) mod p == 0. */
1157 if(!BN_is_zero(e))
1158 {
1159 fprintf(stderr,"GF(2^m) modulo test failed!\n");
1160 goto err;
1161 }
1162 }
1163 }
1164 ret = 1;
1165 err:
1166 BN_free(a);
1167 BN_free(b[0]);
1168 BN_free(b[1]);
1169 BN_free(c);
1170 BN_free(d);
1171 BN_free(e);
1172 return ret;
1173 }
1174
1175int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
1176 {
1177 BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
1178 int i, j, ret = 0;
1179 int p0[] = {163,7,6,3,0,-1};
1180 int p1[] = {193,15,0,-1};
1181
1182 a=BN_new();
1183 b[0]=BN_new();
1184 b[1]=BN_new();
1185 c=BN_new();
1186 d=BN_new();
1187 e=BN_new();
1188 f=BN_new();
1189 g=BN_new();
1190 h=BN_new();
1191
1192 BN_GF2m_arr2poly(p0, b[0]);
1193 BN_GF2m_arr2poly(p1, b[1]);
1194
1195 for (i=0; i<num0; i++)
1196 {
1197 BN_bntest_rand(a, 1024, 0, 0);
1198 BN_bntest_rand(c, 1024, 0, 0);
1199 BN_bntest_rand(d, 1024, 0, 0);
1200 for (j=0; j < 2; j++)
1201 {
1202 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1203#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1204 if (bp != NULL)
1205 {
1206 if (!results)
1207 {
1208 BN_print(bp,a);
1209 BIO_puts(bp," * ");
1210 BN_print(bp,c);
1211 BIO_puts(bp," % ");
1212 BN_print(bp,b[j]);
1213 BIO_puts(bp," - ");
1214 BN_print(bp,e);
1215 BIO_puts(bp,"\n");
1216 }
1217 }
1218#endif
1219 BN_GF2m_add(f, a, d);
1220 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1221 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1222 BN_GF2m_add(f, e, g);
1223 BN_GF2m_add(f, f, h);
1224 /* Test that (a+d)*c = a*c + d*c. */
1225 if(!BN_is_zero(f))
1226 {
1227 fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
1228 goto err;
1229 }
1230 }
1231 }
1232 ret = 1;
1233 err:
1234 BN_free(a);
1235 BN_free(b[0]);
1236 BN_free(b[1]);
1237 BN_free(c);
1238 BN_free(d);
1239 BN_free(e);
1240 BN_free(f);
1241 BN_free(g);
1242 BN_free(h);
1243 return ret;
1244 }
1245
1246int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
1247 {
1248 BIGNUM *a,*b[2],*c,*d;
1249 int i, j, ret = 0;
1250 int p0[] = {163,7,6,3,0,-1};
1251 int p1[] = {193,15,0,-1};
1252
1253 a=BN_new();
1254 b[0]=BN_new();
1255 b[1]=BN_new();
1256 c=BN_new();
1257 d=BN_new();
1258
1259 BN_GF2m_arr2poly(p0, b[0]);
1260 BN_GF2m_arr2poly(p1, b[1]);
1261
1262 for (i=0; i<num0; i++)
1263 {
1264 BN_bntest_rand(a, 1024, 0, 0);
1265 for (j=0; j < 2; j++)
1266 {
1267 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1268 BN_copy(d, a);
1269 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1270#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1271 if (bp != NULL)
1272 {
1273 if (!results)
1274 {
1275 BN_print(bp,a);
1276 BIO_puts(bp," ^ 2 % ");
1277 BN_print(bp,b[j]);
1278 BIO_puts(bp, " = ");
1279 BN_print(bp,c);
1280 BIO_puts(bp,"; a * a = ");
1281 BN_print(bp,d);
1282 BIO_puts(bp,"\n");
1283 }
1284 }
1285#endif
1286 BN_GF2m_add(d, c, d);
1287 /* Test that a*a = a^2. */
1288 if(!BN_is_zero(d))
1289 {
1290 fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
1291 goto err;
1292 }
1293 }
1294 }
1295 ret = 1;
1296 err:
1297 BN_free(a);
1298 BN_free(b[0]);
1299 BN_free(b[1]);
1300 BN_free(c);
1301 BN_free(d);
1302 return ret;
1303 }
1304
1305int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
1306 {
1307 BIGNUM *a,*b[2],*c,*d;
1308 int i, j, ret = 0;
1309 int p0[] = {163,7,6,3,0,-1};
1310 int p1[] = {193,15,0,-1};
1311
1312 a=BN_new();
1313 b[0]=BN_new();
1314 b[1]=BN_new();
1315 c=BN_new();
1316 d=BN_new();
1317
1318 BN_GF2m_arr2poly(p0, b[0]);
1319 BN_GF2m_arr2poly(p1, b[1]);
1320
1321 for (i=0; i<num0; i++)
1322 {
1323 BN_bntest_rand(a, 512, 0, 0);
1324 for (j=0; j < 2; j++)
1325 {
1326 BN_GF2m_mod_inv(c, a, b[j], ctx);
1327 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1328#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1329 if (bp != NULL)
1330 {
1331 if (!results)
1332 {
1333 BN_print(bp,a);
1334 BIO_puts(bp, " * ");
1335 BN_print(bp,c);
1336 BIO_puts(bp," - 1 % ");
1337 BN_print(bp,b[j]);
1338 BIO_puts(bp,"\n");
1339 }
1340 }
1341#endif
1342 /* Test that ((1/a)*a) = 1. */
1343 if(!BN_is_one(d))
1344 {
1345 fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
1346 goto err;
1347 }
1348 }
1349 }
1350 ret = 1;
1351 err:
1352 BN_free(a);
1353 BN_free(b[0]);
1354 BN_free(b[1]);
1355 BN_free(c);
1356 BN_free(d);
1357 return ret;
1358 }
1359
1360int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
1361 {
1362 BIGNUM *a,*b[2],*c,*d,*e,*f;
1363 int i, j, ret = 0;
1364 int p0[] = {163,7,6,3,0,-1};
1365 int p1[] = {193,15,0,-1};
1366
1367 a=BN_new();
1368 b[0]=BN_new();
1369 b[1]=BN_new();
1370 c=BN_new();
1371 d=BN_new();
1372 e=BN_new();
1373 f=BN_new();
1374
1375 BN_GF2m_arr2poly(p0, b[0]);
1376 BN_GF2m_arr2poly(p1, b[1]);
1377
1378 for (i=0; i<num0; i++)
1379 {
1380 BN_bntest_rand(a, 512, 0, 0);
1381 BN_bntest_rand(c, 512, 0, 0);
1382 for (j=0; j < 2; j++)
1383 {
1384 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1385 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1386 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1387#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1388 if (bp != NULL)
1389 {
1390 if (!results)
1391 {
1392 BN_print(bp,a);
1393 BIO_puts(bp, " = ");
1394 BN_print(bp,c);
1395 BIO_puts(bp," * ");
1396 BN_print(bp,d);
1397 BIO_puts(bp, " % ");
1398 BN_print(bp,b[j]);
1399 BIO_puts(bp,"\n");
1400 }
1401 }
1402#endif
1403 /* Test that ((a/c)*c)/a = 1. */
1404 if(!BN_is_one(f))
1405 {
1406 fprintf(stderr,"GF(2^m) modular division test failed!\n");
1407 goto err;
1408 }
1409 }
1410 }
1411 ret = 1;
1412 err:
1413 BN_free(a);
1414 BN_free(b[0]);
1415 BN_free(b[1]);
1416 BN_free(c);
1417 BN_free(d);
1418 BN_free(e);
1419 BN_free(f);
1420 return ret;
1421 }
1422
1423int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
1424 {
1425 BIGNUM *a,*b[2],*c,*d,*e,*f;
1426 int i, j, ret = 0;
1427 int p0[] = {163,7,6,3,0,-1};
1428 int p1[] = {193,15,0,-1};
1429
1430 a=BN_new();
1431 b[0]=BN_new();
1432 b[1]=BN_new();
1433 c=BN_new();
1434 d=BN_new();
1435 e=BN_new();
1436 f=BN_new();
1437
1438 BN_GF2m_arr2poly(p0, b[0]);
1439 BN_GF2m_arr2poly(p1, b[1]);
1440
1441 for (i=0; i<num0; i++)
1442 {
1443 BN_bntest_rand(a, 512, 0, 0);
1444 BN_bntest_rand(c, 512, 0, 0);
1445 BN_bntest_rand(d, 512, 0, 0);
1446 for (j=0; j < 2; j++)
1447 {
1448 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1449 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1450 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1451 BN_add(f, c, d);
1452 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1453#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1454 if (bp != NULL)
1455 {
1456 if (!results)
1457 {
1458 BN_print(bp,a);
1459 BIO_puts(bp, " ^ (");
1460 BN_print(bp,c);
1461 BIO_puts(bp," + ");
1462 BN_print(bp,d);
1463 BIO_puts(bp, ") = ");
1464 BN_print(bp,e);
1465 BIO_puts(bp, "; - ");
1466 BN_print(bp,f);
1467 BIO_puts(bp, " % ");
1468 BN_print(bp,b[j]);
1469 BIO_puts(bp,"\n");
1470 }
1471 }
1472#endif
1473 BN_GF2m_add(f, e, f);
1474 /* Test that a^(c+d)=a^c*a^d. */
1475 if(!BN_is_zero(f))
1476 {
1477 fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
1478 goto err;
1479 }
1480 }
1481 }
1482 ret = 1;
1483 err:
1484 BN_free(a);
1485 BN_free(b[0]);
1486 BN_free(b[1]);
1487 BN_free(c);
1488 BN_free(d);
1489 BN_free(e);
1490 BN_free(f);
1491 return ret;
1492 }
1493
1494int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
1495 {
1496 BIGNUM *a,*b[2],*c,*d,*e,*f;
1497 int i, j, ret = 0;
1498 int p0[] = {163,7,6,3,0,-1};
1499 int p1[] = {193,15,0,-1};
1500
1501 a=BN_new();
1502 b[0]=BN_new();
1503 b[1]=BN_new();
1504 c=BN_new();
1505 d=BN_new();
1506 e=BN_new();
1507 f=BN_new();
1508
1509 BN_GF2m_arr2poly(p0, b[0]);
1510 BN_GF2m_arr2poly(p1, b[1]);
1511
1512 for (i=0; i<num0; i++)
1513 {
1514 BN_bntest_rand(a, 512, 0, 0);
1515 for (j=0; j < 2; j++)
1516 {
1517 BN_GF2m_mod(c, a, b[j]);
1518 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1519 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1520#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1521 if (bp != NULL)
1522 {
1523 if (!results)
1524 {
1525 BN_print(bp,d);
1526 BIO_puts(bp, " ^ 2 - ");
1527 BN_print(bp,a);
1528 BIO_puts(bp,"\n");
1529 }
1530 }
1531#endif
1532 BN_GF2m_add(f, c, e);
1533 /* Test that d^2 = a, where d = sqrt(a). */
1534 if(!BN_is_zero(f))
1535 {
1536 fprintf(stderr,"GF(2^m) modular square root test failed!\n");
1537 goto err;
1538 }
1539 }
1540 }
1541 ret = 1;
1542 err:
1543 BN_free(a);
1544 BN_free(b[0]);
1545 BN_free(b[1]);
1546 BN_free(c);
1547 BN_free(d);
1548 BN_free(e);
1549 BN_free(f);
1550 return ret;
1551 }
1552
1553int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
1554 {
1555 BIGNUM *a,*b[2],*c,*d,*e;
1556 int i, j, s = 0, t, ret = 0;
1557 int p0[] = {163,7,6,3,0,-1};
1558 int p1[] = {193,15,0,-1};
1559
1560 a=BN_new();
1561 b[0]=BN_new();
1562 b[1]=BN_new();
1563 c=BN_new();
1564 d=BN_new();
1565 e=BN_new();
1566
1567 BN_GF2m_arr2poly(p0, b[0]);
1568 BN_GF2m_arr2poly(p1, b[1]);
1569
1570 for (i=0; i<num0; i++)
1571 {
1572 BN_bntest_rand(a, 512, 0, 0);
1573 for (j=0; j < 2; j++)
1574 {
1575 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1576 if (t)
1577 {
1578 s++;
1579 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1580 BN_GF2m_add(d, c, d);
1581 BN_GF2m_mod(e, a, b[j]);
1582#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1583 if (bp != NULL)
1584 {
1585 if (!results)
1586 {
1587 BN_print(bp,c);
1588 BIO_puts(bp, " is root of z^2 + z = ");
1589 BN_print(bp,a);
1590 BIO_puts(bp, " % ");
1591 BN_print(bp,b[j]);
1592 BIO_puts(bp, "\n");
1593 }
1594 }
1595#endif
1596 BN_GF2m_add(e, e, d);
1597 /* Test that solution of quadratic c satisfies c^2 + c = a. */
1598 if(!BN_is_zero(e))
1599 {
1600 fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
1601 goto err;
1602 }
1603
1604 }
1605 else
1606 {
1607#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1608 if (bp != NULL)
1609 {
1610 if (!results)
1611 {
1612 BIO_puts(bp, "There are no roots of z^2 + z = ");
1613 BN_print(bp,a);
1614 BIO_puts(bp, " % ");
1615 BN_print(bp,b[j]);
1616 BIO_puts(bp, "\n");
1617 }
1618 }
1619#endif
1620 }
1621 }
1622 }
1623 if (s == 0)
1624 {
1625 fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1626 fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
1627 goto err;
1628 }
1629 ret = 1;
1630 err:
1631 BN_free(a);
1632 BN_free(b[0]);
1633 BN_free(b[1]);
1634 BN_free(c);
1635 BN_free(d);
1636 BN_free(e);
1637 return ret;
1638 }
1639#endif
1640static int genprime_cb(int p, int n, BN_GENCB *arg)
1641 {
1642 char c='*';
1643
1644 if (p == 0) c='.';
1645 if (p == 1) c='+';
1646 if (p == 2) c='*';
1647 if (p == 3) c='\n';
1648 putc(c, stderr);
1649 fflush(stderr);
1650 return 1;
1651 }
1652
1653int test_kron(BIO *bp, BN_CTX *ctx)
1654 {
1655 BN_GENCB cb;
1656 BIGNUM *a,*b,*r,*t;
1657 int i;
1658 int legendre, kronecker;
1659 int ret = 0;
1660
1661 a = BN_new();
1662 b = BN_new();
1663 r = BN_new();
1664 t = BN_new();
1665 if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
1666
1667 BN_GENCB_set(&cb, genprime_cb, NULL);
1668
1669 /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
1670 * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
1671 * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
1672 * So we generate a random prime b and compare these values
1673 * for a number of random a's. (That is, we run the Solovay-Strassen
1674 * primality test to confirm that b is prime, except that we
1675 * don't want to test whether b is prime but whether BN_kronecker
1676 * works.) */
1677
1678 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
1679 b->neg = rand_neg();
1680 putc('\n', stderr);
1681
1682 for (i = 0; i < num0; i++)
1683 {
1684 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
1685 a->neg = rand_neg();
1686
1687 /* t := (|b|-1)/2 (note that b is odd) */
1688 if (!BN_copy(t, b)) goto err;
1689 t->neg = 0;
1690 if (!BN_sub_word(t, 1)) goto err;
1691 if (!BN_rshift1(t, t)) goto err;
1692 /* r := a^t mod b */
1693 b->neg=0;
1694
1695 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
1696 b->neg=1;
1697
1698 if (BN_is_word(r, 1))
1699 legendre = 1;
1700 else if (BN_is_zero(r))
1701 legendre = 0;
1702 else
1703 {
1704 if (!BN_add_word(r, 1)) goto err;
1705 if (0 != BN_ucmp(r, b))
1706 {
1707 fprintf(stderr, "Legendre symbol computation failed\n");
1708 goto err;
1709 }
1710 legendre = -1;
1711 }
1712
1713 kronecker = BN_kronecker(a, b, ctx);
1714 if (kronecker < -1) goto err;
1715 /* we actually need BN_kronecker(a, |b|) */
1716 if (a->neg && b->neg)
1717 kronecker = -kronecker;
1718
1719 if (legendre != kronecker)
1720 {
1721 fprintf(stderr, "legendre != kronecker; a = ");
1722 BN_print_fp(stderr, a);
1723 fprintf(stderr, ", b = ");
1724 BN_print_fp(stderr, b);
1725 fprintf(stderr, "\n");
1726 goto err;
1727 }
1728
1729 putc('.', stderr);
1730 fflush(stderr);
1731 }
1732
1733 putc('\n', stderr);
1734 fflush(stderr);
1735 ret = 1;
1736 err:
1737 if (a != NULL) BN_free(a);
1738 if (b != NULL) BN_free(b);
1739 if (r != NULL) BN_free(r);
1740 if (t != NULL) BN_free(t);
1741 return ret;
1742 }
1743
1744int test_sqrt(BIO *bp, BN_CTX *ctx)
1745 {
1746 BN_GENCB cb;
1747 BIGNUM *a,*p,*r;
1748 int i, j;
1749 int ret = 0;
1750
1751 a = BN_new();
1752 p = BN_new();
1753 r = BN_new();
1754 if (a == NULL || p == NULL || r == NULL) goto err;
1755
1756 BN_GENCB_set(&cb, genprime_cb, NULL);
1757
1758 for (i = 0; i < 16; i++)
1759 {
1760 if (i < 8)
1761 {
1762 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1763
1764 if (!BN_set_word(p, primes[i])) goto err;
1765 }
1766 else
1767 {
1768 if (!BN_set_word(a, 32)) goto err;
1769 if (!BN_set_word(r, 2*i + 1)) goto err;
1770
1771 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
1772 putc('\n', stderr);
1773 }
1774 p->neg = rand_neg();
1775
1776 for (j = 0; j < num2; j++)
1777 {
1778 /* construct 'a' such that it is a square modulo p,
1779 * but in general not a proper square and not reduced modulo p */
1780 if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1781 if (!BN_nnmod(r, r, p, ctx)) goto err;
1782 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1783 if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1784 if (!BN_nnmod(a, a, p, ctx)) goto err;
1785 if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1786 if (!BN_mul(a, a, r, ctx)) goto err;
1787 if (rand_neg())
1788 if (!BN_sub(a, a, p)) goto err;
1789
1790 if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1791 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1792
1793 if (!BN_nnmod(a, a, p, ctx)) goto err;
1794
1795 if (BN_cmp(a, r) != 0)
1796 {
1797 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1798 BN_print_fp(stderr, a);
1799 fprintf(stderr, ", r = ");
1800 BN_print_fp(stderr, r);
1801 fprintf(stderr, ", p = ");
1802 BN_print_fp(stderr, p);
1803 fprintf(stderr, "\n");
1804 goto err;
1805 }
1806
1807 putc('.', stderr);
1808 fflush(stderr);
1809 }
1810
1811 putc('\n', stderr);
1812 fflush(stderr);
1813 }
1814 ret = 1;
1815 err:
1816 if (a != NULL) BN_free(a);
1817 if (p != NULL) BN_free(p);
1818 if (r != NULL) BN_free(r);
1819 return ret;
1820 }
1821
1822int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1823 {
1824 BIGNUM *a,*b,*c,*d;
1825 int i;
1826
1827 b=BN_new();
1828 c=BN_new();
1829 d=BN_new();
1830 BN_one(c);
1831
1832 if(a_)
1833 a=a_;
1834 else
1835 {
1836 a=BN_new();
1837 BN_bntest_rand(a,200,0,0); /**/
1838 a->neg=rand_neg();
1839 }
1840 for (i=0; i<num0; i++)
1841 {
1842 BN_lshift(b,a,i+1);
1843 BN_add(c,c,c);
1844 if (bp != NULL)
1845 {
1846 if (!results)
1847 {
1848 BN_print(bp,a);
1849 BIO_puts(bp," * ");
1850 BN_print(bp,c);
1851 BIO_puts(bp," - ");
1852 }
1853 BN_print(bp,b);
1854 BIO_puts(bp,"\n");
1855 }
1856 BN_mul(d,a,c,ctx);
1857 BN_sub(d,d,b);
1858 if(!BN_is_zero(d))
1859 {
1860 fprintf(stderr,"Left shift test failed!\n");
1861 fprintf(stderr,"a=");
1862 BN_print_fp(stderr,a);
1863 fprintf(stderr,"\nb=");
1864 BN_print_fp(stderr,b);
1865 fprintf(stderr,"\nc=");
1866 BN_print_fp(stderr,c);
1867 fprintf(stderr,"\nd=");
1868 BN_print_fp(stderr,d);
1869 fprintf(stderr,"\n");
1870 return 0;
1871 }
1872 }
1873 BN_free(a);
1874 BN_free(b);
1875 BN_free(c);
1876 BN_free(d);
1877 return(1);
1878 }
1879
1880int test_lshift1(BIO *bp)
1881 {
1882 BIGNUM *a,*b,*c;
1883 int i;
1884
1885 a=BN_new();
1886 b=BN_new();
1887 c=BN_new();
1888
1889 BN_bntest_rand(a,200,0,0); /**/
1890 a->neg=rand_neg();
1891 for (i=0; i<num0; i++)
1892 {
1893 BN_lshift1(b,a);
1894 if (bp != NULL)
1895 {
1896 if (!results)
1897 {
1898 BN_print(bp,a);
1899 BIO_puts(bp," * 2");
1900 BIO_puts(bp," - ");
1901 }
1902 BN_print(bp,b);
1903 BIO_puts(bp,"\n");
1904 }
1905 BN_add(c,a,a);
1906 BN_sub(a,b,c);
1907 if(!BN_is_zero(a))
1908 {
1909 fprintf(stderr,"Left shift one test failed!\n");
1910 return 0;
1911 }
1912
1913 BN_copy(a,b);
1914 }
1915 BN_free(a);
1916 BN_free(b);
1917 BN_free(c);
1918 return(1);
1919 }
1920
1921int test_rshift(BIO *bp,BN_CTX *ctx)
1922 {
1923 BIGNUM *a,*b,*c,*d,*e;
1924 int i;
1925
1926 a=BN_new();
1927 b=BN_new();
1928 c=BN_new();
1929 d=BN_new();
1930 e=BN_new();
1931 BN_one(c);
1932
1933 BN_bntest_rand(a,200,0,0); /**/
1934 a->neg=rand_neg();
1935 for (i=0; i<num0; i++)
1936 {
1937 BN_rshift(b,a,i+1);
1938 BN_add(c,c,c);
1939 if (bp != NULL)
1940 {
1941 if (!results)
1942 {
1943 BN_print(bp,a);
1944 BIO_puts(bp," / ");
1945 BN_print(bp,c);
1946 BIO_puts(bp," - ");
1947 }
1948 BN_print(bp,b);
1949 BIO_puts(bp,"\n");
1950 }
1951 BN_div(d,e,a,c,ctx);
1952 BN_sub(d,d,b);
1953 if(!BN_is_zero(d))
1954 {
1955 fprintf(stderr,"Right shift test failed!\n");
1956 return 0;
1957 }
1958 }
1959 BN_free(a);
1960 BN_free(b);
1961 BN_free(c);
1962 BN_free(d);
1963 BN_free(e);
1964 return(1);
1965 }
1966
1967int test_rshift1(BIO *bp)
1968 {
1969 BIGNUM *a,*b,*c;
1970 int i;
1971
1972 a=BN_new();
1973 b=BN_new();
1974 c=BN_new();
1975
1976 BN_bntest_rand(a,200,0,0); /**/
1977 a->neg=rand_neg();
1978 for (i=0; i<num0; i++)
1979 {
1980 BN_rshift1(b,a);
1981 if (bp != NULL)
1982 {
1983 if (!results)
1984 {
1985 BN_print(bp,a);
1986 BIO_puts(bp," / 2");
1987 BIO_puts(bp," - ");
1988 }
1989 BN_print(bp,b);
1990 BIO_puts(bp,"\n");
1991 }
1992 BN_sub(c,a,b);
1993 BN_sub(c,c,b);
1994 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
1995 {
1996 fprintf(stderr,"Right shift one test failed!\n");
1997 return 0;
1998 }
1999 BN_copy(a,b);
2000 }
2001 BN_free(a);
2002 BN_free(b);
2003 BN_free(c);
2004 return(1);
2005 }
2006
2007int rand_neg(void)
2008 {
2009 static unsigned int neg=0;
2010 static int sign[8]={0,0,0,1,1,0,1,1};
2011
2012 return(sign[(neg++)%8]);
2013 }
diff --git a/src/lib/libcrypto/bn/exptest.c b/src/lib/libcrypto/bn/exptest.c
deleted file mode 100644
index 074a8e882a..0000000000
--- a/src/lib/libcrypto/bn/exptest.c
+++ /dev/null
@@ -1,204 +0,0 @@
1/* crypto/bn/exptest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#include <openssl/bio.h>
66#include <openssl/bn.h>
67#include <openssl/rand.h>
68#include <openssl/err.h>
69
70#define NUM_BITS (BN_BITS*2)
71
72static const char rnd_seed[] = "string to make the random number generator think it has entropy";
73
74int main(int argc, char *argv[])
75 {
76 BN_CTX *ctx;
77 BIO *out=NULL;
78 int i,ret;
79 unsigned char c;
80 BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
81
82 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
83 * even check its return value
84 * (which we should) */
85
86 ERR_load_BN_strings();
87
88 ctx=BN_CTX_new();
89 if (ctx == NULL) EXIT(1);
90 r_mont=BN_new();
91 r_mont_const=BN_new();
92 r_recp=BN_new();
93 r_simple=BN_new();
94 a=BN_new();
95 b=BN_new();
96 m=BN_new();
97 if ( (r_mont == NULL) || (r_recp == NULL) ||
98 (a == NULL) || (b == NULL))
99 goto err;
100
101 out=BIO_new(BIO_s_file());
102
103 if (out == NULL) EXIT(1);
104 BIO_set_fp(out,stdout,BIO_NOCLOSE);
105
106 for (i=0; i<200; i++)
107 {
108 RAND_bytes(&c,1);
109 c=(c%BN_BITS)-BN_BITS2;
110 BN_rand(a,NUM_BITS+c,0,0);
111
112 RAND_bytes(&c,1);
113 c=(c%BN_BITS)-BN_BITS2;
114 BN_rand(b,NUM_BITS+c,0,0);
115
116 RAND_bytes(&c,1);
117 c=(c%BN_BITS)-BN_BITS2;
118 BN_rand(m,NUM_BITS+c,0,1);
119
120 BN_mod(a,a,m,ctx);
121 BN_mod(b,b,m,ctx);
122
123 ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
124 if (ret <= 0)
125 {
126 printf("BN_mod_exp_mont() problems\n");
127 ERR_print_errors(out);
128 EXIT(1);
129 }
130
131 ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
132 if (ret <= 0)
133 {
134 printf("BN_mod_exp_recp() problems\n");
135 ERR_print_errors(out);
136 EXIT(1);
137 }
138
139 ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
140 if (ret <= 0)
141 {
142 printf("BN_mod_exp_simple() problems\n");
143 ERR_print_errors(out);
144 EXIT(1);
145 }
146
147 ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
148 if (ret <= 0)
149 {
150 printf("BN_mod_exp_mont_consttime() problems\n");
151 ERR_print_errors(out);
152 EXIT(1);
153 }
154
155 if (BN_cmp(r_simple, r_mont) == 0
156 && BN_cmp(r_simple,r_recp) == 0
157 && BN_cmp(r_simple,r_mont_const) == 0)
158 {
159 printf(".");
160 fflush(stdout);
161 }
162 else
163 {
164 if (BN_cmp(r_simple,r_mont) != 0)
165 printf("\nsimple and mont results differ\n");
166 if (BN_cmp(r_simple,r_mont_const) != 0)
167 printf("\nsimple and mont const time results differ\n");
168 if (BN_cmp(r_simple,r_recp) != 0)
169 printf("\nsimple and recp results differ\n");
170
171 printf("a (%3d) = ",BN_num_bits(a)); BN_print(out,a);
172 printf("\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
173 printf("\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
174 printf("\nsimple ="); BN_print(out,r_simple);
175 printf("\nrecp ="); BN_print(out,r_recp);
176 printf("\nmont ="); BN_print(out,r_mont);
177 printf("\nmont_ct ="); BN_print(out,r_mont_const);
178 printf("\n");
179 EXIT(1);
180 }
181 }
182 BN_free(r_mont);
183 BN_free(r_mont_const);
184 BN_free(r_recp);
185 BN_free(r_simple);
186 BN_free(a);
187 BN_free(b);
188 BN_free(m);
189 BN_CTX_free(ctx);
190 ERR_remove_thread_state(NULL);
191 CRYPTO_mem_leaks(out);
192 BIO_free(out);
193 printf(" done\n");
194 EXIT(0);
195err:
196 ERR_load_crypto_strings();
197 ERR_print_errors(out);
198#ifdef OPENSSL_SYS_NETWARE
199 printf("ERROR\n");
200#endif
201 EXIT(1);
202 return(1);
203 }
204
diff --git a/src/lib/libcrypto/cast/casttest.c b/src/lib/libcrypto/cast/casttest.c
deleted file mode 100644
index 0d020d6975..0000000000
--- a/src/lib/libcrypto/cast/casttest.c
+++ /dev/null
@@ -1,233 +0,0 @@
1/* crypto/cast/casttest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */
63
64#include "../e_os.h"
65
66#ifdef OPENSSL_NO_CAST
67int main(int argc, char *argv[])
68{
69 printf("No CAST support\n");
70 return(0);
71}
72#else
73#include <openssl/cast.h>
74
75#define FULL_TEST
76
77static unsigned char k[16]={
78 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
79 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A
80 };
81
82static unsigned char in[8]={ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
83
84static int k_len[3]={16,10,5};
85static unsigned char c[3][8]={
86 {0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2},
87 {0xEB,0x6A,0x71,0x1A,0x2C,0x02,0x27,0x1B},
88 {0x7A,0xC8,0x16,0xD1,0x6E,0x9B,0x30,0x2E},
89 };
90static unsigned char out[80];
91
92static unsigned char in_a[16]={
93 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
94 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
95static unsigned char in_b[16]={
96 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
97 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
98
99static unsigned char c_a[16]={
100 0xEE,0xA9,0xD0,0xA2,0x49,0xFD,0x3B,0xA6,
101 0xB3,0x43,0x6F,0xB8,0x9D,0x6D,0xCA,0x92};
102static unsigned char c_b[16]={
103 0xB2,0xC9,0x5E,0xB0,0x0C,0x31,0xAD,0x71,
104 0x80,0xAC,0x05,0xB8,0xE8,0x3D,0x69,0x6E};
105
106#if 0
107char *text="Hello to all people out there";
108
109static unsigned char cfb_key[16]={
110 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
111 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
112 };
113static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
114static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
115#define CFB_TEST_SIZE 24
116static unsigned char plain[CFB_TEST_SIZE]=
117 {
118 0x4e,0x6f,0x77,0x20,0x69,0x73,
119 0x20,0x74,0x68,0x65,0x20,0x74,
120 0x69,0x6d,0x65,0x20,0x66,0x6f,
121 0x72,0x20,0x61,0x6c,0x6c,0x20
122 };
123static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
124 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
125 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
126 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
127
128/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
129 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
130 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
131 };
132#endif
133
134int main(int argc, char *argv[])
135 {
136#ifdef FULL_TEST
137 long l;
138 CAST_KEY key_b;
139#endif
140 int i,z,err=0;
141 CAST_KEY key;
142
143 for (z=0; z<3; z++)
144 {
145 CAST_set_key(&key,k_len[z],k);
146
147 CAST_ecb_encrypt(in,out,&key,CAST_ENCRYPT);
148 if (memcmp(out,&(c[z][0]),8) != 0)
149 {
150 printf("ecb cast error encrypting for keysize %d\n",k_len[z]*8);
151 printf("got :");
152 for (i=0; i<8; i++)
153 printf("%02X ",out[i]);
154 printf("\n");
155 printf("expected:");
156 for (i=0; i<8; i++)
157 printf("%02X ",c[z][i]);
158 err=20;
159 printf("\n");
160 }
161
162 CAST_ecb_encrypt(out,out,&key,CAST_DECRYPT);
163 if (memcmp(out,in,8) != 0)
164 {
165 printf("ecb cast error decrypting for keysize %d\n",k_len[z]*8);
166 printf("got :");
167 for (i=0; i<8; i++)
168 printf("%02X ",out[i]);
169 printf("\n");
170 printf("expected:");
171 for (i=0; i<8; i++)
172 printf("%02X ",in[i]);
173 printf("\n");
174 err=3;
175 }
176 }
177 if (err == 0)
178 printf("ecb cast5 ok\n");
179
180#ifdef FULL_TEST
181 {
182 unsigned char out_a[16],out_b[16];
183 static char *hex="0123456789ABCDEF";
184
185 printf("This test will take some time....");
186 fflush(stdout);
187 memcpy(out_a,in_a,sizeof(in_a));
188 memcpy(out_b,in_b,sizeof(in_b));
189 i=1;
190
191 for (l=0; l<1000000L; l++)
192 {
193 CAST_set_key(&key_b,16,out_b);
194 CAST_ecb_encrypt(&(out_a[0]),&(out_a[0]),&key_b,CAST_ENCRYPT);
195 CAST_ecb_encrypt(&(out_a[8]),&(out_a[8]),&key_b,CAST_ENCRYPT);
196 CAST_set_key(&key,16,out_a);
197 CAST_ecb_encrypt(&(out_b[0]),&(out_b[0]),&key,CAST_ENCRYPT);
198 CAST_ecb_encrypt(&(out_b[8]),&(out_b[8]),&key,CAST_ENCRYPT);
199 if ((l & 0xffff) == 0xffff)
200 {
201 printf("%c",hex[i&0x0f]);
202 fflush(stdout);
203 i++;
204 }
205 }
206
207 if ( (memcmp(out_a,c_a,sizeof(c_a)) != 0) ||
208 (memcmp(out_b,c_b,sizeof(c_b)) != 0))
209 {
210 printf("\n");
211 printf("Error\n");
212
213 printf("A out =");
214 for (i=0; i<16; i++) printf("%02X ",out_a[i]);
215 printf("\nactual=");
216 for (i=0; i<16; i++) printf("%02X ",c_a[i]);
217 printf("\n");
218
219 printf("B out =");
220 for (i=0; i<16; i++) printf("%02X ",out_b[i]);
221 printf("\nactual=");
222 for (i=0; i<16; i++) printf("%02X ",c_b[i]);
223 printf("\n");
224 }
225 else
226 printf(" ok\n");
227 }
228#endif
229
230 EXIT(err);
231 return(err);
232 }
233#endif
diff --git a/src/lib/libcrypto/des/destest.c b/src/lib/libcrypto/des/destest.c
deleted file mode 100644
index 64b92a34fe..0000000000
--- a/src/lib/libcrypto/des/destest.c
+++ /dev/null
@@ -1,952 +0,0 @@
1/* crypto/des/destest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61
62#include <openssl/e_os2.h>
63#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_WINDOWS)
64#ifndef OPENSSL_SYS_MSDOS
65#define OPENSSL_SYS_MSDOS
66#endif
67#endif
68
69#ifndef OPENSSL_SYS_MSDOS
70#if !defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VMS_DECC)
71#include OPENSSL_UNISTD
72#endif
73#else
74#include <io.h>
75#endif
76#include <string.h>
77
78#ifdef OPENSSL_NO_DES
79int main(int argc, char *argv[])
80{
81 printf("No DES support\n");
82 return(0);
83}
84#else
85#include <openssl/des.h>
86
87#define crypt(c,s) (DES_crypt((c),(s)))
88
89/* tisk tisk - the test keys don't all have odd parity :-( */
90/* test data */
91#define NUM_TESTS 34
92static unsigned char key_data[NUM_TESTS][8]={
93 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
94 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
95 {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
96 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
97 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
98 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
99 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
100 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
101 {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
102 {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
103 {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
104 {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
105 {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
106 {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
107 {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
108 {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
109 {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
110 {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
111 {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
112 {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
113 {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
114 {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
115 {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
116 {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
117 {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
118 {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
119 {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
120 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
121 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
122 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
123 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
124 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
125 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
126 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
127
128static unsigned char plain_data[NUM_TESTS][8]={
129 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
130 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
131 {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
132 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
133 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
134 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
135 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
136 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
137 {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
138 {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
139 {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
140 {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
141 {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
142 {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
143 {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
144 {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
145 {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
146 {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
147 {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
148 {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
149 {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
150 {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
151 {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
152 {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
153 {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
154 {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
155 {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
156 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
157 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
158 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
159 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
160 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
161 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
162 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
163
164static unsigned char cipher_data[NUM_TESTS][8]={
165 {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
166 {0x73,0x59,0xB2,0x16,0x3E,0x4E,0xDC,0x58},
167 {0x95,0x8E,0x6E,0x62,0x7A,0x05,0x55,0x7B},
168 {0xF4,0x03,0x79,0xAB,0x9E,0x0E,0xC5,0x33},
169 {0x17,0x66,0x8D,0xFC,0x72,0x92,0x53,0x2D},
170 {0x8A,0x5A,0xE1,0xF8,0x1A,0xB8,0xF2,0xDD},
171 {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
172 {0xED,0x39,0xD9,0x50,0xFA,0x74,0xBC,0xC4},
173 {0x69,0x0F,0x5B,0x0D,0x9A,0x26,0x93,0x9B},
174 {0x7A,0x38,0x9D,0x10,0x35,0x4B,0xD2,0x71},
175 {0x86,0x8E,0xBB,0x51,0xCA,0xB4,0x59,0x9A},
176 {0x71,0x78,0x87,0x6E,0x01,0xF1,0x9B,0x2A},
177 {0xAF,0x37,0xFB,0x42,0x1F,0x8C,0x40,0x95},
178 {0x86,0xA5,0x60,0xF1,0x0E,0xC6,0xD8,0x5B},
179 {0x0C,0xD3,0xDA,0x02,0x00,0x21,0xDC,0x09},
180 {0xEA,0x67,0x6B,0x2C,0xB7,0xDB,0x2B,0x7A},
181 {0xDF,0xD6,0x4A,0x81,0x5C,0xAF,0x1A,0x0F},
182 {0x5C,0x51,0x3C,0x9C,0x48,0x86,0xC0,0x88},
183 {0x0A,0x2A,0xEE,0xAE,0x3F,0xF4,0xAB,0x77},
184 {0xEF,0x1B,0xF0,0x3E,0x5D,0xFA,0x57,0x5A},
185 {0x88,0xBF,0x0D,0xB6,0xD7,0x0D,0xEE,0x56},
186 {0xA1,0xF9,0x91,0x55,0x41,0x02,0x0B,0x56},
187 {0x6F,0xBF,0x1C,0xAF,0xCF,0xFD,0x05,0x56},
188 {0x2F,0x22,0xE4,0x9B,0xAB,0x7C,0xA1,0xAC},
189 {0x5A,0x6B,0x61,0x2C,0xC2,0x6C,0xCE,0x4A},
190 {0x5F,0x4C,0x03,0x8E,0xD1,0x2B,0x2E,0x41},
191 {0x63,0xFA,0xC0,0xD0,0x34,0xD9,0xF7,0x93},
192 {0x61,0x7B,0x3A,0x0C,0xE8,0xF0,0x71,0x00},
193 {0xDB,0x95,0x86,0x05,0xF8,0xC8,0xC6,0x06},
194 {0xED,0xBF,0xD1,0xC6,0x6C,0x29,0xCC,0xC7},
195 {0x35,0x55,0x50,0xB2,0x15,0x0E,0x24,0x51},
196 {0xCA,0xAA,0xAF,0x4D,0xEA,0xF1,0xDB,0xAE},
197 {0xD5,0xD4,0x4F,0xF7,0x20,0x68,0x3D,0x0D},
198 {0x2A,0x2B,0xB0,0x08,0xDF,0x97,0xC2,0xF2}};
199
200static unsigned char cipher_ecb2[NUM_TESTS-1][8]={
201 {0x92,0x95,0xB5,0x9B,0xB3,0x84,0x73,0x6E},
202 {0x19,0x9E,0x9D,0x6D,0xF3,0x9A,0xA8,0x16},
203 {0x2A,0x4B,0x4D,0x24,0x52,0x43,0x84,0x27},
204 {0x35,0x84,0x3C,0x01,0x9D,0x18,0xC5,0xB6},
205 {0x4A,0x5B,0x2F,0x42,0xAA,0x77,0x19,0x25},
206 {0xA0,0x6B,0xA9,0xB8,0xCA,0x5B,0x17,0x8A},
207 {0xAB,0x9D,0xB7,0xFB,0xED,0x95,0xF2,0x74},
208 {0x3D,0x25,0x6C,0x23,0xA7,0x25,0x2F,0xD6},
209 {0xB7,0x6F,0xAB,0x4F,0xBD,0xBD,0xB7,0x67},
210 {0x8F,0x68,0x27,0xD6,0x9C,0xF4,0x1A,0x10},
211 {0x82,0x57,0xA1,0xD6,0x50,0x5E,0x81,0x85},
212 {0xA2,0x0F,0x0A,0xCD,0x80,0x89,0x7D,0xFA},
213 {0xCD,0x2A,0x53,0x3A,0xDB,0x0D,0x7E,0xF3},
214 {0xD2,0xC2,0xBE,0x27,0xE8,0x1B,0x68,0xE3},
215 {0xE9,0x24,0xCF,0x4F,0x89,0x3C,0x5B,0x0A},
216 {0xA7,0x18,0xC3,0x9F,0xFA,0x9F,0xD7,0x69},
217 {0x77,0x2C,0x79,0xB1,0xD2,0x31,0x7E,0xB1},
218 {0x49,0xAB,0x92,0x7F,0xD0,0x22,0x00,0xB7},
219 {0xCE,0x1C,0x6C,0x7D,0x85,0xE3,0x4A,0x6F},
220 {0xBE,0x91,0xD6,0xE1,0x27,0xB2,0xE9,0x87},
221 {0x70,0x28,0xAE,0x8F,0xD1,0xF5,0x74,0x1A},
222 {0xAA,0x37,0x80,0xBB,0xF3,0x22,0x1D,0xDE},
223 {0xA6,0xC4,0xD2,0x5E,0x28,0x93,0xAC,0xB3},
224 {0x22,0x07,0x81,0x5A,0xE4,0xB7,0x1A,0xAD},
225 {0xDC,0xCE,0x05,0xE7,0x07,0xBD,0xF5,0x84},
226 {0x26,0x1D,0x39,0x2C,0xB3,0xBA,0xA5,0x85},
227 {0xB4,0xF7,0x0F,0x72,0xFB,0x04,0xF0,0xDC},
228 {0x95,0xBA,0xA9,0x4E,0x87,0x36,0xF2,0x89},
229 {0xD4,0x07,0x3A,0xF1,0x5A,0x17,0x82,0x0E},
230 {0xEF,0x6F,0xAF,0xA7,0x66,0x1A,0x7E,0x89},
231 {0xC1,0x97,0xF5,0x58,0x74,0x8A,0x20,0xE7},
232 {0x43,0x34,0xCF,0xDA,0x22,0xC4,0x86,0xC8},
233 {0x08,0xD7,0xB4,0xFB,0x62,0x9D,0x08,0x85}};
234
235static unsigned char cbc_key [8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
236static unsigned char cbc2_key[8]={0xf1,0xe0,0xd3,0xc2,0xb5,0xa4,0x97,0x86};
237static unsigned char cbc3_key[8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
238static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
239/* Changed the following text constant to binary so it will work on ebcdic
240 * machines :-) */
241/* static char cbc_data[40]="7654321 Now is the time for \0001"; */
242static unsigned char cbc_data[40]={
243 0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x20,
244 0x4E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,
245 0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20,
246 0x66,0x6F,0x72,0x20,0x00,0x31,0x00,0x00,
247 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
248 };
249
250static unsigned char cbc_ok[32]={
251 0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
252 0xac,0xd8,0xae,0xfd,0xdf,0xd8,0xa1,0xeb,
253 0x46,0x8e,0x91,0x15,0x78,0x88,0xba,0x68,
254 0x1d,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
255
256#ifdef SCREW_THE_PARITY
257#error "SCREW_THE_PARITY is not ment to be defined."
258#error "Original vectors are preserved for reference only."
259static unsigned char cbc2_key[8]={0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
260static unsigned char xcbc_ok[32]={
261 0x86,0x74,0x81,0x0D,0x61,0xA4,0xA5,0x48,
262 0xB9,0x93,0x03,0xE1,0xB8,0xBB,0xBD,0xBD,
263 0x64,0x30,0x0B,0xB9,0x06,0x65,0x81,0x76,
264 0x04,0x1D,0x77,0x62,0x17,0xCA,0x2B,0xD2,
265 };
266#else
267static unsigned char xcbc_ok[32]={
268 0x84,0x6B,0x29,0x14,0x85,0x1E,0x9A,0x29,
269 0x54,0x73,0x2F,0x8A,0xA0,0xA6,0x11,0xC1,
270 0x15,0xCD,0xC2,0xD7,0x95,0x1B,0x10,0x53,
271 0xA6,0x3C,0x5E,0x03,0xB2,0x1A,0xA3,0xC4,
272 };
273#endif
274
275static unsigned char cbc3_ok[32]={
276 0x3F,0xE3,0x01,0xC9,0x62,0xAC,0x01,0xD0,
277 0x22,0x13,0x76,0x3C,0x1C,0xBD,0x4C,0xDC,
278 0x79,0x96,0x57,0xC0,0x64,0xEC,0xF5,0xD4,
279 0x1C,0x67,0x38,0x12,0xCF,0xDE,0x96,0x75};
280
281static unsigned char pcbc_ok[32]={
282 0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
283 0x6d,0xec,0xb4,0x70,0xa0,0xe5,0x6b,0x15,
284 0xae,0xa6,0xbf,0x61,0xed,0x7d,0x9c,0x9f,
285 0xf7,0x17,0x46,0x3b,0x8a,0xb3,0xcc,0x88};
286
287static unsigned char cfb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
288static unsigned char cfb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
289static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
290static unsigned char plain[24]=
291 {
292 0x4e,0x6f,0x77,0x20,0x69,0x73,
293 0x20,0x74,0x68,0x65,0x20,0x74,
294 0x69,0x6d,0x65,0x20,0x66,0x6f,
295 0x72,0x20,0x61,0x6c,0x6c,0x20
296 };
297static unsigned char cfb_cipher8[24]= {
298 0xf3,0x1f,0xda,0x07,0x01,0x14, 0x62,0xee,0x18,0x7f,0x43,0xd8,
299 0x0a,0x7c,0xd9,0xb5,0xb0,0xd2, 0x90,0xda,0x6e,0x5b,0x9a,0x87 };
300static unsigned char cfb_cipher16[24]={
301 0xF3,0x09,0x87,0x87,0x7F,0x57, 0xF7,0x3C,0x36,0xB6,0xDB,0x70,
302 0xD8,0xD5,0x34,0x19,0xD3,0x86, 0xB2,0x23,0xB7,0xB2,0xAD,0x1B };
303static unsigned char cfb_cipher32[24]={
304 0xF3,0x09,0x62,0x49,0xA4,0xDF, 0xA4,0x9F,0x33,0xDC,0x7B,0xAD,
305 0x4C,0xC8,0x9F,0x64,0xE4,0x53, 0xE5,0xEC,0x67,0x20,0xDA,0xB6 };
306static unsigned char cfb_cipher48[24]={
307 0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x30,0xB5,0x15,0xEC,0xBB,0x85,
308 0x97,0x5A,0x13,0x8C,0x68,0x60, 0xE2,0x38,0x34,0x3C,0xDC,0x1F };
309static unsigned char cfb_cipher64[24]={
310 0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x6E,0x51,0xA6,0x9E,0x83,0x9B,
311 0x1A,0x92,0xF7,0x84,0x03,0x46, 0x71,0x33,0x89,0x8E,0xA6,0x22 };
312
313static unsigned char ofb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
314static unsigned char ofb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
315static unsigned char ofb_buf1[24],ofb_buf2[24],ofb_tmp[8];
316static unsigned char ofb_cipher[24]=
317 {
318 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
319 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
320 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3
321 };
322
323#if 0
324static DES_LONG cbc_cksum_ret=0xB462FEF7L;
325#else
326static DES_LONG cbc_cksum_ret=0xF7FE62B4L;
327#endif
328static unsigned char cbc_cksum_data[8]={0x1D,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
329
330static char *pt(unsigned char *p);
331static int cfb_test(int bits, unsigned char *cfb_cipher);
332static int cfb64_test(unsigned char *cfb_cipher);
333static int ede_cfb64_test(unsigned char *cfb_cipher);
334int main(int argc, char *argv[])
335 {
336 int j,err=0;
337 unsigned int i;
338 des_cblock in,out,outin,iv3,iv2;
339 des_key_schedule ks,ks2,ks3;
340 unsigned char cbc_in[40];
341 unsigned char cbc_out[40];
342 DES_LONG cs;
343 unsigned char cret[8];
344#ifdef _CRAY
345 struct {
346 int a:32;
347 int b:32;
348 } lqret[2];
349#else
350 DES_LONG lqret[4];
351#endif
352 int num;
353 char *str;
354
355#ifndef OPENSSL_NO_DESCBCM
356 printf("Doing cbcm\n");
357 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
358 {
359 printf("Key error %d\n",j);
360 err=1;
361 }
362 if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
363 {
364 printf("Key error %d\n",j);
365 err=1;
366 }
367 if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
368 {
369 printf("Key error %d\n",j);
370 err=1;
371 }
372 memset(cbc_out,0,40);
373 memset(cbc_in,0,40);
374 i=strlen((char *)cbc_data)+1;
375 /* i=((i+7)/8)*8; */
376 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
377 memset(iv2,'\0',sizeof iv2);
378
379 DES_ede3_cbcm_encrypt(cbc_data,cbc_out,16L,&ks,&ks2,&ks3,&iv3,&iv2,
380 DES_ENCRYPT);
381 DES_ede3_cbcm_encrypt(&cbc_data[16],&cbc_out[16],i-16,&ks,&ks2,&ks3,
382 &iv3,&iv2,DES_ENCRYPT);
383 /* if (memcmp(cbc_out,cbc3_ok,
384 (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
385 {
386 printf("des_ede3_cbc_encrypt encrypt error\n");
387 err=1;
388 }
389 */
390 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
391 memset(iv2,'\0',sizeof iv2);
392 DES_ede3_cbcm_encrypt(cbc_out,cbc_in,i,&ks,&ks2,&ks3,&iv3,&iv2,DES_DECRYPT);
393 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
394 {
395 unsigned int n;
396
397 printf("des_ede3_cbcm_encrypt decrypt error\n");
398 for(n=0 ; n < i ; ++n)
399 printf(" %02x",cbc_data[n]);
400 printf("\n");
401 for(n=0 ; n < i ; ++n)
402 printf(" %02x",cbc_in[n]);
403 printf("\n");
404 err=1;
405 }
406#endif
407
408 printf("Doing ecb\n");
409 for (i=0; i<NUM_TESTS; i++)
410 {
411 DES_set_key_unchecked(&key_data[i],&ks);
412 memcpy(in,plain_data[i],8);
413 memset(out,0,8);
414 memset(outin,0,8);
415 des_ecb_encrypt(&in,&out,ks,DES_ENCRYPT);
416 des_ecb_encrypt(&out,&outin,ks,DES_DECRYPT);
417
418 if (memcmp(out,cipher_data[i],8) != 0)
419 {
420 printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
421 i+1,pt(key_data[i]),pt(in),pt(cipher_data[i]),
422 pt(out));
423 err=1;
424 }
425 if (memcmp(in,outin,8) != 0)
426 {
427 printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
428 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
429 err=1;
430 }
431 }
432
433#ifndef LIBDES_LIT
434 printf("Doing ede ecb\n");
435 for (i=0; i<(NUM_TESTS-2); i++)
436 {
437 DES_set_key_unchecked(&key_data[i],&ks);
438 DES_set_key_unchecked(&key_data[i+1],&ks2);
439 DES_set_key_unchecked(&key_data[i+2],&ks3);
440 memcpy(in,plain_data[i],8);
441 memset(out,0,8);
442 memset(outin,0,8);
443 des_ecb2_encrypt(&in,&out,ks,ks2,DES_ENCRYPT);
444 des_ecb2_encrypt(&out,&outin,ks,ks2,DES_DECRYPT);
445
446 if (memcmp(out,cipher_ecb2[i],8) != 0)
447 {
448 printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
449 i+1,pt(key_data[i]),pt(in),pt(cipher_ecb2[i]),
450 pt(out));
451 err=1;
452 }
453 if (memcmp(in,outin,8) != 0)
454 {
455 printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
456 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
457 err=1;
458 }
459 }
460#endif
461
462 printf("Doing cbc\n");
463 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
464 {
465 printf("Key error %d\n",j);
466 err=1;
467 }
468 memset(cbc_out,0,40);
469 memset(cbc_in,0,40);
470 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
471 des_ncbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
472 &iv3,DES_ENCRYPT);
473 if (memcmp(cbc_out,cbc_ok,32) != 0)
474 {
475 printf("cbc_encrypt encrypt error\n");
476 err=1;
477 }
478
479 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
480 des_ncbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,
481 &iv3,DES_DECRYPT);
482 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)) != 0)
483 {
484 printf("cbc_encrypt decrypt error\n");
485 err=1;
486 }
487
488#ifndef LIBDES_LIT
489 printf("Doing desx cbc\n");
490 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
491 {
492 printf("Key error %d\n",j);
493 err=1;
494 }
495 memset(cbc_out,0,40);
496 memset(cbc_in,0,40);
497 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
498 des_xcbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
499 &iv3,&cbc2_key,&cbc3_key, DES_ENCRYPT);
500 if (memcmp(cbc_out,xcbc_ok,32) != 0)
501 {
502 printf("des_xcbc_encrypt encrypt error\n");
503 err=1;
504 }
505 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
506 des_xcbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,
507 &iv3,&cbc2_key,&cbc3_key, DES_DECRYPT);
508 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
509 {
510 printf("des_xcbc_encrypt decrypt error\n");
511 err=1;
512 }
513#endif
514
515 printf("Doing ede cbc\n");
516 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
517 {
518 printf("Key error %d\n",j);
519 err=1;
520 }
521 if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
522 {
523 printf("Key error %d\n",j);
524 err=1;
525 }
526 if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
527 {
528 printf("Key error %d\n",j);
529 err=1;
530 }
531 memset(cbc_out,0,40);
532 memset(cbc_in,0,40);
533 i=strlen((char *)cbc_data)+1;
534 /* i=((i+7)/8)*8; */
535 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
536
537 des_ede3_cbc_encrypt(cbc_data,cbc_out,16L,ks,ks2,ks3,&iv3,
538 DES_ENCRYPT);
539 des_ede3_cbc_encrypt(&(cbc_data[16]),&(cbc_out[16]),i-16,ks,ks2,ks3,
540 &iv3,DES_ENCRYPT);
541 if (memcmp(cbc_out,cbc3_ok,
542 (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
543 {
544 unsigned int n;
545
546 printf("des_ede3_cbc_encrypt encrypt error\n");
547 for(n=0 ; n < i ; ++n)
548 printf(" %02x",cbc_out[n]);
549 printf("\n");
550 for(n=0 ; n < i ; ++n)
551 printf(" %02x",cbc3_ok[n]);
552 printf("\n");
553 err=1;
554 }
555
556 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
557 des_ede3_cbc_encrypt(cbc_out,cbc_in,i,ks,ks2,ks3,&iv3,DES_DECRYPT);
558 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
559 {
560 unsigned int n;
561
562 printf("des_ede3_cbc_encrypt decrypt error\n");
563 for(n=0 ; n < i ; ++n)
564 printf(" %02x",cbc_data[n]);
565 printf("\n");
566 for(n=0 ; n < i ; ++n)
567 printf(" %02x",cbc_in[n]);
568 printf("\n");
569 err=1;
570 }
571
572#ifndef LIBDES_LIT
573 printf("Doing pcbc\n");
574 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
575 {
576 printf("Key error %d\n",j);
577 err=1;
578 }
579 memset(cbc_out,0,40);
580 memset(cbc_in,0,40);
581 des_pcbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
582 &cbc_iv,DES_ENCRYPT);
583 if (memcmp(cbc_out,pcbc_ok,32) != 0)
584 {
585 printf("pcbc_encrypt encrypt error\n");
586 err=1;
587 }
588 des_pcbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,&cbc_iv,
589 DES_DECRYPT);
590 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
591 {
592 printf("pcbc_encrypt decrypt error\n");
593 err=1;
594 }
595
596 printf("Doing ");
597 printf("cfb8 ");
598 err+=cfb_test(8,cfb_cipher8);
599 printf("cfb16 ");
600 err+=cfb_test(16,cfb_cipher16);
601 printf("cfb32 ");
602 err+=cfb_test(32,cfb_cipher32);
603 printf("cfb48 ");
604 err+=cfb_test(48,cfb_cipher48);
605 printf("cfb64 ");
606 err+=cfb_test(64,cfb_cipher64);
607
608 printf("cfb64() ");
609 err+=cfb64_test(cfb_cipher64);
610
611 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
612 for (i=0; i<sizeof(plain); i++)
613 des_cfb_encrypt(&(plain[i]),&(cfb_buf1[i]),
614 8,1,ks,&cfb_tmp,DES_ENCRYPT);
615 if (memcmp(cfb_cipher8,cfb_buf1,sizeof(plain)) != 0)
616 {
617 printf("cfb_encrypt small encrypt error\n");
618 err=1;
619 }
620
621 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
622 for (i=0; i<sizeof(plain); i++)
623 des_cfb_encrypt(&(cfb_buf1[i]),&(cfb_buf2[i]),
624 8,1,ks,&cfb_tmp,DES_DECRYPT);
625 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
626 {
627 printf("cfb_encrypt small decrypt error\n");
628 err=1;
629 }
630
631 printf("ede_cfb64() ");
632 err+=ede_cfb64_test(cfb_cipher64);
633
634 printf("done\n");
635
636 printf("Doing ofb\n");
637 DES_set_key_checked(&ofb_key,&ks);
638 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
639 des_ofb_encrypt(plain,ofb_buf1,64,sizeof(plain)/8,ks,&ofb_tmp);
640 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
641 {
642 printf("ofb_encrypt encrypt error\n");
643printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
644ofb_buf1[8+0], ofb_buf1[8+1], ofb_buf1[8+2], ofb_buf1[8+3],
645ofb_buf1[8+4], ofb_buf1[8+5], ofb_buf1[8+6], ofb_buf1[8+7]);
646printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
647ofb_buf1[8+0], ofb_cipher[8+1], ofb_cipher[8+2], ofb_cipher[8+3],
648ofb_buf1[8+4], ofb_cipher[8+5], ofb_cipher[8+6], ofb_cipher[8+7]);
649 err=1;
650 }
651 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
652 des_ofb_encrypt(ofb_buf1,ofb_buf2,64,sizeof(ofb_buf1)/8,ks,&ofb_tmp);
653 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
654 {
655 printf("ofb_encrypt decrypt error\n");
656printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
657ofb_buf2[8+0], ofb_buf2[8+1], ofb_buf2[8+2], ofb_buf2[8+3],
658ofb_buf2[8+4], ofb_buf2[8+5], ofb_buf2[8+6], ofb_buf2[8+7]);
659printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
660plain[8+0], plain[8+1], plain[8+2], plain[8+3],
661plain[8+4], plain[8+5], plain[8+6], plain[8+7]);
662 err=1;
663 }
664
665 printf("Doing ofb64\n");
666 DES_set_key_checked(&ofb_key,&ks);
667 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
668 memset(ofb_buf1,0,sizeof(ofb_buf1));
669 memset(ofb_buf2,0,sizeof(ofb_buf1));
670 num=0;
671 for (i=0; i<sizeof(plain); i++)
672 {
673 des_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,&ofb_tmp,
674 &num);
675 }
676 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
677 {
678 printf("ofb64_encrypt encrypt error\n");
679 err=1;
680 }
681 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
682 num=0;
683 des_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,&ofb_tmp,
684 &num);
685 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
686 {
687 printf("ofb64_encrypt decrypt error\n");
688 err=1;
689 }
690
691 printf("Doing ede_ofb64\n");
692 DES_set_key_checked(&ofb_key,&ks);
693 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
694 memset(ofb_buf1,0,sizeof(ofb_buf1));
695 memset(ofb_buf2,0,sizeof(ofb_buf1));
696 num=0;
697 for (i=0; i<sizeof(plain); i++)
698 {
699 des_ede3_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,ks,
700 ks,&ofb_tmp,&num);
701 }
702 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
703 {
704 printf("ede_ofb64_encrypt encrypt error\n");
705 err=1;
706 }
707 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
708 num=0;
709 des_ede3_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,ks,ks,
710 &ofb_tmp,&num);
711 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
712 {
713 printf("ede_ofb64_encrypt decrypt error\n");
714 err=1;
715 }
716
717 printf("Doing cbc_cksum\n");
718 DES_set_key_checked(&cbc_key,&ks);
719 cs=des_cbc_cksum(cbc_data,&cret,strlen((char *)cbc_data),ks,&cbc_iv);
720 if (cs != cbc_cksum_ret)
721 {
722 printf("bad return value (%08lX), should be %08lX\n",
723 (unsigned long)cs,(unsigned long)cbc_cksum_ret);
724 err=1;
725 }
726 if (memcmp(cret,cbc_cksum_data,8) != 0)
727 {
728 printf("bad cbc_cksum block returned\n");
729 err=1;
730 }
731
732 printf("Doing quad_cksum\n");
733 cs=des_quad_cksum(cbc_data,(des_cblock *)lqret,
734 (long)strlen((char *)cbc_data),2,(des_cblock *)cbc_iv);
735 if (cs != 0x70d7a63aL)
736 {
737 printf("quad_cksum error, ret %08lx should be 70d7a63a\n",
738 (unsigned long)cs);
739 err=1;
740 }
741#ifdef _CRAY
742 if (lqret[0].a != 0x327eba8dL)
743 {
744 printf("quad_cksum error, out[0] %08lx is not %08lx\n",
745 (unsigned long)lqret[0].a,0x327eba8dUL);
746 err=1;
747 }
748 if (lqret[0].b != 0x201a49ccL)
749 {
750 printf("quad_cksum error, out[1] %08lx is not %08lx\n",
751 (unsigned long)lqret[0].b,0x201a49ccUL);
752 err=1;
753 }
754 if (lqret[1].a != 0x70d7a63aL)
755 {
756 printf("quad_cksum error, out[2] %08lx is not %08lx\n",
757 (unsigned long)lqret[1].a,0x70d7a63aUL);
758 err=1;
759 }
760 if (lqret[1].b != 0x501c2c26L)
761 {
762 printf("quad_cksum error, out[3] %08lx is not %08lx\n",
763 (unsigned long)lqret[1].b,0x501c2c26UL);
764 err=1;
765 }
766#else
767 if (lqret[0] != 0x327eba8dL)
768 {
769 printf("quad_cksum error, out[0] %08lx is not %08lx\n",
770 (unsigned long)lqret[0],0x327eba8dUL);
771 err=1;
772 }
773 if (lqret[1] != 0x201a49ccL)
774 {
775 printf("quad_cksum error, out[1] %08lx is not %08lx\n",
776 (unsigned long)lqret[1],0x201a49ccUL);
777 err=1;
778 }
779 if (lqret[2] != 0x70d7a63aL)
780 {
781 printf("quad_cksum error, out[2] %08lx is not %08lx\n",
782 (unsigned long)lqret[2],0x70d7a63aUL);
783 err=1;
784 }
785 if (lqret[3] != 0x501c2c26L)
786 {
787 printf("quad_cksum error, out[3] %08lx is not %08lx\n",
788 (unsigned long)lqret[3],0x501c2c26UL);
789 err=1;
790 }
791#endif
792#endif
793
794 printf("input word alignment test");
795 for (i=0; i<4; i++)
796 {
797 printf(" %d",i);
798 des_ncbc_encrypt(&(cbc_out[i]),cbc_in,
799 strlen((char *)cbc_data)+1,ks,
800 &cbc_iv,DES_ENCRYPT);
801 }
802 printf("\noutput word alignment test");
803 for (i=0; i<4; i++)
804 {
805 printf(" %d",i);
806 des_ncbc_encrypt(cbc_out,&(cbc_in[i]),
807 strlen((char *)cbc_data)+1,ks,
808 &cbc_iv,DES_ENCRYPT);
809 }
810 printf("\n");
811 printf("fast crypt test ");
812 str=crypt("testing","ef");
813 if (strcmp("efGnQx2725bI2",str) != 0)
814 {
815 printf("fast crypt error, %s should be efGnQx2725bI2\n",str);
816 err=1;
817 }
818 str=crypt("bca76;23","yA");
819 if (strcmp("yA1Rp/1hZXIJk",str) != 0)
820 {
821 printf("fast crypt error, %s should be yA1Rp/1hZXIJk\n",str);
822 err=1;
823 }
824#ifdef OPENSSL_SYS_NETWARE
825 if (err) printf("ERROR: %d\n", err);
826#endif
827 printf("\n");
828 return(err);
829 }
830
831static char *pt(unsigned char *p)
832 {
833 static char bufs[10][20];
834 static int bnum=0;
835 char *ret;
836 int i;
837 static char *f="0123456789ABCDEF";
838
839 ret= &(bufs[bnum++][0]);
840 bnum%=10;
841 for (i=0; i<8; i++)
842 {
843 ret[i*2]=f[(p[i]>>4)&0xf];
844 ret[i*2+1]=f[p[i]&0xf];
845 }
846 ret[16]='\0';
847 return(ret);
848 }
849
850#ifndef LIBDES_LIT
851
852static int cfb_test(int bits, unsigned char *cfb_cipher)
853 {
854 des_key_schedule ks;
855 int i,err=0;
856
857 DES_set_key_checked(&cfb_key,&ks);
858 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
859 des_cfb_encrypt(plain,cfb_buf1,bits,sizeof(plain),ks,&cfb_tmp,
860 DES_ENCRYPT);
861 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
862 {
863 err=1;
864 printf("cfb_encrypt encrypt error\n");
865 for (i=0; i<24; i+=8)
866 printf("%s\n",pt(&(cfb_buf1[i])));
867 }
868 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
869 des_cfb_encrypt(cfb_buf1,cfb_buf2,bits,sizeof(plain),ks,&cfb_tmp,
870 DES_DECRYPT);
871 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
872 {
873 err=1;
874 printf("cfb_encrypt decrypt error\n");
875 for (i=0; i<24; i+=8)
876 printf("%s\n",pt(&(cfb_buf1[i])));
877 }
878 return(err);
879 }
880
881static int cfb64_test(unsigned char *cfb_cipher)
882 {
883 des_key_schedule ks;
884 int err=0,i,n;
885
886 DES_set_key_checked(&cfb_key,&ks);
887 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
888 n=0;
889 des_cfb64_encrypt(plain,cfb_buf1,12,ks,&cfb_tmp,&n,DES_ENCRYPT);
890 des_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,
891 &cfb_tmp,&n,DES_ENCRYPT);
892 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
893 {
894 err=1;
895 printf("cfb_encrypt encrypt error\n");
896 for (i=0; i<24; i+=8)
897 printf("%s\n",pt(&(cfb_buf1[i])));
898 }
899 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
900 n=0;
901 des_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,&cfb_tmp,&n,DES_DECRYPT);
902 des_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
903 sizeof(plain)-17,ks,&cfb_tmp,&n,DES_DECRYPT);
904 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
905 {
906 err=1;
907 printf("cfb_encrypt decrypt error\n");
908 for (i=0; i<24; i+=8)
909 printf("%s\n",pt(&(cfb_buf2[i])));
910 }
911 return(err);
912 }
913
914static int ede_cfb64_test(unsigned char *cfb_cipher)
915 {
916 des_key_schedule ks;
917 int err=0,i,n;
918
919 DES_set_key_checked(&cfb_key,&ks);
920 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
921 n=0;
922 des_ede3_cfb64_encrypt(plain,cfb_buf1,12,ks,ks,ks,&cfb_tmp,&n,
923 DES_ENCRYPT);
924 des_ede3_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
925 sizeof(plain)-12,ks,ks,ks,
926 &cfb_tmp,&n,DES_ENCRYPT);
927 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
928 {
929 err=1;
930 printf("ede_cfb_encrypt encrypt error\n");
931 for (i=0; i<24; i+=8)
932 printf("%s\n",pt(&(cfb_buf1[i])));
933 }
934 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
935 n=0;
936 des_ede3_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,ks,ks,ks,
937 &cfb_tmp,&n,DES_DECRYPT);
938 des_ede3_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
939 sizeof(plain)-17,ks,ks,ks,
940 &cfb_tmp,&n,DES_DECRYPT);
941 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
942 {
943 err=1;
944 printf("ede_cfb_encrypt decrypt error\n");
945 for (i=0; i<24; i+=8)
946 printf("%s\n",pt(&(cfb_buf2[i])));
947 }
948 return(err);
949 }
950
951#endif
952#endif
diff --git a/src/lib/libcrypto/dh/dhtest.c b/src/lib/libcrypto/dh/dhtest.c
deleted file mode 100644
index 882f5c310a..0000000000
--- a/src/lib/libcrypto/dh/dhtest.c
+++ /dev/null
@@ -1,226 +0,0 @@
1/* crypto/dh/dhtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Until the key-gen callbacks are modified to use newer prototypes, we allow
60 * deprecated functions for openssl-internal code */
61#ifdef OPENSSL_NO_DEPRECATED
62#undef OPENSSL_NO_DEPRECATED
63#endif
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68
69#include "../e_os.h"
70
71#include <openssl/crypto.h>
72#include <openssl/bio.h>
73#include <openssl/bn.h>
74#include <openssl/rand.h>
75#include <openssl/err.h>
76
77#ifdef OPENSSL_NO_DH
78int main(int argc, char *argv[])
79{
80 printf("No DH support\n");
81 return(0);
82}
83#else
84#include <openssl/dh.h>
85
86#ifdef OPENSSL_SYS_WIN16
87#define MS_CALLBACK _far _loadds
88#else
89#define MS_CALLBACK
90#endif
91
92static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
93
94static const char rnd_seed[] = "string to make the random number generator think it has entropy";
95
96int main(int argc, char *argv[])
97 {
98 BN_GENCB _cb;
99 DH *a;
100 DH *b=NULL;
101 char buf[12];
102 unsigned char *abuf=NULL,*bbuf=NULL;
103 int i,alen,blen,aout,bout,ret=1;
104 BIO *out;
105
106 CRYPTO_malloc_debug_init();
107 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
108 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
109
110#ifdef OPENSSL_SYS_WIN32
111 CRYPTO_malloc_init();
112#endif
113
114 RAND_seed(rnd_seed, sizeof rnd_seed);
115
116 out=BIO_new(BIO_s_file());
117 if (out == NULL) EXIT(1);
118 BIO_set_fp(out,stdout,BIO_NOCLOSE);
119
120 BN_GENCB_set(&_cb, &cb, out);
121 if(((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 64,
122 DH_GENERATOR_5, &_cb))
123 goto err;
124
125 if (!DH_check(a, &i)) goto err;
126 if (i & DH_CHECK_P_NOT_PRIME)
127 BIO_puts(out, "p value is not prime\n");
128 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
129 BIO_puts(out, "p value is not a safe prime\n");
130 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
131 BIO_puts(out, "unable to check the generator value\n");
132 if (i & DH_NOT_SUITABLE_GENERATOR)
133 BIO_puts(out, "the g value is not a generator\n");
134
135 BIO_puts(out,"\np =");
136 BN_print(out,a->p);
137 BIO_puts(out,"\ng =");
138 BN_print(out,a->g);
139 BIO_puts(out,"\n");
140
141 b=DH_new();
142 if (b == NULL) goto err;
143
144 b->p=BN_dup(a->p);
145 b->g=BN_dup(a->g);
146 if ((b->p == NULL) || (b->g == NULL)) goto err;
147
148 /* Set a to run with normal modexp and b to use constant time */
149 a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
150 b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
151
152 if (!DH_generate_key(a)) goto err;
153 BIO_puts(out,"pri 1=");
154 BN_print(out,a->priv_key);
155 BIO_puts(out,"\npub 1=");
156 BN_print(out,a->pub_key);
157 BIO_puts(out,"\n");
158
159 if (!DH_generate_key(b)) goto err;
160 BIO_puts(out,"pri 2=");
161 BN_print(out,b->priv_key);
162 BIO_puts(out,"\npub 2=");
163 BN_print(out,b->pub_key);
164 BIO_puts(out,"\n");
165
166 alen=DH_size(a);
167 abuf=(unsigned char *)OPENSSL_malloc(alen);
168 aout=DH_compute_key(abuf,b->pub_key,a);
169
170 BIO_puts(out,"key1 =");
171 for (i=0; i<aout; i++)
172 {
173 sprintf(buf,"%02X",abuf[i]);
174 BIO_puts(out,buf);
175 }
176 BIO_puts(out,"\n");
177
178 blen=DH_size(b);
179 bbuf=(unsigned char *)OPENSSL_malloc(blen);
180 bout=DH_compute_key(bbuf,a->pub_key,b);
181
182 BIO_puts(out,"key2 =");
183 for (i=0; i<bout; i++)
184 {
185 sprintf(buf,"%02X",bbuf[i]);
186 BIO_puts(out,buf);
187 }
188 BIO_puts(out,"\n");
189 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
190 {
191 fprintf(stderr,"Error in DH routines\n");
192 ret=1;
193 }
194 else
195 ret=0;
196err:
197 ERR_print_errors_fp(stderr);
198
199 if (abuf != NULL) OPENSSL_free(abuf);
200 if (bbuf != NULL) OPENSSL_free(bbuf);
201 if(b != NULL) DH_free(b);
202 if(a != NULL) DH_free(a);
203 BIO_free(out);
204#ifdef OPENSSL_SYS_NETWARE
205 if (ret) printf("ERROR: %d\n", ret);
206#endif
207 EXIT(ret);
208 return(ret);
209 }
210
211static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
212 {
213 char c='*';
214
215 if (p == 0) c='.';
216 if (p == 1) c='+';
217 if (p == 2) c='*';
218 if (p == 3) c='\n';
219 BIO_write(arg->arg,&c,1);
220 (void)BIO_flush(arg->arg);
221#ifdef LINT
222 p=n;
223#endif
224 return 1;
225 }
226#endif
diff --git a/src/lib/libcrypto/dsa/dsatest.c b/src/lib/libcrypto/dsa/dsatest.c
deleted file mode 100644
index edffd24e6b..0000000000
--- a/src/lib/libcrypto/dsa/dsatest.c
+++ /dev/null
@@ -1,259 +0,0 @@
1/* crypto/dsa/dsatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Until the key-gen callbacks are modified to use newer prototypes, we allow
60 * deprecated functions for openssl-internal code */
61#ifdef OPENSSL_NO_DEPRECATED
62#undef OPENSSL_NO_DEPRECATED
63#endif
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <sys/types.h>
69#include <sys/stat.h>
70
71#include "../e_os.h"
72
73#include <openssl/crypto.h>
74#include <openssl/rand.h>
75#include <openssl/bio.h>
76#include <openssl/err.h>
77#include <openssl/bn.h>
78
79#ifdef OPENSSL_NO_DSA
80int main(int argc, char *argv[])
81{
82 printf("No DSA support\n");
83 return(0);
84}
85#else
86#include <openssl/dsa.h>
87
88#ifdef OPENSSL_SYS_WIN16
89#define MS_CALLBACK _far _loadds
90#else
91#define MS_CALLBACK
92#endif
93
94static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg);
95
96/* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
97 * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
98static unsigned char seed[20]={
99 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
100 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
101 };
102
103static unsigned char out_p[]={
104 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
105 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
106 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
107 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
108 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
109 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
110 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
111 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91,
112 };
113
114static unsigned char out_q[]={
115 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
116 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
117 0xda,0xce,0x91,0x5f,
118 };
119
120static unsigned char out_g[]={
121 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
122 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
123 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
124 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
125 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
126 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
127 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
128 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02,
129 };
130
131static const unsigned char str1[]="12345678901234567890";
132
133static const char rnd_seed[] = "string to make the random number generator think it has entropy";
134
135static BIO *bio_err=NULL;
136
137int main(int argc, char **argv)
138 {
139 BN_GENCB cb;
140 DSA *dsa=NULL;
141 int counter,ret=0,i,j;
142 unsigned char buf[256];
143 unsigned long h;
144 unsigned char sig[256];
145 unsigned int siglen;
146
147 if (bio_err == NULL)
148 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
149
150 CRYPTO_malloc_debug_init();
151 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
152 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
153
154 ERR_load_crypto_strings();
155 RAND_seed(rnd_seed, sizeof rnd_seed);
156
157 BIO_printf(bio_err,"test generation of DSA parameters\n");
158
159 BN_GENCB_set(&cb, dsa_cb, bio_err);
160 if(((dsa = DSA_new()) == NULL) || !DSA_generate_parameters_ex(dsa, 512,
161 seed, 20, &counter, &h, &cb))
162 goto end;
163
164 BIO_printf(bio_err,"seed\n");
165 for (i=0; i<20; i+=4)
166 {
167 BIO_printf(bio_err,"%02X%02X%02X%02X ",
168 seed[i],seed[i+1],seed[i+2],seed[i+3]);
169 }
170 BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h);
171
172 DSA_print(bio_err,dsa,0);
173 if (counter != 105)
174 {
175 BIO_printf(bio_err,"counter should be 105\n");
176 goto end;
177 }
178 if (h != 2)
179 {
180 BIO_printf(bio_err,"h should be 2\n");
181 goto end;
182 }
183
184 i=BN_bn2bin(dsa->q,buf);
185 j=sizeof(out_q);
186 if ((i != j) || (memcmp(buf,out_q,i) != 0))
187 {
188 BIO_printf(bio_err,"q value is wrong\n");
189 goto end;
190 }
191
192 i=BN_bn2bin(dsa->p,buf);
193 j=sizeof(out_p);
194 if ((i != j) || (memcmp(buf,out_p,i) != 0))
195 {
196 BIO_printf(bio_err,"p value is wrong\n");
197 goto end;
198 }
199
200 i=BN_bn2bin(dsa->g,buf);
201 j=sizeof(out_g);
202 if ((i != j) || (memcmp(buf,out_g,i) != 0))
203 {
204 BIO_printf(bio_err,"g value is wrong\n");
205 goto end;
206 }
207
208 dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME;
209 DSA_generate_key(dsa);
210 DSA_sign(0, str1, 20, sig, &siglen, dsa);
211 if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
212 ret=1;
213
214 dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME;
215 DSA_generate_key(dsa);
216 DSA_sign(0, str1, 20, sig, &siglen, dsa);
217 if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
218 ret=1;
219
220end:
221 if (!ret)
222 ERR_print_errors(bio_err);
223 if (dsa != NULL) DSA_free(dsa);
224 CRYPTO_cleanup_all_ex_data();
225 ERR_remove_thread_state(NULL);
226 ERR_free_strings();
227 CRYPTO_mem_leaks(bio_err);
228 if (bio_err != NULL)
229 {
230 BIO_free(bio_err);
231 bio_err = NULL;
232 }
233#ifdef OPENSSL_SYS_NETWARE
234 if (!ret) printf("ERROR\n");
235#endif
236 EXIT(!ret);
237 return(0);
238 }
239
240static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg)
241 {
242 char c='*';
243 static int ok=0,num=0;
244
245 if (p == 0) { c='.'; num++; };
246 if (p == 1) c='+';
247 if (p == 2) { c='*'; ok++; }
248 if (p == 3) c='\n';
249 BIO_write(arg->arg,&c,1);
250 (void)BIO_flush(arg->arg);
251
252 if (!ok && (p == 0) && (num > 1))
253 {
254 BIO_printf((BIO *)arg,"error in dsatest\n");
255 return 0;
256 }
257 return 1;
258 }
259#endif
diff --git a/src/lib/libcrypto/ec/ectest.c b/src/lib/libcrypto/ec/ectest.c
deleted file mode 100644
index 102eaa9b23..0000000000
--- a/src/lib/libcrypto/ec/ectest.c
+++ /dev/null
@@ -1,1489 +0,0 @@
1/* crypto/ec/ectest.c */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <stdio.h>
73#include <stdlib.h>
74#ifdef FLAT_INC
75#include "e_os.h"
76#else
77#include "../e_os.h"
78#endif
79#include <string.h>
80#include <time.h>
81
82
83#ifdef OPENSSL_NO_EC
84int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
85#else
86
87
88#include <openssl/ec.h>
89#ifndef OPENSSL_NO_ENGINE
90#include <openssl/engine.h>
91#endif
92#include <openssl/err.h>
93#include <openssl/obj_mac.h>
94#include <openssl/objects.h>
95#include <openssl/rand.h>
96#include <openssl/bn.h>
97#include <openssl/opensslconf.h>
98
99#if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
100/* suppress "too big too optimize" warning */
101#pragma warning(disable:4959)
102#endif
103
104#define ABORT do { \
105 fflush(stdout); \
106 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
107 ERR_print_errors_fp(stderr); \
108 EXIT(1); \
109} while (0)
110
111#define TIMING_BASE_PT 0
112#define TIMING_RAND_PT 1
113#define TIMING_SIMUL 2
114
115#if 0
116static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
117 {
118 clock_t clck;
119 int i, j;
120 BIGNUM *s;
121 BIGNUM *r[10], *r0[10];
122 EC_POINT *P;
123
124 s = BN_new();
125 if (s == NULL) ABORT;
126
127 fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
128 if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
129 fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
130 fflush(stdout);
131
132 P = EC_POINT_new(group);
133 if (P == NULL) ABORT;
134 EC_POINT_copy(P, EC_GROUP_get0_generator(group));
135
136 for (i = 0; i < 10; i++)
137 {
138 if ((r[i] = BN_new()) == NULL) ABORT;
139 if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
140 if (type != TIMING_BASE_PT)
141 {
142 if ((r0[i] = BN_new()) == NULL) ABORT;
143 if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
144 }
145 }
146
147 clck = clock();
148 for (i = 0; i < 10; i++)
149 {
150 for (j = 0; j < 10; j++)
151 {
152 if (!EC_POINT_mul(group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
153 (type != TIMING_BASE_PT) ? P : NULL, (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx)) ABORT;
154 }
155 }
156 clck = clock() - clck;
157
158 fprintf(stdout, "\n");
159
160#ifdef CLOCKS_PER_SEC
161 /* "To determine the time in seconds, the value returned
162 * by the clock function should be divided by the value
163 * of the macro CLOCKS_PER_SEC."
164 * -- ISO/IEC 9899 */
165# define UNIT "s"
166#else
167 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
168 * -- cc on NeXTstep/OpenStep */
169# define UNIT "units"
170# define CLOCKS_PER_SEC 1
171#endif
172
173 if (type == TIMING_BASE_PT) {
174 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
175 "base point multiplications", (double)clck/CLOCKS_PER_SEC);
176 } else if (type == TIMING_RAND_PT) {
177 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
178 "random point multiplications", (double)clck/CLOCKS_PER_SEC);
179 } else if (type == TIMING_SIMUL) {
180 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
181 "s*P+t*Q operations", (double)clck/CLOCKS_PER_SEC);
182 }
183 fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
184
185 EC_POINT_free(P);
186 BN_free(s);
187 for (i = 0; i < 10; i++)
188 {
189 BN_free(r[i]);
190 if (type != TIMING_BASE_PT) BN_free(r0[i]);
191 }
192 }
193#endif
194
195/* test multiplication with group order, long and negative scalars */
196static void group_order_tests(EC_GROUP *group)
197 {
198 BIGNUM *n1, *n2, *order;
199 EC_POINT *P = EC_POINT_new(group);
200 EC_POINT *Q = EC_POINT_new(group);
201 BN_CTX *ctx = BN_CTX_new();
202
203 n1 = BN_new(); n2 = BN_new(); order = BN_new();
204 fprintf(stdout, "verify group order ...");
205 fflush(stdout);
206 if (!EC_GROUP_get_order(group, order, ctx)) ABORT;
207 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
208 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
209 fprintf(stdout, ".");
210 fflush(stdout);
211 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
212 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
213 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
214 fprintf(stdout, " ok\n");
215 fprintf(stdout, "long/negative scalar tests ... ");
216 if (!BN_one(n1)) ABORT;
217 /* n1 = 1 - order */
218 if (!BN_sub(n1, n1, order)) ABORT;
219 if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
220 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
221 /* n2 = 1 + order */
222 if (!BN_add(n2, order, BN_value_one())) ABORT;
223 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
224 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
225 /* n2 = (1 - order) * (1 + order) */
226 if (!BN_mul(n2, n1, n2, ctx)) ABORT;
227 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
228 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
229 fprintf(stdout, "ok\n");
230 EC_POINT_free(P);
231 EC_POINT_free(Q);
232 BN_free(n1);
233 BN_free(n2);
234 BN_free(order);
235 BN_CTX_free(ctx);
236 }
237
238static void prime_field_tests(void)
239 {
240 BN_CTX *ctx = NULL;
241 BIGNUM *p, *a, *b;
242 EC_GROUP *group;
243 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
244 EC_POINT *P, *Q, *R;
245 BIGNUM *x, *y, *z;
246 unsigned char buf[100];
247 size_t i, len;
248 int k;
249
250#if 1 /* optional */
251 ctx = BN_CTX_new();
252 if (!ctx) ABORT;
253#endif
254
255 p = BN_new();
256 a = BN_new();
257 b = BN_new();
258 if (!p || !a || !b) ABORT;
259
260 if (!BN_hex2bn(&p, "17")) ABORT;
261 if (!BN_hex2bn(&a, "1")) ABORT;
262 if (!BN_hex2bn(&b, "1")) ABORT;
263
264 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
265 * so that the library gets to choose the EC_METHOD */
266 if (!group) ABORT;
267
268 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
269
270 {
271 EC_GROUP *tmp;
272 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
273 if (!tmp) ABORT;
274 if (!EC_GROUP_copy(tmp, group)) ABORT;
275 EC_GROUP_free(group);
276 group = tmp;
277 }
278
279 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
280
281 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
282 BN_print_fp(stdout, p);
283 fprintf(stdout, ")\n a = 0x");
284 BN_print_fp(stdout, a);
285 fprintf(stdout, "\n b = 0x");
286 BN_print_fp(stdout, b);
287 fprintf(stdout, "\n");
288
289 P = EC_POINT_new(group);
290 Q = EC_POINT_new(group);
291 R = EC_POINT_new(group);
292 if (!P || !Q || !R) ABORT;
293
294 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
295 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
296
297 buf[0] = 0;
298 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
299
300 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
301 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
302
303 x = BN_new();
304 y = BN_new();
305 z = BN_new();
306 if (!x || !y || !z) ABORT;
307
308 if (!BN_hex2bn(&x, "D")) ABORT;
309 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
310 if (!EC_POINT_is_on_curve(group, Q, ctx))
311 {
312 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
313 fprintf(stderr, "Point is not on curve: x = 0x");
314 BN_print_fp(stderr, x);
315 fprintf(stderr, ", y = 0x");
316 BN_print_fp(stderr, y);
317 fprintf(stderr, "\n");
318 ABORT;
319 }
320
321 fprintf(stdout, "A cyclic subgroup:\n");
322 k = 100;
323 do
324 {
325 if (k-- == 0) ABORT;
326
327 if (EC_POINT_is_at_infinity(group, P))
328 fprintf(stdout, " point at infinity\n");
329 else
330 {
331 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
332
333 fprintf(stdout, " x = 0x");
334 BN_print_fp(stdout, x);
335 fprintf(stdout, ", y = 0x");
336 BN_print_fp(stdout, y);
337 fprintf(stdout, "\n");
338 }
339
340 if (!EC_POINT_copy(R, P)) ABORT;
341 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
342
343#if 0 /* optional */
344 {
345 EC_POINT *points[3];
346
347 points[0] = R;
348 points[1] = Q;
349 points[2] = P;
350 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
351 }
352#endif
353
354 }
355 while (!EC_POINT_is_at_infinity(group, P));
356
357 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
358 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
359
360 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
361 if (len == 0) ABORT;
362 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
363 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
364 fprintf(stdout, "Generator as octet string, compressed form:\n ");
365 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
366
367 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
368 if (len == 0) ABORT;
369 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
370 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
371 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
372 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
373
374 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
375 if (len == 0) ABORT;
376 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
377 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
378 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
379 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
380
381 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
382 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
383 BN_print_fp(stdout, x);
384 fprintf(stdout, ", Y = 0x");
385 BN_print_fp(stdout, y);
386 fprintf(stdout, ", Z = 0x");
387 BN_print_fp(stdout, z);
388 fprintf(stdout, "\n");
389
390 if (!EC_POINT_invert(group, P, ctx)) ABORT;
391 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
392
393
394 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
395 * -- not a NIST curve, but commonly used */
396
397 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
398 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
399 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
400 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
401 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
402
403 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
404 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
405 if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
406 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
407 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
408 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
409
410 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
411 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
412 BN_print_fp(stdout, x);
413 fprintf(stdout, "\n y = 0x");
414 BN_print_fp(stdout, y);
415 fprintf(stdout, "\n");
416 /* G_y value taken from the standard: */
417 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
418 if (0 != BN_cmp(y, z)) ABORT;
419
420 fprintf(stdout, "verify degree ...");
421 if (EC_GROUP_get_degree(group) != 160) ABORT;
422 fprintf(stdout, " ok\n");
423
424 group_order_tests(group);
425
426 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
427 if (!EC_GROUP_copy(P_160, group)) ABORT;
428
429
430 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
431
432 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
433 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
434 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
435 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
436 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
437
438 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
439 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
440 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
441 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
442 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
443
444 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
445 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
446 BN_print_fp(stdout, x);
447 fprintf(stdout, "\n y = 0x");
448 BN_print_fp(stdout, y);
449 fprintf(stdout, "\n");
450 /* G_y value taken from the standard: */
451 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
452 if (0 != BN_cmp(y, z)) ABORT;
453
454 fprintf(stdout, "verify degree ...");
455 if (EC_GROUP_get_degree(group) != 192) ABORT;
456 fprintf(stdout, " ok\n");
457
458 group_order_tests(group);
459
460 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
461 if (!EC_GROUP_copy(P_192, group)) ABORT;
462
463
464 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
465
466 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
467 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
468 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
469 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
470 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
471
472 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
473 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
474 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
475 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
476 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
477
478 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
479 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
480 BN_print_fp(stdout, x);
481 fprintf(stdout, "\n y = 0x");
482 BN_print_fp(stdout, y);
483 fprintf(stdout, "\n");
484 /* G_y value taken from the standard: */
485 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
486 if (0 != BN_cmp(y, z)) ABORT;
487
488 fprintf(stdout, "verify degree ...");
489 if (EC_GROUP_get_degree(group) != 224) ABORT;
490 fprintf(stdout, " ok\n");
491
492 group_order_tests(group);
493
494 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
495 if (!EC_GROUP_copy(P_224, group)) ABORT;
496
497
498 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
499
500 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
501 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
502 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
503 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
504 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
505
506 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
507 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
508 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
509 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
510 "84F3B9CAC2FC632551")) ABORT;
511 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
512
513 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
514 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
515 BN_print_fp(stdout, x);
516 fprintf(stdout, "\n y = 0x");
517 BN_print_fp(stdout, y);
518 fprintf(stdout, "\n");
519 /* G_y value taken from the standard: */
520 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
521 if (0 != BN_cmp(y, z)) ABORT;
522
523 fprintf(stdout, "verify degree ...");
524 if (EC_GROUP_get_degree(group) != 256) ABORT;
525 fprintf(stdout, " ok\n");
526
527 group_order_tests(group);
528
529 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
530 if (!EC_GROUP_copy(P_256, group)) ABORT;
531
532
533 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
534
535 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
537 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
538 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
539 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
540 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
541 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
542 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
543
544 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
545 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
546 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
547 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
548 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
549 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
550 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
551
552 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
553 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
554 BN_print_fp(stdout, x);
555 fprintf(stdout, "\n y = 0x");
556 BN_print_fp(stdout, y);
557 fprintf(stdout, "\n");
558 /* G_y value taken from the standard: */
559 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
560 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
561 if (0 != BN_cmp(y, z)) ABORT;
562
563 fprintf(stdout, "verify degree ...");
564 if (EC_GROUP_get_degree(group) != 384) ABORT;
565 fprintf(stdout, " ok\n");
566
567 group_order_tests(group);
568
569 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
570 if (!EC_GROUP_copy(P_384, group)) ABORT;
571
572
573 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
574
575 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
576 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
577 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
578 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
579 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
580 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
581 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
582 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
583 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
584 "DF883D2C34F1EF451FD46B503F00")) ABORT;
585 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
586
587 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
588 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
589 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
590 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
591 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
592 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
593 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
594 "C9B8899C47AEBB6FB71E91386409")) ABORT;
595 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
596
597 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
598 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
599 BN_print_fp(stdout, x);
600 fprintf(stdout, "\n y = 0x");
601 BN_print_fp(stdout, y);
602 fprintf(stdout, "\n");
603 /* G_y value taken from the standard: */
604 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
605 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
606 "7086A272C24088BE94769FD16650")) ABORT;
607 if (0 != BN_cmp(y, z)) ABORT;
608
609 fprintf(stdout, "verify degree ...");
610 if (EC_GROUP_get_degree(group) != 521) ABORT;
611 fprintf(stdout, " ok\n");
612
613 group_order_tests(group);
614
615 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
616 if (!EC_GROUP_copy(P_521, group)) ABORT;
617
618
619 /* more tests using the last curve */
620
621 if (!EC_POINT_copy(Q, P)) ABORT;
622 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
623 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
624 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
625 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
626
627 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
628 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
629 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
630
631 {
632 const EC_POINT *points[4];
633 const BIGNUM *scalars[4];
634 BIGNUM scalar3;
635
636 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
637 points[0] = Q;
638 points[1] = Q;
639 points[2] = Q;
640 points[3] = Q;
641
642 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
643 if (!BN_add(y, z, BN_value_one())) ABORT;
644 if (BN_is_odd(y)) ABORT;
645 if (!BN_rshift1(y, y)) ABORT;
646 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
647 scalars[1] = y;
648
649 fprintf(stdout, "combined multiplication ...");
650 fflush(stdout);
651
652 /* z is still the group order */
653 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
654 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
655 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
656 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
657
658 fprintf(stdout, ".");
659 fflush(stdout);
660
661 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
662 if (!BN_add(z, z, y)) ABORT;
663 BN_set_negative(z, 1);
664 scalars[0] = y;
665 scalars[1] = z; /* z = -(order + y) */
666
667 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
668 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
669
670 fprintf(stdout, ".");
671 fflush(stdout);
672
673 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
674 if (!BN_add(z, x, y)) ABORT;
675 BN_set_negative(z, 1);
676 scalars[0] = x;
677 scalars[1] = y;
678 scalars[2] = z; /* z = -(x+y) */
679
680 BN_init(&scalar3);
681 BN_zero(&scalar3);
682 scalars[3] = &scalar3;
683
684 if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) ABORT;
685 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
686
687 fprintf(stdout, " ok\n\n");
688
689 BN_free(&scalar3);
690 }
691
692
693#if 0
694 timings(P_160, TIMING_BASE_PT, ctx);
695 timings(P_160, TIMING_RAND_PT, ctx);
696 timings(P_160, TIMING_SIMUL, ctx);
697 timings(P_192, TIMING_BASE_PT, ctx);
698 timings(P_192, TIMING_RAND_PT, ctx);
699 timings(P_192, TIMING_SIMUL, ctx);
700 timings(P_224, TIMING_BASE_PT, ctx);
701 timings(P_224, TIMING_RAND_PT, ctx);
702 timings(P_224, TIMING_SIMUL, ctx);
703 timings(P_256, TIMING_BASE_PT, ctx);
704 timings(P_256, TIMING_RAND_PT, ctx);
705 timings(P_256, TIMING_SIMUL, ctx);
706 timings(P_384, TIMING_BASE_PT, ctx);
707 timings(P_384, TIMING_RAND_PT, ctx);
708 timings(P_384, TIMING_SIMUL, ctx);
709 timings(P_521, TIMING_BASE_PT, ctx);
710 timings(P_521, TIMING_RAND_PT, ctx);
711 timings(P_521, TIMING_SIMUL, ctx);
712#endif
713
714
715 if (ctx)
716 BN_CTX_free(ctx);
717 BN_free(p); BN_free(a); BN_free(b);
718 EC_GROUP_free(group);
719 EC_POINT_free(P);
720 EC_POINT_free(Q);
721 EC_POINT_free(R);
722 BN_free(x); BN_free(y); BN_free(z);
723
724 if (P_160) EC_GROUP_free(P_160);
725 if (P_192) EC_GROUP_free(P_192);
726 if (P_224) EC_GROUP_free(P_224);
727 if (P_256) EC_GROUP_free(P_256);
728 if (P_384) EC_GROUP_free(P_384);
729 if (P_521) EC_GROUP_free(P_521);
730
731 }
732
733/* Change test based on whether binary point compression is enabled or not. */
734#ifdef OPENSSL_EC_BIN_PT_COMP
735#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
736 if (!BN_hex2bn(&x, _x)) ABORT; \
737 if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
738 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
739 if (!BN_hex2bn(&z, _order)) ABORT; \
740 if (!BN_hex2bn(&cof, _cof)) ABORT; \
741 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
742 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
743 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
744 BN_print_fp(stdout, x); \
745 fprintf(stdout, "\n y = 0x"); \
746 BN_print_fp(stdout, y); \
747 fprintf(stdout, "\n"); \
748 /* G_y value taken from the standard: */ \
749 if (!BN_hex2bn(&z, _y)) ABORT; \
750 if (0 != BN_cmp(y, z)) ABORT;
751#else
752#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
753 if (!BN_hex2bn(&x, _x)) ABORT; \
754 if (!BN_hex2bn(&y, _y)) ABORT; \
755 if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
756 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
757 if (!BN_hex2bn(&z, _order)) ABORT; \
758 if (!BN_hex2bn(&cof, _cof)) ABORT; \
759 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
760 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
761 BN_print_fp(stdout, x); \
762 fprintf(stdout, "\n y = 0x"); \
763 BN_print_fp(stdout, y); \
764 fprintf(stdout, "\n");
765#endif
766
767#define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
768 if (!BN_hex2bn(&p, _p)) ABORT; \
769 if (!BN_hex2bn(&a, _a)) ABORT; \
770 if (!BN_hex2bn(&b, _b)) ABORT; \
771 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
772 CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
773 fprintf(stdout, "verify degree ..."); \
774 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
775 fprintf(stdout, " ok\n"); \
776 group_order_tests(group); \
777 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
778 if (!EC_GROUP_copy(_variable, group)) ABORT; \
779
780#ifndef OPENSSL_NO_EC2M
781
782static void char2_field_tests(void)
783 {
784 BN_CTX *ctx = NULL;
785 BIGNUM *p, *a, *b;
786 EC_GROUP *group;
787 EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
788 EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
789 EC_POINT *P, *Q, *R;
790 BIGNUM *x, *y, *z, *cof;
791 unsigned char buf[100];
792 size_t i, len;
793 int k;
794
795#if 1 /* optional */
796 ctx = BN_CTX_new();
797 if (!ctx) ABORT;
798#endif
799
800 p = BN_new();
801 a = BN_new();
802 b = BN_new();
803 if (!p || !a || !b) ABORT;
804
805 if (!BN_hex2bn(&p, "13")) ABORT;
806 if (!BN_hex2bn(&a, "3")) ABORT;
807 if (!BN_hex2bn(&b, "1")) ABORT;
808
809 group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
810 * so that the library gets to choose the EC_METHOD */
811 if (!group) ABORT;
812 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
813
814 {
815 EC_GROUP *tmp;
816 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
817 if (!tmp) ABORT;
818 if (!EC_GROUP_copy(tmp, group)) ABORT;
819 EC_GROUP_free(group);
820 group = tmp;
821 }
822
823 if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
824
825 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
826 BN_print_fp(stdout, p);
827 fprintf(stdout, ")\n a = 0x");
828 BN_print_fp(stdout, a);
829 fprintf(stdout, "\n b = 0x");
830 BN_print_fp(stdout, b);
831 fprintf(stdout, "\n(0x... means binary polynomial)\n");
832
833 P = EC_POINT_new(group);
834 Q = EC_POINT_new(group);
835 R = EC_POINT_new(group);
836 if (!P || !Q || !R) ABORT;
837
838 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
839 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
840
841 buf[0] = 0;
842 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
843
844 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
845 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
846
847 x = BN_new();
848 y = BN_new();
849 z = BN_new();
850 cof = BN_new();
851 if (!x || !y || !z || !cof) ABORT;
852
853 if (!BN_hex2bn(&x, "6")) ABORT;
854/* Change test based on whether binary point compression is enabled or not. */
855#ifdef OPENSSL_EC_BIN_PT_COMP
856 if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
857#else
858 if (!BN_hex2bn(&y, "8")) ABORT;
859 if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
860#endif
861 if (!EC_POINT_is_on_curve(group, Q, ctx))
862 {
863/* Change test based on whether binary point compression is enabled or not. */
864#ifdef OPENSSL_EC_BIN_PT_COMP
865 if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
866#endif
867 fprintf(stderr, "Point is not on curve: x = 0x");
868 BN_print_fp(stderr, x);
869 fprintf(stderr, ", y = 0x");
870 BN_print_fp(stderr, y);
871 fprintf(stderr, "\n");
872 ABORT;
873 }
874
875 fprintf(stdout, "A cyclic subgroup:\n");
876 k = 100;
877 do
878 {
879 if (k-- == 0) ABORT;
880
881 if (EC_POINT_is_at_infinity(group, P))
882 fprintf(stdout, " point at infinity\n");
883 else
884 {
885 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
886
887 fprintf(stdout, " x = 0x");
888 BN_print_fp(stdout, x);
889 fprintf(stdout, ", y = 0x");
890 BN_print_fp(stdout, y);
891 fprintf(stdout, "\n");
892 }
893
894 if (!EC_POINT_copy(R, P)) ABORT;
895 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
896 }
897 while (!EC_POINT_is_at_infinity(group, P));
898
899 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
900 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
901
902/* Change test based on whether binary point compression is enabled or not. */
903#ifdef OPENSSL_EC_BIN_PT_COMP
904 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
905 if (len == 0) ABORT;
906 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
907 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
908 fprintf(stdout, "Generator as octet string, compressed form:\n ");
909 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
910#endif
911
912 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
913 if (len == 0) ABORT;
914 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
915 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
916 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
917 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
918
919/* Change test based on whether binary point compression is enabled or not. */
920#ifdef OPENSSL_EC_BIN_PT_COMP
921 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
922 if (len == 0) ABORT;
923 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
924 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
925 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
926 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
927#endif
928
929 fprintf(stdout, "\n");
930
931 if (!EC_POINT_invert(group, P, ctx)) ABORT;
932 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
933
934
935 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
936 CHAR2_CURVE_TEST
937 (
938 "NIST curve K-163",
939 "0800000000000000000000000000000000000000C9",
940 "1",
941 "1",
942 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
943 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
944 1,
945 "04000000000000000000020108A2E0CC0D99F8A5EF",
946 "2",
947 163,
948 C2_K163
949 );
950
951 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
952 CHAR2_CURVE_TEST
953 (
954 "NIST curve B-163",
955 "0800000000000000000000000000000000000000C9",
956 "1",
957 "020A601907B8C953CA1481EB10512F78744A3205FD",
958 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
959 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
960 1,
961 "040000000000000000000292FE77E70C12A4234C33",
962 "2",
963 163,
964 C2_B163
965 );
966
967 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
968 CHAR2_CURVE_TEST
969 (
970 "NIST curve K-233",
971 "020000000000000000000000000000000000000004000000000000000001",
972 "0",
973 "1",
974 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
975 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
976 0,
977 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
978 "4",
979 233,
980 C2_K233
981 );
982
983 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
984 CHAR2_CURVE_TEST
985 (
986 "NIST curve B-233",
987 "020000000000000000000000000000000000000004000000000000000001",
988 "000000000000000000000000000000000000000000000000000000000001",
989 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
990 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
991 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
992 1,
993 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
994 "2",
995 233,
996 C2_B233
997 );
998
999 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
1000 CHAR2_CURVE_TEST
1001 (
1002 "NIST curve K-283",
1003 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1004 "0",
1005 "1",
1006 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
1007 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
1008 0,
1009 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
1010 "4",
1011 283,
1012 C2_K283
1013 );
1014
1015 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
1016 CHAR2_CURVE_TEST
1017 (
1018 "NIST curve B-283",
1019 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1020 "000000000000000000000000000000000000000000000000000000000000000000000001",
1021 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
1022 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
1023 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
1024 1,
1025 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
1026 "2",
1027 283,
1028 C2_B283
1029 );
1030
1031 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
1032 CHAR2_CURVE_TEST
1033 (
1034 "NIST curve K-409",
1035 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1036 "0",
1037 "1",
1038 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
1039 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
1040 1,
1041 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
1042 "4",
1043 409,
1044 C2_K409
1045 );
1046
1047 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
1048 CHAR2_CURVE_TEST
1049 (
1050 "NIST curve B-409",
1051 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1052 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1053 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
1054 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
1055 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1056 1,
1057 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1058 "2",
1059 409,
1060 C2_B409
1061 );
1062
1063 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1064 CHAR2_CURVE_TEST
1065 (
1066 "NIST curve K-571",
1067 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1068 "0",
1069 "1",
1070 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1071 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1072 0,
1073 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1074 "4",
1075 571,
1076 C2_K571
1077 );
1078
1079 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1080 CHAR2_CURVE_TEST
1081 (
1082 "NIST curve B-571",
1083 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1084 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1085 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1086 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1087 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1088 1,
1089 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1090 "2",
1091 571,
1092 C2_B571
1093 );
1094
1095 /* more tests using the last curve */
1096
1097 if (!EC_POINT_copy(Q, P)) ABORT;
1098 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1099 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
1100 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
1101 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
1102
1103 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
1104 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
1105 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
1106
1107 {
1108 const EC_POINT *points[3];
1109 const BIGNUM *scalars[3];
1110
1111 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1112 points[0] = Q;
1113 points[1] = Q;
1114 points[2] = Q;
1115
1116 if (!BN_add(y, z, BN_value_one())) ABORT;
1117 if (BN_is_odd(y)) ABORT;
1118 if (!BN_rshift1(y, y)) ABORT;
1119 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
1120 scalars[1] = y;
1121
1122 fprintf(stdout, "combined multiplication ...");
1123 fflush(stdout);
1124
1125 /* z is still the group order */
1126 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1127 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1128 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1129 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1130
1131 fprintf(stdout, ".");
1132 fflush(stdout);
1133
1134 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1135 if (!BN_add(z, z, y)) ABORT;
1136 BN_set_negative(z, 1);
1137 scalars[0] = y;
1138 scalars[1] = z; /* z = -(order + y) */
1139
1140 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1141 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1142
1143 fprintf(stdout, ".");
1144 fflush(stdout);
1145
1146 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1147 if (!BN_add(z, x, y)) ABORT;
1148 BN_set_negative(z, 1);
1149 scalars[0] = x;
1150 scalars[1] = y;
1151 scalars[2] = z; /* z = -(x+y) */
1152
1153 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1154 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1155
1156 fprintf(stdout, " ok\n\n");
1157 }
1158
1159
1160#if 0
1161 timings(C2_K163, TIMING_BASE_PT, ctx);
1162 timings(C2_K163, TIMING_RAND_PT, ctx);
1163 timings(C2_K163, TIMING_SIMUL, ctx);
1164 timings(C2_B163, TIMING_BASE_PT, ctx);
1165 timings(C2_B163, TIMING_RAND_PT, ctx);
1166 timings(C2_B163, TIMING_SIMUL, ctx);
1167 timings(C2_K233, TIMING_BASE_PT, ctx);
1168 timings(C2_K233, TIMING_RAND_PT, ctx);
1169 timings(C2_K233, TIMING_SIMUL, ctx);
1170 timings(C2_B233, TIMING_BASE_PT, ctx);
1171 timings(C2_B233, TIMING_RAND_PT, ctx);
1172 timings(C2_B233, TIMING_SIMUL, ctx);
1173 timings(C2_K283, TIMING_BASE_PT, ctx);
1174 timings(C2_K283, TIMING_RAND_PT, ctx);
1175 timings(C2_K283, TIMING_SIMUL, ctx);
1176 timings(C2_B283, TIMING_BASE_PT, ctx);
1177 timings(C2_B283, TIMING_RAND_PT, ctx);
1178 timings(C2_B283, TIMING_SIMUL, ctx);
1179 timings(C2_K409, TIMING_BASE_PT, ctx);
1180 timings(C2_K409, TIMING_RAND_PT, ctx);
1181 timings(C2_K409, TIMING_SIMUL, ctx);
1182 timings(C2_B409, TIMING_BASE_PT, ctx);
1183 timings(C2_B409, TIMING_RAND_PT, ctx);
1184 timings(C2_B409, TIMING_SIMUL, ctx);
1185 timings(C2_K571, TIMING_BASE_PT, ctx);
1186 timings(C2_K571, TIMING_RAND_PT, ctx);
1187 timings(C2_K571, TIMING_SIMUL, ctx);
1188 timings(C2_B571, TIMING_BASE_PT, ctx);
1189 timings(C2_B571, TIMING_RAND_PT, ctx);
1190 timings(C2_B571, TIMING_SIMUL, ctx);
1191#endif
1192
1193
1194 if (ctx)
1195 BN_CTX_free(ctx);
1196 BN_free(p); BN_free(a); BN_free(b);
1197 EC_GROUP_free(group);
1198 EC_POINT_free(P);
1199 EC_POINT_free(Q);
1200 EC_POINT_free(R);
1201 BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1202
1203 if (C2_K163) EC_GROUP_free(C2_K163);
1204 if (C2_B163) EC_GROUP_free(C2_B163);
1205 if (C2_K233) EC_GROUP_free(C2_K233);
1206 if (C2_B233) EC_GROUP_free(C2_B233);
1207 if (C2_K283) EC_GROUP_free(C2_K283);
1208 if (C2_B283) EC_GROUP_free(C2_B283);
1209 if (C2_K409) EC_GROUP_free(C2_K409);
1210 if (C2_B409) EC_GROUP_free(C2_B409);
1211 if (C2_K571) EC_GROUP_free(C2_K571);
1212 if (C2_B571) EC_GROUP_free(C2_B571);
1213
1214 }
1215#endif
1216
1217static void internal_curve_test(void)
1218 {
1219 EC_builtin_curve *curves = NULL;
1220 size_t crv_len = 0, n = 0;
1221 int ok = 1;
1222
1223 crv_len = EC_get_builtin_curves(NULL, 0);
1224
1225 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1226
1227 if (curves == NULL)
1228 return;
1229
1230 if (!EC_get_builtin_curves(curves, crv_len))
1231 {
1232 OPENSSL_free(curves);
1233 return;
1234 }
1235
1236 fprintf(stdout, "testing internal curves: ");
1237
1238 for (n = 0; n < crv_len; n++)
1239 {
1240 EC_GROUP *group = NULL;
1241 int nid = curves[n].nid;
1242 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL)
1243 {
1244 ok = 0;
1245 fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
1246 " curve %s\n", OBJ_nid2sn(nid));
1247 /* try next curve */
1248 continue;
1249 }
1250 if (!EC_GROUP_check(group, NULL))
1251 {
1252 ok = 0;
1253 fprintf(stdout, "\nEC_GROUP_check() failed with"
1254 " curve %s\n", OBJ_nid2sn(nid));
1255 EC_GROUP_free(group);
1256 /* try the next curve */
1257 continue;
1258 }
1259 fprintf(stdout, ".");
1260 fflush(stdout);
1261 EC_GROUP_free(group);
1262 }
1263 if (ok)
1264 fprintf(stdout, " ok\n\n");
1265 else
1266 {
1267 fprintf(stdout, " failed\n\n");
1268 ABORT;
1269 }
1270 OPENSSL_free(curves);
1271 return;
1272 }
1273
1274#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1275/* nistp_test_params contains magic numbers for testing our optimized
1276 * implementations of several NIST curves with characteristic > 3. */
1277struct nistp_test_params
1278 {
1279 const EC_METHOD* (*meth) ();
1280 int degree;
1281 /* Qx, Qy and D are taken from
1282 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1283 * Otherwise, values are standard curve parameters from FIPS 180-3 */
1284 const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
1285 };
1286
1287static const struct nistp_test_params nistp_tests_params[] =
1288 {
1289 {
1290 /* P-224 */
1291 EC_GFp_nistp224_method,
1292 224,
1293 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* p */
1294 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* a */
1295 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* b */
1296 "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", /* Qx */
1297 "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", /* Qy */
1298 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
1299 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
1300 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
1301 "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", /* d */
1302 },
1303 {
1304 /* P-256 */
1305 EC_GFp_nistp256_method,
1306 256,
1307 "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", /* p */
1308 "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", /* a */
1309 "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", /* b */
1310 "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", /* Qx */
1311 "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", /* Qy */
1312 "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", /* Gx */
1313 "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", /* Gy */
1314 "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", /* order */
1315 "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", /* d */
1316 },
1317 {
1318 /* P-521 */
1319 EC_GFp_nistp521_method,
1320 521,
1321 "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", /* p */
1322 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", /* a */
1323 "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", /* b */
1324 "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", /* Qx */
1325 "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", /* Qy */
1326 "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", /* Gx */
1327 "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", /* Gy */
1328 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", /* order */
1329 "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", /* d */
1330 },
1331 };
1332
1333void nistp_single_test(const struct nistp_test_params *test)
1334 {
1335 BN_CTX *ctx;
1336 BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
1337 EC_GROUP *NISTP;
1338 EC_POINT *G, *P, *Q, *Q_CHECK;
1339
1340 fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n", test->degree);
1341 ctx = BN_CTX_new();
1342 p = BN_new();
1343 a = BN_new();
1344 b = BN_new();
1345 x = BN_new(); y = BN_new();
1346 m = BN_new(); n = BN_new(); order = BN_new();
1347
1348 NISTP = EC_GROUP_new(test->meth());
1349 if(!NISTP) ABORT;
1350 if (!BN_hex2bn(&p, test->p)) ABORT;
1351 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
1352 if (!BN_hex2bn(&a, test->a)) ABORT;
1353 if (!BN_hex2bn(&b, test->b)) ABORT;
1354 if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) ABORT;
1355 G = EC_POINT_new(NISTP);
1356 P = EC_POINT_new(NISTP);
1357 Q = EC_POINT_new(NISTP);
1358 Q_CHECK = EC_POINT_new(NISTP);
1359 if(!BN_hex2bn(&x, test->Qx)) ABORT;
1360 if(!BN_hex2bn(&y, test->Qy)) ABORT;
1361 if(!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx)) ABORT;
1362 if (!BN_hex2bn(&x, test->Gx)) ABORT;
1363 if (!BN_hex2bn(&y, test->Gy)) ABORT;
1364 if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) ABORT;
1365 if (!BN_hex2bn(&order, test->order)) ABORT;
1366 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1367
1368 fprintf(stdout, "verify degree ... ");
1369 if (EC_GROUP_get_degree(NISTP) != test->degree) ABORT;
1370 fprintf(stdout, "ok\n");
1371
1372 fprintf(stdout, "NIST test vectors ... ");
1373 if (!BN_hex2bn(&n, test->d)) ABORT;
1374 /* fixed point multiplication */
1375 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1376 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1377 /* random point multiplication */
1378 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1379 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1380
1381 /* set generator to P = 2*G, where G is the standard generator */
1382 if (!EC_POINT_dbl(NISTP, P, G, ctx)) ABORT;
1383 if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) ABORT;
1384 /* set the scalar to m=n/2, where n is the NIST test scalar */
1385 if (!BN_rshift(m, n, 1)) ABORT;
1386
1387 /* test the non-standard generator */
1388 /* fixed point multiplication */
1389 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1390 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1391 /* random point multiplication */
1392 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1393 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1394
1395 /* now repeat all tests with precomputation */
1396 if (!EC_GROUP_precompute_mult(NISTP, ctx)) ABORT;
1397
1398 /* fixed point multiplication */
1399 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1400 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1401 /* random point multiplication */
1402 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1403 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1404
1405 /* reset generator */
1406 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1407 /* fixed point multiplication */
1408 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1409 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1410 /* random point multiplication */
1411 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1412 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1413
1414 fprintf(stdout, "ok\n");
1415 group_order_tests(NISTP);
1416#if 0
1417 timings(NISTP, TIMING_BASE_PT, ctx);
1418 timings(NISTP, TIMING_RAND_PT, ctx);
1419#endif
1420 EC_GROUP_free(NISTP);
1421 EC_POINT_free(G);
1422 EC_POINT_free(P);
1423 EC_POINT_free(Q);
1424 EC_POINT_free(Q_CHECK);
1425 BN_free(n);
1426 BN_free(m);
1427 BN_free(p);
1428 BN_free(a);
1429 BN_free(b);
1430 BN_free(x);
1431 BN_free(y);
1432 BN_free(order);
1433 BN_CTX_free(ctx);
1434 }
1435
1436void nistp_tests()
1437 {
1438 unsigned i;
1439
1440 for (i = 0; i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params); i++)
1441 {
1442 nistp_single_test(&nistp_tests_params[i]);
1443 }
1444 }
1445#endif
1446
1447static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1448
1449int main(int argc, char *argv[])
1450 {
1451
1452 /* enable memory leak checking unless explicitly disabled */
1453 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1454 {
1455 CRYPTO_malloc_debug_init();
1456 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1457 }
1458 else
1459 {
1460 /* OPENSSL_DEBUG_MEMORY=off */
1461 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1462 }
1463 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1464 ERR_load_crypto_strings();
1465
1466 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1467
1468 prime_field_tests();
1469 puts("");
1470#ifndef OPENSSL_NO_EC2M
1471 char2_field_tests();
1472#endif
1473#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1474 nistp_tests();
1475#endif
1476 /* test the internal curves */
1477 internal_curve_test();
1478
1479#ifndef OPENSSL_NO_ENGINE
1480 ENGINE_cleanup();
1481#endif
1482 CRYPTO_cleanup_all_ex_data();
1483 ERR_free_strings();
1484 ERR_remove_thread_state(NULL);
1485 CRYPTO_mem_leaks_fp(stderr);
1486
1487 return 0;
1488 }
1489#endif
diff --git a/src/lib/libcrypto/ecdh/ecdhtest.c b/src/lib/libcrypto/ecdh/ecdhtest.c
deleted file mode 100644
index 823d7baa65..0000000000
--- a/src/lib/libcrypto/ecdh/ecdhtest.c
+++ /dev/null
@@ -1,374 +0,0 @@
1/* crypto/ecdh/ecdhtest.c */
2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 *
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
8 *
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
11 *
12 * The ECDH software is originally written by Douglas Stebila of
13 * Sun Microsystems Laboratories.
14 *
15 */
16/* ====================================================================
17 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 *
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 *
31 * 3. All advertising materials mentioning features or use of this
32 * software must display the following acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35 *
36 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37 * endorse or promote products derived from this software without
38 * prior written permission. For written permission, please contact
39 * openssl-core@openssl.org.
40 *
41 * 5. Products derived from this software may not be called "OpenSSL"
42 * nor may "OpenSSL" appear in their names without prior written
43 * permission of the OpenSSL Project.
44 *
45 * 6. Redistributions of any form whatsoever must retain the following
46 * acknowledgment:
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61 * OF THE POSSIBILITY OF SUCH DAMAGE.
62 * ====================================================================
63 *
64 * This product includes cryptographic software written by Eric Young
65 * (eay@cryptsoft.com). This product includes software written by Tim
66 * Hudson (tjh@cryptsoft.com).
67 *
68 */
69
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73
74#include "../e_os.h"
75
76#include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */
77#include <openssl/crypto.h>
78#include <openssl/bio.h>
79#include <openssl/bn.h>
80#include <openssl/objects.h>
81#include <openssl/rand.h>
82#include <openssl/sha.h>
83#include <openssl/err.h>
84
85#ifdef OPENSSL_NO_ECDH
86int main(int argc, char *argv[])
87{
88 printf("No ECDH support\n");
89 return(0);
90}
91#else
92#include <openssl/ec.h>
93#include <openssl/ecdh.h>
94
95#ifdef OPENSSL_SYS_WIN16
96#define MS_CALLBACK _far _loadds
97#else
98#define MS_CALLBACK
99#endif
100
101#if 0
102static void MS_CALLBACK cb(int p, int n, void *arg);
103#endif
104
105static const char rnd_seed[] = "string to make the random number generator think it has entropy";
106
107
108static const int KDF1_SHA1_len = 20;
109static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
110 {
111#ifndef OPENSSL_NO_SHA
112 if (*outlen < SHA_DIGEST_LENGTH)
113 return NULL;
114 else
115 *outlen = SHA_DIGEST_LENGTH;
116 return SHA1(in, inlen, out);
117#else
118 return NULL;
119#endif
120 }
121
122
123static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
124 {
125 EC_KEY *a=NULL;
126 EC_KEY *b=NULL;
127 BIGNUM *x_a=NULL, *y_a=NULL,
128 *x_b=NULL, *y_b=NULL;
129 char buf[12];
130 unsigned char *abuf=NULL,*bbuf=NULL;
131 int i,alen,blen,aout,bout,ret=0;
132 const EC_GROUP *group;
133
134 a = EC_KEY_new_by_curve_name(nid);
135 b = EC_KEY_new_by_curve_name(nid);
136 if (a == NULL || b == NULL)
137 goto err;
138
139 group = EC_KEY_get0_group(a);
140
141 if ((x_a=BN_new()) == NULL) goto err;
142 if ((y_a=BN_new()) == NULL) goto err;
143 if ((x_b=BN_new()) == NULL) goto err;
144 if ((y_b=BN_new()) == NULL) goto err;
145
146 BIO_puts(out,"Testing key generation with ");
147 BIO_puts(out,text);
148#ifdef NOISY
149 BIO_puts(out,"\n");
150#else
151 (void)BIO_flush(out);
152#endif
153
154 if (!EC_KEY_generate_key(a)) goto err;
155
156 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
157 {
158 if (!EC_POINT_get_affine_coordinates_GFp(group,
159 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
160 }
161#ifndef OPENSSL_NO_EC2M
162 else
163 {
164 if (!EC_POINT_get_affine_coordinates_GF2m(group,
165 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
166 }
167#endif
168#ifdef NOISY
169 BIO_puts(out," pri 1=");
170 BN_print(out,a->priv_key);
171 BIO_puts(out,"\n pub 1=");
172 BN_print(out,x_a);
173 BIO_puts(out,",");
174 BN_print(out,y_a);
175 BIO_puts(out,"\n");
176#else
177 BIO_printf(out," .");
178 (void)BIO_flush(out);
179#endif
180
181 if (!EC_KEY_generate_key(b)) goto err;
182
183 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
184 {
185 if (!EC_POINT_get_affine_coordinates_GFp(group,
186 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
187 }
188#ifndef OPENSSL_NO_EC2M
189 else
190 {
191 if (!EC_POINT_get_affine_coordinates_GF2m(group,
192 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
193 }
194#endif
195
196#ifdef NOISY
197 BIO_puts(out," pri 2=");
198 BN_print(out,b->priv_key);
199 BIO_puts(out,"\n pub 2=");
200 BN_print(out,x_b);
201 BIO_puts(out,",");
202 BN_print(out,y_b);
203 BIO_puts(out,"\n");
204#else
205 BIO_printf(out,".");
206 (void)BIO_flush(out);
207#endif
208
209 alen=KDF1_SHA1_len;
210 abuf=(unsigned char *)OPENSSL_malloc(alen);
211 aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
212
213#ifdef NOISY
214 BIO_puts(out," key1 =");
215 for (i=0; i<aout; i++)
216 {
217 sprintf(buf,"%02X",abuf[i]);
218 BIO_puts(out,buf);
219 }
220 BIO_puts(out,"\n");
221#else
222 BIO_printf(out,".");
223 (void)BIO_flush(out);
224#endif
225
226 blen=KDF1_SHA1_len;
227 bbuf=(unsigned char *)OPENSSL_malloc(blen);
228 bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
229
230#ifdef NOISY
231 BIO_puts(out," key2 =");
232 for (i=0; i<bout; i++)
233 {
234 sprintf(buf,"%02X",bbuf[i]);
235 BIO_puts(out,buf);
236 }
237 BIO_puts(out,"\n");
238#else
239 BIO_printf(out,".");
240 (void)BIO_flush(out);
241#endif
242
243 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
244 {
245#ifndef NOISY
246 BIO_printf(out, " failed\n\n");
247 BIO_printf(out, "key a:\n");
248 BIO_printf(out, "private key: ");
249 BN_print(out, EC_KEY_get0_private_key(a));
250 BIO_printf(out, "\n");
251 BIO_printf(out, "public key (x,y): ");
252 BN_print(out, x_a);
253 BIO_printf(out, ",");
254 BN_print(out, y_a);
255 BIO_printf(out, "\nkey b:\n");
256 BIO_printf(out, "private key: ");
257 BN_print(out, EC_KEY_get0_private_key(b));
258 BIO_printf(out, "\n");
259 BIO_printf(out, "public key (x,y): ");
260 BN_print(out, x_b);
261 BIO_printf(out, ",");
262 BN_print(out, y_b);
263 BIO_printf(out, "\n");
264 BIO_printf(out, "generated key a: ");
265 for (i=0; i<bout; i++)
266 {
267 sprintf(buf, "%02X", bbuf[i]);
268 BIO_puts(out, buf);
269 }
270 BIO_printf(out, "\n");
271 BIO_printf(out, "generated key b: ");
272 for (i=0; i<aout; i++)
273 {
274 sprintf(buf, "%02X", abuf[i]);
275 BIO_puts(out,buf);
276 }
277 BIO_printf(out, "\n");
278#endif
279 fprintf(stderr,"Error in ECDH routines\n");
280 ret=0;
281 }
282 else
283 {
284#ifndef NOISY
285 BIO_printf(out, " ok\n");
286#endif
287 ret=1;
288 }
289err:
290 ERR_print_errors_fp(stderr);
291
292 if (abuf != NULL) OPENSSL_free(abuf);
293 if (bbuf != NULL) OPENSSL_free(bbuf);
294 if (x_a) BN_free(x_a);
295 if (y_a) BN_free(y_a);
296 if (x_b) BN_free(x_b);
297 if (y_b) BN_free(y_b);
298 if (b) EC_KEY_free(b);
299 if (a) EC_KEY_free(a);
300 return(ret);
301 }
302
303int main(int argc, char *argv[])
304 {
305 BN_CTX *ctx=NULL;
306 int ret=1;
307 BIO *out;
308
309 CRYPTO_malloc_debug_init();
310 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
311 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
312
313#ifdef OPENSSL_SYS_WIN32
314 CRYPTO_malloc_init();
315#endif
316
317 RAND_seed(rnd_seed, sizeof rnd_seed);
318
319 out=BIO_new(BIO_s_file());
320 if (out == NULL) EXIT(1);
321 BIO_set_fp(out,stdout,BIO_NOCLOSE);
322
323 if ((ctx=BN_CTX_new()) == NULL) goto err;
324
325 /* NIST PRIME CURVES TESTS */
326 if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
327 if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
328 if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
329 if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
330 if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
331#ifndef OPENSSL_NO_EC2M
332 /* NIST BINARY CURVES TESTS */
333 if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
334 if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
335 if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
336 if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
337 if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
338 if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
339 if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
340 if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
341 if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
342 if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
343#endif
344
345 ret = 0;
346
347err:
348 ERR_print_errors_fp(stderr);
349 if (ctx) BN_CTX_free(ctx);
350 BIO_free(out);
351 CRYPTO_cleanup_all_ex_data();
352 ERR_remove_thread_state(NULL);
353 CRYPTO_mem_leaks_fp(stderr);
354 EXIT(ret);
355 return(ret);
356 }
357
358#if 0
359static void MS_CALLBACK cb(int p, int n, void *arg)
360 {
361 char c='*';
362
363 if (p == 0) c='.';
364 if (p == 1) c='+';
365 if (p == 2) c='*';
366 if (p == 3) c='\n';
367 BIO_write((BIO *)arg,&c,1);
368 (void)BIO_flush((BIO *)arg);
369#ifdef LINT
370 p=n;
371#endif
372 }
373#endif
374#endif
diff --git a/src/lib/libcrypto/ecdsa/ecdsatest.c b/src/lib/libcrypto/ecdsa/ecdsatest.c
deleted file mode 100644
index 537bb30362..0000000000
--- a/src/lib/libcrypto/ecdsa/ecdsatest.c
+++ /dev/null
@@ -1,572 +0,0 @@
1/* crypto/ecdsa/ecdsatest.c */
2/*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75
76#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_ECDSA is defined */
77
78#ifdef OPENSSL_NO_ECDSA
79int main(int argc, char * argv[])
80 {
81 puts("Elliptic curves are disabled.");
82 return 0;
83 }
84#else
85
86#include <openssl/crypto.h>
87#include <openssl/bio.h>
88#include <openssl/evp.h>
89#include <openssl/bn.h>
90#include <openssl/ecdsa.h>
91#ifndef OPENSSL_NO_ENGINE
92#include <openssl/engine.h>
93#endif
94#include <openssl/err.h>
95#include <openssl/rand.h>
96
97static const char rnd_seed[] = "string to make the random number generator "
98 "think it has entropy";
99
100/* declaration of the test functions */
101int x9_62_tests(BIO *);
102int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
103int test_builtin(BIO *);
104
105/* functions to change the RAND_METHOD */
106int change_rand(void);
107int restore_rand(void);
108int fbytes(unsigned char *buf, int num);
109
110RAND_METHOD fake_rand;
111const RAND_METHOD *old_rand;
112
113int change_rand(void)
114 {
115 /* save old rand method */
116 if ((old_rand = RAND_get_rand_method()) == NULL)
117 return 0;
118
119 fake_rand.seed = old_rand->seed;
120 fake_rand.cleanup = old_rand->cleanup;
121 fake_rand.add = old_rand->add;
122 fake_rand.status = old_rand->status;
123 /* use own random function */
124 fake_rand.bytes = fbytes;
125 fake_rand.pseudorand = old_rand->bytes;
126 /* set new RAND_METHOD */
127 if (!RAND_set_rand_method(&fake_rand))
128 return 0;
129 return 1;
130 }
131
132int restore_rand(void)
133 {
134 if (!RAND_set_rand_method(old_rand))
135 return 0;
136 else
137 return 1;
138 }
139
140static int fbytes_counter = 0;
141static const char *numbers[8] = {
142 "651056770906015076056810763456358567190100156695615665659",
143 "6140507067065001063065065565667405560006161556565665656654",
144 "8763001015071075675010661307616710783570106710677817767166"
145 "71676178726717",
146 "7000000175690566466555057817571571075705015757757057795755"
147 "55657156756655",
148 "1275552191113212300012030439187146164646146646466749494799",
149 "1542725565216523985789236956265265265235675811949404040041",
150 "1456427555219115346513212300075341203043918714616464614664"
151 "64667494947990",
152 "1712787255652165239672857892369562652652652356758119494040"
153 "40041670216363"};
154
155int fbytes(unsigned char *buf, int num)
156 {
157 int ret;
158 BIGNUM *tmp = NULL;
159
160 if (fbytes_counter >= 8)
161 return 0;
162 tmp = BN_new();
163 if (!tmp)
164 return 0;
165 if (!BN_dec2bn(&tmp, numbers[fbytes_counter]))
166 {
167 BN_free(tmp);
168 return 0;
169 }
170 fbytes_counter ++;
171 if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
172 ret = 0;
173 else
174 ret = 1;
175 if (tmp)
176 BN_free(tmp);
177 return ret;
178 }
179
180/* some tests from the X9.62 draft */
181int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
182 {
183 int ret = 0;
184 const char message[] = "abc";
185 unsigned char digest[20];
186 unsigned int dgst_len = 0;
187 EVP_MD_CTX md_ctx;
188 EC_KEY *key = NULL;
189 ECDSA_SIG *signature = NULL;
190 BIGNUM *r = NULL, *s = NULL;
191
192 EVP_MD_CTX_init(&md_ctx);
193 /* get the message digest */
194 EVP_DigestInit(&md_ctx, EVP_ecdsa());
195 EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
196 EVP_DigestFinal(&md_ctx, digest, &dgst_len);
197
198 BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
199 /* create the key */
200 if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
201 goto x962_int_err;
202 if (!EC_KEY_generate_key(key))
203 goto x962_int_err;
204 BIO_printf(out, ".");
205 (void)BIO_flush(out);
206 /* create the signature */
207 signature = ECDSA_do_sign(digest, 20, key);
208 if (signature == NULL)
209 goto x962_int_err;
210 BIO_printf(out, ".");
211 (void)BIO_flush(out);
212 /* compare the created signature with the expected signature */
213 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
214 goto x962_int_err;
215 if (!BN_dec2bn(&r, r_in) ||
216 !BN_dec2bn(&s, s_in))
217 goto x962_int_err;
218 if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
219 goto x962_int_err;
220 BIO_printf(out, ".");
221 (void)BIO_flush(out);
222 /* verify the signature */
223 if (ECDSA_do_verify(digest, 20, signature, key) != 1)
224 goto x962_int_err;
225 BIO_printf(out, ".");
226 (void)BIO_flush(out);
227
228 BIO_printf(out, " ok\n");
229 ret = 1;
230x962_int_err:
231 if (!ret)
232 BIO_printf(out, " failed\n");
233 if (key)
234 EC_KEY_free(key);
235 if (signature)
236 ECDSA_SIG_free(signature);
237 if (r)
238 BN_free(r);
239 if (s)
240 BN_free(s);
241 EVP_MD_CTX_cleanup(&md_ctx);
242 return ret;
243 }
244
245int x9_62_tests(BIO *out)
246 {
247 int ret = 0;
248
249 BIO_printf(out, "some tests from X9.62:\n");
250
251 /* set own rand method */
252 if (!change_rand())
253 goto x962_err;
254
255 if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
256 "3342403536405981729393488334694600415596881826869351677613",
257 "5735822328888155254683894997897571951568553642892029982342"))
258 goto x962_err;
259 if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
260 "3086361431751678114926225473006680188549593787585317781474"
261 "62058306432176",
262 "3238135532097973577080787768312505059318910517550078427819"
263 "78505179448783"))
264 goto x962_err;
265#ifndef OPENSSL_NO_EC2M
266 if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
267 "87194383164871543355722284926904419997237591535066528048",
268 "308992691965804947361541664549085895292153777025772063598"))
269 goto x962_err;
270 if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
271 "2159633321041961198501834003903461262881815148684178964245"
272 "5876922391552",
273 "1970303740007316867383349976549972270528498040721988191026"
274 "49413465737174"))
275 goto x962_err;
276#endif
277 ret = 1;
278x962_err:
279 if (!restore_rand())
280 ret = 0;
281 return ret;
282 }
283
284int test_builtin(BIO *out)
285 {
286 EC_builtin_curve *curves = NULL;
287 size_t crv_len = 0, n = 0;
288 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
289 EC_GROUP *group;
290 ECDSA_SIG *ecdsa_sig = NULL;
291 unsigned char digest[20], wrong_digest[20];
292 unsigned char *signature = NULL;
293 const unsigned char *sig_ptr;
294 unsigned char *sig_ptr2;
295 unsigned char *raw_buf = NULL;
296 unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
297 int nid, ret = 0;
298
299 /* fill digest values with some random data */
300 if (!RAND_pseudo_bytes(digest, 20) ||
301 !RAND_pseudo_bytes(wrong_digest, 20))
302 {
303 BIO_printf(out, "ERROR: unable to get random data\n");
304 goto builtin_err;
305 }
306
307 /* create and verify a ecdsa signature with every availble curve
308 * (with ) */
309 BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
310 "with some internal curves:\n");
311
312 /* get a list of all internal curves */
313 crv_len = EC_get_builtin_curves(NULL, 0);
314
315 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
316
317 if (curves == NULL)
318 {
319 BIO_printf(out, "malloc error\n");
320 goto builtin_err;
321 }
322
323 if (!EC_get_builtin_curves(curves, crv_len))
324 {
325 BIO_printf(out, "unable to get internal curves\n");
326 goto builtin_err;
327 }
328
329 /* now create and verify a signature for every curve */
330 for (n = 0; n < crv_len; n++)
331 {
332 unsigned char dirt, offset;
333
334 nid = curves[n].nid;
335 if (nid == NID_ipsec4)
336 continue;
337 /* create new ecdsa key (== EC_KEY) */
338 if ((eckey = EC_KEY_new()) == NULL)
339 goto builtin_err;
340 group = EC_GROUP_new_by_curve_name(nid);
341 if (group == NULL)
342 goto builtin_err;
343 if (EC_KEY_set_group(eckey, group) == 0)
344 goto builtin_err;
345 EC_GROUP_free(group);
346 degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
347 if (degree < 160)
348 /* drop the curve */
349 {
350 EC_KEY_free(eckey);
351 eckey = NULL;
352 continue;
353 }
354 BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
355 /* create key */
356 if (!EC_KEY_generate_key(eckey))
357 {
358 BIO_printf(out, " failed\n");
359 goto builtin_err;
360 }
361 /* create second key */
362 if ((wrong_eckey = EC_KEY_new()) == NULL)
363 goto builtin_err;
364 group = EC_GROUP_new_by_curve_name(nid);
365 if (group == NULL)
366 goto builtin_err;
367 if (EC_KEY_set_group(wrong_eckey, group) == 0)
368 goto builtin_err;
369 EC_GROUP_free(group);
370 if (!EC_KEY_generate_key(wrong_eckey))
371 {
372 BIO_printf(out, " failed\n");
373 goto builtin_err;
374 }
375
376 BIO_printf(out, ".");
377 (void)BIO_flush(out);
378 /* check key */
379 if (!EC_KEY_check_key(eckey))
380 {
381 BIO_printf(out, " failed\n");
382 goto builtin_err;
383 }
384 BIO_printf(out, ".");
385 (void)BIO_flush(out);
386 /* create signature */
387 sig_len = ECDSA_size(eckey);
388 if ((signature = OPENSSL_malloc(sig_len)) == NULL)
389 goto builtin_err;
390 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
391 {
392 BIO_printf(out, " failed\n");
393 goto builtin_err;
394 }
395 BIO_printf(out, ".");
396 (void)BIO_flush(out);
397 /* verify signature */
398 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
399 {
400 BIO_printf(out, " failed\n");
401 goto builtin_err;
402 }
403 BIO_printf(out, ".");
404 (void)BIO_flush(out);
405 /* verify signature with the wrong key */
406 if (ECDSA_verify(0, digest, 20, signature, sig_len,
407 wrong_eckey) == 1)
408 {
409 BIO_printf(out, " failed\n");
410 goto builtin_err;
411 }
412 BIO_printf(out, ".");
413 (void)BIO_flush(out);
414 /* wrong digest */
415 if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
416 eckey) == 1)
417 {
418 BIO_printf(out, " failed\n");
419 goto builtin_err;
420 }
421 BIO_printf(out, ".");
422 (void)BIO_flush(out);
423 /* wrong length */
424 if (ECDSA_verify(0, digest, 20, signature, sig_len - 1,
425 eckey) == 1)
426 {
427 BIO_printf(out, " failed\n");
428 goto builtin_err;
429 }
430 BIO_printf(out, ".");
431 (void)BIO_flush(out);
432
433 /* Modify a single byte of the signature: to ensure we don't
434 * garble the ASN1 structure, we read the raw signature and
435 * modify a byte in one of the bignums directly. */
436 sig_ptr = signature;
437 if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL)
438 {
439 BIO_printf(out, " failed\n");
440 goto builtin_err;
441 }
442
443 /* Store the two BIGNUMs in raw_buf. */
444 r_len = BN_num_bytes(ecdsa_sig->r);
445 s_len = BN_num_bytes(ecdsa_sig->s);
446 bn_len = (degree + 7) / 8;
447 if ((r_len > bn_len) || (s_len > bn_len))
448 {
449 BIO_printf(out, " failed\n");
450 goto builtin_err;
451 }
452 buf_len = 2 * bn_len;
453 if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
454 goto builtin_err;
455 /* Pad the bignums with leading zeroes. */
456 memset(raw_buf, 0, buf_len);
457 BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
458 BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
459
460 /* Modify a single byte in the buffer. */
461 offset = raw_buf[10] % buf_len;
462 dirt = raw_buf[11] ? raw_buf[11] : 1;
463 raw_buf[offset] ^= dirt;
464 /* Now read the BIGNUMs back in from raw_buf. */
465 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
466 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
467 goto builtin_err;
468
469 sig_ptr2 = signature;
470 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
471 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
472 {
473 BIO_printf(out, " failed\n");
474 goto builtin_err;
475 }
476 /* Sanity check: undo the modification and verify signature. */
477 raw_buf[offset] ^= dirt;
478 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
479 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
480 goto builtin_err;
481
482 sig_ptr2 = signature;
483 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
484 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
485 {
486 BIO_printf(out, " failed\n");
487 goto builtin_err;
488 }
489 BIO_printf(out, ".");
490 (void)BIO_flush(out);
491
492 BIO_printf(out, " ok\n");
493 /* cleanup */
494 /* clean bogus errors */
495 ERR_clear_error();
496 OPENSSL_free(signature);
497 signature = NULL;
498 EC_KEY_free(eckey);
499 eckey = NULL;
500 EC_KEY_free(wrong_eckey);
501 wrong_eckey = NULL;
502 ECDSA_SIG_free(ecdsa_sig);
503 ecdsa_sig = NULL;
504 OPENSSL_free(raw_buf);
505 raw_buf = NULL;
506 }
507
508 ret = 1;
509builtin_err:
510 if (eckey)
511 EC_KEY_free(eckey);
512 if (wrong_eckey)
513 EC_KEY_free(wrong_eckey);
514 if (ecdsa_sig)
515 ECDSA_SIG_free(ecdsa_sig);
516 if (signature)
517 OPENSSL_free(signature);
518 if (raw_buf)
519 OPENSSL_free(raw_buf);
520 if (curves)
521 OPENSSL_free(curves);
522
523 return ret;
524 }
525
526int main(void)
527 {
528 int ret = 1;
529 BIO *out;
530
531 out = BIO_new_fp(stdout, BIO_NOCLOSE);
532
533 /* enable memory leak checking unless explicitly disabled */
534 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
535 (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
536 {
537 CRYPTO_malloc_debug_init();
538 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
539 }
540 else
541 {
542 /* OPENSSL_DEBUG_MEMORY=off */
543 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
544 }
545 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
546
547 ERR_load_crypto_strings();
548
549 /* initialize the prng */
550 RAND_seed(rnd_seed, sizeof(rnd_seed));
551
552 /* the tests */
553 if (!x9_62_tests(out)) goto err;
554 if (!test_builtin(out)) goto err;
555
556 ret = 0;
557err:
558 if (ret)
559 BIO_printf(out, "\nECDSA test failed\n");
560 else
561 BIO_printf(out, "\nECDSA test passed\n");
562 if (ret)
563 ERR_print_errors(out);
564 CRYPTO_cleanup_all_ex_data();
565 ERR_remove_thread_state(NULL);
566 ERR_free_strings();
567 CRYPTO_mem_leaks(out);
568 if (out != NULL)
569 BIO_free(out);
570 return ret;
571 }
572#endif
diff --git a/src/lib/libcrypto/evp/evp_test.c b/src/lib/libcrypto/evp/evp_test.c
deleted file mode 100644
index 55c7cdfdcc..0000000000
--- a/src/lib/libcrypto/evp/evp_test.c
+++ /dev/null
@@ -1,450 +0,0 @@
1/* Written by Ben Laurie, 2001 */
2/*
3 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 */
49
50#include <stdio.h>
51#include <string.h>
52
53#include "../e_os.h"
54
55#include <openssl/opensslconf.h>
56#include <openssl/evp.h>
57#ifndef OPENSSL_NO_ENGINE
58#include <openssl/engine.h>
59#endif
60#include <openssl/err.h>
61#include <openssl/conf.h>
62
63static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
64 {
65 int n=0;
66
67 fprintf(f,"%s",title);
68 for( ; n < l ; ++n)
69 {
70 if((n%16) == 0)
71 fprintf(f,"\n%04x",n);
72 fprintf(f," %02x",s[n]);
73 }
74 fprintf(f,"\n");
75 }
76
77static int convert(unsigned char *s)
78 {
79 unsigned char *d;
80
81 for(d=s ; *s ; s+=2,++d)
82 {
83 unsigned int n;
84
85 if(!s[1])
86 {
87 fprintf(stderr,"Odd number of hex digits!");
88 EXIT(4);
89 }
90 sscanf((char *)s,"%2x",&n);
91 *d=(unsigned char)n;
92 }
93 return s-d;
94 }
95
96static char *sstrsep(char **string, const char *delim)
97 {
98 char isdelim[256];
99 char *token = *string;
100
101 if (**string == 0)
102 return NULL;
103
104 memset(isdelim, 0, 256);
105 isdelim[0] = 1;
106
107 while (*delim)
108 {
109 isdelim[(unsigned char)(*delim)] = 1;
110 delim++;
111 }
112
113 while (!isdelim[(unsigned char)(**string)])
114 {
115 (*string)++;
116 }
117
118 if (**string)
119 {
120 **string = 0;
121 (*string)++;
122 }
123
124 return token;
125 }
126
127static unsigned char *ustrsep(char **p,const char *sep)
128 { return (unsigned char *)sstrsep(p,sep); }
129
130static int test1_exit(int ec)
131 {
132 EXIT(ec);
133 return(0); /* To keep some compilers quiet */
134 }
135
136static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
137 const unsigned char *iv,int in,
138 const unsigned char *plaintext,int pn,
139 const unsigned char *ciphertext,int cn,
140 int encdec)
141 {
142 EVP_CIPHER_CTX ctx;
143 unsigned char out[4096];
144 int outl,outl2;
145
146 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
147 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
148 hexdump(stdout,"Key",key,kn);
149 if(in)
150 hexdump(stdout,"IV",iv,in);
151 hexdump(stdout,"Plaintext",plaintext,pn);
152 hexdump(stdout,"Ciphertext",ciphertext,cn);
153
154 if(kn != c->key_len)
155 {
156 fprintf(stderr,"Key length doesn't match, got %d expected %lu\n",kn,
157 (unsigned long)c->key_len);
158 test1_exit(5);
159 }
160 EVP_CIPHER_CTX_init(&ctx);
161 if (encdec != 0)
162 {
163 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
164 {
165 fprintf(stderr,"EncryptInit failed\n");
166 ERR_print_errors_fp(stderr);
167 test1_exit(10);
168 }
169 EVP_CIPHER_CTX_set_padding(&ctx,0);
170
171 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
172 {
173 fprintf(stderr,"Encrypt failed\n");
174 ERR_print_errors_fp(stderr);
175 test1_exit(6);
176 }
177 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
178 {
179 fprintf(stderr,"EncryptFinal failed\n");
180 ERR_print_errors_fp(stderr);
181 test1_exit(7);
182 }
183
184 if(outl+outl2 != cn)
185 {
186 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
187 outl+outl2,cn);
188 test1_exit(8);
189 }
190
191 if(memcmp(out,ciphertext,cn))
192 {
193 fprintf(stderr,"Ciphertext mismatch\n");
194 hexdump(stderr,"Got",out,cn);
195 hexdump(stderr,"Expected",ciphertext,cn);
196 test1_exit(9);
197 }
198 }
199
200 if (encdec <= 0)
201 {
202 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
203 {
204 fprintf(stderr,"DecryptInit failed\n");
205 ERR_print_errors_fp(stderr);
206 test1_exit(11);
207 }
208 EVP_CIPHER_CTX_set_padding(&ctx,0);
209
210 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
211 {
212 fprintf(stderr,"Decrypt failed\n");
213 ERR_print_errors_fp(stderr);
214 test1_exit(6);
215 }
216 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
217 {
218 fprintf(stderr,"DecryptFinal failed\n");
219 ERR_print_errors_fp(stderr);
220 test1_exit(7);
221 }
222
223 if(outl+outl2 != pn)
224 {
225 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
226 outl+outl2,pn);
227 test1_exit(8);
228 }
229
230 if(memcmp(out,plaintext,pn))
231 {
232 fprintf(stderr,"Plaintext mismatch\n");
233 hexdump(stderr,"Got",out,pn);
234 hexdump(stderr,"Expected",plaintext,pn);
235 test1_exit(9);
236 }
237 }
238
239 EVP_CIPHER_CTX_cleanup(&ctx);
240
241 printf("\n");
242 }
243
244static int test_cipher(const char *cipher,const unsigned char *key,int kn,
245 const unsigned char *iv,int in,
246 const unsigned char *plaintext,int pn,
247 const unsigned char *ciphertext,int cn,
248 int encdec)
249 {
250 const EVP_CIPHER *c;
251
252 c=EVP_get_cipherbyname(cipher);
253 if(!c)
254 return 0;
255
256 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
257
258 return 1;
259 }
260
261static int test_digest(const char *digest,
262 const unsigned char *plaintext,int pn,
263 const unsigned char *ciphertext, unsigned int cn)
264 {
265 const EVP_MD *d;
266 EVP_MD_CTX ctx;
267 unsigned char md[EVP_MAX_MD_SIZE];
268 unsigned int mdn;
269
270 d=EVP_get_digestbyname(digest);
271 if(!d)
272 return 0;
273
274 printf("Testing digest %s\n",EVP_MD_name(d));
275 hexdump(stdout,"Plaintext",plaintext,pn);
276 hexdump(stdout,"Digest",ciphertext,cn);
277
278 EVP_MD_CTX_init(&ctx);
279 if(!EVP_DigestInit_ex(&ctx,d, NULL))
280 {
281 fprintf(stderr,"DigestInit failed\n");
282 ERR_print_errors_fp(stderr);
283 EXIT(100);
284 }
285 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
286 {
287 fprintf(stderr,"DigestUpdate failed\n");
288 ERR_print_errors_fp(stderr);
289 EXIT(101);
290 }
291 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
292 {
293 fprintf(stderr,"DigestFinal failed\n");
294 ERR_print_errors_fp(stderr);
295 EXIT(101);
296 }
297 EVP_MD_CTX_cleanup(&ctx);
298
299 if(mdn != cn)
300 {
301 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
302 EXIT(102);
303 }
304
305 if(memcmp(md,ciphertext,cn))
306 {
307 fprintf(stderr,"Digest mismatch\n");
308 hexdump(stderr,"Got",md,cn);
309 hexdump(stderr,"Expected",ciphertext,cn);
310 EXIT(103);
311 }
312
313 printf("\n");
314
315 EVP_MD_CTX_cleanup(&ctx);
316
317 return 1;
318 }
319
320int main(int argc,char **argv)
321 {
322 const char *szTestFile;
323 FILE *f;
324
325 if(argc != 2)
326 {
327 fprintf(stderr,"%s <test file>\n",argv[0]);
328 EXIT(1);
329 }
330 CRYPTO_malloc_debug_init();
331 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
332 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
333
334 szTestFile=argv[1];
335
336 f=fopen(szTestFile,"r");
337 if(!f)
338 {
339 perror(szTestFile);
340 EXIT(2);
341 }
342
343 /* Load up the software EVP_CIPHER and EVP_MD definitions */
344 OpenSSL_add_all_ciphers();
345 OpenSSL_add_all_digests();
346#ifndef OPENSSL_NO_ENGINE
347 /* Load all compiled-in ENGINEs */
348 ENGINE_load_builtin_engines();
349#endif
350#if 0
351 OPENSSL_config();
352#endif
353#ifndef OPENSSL_NO_ENGINE
354 /* Register all available ENGINE implementations of ciphers and digests.
355 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
356 ENGINE_register_all_ciphers();
357 ENGINE_register_all_digests();
358 /* If we add command-line options, this statement should be switchable.
359 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
360 * they weren't already initialised. */
361 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
362#endif
363
364 for( ; ; )
365 {
366 char line[4096];
367 char *p;
368 char *cipher;
369 unsigned char *iv,*key,*plaintext,*ciphertext;
370 int encdec;
371 int kn,in,pn,cn;
372
373 if(!fgets((char *)line,sizeof line,f))
374 break;
375 if(line[0] == '#' || line[0] == '\n')
376 continue;
377 p=line;
378 cipher=sstrsep(&p,":");
379 key=ustrsep(&p,":");
380 iv=ustrsep(&p,":");
381 plaintext=ustrsep(&p,":");
382 ciphertext=ustrsep(&p,":");
383 if (p[-1] == '\n') {
384 p[-1] = '\0';
385 encdec = -1;
386 } else {
387 encdec = atoi(sstrsep(&p,"\n"));
388 }
389
390
391 kn=convert(key);
392 in=convert(iv);
393 pn=convert(plaintext);
394 cn=convert(ciphertext);
395
396 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
397 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
398 {
399#ifdef OPENSSL_NO_AES
400 if (strstr(cipher, "AES") == cipher)
401 {
402 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
403 continue;
404 }
405#endif
406#ifdef OPENSSL_NO_DES
407 if (strstr(cipher, "DES") == cipher)
408 {
409 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
410 continue;
411 }
412#endif
413#ifdef OPENSSL_NO_RC4
414 if (strstr(cipher, "RC4") == cipher)
415 {
416 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
417 continue;
418 }
419#endif
420#ifdef OPENSSL_NO_CAMELLIA
421 if (strstr(cipher, "CAMELLIA") == cipher)
422 {
423 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
424 continue;
425 }
426#endif
427#ifdef OPENSSL_NO_SEED
428 if (strstr(cipher, "SEED") == cipher)
429 {
430 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
431 continue;
432 }
433#endif
434 fprintf(stderr,"Can't find %s\n",cipher);
435 EXIT(3);
436 }
437 }
438 fclose(f);
439
440#ifndef OPENSSL_NO_ENGINE
441 ENGINE_cleanup();
442#endif
443 EVP_cleanup();
444 CRYPTO_cleanup_all_ex_data();
445 ERR_remove_thread_state(NULL);
446 ERR_free_strings();
447 CRYPTO_mem_leaks_fp(stderr);
448
449 return 0;
450 }
diff --git a/src/lib/libcrypto/hmac/hmactest.c b/src/lib/libcrypto/hmac/hmactest.c
deleted file mode 100644
index 282e06e53e..0000000000
--- a/src/lib/libcrypto/hmac/hmactest.c
+++ /dev/null
@@ -1,164 +0,0 @@
1/* crypto/hmac/hmactest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_HMAC
66int main(int argc, char *argv[])
67{
68 printf("No HMAC support\n");
69 return(0);
70}
71#else
72#include <openssl/hmac.h>
73#ifndef OPENSSL_NO_MD5
74#include <openssl/md5.h>
75#endif
76
77
78#ifndef OPENSSL_NO_MD5
79static struct test_st
80 {
81 unsigned char key[16];
82 int key_len;
83 unsigned char data[64];
84 int data_len;
85 unsigned char *digest;
86 } test[4]={
87 { "",
88 0,
89 "More text test vectors to stuff up EBCDIC machines :-)",
90 54,
91 (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
92 },{ {0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
93 0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,},
94 16,
95 "Hi There",
96 8,
97 (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
98 },{ "Jefe",
99 4,
100 "what do ya want for nothing?",
101 28,
102 (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
103 },{
104 {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
105 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,},
106 16,
107 {0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
108 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
109 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
110 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
111 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
112 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
113 0xdd,0xdd},
114 50,
115 (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
116 },
117 };
118#endif
119
120static char *pt(unsigned char *md);
121int main(int argc, char *argv[])
122 {
123#ifndef OPENSSL_NO_MD5
124 int i;
125 char *p;
126#endif
127 int err=0;
128
129#ifdef OPENSSL_NO_MD5
130 printf("test skipped: MD5 disabled\n");
131#else
132 for (i=0; i<4; i++)
133 {
134 p=pt(HMAC(EVP_md5(),
135 test[i].key, test[i].key_len,
136 test[i].data, test[i].data_len,
137 NULL,NULL));
138
139 if (strcmp(p,(char *)test[i].digest) != 0)
140 {
141 printf("error calculating HMAC on %d entry'\n",i);
142 printf("got %s instead of %s\n",p,test[i].digest);
143 err++;
144 }
145 else
146 printf("test %d ok\n",i);
147 }
148#endif /* OPENSSL_NO_MD5 */
149 EXIT(err);
150 return(0);
151 }
152
153#ifndef OPENSSL_NO_MD5
154static char *pt(unsigned char *md)
155 {
156 int i;
157 static char buf[80];
158
159 for (i=0; i<MD5_DIGEST_LENGTH; i++)
160 sprintf(&(buf[i*2]),"%02x",md[i]);
161 return(buf);
162 }
163#endif
164#endif
diff --git a/src/lib/libcrypto/idea/ideatest.c b/src/lib/libcrypto/idea/ideatest.c
deleted file mode 100644
index e6ffc7025e..0000000000
--- a/src/lib/libcrypto/idea/ideatest.c
+++ /dev/null
@@ -1,235 +0,0 @@
1/* crypto/idea/ideatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_IDEA
66int main(int argc, char *argv[])
67{
68 printf("No IDEA support\n");
69 return(0);
70}
71#else
72#include <openssl/idea.h>
73
74unsigned char k[16]={
75 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
76 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
77
78unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
79unsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
80unsigned char out[80];
81
82char *text="Hello to all people out there";
83
84static unsigned char cfb_key[16]={
85 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
86 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
87 };
88static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
89static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
90#define CFB_TEST_SIZE 24
91static unsigned char plain[CFB_TEST_SIZE]=
92 {
93 0x4e,0x6f,0x77,0x20,0x69,0x73,
94 0x20,0x74,0x68,0x65,0x20,0x74,
95 0x69,0x6d,0x65,0x20,0x66,0x6f,
96 0x72,0x20,0x61,0x6c,0x6c,0x20
97 };
98static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
99 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
100 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
101 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
102
103/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
104 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
105 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
106 };
107
108static int cfb64_test(unsigned char *cfb_cipher);
109static char *pt(unsigned char *p);
110int main(int argc, char *argv[])
111 {
112 int i,err=0;
113 IDEA_KEY_SCHEDULE key,dkey;
114 unsigned char iv[8];
115
116 idea_set_encrypt_key(k,&key);
117 idea_ecb_encrypt(in,out,&key);
118 if (memcmp(out,c,8) != 0)
119 {
120 printf("ecb idea error encrypting\n");
121 printf("got :");
122 for (i=0; i<8; i++)
123 printf("%02X ",out[i]);
124 printf("\n");
125 printf("expected:");
126 for (i=0; i<8; i++)
127 printf("%02X ",c[i]);
128 err=20;
129 printf("\n");
130 }
131
132 idea_set_decrypt_key(&key,&dkey);
133 idea_ecb_encrypt(c,out,&dkey);
134 if (memcmp(out,in,8) != 0)
135 {
136 printf("ecb idea error decrypting\n");
137 printf("got :");
138 for (i=0; i<8; i++)
139 printf("%02X ",out[i]);
140 printf("\n");
141 printf("expected:");
142 for (i=0; i<8; i++)
143 printf("%02X ",in[i]);
144 printf("\n");
145 err=3;
146 }
147
148 if (err == 0) printf("ecb idea ok\n");
149
150 memcpy(iv,k,8);
151 idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
152 memcpy(iv,k,8);
153 idea_cbc_encrypt(out,out,8,&dkey,iv,0);
154 idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
155 if (memcmp(text,out,strlen(text)+1) != 0)
156 {
157 printf("cbc idea bad\n");
158 err=4;
159 }
160 else
161 printf("cbc idea ok\n");
162
163 printf("cfb64 idea ");
164 if (cfb64_test(cfb_cipher64))
165 {
166 printf("bad\n");
167 err=5;
168 }
169 else
170 printf("ok\n");
171
172#ifdef OPENSSL_SYS_NETWARE
173 if (err) printf("ERROR: %d\n", err);
174#endif
175 EXIT(err);
176 return(err);
177 }
178
179static int cfb64_test(unsigned char *cfb_cipher)
180 {
181 IDEA_KEY_SCHEDULE eks,dks;
182 int err=0,i,n;
183
184 idea_set_encrypt_key(cfb_key,&eks);
185 idea_set_decrypt_key(&eks,&dks);
186 memcpy(cfb_tmp,cfb_iv,8);
187 n=0;
188 idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
189 cfb_tmp,&n,IDEA_ENCRYPT);
190 idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
191 (long)CFB_TEST_SIZE-12,&eks,
192 cfb_tmp,&n,IDEA_ENCRYPT);
193 if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
194 {
195 err=1;
196 printf("idea_cfb64_encrypt encrypt error\n");
197 for (i=0; i<CFB_TEST_SIZE; i+=8)
198 printf("%s\n",pt(&(cfb_buf1[i])));
199 }
200 memcpy(cfb_tmp,cfb_iv,8);
201 n=0;
202 idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
203 cfb_tmp,&n,IDEA_DECRYPT);
204 idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
205 (long)CFB_TEST_SIZE-17,&dks,
206 cfb_tmp,&n,IDEA_DECRYPT);
207 if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
208 {
209 err=1;
210 printf("idea_cfb_encrypt decrypt error\n");
211 for (i=0; i<24; i+=8)
212 printf("%s\n",pt(&(cfb_buf2[i])));
213 }
214 return(err);
215 }
216
217static char *pt(unsigned char *p)
218 {
219 static char bufs[10][20];
220 static int bnum=0;
221 char *ret;
222 int i;
223 static char *f="0123456789ABCDEF";
224
225 ret= &(bufs[bnum++][0]);
226 bnum%=10;
227 for (i=0; i<8; i++)
228 {
229 ret[i*2]=f[(p[i]>>4)&0xf];
230 ret[i*2+1]=f[p[i]&0xf];
231 }
232 ret[16]='\0';
233 return(ret);
234 }
235#endif
diff --git a/src/lib/libcrypto/md2/md2test.c b/src/lib/libcrypto/md2/md2test.c
deleted file mode 100644
index 0195214036..0000000000
--- a/src/lib/libcrypto/md2/md2test.c
+++ /dev/null
@@ -1,139 +0,0 @@
1/* crypto/md2/md2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD2
66int main(int argc, char *argv[])
67{
68 printf("No MD2 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md2.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87 "8350e5a3e24c153df2275c9f80692773",
88 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
89 "da853b0d3f88d99b30283a69e6ded6bb",
90 "ab4f496bfb2a530b219ff33031fe06b0",
91 "4e8ddff3650292ab5a4108c3aa47940b",
92 "da33def2a42df13975352846c30338cd",
93 "d5976f79d83d3a0dc9806c3c66f3efd8",
94 };
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD2_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest((unsigned char *)*P,strlen(*P),md,NULL,EVP_md2(), NULL);
110 p=pt(md);
111 if (strcmp(p,*R) != 0)
112 {
113 printf("error calculating MD2 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123#ifdef OPENSSL_SYS_NETWARE
124 if (err) printf("ERROR: %d\n", err);
125#endif
126 EXIT(err);
127 return err;
128 }
129
130static char *pt(unsigned char *md)
131 {
132 int i;
133 static char buf[80];
134
135 for (i=0; i<MD2_DIGEST_LENGTH; i++)
136 sprintf(&(buf[i*2]),"%02x",md[i]);
137 return(buf);
138 }
139#endif
diff --git a/src/lib/libcrypto/md4/md4test.c b/src/lib/libcrypto/md4/md4test.c
deleted file mode 100644
index 56591728a1..0000000000
--- a/src/lib/libcrypto/md4/md4test.c
+++ /dev/null
@@ -1,136 +0,0 @@
1/* crypto/md4/md4test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD4
66int main(int argc, char *argv[])
67{
68 printf("No MD4 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md4.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87"31d6cfe0d16ae931b73c59d7e0c089c0",
88"bde52cb31de33e46245e05fbdbd6fb24",
89"a448017aaf21d8525fc10ae87aa6729d",
90"d9130a8164549fe818874806e1c7014b",
91"d79e1c308aa5bbcdeea8ed63df412da9",
92"043f8582f241db351ce627e153e7f0e4",
93"e33b4ddc9c38f2199c3e7b164fcc0536",
94};
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD4_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_md4(), NULL);
110 p=pt(md);
111 if (strcmp(p,(char *)*R) != 0)
112 {
113 printf("error calculating MD4 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123 EXIT(err);
124 return(0);
125 }
126
127static char *pt(unsigned char *md)
128 {
129 int i;
130 static char buf[80];
131
132 for (i=0; i<MD4_DIGEST_LENGTH; i++)
133 sprintf(&(buf[i*2]),"%02x",md[i]);
134 return(buf);
135 }
136#endif
diff --git a/src/lib/libcrypto/md5/md5test.c b/src/lib/libcrypto/md5/md5test.c
deleted file mode 100644
index 2b37190e32..0000000000
--- a/src/lib/libcrypto/md5/md5test.c
+++ /dev/null
@@ -1,140 +0,0 @@
1/* crypto/md5/md5test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD5
66int main(int argc, char *argv[])
67{
68 printf("No MD5 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md5.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87 "d41d8cd98f00b204e9800998ecf8427e",
88 "0cc175b9c0f1b6a831c399e269772661",
89 "900150983cd24fb0d6963f7d28e17f72",
90 "f96b697d7cb7938d525a2f31aaf161d0",
91 "c3fcd3d76192e4007dfb496cca67e13b",
92 "d174ab98d277d9f5a5611c2c9f419d9f",
93 "57edf4a22be3c955ac49da2e2107b67a",
94 };
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD5_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_md5(), NULL);
110 p=pt(md);
111 if (strcmp(p,(char *)*R) != 0)
112 {
113 printf("error calculating MD5 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123
124#ifdef OPENSSL_SYS_NETWARE
125 if (err) printf("ERROR: %d\n", err);
126#endif
127 EXIT(err);
128 return(0);
129 }
130
131static char *pt(unsigned char *md)
132 {
133 int i;
134 static char buf[80];
135
136 for (i=0; i<MD5_DIGEST_LENGTH; i++)
137 sprintf(&(buf[i*2]),"%02x",md[i]);
138 return(buf);
139 }
140#endif
diff --git a/src/lib/libcrypto/mdc2/mdc2test.c b/src/lib/libcrypto/mdc2/mdc2test.c
deleted file mode 100644
index cf8f65cf27..0000000000
--- a/src/lib/libcrypto/mdc2/mdc2test.c
+++ /dev/null
@@ -1,141 +0,0 @@
1/* crypto/mdc2/mdc2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
66#define OPENSSL_NO_MDC2
67#endif
68
69#ifdef OPENSSL_NO_MDC2
70int main(int argc, char *argv[])
71{
72 printf("No MDC2 support\n");
73 return(0);
74}
75#else
76#include <openssl/evp.h>
77#include <openssl/mdc2.h>
78
79static unsigned char pad1[16]={
80 0x42,0xE5,0x0C,0xD2,0x24,0xBA,0xCE,0xBA,
81 0x76,0x0B,0xDD,0x2B,0xD4,0x09,0x28,0x1A
82 };
83
84static unsigned char pad2[16]={
85 0x2E,0x46,0x79,0xB5,0xAD,0xD9,0xCA,0x75,
86 0x35,0xD8,0x7A,0xFE,0xAB,0x33,0xBE,0xE2
87 };
88
89int main(int argc, char *argv[])
90 {
91 int ret=0;
92 unsigned char md[MDC2_DIGEST_LENGTH];
93 int i;
94 EVP_MD_CTX c;
95 static char *text="Now is the time for all ";
96
97 EVP_MD_CTX_init(&c);
98 EVP_DigestInit_ex(&c,EVP_mdc2(), NULL);
99 EVP_DigestUpdate(&c,(unsigned char *)text,strlen(text));
100 EVP_DigestFinal_ex(&c,&(md[0]),NULL);
101
102 if (memcmp(md,pad1,MDC2_DIGEST_LENGTH) != 0)
103 {
104 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
105 printf("%02X",md[i]);
106 printf(" <- generated\n");
107 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
108 printf("%02X",pad1[i]);
109 printf(" <- correct\n");
110 ret=1;
111 }
112 else
113 printf("pad1 - ok\n");
114
115 EVP_DigestInit_ex(&c,EVP_mdc2(), NULL);
116 /* FIXME: use a ctl function? */
117 ((MDC2_CTX *)c.md_data)->pad_type=2;
118 EVP_DigestUpdate(&c,(unsigned char *)text,strlen(text));
119 EVP_DigestFinal_ex(&c,&(md[0]),NULL);
120
121 if (memcmp(md,pad2,MDC2_DIGEST_LENGTH) != 0)
122 {
123 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
124 printf("%02X",md[i]);
125 printf(" <- generated\n");
126 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
127 printf("%02X",pad2[i]);
128 printf(" <- correct\n");
129 ret=1;
130 }
131 else
132 printf("pad2 - ok\n");
133
134 EVP_MD_CTX_cleanup(&c);
135#ifdef OPENSSL_SYS_NETWARE
136 if (ret) printf("ERROR: %d\n", ret);
137#endif
138 EXIT(ret);
139 return(ret);
140 }
141#endif
diff --git a/src/lib/libcrypto/rand/randtest.c b/src/lib/libcrypto/rand/randtest.c
deleted file mode 100644
index 9e92a70b03..0000000000
--- a/src/lib/libcrypto/rand/randtest.c
+++ /dev/null
@@ -1,219 +0,0 @@
1/* crypto/rand/randtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <openssl/rand.h>
62
63#include "../e_os.h"
64
65/* some FIPS 140-1 random number test */
66/* some simple tests */
67
68int main(int argc,char **argv)
69 {
70 unsigned char buf[2500];
71 int i,j,k,s,sign,nsign,err=0;
72 unsigned long n1;
73 unsigned long n2[16];
74 unsigned long runs[2][34];
75 /*double d; */
76 long d;
77
78 i = RAND_pseudo_bytes(buf,2500);
79 if (i < 0)
80 {
81 printf ("init failed, the rand method is not properly installed\n");
82 err++;
83 goto err;
84 }
85
86 n1=0;
87 for (i=0; i<16; i++) n2[i]=0;
88 for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0;
89
90 /* test 1 and 2 */
91 sign=0;
92 nsign=0;
93 for (i=0; i<2500; i++)
94 {
95 j=buf[i];
96
97 n2[j&0x0f]++;
98 n2[(j>>4)&0x0f]++;
99
100 for (k=0; k<8; k++)
101 {
102 s=(j&0x01);
103 if (s == sign)
104 nsign++;
105 else
106 {
107 if (nsign > 34) nsign=34;
108 if (nsign != 0)
109 {
110 runs[sign][nsign-1]++;
111 if (nsign > 6)
112 runs[sign][5]++;
113 }
114 sign=s;
115 nsign=1;
116 }
117
118 if (s) n1++;
119 j>>=1;
120 }
121 }
122 if (nsign > 34) nsign=34;
123 if (nsign != 0) runs[sign][nsign-1]++;
124
125 /* test 1 */
126 if (!((9654 < n1) && (n1 < 10346)))
127 {
128 printf("test 1 failed, X=%lu\n",n1);
129 err++;
130 }
131 printf("test 1 done\n");
132
133 /* test 2 */
134#ifdef undef
135 d=0;
136 for (i=0; i<16; i++)
137 d+=n2[i]*n2[i];
138 d=d*16.0/5000.0-5000.0;
139 if (!((1.03 < d) && (d < 57.4)))
140 {
141 printf("test 2 failed, X=%.2f\n",d);
142 err++;
143 }
144#endif
145 d=0;
146 for (i=0; i<16; i++)
147 d+=n2[i]*n2[i];
148 d=(d*8)/25-500000;
149 if (!((103 < d) && (d < 5740)))
150 {
151 printf("test 2 failed, X=%ld.%02ld\n",d/100L,d%100L);
152 err++;
153 }
154 printf("test 2 done\n");
155
156 /* test 3 */
157 for (i=0; i<2; i++)
158 {
159 if (!((2267 < runs[i][0]) && (runs[i][0] < 2733)))
160 {
161 printf("test 3 failed, bit=%d run=%d num=%lu\n",
162 i,1,runs[i][0]);
163 err++;
164 }
165 if (!((1079 < runs[i][1]) && (runs[i][1] < 1421)))
166 {
167 printf("test 3 failed, bit=%d run=%d num=%lu\n",
168 i,2,runs[i][1]);
169 err++;
170 }
171 if (!(( 502 < runs[i][2]) && (runs[i][2] < 748)))
172 {
173 printf("test 3 failed, bit=%d run=%d num=%lu\n",
174 i,3,runs[i][2]);
175 err++;
176 }
177 if (!(( 223 < runs[i][3]) && (runs[i][3] < 402)))
178 {
179 printf("test 3 failed, bit=%d run=%d num=%lu\n",
180 i,4,runs[i][3]);
181 err++;
182 }
183 if (!(( 90 < runs[i][4]) && (runs[i][4] < 223)))
184 {
185 printf("test 3 failed, bit=%d run=%d num=%lu\n",
186 i,5,runs[i][4]);
187 err++;
188 }
189 if (!(( 90 < runs[i][5]) && (runs[i][5] < 223)))
190 {
191 printf("test 3 failed, bit=%d run=%d num=%lu\n",
192 i,6,runs[i][5]);
193 err++;
194 }
195 }
196 printf("test 3 done\n");
197
198 /* test 4 */
199 if (runs[0][33] != 0)
200 {
201 printf("test 4 failed, bit=%d run=%d num=%lu\n",
202 0,34,runs[0][33]);
203 err++;
204 }
205 if (runs[1][33] != 0)
206 {
207 printf("test 4 failed, bit=%d run=%d num=%lu\n",
208 1,34,runs[1][33]);
209 err++;
210 }
211 printf("test 4 done\n");
212 err:
213 err=((err)?1:0);
214#ifdef OPENSSL_SYS_NETWARE
215 if (err) printf("ERROR: %d\n", err);
216#endif
217 EXIT(err);
218 return(err);
219 }
diff --git a/src/lib/libcrypto/rc2/rc2test.c b/src/lib/libcrypto/rc2/rc2test.c
deleted file mode 100644
index 0e117436bb..0000000000
--- a/src/lib/libcrypto/rc2/rc2test.c
+++ /dev/null
@@ -1,274 +0,0 @@
1/* crypto/rc2/rc2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* This has been a quickly hacked 'ideatest.c'. When I add tests for other
60 * RC2 modes, more of the code will be uncommented. */
61
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65
66#include "../e_os.h"
67
68#ifdef OPENSSL_NO_RC2
69int main(int argc, char *argv[])
70{
71 printf("No RC2 support\n");
72 return(0);
73}
74#else
75#include <openssl/rc2.h>
76
77static unsigned char RC2key[4][16]={
78 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
79 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
80 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
81 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
82 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
83 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
84 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
85 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F},
86 };
87
88static unsigned char RC2plain[4][8]={
89 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
90 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
91 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
92 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
93 };
94
95static unsigned char RC2cipher[4][8]={
96 {0x1C,0x19,0x8A,0x83,0x8D,0xF0,0x28,0xB7},
97 {0x21,0x82,0x9C,0x78,0xA9,0xF9,0xC0,0x74},
98 {0x13,0xDB,0x35,0x17,0xD3,0x21,0x86,0x9E},
99 {0x50,0xDC,0x01,0x62,0xBD,0x75,0x7F,0x31},
100 };
101/************/
102#ifdef undef
103unsigned char k[16]={
104 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
105 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
106
107unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
108unsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
109unsigned char out[80];
110
111char *text="Hello to all people out there";
112
113static unsigned char cfb_key[16]={
114 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
115 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
116 };
117static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
118static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
119#define CFB_TEST_SIZE 24
120static unsigned char plain[CFB_TEST_SIZE]=
121 {
122 0x4e,0x6f,0x77,0x20,0x69,0x73,
123 0x20,0x74,0x68,0x65,0x20,0x74,
124 0x69,0x6d,0x65,0x20,0x66,0x6f,
125 0x72,0x20,0x61,0x6c,0x6c,0x20
126 };
127static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
128 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
129 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
130 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
131
132/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
133 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
134 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
135 };
136
137
138/*static int cfb64_test(unsigned char *cfb_cipher);*/
139static char *pt(unsigned char *p);
140#endif
141
142int main(int argc, char *argv[])
143 {
144 int i,n,err=0;
145 RC2_KEY key;
146 unsigned char buf[8],buf2[8];
147
148 for (n=0; n<4; n++)
149 {
150 RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);
151
152 RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
153 if (memcmp(&(RC2cipher[n][0]),buf,8) != 0)
154 {
155 printf("ecb rc2 error encrypting\n");
156 printf("got :");
157 for (i=0; i<8; i++)
158 printf("%02X ",buf[i]);
159 printf("\n");
160 printf("expected:");
161 for (i=0; i<8; i++)
162 printf("%02X ",RC2cipher[n][i]);
163 err=20;
164 printf("\n");
165 }
166
167 RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
168 if (memcmp(&(RC2plain[n][0]),buf2,8) != 0)
169 {
170 printf("ecb RC2 error decrypting\n");
171 printf("got :");
172 for (i=0; i<8; i++)
173 printf("%02X ",buf[i]);
174 printf("\n");
175 printf("expected:");
176 for (i=0; i<8; i++)
177 printf("%02X ",RC2plain[n][i]);
178 printf("\n");
179 err=3;
180 }
181 }
182
183 if (err == 0) printf("ecb RC2 ok\n");
184#ifdef undef
185 memcpy(iv,k,8);
186 idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
187 memcpy(iv,k,8);
188 idea_cbc_encrypt(out,out,8,&dkey,iv,0);
189 idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
190 if (memcmp(text,out,strlen(text)+1) != 0)
191 {
192 printf("cbc idea bad\n");
193 err=4;
194 }
195 else
196 printf("cbc idea ok\n");
197
198 printf("cfb64 idea ");
199 if (cfb64_test(cfb_cipher64))
200 {
201 printf("bad\n");
202 err=5;
203 }
204 else
205 printf("ok\n");
206#endif
207
208#ifdef OPENSSL_SYS_NETWARE
209 if (err) printf("ERROR: %d\n", err);
210#endif
211 EXIT(err);
212 return(err);
213 }
214
215#ifdef undef
216static int cfb64_test(unsigned char *cfb_cipher)
217 {
218 IDEA_KEY_SCHEDULE eks,dks;
219 int err=0,i,n;
220
221 idea_set_encrypt_key(cfb_key,&eks);
222 idea_set_decrypt_key(&eks,&dks);
223 memcpy(cfb_tmp,cfb_iv,8);
224 n=0;
225 idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
226 cfb_tmp,&n,IDEA_ENCRYPT);
227 idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
228 (long)CFB_TEST_SIZE-12,&eks,
229 cfb_tmp,&n,IDEA_ENCRYPT);
230 if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
231 {
232 err=1;
233 printf("idea_cfb64_encrypt encrypt error\n");
234 for (i=0; i<CFB_TEST_SIZE; i+=8)
235 printf("%s\n",pt(&(cfb_buf1[i])));
236 }
237 memcpy(cfb_tmp,cfb_iv,8);
238 n=0;
239 idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
240 cfb_tmp,&n,IDEA_DECRYPT);
241 idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
242 (long)CFB_TEST_SIZE-17,&dks,
243 cfb_tmp,&n,IDEA_DECRYPT);
244 if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
245 {
246 err=1;
247 printf("idea_cfb_encrypt decrypt error\n");
248 for (i=0; i<24; i+=8)
249 printf("%s\n",pt(&(cfb_buf2[i])));
250 }
251 return(err);
252 }
253
254static char *pt(unsigned char *p)
255 {
256 static char bufs[10][20];
257 static int bnum=0;
258 char *ret;
259 int i;
260 static char *f="0123456789ABCDEF";
261
262 ret= &(bufs[bnum++][0]);
263 bnum%=10;
264 for (i=0; i<8; i++)
265 {
266 ret[i*2]=f[(p[i]>>4)&0xf];
267 ret[i*2+1]=f[p[i]&0xf];
268 }
269 ret[16]='\0';
270 return(ret);
271 }
272
273#endif
274#endif
diff --git a/src/lib/libcrypto/rc4/rc4test.c b/src/lib/libcrypto/rc4/rc4test.c
deleted file mode 100644
index 4312605ccb..0000000000
--- a/src/lib/libcrypto/rc4/rc4test.c
+++ /dev/null
@@ -1,242 +0,0 @@
1/* crypto/rc4/rc4test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_RC4
66int main(int argc, char *argv[])
67{
68 printf("No RC4 support\n");
69 return(0);
70}
71#else
72#include <openssl/rc4.h>
73#include <openssl/sha.h>
74
75static unsigned char keys[7][30]={
76 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
77 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
78 {8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
79 {4,0xef,0x01,0x23,0x45},
80 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
81 {4,0xef,0x01,0x23,0x45},
82 };
83
84static unsigned char data_len[7]={8,8,8,20,28,10};
85static unsigned char data[7][30]={
86 {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
87 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
88 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
89 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
90 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
91 0x00,0x00,0x00,0x00,0xff},
92 {0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
93 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
94 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
95 0x12,0x34,0x56,0x78,0xff},
96 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
97 {0},
98 };
99
100static unsigned char output[7][30]={
101 {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
102 {0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
103 {0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
104 {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
105 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
106 0x36,0xb6,0x78,0x58,0x00},
107 {0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
108 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
109 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
110 0x40,0x01,0x1e,0xcf,0x00},
111 {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
112 {0},
113 };
114
115int main(int argc, char *argv[])
116 {
117 int i,err=0;
118 int j;
119 unsigned char *p;
120 RC4_KEY key;
121 unsigned char obuf[512];
122
123#if !defined(OPENSSL_PIC)
124 void OPENSSL_cpuid_setup(void);
125
126 OPENSSL_cpuid_setup();
127#endif
128
129 for (i=0; i<6; i++)
130 {
131 RC4_set_key(&key,keys[i][0],&(keys[i][1]));
132 memset(obuf,0x00,sizeof(obuf));
133 RC4(&key,data_len[i],&(data[i][0]),obuf);
134 if (memcmp(obuf,output[i],data_len[i]+1) != 0)
135 {
136 printf("error calculating RC4\n");
137 printf("output:");
138 for (j=0; j<data_len[i]+1; j++)
139 printf(" %02x",obuf[j]);
140 printf("\n");
141 printf("expect:");
142 p= &(output[i][0]);
143 for (j=0; j<data_len[i]+1; j++)
144 printf(" %02x",*(p++));
145 printf("\n");
146 err++;
147 }
148 else
149 printf("test %d ok\n",i);
150 }
151 printf("test end processing ");
152 for (i=0; i<data_len[3]; i++)
153 {
154 RC4_set_key(&key,keys[3][0],&(keys[3][1]));
155 memset(obuf,0x00,sizeof(obuf));
156 RC4(&key,i,&(data[3][0]),obuf);
157 if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
158 {
159 printf("error in RC4 length processing\n");
160 printf("output:");
161 for (j=0; j<i+1; j++)
162 printf(" %02x",obuf[j]);
163 printf("\n");
164 printf("expect:");
165 p= &(output[3][0]);
166 for (j=0; j<i; j++)
167 printf(" %02x",*(p++));
168 printf(" 00\n");
169 err++;
170 }
171 else
172 {
173 printf(".");
174 fflush(stdout);
175 }
176 }
177 printf("done\n");
178 printf("test multi-call ");
179 for (i=0; i<data_len[3]; i++)
180 {
181 RC4_set_key(&key,keys[3][0],&(keys[3][1]));
182 memset(obuf,0x00,sizeof(obuf));
183 RC4(&key,i,&(data[3][0]),obuf);
184 RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
185 if (memcmp(obuf,output[3],data_len[3]+1) != 0)
186 {
187 printf("error in RC4 multi-call processing\n");
188 printf("output:");
189 for (j=0; j<data_len[3]+1; j++)
190 printf(" %02x",obuf[j]);
191 printf("\n");
192 printf("expect:");
193 p= &(output[3][0]);
194 for (j=0; j<data_len[3]+1; j++)
195 printf(" %02x",*(p++));
196 err++;
197 }
198 else
199 {
200 printf(".");
201 fflush(stdout);
202 }
203 }
204 printf("done\n");
205 printf("bulk test ");
206 { unsigned char buf[513];
207 SHA_CTX c;
208 unsigned char md[SHA_DIGEST_LENGTH];
209 static unsigned char expected[]={
210 0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
211 0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };
212
213 RC4_set_key(&key,keys[0][0],&(keys[3][1]));
214 memset(buf,'\0',sizeof(buf));
215 SHA1_Init(&c);
216 for (i=0;i<2571;i++) {
217 RC4(&key,sizeof(buf),buf,buf);
218 SHA1_Update(&c,buf,sizeof(buf));
219 }
220 SHA1_Final(md,&c);
221
222 if (memcmp(md,expected,sizeof(md))) {
223 printf("error in RC4 bulk test\n");
224 printf("output:");
225 for (j=0; j<(int)sizeof(md); j++)
226 printf(" %02x",md[j]);
227 printf("\n");
228 printf("expect:");
229 for (j=0; j<(int)sizeof(md); j++)
230 printf(" %02x",expected[j]);
231 printf("\n");
232 err++;
233 }
234 else printf("ok\n");
235 }
236#ifdef OPENSSL_SYS_NETWARE
237 if (err) printf("ERROR: %d\n", err);
238#endif
239 EXIT(err);
240 return(0);
241 }
242#endif
diff --git a/src/lib/libcrypto/ripemd/rmdtest.c b/src/lib/libcrypto/ripemd/rmdtest.c
deleted file mode 100644
index b32fda96ed..0000000000
--- a/src/lib/libcrypto/ripemd/rmdtest.c
+++ /dev/null
@@ -1,138 +0,0 @@
1/* crypto/ripemd/rmdtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_RIPEMD
66int main(int argc, char *argv[])
67{
68 printf("No ripemd support\n");
69 return(0);
70}
71#else
72#include <openssl/ripemd.h>
73#include <openssl/evp.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
82 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
83 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
84 NULL,
85 };
86
87static char *ret[]={
88 "9c1185a5c5e9fc54612808977ee8f548b2258d31",
89 "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe",
90 "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
91 "5d0689ef49d2fae572b881b123a85ffa21595f36",
92 "f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
93 "12a053384a9c0c88e405a06c27dcf49ada62eb2b",
94 "b0e20b6e3116640286ed3a87a5713079b21f5189",
95 "9b752e45573d4b39f4dbd3323cab82bf63326bfb",
96 };
97
98static char *pt(unsigned char *md);
99int main(int argc, char *argv[])
100 {
101 int i,err=0;
102 char **P,**R;
103 char *p;
104 unsigned char md[RIPEMD160_DIGEST_LENGTH];
105
106 P=test;
107 R=ret;
108 i=1;
109 while (*P != NULL)
110 {
111 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_ripemd160(), NULL);
112 p=pt(md);
113 if (strcmp(p,(char *)*R) != 0)
114 {
115 printf("error calculating RIPEMD160 on '%s'\n",*P);
116 printf("got %s instead of %s\n",p,*R);
117 err++;
118 }
119 else
120 printf("test %d ok\n",i);
121 i++;
122 R++;
123 P++;
124 }
125 EXIT(err);
126 return(0);
127 }
128
129static char *pt(unsigned char *md)
130 {
131 int i;
132 static char buf[80];
133
134 for (i=0; i<RIPEMD160_DIGEST_LENGTH; i++)
135 sprintf(&(buf[i*2]),"%02x",md[i]);
136 return(buf);
137 }
138#endif
diff --git a/src/lib/libcrypto/sha/sha1test.c b/src/lib/libcrypto/sha/sha1test.c
deleted file mode 100644
index ad9e3dc407..0000000000
--- a/src/lib/libcrypto/sha/sha1test.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/* crypto/sha/sha1test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_SHA
66int main(int argc, char *argv[])
67{
68 printf("No SHA support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/sha.h>
74
75#undef SHA_0 /* FIPS 180 */
76#define SHA_1 /* FIPS 180-1 */
77
78static char *test[]={
79 "abc",
80 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
81 NULL,
82 };
83
84#ifdef SHA_0
85static char *ret[]={
86 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
87 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
88 };
89static char *bigret=
90 "3232affa48628a26653b5aaa44541fd90d690603";
91#endif
92#ifdef SHA_1
93static char *ret[]={
94 "a9993e364706816aba3e25717850c26c9cd0d89d",
95 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
96 };
97static char *bigret=
98 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
99#endif
100
101static char *pt(unsigned char *md);
102int main(int argc, char *argv[])
103 {
104 int i,err=0;
105 char **P,**R;
106 static unsigned char buf[1000];
107 char *p,*r;
108 EVP_MD_CTX c;
109 unsigned char md[SHA_DIGEST_LENGTH];
110
111 EVP_MD_CTX_init(&c);
112 P=test;
113 R=ret;
114 i=1;
115 while (*P != NULL)
116 {
117 EVP_Digest(*P,strlen((char *)*P),md,NULL,EVP_sha1(), NULL);
118 p=pt(md);
119 if (strcmp(p,(char *)*R) != 0)
120 {
121 printf("error calculating SHA1 on '%s'\n",*P);
122 printf("got %s instead of %s\n",p,*R);
123 err++;
124 }
125 else
126 printf("test %d ok\n",i);
127 i++;
128 R++;
129 P++;
130 }
131
132 memset(buf,'a',1000);
133 EVP_DigestInit_ex(&c,EVP_sha1(), NULL);
134 for (i=0; i<1000; i++)
135 EVP_DigestUpdate(&c,buf,1000);
136 EVP_DigestFinal_ex(&c,md,NULL);
137 p=pt(md);
138
139 r=bigret;
140 if (strcmp(p,r) != 0)
141 {
142 printf("error calculating SHA1 on 'a' * 1000\n");
143 printf("got %s instead of %s\n",p,r);
144 err++;
145 }
146 else
147 printf("test 3 ok\n");
148
149#ifdef OPENSSL_SYS_NETWARE
150 if (err) printf("ERROR: %d\n", err);
151#endif
152 EXIT(err);
153 EVP_MD_CTX_cleanup(&c);
154 return(0);
155 }
156
157static char *pt(unsigned char *md)
158 {
159 int i;
160 static char buf[80];
161
162 for (i=0; i<SHA_DIGEST_LENGTH; i++)
163 sprintf(&(buf[i*2]),"%02x",md[i]);
164 return(buf);
165 }
166#endif
diff --git a/src/lib/libcrypto/sha/shatest.c b/src/lib/libcrypto/sha/shatest.c
deleted file mode 100644
index 6c93c39590..0000000000
--- a/src/lib/libcrypto/sha/shatest.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/* crypto/sha/shatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0)
66int main(int argc, char *argv[])
67{
68 printf("No SHA0 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/sha.h>
74
75#define SHA_0 /* FIPS 180 */
76#undef SHA_1 /* FIPS 180-1 */
77
78static char *test[]={
79 "abc",
80 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
81 NULL,
82 };
83
84#ifdef SHA_0
85static char *ret[]={
86 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
87 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
88 };
89static char *bigret=
90 "3232affa48628a26653b5aaa44541fd90d690603";
91#endif
92#ifdef SHA_1
93static char *ret[]={
94 "a9993e364706816aba3e25717850c26c9cd0d89d",
95 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
96 };
97static char *bigret=
98 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
99#endif
100
101static char *pt(unsigned char *md);
102int main(int argc, char *argv[])
103 {
104 int i,err=0;
105 char **P,**R;
106 static unsigned char buf[1000];
107 char *p,*r;
108 EVP_MD_CTX c;
109 unsigned char md[SHA_DIGEST_LENGTH];
110
111 EVP_MD_CTX_init(&c);
112 P=test;
113 R=ret;
114 i=1;
115 while (*P != NULL)
116 {
117 EVP_Digest(*P,strlen(*P),md,NULL,EVP_sha(), NULL);
118 p=pt(md);
119 if (strcmp(p,*R) != 0)
120 {
121 printf("error calculating SHA on '%s'\n",*P);
122 printf("got %s instead of %s\n",p,*R);
123 err++;
124 }
125 else
126 printf("test %d ok\n",i);
127 i++;
128 R++;
129 P++;
130 }
131
132 memset(buf,'a',1000);
133 EVP_DigestInit_ex(&c,EVP_sha(), NULL);
134 for (i=0; i<1000; i++)
135 EVP_DigestUpdate(&c,buf,1000);
136 EVP_DigestFinal_ex(&c,md,NULL);
137 p=pt(md);
138
139 r=bigret;
140 if (strcmp(p,r) != 0)
141 {
142 printf("error calculating SHA on '%s'\n",p);
143 printf("got %s instead of %s\n",p,r);
144 err++;
145 }
146 else
147 printf("test 3 ok\n");
148
149#ifdef OPENSSL_SYS_NETWARE
150 if (err) printf("ERROR: %d\n", err);
151#endif
152 EVP_MD_CTX_cleanup(&c);
153 EXIT(err);
154 return(0);
155 }
156
157static char *pt(unsigned char *md)
158 {
159 int i;
160 static char buf[80];
161
162 for (i=0; i<SHA_DIGEST_LENGTH; i++)
163 sprintf(&(buf[i*2]),"%02x",md[i]);
164 return(buf);
165 }
166#endif
diff --git a/src/lib/libssl/src/crypto/bf/bftest.c b/src/lib/libssl/src/crypto/bf/bftest.c
deleted file mode 100644
index 20caf0f3f8..0000000000
--- a/src/lib/libssl/src/crypto/bf/bftest.c
+++ /dev/null
@@ -1,529 +0,0 @@
1/* crypto/bf/bftest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* This has been a quickly hacked 'ideatest.c'. When I add tests for other
60 * RC2 modes, more of the code will be uncommented. */
61
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_BF is defined */
66
67#include "../e_os.h"
68
69#ifdef OPENSSL_NO_BF
70int main(int argc, char *argv[])
71{
72 printf("No BF support\n");
73 return(0);
74}
75#else
76#include <openssl/blowfish.h>
77
78static char *bf_key[2]={
79 "abcdefghijklmnopqrstuvwxyz",
80 "Who is John Galt?"
81 };
82
83/* big endian */
84static BF_LONG bf_plain[2][2]={
85 {0x424c4f57L,0x46495348L},
86 {0xfedcba98L,0x76543210L}
87 };
88
89static BF_LONG bf_cipher[2][2]={
90 {0x324ed0feL,0xf413a203L},
91 {0xcc91732bL,0x8022f684L}
92 };
93/************/
94
95/* Lets use the DES test vectors :-) */
96#define NUM_TESTS 34
97static unsigned char ecb_data[NUM_TESTS][8]={
98 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
99 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
100 {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
101 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
102 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
103 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
104 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
105 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
106 {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
107 {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
108 {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
109 {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
110 {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
111 {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
112 {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
113 {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
114 {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
115 {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
116 {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
117 {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
118 {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
119 {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
120 {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
121 {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
122 {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
123 {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
124 {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
125 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
126 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
127 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
128 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
129 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
130 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
131 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
132
133static unsigned char plain_data[NUM_TESTS][8]={
134 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
135 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
136 {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
137 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
138 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
139 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
140 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
141 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
142 {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
143 {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
144 {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
145 {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
146 {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
147 {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
148 {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
149 {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
150 {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
151 {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
152 {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
153 {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
154 {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
155 {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
156 {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
157 {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
158 {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
159 {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
160 {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
161 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
162 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
163 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
164 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
165 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
166 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
167 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
168
169static unsigned char cipher_data[NUM_TESTS][8]={
170 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
171 {0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A},
172 {0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2},
173 {0x24,0x66,0xDD,0x87,0x8B,0x96,0x3C,0x9D},
174 {0x61,0xF9,0xC3,0x80,0x22,0x81,0xB0,0x96},
175 {0x7D,0x0C,0xC6,0x30,0xAF,0xDA,0x1E,0xC7},
176 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
177 {0x0A,0xCE,0xAB,0x0F,0xC6,0xA0,0xA2,0x8D},
178 {0x59,0xC6,0x82,0x45,0xEB,0x05,0x28,0x2B},
179 {0xB1,0xB8,0xCC,0x0B,0x25,0x0F,0x09,0xA0},
180 {0x17,0x30,0xE5,0x77,0x8B,0xEA,0x1D,0xA4},
181 {0xA2,0x5E,0x78,0x56,0xCF,0x26,0x51,0xEB},
182 {0x35,0x38,0x82,0xB1,0x09,0xCE,0x8F,0x1A},
183 {0x48,0xF4,0xD0,0x88,0x4C,0x37,0x99,0x18},
184 {0x43,0x21,0x93,0xB7,0x89,0x51,0xFC,0x98},
185 {0x13,0xF0,0x41,0x54,0xD6,0x9D,0x1A,0xE5},
186 {0x2E,0xED,0xDA,0x93,0xFF,0xD3,0x9C,0x79},
187 {0xD8,0x87,0xE0,0x39,0x3C,0x2D,0xA6,0xE3},
188 {0x5F,0x99,0xD0,0x4F,0x5B,0x16,0x39,0x69},
189 {0x4A,0x05,0x7A,0x3B,0x24,0xD3,0x97,0x7B},
190 {0x45,0x20,0x31,0xC1,0xE4,0xFA,0xDA,0x8E},
191 {0x75,0x55,0xAE,0x39,0xF5,0x9B,0x87,0xBD},
192 {0x53,0xC5,0x5F,0x9C,0xB4,0x9F,0xC0,0x19},
193 {0x7A,0x8E,0x7B,0xFA,0x93,0x7E,0x89,0xA3},
194 {0xCF,0x9C,0x5D,0x7A,0x49,0x86,0xAD,0xB5},
195 {0xD1,0xAB,0xB2,0x90,0x65,0x8B,0xC7,0x78},
196 {0x55,0xCB,0x37,0x74,0xD1,0x3E,0xF2,0x01},
197 {0xFA,0x34,0xEC,0x48,0x47,0xB2,0x68,0xB2},
198 {0xA7,0x90,0x79,0x51,0x08,0xEA,0x3C,0xAE},
199 {0xC3,0x9E,0x07,0x2D,0x9F,0xAC,0x63,0x1D},
200 {0x01,0x49,0x33,0xE0,0xCD,0xAF,0xF6,0xE4},
201 {0xF2,0x1E,0x9A,0x77,0xB7,0x1C,0x49,0xBC},
202 {0x24,0x59,0x46,0x88,0x57,0x54,0x36,0x9A},
203 {0x6B,0x5C,0x5A,0x9C,0x5D,0x9E,0x0A,0x5A},
204 };
205
206static unsigned char cbc_key [16]={
207 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
208 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
209static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
210static char cbc_data[40]="7654321 Now is the time for ";
211static unsigned char cbc_ok[32]={
212 0x6B,0x77,0xB4,0xD6,0x30,0x06,0xDE,0xE6,
213 0x05,0xB1,0x56,0xE2,0x74,0x03,0x97,0x93,
214 0x58,0xDE,0xB9,0xE7,0x15,0x46,0x16,0xD9,
215 0x59,0xF1,0x65,0x2B,0xD5,0xFF,0x92,0xCC};
216
217static unsigned char cfb64_ok[]={
218 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
219 0xF2,0x6E,0xCF,0x6D,0x2E,0xB9,0xE7,0x6E,
220 0x3D,0xA3,0xDE,0x04,0xD1,0x51,0x72,0x00,
221 0x51,0x9D,0x57,0xA6,0xC3};
222
223static unsigned char ofb64_ok[]={
224 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
225 0x62,0xB3,0x43,0xCC,0x5B,0x65,0x58,0x73,
226 0x10,0xDD,0x90,0x8D,0x0C,0x24,0x1B,0x22,
227 0x63,0xC2,0xCF,0x80,0xDA};
228
229#define KEY_TEST_NUM 25
230static unsigned char key_test[KEY_TEST_NUM]={
231 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87,
232 0x78,0x69,0x5a,0x4b,0x3c,0x2d,0x1e,0x0f,
233 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
234 0x88};
235
236static unsigned char key_data[8]=
237 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10};
238
239static unsigned char key_out[KEY_TEST_NUM][8]={
240 {0xF9,0xAD,0x59,0x7C,0x49,0xDB,0x00,0x5E},
241 {0xE9,0x1D,0x21,0xC1,0xD9,0x61,0xA6,0xD6},
242 {0xE9,0xC2,0xB7,0x0A,0x1B,0xC6,0x5C,0xF3},
243 {0xBE,0x1E,0x63,0x94,0x08,0x64,0x0F,0x05},
244 {0xB3,0x9E,0x44,0x48,0x1B,0xDB,0x1E,0x6E},
245 {0x94,0x57,0xAA,0x83,0xB1,0x92,0x8C,0x0D},
246 {0x8B,0xB7,0x70,0x32,0xF9,0x60,0x62,0x9D},
247 {0xE8,0x7A,0x24,0x4E,0x2C,0xC8,0x5E,0x82},
248 {0x15,0x75,0x0E,0x7A,0x4F,0x4E,0xC5,0x77},
249 {0x12,0x2B,0xA7,0x0B,0x3A,0xB6,0x4A,0xE0},
250 {0x3A,0x83,0x3C,0x9A,0xFF,0xC5,0x37,0xF6},
251 {0x94,0x09,0xDA,0x87,0xA9,0x0F,0x6B,0xF2},
252 {0x88,0x4F,0x80,0x62,0x50,0x60,0xB8,0xB4},
253 {0x1F,0x85,0x03,0x1C,0x19,0xE1,0x19,0x68},
254 {0x79,0xD9,0x37,0x3A,0x71,0x4C,0xA3,0x4F},
255 {0x93,0x14,0x28,0x87,0xEE,0x3B,0xE1,0x5C},
256 {0x03,0x42,0x9E,0x83,0x8C,0xE2,0xD1,0x4B},
257 {0xA4,0x29,0x9E,0x27,0x46,0x9F,0xF6,0x7B},
258 {0xAF,0xD5,0xAE,0xD1,0xC1,0xBC,0x96,0xA8},
259 {0x10,0x85,0x1C,0x0E,0x38,0x58,0xDA,0x9F},
260 {0xE6,0xF5,0x1E,0xD7,0x9B,0x9D,0xB2,0x1F},
261 {0x64,0xA6,0xE1,0x4A,0xFD,0x36,0xB4,0x6F},
262 {0x80,0xC7,0xD7,0xD4,0x5A,0x54,0x79,0xAD},
263 {0x05,0x04,0x4B,0x62,0xFA,0x52,0xD0,0x80},
264 };
265
266static int test(void );
267static int print_test_data(void );
268int main(int argc, char *argv[])
269 {
270 int ret;
271
272 if (argc > 1)
273 ret=print_test_data();
274 else
275 ret=test();
276
277#ifdef OPENSSL_SYS_NETWARE
278 if (ret) printf("ERROR: %d\n", ret);
279#endif
280 EXIT(ret);
281 return(0);
282 }
283
284static int print_test_data(void)
285 {
286 unsigned int i,j;
287
288 printf("ecb test data\n");
289 printf("key bytes\t\tclear bytes\t\tcipher bytes\n");
290 for (i=0; i<NUM_TESTS; i++)
291 {
292 for (j=0; j<8; j++)
293 printf("%02X",ecb_data[i][j]);
294 printf("\t");
295 for (j=0; j<8; j++)
296 printf("%02X",plain_data[i][j]);
297 printf("\t");
298 for (j=0; j<8; j++)
299 printf("%02X",cipher_data[i][j]);
300 printf("\n");
301 }
302
303 printf("set_key test data\n");
304 printf("data[8]= ");
305 for (j=0; j<8; j++)
306 printf("%02X",key_data[j]);
307 printf("\n");
308 for (i=0; i<KEY_TEST_NUM-1; i++)
309 {
310 printf("c=");
311 for (j=0; j<8; j++)
312 printf("%02X",key_out[i][j]);
313 printf(" k[%2u]=",i+1);
314 for (j=0; j<i+1; j++)
315 printf("%02X",key_test[j]);
316 printf("\n");
317 }
318
319 printf("\nchaining mode test data\n");
320 printf("key[16] = ");
321 for (j=0; j<16; j++)
322 printf("%02X",cbc_key[j]);
323 printf("\niv[8] = ");
324 for (j=0; j<8; j++)
325 printf("%02X",cbc_iv[j]);
326 printf("\ndata[%d] = '%s'",(int)strlen(cbc_data)+1,cbc_data);
327 printf("\ndata[%d] = ",(int)strlen(cbc_data)+1);
328 for (j=0; j<strlen(cbc_data)+1; j++)
329 printf("%02X",cbc_data[j]);
330 printf("\n");
331 printf("cbc cipher text\n");
332 printf("cipher[%d]= ",32);
333 for (j=0; j<32; j++)
334 printf("%02X",cbc_ok[j]);
335 printf("\n");
336
337 printf("cfb64 cipher text\n");
338 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
339 for (j=0; j<strlen(cbc_data)+1; j++)
340 printf("%02X",cfb64_ok[j]);
341 printf("\n");
342
343 printf("ofb64 cipher text\n");
344 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
345 for (j=0; j<strlen(cbc_data)+1; j++)
346 printf("%02X",ofb64_ok[j]);
347 printf("\n");
348 return(0);
349 }
350
351static int test(void)
352 {
353 unsigned char cbc_in[40],cbc_out[40],iv[8];
354 int i,n,err=0;
355 BF_KEY key;
356 BF_LONG data[2];
357 unsigned char out[8];
358 BF_LONG len;
359
360 printf("testing blowfish in raw ecb mode\n");
361 for (n=0; n<2; n++)
362 {
363 BF_set_key(&key,strlen(bf_key[n]),(unsigned char *)bf_key[n]);
364
365 data[0]=bf_plain[n][0];
366 data[1]=bf_plain[n][1];
367 BF_encrypt(data,&key);
368 if (memcmp(&(bf_cipher[n][0]),&(data[0]),8) != 0)
369 {
370 printf("BF_encrypt error encrypting\n");
371 printf("got :");
372 for (i=0; i<2; i++)
373 printf("%08lX ",(unsigned long)data[i]);
374 printf("\n");
375 printf("expected:");
376 for (i=0; i<2; i++)
377 printf("%08lX ",(unsigned long)bf_cipher[n][i]);
378 err=1;
379 printf("\n");
380 }
381
382 BF_decrypt(&(data[0]),&key);
383 if (memcmp(&(bf_plain[n][0]),&(data[0]),8) != 0)
384 {
385 printf("BF_encrypt error decrypting\n");
386 printf("got :");
387 for (i=0; i<2; i++)
388 printf("%08lX ",(unsigned long)data[i]);
389 printf("\n");
390 printf("expected:");
391 for (i=0; i<2; i++)
392 printf("%08lX ",(unsigned long)bf_plain[n][i]);
393 printf("\n");
394 err=1;
395 }
396 }
397
398 printf("testing blowfish in ecb mode\n");
399
400 for (n=0; n<NUM_TESTS; n++)
401 {
402 BF_set_key(&key,8,ecb_data[n]);
403
404 BF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT);
405 if (memcmp(&(cipher_data[n][0]),out,8) != 0)
406 {
407 printf("BF_ecb_encrypt blowfish error encrypting\n");
408 printf("got :");
409 for (i=0; i<8; i++)
410 printf("%02X ",out[i]);
411 printf("\n");
412 printf("expected:");
413 for (i=0; i<8; i++)
414 printf("%02X ",cipher_data[n][i]);
415 err=1;
416 printf("\n");
417 }
418
419 BF_ecb_encrypt(out,out,&key,BF_DECRYPT);
420 if (memcmp(&(plain_data[n][0]),out,8) != 0)
421 {
422 printf("BF_ecb_encrypt error decrypting\n");
423 printf("got :");
424 for (i=0; i<8; i++)
425 printf("%02X ",out[i]);
426 printf("\n");
427 printf("expected:");
428 for (i=0; i<8; i++)
429 printf("%02X ",plain_data[n][i]);
430 printf("\n");
431 err=1;
432 }
433 }
434
435 printf("testing blowfish set_key\n");
436 for (n=1; n<KEY_TEST_NUM; n++)
437 {
438 BF_set_key(&key,n,key_test);
439 BF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT);
440 /* mips-sgi-irix6.5-gcc vv -mabi=64 bug workaround */
441 if (memcmp(out,&(key_out[i=n-1][0]),8) != 0)
442 {
443 printf("blowfish setkey error\n");
444 err=1;
445 }
446 }
447
448 printf("testing blowfish in cbc mode\n");
449 len=strlen(cbc_data)+1;
450
451 BF_set_key(&key,16,cbc_key);
452 memset(cbc_in,0,sizeof cbc_in);
453 memset(cbc_out,0,sizeof cbc_out);
454 memcpy(iv,cbc_iv,sizeof iv);
455 BF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len,
456 &key,iv,BF_ENCRYPT);
457 if (memcmp(cbc_out,cbc_ok,32) != 0)
458 {
459 err=1;
460 printf("BF_cbc_encrypt encrypt error\n");
461 for (i=0; i<32; i++) printf("0x%02X,",cbc_out[i]);
462 }
463 memcpy(iv,cbc_iv,8);
464 BF_cbc_encrypt(cbc_out,cbc_in,len,
465 &key,iv,BF_DECRYPT);
466 if (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0)
467 {
468 printf("BF_cbc_encrypt decrypt error\n");
469 err=1;
470 }
471
472 printf("testing blowfish in cfb64 mode\n");
473
474 BF_set_key(&key,16,cbc_key);
475 memset(cbc_in,0,40);
476 memset(cbc_out,0,40);
477 memcpy(iv,cbc_iv,8);
478 n=0;
479 BF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,
480 &key,iv,&n,BF_ENCRYPT);
481 BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13,
482 &key,iv,&n,BF_ENCRYPT);
483 if (memcmp(cbc_out,cfb64_ok,(int)len) != 0)
484 {
485 err=1;
486 printf("BF_cfb64_encrypt encrypt error\n");
487 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
488 }
489 n=0;
490 memcpy(iv,cbc_iv,8);
491 BF_cfb64_encrypt(cbc_out,cbc_in,17,
492 &key,iv,&n,BF_DECRYPT);
493 BF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,
494 &key,iv,&n,BF_DECRYPT);
495 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
496 {
497 printf("BF_cfb64_encrypt decrypt error\n");
498 err=1;
499 }
500
501 printf("testing blowfish in ofb64\n");
502
503 BF_set_key(&key,16,cbc_key);
504 memset(cbc_in,0,40);
505 memset(cbc_out,0,40);
506 memcpy(iv,cbc_iv,8);
507 n=0;
508 BF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n);
509 BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]),
510 &(cbc_out[13]),len-13,&key,iv,&n);
511 if (memcmp(cbc_out,ofb64_ok,(int)len) != 0)
512 {
513 err=1;
514 printf("BF_ofb64_encrypt encrypt error\n");
515 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
516 }
517 n=0;
518 memcpy(iv,cbc_iv,8);
519 BF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n);
520 BF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n);
521 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
522 {
523 printf("BF_ofb64_encrypt decrypt error\n");
524 err=1;
525 }
526
527 return(err);
528 }
529#endif
diff --git a/src/lib/libssl/src/crypto/bn/bntest.c b/src/lib/libssl/src/crypto/bn/bntest.c
deleted file mode 100644
index 06f5954acc..0000000000
--- a/src/lib/libssl/src/crypto/bn/bntest.c
+++ /dev/null
@@ -1,2013 +0,0 @@
1/* crypto/bn/bntest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the Eric Young open source
65 * license provided above.
66 *
67 * The binary polynomial arithmetic software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72/* Until the key-gen callbacks are modified to use newer prototypes, we allow
73 * deprecated functions for openssl-internal code */
74#ifdef OPENSSL_NO_DEPRECATED
75#undef OPENSSL_NO_DEPRECATED
76#endif
77
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81
82#include "e_os.h"
83
84#include <openssl/bio.h>
85#include <openssl/bn.h>
86#include <openssl/rand.h>
87#include <openssl/x509.h>
88#include <openssl/err.h>
89
90const int num0 = 100; /* number of tests */
91const int num1 = 50; /* additional tests for some functions */
92const int num2 = 5; /* number of tests for slow functions */
93
94int test_add(BIO *bp);
95int test_sub(BIO *bp);
96int test_lshift1(BIO *bp);
97int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_);
98int test_rshift1(BIO *bp);
99int test_rshift(BIO *bp,BN_CTX *ctx);
100int test_div(BIO *bp,BN_CTX *ctx);
101int test_div_word(BIO *bp);
102int test_div_recp(BIO *bp,BN_CTX *ctx);
103int test_mul(BIO *bp);
104int test_sqr(BIO *bp,BN_CTX *ctx);
105int test_mont(BIO *bp,BN_CTX *ctx);
106int test_mod(BIO *bp,BN_CTX *ctx);
107int test_mod_mul(BIO *bp,BN_CTX *ctx);
108int test_mod_exp(BIO *bp,BN_CTX *ctx);
109int test_mod_exp_mont_consttime(BIO *bp,BN_CTX *ctx);
110int test_exp(BIO *bp,BN_CTX *ctx);
111int test_gf2m_add(BIO *bp);
112int test_gf2m_mod(BIO *bp);
113int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx);
114int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx);
115int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx);
116int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx);
117int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx);
118int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx);
119int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx);
120int test_kron(BIO *bp,BN_CTX *ctx);
121int test_sqrt(BIO *bp,BN_CTX *ctx);
122int rand_neg(void);
123static int results=0;
124
125static unsigned char lst[]="\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
126"\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
127
128static const char rnd_seed[] = "string to make the random number generator think it has entropy";
129
130static void message(BIO *out, char *m)
131 {
132 fprintf(stderr, "test %s\n", m);
133 BIO_puts(out, "print \"test ");
134 BIO_puts(out, m);
135 BIO_puts(out, "\\n\"\n");
136 }
137
138int main(int argc, char *argv[])
139 {
140 BN_CTX *ctx;
141 BIO *out;
142 char *outfile=NULL;
143
144 results = 0;
145
146 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
147
148 argc--;
149 argv++;
150 while (argc >= 1)
151 {
152 if (strcmp(*argv,"-results") == 0)
153 results=1;
154 else if (strcmp(*argv,"-out") == 0)
155 {
156 if (--argc < 1) break;
157 outfile= *(++argv);
158 }
159 argc--;
160 argv++;
161 }
162
163
164 ctx=BN_CTX_new();
165 if (ctx == NULL) EXIT(1);
166
167 out=BIO_new(BIO_s_file());
168 if (out == NULL) EXIT(1);
169 if (outfile == NULL)
170 {
171 BIO_set_fp(out,stdout,BIO_NOCLOSE);
172 }
173 else
174 {
175 if (!BIO_write_filename(out,outfile))
176 {
177 perror(outfile);
178 EXIT(1);
179 }
180 }
181
182 if (!results)
183 BIO_puts(out,"obase=16\nibase=16\n");
184
185 message(out,"BN_add");
186 if (!test_add(out)) goto err;
187 (void)BIO_flush(out);
188
189 message(out,"BN_sub");
190 if (!test_sub(out)) goto err;
191 (void)BIO_flush(out);
192
193 message(out,"BN_lshift1");
194 if (!test_lshift1(out)) goto err;
195 (void)BIO_flush(out);
196
197 message(out,"BN_lshift (fixed)");
198 if (!test_lshift(out,ctx,BN_bin2bn(lst,sizeof(lst)-1,NULL)))
199 goto err;
200 (void)BIO_flush(out);
201
202 message(out,"BN_lshift");
203 if (!test_lshift(out,ctx,NULL)) goto err;
204 (void)BIO_flush(out);
205
206 message(out,"BN_rshift1");
207 if (!test_rshift1(out)) goto err;
208 (void)BIO_flush(out);
209
210 message(out,"BN_rshift");
211 if (!test_rshift(out,ctx)) goto err;
212 (void)BIO_flush(out);
213
214 message(out,"BN_sqr");
215 if (!test_sqr(out,ctx)) goto err;
216 (void)BIO_flush(out);
217
218 message(out,"BN_mul");
219 if (!test_mul(out)) goto err;
220 (void)BIO_flush(out);
221
222 message(out,"BN_div");
223 if (!test_div(out,ctx)) goto err;
224 (void)BIO_flush(out);
225
226 message(out,"BN_div_word");
227 if (!test_div_word(out)) goto err;
228 (void)BIO_flush(out);
229
230 message(out,"BN_div_recp");
231 if (!test_div_recp(out,ctx)) goto err;
232 (void)BIO_flush(out);
233
234 message(out,"BN_mod");
235 if (!test_mod(out,ctx)) goto err;
236 (void)BIO_flush(out);
237
238 message(out,"BN_mod_mul");
239 if (!test_mod_mul(out,ctx)) goto err;
240 (void)BIO_flush(out);
241
242 message(out,"BN_mont");
243 if (!test_mont(out,ctx)) goto err;
244 (void)BIO_flush(out);
245
246 message(out,"BN_mod_exp");
247 if (!test_mod_exp(out,ctx)) goto err;
248 (void)BIO_flush(out);
249
250 message(out,"BN_mod_exp_mont_consttime");
251 if (!test_mod_exp_mont_consttime(out,ctx)) goto err;
252 (void)BIO_flush(out);
253
254 message(out,"BN_exp");
255 if (!test_exp(out,ctx)) goto err;
256 (void)BIO_flush(out);
257
258 message(out,"BN_kronecker");
259 if (!test_kron(out,ctx)) goto err;
260 (void)BIO_flush(out);
261
262 message(out,"BN_mod_sqrt");
263 if (!test_sqrt(out,ctx)) goto err;
264 (void)BIO_flush(out);
265#ifndef OPENSSL_NO_EC2M
266 message(out,"BN_GF2m_add");
267 if (!test_gf2m_add(out)) goto err;
268 (void)BIO_flush(out);
269
270 message(out,"BN_GF2m_mod");
271 if (!test_gf2m_mod(out)) goto err;
272 (void)BIO_flush(out);
273
274 message(out,"BN_GF2m_mod_mul");
275 if (!test_gf2m_mod_mul(out,ctx)) goto err;
276 (void)BIO_flush(out);
277
278 message(out,"BN_GF2m_mod_sqr");
279 if (!test_gf2m_mod_sqr(out,ctx)) goto err;
280 (void)BIO_flush(out);
281
282 message(out,"BN_GF2m_mod_inv");
283 if (!test_gf2m_mod_inv(out,ctx)) goto err;
284 (void)BIO_flush(out);
285
286 message(out,"BN_GF2m_mod_div");
287 if (!test_gf2m_mod_div(out,ctx)) goto err;
288 (void)BIO_flush(out);
289
290 message(out,"BN_GF2m_mod_exp");
291 if (!test_gf2m_mod_exp(out,ctx)) goto err;
292 (void)BIO_flush(out);
293
294 message(out,"BN_GF2m_mod_sqrt");
295 if (!test_gf2m_mod_sqrt(out,ctx)) goto err;
296 (void)BIO_flush(out);
297
298 message(out,"BN_GF2m_mod_solve_quad");
299 if (!test_gf2m_mod_solve_quad(out,ctx)) goto err;
300 (void)BIO_flush(out);
301#endif
302 BN_CTX_free(ctx);
303 BIO_free(out);
304
305/**/
306 EXIT(0);
307err:
308 BIO_puts(out,"1\n"); /* make sure the Perl script fed by bc notices
309 * the failure, see test_bn in test/Makefile.ssl*/
310 (void)BIO_flush(out);
311 ERR_load_crypto_strings();
312 ERR_print_errors_fp(stderr);
313 EXIT(1);
314 return(1);
315 }
316
317int test_add(BIO *bp)
318 {
319 BIGNUM a,b,c;
320 int i;
321
322 BN_init(&a);
323 BN_init(&b);
324 BN_init(&c);
325
326 BN_bntest_rand(&a,512,0,0);
327 for (i=0; i<num0; i++)
328 {
329 BN_bntest_rand(&b,450+i,0,0);
330 a.neg=rand_neg();
331 b.neg=rand_neg();
332 BN_add(&c,&a,&b);
333 if (bp != NULL)
334 {
335 if (!results)
336 {
337 BN_print(bp,&a);
338 BIO_puts(bp," + ");
339 BN_print(bp,&b);
340 BIO_puts(bp," - ");
341 }
342 BN_print(bp,&c);
343 BIO_puts(bp,"\n");
344 }
345 a.neg=!a.neg;
346 b.neg=!b.neg;
347 BN_add(&c,&c,&b);
348 BN_add(&c,&c,&a);
349 if(!BN_is_zero(&c))
350 {
351 fprintf(stderr,"Add test failed!\n");
352 return 0;
353 }
354 }
355 BN_free(&a);
356 BN_free(&b);
357 BN_free(&c);
358 return(1);
359 }
360
361int test_sub(BIO *bp)
362 {
363 BIGNUM a,b,c;
364 int i;
365
366 BN_init(&a);
367 BN_init(&b);
368 BN_init(&c);
369
370 for (i=0; i<num0+num1; i++)
371 {
372 if (i < num1)
373 {
374 BN_bntest_rand(&a,512,0,0);
375 BN_copy(&b,&a);
376 if (BN_set_bit(&a,i)==0) return(0);
377 BN_add_word(&b,i);
378 }
379 else
380 {
381 BN_bntest_rand(&b,400+i-num1,0,0);
382 a.neg=rand_neg();
383 b.neg=rand_neg();
384 }
385 BN_sub(&c,&a,&b);
386 if (bp != NULL)
387 {
388 if (!results)
389 {
390 BN_print(bp,&a);
391 BIO_puts(bp," - ");
392 BN_print(bp,&b);
393 BIO_puts(bp," - ");
394 }
395 BN_print(bp,&c);
396 BIO_puts(bp,"\n");
397 }
398 BN_add(&c,&c,&b);
399 BN_sub(&c,&c,&a);
400 if(!BN_is_zero(&c))
401 {
402 fprintf(stderr,"Subtract test failed!\n");
403 return 0;
404 }
405 }
406 BN_free(&a);
407 BN_free(&b);
408 BN_free(&c);
409 return(1);
410 }
411
412int test_div(BIO *bp, BN_CTX *ctx)
413 {
414 BIGNUM a,b,c,d,e;
415 int i;
416
417 BN_init(&a);
418 BN_init(&b);
419 BN_init(&c);
420 BN_init(&d);
421 BN_init(&e);
422
423 for (i=0; i<num0+num1; i++)
424 {
425 if (i < num1)
426 {
427 BN_bntest_rand(&a,400,0,0);
428 BN_copy(&b,&a);
429 BN_lshift(&a,&a,i);
430 BN_add_word(&a,i);
431 }
432 else
433 BN_bntest_rand(&b,50+3*(i-num1),0,0);
434 a.neg=rand_neg();
435 b.neg=rand_neg();
436 BN_div(&d,&c,&a,&b,ctx);
437 if (bp != NULL)
438 {
439 if (!results)
440 {
441 BN_print(bp,&a);
442 BIO_puts(bp," / ");
443 BN_print(bp,&b);
444 BIO_puts(bp," - ");
445 }
446 BN_print(bp,&d);
447 BIO_puts(bp,"\n");
448
449 if (!results)
450 {
451 BN_print(bp,&a);
452 BIO_puts(bp," % ");
453 BN_print(bp,&b);
454 BIO_puts(bp," - ");
455 }
456 BN_print(bp,&c);
457 BIO_puts(bp,"\n");
458 }
459 BN_mul(&e,&d,&b,ctx);
460 BN_add(&d,&e,&c);
461 BN_sub(&d,&d,&a);
462 if(!BN_is_zero(&d))
463 {
464 fprintf(stderr,"Division test failed!\n");
465 return 0;
466 }
467 }
468 BN_free(&a);
469 BN_free(&b);
470 BN_free(&c);
471 BN_free(&d);
472 BN_free(&e);
473 return(1);
474 }
475
476static void print_word(BIO *bp,BN_ULONG w)
477 {
478#ifdef SIXTY_FOUR_BIT
479 if (sizeof(w) > sizeof(unsigned long))
480 {
481 unsigned long h=(unsigned long)(w>>32),
482 l=(unsigned long)(w);
483
484 if (h) BIO_printf(bp,"%lX%08lX",h,l);
485 else BIO_printf(bp,"%lX",l);
486 return;
487 }
488#endif
489 BIO_printf(bp,BN_HEX_FMT1,w);
490 }
491
492int test_div_word(BIO *bp)
493 {
494 BIGNUM a,b;
495 BN_ULONG r,s;
496 int i;
497
498 BN_init(&a);
499 BN_init(&b);
500
501 for (i=0; i<num0; i++)
502 {
503 do {
504 BN_bntest_rand(&a,512,-1,0);
505 BN_bntest_rand(&b,BN_BITS2,-1,0);
506 s = b.d[0];
507 } while (!s);
508
509 BN_copy(&b, &a);
510 r = BN_div_word(&b, s);
511
512 if (bp != NULL)
513 {
514 if (!results)
515 {
516 BN_print(bp,&a);
517 BIO_puts(bp," / ");
518 print_word(bp,s);
519 BIO_puts(bp," - ");
520 }
521 BN_print(bp,&b);
522 BIO_puts(bp,"\n");
523
524 if (!results)
525 {
526 BN_print(bp,&a);
527 BIO_puts(bp," % ");
528 print_word(bp,s);
529 BIO_puts(bp," - ");
530 }
531 print_word(bp,r);
532 BIO_puts(bp,"\n");
533 }
534 BN_mul_word(&b,s);
535 BN_add_word(&b,r);
536 BN_sub(&b,&a,&b);
537 if(!BN_is_zero(&b))
538 {
539 fprintf(stderr,"Division (word) test failed!\n");
540 return 0;
541 }
542 }
543 BN_free(&a);
544 BN_free(&b);
545 return(1);
546 }
547
548int test_div_recp(BIO *bp, BN_CTX *ctx)
549 {
550 BIGNUM a,b,c,d,e;
551 BN_RECP_CTX recp;
552 int i;
553
554 BN_RECP_CTX_init(&recp);
555 BN_init(&a);
556 BN_init(&b);
557 BN_init(&c);
558 BN_init(&d);
559 BN_init(&e);
560
561 for (i=0; i<num0+num1; i++)
562 {
563 if (i < num1)
564 {
565 BN_bntest_rand(&a,400,0,0);
566 BN_copy(&b,&a);
567 BN_lshift(&a,&a,i);
568 BN_add_word(&a,i);
569 }
570 else
571 BN_bntest_rand(&b,50+3*(i-num1),0,0);
572 a.neg=rand_neg();
573 b.neg=rand_neg();
574 BN_RECP_CTX_set(&recp,&b,ctx);
575 BN_div_recp(&d,&c,&a,&recp,ctx);
576 if (bp != NULL)
577 {
578 if (!results)
579 {
580 BN_print(bp,&a);
581 BIO_puts(bp," / ");
582 BN_print(bp,&b);
583 BIO_puts(bp," - ");
584 }
585 BN_print(bp,&d);
586 BIO_puts(bp,"\n");
587
588 if (!results)
589 {
590 BN_print(bp,&a);
591 BIO_puts(bp," % ");
592 BN_print(bp,&b);
593 BIO_puts(bp," - ");
594 }
595 BN_print(bp,&c);
596 BIO_puts(bp,"\n");
597 }
598 BN_mul(&e,&d,&b,ctx);
599 BN_add(&d,&e,&c);
600 BN_sub(&d,&d,&a);
601 if(!BN_is_zero(&d))
602 {
603 fprintf(stderr,"Reciprocal division test failed!\n");
604 fprintf(stderr,"a=");
605 BN_print_fp(stderr,&a);
606 fprintf(stderr,"\nb=");
607 BN_print_fp(stderr,&b);
608 fprintf(stderr,"\n");
609 return 0;
610 }
611 }
612 BN_free(&a);
613 BN_free(&b);
614 BN_free(&c);
615 BN_free(&d);
616 BN_free(&e);
617 BN_RECP_CTX_free(&recp);
618 return(1);
619 }
620
621int test_mul(BIO *bp)
622 {
623 BIGNUM a,b,c,d,e;
624 int i;
625 BN_CTX *ctx;
626
627 ctx = BN_CTX_new();
628 if (ctx == NULL) EXIT(1);
629
630 BN_init(&a);
631 BN_init(&b);
632 BN_init(&c);
633 BN_init(&d);
634 BN_init(&e);
635
636 for (i=0; i<num0+num1; i++)
637 {
638 if (i <= num1)
639 {
640 BN_bntest_rand(&a,100,0,0);
641 BN_bntest_rand(&b,100,0,0);
642 }
643 else
644 BN_bntest_rand(&b,i-num1,0,0);
645 a.neg=rand_neg();
646 b.neg=rand_neg();
647 BN_mul(&c,&a,&b,ctx);
648 if (bp != NULL)
649 {
650 if (!results)
651 {
652 BN_print(bp,&a);
653 BIO_puts(bp," * ");
654 BN_print(bp,&b);
655 BIO_puts(bp," - ");
656 }
657 BN_print(bp,&c);
658 BIO_puts(bp,"\n");
659 }
660 BN_div(&d,&e,&c,&a,ctx);
661 BN_sub(&d,&d,&b);
662 if(!BN_is_zero(&d) || !BN_is_zero(&e))
663 {
664 fprintf(stderr,"Multiplication test failed!\n");
665 return 0;
666 }
667 }
668 BN_free(&a);
669 BN_free(&b);
670 BN_free(&c);
671 BN_free(&d);
672 BN_free(&e);
673 BN_CTX_free(ctx);
674 return(1);
675 }
676
677int test_sqr(BIO *bp, BN_CTX *ctx)
678 {
679 BIGNUM a,c,d,e;
680 int i;
681
682 BN_init(&a);
683 BN_init(&c);
684 BN_init(&d);
685 BN_init(&e);
686
687 for (i=0; i<num0; i++)
688 {
689 BN_bntest_rand(&a,40+i*10,0,0);
690 a.neg=rand_neg();
691 BN_sqr(&c,&a,ctx);
692 if (bp != NULL)
693 {
694 if (!results)
695 {
696 BN_print(bp,&a);
697 BIO_puts(bp," * ");
698 BN_print(bp,&a);
699 BIO_puts(bp," - ");
700 }
701 BN_print(bp,&c);
702 BIO_puts(bp,"\n");
703 }
704 BN_div(&d,&e,&c,&a,ctx);
705 BN_sub(&d,&d,&a);
706 if(!BN_is_zero(&d) || !BN_is_zero(&e))
707 {
708 fprintf(stderr,"Square test failed!\n");
709 return 0;
710 }
711 }
712 BN_free(&a);
713 BN_free(&c);
714 BN_free(&d);
715 BN_free(&e);
716 return(1);
717 }
718
719int test_mont(BIO *bp, BN_CTX *ctx)
720 {
721 BIGNUM a,b,c,d,A,B;
722 BIGNUM n;
723 int i;
724 BN_MONT_CTX *mont;
725
726 BN_init(&a);
727 BN_init(&b);
728 BN_init(&c);
729 BN_init(&d);
730 BN_init(&A);
731 BN_init(&B);
732 BN_init(&n);
733
734 mont=BN_MONT_CTX_new();
735 if (mont == NULL)
736 return 0;
737
738 BN_bntest_rand(&a,100,0,0); /**/
739 BN_bntest_rand(&b,100,0,0); /**/
740 for (i=0; i<num2; i++)
741 {
742 int bits = (200*(i+1))/num2;
743
744 if (bits == 0)
745 continue;
746 BN_bntest_rand(&n,bits,0,1);
747 BN_MONT_CTX_set(mont,&n,ctx);
748
749 BN_nnmod(&a,&a,&n,ctx);
750 BN_nnmod(&b,&b,&n,ctx);
751
752 BN_to_montgomery(&A,&a,mont,ctx);
753 BN_to_montgomery(&B,&b,mont,ctx);
754
755 BN_mod_mul_montgomery(&c,&A,&B,mont,ctx);/**/
756 BN_from_montgomery(&A,&c,mont,ctx);/**/
757 if (bp != NULL)
758 {
759 if (!results)
760 {
761#ifdef undef
762fprintf(stderr,"%d * %d %% %d\n",
763BN_num_bits(&a),
764BN_num_bits(&b),
765BN_num_bits(mont->N));
766#endif
767 BN_print(bp,&a);
768 BIO_puts(bp," * ");
769 BN_print(bp,&b);
770 BIO_puts(bp," % ");
771 BN_print(bp,&(mont->N));
772 BIO_puts(bp," - ");
773 }
774 BN_print(bp,&A);
775 BIO_puts(bp,"\n");
776 }
777 BN_mod_mul(&d,&a,&b,&n,ctx);
778 BN_sub(&d,&d,&A);
779 if(!BN_is_zero(&d))
780 {
781 fprintf(stderr,"Montgomery multiplication test failed!\n");
782 return 0;
783 }
784 }
785 BN_MONT_CTX_free(mont);
786 BN_free(&a);
787 BN_free(&b);
788 BN_free(&c);
789 BN_free(&d);
790 BN_free(&A);
791 BN_free(&B);
792 BN_free(&n);
793 return(1);
794 }
795
796int test_mod(BIO *bp, BN_CTX *ctx)
797 {
798 BIGNUM *a,*b,*c,*d,*e;
799 int i;
800
801 a=BN_new();
802 b=BN_new();
803 c=BN_new();
804 d=BN_new();
805 e=BN_new();
806
807 BN_bntest_rand(a,1024,0,0); /**/
808 for (i=0; i<num0; i++)
809 {
810 BN_bntest_rand(b,450+i*10,0,0); /**/
811 a->neg=rand_neg();
812 b->neg=rand_neg();
813 BN_mod(c,a,b,ctx);/**/
814 if (bp != NULL)
815 {
816 if (!results)
817 {
818 BN_print(bp,a);
819 BIO_puts(bp," % ");
820 BN_print(bp,b);
821 BIO_puts(bp," - ");
822 }
823 BN_print(bp,c);
824 BIO_puts(bp,"\n");
825 }
826 BN_div(d,e,a,b,ctx);
827 BN_sub(e,e,c);
828 if(!BN_is_zero(e))
829 {
830 fprintf(stderr,"Modulo test failed!\n");
831 return 0;
832 }
833 }
834 BN_free(a);
835 BN_free(b);
836 BN_free(c);
837 BN_free(d);
838 BN_free(e);
839 return(1);
840 }
841
842int test_mod_mul(BIO *bp, BN_CTX *ctx)
843 {
844 BIGNUM *a,*b,*c,*d,*e;
845 int i,j;
846
847 a=BN_new();
848 b=BN_new();
849 c=BN_new();
850 d=BN_new();
851 e=BN_new();
852
853 for (j=0; j<3; j++) {
854 BN_bntest_rand(c,1024,0,0); /**/
855 for (i=0; i<num0; i++)
856 {
857 BN_bntest_rand(a,475+i*10,0,0); /**/
858 BN_bntest_rand(b,425+i*11,0,0); /**/
859 a->neg=rand_neg();
860 b->neg=rand_neg();
861 if (!BN_mod_mul(e,a,b,c,ctx))
862 {
863 unsigned long l;
864
865 while ((l=ERR_get_error()))
866 fprintf(stderr,"ERROR:%s\n",
867 ERR_error_string(l,NULL));
868 EXIT(1);
869 }
870 if (bp != NULL)
871 {
872 if (!results)
873 {
874 BN_print(bp,a);
875 BIO_puts(bp," * ");
876 BN_print(bp,b);
877 BIO_puts(bp," % ");
878 BN_print(bp,c);
879 if ((a->neg ^ b->neg) && !BN_is_zero(e))
880 {
881 /* If (a*b) % c is negative, c must be added
882 * in order to obtain the normalized remainder
883 * (new with OpenSSL 0.9.7, previous versions of
884 * BN_mod_mul could generate negative results)
885 */
886 BIO_puts(bp," + ");
887 BN_print(bp,c);
888 }
889 BIO_puts(bp," - ");
890 }
891 BN_print(bp,e);
892 BIO_puts(bp,"\n");
893 }
894 BN_mul(d,a,b,ctx);
895 BN_sub(d,d,e);
896 BN_div(a,b,d,c,ctx);
897 if(!BN_is_zero(b))
898 {
899 fprintf(stderr,"Modulo multiply test failed!\n");
900 ERR_print_errors_fp(stderr);
901 return 0;
902 }
903 }
904 }
905 BN_free(a);
906 BN_free(b);
907 BN_free(c);
908 BN_free(d);
909 BN_free(e);
910 return(1);
911 }
912
913int test_mod_exp(BIO *bp, BN_CTX *ctx)
914 {
915 BIGNUM *a,*b,*c,*d,*e;
916 int i;
917
918 a=BN_new();
919 b=BN_new();
920 c=BN_new();
921 d=BN_new();
922 e=BN_new();
923
924 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
925 for (i=0; i<num2; i++)
926 {
927 BN_bntest_rand(a,20+i*5,0,0); /**/
928 BN_bntest_rand(b,2+i,0,0); /**/
929
930 if (!BN_mod_exp(d,a,b,c,ctx))
931 return(0);
932
933 if (bp != NULL)
934 {
935 if (!results)
936 {
937 BN_print(bp,a);
938 BIO_puts(bp," ^ ");
939 BN_print(bp,b);
940 BIO_puts(bp," % ");
941 BN_print(bp,c);
942 BIO_puts(bp," - ");
943 }
944 BN_print(bp,d);
945 BIO_puts(bp,"\n");
946 }
947 BN_exp(e,a,b,ctx);
948 BN_sub(e,e,d);
949 BN_div(a,b,e,c,ctx);
950 if(!BN_is_zero(b))
951 {
952 fprintf(stderr,"Modulo exponentiation test failed!\n");
953 return 0;
954 }
955 }
956 BN_free(a);
957 BN_free(b);
958 BN_free(c);
959 BN_free(d);
960 BN_free(e);
961 return(1);
962 }
963
964int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
965 {
966 BIGNUM *a,*b,*c,*d,*e;
967 int i;
968
969 a=BN_new();
970 b=BN_new();
971 c=BN_new();
972 d=BN_new();
973 e=BN_new();
974
975 BN_bntest_rand(c,30,0,1); /* must be odd for montgomery */
976 for (i=0; i<num2; i++)
977 {
978 BN_bntest_rand(a,20+i*5,0,0); /**/
979 BN_bntest_rand(b,2+i,0,0); /**/
980
981 if (!BN_mod_exp_mont_consttime(d,a,b,c,ctx,NULL))
982 return(00);
983
984 if (bp != NULL)
985 {
986 if (!results)
987 {
988 BN_print(bp,a);
989 BIO_puts(bp," ^ ");
990 BN_print(bp,b);
991 BIO_puts(bp," % ");
992 BN_print(bp,c);
993 BIO_puts(bp," - ");
994 }
995 BN_print(bp,d);
996 BIO_puts(bp,"\n");
997 }
998 BN_exp(e,a,b,ctx);
999 BN_sub(e,e,d);
1000 BN_div(a,b,e,c,ctx);
1001 if(!BN_is_zero(b))
1002 {
1003 fprintf(stderr,"Modulo exponentiation test failed!\n");
1004 return 0;
1005 }
1006 }
1007 BN_free(a);
1008 BN_free(b);
1009 BN_free(c);
1010 BN_free(d);
1011 BN_free(e);
1012 return(1);
1013 }
1014
1015int test_exp(BIO *bp, BN_CTX *ctx)
1016 {
1017 BIGNUM *a,*b,*d,*e,*one;
1018 int i;
1019
1020 a=BN_new();
1021 b=BN_new();
1022 d=BN_new();
1023 e=BN_new();
1024 one=BN_new();
1025 BN_one(one);
1026
1027 for (i=0; i<num2; i++)
1028 {
1029 BN_bntest_rand(a,20+i*5,0,0); /**/
1030 BN_bntest_rand(b,2+i,0,0); /**/
1031
1032 if (BN_exp(d,a,b,ctx) <= 0)
1033 return(0);
1034
1035 if (bp != NULL)
1036 {
1037 if (!results)
1038 {
1039 BN_print(bp,a);
1040 BIO_puts(bp," ^ ");
1041 BN_print(bp,b);
1042 BIO_puts(bp," - ");
1043 }
1044 BN_print(bp,d);
1045 BIO_puts(bp,"\n");
1046 }
1047 BN_one(e);
1048 for( ; !BN_is_zero(b) ; BN_sub(b,b,one))
1049 BN_mul(e,e,a,ctx);
1050 BN_sub(e,e,d);
1051 if(!BN_is_zero(e))
1052 {
1053 fprintf(stderr,"Exponentiation test failed!\n");
1054 return 0;
1055 }
1056 }
1057 BN_free(a);
1058 BN_free(b);
1059 BN_free(d);
1060 BN_free(e);
1061 BN_free(one);
1062 return(1);
1063 }
1064#ifndef OPENSSL_NO_EC2M
1065int test_gf2m_add(BIO *bp)
1066 {
1067 BIGNUM a,b,c;
1068 int i, ret = 0;
1069
1070 BN_init(&a);
1071 BN_init(&b);
1072 BN_init(&c);
1073
1074 for (i=0; i<num0; i++)
1075 {
1076 BN_rand(&a,512,0,0);
1077 BN_copy(&b, BN_value_one());
1078 a.neg=rand_neg();
1079 b.neg=rand_neg();
1080 BN_GF2m_add(&c,&a,&b);
1081#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1082 if (bp != NULL)
1083 {
1084 if (!results)
1085 {
1086 BN_print(bp,&a);
1087 BIO_puts(bp," ^ ");
1088 BN_print(bp,&b);
1089 BIO_puts(bp," = ");
1090 }
1091 BN_print(bp,&c);
1092 BIO_puts(bp,"\n");
1093 }
1094#endif
1095 /* Test that two added values have the correct parity. */
1096 if((BN_is_odd(&a) && BN_is_odd(&c)) || (!BN_is_odd(&a) && !BN_is_odd(&c)))
1097 {
1098 fprintf(stderr,"GF(2^m) addition test (a) failed!\n");
1099 goto err;
1100 }
1101 BN_GF2m_add(&c,&c,&c);
1102 /* Test that c + c = 0. */
1103 if(!BN_is_zero(&c))
1104 {
1105 fprintf(stderr,"GF(2^m) addition test (b) failed!\n");
1106 goto err;
1107 }
1108 }
1109 ret = 1;
1110 err:
1111 BN_free(&a);
1112 BN_free(&b);
1113 BN_free(&c);
1114 return ret;
1115 }
1116
1117int test_gf2m_mod(BIO *bp)
1118 {
1119 BIGNUM *a,*b[2],*c,*d,*e;
1120 int i, j, ret = 0;
1121 int p0[] = {163,7,6,3,0,-1};
1122 int p1[] = {193,15,0,-1};
1123
1124 a=BN_new();
1125 b[0]=BN_new();
1126 b[1]=BN_new();
1127 c=BN_new();
1128 d=BN_new();
1129 e=BN_new();
1130
1131 BN_GF2m_arr2poly(p0, b[0]);
1132 BN_GF2m_arr2poly(p1, b[1]);
1133
1134 for (i=0; i<num0; i++)
1135 {
1136 BN_bntest_rand(a, 1024, 0, 0);
1137 for (j=0; j < 2; j++)
1138 {
1139 BN_GF2m_mod(c, a, b[j]);
1140#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1141 if (bp != NULL)
1142 {
1143 if (!results)
1144 {
1145 BN_print(bp,a);
1146 BIO_puts(bp," % ");
1147 BN_print(bp,b[j]);
1148 BIO_puts(bp," - ");
1149 BN_print(bp,c);
1150 BIO_puts(bp,"\n");
1151 }
1152 }
1153#endif
1154 BN_GF2m_add(d, a, c);
1155 BN_GF2m_mod(e, d, b[j]);
1156 /* Test that a + (a mod p) mod p == 0. */
1157 if(!BN_is_zero(e))
1158 {
1159 fprintf(stderr,"GF(2^m) modulo test failed!\n");
1160 goto err;
1161 }
1162 }
1163 }
1164 ret = 1;
1165 err:
1166 BN_free(a);
1167 BN_free(b[0]);
1168 BN_free(b[1]);
1169 BN_free(c);
1170 BN_free(d);
1171 BN_free(e);
1172 return ret;
1173 }
1174
1175int test_gf2m_mod_mul(BIO *bp,BN_CTX *ctx)
1176 {
1177 BIGNUM *a,*b[2],*c,*d,*e,*f,*g,*h;
1178 int i, j, ret = 0;
1179 int p0[] = {163,7,6,3,0,-1};
1180 int p1[] = {193,15,0,-1};
1181
1182 a=BN_new();
1183 b[0]=BN_new();
1184 b[1]=BN_new();
1185 c=BN_new();
1186 d=BN_new();
1187 e=BN_new();
1188 f=BN_new();
1189 g=BN_new();
1190 h=BN_new();
1191
1192 BN_GF2m_arr2poly(p0, b[0]);
1193 BN_GF2m_arr2poly(p1, b[1]);
1194
1195 for (i=0; i<num0; i++)
1196 {
1197 BN_bntest_rand(a, 1024, 0, 0);
1198 BN_bntest_rand(c, 1024, 0, 0);
1199 BN_bntest_rand(d, 1024, 0, 0);
1200 for (j=0; j < 2; j++)
1201 {
1202 BN_GF2m_mod_mul(e, a, c, b[j], ctx);
1203#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1204 if (bp != NULL)
1205 {
1206 if (!results)
1207 {
1208 BN_print(bp,a);
1209 BIO_puts(bp," * ");
1210 BN_print(bp,c);
1211 BIO_puts(bp," % ");
1212 BN_print(bp,b[j]);
1213 BIO_puts(bp," - ");
1214 BN_print(bp,e);
1215 BIO_puts(bp,"\n");
1216 }
1217 }
1218#endif
1219 BN_GF2m_add(f, a, d);
1220 BN_GF2m_mod_mul(g, f, c, b[j], ctx);
1221 BN_GF2m_mod_mul(h, d, c, b[j], ctx);
1222 BN_GF2m_add(f, e, g);
1223 BN_GF2m_add(f, f, h);
1224 /* Test that (a+d)*c = a*c + d*c. */
1225 if(!BN_is_zero(f))
1226 {
1227 fprintf(stderr,"GF(2^m) modular multiplication test failed!\n");
1228 goto err;
1229 }
1230 }
1231 }
1232 ret = 1;
1233 err:
1234 BN_free(a);
1235 BN_free(b[0]);
1236 BN_free(b[1]);
1237 BN_free(c);
1238 BN_free(d);
1239 BN_free(e);
1240 BN_free(f);
1241 BN_free(g);
1242 BN_free(h);
1243 return ret;
1244 }
1245
1246int test_gf2m_mod_sqr(BIO *bp,BN_CTX *ctx)
1247 {
1248 BIGNUM *a,*b[2],*c,*d;
1249 int i, j, ret = 0;
1250 int p0[] = {163,7,6,3,0,-1};
1251 int p1[] = {193,15,0,-1};
1252
1253 a=BN_new();
1254 b[0]=BN_new();
1255 b[1]=BN_new();
1256 c=BN_new();
1257 d=BN_new();
1258
1259 BN_GF2m_arr2poly(p0, b[0]);
1260 BN_GF2m_arr2poly(p1, b[1]);
1261
1262 for (i=0; i<num0; i++)
1263 {
1264 BN_bntest_rand(a, 1024, 0, 0);
1265 for (j=0; j < 2; j++)
1266 {
1267 BN_GF2m_mod_sqr(c, a, b[j], ctx);
1268 BN_copy(d, a);
1269 BN_GF2m_mod_mul(d, a, d, b[j], ctx);
1270#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1271 if (bp != NULL)
1272 {
1273 if (!results)
1274 {
1275 BN_print(bp,a);
1276 BIO_puts(bp," ^ 2 % ");
1277 BN_print(bp,b[j]);
1278 BIO_puts(bp, " = ");
1279 BN_print(bp,c);
1280 BIO_puts(bp,"; a * a = ");
1281 BN_print(bp,d);
1282 BIO_puts(bp,"\n");
1283 }
1284 }
1285#endif
1286 BN_GF2m_add(d, c, d);
1287 /* Test that a*a = a^2. */
1288 if(!BN_is_zero(d))
1289 {
1290 fprintf(stderr,"GF(2^m) modular squaring test failed!\n");
1291 goto err;
1292 }
1293 }
1294 }
1295 ret = 1;
1296 err:
1297 BN_free(a);
1298 BN_free(b[0]);
1299 BN_free(b[1]);
1300 BN_free(c);
1301 BN_free(d);
1302 return ret;
1303 }
1304
1305int test_gf2m_mod_inv(BIO *bp,BN_CTX *ctx)
1306 {
1307 BIGNUM *a,*b[2],*c,*d;
1308 int i, j, ret = 0;
1309 int p0[] = {163,7,6,3,0,-1};
1310 int p1[] = {193,15,0,-1};
1311
1312 a=BN_new();
1313 b[0]=BN_new();
1314 b[1]=BN_new();
1315 c=BN_new();
1316 d=BN_new();
1317
1318 BN_GF2m_arr2poly(p0, b[0]);
1319 BN_GF2m_arr2poly(p1, b[1]);
1320
1321 for (i=0; i<num0; i++)
1322 {
1323 BN_bntest_rand(a, 512, 0, 0);
1324 for (j=0; j < 2; j++)
1325 {
1326 BN_GF2m_mod_inv(c, a, b[j], ctx);
1327 BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1328#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1329 if (bp != NULL)
1330 {
1331 if (!results)
1332 {
1333 BN_print(bp,a);
1334 BIO_puts(bp, " * ");
1335 BN_print(bp,c);
1336 BIO_puts(bp," - 1 % ");
1337 BN_print(bp,b[j]);
1338 BIO_puts(bp,"\n");
1339 }
1340 }
1341#endif
1342 /* Test that ((1/a)*a) = 1. */
1343 if(!BN_is_one(d))
1344 {
1345 fprintf(stderr,"GF(2^m) modular inversion test failed!\n");
1346 goto err;
1347 }
1348 }
1349 }
1350 ret = 1;
1351 err:
1352 BN_free(a);
1353 BN_free(b[0]);
1354 BN_free(b[1]);
1355 BN_free(c);
1356 BN_free(d);
1357 return ret;
1358 }
1359
1360int test_gf2m_mod_div(BIO *bp,BN_CTX *ctx)
1361 {
1362 BIGNUM *a,*b[2],*c,*d,*e,*f;
1363 int i, j, ret = 0;
1364 int p0[] = {163,7,6,3,0,-1};
1365 int p1[] = {193,15,0,-1};
1366
1367 a=BN_new();
1368 b[0]=BN_new();
1369 b[1]=BN_new();
1370 c=BN_new();
1371 d=BN_new();
1372 e=BN_new();
1373 f=BN_new();
1374
1375 BN_GF2m_arr2poly(p0, b[0]);
1376 BN_GF2m_arr2poly(p1, b[1]);
1377
1378 for (i=0; i<num0; i++)
1379 {
1380 BN_bntest_rand(a, 512, 0, 0);
1381 BN_bntest_rand(c, 512, 0, 0);
1382 for (j=0; j < 2; j++)
1383 {
1384 BN_GF2m_mod_div(d, a, c, b[j], ctx);
1385 BN_GF2m_mod_mul(e, d, c, b[j], ctx);
1386 BN_GF2m_mod_div(f, a, e, b[j], ctx);
1387#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1388 if (bp != NULL)
1389 {
1390 if (!results)
1391 {
1392 BN_print(bp,a);
1393 BIO_puts(bp, " = ");
1394 BN_print(bp,c);
1395 BIO_puts(bp," * ");
1396 BN_print(bp,d);
1397 BIO_puts(bp, " % ");
1398 BN_print(bp,b[j]);
1399 BIO_puts(bp,"\n");
1400 }
1401 }
1402#endif
1403 /* Test that ((a/c)*c)/a = 1. */
1404 if(!BN_is_one(f))
1405 {
1406 fprintf(stderr,"GF(2^m) modular division test failed!\n");
1407 goto err;
1408 }
1409 }
1410 }
1411 ret = 1;
1412 err:
1413 BN_free(a);
1414 BN_free(b[0]);
1415 BN_free(b[1]);
1416 BN_free(c);
1417 BN_free(d);
1418 BN_free(e);
1419 BN_free(f);
1420 return ret;
1421 }
1422
1423int test_gf2m_mod_exp(BIO *bp,BN_CTX *ctx)
1424 {
1425 BIGNUM *a,*b[2],*c,*d,*e,*f;
1426 int i, j, ret = 0;
1427 int p0[] = {163,7,6,3,0,-1};
1428 int p1[] = {193,15,0,-1};
1429
1430 a=BN_new();
1431 b[0]=BN_new();
1432 b[1]=BN_new();
1433 c=BN_new();
1434 d=BN_new();
1435 e=BN_new();
1436 f=BN_new();
1437
1438 BN_GF2m_arr2poly(p0, b[0]);
1439 BN_GF2m_arr2poly(p1, b[1]);
1440
1441 for (i=0; i<num0; i++)
1442 {
1443 BN_bntest_rand(a, 512, 0, 0);
1444 BN_bntest_rand(c, 512, 0, 0);
1445 BN_bntest_rand(d, 512, 0, 0);
1446 for (j=0; j < 2; j++)
1447 {
1448 BN_GF2m_mod_exp(e, a, c, b[j], ctx);
1449 BN_GF2m_mod_exp(f, a, d, b[j], ctx);
1450 BN_GF2m_mod_mul(e, e, f, b[j], ctx);
1451 BN_add(f, c, d);
1452 BN_GF2m_mod_exp(f, a, f, b[j], ctx);
1453#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1454 if (bp != NULL)
1455 {
1456 if (!results)
1457 {
1458 BN_print(bp,a);
1459 BIO_puts(bp, " ^ (");
1460 BN_print(bp,c);
1461 BIO_puts(bp," + ");
1462 BN_print(bp,d);
1463 BIO_puts(bp, ") = ");
1464 BN_print(bp,e);
1465 BIO_puts(bp, "; - ");
1466 BN_print(bp,f);
1467 BIO_puts(bp, " % ");
1468 BN_print(bp,b[j]);
1469 BIO_puts(bp,"\n");
1470 }
1471 }
1472#endif
1473 BN_GF2m_add(f, e, f);
1474 /* Test that a^(c+d)=a^c*a^d. */
1475 if(!BN_is_zero(f))
1476 {
1477 fprintf(stderr,"GF(2^m) modular exponentiation test failed!\n");
1478 goto err;
1479 }
1480 }
1481 }
1482 ret = 1;
1483 err:
1484 BN_free(a);
1485 BN_free(b[0]);
1486 BN_free(b[1]);
1487 BN_free(c);
1488 BN_free(d);
1489 BN_free(e);
1490 BN_free(f);
1491 return ret;
1492 }
1493
1494int test_gf2m_mod_sqrt(BIO *bp,BN_CTX *ctx)
1495 {
1496 BIGNUM *a,*b[2],*c,*d,*e,*f;
1497 int i, j, ret = 0;
1498 int p0[] = {163,7,6,3,0,-1};
1499 int p1[] = {193,15,0,-1};
1500
1501 a=BN_new();
1502 b[0]=BN_new();
1503 b[1]=BN_new();
1504 c=BN_new();
1505 d=BN_new();
1506 e=BN_new();
1507 f=BN_new();
1508
1509 BN_GF2m_arr2poly(p0, b[0]);
1510 BN_GF2m_arr2poly(p1, b[1]);
1511
1512 for (i=0; i<num0; i++)
1513 {
1514 BN_bntest_rand(a, 512, 0, 0);
1515 for (j=0; j < 2; j++)
1516 {
1517 BN_GF2m_mod(c, a, b[j]);
1518 BN_GF2m_mod_sqrt(d, a, b[j], ctx);
1519 BN_GF2m_mod_sqr(e, d, b[j], ctx);
1520#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1521 if (bp != NULL)
1522 {
1523 if (!results)
1524 {
1525 BN_print(bp,d);
1526 BIO_puts(bp, " ^ 2 - ");
1527 BN_print(bp,a);
1528 BIO_puts(bp,"\n");
1529 }
1530 }
1531#endif
1532 BN_GF2m_add(f, c, e);
1533 /* Test that d^2 = a, where d = sqrt(a). */
1534 if(!BN_is_zero(f))
1535 {
1536 fprintf(stderr,"GF(2^m) modular square root test failed!\n");
1537 goto err;
1538 }
1539 }
1540 }
1541 ret = 1;
1542 err:
1543 BN_free(a);
1544 BN_free(b[0]);
1545 BN_free(b[1]);
1546 BN_free(c);
1547 BN_free(d);
1548 BN_free(e);
1549 BN_free(f);
1550 return ret;
1551 }
1552
1553int test_gf2m_mod_solve_quad(BIO *bp,BN_CTX *ctx)
1554 {
1555 BIGNUM *a,*b[2],*c,*d,*e;
1556 int i, j, s = 0, t, ret = 0;
1557 int p0[] = {163,7,6,3,0,-1};
1558 int p1[] = {193,15,0,-1};
1559
1560 a=BN_new();
1561 b[0]=BN_new();
1562 b[1]=BN_new();
1563 c=BN_new();
1564 d=BN_new();
1565 e=BN_new();
1566
1567 BN_GF2m_arr2poly(p0, b[0]);
1568 BN_GF2m_arr2poly(p1, b[1]);
1569
1570 for (i=0; i<num0; i++)
1571 {
1572 BN_bntest_rand(a, 512, 0, 0);
1573 for (j=0; j < 2; j++)
1574 {
1575 t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
1576 if (t)
1577 {
1578 s++;
1579 BN_GF2m_mod_sqr(d, c, b[j], ctx);
1580 BN_GF2m_add(d, c, d);
1581 BN_GF2m_mod(e, a, b[j]);
1582#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1583 if (bp != NULL)
1584 {
1585 if (!results)
1586 {
1587 BN_print(bp,c);
1588 BIO_puts(bp, " is root of z^2 + z = ");
1589 BN_print(bp,a);
1590 BIO_puts(bp, " % ");
1591 BN_print(bp,b[j]);
1592 BIO_puts(bp, "\n");
1593 }
1594 }
1595#endif
1596 BN_GF2m_add(e, e, d);
1597 /* Test that solution of quadratic c satisfies c^2 + c = a. */
1598 if(!BN_is_zero(e))
1599 {
1600 fprintf(stderr,"GF(2^m) modular solve quadratic test failed!\n");
1601 goto err;
1602 }
1603
1604 }
1605 else
1606 {
1607#if 0 /* make test uses ouput in bc but bc can't handle GF(2^m) arithmetic */
1608 if (bp != NULL)
1609 {
1610 if (!results)
1611 {
1612 BIO_puts(bp, "There are no roots of z^2 + z = ");
1613 BN_print(bp,a);
1614 BIO_puts(bp, " % ");
1615 BN_print(bp,b[j]);
1616 BIO_puts(bp, "\n");
1617 }
1618 }
1619#endif
1620 }
1621 }
1622 }
1623 if (s == 0)
1624 {
1625 fprintf(stderr,"All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n", num0);
1626 fprintf(stderr,"this is very unlikely and probably indicates an error.\n");
1627 goto err;
1628 }
1629 ret = 1;
1630 err:
1631 BN_free(a);
1632 BN_free(b[0]);
1633 BN_free(b[1]);
1634 BN_free(c);
1635 BN_free(d);
1636 BN_free(e);
1637 return ret;
1638 }
1639#endif
1640static int genprime_cb(int p, int n, BN_GENCB *arg)
1641 {
1642 char c='*';
1643
1644 if (p == 0) c='.';
1645 if (p == 1) c='+';
1646 if (p == 2) c='*';
1647 if (p == 3) c='\n';
1648 putc(c, stderr);
1649 fflush(stderr);
1650 return 1;
1651 }
1652
1653int test_kron(BIO *bp, BN_CTX *ctx)
1654 {
1655 BN_GENCB cb;
1656 BIGNUM *a,*b,*r,*t;
1657 int i;
1658 int legendre, kronecker;
1659 int ret = 0;
1660
1661 a = BN_new();
1662 b = BN_new();
1663 r = BN_new();
1664 t = BN_new();
1665 if (a == NULL || b == NULL || r == NULL || t == NULL) goto err;
1666
1667 BN_GENCB_set(&cb, genprime_cb, NULL);
1668
1669 /* We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol).
1670 * In this case we know that if b is prime, then BN_kronecker(a, b, ctx)
1671 * is congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol).
1672 * So we generate a random prime b and compare these values
1673 * for a number of random a's. (That is, we run the Solovay-Strassen
1674 * primality test to confirm that b is prime, except that we
1675 * don't want to test whether b is prime but whether BN_kronecker
1676 * works.) */
1677
1678 if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb)) goto err;
1679 b->neg = rand_neg();
1680 putc('\n', stderr);
1681
1682 for (i = 0; i < num0; i++)
1683 {
1684 if (!BN_bntest_rand(a, 512, 0, 0)) goto err;
1685 a->neg = rand_neg();
1686
1687 /* t := (|b|-1)/2 (note that b is odd) */
1688 if (!BN_copy(t, b)) goto err;
1689 t->neg = 0;
1690 if (!BN_sub_word(t, 1)) goto err;
1691 if (!BN_rshift1(t, t)) goto err;
1692 /* r := a^t mod b */
1693 b->neg=0;
1694
1695 if (!BN_mod_exp_recp(r, a, t, b, ctx)) goto err;
1696 b->neg=1;
1697
1698 if (BN_is_word(r, 1))
1699 legendre = 1;
1700 else if (BN_is_zero(r))
1701 legendre = 0;
1702 else
1703 {
1704 if (!BN_add_word(r, 1)) goto err;
1705 if (0 != BN_ucmp(r, b))
1706 {
1707 fprintf(stderr, "Legendre symbol computation failed\n");
1708 goto err;
1709 }
1710 legendre = -1;
1711 }
1712
1713 kronecker = BN_kronecker(a, b, ctx);
1714 if (kronecker < -1) goto err;
1715 /* we actually need BN_kronecker(a, |b|) */
1716 if (a->neg && b->neg)
1717 kronecker = -kronecker;
1718
1719 if (legendre != kronecker)
1720 {
1721 fprintf(stderr, "legendre != kronecker; a = ");
1722 BN_print_fp(stderr, a);
1723 fprintf(stderr, ", b = ");
1724 BN_print_fp(stderr, b);
1725 fprintf(stderr, "\n");
1726 goto err;
1727 }
1728
1729 putc('.', stderr);
1730 fflush(stderr);
1731 }
1732
1733 putc('\n', stderr);
1734 fflush(stderr);
1735 ret = 1;
1736 err:
1737 if (a != NULL) BN_free(a);
1738 if (b != NULL) BN_free(b);
1739 if (r != NULL) BN_free(r);
1740 if (t != NULL) BN_free(t);
1741 return ret;
1742 }
1743
1744int test_sqrt(BIO *bp, BN_CTX *ctx)
1745 {
1746 BN_GENCB cb;
1747 BIGNUM *a,*p,*r;
1748 int i, j;
1749 int ret = 0;
1750
1751 a = BN_new();
1752 p = BN_new();
1753 r = BN_new();
1754 if (a == NULL || p == NULL || r == NULL) goto err;
1755
1756 BN_GENCB_set(&cb, genprime_cb, NULL);
1757
1758 for (i = 0; i < 16; i++)
1759 {
1760 if (i < 8)
1761 {
1762 unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
1763
1764 if (!BN_set_word(p, primes[i])) goto err;
1765 }
1766 else
1767 {
1768 if (!BN_set_word(a, 32)) goto err;
1769 if (!BN_set_word(r, 2*i + 1)) goto err;
1770
1771 if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb)) goto err;
1772 putc('\n', stderr);
1773 }
1774 p->neg = rand_neg();
1775
1776 for (j = 0; j < num2; j++)
1777 {
1778 /* construct 'a' such that it is a square modulo p,
1779 * but in general not a proper square and not reduced modulo p */
1780 if (!BN_bntest_rand(r, 256, 0, 3)) goto err;
1781 if (!BN_nnmod(r, r, p, ctx)) goto err;
1782 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1783 if (!BN_bntest_rand(a, 256, 0, 3)) goto err;
1784 if (!BN_nnmod(a, a, p, ctx)) goto err;
1785 if (!BN_mod_sqr(a, a, p, ctx)) goto err;
1786 if (!BN_mul(a, a, r, ctx)) goto err;
1787 if (rand_neg())
1788 if (!BN_sub(a, a, p)) goto err;
1789
1790 if (!BN_mod_sqrt(r, a, p, ctx)) goto err;
1791 if (!BN_mod_sqr(r, r, p, ctx)) goto err;
1792
1793 if (!BN_nnmod(a, a, p, ctx)) goto err;
1794
1795 if (BN_cmp(a, r) != 0)
1796 {
1797 fprintf(stderr, "BN_mod_sqrt failed: a = ");
1798 BN_print_fp(stderr, a);
1799 fprintf(stderr, ", r = ");
1800 BN_print_fp(stderr, r);
1801 fprintf(stderr, ", p = ");
1802 BN_print_fp(stderr, p);
1803 fprintf(stderr, "\n");
1804 goto err;
1805 }
1806
1807 putc('.', stderr);
1808 fflush(stderr);
1809 }
1810
1811 putc('\n', stderr);
1812 fflush(stderr);
1813 }
1814 ret = 1;
1815 err:
1816 if (a != NULL) BN_free(a);
1817 if (p != NULL) BN_free(p);
1818 if (r != NULL) BN_free(r);
1819 return ret;
1820 }
1821
1822int test_lshift(BIO *bp,BN_CTX *ctx,BIGNUM *a_)
1823 {
1824 BIGNUM *a,*b,*c,*d;
1825 int i;
1826
1827 b=BN_new();
1828 c=BN_new();
1829 d=BN_new();
1830 BN_one(c);
1831
1832 if(a_)
1833 a=a_;
1834 else
1835 {
1836 a=BN_new();
1837 BN_bntest_rand(a,200,0,0); /**/
1838 a->neg=rand_neg();
1839 }
1840 for (i=0; i<num0; i++)
1841 {
1842 BN_lshift(b,a,i+1);
1843 BN_add(c,c,c);
1844 if (bp != NULL)
1845 {
1846 if (!results)
1847 {
1848 BN_print(bp,a);
1849 BIO_puts(bp," * ");
1850 BN_print(bp,c);
1851 BIO_puts(bp," - ");
1852 }
1853 BN_print(bp,b);
1854 BIO_puts(bp,"\n");
1855 }
1856 BN_mul(d,a,c,ctx);
1857 BN_sub(d,d,b);
1858 if(!BN_is_zero(d))
1859 {
1860 fprintf(stderr,"Left shift test failed!\n");
1861 fprintf(stderr,"a=");
1862 BN_print_fp(stderr,a);
1863 fprintf(stderr,"\nb=");
1864 BN_print_fp(stderr,b);
1865 fprintf(stderr,"\nc=");
1866 BN_print_fp(stderr,c);
1867 fprintf(stderr,"\nd=");
1868 BN_print_fp(stderr,d);
1869 fprintf(stderr,"\n");
1870 return 0;
1871 }
1872 }
1873 BN_free(a);
1874 BN_free(b);
1875 BN_free(c);
1876 BN_free(d);
1877 return(1);
1878 }
1879
1880int test_lshift1(BIO *bp)
1881 {
1882 BIGNUM *a,*b,*c;
1883 int i;
1884
1885 a=BN_new();
1886 b=BN_new();
1887 c=BN_new();
1888
1889 BN_bntest_rand(a,200,0,0); /**/
1890 a->neg=rand_neg();
1891 for (i=0; i<num0; i++)
1892 {
1893 BN_lshift1(b,a);
1894 if (bp != NULL)
1895 {
1896 if (!results)
1897 {
1898 BN_print(bp,a);
1899 BIO_puts(bp," * 2");
1900 BIO_puts(bp," - ");
1901 }
1902 BN_print(bp,b);
1903 BIO_puts(bp,"\n");
1904 }
1905 BN_add(c,a,a);
1906 BN_sub(a,b,c);
1907 if(!BN_is_zero(a))
1908 {
1909 fprintf(stderr,"Left shift one test failed!\n");
1910 return 0;
1911 }
1912
1913 BN_copy(a,b);
1914 }
1915 BN_free(a);
1916 BN_free(b);
1917 BN_free(c);
1918 return(1);
1919 }
1920
1921int test_rshift(BIO *bp,BN_CTX *ctx)
1922 {
1923 BIGNUM *a,*b,*c,*d,*e;
1924 int i;
1925
1926 a=BN_new();
1927 b=BN_new();
1928 c=BN_new();
1929 d=BN_new();
1930 e=BN_new();
1931 BN_one(c);
1932
1933 BN_bntest_rand(a,200,0,0); /**/
1934 a->neg=rand_neg();
1935 for (i=0; i<num0; i++)
1936 {
1937 BN_rshift(b,a,i+1);
1938 BN_add(c,c,c);
1939 if (bp != NULL)
1940 {
1941 if (!results)
1942 {
1943 BN_print(bp,a);
1944 BIO_puts(bp," / ");
1945 BN_print(bp,c);
1946 BIO_puts(bp," - ");
1947 }
1948 BN_print(bp,b);
1949 BIO_puts(bp,"\n");
1950 }
1951 BN_div(d,e,a,c,ctx);
1952 BN_sub(d,d,b);
1953 if(!BN_is_zero(d))
1954 {
1955 fprintf(stderr,"Right shift test failed!\n");
1956 return 0;
1957 }
1958 }
1959 BN_free(a);
1960 BN_free(b);
1961 BN_free(c);
1962 BN_free(d);
1963 BN_free(e);
1964 return(1);
1965 }
1966
1967int test_rshift1(BIO *bp)
1968 {
1969 BIGNUM *a,*b,*c;
1970 int i;
1971
1972 a=BN_new();
1973 b=BN_new();
1974 c=BN_new();
1975
1976 BN_bntest_rand(a,200,0,0); /**/
1977 a->neg=rand_neg();
1978 for (i=0; i<num0; i++)
1979 {
1980 BN_rshift1(b,a);
1981 if (bp != NULL)
1982 {
1983 if (!results)
1984 {
1985 BN_print(bp,a);
1986 BIO_puts(bp," / 2");
1987 BIO_puts(bp," - ");
1988 }
1989 BN_print(bp,b);
1990 BIO_puts(bp,"\n");
1991 }
1992 BN_sub(c,a,b);
1993 BN_sub(c,c,b);
1994 if(!BN_is_zero(c) && !BN_abs_is_word(c, 1))
1995 {
1996 fprintf(stderr,"Right shift one test failed!\n");
1997 return 0;
1998 }
1999 BN_copy(a,b);
2000 }
2001 BN_free(a);
2002 BN_free(b);
2003 BN_free(c);
2004 return(1);
2005 }
2006
2007int rand_neg(void)
2008 {
2009 static unsigned int neg=0;
2010 static int sign[8]={0,0,0,1,1,0,1,1};
2011
2012 return(sign[(neg++)%8]);
2013 }
diff --git a/src/lib/libssl/src/crypto/bn/exptest.c b/src/lib/libssl/src/crypto/bn/exptest.c
deleted file mode 100644
index 074a8e882a..0000000000
--- a/src/lib/libssl/src/crypto/bn/exptest.c
+++ /dev/null
@@ -1,204 +0,0 @@
1/* crypto/bn/exptest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#include <openssl/bio.h>
66#include <openssl/bn.h>
67#include <openssl/rand.h>
68#include <openssl/err.h>
69
70#define NUM_BITS (BN_BITS*2)
71
72static const char rnd_seed[] = "string to make the random number generator think it has entropy";
73
74int main(int argc, char *argv[])
75 {
76 BN_CTX *ctx;
77 BIO *out=NULL;
78 int i,ret;
79 unsigned char c;
80 BIGNUM *r_mont,*r_mont_const,*r_recp,*r_simple,*a,*b,*m;
81
82 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_rand may fail, and we don't
83 * even check its return value
84 * (which we should) */
85
86 ERR_load_BN_strings();
87
88 ctx=BN_CTX_new();
89 if (ctx == NULL) EXIT(1);
90 r_mont=BN_new();
91 r_mont_const=BN_new();
92 r_recp=BN_new();
93 r_simple=BN_new();
94 a=BN_new();
95 b=BN_new();
96 m=BN_new();
97 if ( (r_mont == NULL) || (r_recp == NULL) ||
98 (a == NULL) || (b == NULL))
99 goto err;
100
101 out=BIO_new(BIO_s_file());
102
103 if (out == NULL) EXIT(1);
104 BIO_set_fp(out,stdout,BIO_NOCLOSE);
105
106 for (i=0; i<200; i++)
107 {
108 RAND_bytes(&c,1);
109 c=(c%BN_BITS)-BN_BITS2;
110 BN_rand(a,NUM_BITS+c,0,0);
111
112 RAND_bytes(&c,1);
113 c=(c%BN_BITS)-BN_BITS2;
114 BN_rand(b,NUM_BITS+c,0,0);
115
116 RAND_bytes(&c,1);
117 c=(c%BN_BITS)-BN_BITS2;
118 BN_rand(m,NUM_BITS+c,0,1);
119
120 BN_mod(a,a,m,ctx);
121 BN_mod(b,b,m,ctx);
122
123 ret=BN_mod_exp_mont(r_mont,a,b,m,ctx,NULL);
124 if (ret <= 0)
125 {
126 printf("BN_mod_exp_mont() problems\n");
127 ERR_print_errors(out);
128 EXIT(1);
129 }
130
131 ret=BN_mod_exp_recp(r_recp,a,b,m,ctx);
132 if (ret <= 0)
133 {
134 printf("BN_mod_exp_recp() problems\n");
135 ERR_print_errors(out);
136 EXIT(1);
137 }
138
139 ret=BN_mod_exp_simple(r_simple,a,b,m,ctx);
140 if (ret <= 0)
141 {
142 printf("BN_mod_exp_simple() problems\n");
143 ERR_print_errors(out);
144 EXIT(1);
145 }
146
147 ret=BN_mod_exp_mont_consttime(r_mont_const,a,b,m,ctx,NULL);
148 if (ret <= 0)
149 {
150 printf("BN_mod_exp_mont_consttime() problems\n");
151 ERR_print_errors(out);
152 EXIT(1);
153 }
154
155 if (BN_cmp(r_simple, r_mont) == 0
156 && BN_cmp(r_simple,r_recp) == 0
157 && BN_cmp(r_simple,r_mont_const) == 0)
158 {
159 printf(".");
160 fflush(stdout);
161 }
162 else
163 {
164 if (BN_cmp(r_simple,r_mont) != 0)
165 printf("\nsimple and mont results differ\n");
166 if (BN_cmp(r_simple,r_mont_const) != 0)
167 printf("\nsimple and mont const time results differ\n");
168 if (BN_cmp(r_simple,r_recp) != 0)
169 printf("\nsimple and recp results differ\n");
170
171 printf("a (%3d) = ",BN_num_bits(a)); BN_print(out,a);
172 printf("\nb (%3d) = ",BN_num_bits(b)); BN_print(out,b);
173 printf("\nm (%3d) = ",BN_num_bits(m)); BN_print(out,m);
174 printf("\nsimple ="); BN_print(out,r_simple);
175 printf("\nrecp ="); BN_print(out,r_recp);
176 printf("\nmont ="); BN_print(out,r_mont);
177 printf("\nmont_ct ="); BN_print(out,r_mont_const);
178 printf("\n");
179 EXIT(1);
180 }
181 }
182 BN_free(r_mont);
183 BN_free(r_mont_const);
184 BN_free(r_recp);
185 BN_free(r_simple);
186 BN_free(a);
187 BN_free(b);
188 BN_free(m);
189 BN_CTX_free(ctx);
190 ERR_remove_thread_state(NULL);
191 CRYPTO_mem_leaks(out);
192 BIO_free(out);
193 printf(" done\n");
194 EXIT(0);
195err:
196 ERR_load_crypto_strings();
197 ERR_print_errors(out);
198#ifdef OPENSSL_SYS_NETWARE
199 printf("ERROR\n");
200#endif
201 EXIT(1);
202 return(1);
203 }
204
diff --git a/src/lib/libssl/src/crypto/cast/casttest.c b/src/lib/libssl/src/crypto/cast/casttest.c
deleted file mode 100644
index 0d020d6975..0000000000
--- a/src/lib/libssl/src/crypto/cast/casttest.c
+++ /dev/null
@@ -1,233 +0,0 @@
1/* crypto/cast/casttest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */
63
64#include "../e_os.h"
65
66#ifdef OPENSSL_NO_CAST
67int main(int argc, char *argv[])
68{
69 printf("No CAST support\n");
70 return(0);
71}
72#else
73#include <openssl/cast.h>
74
75#define FULL_TEST
76
77static unsigned char k[16]={
78 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
79 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A
80 };
81
82static unsigned char in[8]={ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
83
84static int k_len[3]={16,10,5};
85static unsigned char c[3][8]={
86 {0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2},
87 {0xEB,0x6A,0x71,0x1A,0x2C,0x02,0x27,0x1B},
88 {0x7A,0xC8,0x16,0xD1,0x6E,0x9B,0x30,0x2E},
89 };
90static unsigned char out[80];
91
92static unsigned char in_a[16]={
93 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
94 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
95static unsigned char in_b[16]={
96 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
97 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
98
99static unsigned char c_a[16]={
100 0xEE,0xA9,0xD0,0xA2,0x49,0xFD,0x3B,0xA6,
101 0xB3,0x43,0x6F,0xB8,0x9D,0x6D,0xCA,0x92};
102static unsigned char c_b[16]={
103 0xB2,0xC9,0x5E,0xB0,0x0C,0x31,0xAD,0x71,
104 0x80,0xAC,0x05,0xB8,0xE8,0x3D,0x69,0x6E};
105
106#if 0
107char *text="Hello to all people out there";
108
109static unsigned char cfb_key[16]={
110 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
111 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
112 };
113static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
114static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
115#define CFB_TEST_SIZE 24
116static unsigned char plain[CFB_TEST_SIZE]=
117 {
118 0x4e,0x6f,0x77,0x20,0x69,0x73,
119 0x20,0x74,0x68,0x65,0x20,0x74,
120 0x69,0x6d,0x65,0x20,0x66,0x6f,
121 0x72,0x20,0x61,0x6c,0x6c,0x20
122 };
123static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
124 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
125 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
126 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
127
128/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
129 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
130 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
131 };
132#endif
133
134int main(int argc, char *argv[])
135 {
136#ifdef FULL_TEST
137 long l;
138 CAST_KEY key_b;
139#endif
140 int i,z,err=0;
141 CAST_KEY key;
142
143 for (z=0; z<3; z++)
144 {
145 CAST_set_key(&key,k_len[z],k);
146
147 CAST_ecb_encrypt(in,out,&key,CAST_ENCRYPT);
148 if (memcmp(out,&(c[z][0]),8) != 0)
149 {
150 printf("ecb cast error encrypting for keysize %d\n",k_len[z]*8);
151 printf("got :");
152 for (i=0; i<8; i++)
153 printf("%02X ",out[i]);
154 printf("\n");
155 printf("expected:");
156 for (i=0; i<8; i++)
157 printf("%02X ",c[z][i]);
158 err=20;
159 printf("\n");
160 }
161
162 CAST_ecb_encrypt(out,out,&key,CAST_DECRYPT);
163 if (memcmp(out,in,8) != 0)
164 {
165 printf("ecb cast error decrypting for keysize %d\n",k_len[z]*8);
166 printf("got :");
167 for (i=0; i<8; i++)
168 printf("%02X ",out[i]);
169 printf("\n");
170 printf("expected:");
171 for (i=0; i<8; i++)
172 printf("%02X ",in[i]);
173 printf("\n");
174 err=3;
175 }
176 }
177 if (err == 0)
178 printf("ecb cast5 ok\n");
179
180#ifdef FULL_TEST
181 {
182 unsigned char out_a[16],out_b[16];
183 static char *hex="0123456789ABCDEF";
184
185 printf("This test will take some time....");
186 fflush(stdout);
187 memcpy(out_a,in_a,sizeof(in_a));
188 memcpy(out_b,in_b,sizeof(in_b));
189 i=1;
190
191 for (l=0; l<1000000L; l++)
192 {
193 CAST_set_key(&key_b,16,out_b);
194 CAST_ecb_encrypt(&(out_a[0]),&(out_a[0]),&key_b,CAST_ENCRYPT);
195 CAST_ecb_encrypt(&(out_a[8]),&(out_a[8]),&key_b,CAST_ENCRYPT);
196 CAST_set_key(&key,16,out_a);
197 CAST_ecb_encrypt(&(out_b[0]),&(out_b[0]),&key,CAST_ENCRYPT);
198 CAST_ecb_encrypt(&(out_b[8]),&(out_b[8]),&key,CAST_ENCRYPT);
199 if ((l & 0xffff) == 0xffff)
200 {
201 printf("%c",hex[i&0x0f]);
202 fflush(stdout);
203 i++;
204 }
205 }
206
207 if ( (memcmp(out_a,c_a,sizeof(c_a)) != 0) ||
208 (memcmp(out_b,c_b,sizeof(c_b)) != 0))
209 {
210 printf("\n");
211 printf("Error\n");
212
213 printf("A out =");
214 for (i=0; i<16; i++) printf("%02X ",out_a[i]);
215 printf("\nactual=");
216 for (i=0; i<16; i++) printf("%02X ",c_a[i]);
217 printf("\n");
218
219 printf("B out =");
220 for (i=0; i<16; i++) printf("%02X ",out_b[i]);
221 printf("\nactual=");
222 for (i=0; i<16; i++) printf("%02X ",c_b[i]);
223 printf("\n");
224 }
225 else
226 printf(" ok\n");
227 }
228#endif
229
230 EXIT(err);
231 return(err);
232 }
233#endif
diff --git a/src/lib/libssl/src/crypto/des/destest.c b/src/lib/libssl/src/crypto/des/destest.c
deleted file mode 100644
index 64b92a34fe..0000000000
--- a/src/lib/libssl/src/crypto/des/destest.c
+++ /dev/null
@@ -1,952 +0,0 @@
1/* crypto/des/destest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61
62#include <openssl/e_os2.h>
63#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_WINDOWS)
64#ifndef OPENSSL_SYS_MSDOS
65#define OPENSSL_SYS_MSDOS
66#endif
67#endif
68
69#ifndef OPENSSL_SYS_MSDOS
70#if !defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VMS_DECC)
71#include OPENSSL_UNISTD
72#endif
73#else
74#include <io.h>
75#endif
76#include <string.h>
77
78#ifdef OPENSSL_NO_DES
79int main(int argc, char *argv[])
80{
81 printf("No DES support\n");
82 return(0);
83}
84#else
85#include <openssl/des.h>
86
87#define crypt(c,s) (DES_crypt((c),(s)))
88
89/* tisk tisk - the test keys don't all have odd parity :-( */
90/* test data */
91#define NUM_TESTS 34
92static unsigned char key_data[NUM_TESTS][8]={
93 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
94 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
95 {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
96 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
97 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
98 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
99 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
100 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
101 {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
102 {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
103 {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
104 {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
105 {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
106 {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
107 {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
108 {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
109 {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
110 {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
111 {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
112 {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
113 {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
114 {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
115 {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
116 {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
117 {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
118 {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
119 {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
120 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
121 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
122 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
123 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
124 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
125 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
126 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
127
128static unsigned char plain_data[NUM_TESTS][8]={
129 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
130 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
131 {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
132 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
133 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
134 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
135 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
136 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
137 {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
138 {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
139 {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
140 {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
141 {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
142 {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
143 {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
144 {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
145 {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
146 {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
147 {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
148 {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
149 {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
150 {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
151 {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
152 {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
153 {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
154 {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
155 {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
156 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
157 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
158 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
159 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
160 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
161 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
162 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
163
164static unsigned char cipher_data[NUM_TESTS][8]={
165 {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
166 {0x73,0x59,0xB2,0x16,0x3E,0x4E,0xDC,0x58},
167 {0x95,0x8E,0x6E,0x62,0x7A,0x05,0x55,0x7B},
168 {0xF4,0x03,0x79,0xAB,0x9E,0x0E,0xC5,0x33},
169 {0x17,0x66,0x8D,0xFC,0x72,0x92,0x53,0x2D},
170 {0x8A,0x5A,0xE1,0xF8,0x1A,0xB8,0xF2,0xDD},
171 {0x8C,0xA6,0x4D,0xE9,0xC1,0xB1,0x23,0xA7},
172 {0xED,0x39,0xD9,0x50,0xFA,0x74,0xBC,0xC4},
173 {0x69,0x0F,0x5B,0x0D,0x9A,0x26,0x93,0x9B},
174 {0x7A,0x38,0x9D,0x10,0x35,0x4B,0xD2,0x71},
175 {0x86,0x8E,0xBB,0x51,0xCA,0xB4,0x59,0x9A},
176 {0x71,0x78,0x87,0x6E,0x01,0xF1,0x9B,0x2A},
177 {0xAF,0x37,0xFB,0x42,0x1F,0x8C,0x40,0x95},
178 {0x86,0xA5,0x60,0xF1,0x0E,0xC6,0xD8,0x5B},
179 {0x0C,0xD3,0xDA,0x02,0x00,0x21,0xDC,0x09},
180 {0xEA,0x67,0x6B,0x2C,0xB7,0xDB,0x2B,0x7A},
181 {0xDF,0xD6,0x4A,0x81,0x5C,0xAF,0x1A,0x0F},
182 {0x5C,0x51,0x3C,0x9C,0x48,0x86,0xC0,0x88},
183 {0x0A,0x2A,0xEE,0xAE,0x3F,0xF4,0xAB,0x77},
184 {0xEF,0x1B,0xF0,0x3E,0x5D,0xFA,0x57,0x5A},
185 {0x88,0xBF,0x0D,0xB6,0xD7,0x0D,0xEE,0x56},
186 {0xA1,0xF9,0x91,0x55,0x41,0x02,0x0B,0x56},
187 {0x6F,0xBF,0x1C,0xAF,0xCF,0xFD,0x05,0x56},
188 {0x2F,0x22,0xE4,0x9B,0xAB,0x7C,0xA1,0xAC},
189 {0x5A,0x6B,0x61,0x2C,0xC2,0x6C,0xCE,0x4A},
190 {0x5F,0x4C,0x03,0x8E,0xD1,0x2B,0x2E,0x41},
191 {0x63,0xFA,0xC0,0xD0,0x34,0xD9,0xF7,0x93},
192 {0x61,0x7B,0x3A,0x0C,0xE8,0xF0,0x71,0x00},
193 {0xDB,0x95,0x86,0x05,0xF8,0xC8,0xC6,0x06},
194 {0xED,0xBF,0xD1,0xC6,0x6C,0x29,0xCC,0xC7},
195 {0x35,0x55,0x50,0xB2,0x15,0x0E,0x24,0x51},
196 {0xCA,0xAA,0xAF,0x4D,0xEA,0xF1,0xDB,0xAE},
197 {0xD5,0xD4,0x4F,0xF7,0x20,0x68,0x3D,0x0D},
198 {0x2A,0x2B,0xB0,0x08,0xDF,0x97,0xC2,0xF2}};
199
200static unsigned char cipher_ecb2[NUM_TESTS-1][8]={
201 {0x92,0x95,0xB5,0x9B,0xB3,0x84,0x73,0x6E},
202 {0x19,0x9E,0x9D,0x6D,0xF3,0x9A,0xA8,0x16},
203 {0x2A,0x4B,0x4D,0x24,0x52,0x43,0x84,0x27},
204 {0x35,0x84,0x3C,0x01,0x9D,0x18,0xC5,0xB6},
205 {0x4A,0x5B,0x2F,0x42,0xAA,0x77,0x19,0x25},
206 {0xA0,0x6B,0xA9,0xB8,0xCA,0x5B,0x17,0x8A},
207 {0xAB,0x9D,0xB7,0xFB,0xED,0x95,0xF2,0x74},
208 {0x3D,0x25,0x6C,0x23,0xA7,0x25,0x2F,0xD6},
209 {0xB7,0x6F,0xAB,0x4F,0xBD,0xBD,0xB7,0x67},
210 {0x8F,0x68,0x27,0xD6,0x9C,0xF4,0x1A,0x10},
211 {0x82,0x57,0xA1,0xD6,0x50,0x5E,0x81,0x85},
212 {0xA2,0x0F,0x0A,0xCD,0x80,0x89,0x7D,0xFA},
213 {0xCD,0x2A,0x53,0x3A,0xDB,0x0D,0x7E,0xF3},
214 {0xD2,0xC2,0xBE,0x27,0xE8,0x1B,0x68,0xE3},
215 {0xE9,0x24,0xCF,0x4F,0x89,0x3C,0x5B,0x0A},
216 {0xA7,0x18,0xC3,0x9F,0xFA,0x9F,0xD7,0x69},
217 {0x77,0x2C,0x79,0xB1,0xD2,0x31,0x7E,0xB1},
218 {0x49,0xAB,0x92,0x7F,0xD0,0x22,0x00,0xB7},
219 {0xCE,0x1C,0x6C,0x7D,0x85,0xE3,0x4A,0x6F},
220 {0xBE,0x91,0xD6,0xE1,0x27,0xB2,0xE9,0x87},
221 {0x70,0x28,0xAE,0x8F,0xD1,0xF5,0x74,0x1A},
222 {0xAA,0x37,0x80,0xBB,0xF3,0x22,0x1D,0xDE},
223 {0xA6,0xC4,0xD2,0x5E,0x28,0x93,0xAC,0xB3},
224 {0x22,0x07,0x81,0x5A,0xE4,0xB7,0x1A,0xAD},
225 {0xDC,0xCE,0x05,0xE7,0x07,0xBD,0xF5,0x84},
226 {0x26,0x1D,0x39,0x2C,0xB3,0xBA,0xA5,0x85},
227 {0xB4,0xF7,0x0F,0x72,0xFB,0x04,0xF0,0xDC},
228 {0x95,0xBA,0xA9,0x4E,0x87,0x36,0xF2,0x89},
229 {0xD4,0x07,0x3A,0xF1,0x5A,0x17,0x82,0x0E},
230 {0xEF,0x6F,0xAF,0xA7,0x66,0x1A,0x7E,0x89},
231 {0xC1,0x97,0xF5,0x58,0x74,0x8A,0x20,0xE7},
232 {0x43,0x34,0xCF,0xDA,0x22,0xC4,0x86,0xC8},
233 {0x08,0xD7,0xB4,0xFB,0x62,0x9D,0x08,0x85}};
234
235static unsigned char cbc_key [8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
236static unsigned char cbc2_key[8]={0xf1,0xe0,0xd3,0xc2,0xb5,0xa4,0x97,0x86};
237static unsigned char cbc3_key[8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
238static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
239/* Changed the following text constant to binary so it will work on ebcdic
240 * machines :-) */
241/* static char cbc_data[40]="7654321 Now is the time for \0001"; */
242static unsigned char cbc_data[40]={
243 0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x20,
244 0x4E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,
245 0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20,
246 0x66,0x6F,0x72,0x20,0x00,0x31,0x00,0x00,
247 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
248 };
249
250static unsigned char cbc_ok[32]={
251 0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
252 0xac,0xd8,0xae,0xfd,0xdf,0xd8,0xa1,0xeb,
253 0x46,0x8e,0x91,0x15,0x78,0x88,0xba,0x68,
254 0x1d,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
255
256#ifdef SCREW_THE_PARITY
257#error "SCREW_THE_PARITY is not ment to be defined."
258#error "Original vectors are preserved for reference only."
259static unsigned char cbc2_key[8]={0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
260static unsigned char xcbc_ok[32]={
261 0x86,0x74,0x81,0x0D,0x61,0xA4,0xA5,0x48,
262 0xB9,0x93,0x03,0xE1,0xB8,0xBB,0xBD,0xBD,
263 0x64,0x30,0x0B,0xB9,0x06,0x65,0x81,0x76,
264 0x04,0x1D,0x77,0x62,0x17,0xCA,0x2B,0xD2,
265 };
266#else
267static unsigned char xcbc_ok[32]={
268 0x84,0x6B,0x29,0x14,0x85,0x1E,0x9A,0x29,
269 0x54,0x73,0x2F,0x8A,0xA0,0xA6,0x11,0xC1,
270 0x15,0xCD,0xC2,0xD7,0x95,0x1B,0x10,0x53,
271 0xA6,0x3C,0x5E,0x03,0xB2,0x1A,0xA3,0xC4,
272 };
273#endif
274
275static unsigned char cbc3_ok[32]={
276 0x3F,0xE3,0x01,0xC9,0x62,0xAC,0x01,0xD0,
277 0x22,0x13,0x76,0x3C,0x1C,0xBD,0x4C,0xDC,
278 0x79,0x96,0x57,0xC0,0x64,0xEC,0xF5,0xD4,
279 0x1C,0x67,0x38,0x12,0xCF,0xDE,0x96,0x75};
280
281static unsigned char pcbc_ok[32]={
282 0xcc,0xd1,0x73,0xff,0xab,0x20,0x39,0xf4,
283 0x6d,0xec,0xb4,0x70,0xa0,0xe5,0x6b,0x15,
284 0xae,0xa6,0xbf,0x61,0xed,0x7d,0x9c,0x9f,
285 0xf7,0x17,0x46,0x3b,0x8a,0xb3,0xcc,0x88};
286
287static unsigned char cfb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
288static unsigned char cfb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
289static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
290static unsigned char plain[24]=
291 {
292 0x4e,0x6f,0x77,0x20,0x69,0x73,
293 0x20,0x74,0x68,0x65,0x20,0x74,
294 0x69,0x6d,0x65,0x20,0x66,0x6f,
295 0x72,0x20,0x61,0x6c,0x6c,0x20
296 };
297static unsigned char cfb_cipher8[24]= {
298 0xf3,0x1f,0xda,0x07,0x01,0x14, 0x62,0xee,0x18,0x7f,0x43,0xd8,
299 0x0a,0x7c,0xd9,0xb5,0xb0,0xd2, 0x90,0xda,0x6e,0x5b,0x9a,0x87 };
300static unsigned char cfb_cipher16[24]={
301 0xF3,0x09,0x87,0x87,0x7F,0x57, 0xF7,0x3C,0x36,0xB6,0xDB,0x70,
302 0xD8,0xD5,0x34,0x19,0xD3,0x86, 0xB2,0x23,0xB7,0xB2,0xAD,0x1B };
303static unsigned char cfb_cipher32[24]={
304 0xF3,0x09,0x62,0x49,0xA4,0xDF, 0xA4,0x9F,0x33,0xDC,0x7B,0xAD,
305 0x4C,0xC8,0x9F,0x64,0xE4,0x53, 0xE5,0xEC,0x67,0x20,0xDA,0xB6 };
306static unsigned char cfb_cipher48[24]={
307 0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x30,0xB5,0x15,0xEC,0xBB,0x85,
308 0x97,0x5A,0x13,0x8C,0x68,0x60, 0xE2,0x38,0x34,0x3C,0xDC,0x1F };
309static unsigned char cfb_cipher64[24]={
310 0xF3,0x09,0x62,0x49,0xC7,0xF4, 0x6E,0x51,0xA6,0x9E,0x83,0x9B,
311 0x1A,0x92,0xF7,0x84,0x03,0x46, 0x71,0x33,0x89,0x8E,0xA6,0x22 };
312
313static unsigned char ofb_key[8]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
314static unsigned char ofb_iv[8]={0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
315static unsigned char ofb_buf1[24],ofb_buf2[24],ofb_tmp[8];
316static unsigned char ofb_cipher[24]=
317 {
318 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
319 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
320 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3
321 };
322
323#if 0
324static DES_LONG cbc_cksum_ret=0xB462FEF7L;
325#else
326static DES_LONG cbc_cksum_ret=0xF7FE62B4L;
327#endif
328static unsigned char cbc_cksum_data[8]={0x1D,0x26,0x93,0x97,0xf7,0xfe,0x62,0xb4};
329
330static char *pt(unsigned char *p);
331static int cfb_test(int bits, unsigned char *cfb_cipher);
332static int cfb64_test(unsigned char *cfb_cipher);
333static int ede_cfb64_test(unsigned char *cfb_cipher);
334int main(int argc, char *argv[])
335 {
336 int j,err=0;
337 unsigned int i;
338 des_cblock in,out,outin,iv3,iv2;
339 des_key_schedule ks,ks2,ks3;
340 unsigned char cbc_in[40];
341 unsigned char cbc_out[40];
342 DES_LONG cs;
343 unsigned char cret[8];
344#ifdef _CRAY
345 struct {
346 int a:32;
347 int b:32;
348 } lqret[2];
349#else
350 DES_LONG lqret[4];
351#endif
352 int num;
353 char *str;
354
355#ifndef OPENSSL_NO_DESCBCM
356 printf("Doing cbcm\n");
357 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
358 {
359 printf("Key error %d\n",j);
360 err=1;
361 }
362 if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
363 {
364 printf("Key error %d\n",j);
365 err=1;
366 }
367 if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
368 {
369 printf("Key error %d\n",j);
370 err=1;
371 }
372 memset(cbc_out,0,40);
373 memset(cbc_in,0,40);
374 i=strlen((char *)cbc_data)+1;
375 /* i=((i+7)/8)*8; */
376 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
377 memset(iv2,'\0',sizeof iv2);
378
379 DES_ede3_cbcm_encrypt(cbc_data,cbc_out,16L,&ks,&ks2,&ks3,&iv3,&iv2,
380 DES_ENCRYPT);
381 DES_ede3_cbcm_encrypt(&cbc_data[16],&cbc_out[16],i-16,&ks,&ks2,&ks3,
382 &iv3,&iv2,DES_ENCRYPT);
383 /* if (memcmp(cbc_out,cbc3_ok,
384 (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
385 {
386 printf("des_ede3_cbc_encrypt encrypt error\n");
387 err=1;
388 }
389 */
390 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
391 memset(iv2,'\0',sizeof iv2);
392 DES_ede3_cbcm_encrypt(cbc_out,cbc_in,i,&ks,&ks2,&ks3,&iv3,&iv2,DES_DECRYPT);
393 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
394 {
395 unsigned int n;
396
397 printf("des_ede3_cbcm_encrypt decrypt error\n");
398 for(n=0 ; n < i ; ++n)
399 printf(" %02x",cbc_data[n]);
400 printf("\n");
401 for(n=0 ; n < i ; ++n)
402 printf(" %02x",cbc_in[n]);
403 printf("\n");
404 err=1;
405 }
406#endif
407
408 printf("Doing ecb\n");
409 for (i=0; i<NUM_TESTS; i++)
410 {
411 DES_set_key_unchecked(&key_data[i],&ks);
412 memcpy(in,plain_data[i],8);
413 memset(out,0,8);
414 memset(outin,0,8);
415 des_ecb_encrypt(&in,&out,ks,DES_ENCRYPT);
416 des_ecb_encrypt(&out,&outin,ks,DES_DECRYPT);
417
418 if (memcmp(out,cipher_data[i],8) != 0)
419 {
420 printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
421 i+1,pt(key_data[i]),pt(in),pt(cipher_data[i]),
422 pt(out));
423 err=1;
424 }
425 if (memcmp(in,outin,8) != 0)
426 {
427 printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
428 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
429 err=1;
430 }
431 }
432
433#ifndef LIBDES_LIT
434 printf("Doing ede ecb\n");
435 for (i=0; i<(NUM_TESTS-2); i++)
436 {
437 DES_set_key_unchecked(&key_data[i],&ks);
438 DES_set_key_unchecked(&key_data[i+1],&ks2);
439 DES_set_key_unchecked(&key_data[i+2],&ks3);
440 memcpy(in,plain_data[i],8);
441 memset(out,0,8);
442 memset(outin,0,8);
443 des_ecb2_encrypt(&in,&out,ks,ks2,DES_ENCRYPT);
444 des_ecb2_encrypt(&out,&outin,ks,ks2,DES_DECRYPT);
445
446 if (memcmp(out,cipher_ecb2[i],8) != 0)
447 {
448 printf("Encryption error %2d\nk=%s p=%s o=%s act=%s\n",
449 i+1,pt(key_data[i]),pt(in),pt(cipher_ecb2[i]),
450 pt(out));
451 err=1;
452 }
453 if (memcmp(in,outin,8) != 0)
454 {
455 printf("Decryption error %2d\nk=%s p=%s o=%s act=%s\n",
456 i+1,pt(key_data[i]),pt(out),pt(in),pt(outin));
457 err=1;
458 }
459 }
460#endif
461
462 printf("Doing cbc\n");
463 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
464 {
465 printf("Key error %d\n",j);
466 err=1;
467 }
468 memset(cbc_out,0,40);
469 memset(cbc_in,0,40);
470 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
471 des_ncbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
472 &iv3,DES_ENCRYPT);
473 if (memcmp(cbc_out,cbc_ok,32) != 0)
474 {
475 printf("cbc_encrypt encrypt error\n");
476 err=1;
477 }
478
479 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
480 des_ncbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,
481 &iv3,DES_DECRYPT);
482 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)) != 0)
483 {
484 printf("cbc_encrypt decrypt error\n");
485 err=1;
486 }
487
488#ifndef LIBDES_LIT
489 printf("Doing desx cbc\n");
490 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
491 {
492 printf("Key error %d\n",j);
493 err=1;
494 }
495 memset(cbc_out,0,40);
496 memset(cbc_in,0,40);
497 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
498 des_xcbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
499 &iv3,&cbc2_key,&cbc3_key, DES_ENCRYPT);
500 if (memcmp(cbc_out,xcbc_ok,32) != 0)
501 {
502 printf("des_xcbc_encrypt encrypt error\n");
503 err=1;
504 }
505 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
506 des_xcbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,
507 &iv3,&cbc2_key,&cbc3_key, DES_DECRYPT);
508 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
509 {
510 printf("des_xcbc_encrypt decrypt error\n");
511 err=1;
512 }
513#endif
514
515 printf("Doing ede cbc\n");
516 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
517 {
518 printf("Key error %d\n",j);
519 err=1;
520 }
521 if ((j=DES_set_key_checked(&cbc2_key,&ks2)) != 0)
522 {
523 printf("Key error %d\n",j);
524 err=1;
525 }
526 if ((j=DES_set_key_checked(&cbc3_key,&ks3)) != 0)
527 {
528 printf("Key error %d\n",j);
529 err=1;
530 }
531 memset(cbc_out,0,40);
532 memset(cbc_in,0,40);
533 i=strlen((char *)cbc_data)+1;
534 /* i=((i+7)/8)*8; */
535 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
536
537 des_ede3_cbc_encrypt(cbc_data,cbc_out,16L,ks,ks2,ks3,&iv3,
538 DES_ENCRYPT);
539 des_ede3_cbc_encrypt(&(cbc_data[16]),&(cbc_out[16]),i-16,ks,ks2,ks3,
540 &iv3,DES_ENCRYPT);
541 if (memcmp(cbc_out,cbc3_ok,
542 (unsigned int)(strlen((char *)cbc_data)+1+7)/8*8) != 0)
543 {
544 unsigned int n;
545
546 printf("des_ede3_cbc_encrypt encrypt error\n");
547 for(n=0 ; n < i ; ++n)
548 printf(" %02x",cbc_out[n]);
549 printf("\n");
550 for(n=0 ; n < i ; ++n)
551 printf(" %02x",cbc3_ok[n]);
552 printf("\n");
553 err=1;
554 }
555
556 memcpy(iv3,cbc_iv,sizeof(cbc_iv));
557 des_ede3_cbc_encrypt(cbc_out,cbc_in,i,ks,ks2,ks3,&iv3,DES_DECRYPT);
558 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
559 {
560 unsigned int n;
561
562 printf("des_ede3_cbc_encrypt decrypt error\n");
563 for(n=0 ; n < i ; ++n)
564 printf(" %02x",cbc_data[n]);
565 printf("\n");
566 for(n=0 ; n < i ; ++n)
567 printf(" %02x",cbc_in[n]);
568 printf("\n");
569 err=1;
570 }
571
572#ifndef LIBDES_LIT
573 printf("Doing pcbc\n");
574 if ((j=DES_set_key_checked(&cbc_key,&ks)) != 0)
575 {
576 printf("Key error %d\n",j);
577 err=1;
578 }
579 memset(cbc_out,0,40);
580 memset(cbc_in,0,40);
581 des_pcbc_encrypt(cbc_data,cbc_out,strlen((char *)cbc_data)+1,ks,
582 &cbc_iv,DES_ENCRYPT);
583 if (memcmp(cbc_out,pcbc_ok,32) != 0)
584 {
585 printf("pcbc_encrypt encrypt error\n");
586 err=1;
587 }
588 des_pcbc_encrypt(cbc_out,cbc_in,strlen((char *)cbc_data)+1,ks,&cbc_iv,
589 DES_DECRYPT);
590 if (memcmp(cbc_in,cbc_data,strlen((char *)cbc_data)+1) != 0)
591 {
592 printf("pcbc_encrypt decrypt error\n");
593 err=1;
594 }
595
596 printf("Doing ");
597 printf("cfb8 ");
598 err+=cfb_test(8,cfb_cipher8);
599 printf("cfb16 ");
600 err+=cfb_test(16,cfb_cipher16);
601 printf("cfb32 ");
602 err+=cfb_test(32,cfb_cipher32);
603 printf("cfb48 ");
604 err+=cfb_test(48,cfb_cipher48);
605 printf("cfb64 ");
606 err+=cfb_test(64,cfb_cipher64);
607
608 printf("cfb64() ");
609 err+=cfb64_test(cfb_cipher64);
610
611 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
612 for (i=0; i<sizeof(plain); i++)
613 des_cfb_encrypt(&(plain[i]),&(cfb_buf1[i]),
614 8,1,ks,&cfb_tmp,DES_ENCRYPT);
615 if (memcmp(cfb_cipher8,cfb_buf1,sizeof(plain)) != 0)
616 {
617 printf("cfb_encrypt small encrypt error\n");
618 err=1;
619 }
620
621 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
622 for (i=0; i<sizeof(plain); i++)
623 des_cfb_encrypt(&(cfb_buf1[i]),&(cfb_buf2[i]),
624 8,1,ks,&cfb_tmp,DES_DECRYPT);
625 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
626 {
627 printf("cfb_encrypt small decrypt error\n");
628 err=1;
629 }
630
631 printf("ede_cfb64() ");
632 err+=ede_cfb64_test(cfb_cipher64);
633
634 printf("done\n");
635
636 printf("Doing ofb\n");
637 DES_set_key_checked(&ofb_key,&ks);
638 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
639 des_ofb_encrypt(plain,ofb_buf1,64,sizeof(plain)/8,ks,&ofb_tmp);
640 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
641 {
642 printf("ofb_encrypt encrypt error\n");
643printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
644ofb_buf1[8+0], ofb_buf1[8+1], ofb_buf1[8+2], ofb_buf1[8+3],
645ofb_buf1[8+4], ofb_buf1[8+5], ofb_buf1[8+6], ofb_buf1[8+7]);
646printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
647ofb_buf1[8+0], ofb_cipher[8+1], ofb_cipher[8+2], ofb_cipher[8+3],
648ofb_buf1[8+4], ofb_cipher[8+5], ofb_cipher[8+6], ofb_cipher[8+7]);
649 err=1;
650 }
651 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
652 des_ofb_encrypt(ofb_buf1,ofb_buf2,64,sizeof(ofb_buf1)/8,ks,&ofb_tmp);
653 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
654 {
655 printf("ofb_encrypt decrypt error\n");
656printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
657ofb_buf2[8+0], ofb_buf2[8+1], ofb_buf2[8+2], ofb_buf2[8+3],
658ofb_buf2[8+4], ofb_buf2[8+5], ofb_buf2[8+6], ofb_buf2[8+7]);
659printf("%02X %02X %02X %02X %02X %02X %02X %02X\n",
660plain[8+0], plain[8+1], plain[8+2], plain[8+3],
661plain[8+4], plain[8+5], plain[8+6], plain[8+7]);
662 err=1;
663 }
664
665 printf("Doing ofb64\n");
666 DES_set_key_checked(&ofb_key,&ks);
667 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
668 memset(ofb_buf1,0,sizeof(ofb_buf1));
669 memset(ofb_buf2,0,sizeof(ofb_buf1));
670 num=0;
671 for (i=0; i<sizeof(plain); i++)
672 {
673 des_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,&ofb_tmp,
674 &num);
675 }
676 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
677 {
678 printf("ofb64_encrypt encrypt error\n");
679 err=1;
680 }
681 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
682 num=0;
683 des_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,&ofb_tmp,
684 &num);
685 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
686 {
687 printf("ofb64_encrypt decrypt error\n");
688 err=1;
689 }
690
691 printf("Doing ede_ofb64\n");
692 DES_set_key_checked(&ofb_key,&ks);
693 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
694 memset(ofb_buf1,0,sizeof(ofb_buf1));
695 memset(ofb_buf2,0,sizeof(ofb_buf1));
696 num=0;
697 for (i=0; i<sizeof(plain); i++)
698 {
699 des_ede3_ofb64_encrypt(&(plain[i]),&(ofb_buf1[i]),1,ks,ks,
700 ks,&ofb_tmp,&num);
701 }
702 if (memcmp(ofb_cipher,ofb_buf1,sizeof(ofb_buf1)) != 0)
703 {
704 printf("ede_ofb64_encrypt encrypt error\n");
705 err=1;
706 }
707 memcpy(ofb_tmp,ofb_iv,sizeof(ofb_iv));
708 num=0;
709 des_ede3_ofb64_encrypt(ofb_buf1,ofb_buf2,sizeof(ofb_buf1),ks,ks,ks,
710 &ofb_tmp,&num);
711 if (memcmp(plain,ofb_buf2,sizeof(ofb_buf2)) != 0)
712 {
713 printf("ede_ofb64_encrypt decrypt error\n");
714 err=1;
715 }
716
717 printf("Doing cbc_cksum\n");
718 DES_set_key_checked(&cbc_key,&ks);
719 cs=des_cbc_cksum(cbc_data,&cret,strlen((char *)cbc_data),ks,&cbc_iv);
720 if (cs != cbc_cksum_ret)
721 {
722 printf("bad return value (%08lX), should be %08lX\n",
723 (unsigned long)cs,(unsigned long)cbc_cksum_ret);
724 err=1;
725 }
726 if (memcmp(cret,cbc_cksum_data,8) != 0)
727 {
728 printf("bad cbc_cksum block returned\n");
729 err=1;
730 }
731
732 printf("Doing quad_cksum\n");
733 cs=des_quad_cksum(cbc_data,(des_cblock *)lqret,
734 (long)strlen((char *)cbc_data),2,(des_cblock *)cbc_iv);
735 if (cs != 0x70d7a63aL)
736 {
737 printf("quad_cksum error, ret %08lx should be 70d7a63a\n",
738 (unsigned long)cs);
739 err=1;
740 }
741#ifdef _CRAY
742 if (lqret[0].a != 0x327eba8dL)
743 {
744 printf("quad_cksum error, out[0] %08lx is not %08lx\n",
745 (unsigned long)lqret[0].a,0x327eba8dUL);
746 err=1;
747 }
748 if (lqret[0].b != 0x201a49ccL)
749 {
750 printf("quad_cksum error, out[1] %08lx is not %08lx\n",
751 (unsigned long)lqret[0].b,0x201a49ccUL);
752 err=1;
753 }
754 if (lqret[1].a != 0x70d7a63aL)
755 {
756 printf("quad_cksum error, out[2] %08lx is not %08lx\n",
757 (unsigned long)lqret[1].a,0x70d7a63aUL);
758 err=1;
759 }
760 if (lqret[1].b != 0x501c2c26L)
761 {
762 printf("quad_cksum error, out[3] %08lx is not %08lx\n",
763 (unsigned long)lqret[1].b,0x501c2c26UL);
764 err=1;
765 }
766#else
767 if (lqret[0] != 0x327eba8dL)
768 {
769 printf("quad_cksum error, out[0] %08lx is not %08lx\n",
770 (unsigned long)lqret[0],0x327eba8dUL);
771 err=1;
772 }
773 if (lqret[1] != 0x201a49ccL)
774 {
775 printf("quad_cksum error, out[1] %08lx is not %08lx\n",
776 (unsigned long)lqret[1],0x201a49ccUL);
777 err=1;
778 }
779 if (lqret[2] != 0x70d7a63aL)
780 {
781 printf("quad_cksum error, out[2] %08lx is not %08lx\n",
782 (unsigned long)lqret[2],0x70d7a63aUL);
783 err=1;
784 }
785 if (lqret[3] != 0x501c2c26L)
786 {
787 printf("quad_cksum error, out[3] %08lx is not %08lx\n",
788 (unsigned long)lqret[3],0x501c2c26UL);
789 err=1;
790 }
791#endif
792#endif
793
794 printf("input word alignment test");
795 for (i=0; i<4; i++)
796 {
797 printf(" %d",i);
798 des_ncbc_encrypt(&(cbc_out[i]),cbc_in,
799 strlen((char *)cbc_data)+1,ks,
800 &cbc_iv,DES_ENCRYPT);
801 }
802 printf("\noutput word alignment test");
803 for (i=0; i<4; i++)
804 {
805 printf(" %d",i);
806 des_ncbc_encrypt(cbc_out,&(cbc_in[i]),
807 strlen((char *)cbc_data)+1,ks,
808 &cbc_iv,DES_ENCRYPT);
809 }
810 printf("\n");
811 printf("fast crypt test ");
812 str=crypt("testing","ef");
813 if (strcmp("efGnQx2725bI2",str) != 0)
814 {
815 printf("fast crypt error, %s should be efGnQx2725bI2\n",str);
816 err=1;
817 }
818 str=crypt("bca76;23","yA");
819 if (strcmp("yA1Rp/1hZXIJk",str) != 0)
820 {
821 printf("fast crypt error, %s should be yA1Rp/1hZXIJk\n",str);
822 err=1;
823 }
824#ifdef OPENSSL_SYS_NETWARE
825 if (err) printf("ERROR: %d\n", err);
826#endif
827 printf("\n");
828 return(err);
829 }
830
831static char *pt(unsigned char *p)
832 {
833 static char bufs[10][20];
834 static int bnum=0;
835 char *ret;
836 int i;
837 static char *f="0123456789ABCDEF";
838
839 ret= &(bufs[bnum++][0]);
840 bnum%=10;
841 for (i=0; i<8; i++)
842 {
843 ret[i*2]=f[(p[i]>>4)&0xf];
844 ret[i*2+1]=f[p[i]&0xf];
845 }
846 ret[16]='\0';
847 return(ret);
848 }
849
850#ifndef LIBDES_LIT
851
852static int cfb_test(int bits, unsigned char *cfb_cipher)
853 {
854 des_key_schedule ks;
855 int i,err=0;
856
857 DES_set_key_checked(&cfb_key,&ks);
858 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
859 des_cfb_encrypt(plain,cfb_buf1,bits,sizeof(plain),ks,&cfb_tmp,
860 DES_ENCRYPT);
861 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
862 {
863 err=1;
864 printf("cfb_encrypt encrypt error\n");
865 for (i=0; i<24; i+=8)
866 printf("%s\n",pt(&(cfb_buf1[i])));
867 }
868 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
869 des_cfb_encrypt(cfb_buf1,cfb_buf2,bits,sizeof(plain),ks,&cfb_tmp,
870 DES_DECRYPT);
871 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
872 {
873 err=1;
874 printf("cfb_encrypt decrypt error\n");
875 for (i=0; i<24; i+=8)
876 printf("%s\n",pt(&(cfb_buf1[i])));
877 }
878 return(err);
879 }
880
881static int cfb64_test(unsigned char *cfb_cipher)
882 {
883 des_key_schedule ks;
884 int err=0,i,n;
885
886 DES_set_key_checked(&cfb_key,&ks);
887 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
888 n=0;
889 des_cfb64_encrypt(plain,cfb_buf1,12,ks,&cfb_tmp,&n,DES_ENCRYPT);
890 des_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),sizeof(plain)-12,ks,
891 &cfb_tmp,&n,DES_ENCRYPT);
892 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
893 {
894 err=1;
895 printf("cfb_encrypt encrypt error\n");
896 for (i=0; i<24; i+=8)
897 printf("%s\n",pt(&(cfb_buf1[i])));
898 }
899 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
900 n=0;
901 des_cfb64_encrypt(cfb_buf1,cfb_buf2,17,ks,&cfb_tmp,&n,DES_DECRYPT);
902 des_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
903 sizeof(plain)-17,ks,&cfb_tmp,&n,DES_DECRYPT);
904 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
905 {
906 err=1;
907 printf("cfb_encrypt decrypt error\n");
908 for (i=0; i<24; i+=8)
909 printf("%s\n",pt(&(cfb_buf2[i])));
910 }
911 return(err);
912 }
913
914static int ede_cfb64_test(unsigned char *cfb_cipher)
915 {
916 des_key_schedule ks;
917 int err=0,i,n;
918
919 DES_set_key_checked(&cfb_key,&ks);
920 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
921 n=0;
922 des_ede3_cfb64_encrypt(plain,cfb_buf1,12,ks,ks,ks,&cfb_tmp,&n,
923 DES_ENCRYPT);
924 des_ede3_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
925 sizeof(plain)-12,ks,ks,ks,
926 &cfb_tmp,&n,DES_ENCRYPT);
927 if (memcmp(cfb_cipher,cfb_buf1,sizeof(plain)) != 0)
928 {
929 err=1;
930 printf("ede_cfb_encrypt encrypt error\n");
931 for (i=0; i<24; i+=8)
932 printf("%s\n",pt(&(cfb_buf1[i])));
933 }
934 memcpy(cfb_tmp,cfb_iv,sizeof(cfb_iv));
935 n=0;
936 des_ede3_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,ks,ks,ks,
937 &cfb_tmp,&n,DES_DECRYPT);
938 des_ede3_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
939 sizeof(plain)-17,ks,ks,ks,
940 &cfb_tmp,&n,DES_DECRYPT);
941 if (memcmp(plain,cfb_buf2,sizeof(plain)) != 0)
942 {
943 err=1;
944 printf("ede_cfb_encrypt decrypt error\n");
945 for (i=0; i<24; i+=8)
946 printf("%s\n",pt(&(cfb_buf2[i])));
947 }
948 return(err);
949 }
950
951#endif
952#endif
diff --git a/src/lib/libssl/src/crypto/dh/dhtest.c b/src/lib/libssl/src/crypto/dh/dhtest.c
deleted file mode 100644
index 882f5c310a..0000000000
--- a/src/lib/libssl/src/crypto/dh/dhtest.c
+++ /dev/null
@@ -1,226 +0,0 @@
1/* crypto/dh/dhtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Until the key-gen callbacks are modified to use newer prototypes, we allow
60 * deprecated functions for openssl-internal code */
61#ifdef OPENSSL_NO_DEPRECATED
62#undef OPENSSL_NO_DEPRECATED
63#endif
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68
69#include "../e_os.h"
70
71#include <openssl/crypto.h>
72#include <openssl/bio.h>
73#include <openssl/bn.h>
74#include <openssl/rand.h>
75#include <openssl/err.h>
76
77#ifdef OPENSSL_NO_DH
78int main(int argc, char *argv[])
79{
80 printf("No DH support\n");
81 return(0);
82}
83#else
84#include <openssl/dh.h>
85
86#ifdef OPENSSL_SYS_WIN16
87#define MS_CALLBACK _far _loadds
88#else
89#define MS_CALLBACK
90#endif
91
92static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg);
93
94static const char rnd_seed[] = "string to make the random number generator think it has entropy";
95
96int main(int argc, char *argv[])
97 {
98 BN_GENCB _cb;
99 DH *a;
100 DH *b=NULL;
101 char buf[12];
102 unsigned char *abuf=NULL,*bbuf=NULL;
103 int i,alen,blen,aout,bout,ret=1;
104 BIO *out;
105
106 CRYPTO_malloc_debug_init();
107 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
108 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
109
110#ifdef OPENSSL_SYS_WIN32
111 CRYPTO_malloc_init();
112#endif
113
114 RAND_seed(rnd_seed, sizeof rnd_seed);
115
116 out=BIO_new(BIO_s_file());
117 if (out == NULL) EXIT(1);
118 BIO_set_fp(out,stdout,BIO_NOCLOSE);
119
120 BN_GENCB_set(&_cb, &cb, out);
121 if(((a = DH_new()) == NULL) || !DH_generate_parameters_ex(a, 64,
122 DH_GENERATOR_5, &_cb))
123 goto err;
124
125 if (!DH_check(a, &i)) goto err;
126 if (i & DH_CHECK_P_NOT_PRIME)
127 BIO_puts(out, "p value is not prime\n");
128 if (i & DH_CHECK_P_NOT_SAFE_PRIME)
129 BIO_puts(out, "p value is not a safe prime\n");
130 if (i & DH_UNABLE_TO_CHECK_GENERATOR)
131 BIO_puts(out, "unable to check the generator value\n");
132 if (i & DH_NOT_SUITABLE_GENERATOR)
133 BIO_puts(out, "the g value is not a generator\n");
134
135 BIO_puts(out,"\np =");
136 BN_print(out,a->p);
137 BIO_puts(out,"\ng =");
138 BN_print(out,a->g);
139 BIO_puts(out,"\n");
140
141 b=DH_new();
142 if (b == NULL) goto err;
143
144 b->p=BN_dup(a->p);
145 b->g=BN_dup(a->g);
146 if ((b->p == NULL) || (b->g == NULL)) goto err;
147
148 /* Set a to run with normal modexp and b to use constant time */
149 a->flags &= ~DH_FLAG_NO_EXP_CONSTTIME;
150 b->flags |= DH_FLAG_NO_EXP_CONSTTIME;
151
152 if (!DH_generate_key(a)) goto err;
153 BIO_puts(out,"pri 1=");
154 BN_print(out,a->priv_key);
155 BIO_puts(out,"\npub 1=");
156 BN_print(out,a->pub_key);
157 BIO_puts(out,"\n");
158
159 if (!DH_generate_key(b)) goto err;
160 BIO_puts(out,"pri 2=");
161 BN_print(out,b->priv_key);
162 BIO_puts(out,"\npub 2=");
163 BN_print(out,b->pub_key);
164 BIO_puts(out,"\n");
165
166 alen=DH_size(a);
167 abuf=(unsigned char *)OPENSSL_malloc(alen);
168 aout=DH_compute_key(abuf,b->pub_key,a);
169
170 BIO_puts(out,"key1 =");
171 for (i=0; i<aout; i++)
172 {
173 sprintf(buf,"%02X",abuf[i]);
174 BIO_puts(out,buf);
175 }
176 BIO_puts(out,"\n");
177
178 blen=DH_size(b);
179 bbuf=(unsigned char *)OPENSSL_malloc(blen);
180 bout=DH_compute_key(bbuf,a->pub_key,b);
181
182 BIO_puts(out,"key2 =");
183 for (i=0; i<bout; i++)
184 {
185 sprintf(buf,"%02X",bbuf[i]);
186 BIO_puts(out,buf);
187 }
188 BIO_puts(out,"\n");
189 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
190 {
191 fprintf(stderr,"Error in DH routines\n");
192 ret=1;
193 }
194 else
195 ret=0;
196err:
197 ERR_print_errors_fp(stderr);
198
199 if (abuf != NULL) OPENSSL_free(abuf);
200 if (bbuf != NULL) OPENSSL_free(bbuf);
201 if(b != NULL) DH_free(b);
202 if(a != NULL) DH_free(a);
203 BIO_free(out);
204#ifdef OPENSSL_SYS_NETWARE
205 if (ret) printf("ERROR: %d\n", ret);
206#endif
207 EXIT(ret);
208 return(ret);
209 }
210
211static int MS_CALLBACK cb(int p, int n, BN_GENCB *arg)
212 {
213 char c='*';
214
215 if (p == 0) c='.';
216 if (p == 1) c='+';
217 if (p == 2) c='*';
218 if (p == 3) c='\n';
219 BIO_write(arg->arg,&c,1);
220 (void)BIO_flush(arg->arg);
221#ifdef LINT
222 p=n;
223#endif
224 return 1;
225 }
226#endif
diff --git a/src/lib/libssl/src/crypto/dsa/dsatest.c b/src/lib/libssl/src/crypto/dsa/dsatest.c
deleted file mode 100644
index edffd24e6b..0000000000
--- a/src/lib/libssl/src/crypto/dsa/dsatest.c
+++ /dev/null
@@ -1,259 +0,0 @@
1/* crypto/dsa/dsatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* Until the key-gen callbacks are modified to use newer prototypes, we allow
60 * deprecated functions for openssl-internal code */
61#ifdef OPENSSL_NO_DEPRECATED
62#undef OPENSSL_NO_DEPRECATED
63#endif
64
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <sys/types.h>
69#include <sys/stat.h>
70
71#include "../e_os.h"
72
73#include <openssl/crypto.h>
74#include <openssl/rand.h>
75#include <openssl/bio.h>
76#include <openssl/err.h>
77#include <openssl/bn.h>
78
79#ifdef OPENSSL_NO_DSA
80int main(int argc, char *argv[])
81{
82 printf("No DSA support\n");
83 return(0);
84}
85#else
86#include <openssl/dsa.h>
87
88#ifdef OPENSSL_SYS_WIN16
89#define MS_CALLBACK _far _loadds
90#else
91#define MS_CALLBACK
92#endif
93
94static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg);
95
96/* seed, out_p, out_q, out_g are taken from the updated Appendix 5 to
97 * FIPS PUB 186 and also appear in Appendix 5 to FIPS PIB 186-1 */
98static unsigned char seed[20]={
99 0xd5,0x01,0x4e,0x4b,0x60,0xef,0x2b,0xa8,0xb6,0x21,0x1b,0x40,
100 0x62,0xba,0x32,0x24,0xe0,0x42,0x7d,0xd3,
101 };
102
103static unsigned char out_p[]={
104 0x8d,0xf2,0xa4,0x94,0x49,0x22,0x76,0xaa,
105 0x3d,0x25,0x75,0x9b,0xb0,0x68,0x69,0xcb,
106 0xea,0xc0,0xd8,0x3a,0xfb,0x8d,0x0c,0xf7,
107 0xcb,0xb8,0x32,0x4f,0x0d,0x78,0x82,0xe5,
108 0xd0,0x76,0x2f,0xc5,0xb7,0x21,0x0e,0xaf,
109 0xc2,0xe9,0xad,0xac,0x32,0xab,0x7a,0xac,
110 0x49,0x69,0x3d,0xfb,0xf8,0x37,0x24,0xc2,
111 0xec,0x07,0x36,0xee,0x31,0xc8,0x02,0x91,
112 };
113
114static unsigned char out_q[]={
115 0xc7,0x73,0x21,0x8c,0x73,0x7e,0xc8,0xee,
116 0x99,0x3b,0x4f,0x2d,0xed,0x30,0xf4,0x8e,
117 0xda,0xce,0x91,0x5f,
118 };
119
120static unsigned char out_g[]={
121 0x62,0x6d,0x02,0x78,0x39,0xea,0x0a,0x13,
122 0x41,0x31,0x63,0xa5,0x5b,0x4c,0xb5,0x00,
123 0x29,0x9d,0x55,0x22,0x95,0x6c,0xef,0xcb,
124 0x3b,0xff,0x10,0xf3,0x99,0xce,0x2c,0x2e,
125 0x71,0xcb,0x9d,0xe5,0xfa,0x24,0xba,0xbf,
126 0x58,0xe5,0xb7,0x95,0x21,0x92,0x5c,0x9c,
127 0xc4,0x2e,0x9f,0x6f,0x46,0x4b,0x08,0x8c,
128 0xc5,0x72,0xaf,0x53,0xe6,0xd7,0x88,0x02,
129 };
130
131static const unsigned char str1[]="12345678901234567890";
132
133static const char rnd_seed[] = "string to make the random number generator think it has entropy";
134
135static BIO *bio_err=NULL;
136
137int main(int argc, char **argv)
138 {
139 BN_GENCB cb;
140 DSA *dsa=NULL;
141 int counter,ret=0,i,j;
142 unsigned char buf[256];
143 unsigned long h;
144 unsigned char sig[256];
145 unsigned int siglen;
146
147 if (bio_err == NULL)
148 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
149
150 CRYPTO_malloc_debug_init();
151 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
152 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
153
154 ERR_load_crypto_strings();
155 RAND_seed(rnd_seed, sizeof rnd_seed);
156
157 BIO_printf(bio_err,"test generation of DSA parameters\n");
158
159 BN_GENCB_set(&cb, dsa_cb, bio_err);
160 if(((dsa = DSA_new()) == NULL) || !DSA_generate_parameters_ex(dsa, 512,
161 seed, 20, &counter, &h, &cb))
162 goto end;
163
164 BIO_printf(bio_err,"seed\n");
165 for (i=0; i<20; i+=4)
166 {
167 BIO_printf(bio_err,"%02X%02X%02X%02X ",
168 seed[i],seed[i+1],seed[i+2],seed[i+3]);
169 }
170 BIO_printf(bio_err,"\ncounter=%d h=%ld\n",counter,h);
171
172 DSA_print(bio_err,dsa,0);
173 if (counter != 105)
174 {
175 BIO_printf(bio_err,"counter should be 105\n");
176 goto end;
177 }
178 if (h != 2)
179 {
180 BIO_printf(bio_err,"h should be 2\n");
181 goto end;
182 }
183
184 i=BN_bn2bin(dsa->q,buf);
185 j=sizeof(out_q);
186 if ((i != j) || (memcmp(buf,out_q,i) != 0))
187 {
188 BIO_printf(bio_err,"q value is wrong\n");
189 goto end;
190 }
191
192 i=BN_bn2bin(dsa->p,buf);
193 j=sizeof(out_p);
194 if ((i != j) || (memcmp(buf,out_p,i) != 0))
195 {
196 BIO_printf(bio_err,"p value is wrong\n");
197 goto end;
198 }
199
200 i=BN_bn2bin(dsa->g,buf);
201 j=sizeof(out_g);
202 if ((i != j) || (memcmp(buf,out_g,i) != 0))
203 {
204 BIO_printf(bio_err,"g value is wrong\n");
205 goto end;
206 }
207
208 dsa->flags |= DSA_FLAG_NO_EXP_CONSTTIME;
209 DSA_generate_key(dsa);
210 DSA_sign(0, str1, 20, sig, &siglen, dsa);
211 if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
212 ret=1;
213
214 dsa->flags &= ~DSA_FLAG_NO_EXP_CONSTTIME;
215 DSA_generate_key(dsa);
216 DSA_sign(0, str1, 20, sig, &siglen, dsa);
217 if (DSA_verify(0, str1, 20, sig, siglen, dsa) == 1)
218 ret=1;
219
220end:
221 if (!ret)
222 ERR_print_errors(bio_err);
223 if (dsa != NULL) DSA_free(dsa);
224 CRYPTO_cleanup_all_ex_data();
225 ERR_remove_thread_state(NULL);
226 ERR_free_strings();
227 CRYPTO_mem_leaks(bio_err);
228 if (bio_err != NULL)
229 {
230 BIO_free(bio_err);
231 bio_err = NULL;
232 }
233#ifdef OPENSSL_SYS_NETWARE
234 if (!ret) printf("ERROR\n");
235#endif
236 EXIT(!ret);
237 return(0);
238 }
239
240static int MS_CALLBACK dsa_cb(int p, int n, BN_GENCB *arg)
241 {
242 char c='*';
243 static int ok=0,num=0;
244
245 if (p == 0) { c='.'; num++; };
246 if (p == 1) c='+';
247 if (p == 2) { c='*'; ok++; }
248 if (p == 3) c='\n';
249 BIO_write(arg->arg,&c,1);
250 (void)BIO_flush(arg->arg);
251
252 if (!ok && (p == 0) && (num > 1))
253 {
254 BIO_printf((BIO *)arg,"error in dsatest\n");
255 return 0;
256 }
257 return 1;
258 }
259#endif
diff --git a/src/lib/libssl/src/crypto/ec/ectest.c b/src/lib/libssl/src/crypto/ec/ectest.c
deleted file mode 100644
index 102eaa9b23..0000000000
--- a/src/lib/libssl/src/crypto/ec/ectest.c
+++ /dev/null
@@ -1,1489 +0,0 @@
1/* crypto/ec/ectest.c */
2/*
3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@openssl.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <stdio.h>
73#include <stdlib.h>
74#ifdef FLAT_INC
75#include "e_os.h"
76#else
77#include "../e_os.h"
78#endif
79#include <string.h>
80#include <time.h>
81
82
83#ifdef OPENSSL_NO_EC
84int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
85#else
86
87
88#include <openssl/ec.h>
89#ifndef OPENSSL_NO_ENGINE
90#include <openssl/engine.h>
91#endif
92#include <openssl/err.h>
93#include <openssl/obj_mac.h>
94#include <openssl/objects.h>
95#include <openssl/rand.h>
96#include <openssl/bn.h>
97#include <openssl/opensslconf.h>
98
99#if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
100/* suppress "too big too optimize" warning */
101#pragma warning(disable:4959)
102#endif
103
104#define ABORT do { \
105 fflush(stdout); \
106 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
107 ERR_print_errors_fp(stderr); \
108 EXIT(1); \
109} while (0)
110
111#define TIMING_BASE_PT 0
112#define TIMING_RAND_PT 1
113#define TIMING_SIMUL 2
114
115#if 0
116static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
117 {
118 clock_t clck;
119 int i, j;
120 BIGNUM *s;
121 BIGNUM *r[10], *r0[10];
122 EC_POINT *P;
123
124 s = BN_new();
125 if (s == NULL) ABORT;
126
127 fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
128 if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
129 fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
130 fflush(stdout);
131
132 P = EC_POINT_new(group);
133 if (P == NULL) ABORT;
134 EC_POINT_copy(P, EC_GROUP_get0_generator(group));
135
136 for (i = 0; i < 10; i++)
137 {
138 if ((r[i] = BN_new()) == NULL) ABORT;
139 if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
140 if (type != TIMING_BASE_PT)
141 {
142 if ((r0[i] = BN_new()) == NULL) ABORT;
143 if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
144 }
145 }
146
147 clck = clock();
148 for (i = 0; i < 10; i++)
149 {
150 for (j = 0; j < 10; j++)
151 {
152 if (!EC_POINT_mul(group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
153 (type != TIMING_BASE_PT) ? P : NULL, (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx)) ABORT;
154 }
155 }
156 clck = clock() - clck;
157
158 fprintf(stdout, "\n");
159
160#ifdef CLOCKS_PER_SEC
161 /* "To determine the time in seconds, the value returned
162 * by the clock function should be divided by the value
163 * of the macro CLOCKS_PER_SEC."
164 * -- ISO/IEC 9899 */
165# define UNIT "s"
166#else
167 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
168 * -- cc on NeXTstep/OpenStep */
169# define UNIT "units"
170# define CLOCKS_PER_SEC 1
171#endif
172
173 if (type == TIMING_BASE_PT) {
174 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
175 "base point multiplications", (double)clck/CLOCKS_PER_SEC);
176 } else if (type == TIMING_RAND_PT) {
177 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
178 "random point multiplications", (double)clck/CLOCKS_PER_SEC);
179 } else if (type == TIMING_SIMUL) {
180 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
181 "s*P+t*Q operations", (double)clck/CLOCKS_PER_SEC);
182 }
183 fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
184
185 EC_POINT_free(P);
186 BN_free(s);
187 for (i = 0; i < 10; i++)
188 {
189 BN_free(r[i]);
190 if (type != TIMING_BASE_PT) BN_free(r0[i]);
191 }
192 }
193#endif
194
195/* test multiplication with group order, long and negative scalars */
196static void group_order_tests(EC_GROUP *group)
197 {
198 BIGNUM *n1, *n2, *order;
199 EC_POINT *P = EC_POINT_new(group);
200 EC_POINT *Q = EC_POINT_new(group);
201 BN_CTX *ctx = BN_CTX_new();
202
203 n1 = BN_new(); n2 = BN_new(); order = BN_new();
204 fprintf(stdout, "verify group order ...");
205 fflush(stdout);
206 if (!EC_GROUP_get_order(group, order, ctx)) ABORT;
207 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
208 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
209 fprintf(stdout, ".");
210 fflush(stdout);
211 if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
212 if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
213 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
214 fprintf(stdout, " ok\n");
215 fprintf(stdout, "long/negative scalar tests ... ");
216 if (!BN_one(n1)) ABORT;
217 /* n1 = 1 - order */
218 if (!BN_sub(n1, n1, order)) ABORT;
219 if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
220 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
221 /* n2 = 1 + order */
222 if (!BN_add(n2, order, BN_value_one())) ABORT;
223 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
224 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
225 /* n2 = (1 - order) * (1 + order) */
226 if (!BN_mul(n2, n1, n2, ctx)) ABORT;
227 if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
228 if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
229 fprintf(stdout, "ok\n");
230 EC_POINT_free(P);
231 EC_POINT_free(Q);
232 BN_free(n1);
233 BN_free(n2);
234 BN_free(order);
235 BN_CTX_free(ctx);
236 }
237
238static void prime_field_tests(void)
239 {
240 BN_CTX *ctx = NULL;
241 BIGNUM *p, *a, *b;
242 EC_GROUP *group;
243 EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
244 EC_POINT *P, *Q, *R;
245 BIGNUM *x, *y, *z;
246 unsigned char buf[100];
247 size_t i, len;
248 int k;
249
250#if 1 /* optional */
251 ctx = BN_CTX_new();
252 if (!ctx) ABORT;
253#endif
254
255 p = BN_new();
256 a = BN_new();
257 b = BN_new();
258 if (!p || !a || !b) ABORT;
259
260 if (!BN_hex2bn(&p, "17")) ABORT;
261 if (!BN_hex2bn(&a, "1")) ABORT;
262 if (!BN_hex2bn(&b, "1")) ABORT;
263
264 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
265 * so that the library gets to choose the EC_METHOD */
266 if (!group) ABORT;
267
268 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
269
270 {
271 EC_GROUP *tmp;
272 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
273 if (!tmp) ABORT;
274 if (!EC_GROUP_copy(tmp, group)) ABORT;
275 EC_GROUP_free(group);
276 group = tmp;
277 }
278
279 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
280
281 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
282 BN_print_fp(stdout, p);
283 fprintf(stdout, ")\n a = 0x");
284 BN_print_fp(stdout, a);
285 fprintf(stdout, "\n b = 0x");
286 BN_print_fp(stdout, b);
287 fprintf(stdout, "\n");
288
289 P = EC_POINT_new(group);
290 Q = EC_POINT_new(group);
291 R = EC_POINT_new(group);
292 if (!P || !Q || !R) ABORT;
293
294 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
295 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
296
297 buf[0] = 0;
298 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
299
300 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
301 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
302
303 x = BN_new();
304 y = BN_new();
305 z = BN_new();
306 if (!x || !y || !z) ABORT;
307
308 if (!BN_hex2bn(&x, "D")) ABORT;
309 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
310 if (!EC_POINT_is_on_curve(group, Q, ctx))
311 {
312 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
313 fprintf(stderr, "Point is not on curve: x = 0x");
314 BN_print_fp(stderr, x);
315 fprintf(stderr, ", y = 0x");
316 BN_print_fp(stderr, y);
317 fprintf(stderr, "\n");
318 ABORT;
319 }
320
321 fprintf(stdout, "A cyclic subgroup:\n");
322 k = 100;
323 do
324 {
325 if (k-- == 0) ABORT;
326
327 if (EC_POINT_is_at_infinity(group, P))
328 fprintf(stdout, " point at infinity\n");
329 else
330 {
331 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
332
333 fprintf(stdout, " x = 0x");
334 BN_print_fp(stdout, x);
335 fprintf(stdout, ", y = 0x");
336 BN_print_fp(stdout, y);
337 fprintf(stdout, "\n");
338 }
339
340 if (!EC_POINT_copy(R, P)) ABORT;
341 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
342
343#if 0 /* optional */
344 {
345 EC_POINT *points[3];
346
347 points[0] = R;
348 points[1] = Q;
349 points[2] = P;
350 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
351 }
352#endif
353
354 }
355 while (!EC_POINT_is_at_infinity(group, P));
356
357 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
358 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
359
360 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
361 if (len == 0) ABORT;
362 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
363 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
364 fprintf(stdout, "Generator as octet string, compressed form:\n ");
365 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
366
367 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
368 if (len == 0) ABORT;
369 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
370 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
371 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
372 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
373
374 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
375 if (len == 0) ABORT;
376 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
377 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
378 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
379 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
380
381 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
382 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
383 BN_print_fp(stdout, x);
384 fprintf(stdout, ", Y = 0x");
385 BN_print_fp(stdout, y);
386 fprintf(stdout, ", Z = 0x");
387 BN_print_fp(stdout, z);
388 fprintf(stdout, "\n");
389
390 if (!EC_POINT_invert(group, P, ctx)) ABORT;
391 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
392
393
394 /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
395 * -- not a NIST curve, but commonly used */
396
397 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
398 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
399 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
400 if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
401 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
402
403 if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
404 if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
405 if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
406 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
407 if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
408 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
409
410 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
411 fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
412 BN_print_fp(stdout, x);
413 fprintf(stdout, "\n y = 0x");
414 BN_print_fp(stdout, y);
415 fprintf(stdout, "\n");
416 /* G_y value taken from the standard: */
417 if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
418 if (0 != BN_cmp(y, z)) ABORT;
419
420 fprintf(stdout, "verify degree ...");
421 if (EC_GROUP_get_degree(group) != 160) ABORT;
422 fprintf(stdout, " ok\n");
423
424 group_order_tests(group);
425
426 if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
427 if (!EC_GROUP_copy(P_160, group)) ABORT;
428
429
430 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
431
432 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
433 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
434 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
435 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
436 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
437
438 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
439 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
440 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
441 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
442 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
443
444 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
445 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
446 BN_print_fp(stdout, x);
447 fprintf(stdout, "\n y = 0x");
448 BN_print_fp(stdout, y);
449 fprintf(stdout, "\n");
450 /* G_y value taken from the standard: */
451 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
452 if (0 != BN_cmp(y, z)) ABORT;
453
454 fprintf(stdout, "verify degree ...");
455 if (EC_GROUP_get_degree(group) != 192) ABORT;
456 fprintf(stdout, " ok\n");
457
458 group_order_tests(group);
459
460 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
461 if (!EC_GROUP_copy(P_192, group)) ABORT;
462
463
464 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
465
466 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
467 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
468 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
469 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
470 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
471
472 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
473 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
474 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
475 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
476 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
477
478 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
479 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
480 BN_print_fp(stdout, x);
481 fprintf(stdout, "\n y = 0x");
482 BN_print_fp(stdout, y);
483 fprintf(stdout, "\n");
484 /* G_y value taken from the standard: */
485 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
486 if (0 != BN_cmp(y, z)) ABORT;
487
488 fprintf(stdout, "verify degree ...");
489 if (EC_GROUP_get_degree(group) != 224) ABORT;
490 fprintf(stdout, " ok\n");
491
492 group_order_tests(group);
493
494 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
495 if (!EC_GROUP_copy(P_224, group)) ABORT;
496
497
498 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
499
500 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
501 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
502 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
503 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
504 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
505
506 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
507 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
508 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
509 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
510 "84F3B9CAC2FC632551")) ABORT;
511 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
512
513 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
514 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
515 BN_print_fp(stdout, x);
516 fprintf(stdout, "\n y = 0x");
517 BN_print_fp(stdout, y);
518 fprintf(stdout, "\n");
519 /* G_y value taken from the standard: */
520 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
521 if (0 != BN_cmp(y, z)) ABORT;
522
523 fprintf(stdout, "verify degree ...");
524 if (EC_GROUP_get_degree(group) != 256) ABORT;
525 fprintf(stdout, " ok\n");
526
527 group_order_tests(group);
528
529 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
530 if (!EC_GROUP_copy(P_256, group)) ABORT;
531
532
533 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
534
535 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
536 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
537 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
538 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
539 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
540 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
541 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
542 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
543
544 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
545 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
546 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
547 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
548 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
549 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
550 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
551
552 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
553 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
554 BN_print_fp(stdout, x);
555 fprintf(stdout, "\n y = 0x");
556 BN_print_fp(stdout, y);
557 fprintf(stdout, "\n");
558 /* G_y value taken from the standard: */
559 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
560 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
561 if (0 != BN_cmp(y, z)) ABORT;
562
563 fprintf(stdout, "verify degree ...");
564 if (EC_GROUP_get_degree(group) != 384) ABORT;
565 fprintf(stdout, " ok\n");
566
567 group_order_tests(group);
568
569 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
570 if (!EC_GROUP_copy(P_384, group)) ABORT;
571
572
573 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
574
575 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
576 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
577 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
578 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
579 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
580 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
581 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
582 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
583 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
584 "DF883D2C34F1EF451FD46B503F00")) ABORT;
585 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
586
587 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
588 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
589 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
590 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
591 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
592 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
593 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
594 "C9B8899C47AEBB6FB71E91386409")) ABORT;
595 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
596
597 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
598 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
599 BN_print_fp(stdout, x);
600 fprintf(stdout, "\n y = 0x");
601 BN_print_fp(stdout, y);
602 fprintf(stdout, "\n");
603 /* G_y value taken from the standard: */
604 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
605 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
606 "7086A272C24088BE94769FD16650")) ABORT;
607 if (0 != BN_cmp(y, z)) ABORT;
608
609 fprintf(stdout, "verify degree ...");
610 if (EC_GROUP_get_degree(group) != 521) ABORT;
611 fprintf(stdout, " ok\n");
612
613 group_order_tests(group);
614
615 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
616 if (!EC_GROUP_copy(P_521, group)) ABORT;
617
618
619 /* more tests using the last curve */
620
621 if (!EC_POINT_copy(Q, P)) ABORT;
622 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
623 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
624 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
625 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
626
627 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
628 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
629 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
630
631 {
632 const EC_POINT *points[4];
633 const BIGNUM *scalars[4];
634 BIGNUM scalar3;
635
636 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
637 points[0] = Q;
638 points[1] = Q;
639 points[2] = Q;
640 points[3] = Q;
641
642 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
643 if (!BN_add(y, z, BN_value_one())) ABORT;
644 if (BN_is_odd(y)) ABORT;
645 if (!BN_rshift1(y, y)) ABORT;
646 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
647 scalars[1] = y;
648
649 fprintf(stdout, "combined multiplication ...");
650 fflush(stdout);
651
652 /* z is still the group order */
653 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
654 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
655 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
656 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
657
658 fprintf(stdout, ".");
659 fflush(stdout);
660
661 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
662 if (!BN_add(z, z, y)) ABORT;
663 BN_set_negative(z, 1);
664 scalars[0] = y;
665 scalars[1] = z; /* z = -(order + y) */
666
667 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
668 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
669
670 fprintf(stdout, ".");
671 fflush(stdout);
672
673 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
674 if (!BN_add(z, x, y)) ABORT;
675 BN_set_negative(z, 1);
676 scalars[0] = x;
677 scalars[1] = y;
678 scalars[2] = z; /* z = -(x+y) */
679
680 BN_init(&scalar3);
681 BN_zero(&scalar3);
682 scalars[3] = &scalar3;
683
684 if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) ABORT;
685 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
686
687 fprintf(stdout, " ok\n\n");
688
689 BN_free(&scalar3);
690 }
691
692
693#if 0
694 timings(P_160, TIMING_BASE_PT, ctx);
695 timings(P_160, TIMING_RAND_PT, ctx);
696 timings(P_160, TIMING_SIMUL, ctx);
697 timings(P_192, TIMING_BASE_PT, ctx);
698 timings(P_192, TIMING_RAND_PT, ctx);
699 timings(P_192, TIMING_SIMUL, ctx);
700 timings(P_224, TIMING_BASE_PT, ctx);
701 timings(P_224, TIMING_RAND_PT, ctx);
702 timings(P_224, TIMING_SIMUL, ctx);
703 timings(P_256, TIMING_BASE_PT, ctx);
704 timings(P_256, TIMING_RAND_PT, ctx);
705 timings(P_256, TIMING_SIMUL, ctx);
706 timings(P_384, TIMING_BASE_PT, ctx);
707 timings(P_384, TIMING_RAND_PT, ctx);
708 timings(P_384, TIMING_SIMUL, ctx);
709 timings(P_521, TIMING_BASE_PT, ctx);
710 timings(P_521, TIMING_RAND_PT, ctx);
711 timings(P_521, TIMING_SIMUL, ctx);
712#endif
713
714
715 if (ctx)
716 BN_CTX_free(ctx);
717 BN_free(p); BN_free(a); BN_free(b);
718 EC_GROUP_free(group);
719 EC_POINT_free(P);
720 EC_POINT_free(Q);
721 EC_POINT_free(R);
722 BN_free(x); BN_free(y); BN_free(z);
723
724 if (P_160) EC_GROUP_free(P_160);
725 if (P_192) EC_GROUP_free(P_192);
726 if (P_224) EC_GROUP_free(P_224);
727 if (P_256) EC_GROUP_free(P_256);
728 if (P_384) EC_GROUP_free(P_384);
729 if (P_521) EC_GROUP_free(P_521);
730
731 }
732
733/* Change test based on whether binary point compression is enabled or not. */
734#ifdef OPENSSL_EC_BIN_PT_COMP
735#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
736 if (!BN_hex2bn(&x, _x)) ABORT; \
737 if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
738 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
739 if (!BN_hex2bn(&z, _order)) ABORT; \
740 if (!BN_hex2bn(&cof, _cof)) ABORT; \
741 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
742 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
743 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
744 BN_print_fp(stdout, x); \
745 fprintf(stdout, "\n y = 0x"); \
746 BN_print_fp(stdout, y); \
747 fprintf(stdout, "\n"); \
748 /* G_y value taken from the standard: */ \
749 if (!BN_hex2bn(&z, _y)) ABORT; \
750 if (0 != BN_cmp(y, z)) ABORT;
751#else
752#define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
753 if (!BN_hex2bn(&x, _x)) ABORT; \
754 if (!BN_hex2bn(&y, _y)) ABORT; \
755 if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
756 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
757 if (!BN_hex2bn(&z, _order)) ABORT; \
758 if (!BN_hex2bn(&cof, _cof)) ABORT; \
759 if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
760 fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
761 BN_print_fp(stdout, x); \
762 fprintf(stdout, "\n y = 0x"); \
763 BN_print_fp(stdout, y); \
764 fprintf(stdout, "\n");
765#endif
766
767#define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
768 if (!BN_hex2bn(&p, _p)) ABORT; \
769 if (!BN_hex2bn(&a, _a)) ABORT; \
770 if (!BN_hex2bn(&b, _b)) ABORT; \
771 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
772 CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
773 fprintf(stdout, "verify degree ..."); \
774 if (EC_GROUP_get_degree(group) != _degree) ABORT; \
775 fprintf(stdout, " ok\n"); \
776 group_order_tests(group); \
777 if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
778 if (!EC_GROUP_copy(_variable, group)) ABORT; \
779
780#ifndef OPENSSL_NO_EC2M
781
782static void char2_field_tests(void)
783 {
784 BN_CTX *ctx = NULL;
785 BIGNUM *p, *a, *b;
786 EC_GROUP *group;
787 EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
788 EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
789 EC_POINT *P, *Q, *R;
790 BIGNUM *x, *y, *z, *cof;
791 unsigned char buf[100];
792 size_t i, len;
793 int k;
794
795#if 1 /* optional */
796 ctx = BN_CTX_new();
797 if (!ctx) ABORT;
798#endif
799
800 p = BN_new();
801 a = BN_new();
802 b = BN_new();
803 if (!p || !a || !b) ABORT;
804
805 if (!BN_hex2bn(&p, "13")) ABORT;
806 if (!BN_hex2bn(&a, "3")) ABORT;
807 if (!BN_hex2bn(&b, "1")) ABORT;
808
809 group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
810 * so that the library gets to choose the EC_METHOD */
811 if (!group) ABORT;
812 if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
813
814 {
815 EC_GROUP *tmp;
816 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
817 if (!tmp) ABORT;
818 if (!EC_GROUP_copy(tmp, group)) ABORT;
819 EC_GROUP_free(group);
820 group = tmp;
821 }
822
823 if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
824
825 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
826 BN_print_fp(stdout, p);
827 fprintf(stdout, ")\n a = 0x");
828 BN_print_fp(stdout, a);
829 fprintf(stdout, "\n b = 0x");
830 BN_print_fp(stdout, b);
831 fprintf(stdout, "\n(0x... means binary polynomial)\n");
832
833 P = EC_POINT_new(group);
834 Q = EC_POINT_new(group);
835 R = EC_POINT_new(group);
836 if (!P || !Q || !R) ABORT;
837
838 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
839 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
840
841 buf[0] = 0;
842 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
843
844 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
845 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
846
847 x = BN_new();
848 y = BN_new();
849 z = BN_new();
850 cof = BN_new();
851 if (!x || !y || !z || !cof) ABORT;
852
853 if (!BN_hex2bn(&x, "6")) ABORT;
854/* Change test based on whether binary point compression is enabled or not. */
855#ifdef OPENSSL_EC_BIN_PT_COMP
856 if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
857#else
858 if (!BN_hex2bn(&y, "8")) ABORT;
859 if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
860#endif
861 if (!EC_POINT_is_on_curve(group, Q, ctx))
862 {
863/* Change test based on whether binary point compression is enabled or not. */
864#ifdef OPENSSL_EC_BIN_PT_COMP
865 if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
866#endif
867 fprintf(stderr, "Point is not on curve: x = 0x");
868 BN_print_fp(stderr, x);
869 fprintf(stderr, ", y = 0x");
870 BN_print_fp(stderr, y);
871 fprintf(stderr, "\n");
872 ABORT;
873 }
874
875 fprintf(stdout, "A cyclic subgroup:\n");
876 k = 100;
877 do
878 {
879 if (k-- == 0) ABORT;
880
881 if (EC_POINT_is_at_infinity(group, P))
882 fprintf(stdout, " point at infinity\n");
883 else
884 {
885 if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
886
887 fprintf(stdout, " x = 0x");
888 BN_print_fp(stdout, x);
889 fprintf(stdout, ", y = 0x");
890 BN_print_fp(stdout, y);
891 fprintf(stdout, "\n");
892 }
893
894 if (!EC_POINT_copy(R, P)) ABORT;
895 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
896 }
897 while (!EC_POINT_is_at_infinity(group, P));
898
899 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
900 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
901
902/* Change test based on whether binary point compression is enabled or not. */
903#ifdef OPENSSL_EC_BIN_PT_COMP
904 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
905 if (len == 0) ABORT;
906 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
907 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
908 fprintf(stdout, "Generator as octet string, compressed form:\n ");
909 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
910#endif
911
912 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
913 if (len == 0) ABORT;
914 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
915 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
916 fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
917 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
918
919/* Change test based on whether binary point compression is enabled or not. */
920#ifdef OPENSSL_EC_BIN_PT_COMP
921 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
922 if (len == 0) ABORT;
923 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
924 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
925 fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
926 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
927#endif
928
929 fprintf(stdout, "\n");
930
931 if (!EC_POINT_invert(group, P, ctx)) ABORT;
932 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
933
934
935 /* Curve K-163 (FIPS PUB 186-2, App. 6) */
936 CHAR2_CURVE_TEST
937 (
938 "NIST curve K-163",
939 "0800000000000000000000000000000000000000C9",
940 "1",
941 "1",
942 "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
943 "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
944 1,
945 "04000000000000000000020108A2E0CC0D99F8A5EF",
946 "2",
947 163,
948 C2_K163
949 );
950
951 /* Curve B-163 (FIPS PUB 186-2, App. 6) */
952 CHAR2_CURVE_TEST
953 (
954 "NIST curve B-163",
955 "0800000000000000000000000000000000000000C9",
956 "1",
957 "020A601907B8C953CA1481EB10512F78744A3205FD",
958 "03F0EBA16286A2D57EA0991168D4994637E8343E36",
959 "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
960 1,
961 "040000000000000000000292FE77E70C12A4234C33",
962 "2",
963 163,
964 C2_B163
965 );
966
967 /* Curve K-233 (FIPS PUB 186-2, App. 6) */
968 CHAR2_CURVE_TEST
969 (
970 "NIST curve K-233",
971 "020000000000000000000000000000000000000004000000000000000001",
972 "0",
973 "1",
974 "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
975 "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
976 0,
977 "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
978 "4",
979 233,
980 C2_K233
981 );
982
983 /* Curve B-233 (FIPS PUB 186-2, App. 6) */
984 CHAR2_CURVE_TEST
985 (
986 "NIST curve B-233",
987 "020000000000000000000000000000000000000004000000000000000001",
988 "000000000000000000000000000000000000000000000000000000000001",
989 "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
990 "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
991 "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
992 1,
993 "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
994 "2",
995 233,
996 C2_B233
997 );
998
999 /* Curve K-283 (FIPS PUB 186-2, App. 6) */
1000 CHAR2_CURVE_TEST
1001 (
1002 "NIST curve K-283",
1003 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1004 "0",
1005 "1",
1006 "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
1007 "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
1008 0,
1009 "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
1010 "4",
1011 283,
1012 C2_K283
1013 );
1014
1015 /* Curve B-283 (FIPS PUB 186-2, App. 6) */
1016 CHAR2_CURVE_TEST
1017 (
1018 "NIST curve B-283",
1019 "0800000000000000000000000000000000000000000000000000000000000000000010A1",
1020 "000000000000000000000000000000000000000000000000000000000000000000000001",
1021 "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
1022 "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
1023 "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
1024 1,
1025 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
1026 "2",
1027 283,
1028 C2_B283
1029 );
1030
1031 /* Curve K-409 (FIPS PUB 186-2, App. 6) */
1032 CHAR2_CURVE_TEST
1033 (
1034 "NIST curve K-409",
1035 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1036 "0",
1037 "1",
1038 "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
1039 "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
1040 1,
1041 "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
1042 "4",
1043 409,
1044 C2_K409
1045 );
1046
1047 /* Curve B-409 (FIPS PUB 186-2, App. 6) */
1048 CHAR2_CURVE_TEST
1049 (
1050 "NIST curve B-409",
1051 "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
1052 "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1053 "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
1054 "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
1055 "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
1056 1,
1057 "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
1058 "2",
1059 409,
1060 C2_B409
1061 );
1062
1063 /* Curve K-571 (FIPS PUB 186-2, App. 6) */
1064 CHAR2_CURVE_TEST
1065 (
1066 "NIST curve K-571",
1067 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1068 "0",
1069 "1",
1070 "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
1071 "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
1072 0,
1073 "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
1074 "4",
1075 571,
1076 C2_K571
1077 );
1078
1079 /* Curve B-571 (FIPS PUB 186-2, App. 6) */
1080 CHAR2_CURVE_TEST
1081 (
1082 "NIST curve B-571",
1083 "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
1084 "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
1085 "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
1086 "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
1087 "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
1088 1,
1089 "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
1090 "2",
1091 571,
1092 C2_B571
1093 );
1094
1095 /* more tests using the last curve */
1096
1097 if (!EC_POINT_copy(Q, P)) ABORT;
1098 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1099 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
1100 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
1101 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
1102
1103 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
1104 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
1105 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
1106
1107 {
1108 const EC_POINT *points[3];
1109 const BIGNUM *scalars[3];
1110
1111 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
1112 points[0] = Q;
1113 points[1] = Q;
1114 points[2] = Q;
1115
1116 if (!BN_add(y, z, BN_value_one())) ABORT;
1117 if (BN_is_odd(y)) ABORT;
1118 if (!BN_rshift1(y, y)) ABORT;
1119 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
1120 scalars[1] = y;
1121
1122 fprintf(stdout, "combined multiplication ...");
1123 fflush(stdout);
1124
1125 /* z is still the group order */
1126 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1127 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
1128 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
1129 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
1130
1131 fprintf(stdout, ".");
1132 fflush(stdout);
1133
1134 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
1135 if (!BN_add(z, z, y)) ABORT;
1136 BN_set_negative(z, 1);
1137 scalars[0] = y;
1138 scalars[1] = z; /* z = -(order + y) */
1139
1140 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
1141 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1142
1143 fprintf(stdout, ".");
1144 fflush(stdout);
1145
1146 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
1147 if (!BN_add(z, x, y)) ABORT;
1148 BN_set_negative(z, 1);
1149 scalars[0] = x;
1150 scalars[1] = y;
1151 scalars[2] = z; /* z = -(x+y) */
1152
1153 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
1154 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
1155
1156 fprintf(stdout, " ok\n\n");
1157 }
1158
1159
1160#if 0
1161 timings(C2_K163, TIMING_BASE_PT, ctx);
1162 timings(C2_K163, TIMING_RAND_PT, ctx);
1163 timings(C2_K163, TIMING_SIMUL, ctx);
1164 timings(C2_B163, TIMING_BASE_PT, ctx);
1165 timings(C2_B163, TIMING_RAND_PT, ctx);
1166 timings(C2_B163, TIMING_SIMUL, ctx);
1167 timings(C2_K233, TIMING_BASE_PT, ctx);
1168 timings(C2_K233, TIMING_RAND_PT, ctx);
1169 timings(C2_K233, TIMING_SIMUL, ctx);
1170 timings(C2_B233, TIMING_BASE_PT, ctx);
1171 timings(C2_B233, TIMING_RAND_PT, ctx);
1172 timings(C2_B233, TIMING_SIMUL, ctx);
1173 timings(C2_K283, TIMING_BASE_PT, ctx);
1174 timings(C2_K283, TIMING_RAND_PT, ctx);
1175 timings(C2_K283, TIMING_SIMUL, ctx);
1176 timings(C2_B283, TIMING_BASE_PT, ctx);
1177 timings(C2_B283, TIMING_RAND_PT, ctx);
1178 timings(C2_B283, TIMING_SIMUL, ctx);
1179 timings(C2_K409, TIMING_BASE_PT, ctx);
1180 timings(C2_K409, TIMING_RAND_PT, ctx);
1181 timings(C2_K409, TIMING_SIMUL, ctx);
1182 timings(C2_B409, TIMING_BASE_PT, ctx);
1183 timings(C2_B409, TIMING_RAND_PT, ctx);
1184 timings(C2_B409, TIMING_SIMUL, ctx);
1185 timings(C2_K571, TIMING_BASE_PT, ctx);
1186 timings(C2_K571, TIMING_RAND_PT, ctx);
1187 timings(C2_K571, TIMING_SIMUL, ctx);
1188 timings(C2_B571, TIMING_BASE_PT, ctx);
1189 timings(C2_B571, TIMING_RAND_PT, ctx);
1190 timings(C2_B571, TIMING_SIMUL, ctx);
1191#endif
1192
1193
1194 if (ctx)
1195 BN_CTX_free(ctx);
1196 BN_free(p); BN_free(a); BN_free(b);
1197 EC_GROUP_free(group);
1198 EC_POINT_free(P);
1199 EC_POINT_free(Q);
1200 EC_POINT_free(R);
1201 BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
1202
1203 if (C2_K163) EC_GROUP_free(C2_K163);
1204 if (C2_B163) EC_GROUP_free(C2_B163);
1205 if (C2_K233) EC_GROUP_free(C2_K233);
1206 if (C2_B233) EC_GROUP_free(C2_B233);
1207 if (C2_K283) EC_GROUP_free(C2_K283);
1208 if (C2_B283) EC_GROUP_free(C2_B283);
1209 if (C2_K409) EC_GROUP_free(C2_K409);
1210 if (C2_B409) EC_GROUP_free(C2_B409);
1211 if (C2_K571) EC_GROUP_free(C2_K571);
1212 if (C2_B571) EC_GROUP_free(C2_B571);
1213
1214 }
1215#endif
1216
1217static void internal_curve_test(void)
1218 {
1219 EC_builtin_curve *curves = NULL;
1220 size_t crv_len = 0, n = 0;
1221 int ok = 1;
1222
1223 crv_len = EC_get_builtin_curves(NULL, 0);
1224
1225 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
1226
1227 if (curves == NULL)
1228 return;
1229
1230 if (!EC_get_builtin_curves(curves, crv_len))
1231 {
1232 OPENSSL_free(curves);
1233 return;
1234 }
1235
1236 fprintf(stdout, "testing internal curves: ");
1237
1238 for (n = 0; n < crv_len; n++)
1239 {
1240 EC_GROUP *group = NULL;
1241 int nid = curves[n].nid;
1242 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL)
1243 {
1244 ok = 0;
1245 fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
1246 " curve %s\n", OBJ_nid2sn(nid));
1247 /* try next curve */
1248 continue;
1249 }
1250 if (!EC_GROUP_check(group, NULL))
1251 {
1252 ok = 0;
1253 fprintf(stdout, "\nEC_GROUP_check() failed with"
1254 " curve %s\n", OBJ_nid2sn(nid));
1255 EC_GROUP_free(group);
1256 /* try the next curve */
1257 continue;
1258 }
1259 fprintf(stdout, ".");
1260 fflush(stdout);
1261 EC_GROUP_free(group);
1262 }
1263 if (ok)
1264 fprintf(stdout, " ok\n\n");
1265 else
1266 {
1267 fprintf(stdout, " failed\n\n");
1268 ABORT;
1269 }
1270 OPENSSL_free(curves);
1271 return;
1272 }
1273
1274#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1275/* nistp_test_params contains magic numbers for testing our optimized
1276 * implementations of several NIST curves with characteristic > 3. */
1277struct nistp_test_params
1278 {
1279 const EC_METHOD* (*meth) ();
1280 int degree;
1281 /* Qx, Qy and D are taken from
1282 * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
1283 * Otherwise, values are standard curve parameters from FIPS 180-3 */
1284 const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
1285 };
1286
1287static const struct nistp_test_params nistp_tests_params[] =
1288 {
1289 {
1290 /* P-224 */
1291 EC_GFp_nistp224_method,
1292 224,
1293 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* p */
1294 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* a */
1295 "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* b */
1296 "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", /* Qx */
1297 "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", /* Qy */
1298 "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
1299 "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
1300 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
1301 "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", /* d */
1302 },
1303 {
1304 /* P-256 */
1305 EC_GFp_nistp256_method,
1306 256,
1307 "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", /* p */
1308 "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", /* a */
1309 "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", /* b */
1310 "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", /* Qx */
1311 "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", /* Qy */
1312 "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", /* Gx */
1313 "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", /* Gy */
1314 "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", /* order */
1315 "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", /* d */
1316 },
1317 {
1318 /* P-521 */
1319 EC_GFp_nistp521_method,
1320 521,
1321 "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", /* p */
1322 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", /* a */
1323 "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", /* b */
1324 "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", /* Qx */
1325 "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", /* Qy */
1326 "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", /* Gx */
1327 "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", /* Gy */
1328 "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", /* order */
1329 "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", /* d */
1330 },
1331 };
1332
1333void nistp_single_test(const struct nistp_test_params *test)
1334 {
1335 BN_CTX *ctx;
1336 BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
1337 EC_GROUP *NISTP;
1338 EC_POINT *G, *P, *Q, *Q_CHECK;
1339
1340 fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n", test->degree);
1341 ctx = BN_CTX_new();
1342 p = BN_new();
1343 a = BN_new();
1344 b = BN_new();
1345 x = BN_new(); y = BN_new();
1346 m = BN_new(); n = BN_new(); order = BN_new();
1347
1348 NISTP = EC_GROUP_new(test->meth());
1349 if(!NISTP) ABORT;
1350 if (!BN_hex2bn(&p, test->p)) ABORT;
1351 if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
1352 if (!BN_hex2bn(&a, test->a)) ABORT;
1353 if (!BN_hex2bn(&b, test->b)) ABORT;
1354 if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) ABORT;
1355 G = EC_POINT_new(NISTP);
1356 P = EC_POINT_new(NISTP);
1357 Q = EC_POINT_new(NISTP);
1358 Q_CHECK = EC_POINT_new(NISTP);
1359 if(!BN_hex2bn(&x, test->Qx)) ABORT;
1360 if(!BN_hex2bn(&y, test->Qy)) ABORT;
1361 if(!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx)) ABORT;
1362 if (!BN_hex2bn(&x, test->Gx)) ABORT;
1363 if (!BN_hex2bn(&y, test->Gy)) ABORT;
1364 if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) ABORT;
1365 if (!BN_hex2bn(&order, test->order)) ABORT;
1366 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1367
1368 fprintf(stdout, "verify degree ... ");
1369 if (EC_GROUP_get_degree(NISTP) != test->degree) ABORT;
1370 fprintf(stdout, "ok\n");
1371
1372 fprintf(stdout, "NIST test vectors ... ");
1373 if (!BN_hex2bn(&n, test->d)) ABORT;
1374 /* fixed point multiplication */
1375 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1376 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1377 /* random point multiplication */
1378 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1379 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1380
1381 /* set generator to P = 2*G, where G is the standard generator */
1382 if (!EC_POINT_dbl(NISTP, P, G, ctx)) ABORT;
1383 if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) ABORT;
1384 /* set the scalar to m=n/2, where n is the NIST test scalar */
1385 if (!BN_rshift(m, n, 1)) ABORT;
1386
1387 /* test the non-standard generator */
1388 /* fixed point multiplication */
1389 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1390 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1391 /* random point multiplication */
1392 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1393 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1394
1395 /* now repeat all tests with precomputation */
1396 if (!EC_GROUP_precompute_mult(NISTP, ctx)) ABORT;
1397
1398 /* fixed point multiplication */
1399 EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
1400 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1401 /* random point multiplication */
1402 EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
1403 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1404
1405 /* reset generator */
1406 if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
1407 /* fixed point multiplication */
1408 EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
1409 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1410 /* random point multiplication */
1411 EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
1412 if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
1413
1414 fprintf(stdout, "ok\n");
1415 group_order_tests(NISTP);
1416#if 0
1417 timings(NISTP, TIMING_BASE_PT, ctx);
1418 timings(NISTP, TIMING_RAND_PT, ctx);
1419#endif
1420 EC_GROUP_free(NISTP);
1421 EC_POINT_free(G);
1422 EC_POINT_free(P);
1423 EC_POINT_free(Q);
1424 EC_POINT_free(Q_CHECK);
1425 BN_free(n);
1426 BN_free(m);
1427 BN_free(p);
1428 BN_free(a);
1429 BN_free(b);
1430 BN_free(x);
1431 BN_free(y);
1432 BN_free(order);
1433 BN_CTX_free(ctx);
1434 }
1435
1436void nistp_tests()
1437 {
1438 unsigned i;
1439
1440 for (i = 0; i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params); i++)
1441 {
1442 nistp_single_test(&nistp_tests_params[i]);
1443 }
1444 }
1445#endif
1446
1447static const char rnd_seed[] = "string to make the random number generator think it has entropy";
1448
1449int main(int argc, char *argv[])
1450 {
1451
1452 /* enable memory leak checking unless explicitly disabled */
1453 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
1454 {
1455 CRYPTO_malloc_debug_init();
1456 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1457 }
1458 else
1459 {
1460 /* OPENSSL_DEBUG_MEMORY=off */
1461 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
1462 }
1463 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
1464 ERR_load_crypto_strings();
1465
1466 RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
1467
1468 prime_field_tests();
1469 puts("");
1470#ifndef OPENSSL_NO_EC2M
1471 char2_field_tests();
1472#endif
1473#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
1474 nistp_tests();
1475#endif
1476 /* test the internal curves */
1477 internal_curve_test();
1478
1479#ifndef OPENSSL_NO_ENGINE
1480 ENGINE_cleanup();
1481#endif
1482 CRYPTO_cleanup_all_ex_data();
1483 ERR_free_strings();
1484 ERR_remove_thread_state(NULL);
1485 CRYPTO_mem_leaks_fp(stderr);
1486
1487 return 0;
1488 }
1489#endif
diff --git a/src/lib/libssl/src/crypto/ecdh/ecdhtest.c b/src/lib/libssl/src/crypto/ecdh/ecdhtest.c
deleted file mode 100644
index 823d7baa65..0000000000
--- a/src/lib/libssl/src/crypto/ecdh/ecdhtest.c
+++ /dev/null
@@ -1,374 +0,0 @@
1/* crypto/ecdh/ecdhtest.c */
2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 *
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
8 *
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
11 *
12 * The ECDH software is originally written by Douglas Stebila of
13 * Sun Microsystems Laboratories.
14 *
15 */
16/* ====================================================================
17 * Copyright (c) 1998-2003 The OpenSSL Project. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 *
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 *
31 * 3. All advertising materials mentioning features or use of this
32 * software must display the following acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
35 *
36 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37 * endorse or promote products derived from this software without
38 * prior written permission. For written permission, please contact
39 * openssl-core@openssl.org.
40 *
41 * 5. Products derived from this software may not be called "OpenSSL"
42 * nor may "OpenSSL" appear in their names without prior written
43 * permission of the OpenSSL Project.
44 *
45 * 6. Redistributions of any form whatsoever must retain the following
46 * acknowledgment:
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61 * OF THE POSSIBILITY OF SUCH DAMAGE.
62 * ====================================================================
63 *
64 * This product includes cryptographic software written by Eric Young
65 * (eay@cryptsoft.com). This product includes software written by Tim
66 * Hudson (tjh@cryptsoft.com).
67 *
68 */
69
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73
74#include "../e_os.h"
75
76#include <openssl/opensslconf.h> /* for OPENSSL_NO_ECDH */
77#include <openssl/crypto.h>
78#include <openssl/bio.h>
79#include <openssl/bn.h>
80#include <openssl/objects.h>
81#include <openssl/rand.h>
82#include <openssl/sha.h>
83#include <openssl/err.h>
84
85#ifdef OPENSSL_NO_ECDH
86int main(int argc, char *argv[])
87{
88 printf("No ECDH support\n");
89 return(0);
90}
91#else
92#include <openssl/ec.h>
93#include <openssl/ecdh.h>
94
95#ifdef OPENSSL_SYS_WIN16
96#define MS_CALLBACK _far _loadds
97#else
98#define MS_CALLBACK
99#endif
100
101#if 0
102static void MS_CALLBACK cb(int p, int n, void *arg);
103#endif
104
105static const char rnd_seed[] = "string to make the random number generator think it has entropy";
106
107
108static const int KDF1_SHA1_len = 20;
109static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
110 {
111#ifndef OPENSSL_NO_SHA
112 if (*outlen < SHA_DIGEST_LENGTH)
113 return NULL;
114 else
115 *outlen = SHA_DIGEST_LENGTH;
116 return SHA1(in, inlen, out);
117#else
118 return NULL;
119#endif
120 }
121
122
123static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
124 {
125 EC_KEY *a=NULL;
126 EC_KEY *b=NULL;
127 BIGNUM *x_a=NULL, *y_a=NULL,
128 *x_b=NULL, *y_b=NULL;
129 char buf[12];
130 unsigned char *abuf=NULL,*bbuf=NULL;
131 int i,alen,blen,aout,bout,ret=0;
132 const EC_GROUP *group;
133
134 a = EC_KEY_new_by_curve_name(nid);
135 b = EC_KEY_new_by_curve_name(nid);
136 if (a == NULL || b == NULL)
137 goto err;
138
139 group = EC_KEY_get0_group(a);
140
141 if ((x_a=BN_new()) == NULL) goto err;
142 if ((y_a=BN_new()) == NULL) goto err;
143 if ((x_b=BN_new()) == NULL) goto err;
144 if ((y_b=BN_new()) == NULL) goto err;
145
146 BIO_puts(out,"Testing key generation with ");
147 BIO_puts(out,text);
148#ifdef NOISY
149 BIO_puts(out,"\n");
150#else
151 (void)BIO_flush(out);
152#endif
153
154 if (!EC_KEY_generate_key(a)) goto err;
155
156 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
157 {
158 if (!EC_POINT_get_affine_coordinates_GFp(group,
159 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
160 }
161#ifndef OPENSSL_NO_EC2M
162 else
163 {
164 if (!EC_POINT_get_affine_coordinates_GF2m(group,
165 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
166 }
167#endif
168#ifdef NOISY
169 BIO_puts(out," pri 1=");
170 BN_print(out,a->priv_key);
171 BIO_puts(out,"\n pub 1=");
172 BN_print(out,x_a);
173 BIO_puts(out,",");
174 BN_print(out,y_a);
175 BIO_puts(out,"\n");
176#else
177 BIO_printf(out," .");
178 (void)BIO_flush(out);
179#endif
180
181 if (!EC_KEY_generate_key(b)) goto err;
182
183 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
184 {
185 if (!EC_POINT_get_affine_coordinates_GFp(group,
186 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
187 }
188#ifndef OPENSSL_NO_EC2M
189 else
190 {
191 if (!EC_POINT_get_affine_coordinates_GF2m(group,
192 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
193 }
194#endif
195
196#ifdef NOISY
197 BIO_puts(out," pri 2=");
198 BN_print(out,b->priv_key);
199 BIO_puts(out,"\n pub 2=");
200 BN_print(out,x_b);
201 BIO_puts(out,",");
202 BN_print(out,y_b);
203 BIO_puts(out,"\n");
204#else
205 BIO_printf(out,".");
206 (void)BIO_flush(out);
207#endif
208
209 alen=KDF1_SHA1_len;
210 abuf=(unsigned char *)OPENSSL_malloc(alen);
211 aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1);
212
213#ifdef NOISY
214 BIO_puts(out," key1 =");
215 for (i=0; i<aout; i++)
216 {
217 sprintf(buf,"%02X",abuf[i]);
218 BIO_puts(out,buf);
219 }
220 BIO_puts(out,"\n");
221#else
222 BIO_printf(out,".");
223 (void)BIO_flush(out);
224#endif
225
226 blen=KDF1_SHA1_len;
227 bbuf=(unsigned char *)OPENSSL_malloc(blen);
228 bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1);
229
230#ifdef NOISY
231 BIO_puts(out," key2 =");
232 for (i=0; i<bout; i++)
233 {
234 sprintf(buf,"%02X",bbuf[i]);
235 BIO_puts(out,buf);
236 }
237 BIO_puts(out,"\n");
238#else
239 BIO_printf(out,".");
240 (void)BIO_flush(out);
241#endif
242
243 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0))
244 {
245#ifndef NOISY
246 BIO_printf(out, " failed\n\n");
247 BIO_printf(out, "key a:\n");
248 BIO_printf(out, "private key: ");
249 BN_print(out, EC_KEY_get0_private_key(a));
250 BIO_printf(out, "\n");
251 BIO_printf(out, "public key (x,y): ");
252 BN_print(out, x_a);
253 BIO_printf(out, ",");
254 BN_print(out, y_a);
255 BIO_printf(out, "\nkey b:\n");
256 BIO_printf(out, "private key: ");
257 BN_print(out, EC_KEY_get0_private_key(b));
258 BIO_printf(out, "\n");
259 BIO_printf(out, "public key (x,y): ");
260 BN_print(out, x_b);
261 BIO_printf(out, ",");
262 BN_print(out, y_b);
263 BIO_printf(out, "\n");
264 BIO_printf(out, "generated key a: ");
265 for (i=0; i<bout; i++)
266 {
267 sprintf(buf, "%02X", bbuf[i]);
268 BIO_puts(out, buf);
269 }
270 BIO_printf(out, "\n");
271 BIO_printf(out, "generated key b: ");
272 for (i=0; i<aout; i++)
273 {
274 sprintf(buf, "%02X", abuf[i]);
275 BIO_puts(out,buf);
276 }
277 BIO_printf(out, "\n");
278#endif
279 fprintf(stderr,"Error in ECDH routines\n");
280 ret=0;
281 }
282 else
283 {
284#ifndef NOISY
285 BIO_printf(out, " ok\n");
286#endif
287 ret=1;
288 }
289err:
290 ERR_print_errors_fp(stderr);
291
292 if (abuf != NULL) OPENSSL_free(abuf);
293 if (bbuf != NULL) OPENSSL_free(bbuf);
294 if (x_a) BN_free(x_a);
295 if (y_a) BN_free(y_a);
296 if (x_b) BN_free(x_b);
297 if (y_b) BN_free(y_b);
298 if (b) EC_KEY_free(b);
299 if (a) EC_KEY_free(a);
300 return(ret);
301 }
302
303int main(int argc, char *argv[])
304 {
305 BN_CTX *ctx=NULL;
306 int ret=1;
307 BIO *out;
308
309 CRYPTO_malloc_debug_init();
310 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
311 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
312
313#ifdef OPENSSL_SYS_WIN32
314 CRYPTO_malloc_init();
315#endif
316
317 RAND_seed(rnd_seed, sizeof rnd_seed);
318
319 out=BIO_new(BIO_s_file());
320 if (out == NULL) EXIT(1);
321 BIO_set_fp(out,stdout,BIO_NOCLOSE);
322
323 if ((ctx=BN_CTX_new()) == NULL) goto err;
324
325 /* NIST PRIME CURVES TESTS */
326 if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err;
327 if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err;
328 if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err;
329 if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err;
330 if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err;
331#ifndef OPENSSL_NO_EC2M
332 /* NIST BINARY CURVES TESTS */
333 if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err;
334 if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err;
335 if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err;
336 if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err;
337 if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err;
338 if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err;
339 if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err;
340 if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err;
341 if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err;
342 if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err;
343#endif
344
345 ret = 0;
346
347err:
348 ERR_print_errors_fp(stderr);
349 if (ctx) BN_CTX_free(ctx);
350 BIO_free(out);
351 CRYPTO_cleanup_all_ex_data();
352 ERR_remove_thread_state(NULL);
353 CRYPTO_mem_leaks_fp(stderr);
354 EXIT(ret);
355 return(ret);
356 }
357
358#if 0
359static void MS_CALLBACK cb(int p, int n, void *arg)
360 {
361 char c='*';
362
363 if (p == 0) c='.';
364 if (p == 1) c='+';
365 if (p == 2) c='*';
366 if (p == 3) c='\n';
367 BIO_write((BIO *)arg,&c,1);
368 (void)BIO_flush((BIO *)arg);
369#ifdef LINT
370 p=n;
371#endif
372 }
373#endif
374#endif
diff --git a/src/lib/libssl/src/crypto/ecdsa/ecdsatest.c b/src/lib/libssl/src/crypto/ecdsa/ecdsatest.c
deleted file mode 100644
index 537bb30362..0000000000
--- a/src/lib/libssl/src/crypto/ecdsa/ecdsatest.c
+++ /dev/null
@@ -1,572 +0,0 @@
1/* crypto/ecdsa/ecdsatest.c */
2/*
3 * Written by Nils Larsch for the OpenSSL project.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 *
61 * Portions of the attached software ("Contribution") are developed by
62 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63 *
64 * The Contribution is licensed pursuant to the OpenSSL open source
65 * license provided above.
66 *
67 * The elliptic curve binary polynomial software is originally written by
68 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69 *
70 */
71
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75
76#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_ECDSA is defined */
77
78#ifdef OPENSSL_NO_ECDSA
79int main(int argc, char * argv[])
80 {
81 puts("Elliptic curves are disabled.");
82 return 0;
83 }
84#else
85
86#include <openssl/crypto.h>
87#include <openssl/bio.h>
88#include <openssl/evp.h>
89#include <openssl/bn.h>
90#include <openssl/ecdsa.h>
91#ifndef OPENSSL_NO_ENGINE
92#include <openssl/engine.h>
93#endif
94#include <openssl/err.h>
95#include <openssl/rand.h>
96
97static const char rnd_seed[] = "string to make the random number generator "
98 "think it has entropy";
99
100/* declaration of the test functions */
101int x9_62_tests(BIO *);
102int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
103int test_builtin(BIO *);
104
105/* functions to change the RAND_METHOD */
106int change_rand(void);
107int restore_rand(void);
108int fbytes(unsigned char *buf, int num);
109
110RAND_METHOD fake_rand;
111const RAND_METHOD *old_rand;
112
113int change_rand(void)
114 {
115 /* save old rand method */
116 if ((old_rand = RAND_get_rand_method()) == NULL)
117 return 0;
118
119 fake_rand.seed = old_rand->seed;
120 fake_rand.cleanup = old_rand->cleanup;
121 fake_rand.add = old_rand->add;
122 fake_rand.status = old_rand->status;
123 /* use own random function */
124 fake_rand.bytes = fbytes;
125 fake_rand.pseudorand = old_rand->bytes;
126 /* set new RAND_METHOD */
127 if (!RAND_set_rand_method(&fake_rand))
128 return 0;
129 return 1;
130 }
131
132int restore_rand(void)
133 {
134 if (!RAND_set_rand_method(old_rand))
135 return 0;
136 else
137 return 1;
138 }
139
140static int fbytes_counter = 0;
141static const char *numbers[8] = {
142 "651056770906015076056810763456358567190100156695615665659",
143 "6140507067065001063065065565667405560006161556565665656654",
144 "8763001015071075675010661307616710783570106710677817767166"
145 "71676178726717",
146 "7000000175690566466555057817571571075705015757757057795755"
147 "55657156756655",
148 "1275552191113212300012030439187146164646146646466749494799",
149 "1542725565216523985789236956265265265235675811949404040041",
150 "1456427555219115346513212300075341203043918714616464614664"
151 "64667494947990",
152 "1712787255652165239672857892369562652652652356758119494040"
153 "40041670216363"};
154
155int fbytes(unsigned char *buf, int num)
156 {
157 int ret;
158 BIGNUM *tmp = NULL;
159
160 if (fbytes_counter >= 8)
161 return 0;
162 tmp = BN_new();
163 if (!tmp)
164 return 0;
165 if (!BN_dec2bn(&tmp, numbers[fbytes_counter]))
166 {
167 BN_free(tmp);
168 return 0;
169 }
170 fbytes_counter ++;
171 if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
172 ret = 0;
173 else
174 ret = 1;
175 if (tmp)
176 BN_free(tmp);
177 return ret;
178 }
179
180/* some tests from the X9.62 draft */
181int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
182 {
183 int ret = 0;
184 const char message[] = "abc";
185 unsigned char digest[20];
186 unsigned int dgst_len = 0;
187 EVP_MD_CTX md_ctx;
188 EC_KEY *key = NULL;
189 ECDSA_SIG *signature = NULL;
190 BIGNUM *r = NULL, *s = NULL;
191
192 EVP_MD_CTX_init(&md_ctx);
193 /* get the message digest */
194 EVP_DigestInit(&md_ctx, EVP_ecdsa());
195 EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
196 EVP_DigestFinal(&md_ctx, digest, &dgst_len);
197
198 BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
199 /* create the key */
200 if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
201 goto x962_int_err;
202 if (!EC_KEY_generate_key(key))
203 goto x962_int_err;
204 BIO_printf(out, ".");
205 (void)BIO_flush(out);
206 /* create the signature */
207 signature = ECDSA_do_sign(digest, 20, key);
208 if (signature == NULL)
209 goto x962_int_err;
210 BIO_printf(out, ".");
211 (void)BIO_flush(out);
212 /* compare the created signature with the expected signature */
213 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
214 goto x962_int_err;
215 if (!BN_dec2bn(&r, r_in) ||
216 !BN_dec2bn(&s, s_in))
217 goto x962_int_err;
218 if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
219 goto x962_int_err;
220 BIO_printf(out, ".");
221 (void)BIO_flush(out);
222 /* verify the signature */
223 if (ECDSA_do_verify(digest, 20, signature, key) != 1)
224 goto x962_int_err;
225 BIO_printf(out, ".");
226 (void)BIO_flush(out);
227
228 BIO_printf(out, " ok\n");
229 ret = 1;
230x962_int_err:
231 if (!ret)
232 BIO_printf(out, " failed\n");
233 if (key)
234 EC_KEY_free(key);
235 if (signature)
236 ECDSA_SIG_free(signature);
237 if (r)
238 BN_free(r);
239 if (s)
240 BN_free(s);
241 EVP_MD_CTX_cleanup(&md_ctx);
242 return ret;
243 }
244
245int x9_62_tests(BIO *out)
246 {
247 int ret = 0;
248
249 BIO_printf(out, "some tests from X9.62:\n");
250
251 /* set own rand method */
252 if (!change_rand())
253 goto x962_err;
254
255 if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
256 "3342403536405981729393488334694600415596881826869351677613",
257 "5735822328888155254683894997897571951568553642892029982342"))
258 goto x962_err;
259 if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
260 "3086361431751678114926225473006680188549593787585317781474"
261 "62058306432176",
262 "3238135532097973577080787768312505059318910517550078427819"
263 "78505179448783"))
264 goto x962_err;
265#ifndef OPENSSL_NO_EC2M
266 if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
267 "87194383164871543355722284926904419997237591535066528048",
268 "308992691965804947361541664549085895292153777025772063598"))
269 goto x962_err;
270 if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
271 "2159633321041961198501834003903461262881815148684178964245"
272 "5876922391552",
273 "1970303740007316867383349976549972270528498040721988191026"
274 "49413465737174"))
275 goto x962_err;
276#endif
277 ret = 1;
278x962_err:
279 if (!restore_rand())
280 ret = 0;
281 return ret;
282 }
283
284int test_builtin(BIO *out)
285 {
286 EC_builtin_curve *curves = NULL;
287 size_t crv_len = 0, n = 0;
288 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
289 EC_GROUP *group;
290 ECDSA_SIG *ecdsa_sig = NULL;
291 unsigned char digest[20], wrong_digest[20];
292 unsigned char *signature = NULL;
293 const unsigned char *sig_ptr;
294 unsigned char *sig_ptr2;
295 unsigned char *raw_buf = NULL;
296 unsigned int sig_len, degree, r_len, s_len, bn_len, buf_len;
297 int nid, ret = 0;
298
299 /* fill digest values with some random data */
300 if (!RAND_pseudo_bytes(digest, 20) ||
301 !RAND_pseudo_bytes(wrong_digest, 20))
302 {
303 BIO_printf(out, "ERROR: unable to get random data\n");
304 goto builtin_err;
305 }
306
307 /* create and verify a ecdsa signature with every availble curve
308 * (with ) */
309 BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
310 "with some internal curves:\n");
311
312 /* get a list of all internal curves */
313 crv_len = EC_get_builtin_curves(NULL, 0);
314
315 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
316
317 if (curves == NULL)
318 {
319 BIO_printf(out, "malloc error\n");
320 goto builtin_err;
321 }
322
323 if (!EC_get_builtin_curves(curves, crv_len))
324 {
325 BIO_printf(out, "unable to get internal curves\n");
326 goto builtin_err;
327 }
328
329 /* now create and verify a signature for every curve */
330 for (n = 0; n < crv_len; n++)
331 {
332 unsigned char dirt, offset;
333
334 nid = curves[n].nid;
335 if (nid == NID_ipsec4)
336 continue;
337 /* create new ecdsa key (== EC_KEY) */
338 if ((eckey = EC_KEY_new()) == NULL)
339 goto builtin_err;
340 group = EC_GROUP_new_by_curve_name(nid);
341 if (group == NULL)
342 goto builtin_err;
343 if (EC_KEY_set_group(eckey, group) == 0)
344 goto builtin_err;
345 EC_GROUP_free(group);
346 degree = EC_GROUP_get_degree(EC_KEY_get0_group(eckey));
347 if (degree < 160)
348 /* drop the curve */
349 {
350 EC_KEY_free(eckey);
351 eckey = NULL;
352 continue;
353 }
354 BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
355 /* create key */
356 if (!EC_KEY_generate_key(eckey))
357 {
358 BIO_printf(out, " failed\n");
359 goto builtin_err;
360 }
361 /* create second key */
362 if ((wrong_eckey = EC_KEY_new()) == NULL)
363 goto builtin_err;
364 group = EC_GROUP_new_by_curve_name(nid);
365 if (group == NULL)
366 goto builtin_err;
367 if (EC_KEY_set_group(wrong_eckey, group) == 0)
368 goto builtin_err;
369 EC_GROUP_free(group);
370 if (!EC_KEY_generate_key(wrong_eckey))
371 {
372 BIO_printf(out, " failed\n");
373 goto builtin_err;
374 }
375
376 BIO_printf(out, ".");
377 (void)BIO_flush(out);
378 /* check key */
379 if (!EC_KEY_check_key(eckey))
380 {
381 BIO_printf(out, " failed\n");
382 goto builtin_err;
383 }
384 BIO_printf(out, ".");
385 (void)BIO_flush(out);
386 /* create signature */
387 sig_len = ECDSA_size(eckey);
388 if ((signature = OPENSSL_malloc(sig_len)) == NULL)
389 goto builtin_err;
390 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
391 {
392 BIO_printf(out, " failed\n");
393 goto builtin_err;
394 }
395 BIO_printf(out, ".");
396 (void)BIO_flush(out);
397 /* verify signature */
398 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
399 {
400 BIO_printf(out, " failed\n");
401 goto builtin_err;
402 }
403 BIO_printf(out, ".");
404 (void)BIO_flush(out);
405 /* verify signature with the wrong key */
406 if (ECDSA_verify(0, digest, 20, signature, sig_len,
407 wrong_eckey) == 1)
408 {
409 BIO_printf(out, " failed\n");
410 goto builtin_err;
411 }
412 BIO_printf(out, ".");
413 (void)BIO_flush(out);
414 /* wrong digest */
415 if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
416 eckey) == 1)
417 {
418 BIO_printf(out, " failed\n");
419 goto builtin_err;
420 }
421 BIO_printf(out, ".");
422 (void)BIO_flush(out);
423 /* wrong length */
424 if (ECDSA_verify(0, digest, 20, signature, sig_len - 1,
425 eckey) == 1)
426 {
427 BIO_printf(out, " failed\n");
428 goto builtin_err;
429 }
430 BIO_printf(out, ".");
431 (void)BIO_flush(out);
432
433 /* Modify a single byte of the signature: to ensure we don't
434 * garble the ASN1 structure, we read the raw signature and
435 * modify a byte in one of the bignums directly. */
436 sig_ptr = signature;
437 if ((ecdsa_sig = d2i_ECDSA_SIG(NULL, &sig_ptr, sig_len)) == NULL)
438 {
439 BIO_printf(out, " failed\n");
440 goto builtin_err;
441 }
442
443 /* Store the two BIGNUMs in raw_buf. */
444 r_len = BN_num_bytes(ecdsa_sig->r);
445 s_len = BN_num_bytes(ecdsa_sig->s);
446 bn_len = (degree + 7) / 8;
447 if ((r_len > bn_len) || (s_len > bn_len))
448 {
449 BIO_printf(out, " failed\n");
450 goto builtin_err;
451 }
452 buf_len = 2 * bn_len;
453 if ((raw_buf = OPENSSL_malloc(buf_len)) == NULL)
454 goto builtin_err;
455 /* Pad the bignums with leading zeroes. */
456 memset(raw_buf, 0, buf_len);
457 BN_bn2bin(ecdsa_sig->r, raw_buf + bn_len - r_len);
458 BN_bn2bin(ecdsa_sig->s, raw_buf + buf_len - s_len);
459
460 /* Modify a single byte in the buffer. */
461 offset = raw_buf[10] % buf_len;
462 dirt = raw_buf[11] ? raw_buf[11] : 1;
463 raw_buf[offset] ^= dirt;
464 /* Now read the BIGNUMs back in from raw_buf. */
465 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
466 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
467 goto builtin_err;
468
469 sig_ptr2 = signature;
470 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
471 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
472 {
473 BIO_printf(out, " failed\n");
474 goto builtin_err;
475 }
476 /* Sanity check: undo the modification and verify signature. */
477 raw_buf[offset] ^= dirt;
478 if ((BN_bin2bn(raw_buf, bn_len, ecdsa_sig->r) == NULL) ||
479 (BN_bin2bn(raw_buf + bn_len, bn_len, ecdsa_sig->s) == NULL))
480 goto builtin_err;
481
482 sig_ptr2 = signature;
483 sig_len = i2d_ECDSA_SIG(ecdsa_sig, &sig_ptr2);
484 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
485 {
486 BIO_printf(out, " failed\n");
487 goto builtin_err;
488 }
489 BIO_printf(out, ".");
490 (void)BIO_flush(out);
491
492 BIO_printf(out, " ok\n");
493 /* cleanup */
494 /* clean bogus errors */
495 ERR_clear_error();
496 OPENSSL_free(signature);
497 signature = NULL;
498 EC_KEY_free(eckey);
499 eckey = NULL;
500 EC_KEY_free(wrong_eckey);
501 wrong_eckey = NULL;
502 ECDSA_SIG_free(ecdsa_sig);
503 ecdsa_sig = NULL;
504 OPENSSL_free(raw_buf);
505 raw_buf = NULL;
506 }
507
508 ret = 1;
509builtin_err:
510 if (eckey)
511 EC_KEY_free(eckey);
512 if (wrong_eckey)
513 EC_KEY_free(wrong_eckey);
514 if (ecdsa_sig)
515 ECDSA_SIG_free(ecdsa_sig);
516 if (signature)
517 OPENSSL_free(signature);
518 if (raw_buf)
519 OPENSSL_free(raw_buf);
520 if (curves)
521 OPENSSL_free(curves);
522
523 return ret;
524 }
525
526int main(void)
527 {
528 int ret = 1;
529 BIO *out;
530
531 out = BIO_new_fp(stdout, BIO_NOCLOSE);
532
533 /* enable memory leak checking unless explicitly disabled */
534 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
535 (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
536 {
537 CRYPTO_malloc_debug_init();
538 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
539 }
540 else
541 {
542 /* OPENSSL_DEBUG_MEMORY=off */
543 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
544 }
545 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
546
547 ERR_load_crypto_strings();
548
549 /* initialize the prng */
550 RAND_seed(rnd_seed, sizeof(rnd_seed));
551
552 /* the tests */
553 if (!x9_62_tests(out)) goto err;
554 if (!test_builtin(out)) goto err;
555
556 ret = 0;
557err:
558 if (ret)
559 BIO_printf(out, "\nECDSA test failed\n");
560 else
561 BIO_printf(out, "\nECDSA test passed\n");
562 if (ret)
563 ERR_print_errors(out);
564 CRYPTO_cleanup_all_ex_data();
565 ERR_remove_thread_state(NULL);
566 ERR_free_strings();
567 CRYPTO_mem_leaks(out);
568 if (out != NULL)
569 BIO_free(out);
570 return ret;
571 }
572#endif
diff --git a/src/lib/libssl/src/crypto/evp/evp_test.c b/src/lib/libssl/src/crypto/evp/evp_test.c
deleted file mode 100644
index 55c7cdfdcc..0000000000
--- a/src/lib/libssl/src/crypto/evp/evp_test.c
+++ /dev/null
@@ -1,450 +0,0 @@
1/* Written by Ben Laurie, 2001 */
2/*
3 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 */
49
50#include <stdio.h>
51#include <string.h>
52
53#include "../e_os.h"
54
55#include <openssl/opensslconf.h>
56#include <openssl/evp.h>
57#ifndef OPENSSL_NO_ENGINE
58#include <openssl/engine.h>
59#endif
60#include <openssl/err.h>
61#include <openssl/conf.h>
62
63static void hexdump(FILE *f,const char *title,const unsigned char *s,int l)
64 {
65 int n=0;
66
67 fprintf(f,"%s",title);
68 for( ; n < l ; ++n)
69 {
70 if((n%16) == 0)
71 fprintf(f,"\n%04x",n);
72 fprintf(f," %02x",s[n]);
73 }
74 fprintf(f,"\n");
75 }
76
77static int convert(unsigned char *s)
78 {
79 unsigned char *d;
80
81 for(d=s ; *s ; s+=2,++d)
82 {
83 unsigned int n;
84
85 if(!s[1])
86 {
87 fprintf(stderr,"Odd number of hex digits!");
88 EXIT(4);
89 }
90 sscanf((char *)s,"%2x",&n);
91 *d=(unsigned char)n;
92 }
93 return s-d;
94 }
95
96static char *sstrsep(char **string, const char *delim)
97 {
98 char isdelim[256];
99 char *token = *string;
100
101 if (**string == 0)
102 return NULL;
103
104 memset(isdelim, 0, 256);
105 isdelim[0] = 1;
106
107 while (*delim)
108 {
109 isdelim[(unsigned char)(*delim)] = 1;
110 delim++;
111 }
112
113 while (!isdelim[(unsigned char)(**string)])
114 {
115 (*string)++;
116 }
117
118 if (**string)
119 {
120 **string = 0;
121 (*string)++;
122 }
123
124 return token;
125 }
126
127static unsigned char *ustrsep(char **p,const char *sep)
128 { return (unsigned char *)sstrsep(p,sep); }
129
130static int test1_exit(int ec)
131 {
132 EXIT(ec);
133 return(0); /* To keep some compilers quiet */
134 }
135
136static void test1(const EVP_CIPHER *c,const unsigned char *key,int kn,
137 const unsigned char *iv,int in,
138 const unsigned char *plaintext,int pn,
139 const unsigned char *ciphertext,int cn,
140 int encdec)
141 {
142 EVP_CIPHER_CTX ctx;
143 unsigned char out[4096];
144 int outl,outl2;
145
146 printf("Testing cipher %s%s\n",EVP_CIPHER_name(c),
147 (encdec == 1 ? "(encrypt)" : (encdec == 0 ? "(decrypt)" : "(encrypt/decrypt)")));
148 hexdump(stdout,"Key",key,kn);
149 if(in)
150 hexdump(stdout,"IV",iv,in);
151 hexdump(stdout,"Plaintext",plaintext,pn);
152 hexdump(stdout,"Ciphertext",ciphertext,cn);
153
154 if(kn != c->key_len)
155 {
156 fprintf(stderr,"Key length doesn't match, got %d expected %lu\n",kn,
157 (unsigned long)c->key_len);
158 test1_exit(5);
159 }
160 EVP_CIPHER_CTX_init(&ctx);
161 if (encdec != 0)
162 {
163 if(!EVP_EncryptInit_ex(&ctx,c,NULL,key,iv))
164 {
165 fprintf(stderr,"EncryptInit failed\n");
166 ERR_print_errors_fp(stderr);
167 test1_exit(10);
168 }
169 EVP_CIPHER_CTX_set_padding(&ctx,0);
170
171 if(!EVP_EncryptUpdate(&ctx,out,&outl,plaintext,pn))
172 {
173 fprintf(stderr,"Encrypt failed\n");
174 ERR_print_errors_fp(stderr);
175 test1_exit(6);
176 }
177 if(!EVP_EncryptFinal_ex(&ctx,out+outl,&outl2))
178 {
179 fprintf(stderr,"EncryptFinal failed\n");
180 ERR_print_errors_fp(stderr);
181 test1_exit(7);
182 }
183
184 if(outl+outl2 != cn)
185 {
186 fprintf(stderr,"Ciphertext length mismatch got %d expected %d\n",
187 outl+outl2,cn);
188 test1_exit(8);
189 }
190
191 if(memcmp(out,ciphertext,cn))
192 {
193 fprintf(stderr,"Ciphertext mismatch\n");
194 hexdump(stderr,"Got",out,cn);
195 hexdump(stderr,"Expected",ciphertext,cn);
196 test1_exit(9);
197 }
198 }
199
200 if (encdec <= 0)
201 {
202 if(!EVP_DecryptInit_ex(&ctx,c,NULL,key,iv))
203 {
204 fprintf(stderr,"DecryptInit failed\n");
205 ERR_print_errors_fp(stderr);
206 test1_exit(11);
207 }
208 EVP_CIPHER_CTX_set_padding(&ctx,0);
209
210 if(!EVP_DecryptUpdate(&ctx,out,&outl,ciphertext,cn))
211 {
212 fprintf(stderr,"Decrypt failed\n");
213 ERR_print_errors_fp(stderr);
214 test1_exit(6);
215 }
216 if(!EVP_DecryptFinal_ex(&ctx,out+outl,&outl2))
217 {
218 fprintf(stderr,"DecryptFinal failed\n");
219 ERR_print_errors_fp(stderr);
220 test1_exit(7);
221 }
222
223 if(outl+outl2 != pn)
224 {
225 fprintf(stderr,"Plaintext length mismatch got %d expected %d\n",
226 outl+outl2,pn);
227 test1_exit(8);
228 }
229
230 if(memcmp(out,plaintext,pn))
231 {
232 fprintf(stderr,"Plaintext mismatch\n");
233 hexdump(stderr,"Got",out,pn);
234 hexdump(stderr,"Expected",plaintext,pn);
235 test1_exit(9);
236 }
237 }
238
239 EVP_CIPHER_CTX_cleanup(&ctx);
240
241 printf("\n");
242 }
243
244static int test_cipher(const char *cipher,const unsigned char *key,int kn,
245 const unsigned char *iv,int in,
246 const unsigned char *plaintext,int pn,
247 const unsigned char *ciphertext,int cn,
248 int encdec)
249 {
250 const EVP_CIPHER *c;
251
252 c=EVP_get_cipherbyname(cipher);
253 if(!c)
254 return 0;
255
256 test1(c,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec);
257
258 return 1;
259 }
260
261static int test_digest(const char *digest,
262 const unsigned char *plaintext,int pn,
263 const unsigned char *ciphertext, unsigned int cn)
264 {
265 const EVP_MD *d;
266 EVP_MD_CTX ctx;
267 unsigned char md[EVP_MAX_MD_SIZE];
268 unsigned int mdn;
269
270 d=EVP_get_digestbyname(digest);
271 if(!d)
272 return 0;
273
274 printf("Testing digest %s\n",EVP_MD_name(d));
275 hexdump(stdout,"Plaintext",plaintext,pn);
276 hexdump(stdout,"Digest",ciphertext,cn);
277
278 EVP_MD_CTX_init(&ctx);
279 if(!EVP_DigestInit_ex(&ctx,d, NULL))
280 {
281 fprintf(stderr,"DigestInit failed\n");
282 ERR_print_errors_fp(stderr);
283 EXIT(100);
284 }
285 if(!EVP_DigestUpdate(&ctx,plaintext,pn))
286 {
287 fprintf(stderr,"DigestUpdate failed\n");
288 ERR_print_errors_fp(stderr);
289 EXIT(101);
290 }
291 if(!EVP_DigestFinal_ex(&ctx,md,&mdn))
292 {
293 fprintf(stderr,"DigestFinal failed\n");
294 ERR_print_errors_fp(stderr);
295 EXIT(101);
296 }
297 EVP_MD_CTX_cleanup(&ctx);
298
299 if(mdn != cn)
300 {
301 fprintf(stderr,"Digest length mismatch, got %d expected %d\n",mdn,cn);
302 EXIT(102);
303 }
304
305 if(memcmp(md,ciphertext,cn))
306 {
307 fprintf(stderr,"Digest mismatch\n");
308 hexdump(stderr,"Got",md,cn);
309 hexdump(stderr,"Expected",ciphertext,cn);
310 EXIT(103);
311 }
312
313 printf("\n");
314
315 EVP_MD_CTX_cleanup(&ctx);
316
317 return 1;
318 }
319
320int main(int argc,char **argv)
321 {
322 const char *szTestFile;
323 FILE *f;
324
325 if(argc != 2)
326 {
327 fprintf(stderr,"%s <test file>\n",argv[0]);
328 EXIT(1);
329 }
330 CRYPTO_malloc_debug_init();
331 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
332 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
333
334 szTestFile=argv[1];
335
336 f=fopen(szTestFile,"r");
337 if(!f)
338 {
339 perror(szTestFile);
340 EXIT(2);
341 }
342
343 /* Load up the software EVP_CIPHER and EVP_MD definitions */
344 OpenSSL_add_all_ciphers();
345 OpenSSL_add_all_digests();
346#ifndef OPENSSL_NO_ENGINE
347 /* Load all compiled-in ENGINEs */
348 ENGINE_load_builtin_engines();
349#endif
350#if 0
351 OPENSSL_config();
352#endif
353#ifndef OPENSSL_NO_ENGINE
354 /* Register all available ENGINE implementations of ciphers and digests.
355 * This could perhaps be changed to "ENGINE_register_all_complete()"? */
356 ENGINE_register_all_ciphers();
357 ENGINE_register_all_digests();
358 /* If we add command-line options, this statement should be switchable.
359 * It'll prevent ENGINEs being ENGINE_init()ialised for cipher/digest use if
360 * they weren't already initialised. */
361 /* ENGINE_set_cipher_flags(ENGINE_CIPHER_FLAG_NOINIT); */
362#endif
363
364 for( ; ; )
365 {
366 char line[4096];
367 char *p;
368 char *cipher;
369 unsigned char *iv,*key,*plaintext,*ciphertext;
370 int encdec;
371 int kn,in,pn,cn;
372
373 if(!fgets((char *)line,sizeof line,f))
374 break;
375 if(line[0] == '#' || line[0] == '\n')
376 continue;
377 p=line;
378 cipher=sstrsep(&p,":");
379 key=ustrsep(&p,":");
380 iv=ustrsep(&p,":");
381 plaintext=ustrsep(&p,":");
382 ciphertext=ustrsep(&p,":");
383 if (p[-1] == '\n') {
384 p[-1] = '\0';
385 encdec = -1;
386 } else {
387 encdec = atoi(sstrsep(&p,"\n"));
388 }
389
390
391 kn=convert(key);
392 in=convert(iv);
393 pn=convert(plaintext);
394 cn=convert(ciphertext);
395
396 if(!test_cipher(cipher,key,kn,iv,in,plaintext,pn,ciphertext,cn,encdec)
397 && !test_digest(cipher,plaintext,pn,ciphertext,cn))
398 {
399#ifdef OPENSSL_NO_AES
400 if (strstr(cipher, "AES") == cipher)
401 {
402 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
403 continue;
404 }
405#endif
406#ifdef OPENSSL_NO_DES
407 if (strstr(cipher, "DES") == cipher)
408 {
409 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
410 continue;
411 }
412#endif
413#ifdef OPENSSL_NO_RC4
414 if (strstr(cipher, "RC4") == cipher)
415 {
416 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
417 continue;
418 }
419#endif
420#ifdef OPENSSL_NO_CAMELLIA
421 if (strstr(cipher, "CAMELLIA") == cipher)
422 {
423 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
424 continue;
425 }
426#endif
427#ifdef OPENSSL_NO_SEED
428 if (strstr(cipher, "SEED") == cipher)
429 {
430 fprintf(stdout, "Cipher disabled, skipping %s\n", cipher);
431 continue;
432 }
433#endif
434 fprintf(stderr,"Can't find %s\n",cipher);
435 EXIT(3);
436 }
437 }
438 fclose(f);
439
440#ifndef OPENSSL_NO_ENGINE
441 ENGINE_cleanup();
442#endif
443 EVP_cleanup();
444 CRYPTO_cleanup_all_ex_data();
445 ERR_remove_thread_state(NULL);
446 ERR_free_strings();
447 CRYPTO_mem_leaks_fp(stderr);
448
449 return 0;
450 }
diff --git a/src/lib/libssl/src/crypto/hmac/hmactest.c b/src/lib/libssl/src/crypto/hmac/hmactest.c
deleted file mode 100644
index 282e06e53e..0000000000
--- a/src/lib/libssl/src/crypto/hmac/hmactest.c
+++ /dev/null
@@ -1,164 +0,0 @@
1/* crypto/hmac/hmactest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_HMAC
66int main(int argc, char *argv[])
67{
68 printf("No HMAC support\n");
69 return(0);
70}
71#else
72#include <openssl/hmac.h>
73#ifndef OPENSSL_NO_MD5
74#include <openssl/md5.h>
75#endif
76
77
78#ifndef OPENSSL_NO_MD5
79static struct test_st
80 {
81 unsigned char key[16];
82 int key_len;
83 unsigned char data[64];
84 int data_len;
85 unsigned char *digest;
86 } test[4]={
87 { "",
88 0,
89 "More text test vectors to stuff up EBCDIC machines :-)",
90 54,
91 (unsigned char *)"e9139d1e6ee064ef8cf514fc7dc83e86",
92 },{ {0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,
93 0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,0x0b,},
94 16,
95 "Hi There",
96 8,
97 (unsigned char *)"9294727a3638bb1c13f48ef8158bfc9d",
98 },{ "Jefe",
99 4,
100 "what do ya want for nothing?",
101 28,
102 (unsigned char *)"750c783e6ab0b503eaa86e310a5db738",
103 },{
104 {0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,
105 0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,0xaa,},
106 16,
107 {0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
108 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
109 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
110 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
111 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
112 0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,
113 0xdd,0xdd},
114 50,
115 (unsigned char *)"56be34521d144c88dbb8c733f0e8b3f6",
116 },
117 };
118#endif
119
120static char *pt(unsigned char *md);
121int main(int argc, char *argv[])
122 {
123#ifndef OPENSSL_NO_MD5
124 int i;
125 char *p;
126#endif
127 int err=0;
128
129#ifdef OPENSSL_NO_MD5
130 printf("test skipped: MD5 disabled\n");
131#else
132 for (i=0; i<4; i++)
133 {
134 p=pt(HMAC(EVP_md5(),
135 test[i].key, test[i].key_len,
136 test[i].data, test[i].data_len,
137 NULL,NULL));
138
139 if (strcmp(p,(char *)test[i].digest) != 0)
140 {
141 printf("error calculating HMAC on %d entry'\n",i);
142 printf("got %s instead of %s\n",p,test[i].digest);
143 err++;
144 }
145 else
146 printf("test %d ok\n",i);
147 }
148#endif /* OPENSSL_NO_MD5 */
149 EXIT(err);
150 return(0);
151 }
152
153#ifndef OPENSSL_NO_MD5
154static char *pt(unsigned char *md)
155 {
156 int i;
157 static char buf[80];
158
159 for (i=0; i<MD5_DIGEST_LENGTH; i++)
160 sprintf(&(buf[i*2]),"%02x",md[i]);
161 return(buf);
162 }
163#endif
164#endif
diff --git a/src/lib/libssl/src/crypto/idea/ideatest.c b/src/lib/libssl/src/crypto/idea/ideatest.c
deleted file mode 100644
index e6ffc7025e..0000000000
--- a/src/lib/libssl/src/crypto/idea/ideatest.c
+++ /dev/null
@@ -1,235 +0,0 @@
1/* crypto/idea/ideatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_IDEA
66int main(int argc, char *argv[])
67{
68 printf("No IDEA support\n");
69 return(0);
70}
71#else
72#include <openssl/idea.h>
73
74unsigned char k[16]={
75 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
76 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
77
78unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
79unsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
80unsigned char out[80];
81
82char *text="Hello to all people out there";
83
84static unsigned char cfb_key[16]={
85 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
86 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
87 };
88static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
89static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
90#define CFB_TEST_SIZE 24
91static unsigned char plain[CFB_TEST_SIZE]=
92 {
93 0x4e,0x6f,0x77,0x20,0x69,0x73,
94 0x20,0x74,0x68,0x65,0x20,0x74,
95 0x69,0x6d,0x65,0x20,0x66,0x6f,
96 0x72,0x20,0x61,0x6c,0x6c,0x20
97 };
98static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
99 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
100 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
101 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
102
103/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
104 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
105 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
106 };
107
108static int cfb64_test(unsigned char *cfb_cipher);
109static char *pt(unsigned char *p);
110int main(int argc, char *argv[])
111 {
112 int i,err=0;
113 IDEA_KEY_SCHEDULE key,dkey;
114 unsigned char iv[8];
115
116 idea_set_encrypt_key(k,&key);
117 idea_ecb_encrypt(in,out,&key);
118 if (memcmp(out,c,8) != 0)
119 {
120 printf("ecb idea error encrypting\n");
121 printf("got :");
122 for (i=0; i<8; i++)
123 printf("%02X ",out[i]);
124 printf("\n");
125 printf("expected:");
126 for (i=0; i<8; i++)
127 printf("%02X ",c[i]);
128 err=20;
129 printf("\n");
130 }
131
132 idea_set_decrypt_key(&key,&dkey);
133 idea_ecb_encrypt(c,out,&dkey);
134 if (memcmp(out,in,8) != 0)
135 {
136 printf("ecb idea error decrypting\n");
137 printf("got :");
138 for (i=0; i<8; i++)
139 printf("%02X ",out[i]);
140 printf("\n");
141 printf("expected:");
142 for (i=0; i<8; i++)
143 printf("%02X ",in[i]);
144 printf("\n");
145 err=3;
146 }
147
148 if (err == 0) printf("ecb idea ok\n");
149
150 memcpy(iv,k,8);
151 idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
152 memcpy(iv,k,8);
153 idea_cbc_encrypt(out,out,8,&dkey,iv,0);
154 idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
155 if (memcmp(text,out,strlen(text)+1) != 0)
156 {
157 printf("cbc idea bad\n");
158 err=4;
159 }
160 else
161 printf("cbc idea ok\n");
162
163 printf("cfb64 idea ");
164 if (cfb64_test(cfb_cipher64))
165 {
166 printf("bad\n");
167 err=5;
168 }
169 else
170 printf("ok\n");
171
172#ifdef OPENSSL_SYS_NETWARE
173 if (err) printf("ERROR: %d\n", err);
174#endif
175 EXIT(err);
176 return(err);
177 }
178
179static int cfb64_test(unsigned char *cfb_cipher)
180 {
181 IDEA_KEY_SCHEDULE eks,dks;
182 int err=0,i,n;
183
184 idea_set_encrypt_key(cfb_key,&eks);
185 idea_set_decrypt_key(&eks,&dks);
186 memcpy(cfb_tmp,cfb_iv,8);
187 n=0;
188 idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
189 cfb_tmp,&n,IDEA_ENCRYPT);
190 idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
191 (long)CFB_TEST_SIZE-12,&eks,
192 cfb_tmp,&n,IDEA_ENCRYPT);
193 if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
194 {
195 err=1;
196 printf("idea_cfb64_encrypt encrypt error\n");
197 for (i=0; i<CFB_TEST_SIZE; i+=8)
198 printf("%s\n",pt(&(cfb_buf1[i])));
199 }
200 memcpy(cfb_tmp,cfb_iv,8);
201 n=0;
202 idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
203 cfb_tmp,&n,IDEA_DECRYPT);
204 idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
205 (long)CFB_TEST_SIZE-17,&dks,
206 cfb_tmp,&n,IDEA_DECRYPT);
207 if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
208 {
209 err=1;
210 printf("idea_cfb_encrypt decrypt error\n");
211 for (i=0; i<24; i+=8)
212 printf("%s\n",pt(&(cfb_buf2[i])));
213 }
214 return(err);
215 }
216
217static char *pt(unsigned char *p)
218 {
219 static char bufs[10][20];
220 static int bnum=0;
221 char *ret;
222 int i;
223 static char *f="0123456789ABCDEF";
224
225 ret= &(bufs[bnum++][0]);
226 bnum%=10;
227 for (i=0; i<8; i++)
228 {
229 ret[i*2]=f[(p[i]>>4)&0xf];
230 ret[i*2+1]=f[p[i]&0xf];
231 }
232 ret[16]='\0';
233 return(ret);
234 }
235#endif
diff --git a/src/lib/libssl/src/crypto/md2/md2test.c b/src/lib/libssl/src/crypto/md2/md2test.c
deleted file mode 100644
index 0195214036..0000000000
--- a/src/lib/libssl/src/crypto/md2/md2test.c
+++ /dev/null
@@ -1,139 +0,0 @@
1/* crypto/md2/md2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD2
66int main(int argc, char *argv[])
67{
68 printf("No MD2 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md2.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87 "8350e5a3e24c153df2275c9f80692773",
88 "32ec01ec4a6dac72c0ab96fb34c0b5d1",
89 "da853b0d3f88d99b30283a69e6ded6bb",
90 "ab4f496bfb2a530b219ff33031fe06b0",
91 "4e8ddff3650292ab5a4108c3aa47940b",
92 "da33def2a42df13975352846c30338cd",
93 "d5976f79d83d3a0dc9806c3c66f3efd8",
94 };
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD2_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest((unsigned char *)*P,strlen(*P),md,NULL,EVP_md2(), NULL);
110 p=pt(md);
111 if (strcmp(p,*R) != 0)
112 {
113 printf("error calculating MD2 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123#ifdef OPENSSL_SYS_NETWARE
124 if (err) printf("ERROR: %d\n", err);
125#endif
126 EXIT(err);
127 return err;
128 }
129
130static char *pt(unsigned char *md)
131 {
132 int i;
133 static char buf[80];
134
135 for (i=0; i<MD2_DIGEST_LENGTH; i++)
136 sprintf(&(buf[i*2]),"%02x",md[i]);
137 return(buf);
138 }
139#endif
diff --git a/src/lib/libssl/src/crypto/md4/md4test.c b/src/lib/libssl/src/crypto/md4/md4test.c
deleted file mode 100644
index 56591728a1..0000000000
--- a/src/lib/libssl/src/crypto/md4/md4test.c
+++ /dev/null
@@ -1,136 +0,0 @@
1/* crypto/md4/md4test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD4
66int main(int argc, char *argv[])
67{
68 printf("No MD4 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md4.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87"31d6cfe0d16ae931b73c59d7e0c089c0",
88"bde52cb31de33e46245e05fbdbd6fb24",
89"a448017aaf21d8525fc10ae87aa6729d",
90"d9130a8164549fe818874806e1c7014b",
91"d79e1c308aa5bbcdeea8ed63df412da9",
92"043f8582f241db351ce627e153e7f0e4",
93"e33b4ddc9c38f2199c3e7b164fcc0536",
94};
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD4_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_md4(), NULL);
110 p=pt(md);
111 if (strcmp(p,(char *)*R) != 0)
112 {
113 printf("error calculating MD4 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123 EXIT(err);
124 return(0);
125 }
126
127static char *pt(unsigned char *md)
128 {
129 int i;
130 static char buf[80];
131
132 for (i=0; i<MD4_DIGEST_LENGTH; i++)
133 sprintf(&(buf[i*2]),"%02x",md[i]);
134 return(buf);
135 }
136#endif
diff --git a/src/lib/libssl/src/crypto/md5/md5test.c b/src/lib/libssl/src/crypto/md5/md5test.c
deleted file mode 100644
index 2b37190e32..0000000000
--- a/src/lib/libssl/src/crypto/md5/md5test.c
+++ /dev/null
@@ -1,140 +0,0 @@
1/* crypto/md5/md5test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_MD5
66int main(int argc, char *argv[])
67{
68 printf("No MD5 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/md5.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
82 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
83 NULL,
84 };
85
86static char *ret[]={
87 "d41d8cd98f00b204e9800998ecf8427e",
88 "0cc175b9c0f1b6a831c399e269772661",
89 "900150983cd24fb0d6963f7d28e17f72",
90 "f96b697d7cb7938d525a2f31aaf161d0",
91 "c3fcd3d76192e4007dfb496cca67e13b",
92 "d174ab98d277d9f5a5611c2c9f419d9f",
93 "57edf4a22be3c955ac49da2e2107b67a",
94 };
95
96static char *pt(unsigned char *md);
97int main(int argc, char *argv[])
98 {
99 int i,err=0;
100 char **P,**R;
101 char *p;
102 unsigned char md[MD5_DIGEST_LENGTH];
103
104 P=test;
105 R=ret;
106 i=1;
107 while (*P != NULL)
108 {
109 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_md5(), NULL);
110 p=pt(md);
111 if (strcmp(p,(char *)*R) != 0)
112 {
113 printf("error calculating MD5 on '%s'\n",*P);
114 printf("got %s instead of %s\n",p,*R);
115 err++;
116 }
117 else
118 printf("test %d ok\n",i);
119 i++;
120 R++;
121 P++;
122 }
123
124#ifdef OPENSSL_SYS_NETWARE
125 if (err) printf("ERROR: %d\n", err);
126#endif
127 EXIT(err);
128 return(0);
129 }
130
131static char *pt(unsigned char *md)
132 {
133 int i;
134 static char buf[80];
135
136 for (i=0; i<MD5_DIGEST_LENGTH; i++)
137 sprintf(&(buf[i*2]),"%02x",md[i]);
138 return(buf);
139 }
140#endif
diff --git a/src/lib/libssl/src/crypto/mdc2/mdc2test.c b/src/lib/libssl/src/crypto/mdc2/mdc2test.c
deleted file mode 100644
index cf8f65cf27..0000000000
--- a/src/lib/libssl/src/crypto/mdc2/mdc2test.c
+++ /dev/null
@@ -1,141 +0,0 @@
1/* crypto/mdc2/mdc2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#if defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_MDC2)
66#define OPENSSL_NO_MDC2
67#endif
68
69#ifdef OPENSSL_NO_MDC2
70int main(int argc, char *argv[])
71{
72 printf("No MDC2 support\n");
73 return(0);
74}
75#else
76#include <openssl/evp.h>
77#include <openssl/mdc2.h>
78
79static unsigned char pad1[16]={
80 0x42,0xE5,0x0C,0xD2,0x24,0xBA,0xCE,0xBA,
81 0x76,0x0B,0xDD,0x2B,0xD4,0x09,0x28,0x1A
82 };
83
84static unsigned char pad2[16]={
85 0x2E,0x46,0x79,0xB5,0xAD,0xD9,0xCA,0x75,
86 0x35,0xD8,0x7A,0xFE,0xAB,0x33,0xBE,0xE2
87 };
88
89int main(int argc, char *argv[])
90 {
91 int ret=0;
92 unsigned char md[MDC2_DIGEST_LENGTH];
93 int i;
94 EVP_MD_CTX c;
95 static char *text="Now is the time for all ";
96
97 EVP_MD_CTX_init(&c);
98 EVP_DigestInit_ex(&c,EVP_mdc2(), NULL);
99 EVP_DigestUpdate(&c,(unsigned char *)text,strlen(text));
100 EVP_DigestFinal_ex(&c,&(md[0]),NULL);
101
102 if (memcmp(md,pad1,MDC2_DIGEST_LENGTH) != 0)
103 {
104 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
105 printf("%02X",md[i]);
106 printf(" <- generated\n");
107 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
108 printf("%02X",pad1[i]);
109 printf(" <- correct\n");
110 ret=1;
111 }
112 else
113 printf("pad1 - ok\n");
114
115 EVP_DigestInit_ex(&c,EVP_mdc2(), NULL);
116 /* FIXME: use a ctl function? */
117 ((MDC2_CTX *)c.md_data)->pad_type=2;
118 EVP_DigestUpdate(&c,(unsigned char *)text,strlen(text));
119 EVP_DigestFinal_ex(&c,&(md[0]),NULL);
120
121 if (memcmp(md,pad2,MDC2_DIGEST_LENGTH) != 0)
122 {
123 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
124 printf("%02X",md[i]);
125 printf(" <- generated\n");
126 for (i=0; i<MDC2_DIGEST_LENGTH; i++)
127 printf("%02X",pad2[i]);
128 printf(" <- correct\n");
129 ret=1;
130 }
131 else
132 printf("pad2 - ok\n");
133
134 EVP_MD_CTX_cleanup(&c);
135#ifdef OPENSSL_SYS_NETWARE
136 if (ret) printf("ERROR: %d\n", ret);
137#endif
138 EXIT(ret);
139 return(ret);
140 }
141#endif
diff --git a/src/lib/libssl/src/crypto/rand/randtest.c b/src/lib/libssl/src/crypto/rand/randtest.c
deleted file mode 100644
index 9e92a70b03..0000000000
--- a/src/lib/libssl/src/crypto/rand/randtest.c
+++ /dev/null
@@ -1,219 +0,0 @@
1/* crypto/rand/randtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <openssl/rand.h>
62
63#include "../e_os.h"
64
65/* some FIPS 140-1 random number test */
66/* some simple tests */
67
68int main(int argc,char **argv)
69 {
70 unsigned char buf[2500];
71 int i,j,k,s,sign,nsign,err=0;
72 unsigned long n1;
73 unsigned long n2[16];
74 unsigned long runs[2][34];
75 /*double d; */
76 long d;
77
78 i = RAND_pseudo_bytes(buf,2500);
79 if (i < 0)
80 {
81 printf ("init failed, the rand method is not properly installed\n");
82 err++;
83 goto err;
84 }
85
86 n1=0;
87 for (i=0; i<16; i++) n2[i]=0;
88 for (i=0; i<34; i++) runs[0][i]=runs[1][i]=0;
89
90 /* test 1 and 2 */
91 sign=0;
92 nsign=0;
93 for (i=0; i<2500; i++)
94 {
95 j=buf[i];
96
97 n2[j&0x0f]++;
98 n2[(j>>4)&0x0f]++;
99
100 for (k=0; k<8; k++)
101 {
102 s=(j&0x01);
103 if (s == sign)
104 nsign++;
105 else
106 {
107 if (nsign > 34) nsign=34;
108 if (nsign != 0)
109 {
110 runs[sign][nsign-1]++;
111 if (nsign > 6)
112 runs[sign][5]++;
113 }
114 sign=s;
115 nsign=1;
116 }
117
118 if (s) n1++;
119 j>>=1;
120 }
121 }
122 if (nsign > 34) nsign=34;
123 if (nsign != 0) runs[sign][nsign-1]++;
124
125 /* test 1 */
126 if (!((9654 < n1) && (n1 < 10346)))
127 {
128 printf("test 1 failed, X=%lu\n",n1);
129 err++;
130 }
131 printf("test 1 done\n");
132
133 /* test 2 */
134#ifdef undef
135 d=0;
136 for (i=0; i<16; i++)
137 d+=n2[i]*n2[i];
138 d=d*16.0/5000.0-5000.0;
139 if (!((1.03 < d) && (d < 57.4)))
140 {
141 printf("test 2 failed, X=%.2f\n",d);
142 err++;
143 }
144#endif
145 d=0;
146 for (i=0; i<16; i++)
147 d+=n2[i]*n2[i];
148 d=(d*8)/25-500000;
149 if (!((103 < d) && (d < 5740)))
150 {
151 printf("test 2 failed, X=%ld.%02ld\n",d/100L,d%100L);
152 err++;
153 }
154 printf("test 2 done\n");
155
156 /* test 3 */
157 for (i=0; i<2; i++)
158 {
159 if (!((2267 < runs[i][0]) && (runs[i][0] < 2733)))
160 {
161 printf("test 3 failed, bit=%d run=%d num=%lu\n",
162 i,1,runs[i][0]);
163 err++;
164 }
165 if (!((1079 < runs[i][1]) && (runs[i][1] < 1421)))
166 {
167 printf("test 3 failed, bit=%d run=%d num=%lu\n",
168 i,2,runs[i][1]);
169 err++;
170 }
171 if (!(( 502 < runs[i][2]) && (runs[i][2] < 748)))
172 {
173 printf("test 3 failed, bit=%d run=%d num=%lu\n",
174 i,3,runs[i][2]);
175 err++;
176 }
177 if (!(( 223 < runs[i][3]) && (runs[i][3] < 402)))
178 {
179 printf("test 3 failed, bit=%d run=%d num=%lu\n",
180 i,4,runs[i][3]);
181 err++;
182 }
183 if (!(( 90 < runs[i][4]) && (runs[i][4] < 223)))
184 {
185 printf("test 3 failed, bit=%d run=%d num=%lu\n",
186 i,5,runs[i][4]);
187 err++;
188 }
189 if (!(( 90 < runs[i][5]) && (runs[i][5] < 223)))
190 {
191 printf("test 3 failed, bit=%d run=%d num=%lu\n",
192 i,6,runs[i][5]);
193 err++;
194 }
195 }
196 printf("test 3 done\n");
197
198 /* test 4 */
199 if (runs[0][33] != 0)
200 {
201 printf("test 4 failed, bit=%d run=%d num=%lu\n",
202 0,34,runs[0][33]);
203 err++;
204 }
205 if (runs[1][33] != 0)
206 {
207 printf("test 4 failed, bit=%d run=%d num=%lu\n",
208 1,34,runs[1][33]);
209 err++;
210 }
211 printf("test 4 done\n");
212 err:
213 err=((err)?1:0);
214#ifdef OPENSSL_SYS_NETWARE
215 if (err) printf("ERROR: %d\n", err);
216#endif
217 EXIT(err);
218 return(err);
219 }
diff --git a/src/lib/libssl/src/crypto/rc2/rc2test.c b/src/lib/libssl/src/crypto/rc2/rc2test.c
deleted file mode 100644
index 0e117436bb..0000000000
--- a/src/lib/libssl/src/crypto/rc2/rc2test.c
+++ /dev/null
@@ -1,274 +0,0 @@
1/* crypto/rc2/rc2test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* This has been a quickly hacked 'ideatest.c'. When I add tests for other
60 * RC2 modes, more of the code will be uncommented. */
61
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65
66#include "../e_os.h"
67
68#ifdef OPENSSL_NO_RC2
69int main(int argc, char *argv[])
70{
71 printf("No RC2 support\n");
72 return(0);
73}
74#else
75#include <openssl/rc2.h>
76
77static unsigned char RC2key[4][16]={
78 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
79 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
80 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
81 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
82 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
83 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
84 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
85 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F},
86 };
87
88static unsigned char RC2plain[4][8]={
89 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
90 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
91 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
92 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
93 };
94
95static unsigned char RC2cipher[4][8]={
96 {0x1C,0x19,0x8A,0x83,0x8D,0xF0,0x28,0xB7},
97 {0x21,0x82,0x9C,0x78,0xA9,0xF9,0xC0,0x74},
98 {0x13,0xDB,0x35,0x17,0xD3,0x21,0x86,0x9E},
99 {0x50,0xDC,0x01,0x62,0xBD,0x75,0x7F,0x31},
100 };
101/************/
102#ifdef undef
103unsigned char k[16]={
104 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
105 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
106
107unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
108unsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
109unsigned char out[80];
110
111char *text="Hello to all people out there";
112
113static unsigned char cfb_key[16]={
114 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
115 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
116 };
117static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
118static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
119#define CFB_TEST_SIZE 24
120static unsigned char plain[CFB_TEST_SIZE]=
121 {
122 0x4e,0x6f,0x77,0x20,0x69,0x73,
123 0x20,0x74,0x68,0x65,0x20,0x74,
124 0x69,0x6d,0x65,0x20,0x66,0x6f,
125 0x72,0x20,0x61,0x6c,0x6c,0x20
126 };
127static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
128 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
129 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
130 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
131
132/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
133 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
134 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
135 };
136
137
138/*static int cfb64_test(unsigned char *cfb_cipher);*/
139static char *pt(unsigned char *p);
140#endif
141
142int main(int argc, char *argv[])
143 {
144 int i,n,err=0;
145 RC2_KEY key;
146 unsigned char buf[8],buf2[8];
147
148 for (n=0; n<4; n++)
149 {
150 RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);
151
152 RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
153 if (memcmp(&(RC2cipher[n][0]),buf,8) != 0)
154 {
155 printf("ecb rc2 error encrypting\n");
156 printf("got :");
157 for (i=0; i<8; i++)
158 printf("%02X ",buf[i]);
159 printf("\n");
160 printf("expected:");
161 for (i=0; i<8; i++)
162 printf("%02X ",RC2cipher[n][i]);
163 err=20;
164 printf("\n");
165 }
166
167 RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
168 if (memcmp(&(RC2plain[n][0]),buf2,8) != 0)
169 {
170 printf("ecb RC2 error decrypting\n");
171 printf("got :");
172 for (i=0; i<8; i++)
173 printf("%02X ",buf[i]);
174 printf("\n");
175 printf("expected:");
176 for (i=0; i<8; i++)
177 printf("%02X ",RC2plain[n][i]);
178 printf("\n");
179 err=3;
180 }
181 }
182
183 if (err == 0) printf("ecb RC2 ok\n");
184#ifdef undef
185 memcpy(iv,k,8);
186 idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
187 memcpy(iv,k,8);
188 idea_cbc_encrypt(out,out,8,&dkey,iv,0);
189 idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
190 if (memcmp(text,out,strlen(text)+1) != 0)
191 {
192 printf("cbc idea bad\n");
193 err=4;
194 }
195 else
196 printf("cbc idea ok\n");
197
198 printf("cfb64 idea ");
199 if (cfb64_test(cfb_cipher64))
200 {
201 printf("bad\n");
202 err=5;
203 }
204 else
205 printf("ok\n");
206#endif
207
208#ifdef OPENSSL_SYS_NETWARE
209 if (err) printf("ERROR: %d\n", err);
210#endif
211 EXIT(err);
212 return(err);
213 }
214
215#ifdef undef
216static int cfb64_test(unsigned char *cfb_cipher)
217 {
218 IDEA_KEY_SCHEDULE eks,dks;
219 int err=0,i,n;
220
221 idea_set_encrypt_key(cfb_key,&eks);
222 idea_set_decrypt_key(&eks,&dks);
223 memcpy(cfb_tmp,cfb_iv,8);
224 n=0;
225 idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
226 cfb_tmp,&n,IDEA_ENCRYPT);
227 idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
228 (long)CFB_TEST_SIZE-12,&eks,
229 cfb_tmp,&n,IDEA_ENCRYPT);
230 if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
231 {
232 err=1;
233 printf("idea_cfb64_encrypt encrypt error\n");
234 for (i=0; i<CFB_TEST_SIZE; i+=8)
235 printf("%s\n",pt(&(cfb_buf1[i])));
236 }
237 memcpy(cfb_tmp,cfb_iv,8);
238 n=0;
239 idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
240 cfb_tmp,&n,IDEA_DECRYPT);
241 idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
242 (long)CFB_TEST_SIZE-17,&dks,
243 cfb_tmp,&n,IDEA_DECRYPT);
244 if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
245 {
246 err=1;
247 printf("idea_cfb_encrypt decrypt error\n");
248 for (i=0; i<24; i+=8)
249 printf("%s\n",pt(&(cfb_buf2[i])));
250 }
251 return(err);
252 }
253
254static char *pt(unsigned char *p)
255 {
256 static char bufs[10][20];
257 static int bnum=0;
258 char *ret;
259 int i;
260 static char *f="0123456789ABCDEF";
261
262 ret= &(bufs[bnum++][0]);
263 bnum%=10;
264 for (i=0; i<8; i++)
265 {
266 ret[i*2]=f[(p[i]>>4)&0xf];
267 ret[i*2+1]=f[p[i]&0xf];
268 }
269 ret[16]='\0';
270 return(ret);
271 }
272
273#endif
274#endif
diff --git a/src/lib/libssl/src/crypto/rc4/rc4test.c b/src/lib/libssl/src/crypto/rc4/rc4test.c
deleted file mode 100644
index 4312605ccb..0000000000
--- a/src/lib/libssl/src/crypto/rc4/rc4test.c
+++ /dev/null
@@ -1,242 +0,0 @@
1/* crypto/rc4/rc4test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_RC4
66int main(int argc, char *argv[])
67{
68 printf("No RC4 support\n");
69 return(0);
70}
71#else
72#include <openssl/rc4.h>
73#include <openssl/sha.h>
74
75static unsigned char keys[7][30]={
76 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
77 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
78 {8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
79 {4,0xef,0x01,0x23,0x45},
80 {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
81 {4,0xef,0x01,0x23,0x45},
82 };
83
84static unsigned char data_len[7]={8,8,8,20,28,10};
85static unsigned char data[7][30]={
86 {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
87 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
88 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
89 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
90 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
91 0x00,0x00,0x00,0x00,0xff},
92 {0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
93 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
94 0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
95 0x12,0x34,0x56,0x78,0xff},
96 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
97 {0},
98 };
99
100static unsigned char output[7][30]={
101 {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
102 {0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
103 {0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
104 {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
105 0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
106 0x36,0xb6,0x78,0x58,0x00},
107 {0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
108 0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
109 0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
110 0x40,0x01,0x1e,0xcf,0x00},
111 {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
112 {0},
113 };
114
115int main(int argc, char *argv[])
116 {
117 int i,err=0;
118 int j;
119 unsigned char *p;
120 RC4_KEY key;
121 unsigned char obuf[512];
122
123#if !defined(OPENSSL_PIC)
124 void OPENSSL_cpuid_setup(void);
125
126 OPENSSL_cpuid_setup();
127#endif
128
129 for (i=0; i<6; i++)
130 {
131 RC4_set_key(&key,keys[i][0],&(keys[i][1]));
132 memset(obuf,0x00,sizeof(obuf));
133 RC4(&key,data_len[i],&(data[i][0]),obuf);
134 if (memcmp(obuf,output[i],data_len[i]+1) != 0)
135 {
136 printf("error calculating RC4\n");
137 printf("output:");
138 for (j=0; j<data_len[i]+1; j++)
139 printf(" %02x",obuf[j]);
140 printf("\n");
141 printf("expect:");
142 p= &(output[i][0]);
143 for (j=0; j<data_len[i]+1; j++)
144 printf(" %02x",*(p++));
145 printf("\n");
146 err++;
147 }
148 else
149 printf("test %d ok\n",i);
150 }
151 printf("test end processing ");
152 for (i=0; i<data_len[3]; i++)
153 {
154 RC4_set_key(&key,keys[3][0],&(keys[3][1]));
155 memset(obuf,0x00,sizeof(obuf));
156 RC4(&key,i,&(data[3][0]),obuf);
157 if ((memcmp(obuf,output[3],i) != 0) || (obuf[i] != 0))
158 {
159 printf("error in RC4 length processing\n");
160 printf("output:");
161 for (j=0; j<i+1; j++)
162 printf(" %02x",obuf[j]);
163 printf("\n");
164 printf("expect:");
165 p= &(output[3][0]);
166 for (j=0; j<i; j++)
167 printf(" %02x",*(p++));
168 printf(" 00\n");
169 err++;
170 }
171 else
172 {
173 printf(".");
174 fflush(stdout);
175 }
176 }
177 printf("done\n");
178 printf("test multi-call ");
179 for (i=0; i<data_len[3]; i++)
180 {
181 RC4_set_key(&key,keys[3][0],&(keys[3][1]));
182 memset(obuf,0x00,sizeof(obuf));
183 RC4(&key,i,&(data[3][0]),obuf);
184 RC4(&key,data_len[3]-i,&(data[3][i]),&(obuf[i]));
185 if (memcmp(obuf,output[3],data_len[3]+1) != 0)
186 {
187 printf("error in RC4 multi-call processing\n");
188 printf("output:");
189 for (j=0; j<data_len[3]+1; j++)
190 printf(" %02x",obuf[j]);
191 printf("\n");
192 printf("expect:");
193 p= &(output[3][0]);
194 for (j=0; j<data_len[3]+1; j++)
195 printf(" %02x",*(p++));
196 err++;
197 }
198 else
199 {
200 printf(".");
201 fflush(stdout);
202 }
203 }
204 printf("done\n");
205 printf("bulk test ");
206 { unsigned char buf[513];
207 SHA_CTX c;
208 unsigned char md[SHA_DIGEST_LENGTH];
209 static unsigned char expected[]={
210 0xa4,0x7b,0xcc,0x00,0x3d,0xd0,0xbd,0xe1,0xac,0x5f,
211 0x12,0x1e,0x45,0xbc,0xfb,0x1a,0xa1,0xf2,0x7f,0xc5 };
212
213 RC4_set_key(&key,keys[0][0],&(keys[3][1]));
214 memset(buf,'\0',sizeof(buf));
215 SHA1_Init(&c);
216 for (i=0;i<2571;i++) {
217 RC4(&key,sizeof(buf),buf,buf);
218 SHA1_Update(&c,buf,sizeof(buf));
219 }
220 SHA1_Final(md,&c);
221
222 if (memcmp(md,expected,sizeof(md))) {
223 printf("error in RC4 bulk test\n");
224 printf("output:");
225 for (j=0; j<(int)sizeof(md); j++)
226 printf(" %02x",md[j]);
227 printf("\n");
228 printf("expect:");
229 for (j=0; j<(int)sizeof(md); j++)
230 printf(" %02x",expected[j]);
231 printf("\n");
232 err++;
233 }
234 else printf("ok\n");
235 }
236#ifdef OPENSSL_SYS_NETWARE
237 if (err) printf("ERROR: %d\n", err);
238#endif
239 EXIT(err);
240 return(0);
241 }
242#endif
diff --git a/src/lib/libssl/src/crypto/ripemd/rmdtest.c b/src/lib/libssl/src/crypto/ripemd/rmdtest.c
deleted file mode 100644
index b32fda96ed..0000000000
--- a/src/lib/libssl/src/crypto/ripemd/rmdtest.c
+++ /dev/null
@@ -1,138 +0,0 @@
1/* crypto/ripemd/rmdtest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_RIPEMD
66int main(int argc, char *argv[])
67{
68 printf("No ripemd support\n");
69 return(0);
70}
71#else
72#include <openssl/ripemd.h>
73#include <openssl/evp.h>
74
75static char *test[]={
76 "",
77 "a",
78 "abc",
79 "message digest",
80 "abcdefghijklmnopqrstuvwxyz",
81 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
82 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
83 "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
84 NULL,
85 };
86
87static char *ret[]={
88 "9c1185a5c5e9fc54612808977ee8f548b2258d31",
89 "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe",
90 "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc",
91 "5d0689ef49d2fae572b881b123a85ffa21595f36",
92 "f71c27109c692c1b56bbdceb5b9d2865b3708dbc",
93 "12a053384a9c0c88e405a06c27dcf49ada62eb2b",
94 "b0e20b6e3116640286ed3a87a5713079b21f5189",
95 "9b752e45573d4b39f4dbd3323cab82bf63326bfb",
96 };
97
98static char *pt(unsigned char *md);
99int main(int argc, char *argv[])
100 {
101 int i,err=0;
102 char **P,**R;
103 char *p;
104 unsigned char md[RIPEMD160_DIGEST_LENGTH];
105
106 P=test;
107 R=ret;
108 i=1;
109 while (*P != NULL)
110 {
111 EVP_Digest(&(P[0][0]),strlen((char *)*P),md,NULL,EVP_ripemd160(), NULL);
112 p=pt(md);
113 if (strcmp(p,(char *)*R) != 0)
114 {
115 printf("error calculating RIPEMD160 on '%s'\n",*P);
116 printf("got %s instead of %s\n",p,*R);
117 err++;
118 }
119 else
120 printf("test %d ok\n",i);
121 i++;
122 R++;
123 P++;
124 }
125 EXIT(err);
126 return(0);
127 }
128
129static char *pt(unsigned char *md)
130 {
131 int i;
132 static char buf[80];
133
134 for (i=0; i<RIPEMD160_DIGEST_LENGTH; i++)
135 sprintf(&(buf[i*2]),"%02x",md[i]);
136 return(buf);
137 }
138#endif
diff --git a/src/lib/libssl/src/crypto/sha/sha1test.c b/src/lib/libssl/src/crypto/sha/sha1test.c
deleted file mode 100644
index ad9e3dc407..0000000000
--- a/src/lib/libssl/src/crypto/sha/sha1test.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/* crypto/sha/sha1test.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#ifdef OPENSSL_NO_SHA
66int main(int argc, char *argv[])
67{
68 printf("No SHA support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/sha.h>
74
75#undef SHA_0 /* FIPS 180 */
76#define SHA_1 /* FIPS 180-1 */
77
78static char *test[]={
79 "abc",
80 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
81 NULL,
82 };
83
84#ifdef SHA_0
85static char *ret[]={
86 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
87 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
88 };
89static char *bigret=
90 "3232affa48628a26653b5aaa44541fd90d690603";
91#endif
92#ifdef SHA_1
93static char *ret[]={
94 "a9993e364706816aba3e25717850c26c9cd0d89d",
95 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
96 };
97static char *bigret=
98 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
99#endif
100
101static char *pt(unsigned char *md);
102int main(int argc, char *argv[])
103 {
104 int i,err=0;
105 char **P,**R;
106 static unsigned char buf[1000];
107 char *p,*r;
108 EVP_MD_CTX c;
109 unsigned char md[SHA_DIGEST_LENGTH];
110
111 EVP_MD_CTX_init(&c);
112 P=test;
113 R=ret;
114 i=1;
115 while (*P != NULL)
116 {
117 EVP_Digest(*P,strlen((char *)*P),md,NULL,EVP_sha1(), NULL);
118 p=pt(md);
119 if (strcmp(p,(char *)*R) != 0)
120 {
121 printf("error calculating SHA1 on '%s'\n",*P);
122 printf("got %s instead of %s\n",p,*R);
123 err++;
124 }
125 else
126 printf("test %d ok\n",i);
127 i++;
128 R++;
129 P++;
130 }
131
132 memset(buf,'a',1000);
133 EVP_DigestInit_ex(&c,EVP_sha1(), NULL);
134 for (i=0; i<1000; i++)
135 EVP_DigestUpdate(&c,buf,1000);
136 EVP_DigestFinal_ex(&c,md,NULL);
137 p=pt(md);
138
139 r=bigret;
140 if (strcmp(p,r) != 0)
141 {
142 printf("error calculating SHA1 on 'a' * 1000\n");
143 printf("got %s instead of %s\n",p,r);
144 err++;
145 }
146 else
147 printf("test 3 ok\n");
148
149#ifdef OPENSSL_SYS_NETWARE
150 if (err) printf("ERROR: %d\n", err);
151#endif
152 EXIT(err);
153 EVP_MD_CTX_cleanup(&c);
154 return(0);
155 }
156
157static char *pt(unsigned char *md)
158 {
159 int i;
160 static char buf[80];
161
162 for (i=0; i<SHA_DIGEST_LENGTH; i++)
163 sprintf(&(buf[i*2]),"%02x",md[i]);
164 return(buf);
165 }
166#endif
diff --git a/src/lib/libssl/src/crypto/sha/shatest.c b/src/lib/libssl/src/crypto/sha/shatest.c
deleted file mode 100644
index 6c93c39590..0000000000
--- a/src/lib/libssl/src/crypto/sha/shatest.c
+++ /dev/null
@@ -1,166 +0,0 @@
1/* crypto/sha/shatest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62
63#include "../e_os.h"
64
65#if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0)
66int main(int argc, char *argv[])
67{
68 printf("No SHA0 support\n");
69 return(0);
70}
71#else
72#include <openssl/evp.h>
73#include <openssl/sha.h>
74
75#define SHA_0 /* FIPS 180 */
76#undef SHA_1 /* FIPS 180-1 */
77
78static char *test[]={
79 "abc",
80 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
81 NULL,
82 };
83
84#ifdef SHA_0
85static char *ret[]={
86 "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880",
87 "d2516ee1acfa5baf33dfc1c471e438449ef134c8",
88 };
89static char *bigret=
90 "3232affa48628a26653b5aaa44541fd90d690603";
91#endif
92#ifdef SHA_1
93static char *ret[]={
94 "a9993e364706816aba3e25717850c26c9cd0d89d",
95 "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
96 };
97static char *bigret=
98 "34aa973cd4c4daa4f61eeb2bdbad27316534016f";
99#endif
100
101static char *pt(unsigned char *md);
102int main(int argc, char *argv[])
103 {
104 int i,err=0;
105 char **P,**R;
106 static unsigned char buf[1000];
107 char *p,*r;
108 EVP_MD_CTX c;
109 unsigned char md[SHA_DIGEST_LENGTH];
110
111 EVP_MD_CTX_init(&c);
112 P=test;
113 R=ret;
114 i=1;
115 while (*P != NULL)
116 {
117 EVP_Digest(*P,strlen(*P),md,NULL,EVP_sha(), NULL);
118 p=pt(md);
119 if (strcmp(p,*R) != 0)
120 {
121 printf("error calculating SHA on '%s'\n",*P);
122 printf("got %s instead of %s\n",p,*R);
123 err++;
124 }
125 else
126 printf("test %d ok\n",i);
127 i++;
128 R++;
129 P++;
130 }
131
132 memset(buf,'a',1000);
133 EVP_DigestInit_ex(&c,EVP_sha(), NULL);
134 for (i=0; i<1000; i++)
135 EVP_DigestUpdate(&c,buf,1000);
136 EVP_DigestFinal_ex(&c,md,NULL);
137 p=pt(md);
138
139 r=bigret;
140 if (strcmp(p,r) != 0)
141 {
142 printf("error calculating SHA on '%s'\n",p);
143 printf("got %s instead of %s\n",p,r);
144 err++;
145 }
146 else
147 printf("test 3 ok\n");
148
149#ifdef OPENSSL_SYS_NETWARE
150 if (err) printf("ERROR: %d\n", err);
151#endif
152 EVP_MD_CTX_cleanup(&c);
153 EXIT(err);
154 return(0);
155 }
156
157static char *pt(unsigned char *md)
158 {
159 int i;
160 static char buf[80];
161
162 for (i=0; i<SHA_DIGEST_LENGTH; i++)
163 sprintf(&(buf[i*2]),"%02x",md[i]);
164 return(buf);
165 }
166#endif