1*0a6a1f1dSLionel Sambuc /* $NetBSD: test_rsa.c,v 1.1.1.2 2014/04/24 12:45:30 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include <config.h>
37ebfedea0SLionel Sambuc
38ebfedea0SLionel Sambuc #include <stdio.h>
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc #include <krb5/roken.h>
41ebfedea0SLionel Sambuc #include <krb5/getarg.h>
42ebfedea0SLionel Sambuc
43ebfedea0SLionel Sambuc #include <engine.h>
44ebfedea0SLionel Sambuc #include <evp.h>
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc /*
47ebfedea0SLionel Sambuc *
48ebfedea0SLionel Sambuc */
49ebfedea0SLionel Sambuc
50ebfedea0SLionel Sambuc static int version_flag;
51ebfedea0SLionel Sambuc static int help_flag;
52ebfedea0SLionel Sambuc static int time_keygen;
53ebfedea0SLionel Sambuc static char *time_key;
54ebfedea0SLionel Sambuc static int key_blinding = 1;
55ebfedea0SLionel Sambuc static char *rsa_key;
56ebfedea0SLionel Sambuc static char *id_flag;
57ebfedea0SLionel Sambuc static int loops = 1;
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc static struct getargs args[] = {
60ebfedea0SLionel Sambuc { "loops", 0, arg_integer, &loops,
61ebfedea0SLionel Sambuc "number of loops", "loops" },
62ebfedea0SLionel Sambuc { "id", 0, arg_string, &id_flag,
63ebfedea0SLionel Sambuc "selects the engine id", "engine-id" },
64ebfedea0SLionel Sambuc { "time-keygen", 0, arg_flag, &time_keygen,
65ebfedea0SLionel Sambuc "time rsa generation", NULL },
66ebfedea0SLionel Sambuc { "time-key", 0, arg_string, &time_key,
67ebfedea0SLionel Sambuc "rsa key file", NULL },
68ebfedea0SLionel Sambuc { "key-blinding", 0, arg_negative_flag, &key_blinding,
69ebfedea0SLionel Sambuc "key blinding", NULL },
70ebfedea0SLionel Sambuc { "key", 0, arg_string, &rsa_key,
71ebfedea0SLionel Sambuc "rsa key file", NULL },
72ebfedea0SLionel Sambuc { "version", 0, arg_flag, &version_flag,
73ebfedea0SLionel Sambuc "print version", NULL },
74ebfedea0SLionel Sambuc { "help", 0, arg_flag, &help_flag,
75ebfedea0SLionel Sambuc NULL, NULL }
76ebfedea0SLionel Sambuc };
77ebfedea0SLionel Sambuc
78ebfedea0SLionel Sambuc /*
79ebfedea0SLionel Sambuc *
80ebfedea0SLionel Sambuc */
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc static void
check_rsa(const unsigned char * in,size_t len,RSA * rsa,int padding)83ebfedea0SLionel Sambuc check_rsa(const unsigned char *in, size_t len, RSA *rsa, int padding)
84ebfedea0SLionel Sambuc {
85ebfedea0SLionel Sambuc unsigned char *res, *res2;
86ebfedea0SLionel Sambuc unsigned int len2;
87ebfedea0SLionel Sambuc int keylen;
88ebfedea0SLionel Sambuc
89ebfedea0SLionel Sambuc res = malloc(RSA_size(rsa));
90ebfedea0SLionel Sambuc if (res == NULL)
91ebfedea0SLionel Sambuc errx(1, "res: ENOMEM");
92ebfedea0SLionel Sambuc
93ebfedea0SLionel Sambuc res2 = malloc(RSA_size(rsa));
94ebfedea0SLionel Sambuc if (res2 == NULL)
95ebfedea0SLionel Sambuc errx(1, "res2: ENOMEM");
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc /* signing */
98ebfedea0SLionel Sambuc
99ebfedea0SLionel Sambuc keylen = RSA_private_encrypt(len, in, res, rsa, padding);
100ebfedea0SLionel Sambuc if (keylen <= 0)
101ebfedea0SLionel Sambuc errx(1, "failed to private encrypt: %d %d", (int)len, (int)keylen);
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc if (keylen > RSA_size(rsa))
104ebfedea0SLionel Sambuc errx(1, "keylen > RSA_size(rsa)");
105ebfedea0SLionel Sambuc
106ebfedea0SLionel Sambuc keylen = RSA_public_decrypt(keylen, res, res2, rsa, padding);
107ebfedea0SLionel Sambuc if (keylen <= 0)
108ebfedea0SLionel Sambuc errx(1, "failed to public decrypt: %d", (int)keylen);
109ebfedea0SLionel Sambuc
110ebfedea0SLionel Sambuc if (keylen != len)
111ebfedea0SLionel Sambuc errx(1, "output buffer not same length: %d", (int)keylen);
112ebfedea0SLionel Sambuc
113ebfedea0SLionel Sambuc if (memcmp(res2, in, len) != 0)
114ebfedea0SLionel Sambuc errx(1, "string not the same after decryption");
115ebfedea0SLionel Sambuc
116ebfedea0SLionel Sambuc /* encryption */
117ebfedea0SLionel Sambuc
118ebfedea0SLionel Sambuc keylen = RSA_public_encrypt(len, in, res, rsa, padding);
119ebfedea0SLionel Sambuc if (keylen <= 0)
120ebfedea0SLionel Sambuc errx(1, "failed to public encrypt: %d", (int)keylen);
121ebfedea0SLionel Sambuc
122ebfedea0SLionel Sambuc if (keylen > RSA_size(rsa))
123ebfedea0SLionel Sambuc errx(1, "keylen > RSA_size(rsa)");
124ebfedea0SLionel Sambuc
125ebfedea0SLionel Sambuc keylen = RSA_private_decrypt(keylen, res, res2, rsa, padding);
126ebfedea0SLionel Sambuc if (keylen <= 0)
127ebfedea0SLionel Sambuc errx(1, "failed to private decrypt: %d", (int)keylen);
128ebfedea0SLionel Sambuc
129ebfedea0SLionel Sambuc if (keylen != len)
130ebfedea0SLionel Sambuc errx(1, "output buffer not same length: %d", (int)keylen);
131ebfedea0SLionel Sambuc
132ebfedea0SLionel Sambuc if (memcmp(res2, in, len) != 0)
133ebfedea0SLionel Sambuc errx(1, "string not the same after decryption");
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc len2 = keylen;
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc if (RSA_sign(NID_sha1, in, len, res, &len2, rsa) != 1)
138ebfedea0SLionel Sambuc errx(1, "RSA_sign failed");
139ebfedea0SLionel Sambuc
140ebfedea0SLionel Sambuc if (RSA_verify(NID_sha1, in, len, res, len2, rsa) != 1)
141ebfedea0SLionel Sambuc errx(1, "RSA_verify failed");
142ebfedea0SLionel Sambuc
143ebfedea0SLionel Sambuc free(res);
144ebfedea0SLionel Sambuc free(res2);
145ebfedea0SLionel Sambuc }
146ebfedea0SLionel Sambuc
147ebfedea0SLionel Sambuc static int
cb_func(int a,int b,BN_GENCB * c)148ebfedea0SLionel Sambuc cb_func(int a, int b, BN_GENCB *c)
149ebfedea0SLionel Sambuc {
150ebfedea0SLionel Sambuc return 1;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc
153ebfedea0SLionel Sambuc static RSA *
read_key(ENGINE * engine,const char * rsa_key)154ebfedea0SLionel Sambuc read_key(ENGINE *engine, const char *rsa_key)
155ebfedea0SLionel Sambuc {
156ebfedea0SLionel Sambuc unsigned char buf[1024 * 4];
157ebfedea0SLionel Sambuc const unsigned char *p;
158ebfedea0SLionel Sambuc size_t size;
159ebfedea0SLionel Sambuc RSA *rsa;
160ebfedea0SLionel Sambuc FILE *f;
161ebfedea0SLionel Sambuc
162ebfedea0SLionel Sambuc f = fopen(rsa_key, "rb");
163ebfedea0SLionel Sambuc if (f == NULL)
164ebfedea0SLionel Sambuc err(1, "could not open file %s", rsa_key);
165ebfedea0SLionel Sambuc rk_cloexec_file(f);
166ebfedea0SLionel Sambuc
167ebfedea0SLionel Sambuc size = fread(buf, 1, sizeof(buf), f);
168ebfedea0SLionel Sambuc fclose(f);
169ebfedea0SLionel Sambuc if (size == 0)
170ebfedea0SLionel Sambuc err(1, "failed to read file %s", rsa_key);
171ebfedea0SLionel Sambuc if (size == sizeof(buf))
172ebfedea0SLionel Sambuc err(1, "key too long in file %s!", rsa_key);
173ebfedea0SLionel Sambuc
174ebfedea0SLionel Sambuc p = buf;
175ebfedea0SLionel Sambuc rsa = d2i_RSAPrivateKey(NULL, &p, size);
176ebfedea0SLionel Sambuc if (rsa == NULL)
177ebfedea0SLionel Sambuc err(1, "failed to parse key in file %s", rsa_key);
178ebfedea0SLionel Sambuc
179ebfedea0SLionel Sambuc RSA_set_method(rsa, ENGINE_get_RSA(engine));
180ebfedea0SLionel Sambuc
181ebfedea0SLionel Sambuc if (!key_blinding)
182ebfedea0SLionel Sambuc rsa->flags |= RSA_FLAG_NO_BLINDING;
183ebfedea0SLionel Sambuc
184ebfedea0SLionel Sambuc return rsa;
185ebfedea0SLionel Sambuc }
186ebfedea0SLionel Sambuc
187ebfedea0SLionel Sambuc /*
188ebfedea0SLionel Sambuc *
189ebfedea0SLionel Sambuc */
190ebfedea0SLionel Sambuc
191ebfedea0SLionel Sambuc static void
usage(int ret)192ebfedea0SLionel Sambuc usage (int ret)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc arg_printusage (args,
195ebfedea0SLionel Sambuc sizeof(args)/sizeof(*args),
196ebfedea0SLionel Sambuc NULL,
197ebfedea0SLionel Sambuc "filename.so");
198ebfedea0SLionel Sambuc exit (ret);
199ebfedea0SLionel Sambuc }
200ebfedea0SLionel Sambuc
201ebfedea0SLionel Sambuc int
main(int argc,char ** argv)202ebfedea0SLionel Sambuc main(int argc, char **argv)
203ebfedea0SLionel Sambuc {
204ebfedea0SLionel Sambuc ENGINE *engine = NULL;
205ebfedea0SLionel Sambuc int i, j, idx = 0;
206ebfedea0SLionel Sambuc RSA *rsa;
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc setprogname(argv[0]);
209ebfedea0SLionel Sambuc
210ebfedea0SLionel Sambuc if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &idx))
211ebfedea0SLionel Sambuc usage(1);
212ebfedea0SLionel Sambuc
213ebfedea0SLionel Sambuc if (help_flag)
214ebfedea0SLionel Sambuc usage(0);
215ebfedea0SLionel Sambuc
216ebfedea0SLionel Sambuc if(version_flag){
217ebfedea0SLionel Sambuc print_version(NULL);
218ebfedea0SLionel Sambuc exit(0);
219ebfedea0SLionel Sambuc }
220ebfedea0SLionel Sambuc
221ebfedea0SLionel Sambuc argc -= idx;
222ebfedea0SLionel Sambuc argv += idx;
223ebfedea0SLionel Sambuc
224ebfedea0SLionel Sambuc OpenSSL_add_all_algorithms();
225ebfedea0SLionel Sambuc #ifdef OPENSSL
226ebfedea0SLionel Sambuc ENGINE_load_openssl();
227ebfedea0SLionel Sambuc #endif
228ebfedea0SLionel Sambuc ENGINE_load_builtin_engines();
229ebfedea0SLionel Sambuc
230ebfedea0SLionel Sambuc if (argc == 0) {
231ebfedea0SLionel Sambuc engine = ENGINE_by_id("builtin");
232ebfedea0SLionel Sambuc } else {
233ebfedea0SLionel Sambuc engine = ENGINE_by_id(argv[0]);
234ebfedea0SLionel Sambuc if (engine == NULL)
235ebfedea0SLionel Sambuc engine = ENGINE_by_dso(argv[0], id_flag);
236ebfedea0SLionel Sambuc }
237ebfedea0SLionel Sambuc if (engine == NULL)
238ebfedea0SLionel Sambuc errx(1, "ENGINE_by_dso failed");
239ebfedea0SLionel Sambuc
240ebfedea0SLionel Sambuc if (ENGINE_get_RSA(engine) == NULL)
241ebfedea0SLionel Sambuc return 77;
242ebfedea0SLionel Sambuc
243ebfedea0SLionel Sambuc printf("rsa %s\n", ENGINE_get_RSA(engine)->name);
244ebfedea0SLionel Sambuc
245ebfedea0SLionel Sambuc if (RAND_status() != 1)
246ebfedea0SLionel Sambuc errx(77, "no functional random device, refusing to run tests");
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc if (time_keygen) {
249ebfedea0SLionel Sambuc struct timeval tv1, tv2;
250ebfedea0SLionel Sambuc BIGNUM *e;
251ebfedea0SLionel Sambuc
252ebfedea0SLionel Sambuc rsa = RSA_new_method(engine);
253ebfedea0SLionel Sambuc if (!key_blinding)
254ebfedea0SLionel Sambuc rsa->flags |= RSA_FLAG_NO_BLINDING;
255ebfedea0SLionel Sambuc
256ebfedea0SLionel Sambuc e = BN_new();
257ebfedea0SLionel Sambuc BN_set_word(e, 0x10001);
258ebfedea0SLionel Sambuc
259ebfedea0SLionel Sambuc printf("running keygen with %d loops\n", loops);
260ebfedea0SLionel Sambuc
261ebfedea0SLionel Sambuc gettimeofday(&tv1, NULL);
262ebfedea0SLionel Sambuc
263ebfedea0SLionel Sambuc for (i = 0; i < loops; i++) {
264ebfedea0SLionel Sambuc rsa = RSA_new_method(engine);
265ebfedea0SLionel Sambuc if (RSA_generate_key_ex(rsa, 1024, e, NULL) != 1)
266ebfedea0SLionel Sambuc errx(1, "RSA_generate_key_ex");
267ebfedea0SLionel Sambuc RSA_free(rsa);
268ebfedea0SLionel Sambuc }
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc gettimeofday(&tv2, NULL);
271ebfedea0SLionel Sambuc timevalsub(&tv2, &tv1);
272ebfedea0SLionel Sambuc
273ebfedea0SLionel Sambuc printf("time %lu.%06lu\n",
274ebfedea0SLionel Sambuc (unsigned long)tv2.tv_sec,
275ebfedea0SLionel Sambuc (unsigned long)tv2.tv_usec);
276ebfedea0SLionel Sambuc
277ebfedea0SLionel Sambuc BN_free(e);
278ebfedea0SLionel Sambuc ENGINE_finish(engine);
279ebfedea0SLionel Sambuc
280ebfedea0SLionel Sambuc return 0;
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc
283ebfedea0SLionel Sambuc if (time_key) {
284ebfedea0SLionel Sambuc const int size = 20;
285ebfedea0SLionel Sambuc struct timeval tv1, tv2;
286ebfedea0SLionel Sambuc unsigned char *p;
287ebfedea0SLionel Sambuc
288ebfedea0SLionel Sambuc if (strcmp(time_key, "generate") == 0) {
289ebfedea0SLionel Sambuc BIGNUM *e;
290ebfedea0SLionel Sambuc
291ebfedea0SLionel Sambuc rsa = RSA_new_method(engine);
292ebfedea0SLionel Sambuc if (!key_blinding)
293ebfedea0SLionel Sambuc rsa->flags |= RSA_FLAG_NO_BLINDING;
294ebfedea0SLionel Sambuc
295ebfedea0SLionel Sambuc e = BN_new();
296ebfedea0SLionel Sambuc BN_set_word(e, 0x10001);
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc if (RSA_generate_key_ex(rsa, 1024, e, NULL) != 1)
299ebfedea0SLionel Sambuc errx(1, "RSA_generate_key_ex");
300ebfedea0SLionel Sambuc } else {
301ebfedea0SLionel Sambuc rsa = read_key(engine, time_key);
302ebfedea0SLionel Sambuc }
303ebfedea0SLionel Sambuc
304ebfedea0SLionel Sambuc p = emalloc(loops * size);
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc RAND_bytes(p, loops * size);
307ebfedea0SLionel Sambuc
308ebfedea0SLionel Sambuc gettimeofday(&tv1, NULL);
309ebfedea0SLionel Sambuc for (i = 0; i < loops; i++)
310ebfedea0SLionel Sambuc check_rsa(p + (i * size), size, rsa, RSA_PKCS1_PADDING);
311ebfedea0SLionel Sambuc gettimeofday(&tv2, NULL);
312ebfedea0SLionel Sambuc
313ebfedea0SLionel Sambuc timevalsub(&tv2, &tv1);
314ebfedea0SLionel Sambuc
315ebfedea0SLionel Sambuc printf("time %lu.%06lu\n",
316ebfedea0SLionel Sambuc (unsigned long)tv2.tv_sec,
317ebfedea0SLionel Sambuc (unsigned long)tv2.tv_usec);
318ebfedea0SLionel Sambuc
319ebfedea0SLionel Sambuc RSA_free(rsa);
320ebfedea0SLionel Sambuc ENGINE_finish(engine);
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc return 0;
323ebfedea0SLionel Sambuc }
324ebfedea0SLionel Sambuc
325ebfedea0SLionel Sambuc if (rsa_key) {
326ebfedea0SLionel Sambuc rsa = read_key(engine, rsa_key);
327ebfedea0SLionel Sambuc
328ebfedea0SLionel Sambuc /*
329ebfedea0SLionel Sambuc * Assuming that you use the RSA key in the distribution, this
330ebfedea0SLionel Sambuc * test will generate a signature have a starting zero and thus
331ebfedea0SLionel Sambuc * will generate a checksum that is 127 byte instead of the
332ebfedea0SLionel Sambuc * checksum that is 128 byte (like the key).
333ebfedea0SLionel Sambuc */
334ebfedea0SLionel Sambuc {
335ebfedea0SLionel Sambuc const unsigned char sha1[20] = {
336ebfedea0SLionel Sambuc 0x6d, 0x33, 0xf9, 0x40, 0x75, 0x5b, 0x4e, 0xc5, 0x90, 0x35,
337ebfedea0SLionel Sambuc 0x48, 0xab, 0x75, 0x02, 0x09, 0x76, 0x9a, 0xb4, 0x7d, 0x6b
338ebfedea0SLionel Sambuc };
339ebfedea0SLionel Sambuc
340ebfedea0SLionel Sambuc check_rsa(sha1, sizeof(sha1), rsa, RSA_PKCS1_PADDING);
341ebfedea0SLionel Sambuc }
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc for (i = 0; i < 128; i++) {
344ebfedea0SLionel Sambuc unsigned char sha1[20];
345ebfedea0SLionel Sambuc
346ebfedea0SLionel Sambuc RAND_bytes(sha1, sizeof(sha1));
347ebfedea0SLionel Sambuc check_rsa(sha1, sizeof(sha1), rsa, RSA_PKCS1_PADDING);
348ebfedea0SLionel Sambuc }
349ebfedea0SLionel Sambuc for (i = 0; i < 128; i++) {
350ebfedea0SLionel Sambuc unsigned char des3[21];
351ebfedea0SLionel Sambuc
352ebfedea0SLionel Sambuc RAND_bytes(des3, sizeof(des3));
353ebfedea0SLionel Sambuc check_rsa(des3, sizeof(des3), rsa, RSA_PKCS1_PADDING);
354ebfedea0SLionel Sambuc }
355ebfedea0SLionel Sambuc for (i = 0; i < 128; i++) {
356ebfedea0SLionel Sambuc unsigned char aes[32];
357ebfedea0SLionel Sambuc
358ebfedea0SLionel Sambuc RAND_bytes(aes, sizeof(aes));
359ebfedea0SLionel Sambuc check_rsa(aes, sizeof(aes), rsa, RSA_PKCS1_PADDING);
360ebfedea0SLionel Sambuc }
361ebfedea0SLionel Sambuc
362ebfedea0SLionel Sambuc RSA_free(rsa);
363ebfedea0SLionel Sambuc }
364ebfedea0SLionel Sambuc
365ebfedea0SLionel Sambuc for (i = 0; i < loops; i++) {
366ebfedea0SLionel Sambuc BN_GENCB cb;
367ebfedea0SLionel Sambuc BIGNUM *e;
368ebfedea0SLionel Sambuc unsigned int n;
369ebfedea0SLionel Sambuc
370ebfedea0SLionel Sambuc rsa = RSA_new_method(engine);
371ebfedea0SLionel Sambuc if (!key_blinding)
372ebfedea0SLionel Sambuc rsa->flags |= RSA_FLAG_NO_BLINDING;
373ebfedea0SLionel Sambuc
374ebfedea0SLionel Sambuc e = BN_new();
375ebfedea0SLionel Sambuc BN_set_word(e, 0x10001);
376ebfedea0SLionel Sambuc
377ebfedea0SLionel Sambuc BN_GENCB_set(&cb, cb_func, NULL);
378ebfedea0SLionel Sambuc
379ebfedea0SLionel Sambuc RAND_bytes(&n, sizeof(n));
380ebfedea0SLionel Sambuc n &= 0x1ff;
381ebfedea0SLionel Sambuc n += 1024;
382ebfedea0SLionel Sambuc
383ebfedea0SLionel Sambuc if (RSA_generate_key_ex(rsa, n, e, &cb) != 1)
384ebfedea0SLionel Sambuc errx(1, "RSA_generate_key_ex");
385ebfedea0SLionel Sambuc
386ebfedea0SLionel Sambuc BN_free(e);
387ebfedea0SLionel Sambuc
388ebfedea0SLionel Sambuc for (j = 0; j < 8; j++) {
389ebfedea0SLionel Sambuc unsigned char sha1[20];
390ebfedea0SLionel Sambuc RAND_bytes(sha1, sizeof(sha1));
391ebfedea0SLionel Sambuc check_rsa(sha1, sizeof(sha1), rsa, RSA_PKCS1_PADDING);
392ebfedea0SLionel Sambuc }
393ebfedea0SLionel Sambuc
394ebfedea0SLionel Sambuc RSA_free(rsa);
395ebfedea0SLionel Sambuc }
396ebfedea0SLionel Sambuc
397ebfedea0SLionel Sambuc ENGINE_finish(engine);
398ebfedea0SLionel Sambuc
399ebfedea0SLionel Sambuc return 0;
400ebfedea0SLionel Sambuc }
401