sl@0
|
1 |
/* test vectors from p1ovect1.txt */
|
sl@0
|
2 |
/*
|
sl@0
|
3 |
© Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
|
sl@0
|
4 |
*/
|
sl@0
|
5 |
#include <stdio.h>
|
sl@0
|
6 |
#include <string.h>
|
sl@0
|
7 |
#ifndef SYMBIAN
|
sl@0
|
8 |
#include "../e_os.h"
|
sl@0
|
9 |
#else
|
sl@0
|
10 |
#include "e_os.h"
|
sl@0
|
11 |
#endif
|
sl@0
|
12 |
|
sl@0
|
13 |
|
sl@0
|
14 |
#include <openssl/crypto.h>
|
sl@0
|
15 |
#include <openssl/err.h>
|
sl@0
|
16 |
#include <openssl/rand.h>
|
sl@0
|
17 |
#include <openssl/bn.h>
|
sl@0
|
18 |
#ifdef SYMBIAN
|
sl@0
|
19 |
#ifdef stdin
|
sl@0
|
20 |
#undef stdin
|
sl@0
|
21 |
#endif
|
sl@0
|
22 |
#ifdef stdout
|
sl@0
|
23 |
#undef stdout
|
sl@0
|
24 |
#endif
|
sl@0
|
25 |
#ifdef stderr
|
sl@0
|
26 |
#undef stderr
|
sl@0
|
27 |
#endif
|
sl@0
|
28 |
|
sl@0
|
29 |
#define stdin fp_stdin
|
sl@0
|
30 |
#define stdout fp_stdout
|
sl@0
|
31 |
#define stderr fp_stderr
|
sl@0
|
32 |
|
sl@0
|
33 |
extern FILE *fp_stdout;
|
sl@0
|
34 |
extern FILE *fp_stderr;
|
sl@0
|
35 |
#endif
|
sl@0
|
36 |
#ifdef OPENSSL_NO_RSA
|
sl@0
|
37 |
int main(int argc, char *argv[])
|
sl@0
|
38 |
{
|
sl@0
|
39 |
fprintf(stdout,"No RSA support\n");
|
sl@0
|
40 |
return(0);
|
sl@0
|
41 |
}
|
sl@0
|
42 |
#else
|
sl@0
|
43 |
#include <openssl/rsa.h>
|
sl@0
|
44 |
|
sl@0
|
45 |
|
sl@0
|
46 |
#define SetKey \
|
sl@0
|
47 |
key->n = BN_bin2bn(n, sizeof(n)-1, key->n); \
|
sl@0
|
48 |
key->e = BN_bin2bn(e, sizeof(e)-1, key->e); \
|
sl@0
|
49 |
key->d = BN_bin2bn(d, sizeof(d)-1, key->d); \
|
sl@0
|
50 |
key->p = BN_bin2bn(p, sizeof(p)-1, key->p); \
|
sl@0
|
51 |
key->q = BN_bin2bn(q, sizeof(q)-1, key->q); \
|
sl@0
|
52 |
key->dmp1 = BN_bin2bn(dmp1, sizeof(dmp1)-1, key->dmp1); \
|
sl@0
|
53 |
key->dmq1 = BN_bin2bn(dmq1, sizeof(dmq1)-1, key->dmq1); \
|
sl@0
|
54 |
key->iqmp = BN_bin2bn(iqmp, sizeof(iqmp)-1, key->iqmp); \
|
sl@0
|
55 |
memcpy(c, ctext_ex, sizeof(ctext_ex) - 1); \
|
sl@0
|
56 |
return (sizeof(ctext_ex) - 1);
|
sl@0
|
57 |
|
sl@0
|
58 |
static int key1(RSA *key, unsigned char *c)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
static unsigned char n[] =
|
sl@0
|
61 |
"\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F"
|
sl@0
|
62 |
"\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5"
|
sl@0
|
63 |
"\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93"
|
sl@0
|
64 |
"\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1"
|
sl@0
|
65 |
"\xF5";
|
sl@0
|
66 |
|
sl@0
|
67 |
static unsigned char e[] = "\x11";
|
sl@0
|
68 |
|
sl@0
|
69 |
static unsigned char d[] =
|
sl@0
|
70 |
"\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44"
|
sl@0
|
71 |
"\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64"
|
sl@0
|
72 |
"\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9"
|
sl@0
|
73 |
"\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51";
|
sl@0
|
74 |
|
sl@0
|
75 |
static unsigned char p[] =
|
sl@0
|
76 |
"\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
|
sl@0
|
77 |
"\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12"
|
sl@0
|
78 |
"\x0D";
|
sl@0
|
79 |
|
sl@0
|
80 |
static unsigned char q[] =
|
sl@0
|
81 |
"\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
|
sl@0
|
82 |
"\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
|
sl@0
|
83 |
"\x89";
|
sl@0
|
84 |
|
sl@0
|
85 |
static unsigned char dmp1[] =
|
sl@0
|
86 |
"\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF"
|
sl@0
|
87 |
"\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05";
|
sl@0
|
88 |
|
sl@0
|
89 |
static unsigned char dmq1[] =
|
sl@0
|
90 |
"\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99"
|
sl@0
|
91 |
"\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D"
|
sl@0
|
92 |
"\x51";
|
sl@0
|
93 |
|
sl@0
|
94 |
static unsigned char iqmp[] =
|
sl@0
|
95 |
"\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8"
|
sl@0
|
96 |
"\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26";
|
sl@0
|
97 |
|
sl@0
|
98 |
static unsigned char ctext_ex[] =
|
sl@0
|
99 |
"\x1b\x8f\x05\xf9\xca\x1a\x79\x52\x6e\x53\xf3\xcc\x51\x4f\xdb\x89"
|
sl@0
|
100 |
"\x2b\xfb\x91\x93\x23\x1e\x78\xb9\x92\xe6\x8d\x50\xa4\x80\xcb\x52"
|
sl@0
|
101 |
"\x33\x89\x5c\x74\x95\x8d\x5d\x02\xab\x8c\x0f\xd0\x40\xeb\x58\x44"
|
sl@0
|
102 |
"\xb0\x05\xc3\x9e\xd8\x27\x4a\x9d\xbf\xa8\x06\x71\x40\x94\x39\xd2";
|
sl@0
|
103 |
|
sl@0
|
104 |
SetKey;
|
sl@0
|
105 |
}
|
sl@0
|
106 |
|
sl@0
|
107 |
static int key2(RSA *key, unsigned char *c)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
static unsigned char n[] =
|
sl@0
|
110 |
"\x00\xA3\x07\x9A\x90\xDF\x0D\xFD\x72\xAC\x09\x0C\xCC\x2A\x78\xB8"
|
sl@0
|
111 |
"\x74\x13\x13\x3E\x40\x75\x9C\x98\xFA\xF8\x20\x4F\x35\x8A\x0B\x26"
|
sl@0
|
112 |
"\x3C\x67\x70\xE7\x83\xA9\x3B\x69\x71\xB7\x37\x79\xD2\x71\x7B\xE8"
|
sl@0
|
113 |
"\x34\x77\xCF";
|
sl@0
|
114 |
|
sl@0
|
115 |
static unsigned char e[] = "\x3";
|
sl@0
|
116 |
|
sl@0
|
117 |
static unsigned char d[] =
|
sl@0
|
118 |
"\x6C\xAF\xBC\x60\x94\xB3\xFE\x4C\x72\xB0\xB3\x32\xC6\xFB\x25\xA2"
|
sl@0
|
119 |
"\xB7\x62\x29\x80\x4E\x68\x65\xFC\xA4\x5A\x74\xDF\x0F\x8F\xB8\x41"
|
sl@0
|
120 |
"\x3B\x52\xC0\xD0\xE5\x3D\x9B\x59\x0F\xF1\x9B\xE7\x9F\x49\xDD\x21"
|
sl@0
|
121 |
"\xE5\xEB";
|
sl@0
|
122 |
|
sl@0
|
123 |
static unsigned char p[] =
|
sl@0
|
124 |
"\x00\xCF\x20\x35\x02\x8B\x9D\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92"
|
sl@0
|
125 |
"\xEA\x0D\xA3\xB4\x32\x04\xB5\xCF\xCE\x91";
|
sl@0
|
126 |
|
sl@0
|
127 |
static unsigned char q[] =
|
sl@0
|
128 |
"\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
|
sl@0
|
129 |
"\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5F";
|
sl@0
|
130 |
|
sl@0
|
131 |
static unsigned char dmp1[] =
|
sl@0
|
132 |
"\x00\x8A\x15\x78\xAC\x5D\x13\xAF\x10\x2B\x22\xB9\x99\xCD\x74\x61"
|
sl@0
|
133 |
"\xF1\x5E\x6D\x22\xCC\x03\x23\xDF\xDF\x0B";
|
sl@0
|
134 |
|
sl@0
|
135 |
static unsigned char dmq1[] =
|
sl@0
|
136 |
"\x00\x86\x55\x21\x4A\xC5\x4D\x8D\x4E\xCD\x61\x77\xF1\xC7\x36\x90"
|
sl@0
|
137 |
"\xCE\x2A\x48\x2C\x8B\x05\x99\xCB\xE0\x3F";
|
sl@0
|
138 |
|
sl@0
|
139 |
static unsigned char iqmp[] =
|
sl@0
|
140 |
"\x00\x83\xEF\xEF\xB8\xA9\xA4\x0D\x1D\xB6\xED\x98\xAD\x84\xED\x13"
|
sl@0
|
141 |
"\x35\xDC\xC1\x08\xF3\x22\xD0\x57\xCF\x8D";
|
sl@0
|
142 |
|
sl@0
|
143 |
static unsigned char ctext_ex[] =
|
sl@0
|
144 |
"\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a"
|
sl@0
|
145 |
"\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4"
|
sl@0
|
146 |
"\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52"
|
sl@0
|
147 |
"\x62\x51";
|
sl@0
|
148 |
|
sl@0
|
149 |
SetKey;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
|
sl@0
|
152 |
static int key3(RSA *key, unsigned char *c)
|
sl@0
|
153 |
{
|
sl@0
|
154 |
static unsigned char n[] =
|
sl@0
|
155 |
"\x00\xBB\xF8\x2F\x09\x06\x82\xCE\x9C\x23\x38\xAC\x2B\x9D\xA8\x71"
|
sl@0
|
156 |
"\xF7\x36\x8D\x07\xEE\xD4\x10\x43\xA4\x40\xD6\xB6\xF0\x74\x54\xF5"
|
sl@0
|
157 |
"\x1F\xB8\xDF\xBA\xAF\x03\x5C\x02\xAB\x61\xEA\x48\xCE\xEB\x6F\xCD"
|
sl@0
|
158 |
"\x48\x76\xED\x52\x0D\x60\xE1\xEC\x46\x19\x71\x9D\x8A\x5B\x8B\x80"
|
sl@0
|
159 |
"\x7F\xAF\xB8\xE0\xA3\xDF\xC7\x37\x72\x3E\xE6\xB4\xB7\xD9\x3A\x25"
|
sl@0
|
160 |
"\x84\xEE\x6A\x64\x9D\x06\x09\x53\x74\x88\x34\xB2\x45\x45\x98\x39"
|
sl@0
|
161 |
"\x4E\xE0\xAA\xB1\x2D\x7B\x61\xA5\x1F\x52\x7A\x9A\x41\xF6\xC1\x68"
|
sl@0
|
162 |
"\x7F\xE2\x53\x72\x98\xCA\x2A\x8F\x59\x46\xF8\xE5\xFD\x09\x1D\xBD"
|
sl@0
|
163 |
"\xCB";
|
sl@0
|
164 |
|
sl@0
|
165 |
static unsigned char e[] = "\x11";
|
sl@0
|
166 |
|
sl@0
|
167 |
static unsigned char d[] =
|
sl@0
|
168 |
"\x00\xA5\xDA\xFC\x53\x41\xFA\xF2\x89\xC4\xB9\x88\xDB\x30\xC1\xCD"
|
sl@0
|
169 |
"\xF8\x3F\x31\x25\x1E\x06\x68\xB4\x27\x84\x81\x38\x01\x57\x96\x41"
|
sl@0
|
170 |
"\xB2\x94\x10\xB3\xC7\x99\x8D\x6B\xC4\x65\x74\x5E\x5C\x39\x26\x69"
|
sl@0
|
171 |
"\xD6\x87\x0D\xA2\xC0\x82\xA9\x39\xE3\x7F\xDC\xB8\x2E\xC9\x3E\xDA"
|
sl@0
|
172 |
"\xC9\x7F\xF3\xAD\x59\x50\xAC\xCF\xBC\x11\x1C\x76\xF1\xA9\x52\x94"
|
sl@0
|
173 |
"\x44\xE5\x6A\xAF\x68\xC5\x6C\x09\x2C\xD3\x8D\xC3\xBE\xF5\xD2\x0A"
|
sl@0
|
174 |
"\x93\x99\x26\xED\x4F\x74\xA1\x3E\xDD\xFB\xE1\xA1\xCE\xCC\x48\x94"
|
sl@0
|
175 |
"\xAF\x94\x28\xC2\xB7\xB8\x88\x3F\xE4\x46\x3A\x4B\xC8\x5B\x1C\xB3"
|
sl@0
|
176 |
"\xC1";
|
sl@0
|
177 |
|
sl@0
|
178 |
static unsigned char p[] =
|
sl@0
|
179 |
"\x00\xEE\xCF\xAE\x81\xB1\xB9\xB3\xC9\x08\x81\x0B\x10\xA1\xB5\x60"
|
sl@0
|
180 |
"\x01\x99\xEB\x9F\x44\xAE\xF4\xFD\xA4\x93\xB8\x1A\x9E\x3D\x84\xF6"
|
sl@0
|
181 |
"\x32\x12\x4E\xF0\x23\x6E\x5D\x1E\x3B\x7E\x28\xFA\xE7\xAA\x04\x0A"
|
sl@0
|
182 |
"\x2D\x5B\x25\x21\x76\x45\x9D\x1F\x39\x75\x41\xBA\x2A\x58\xFB\x65"
|
sl@0
|
183 |
"\x99";
|
sl@0
|
184 |
|
sl@0
|
185 |
static unsigned char q[] =
|
sl@0
|
186 |
"\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9"
|
sl@0
|
187 |
"\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D"
|
sl@0
|
188 |
"\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5"
|
sl@0
|
189 |
"\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x15"
|
sl@0
|
190 |
"\x03";
|
sl@0
|
191 |
|
sl@0
|
192 |
static unsigned char dmp1[] =
|
sl@0
|
193 |
"\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A"
|
sl@0
|
194 |
"\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E"
|
sl@0
|
195 |
"\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E"
|
sl@0
|
196 |
"\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81";
|
sl@0
|
197 |
|
sl@0
|
198 |
static unsigned char dmq1[] =
|
sl@0
|
199 |
"\x47\x1E\x02\x90\xFF\x0A\xF0\x75\x03\x51\xB7\xF8\x78\x86\x4C\xA9"
|
sl@0
|
200 |
"\x61\xAD\xBD\x3A\x8A\x7E\x99\x1C\x5C\x05\x56\xA9\x4C\x31\x46\xA7"
|
sl@0
|
201 |
"\xF9\x80\x3F\x8F\x6F\x8A\xE3\x42\xE9\x31\xFD\x8A\xE4\x7A\x22\x0D"
|
sl@0
|
202 |
"\x1B\x99\xA4\x95\x84\x98\x07\xFE\x39\xF9\x24\x5A\x98\x36\xDA\x3D";
|
sl@0
|
203 |
|
sl@0
|
204 |
static unsigned char iqmp[] =
|
sl@0
|
205 |
"\x00\xB0\x6C\x4F\xDA\xBB\x63\x01\x19\x8D\x26\x5B\xDB\xAE\x94\x23"
|
sl@0
|
206 |
"\xB3\x80\xF2\x71\xF7\x34\x53\x88\x50\x93\x07\x7F\xCD\x39\xE2\x11"
|
sl@0
|
207 |
"\x9F\xC9\x86\x32\x15\x4F\x58\x83\xB1\x67\xA9\x67\xBF\x40\x2B\x4E"
|
sl@0
|
208 |
"\x9E\x2E\x0F\x96\x56\xE6\x98\xEA\x36\x66\xED\xFB\x25\x79\x80\x39"
|
sl@0
|
209 |
"\xF7";
|
sl@0
|
210 |
|
sl@0
|
211 |
static unsigned char ctext_ex[] =
|
sl@0
|
212 |
"\xb8\x24\x6b\x56\xa6\xed\x58\x81\xae\xb5\x85\xd9\xa2\x5b\x2a\xd7"
|
sl@0
|
213 |
"\x90\xc4\x17\xe0\x80\x68\x1b\xf1\xac\x2b\xc3\xde\xb6\x9d\x8b\xce"
|
sl@0
|
214 |
"\xf0\xc4\x36\x6f\xec\x40\x0a\xf0\x52\xa7\x2e\x9b\x0e\xff\xb5\xb3"
|
sl@0
|
215 |
"\xf2\xf1\x92\xdb\xea\xca\x03\xc1\x27\x40\x05\x71\x13\xbf\x1f\x06"
|
sl@0
|
216 |
"\x69\xac\x22\xe9\xf3\xa7\x85\x2e\x3c\x15\xd9\x13\xca\xb0\xb8\x86"
|
sl@0
|
217 |
"\x3a\x95\xc9\x92\x94\xce\x86\x74\x21\x49\x54\x61\x03\x46\xf4\xd4"
|
sl@0
|
218 |
"\x74\xb2\x6f\x7c\x48\xb4\x2e\xe6\x8e\x1f\x57\x2a\x1f\xc4\x02\x6a"
|
sl@0
|
219 |
"\xc4\x56\xb4\xf5\x9f\x7b\x62\x1e\xa1\xb9\xd8\x8f\x64\x20\x2f\xb1";
|
sl@0
|
220 |
|
sl@0
|
221 |
SetKey;
|
sl@0
|
222 |
}
|
sl@0
|
223 |
|
sl@0
|
224 |
static int pad_unknown(void)
|
sl@0
|
225 |
{
|
sl@0
|
226 |
unsigned long l;
|
sl@0
|
227 |
while ((l = ERR_get_error()) != 0)
|
sl@0
|
228 |
if (ERR_GET_REASON(l) == RSA_R_UNKNOWN_PADDING_TYPE)
|
sl@0
|
229 |
return(1);
|
sl@0
|
230 |
return(0);
|
sl@0
|
231 |
}
|
sl@0
|
232 |
|
sl@0
|
233 |
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
|
sl@0
|
234 |
#ifndef SYMBIAN
|
sl@0
|
235 |
int main(int argc, char *argv[])
|
sl@0
|
236 |
#else
|
sl@0
|
237 |
int rsa_main(int argc, char *argv[])
|
sl@0
|
238 |
#endif
|
sl@0
|
239 |
{
|
sl@0
|
240 |
int err=0;
|
sl@0
|
241 |
int v;
|
sl@0
|
242 |
RSA *key;
|
sl@0
|
243 |
unsigned char ptext[256];
|
sl@0
|
244 |
unsigned char ctext[256];
|
sl@0
|
245 |
static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
|
sl@0
|
246 |
unsigned char ctext_ex[256];
|
sl@0
|
247 |
int plen;
|
sl@0
|
248 |
int clen = 0;
|
sl@0
|
249 |
int num;
|
sl@0
|
250 |
int n;
|
sl@0
|
251 |
|
sl@0
|
252 |
/*CRYPTO_malloc_debug_init();
|
sl@0
|
253 |
if(errno==ENOMEM)
|
sl@0
|
254 |
{
|
sl@0
|
255 |
return 1;
|
sl@0
|
256 |
}
|
sl@0
|
257 |
|
sl@0
|
258 |
CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
|
sl@0
|
259 |
if(errno==ENOMEM)
|
sl@0
|
260 |
{
|
sl@0
|
261 |
return 1;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
|
sl@0
|
264 |
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
|
sl@0
|
265 |
if(errno==ENOMEM)
|
sl@0
|
266 |
{
|
sl@0
|
267 |
return 1;
|
sl@0
|
268 |
}
|
sl@0
|
269 |
*/
|
sl@0
|
270 |
RAND_seed(rnd_seed, sizeof rnd_seed); /* or OAEP may fail */
|
sl@0
|
271 |
if(errno==ENOMEM)
|
sl@0
|
272 |
{
|
sl@0
|
273 |
return 1;
|
sl@0
|
274 |
}
|
sl@0
|
275 |
|
sl@0
|
276 |
plen = sizeof(ptext_ex) - 1;
|
sl@0
|
277 |
|
sl@0
|
278 |
for (v = 0; v < 6; v++)
|
sl@0
|
279 |
{
|
sl@0
|
280 |
key = RSA_new();
|
sl@0
|
281 |
if(errno==ENOMEM)
|
sl@0
|
282 |
{
|
sl@0
|
283 |
return 1;
|
sl@0
|
284 |
}
|
sl@0
|
285 |
|
sl@0
|
286 |
switch (v%3) {
|
sl@0
|
287 |
case 0:
|
sl@0
|
288 |
clen = key1(key, ctext_ex);
|
sl@0
|
289 |
if(errno==ENOMEM)
|
sl@0
|
290 |
{
|
sl@0
|
291 |
return 1;
|
sl@0
|
292 |
}
|
sl@0
|
293 |
break;
|
sl@0
|
294 |
case 1:
|
sl@0
|
295 |
clen = key2(key, ctext_ex);
|
sl@0
|
296 |
if(errno==ENOMEM)
|
sl@0
|
297 |
{
|
sl@0
|
298 |
return 1;
|
sl@0
|
299 |
}
|
sl@0
|
300 |
break;
|
sl@0
|
301 |
case 2:
|
sl@0
|
302 |
clen = key3(key, ctext_ex);
|
sl@0
|
303 |
if(errno==ENOMEM)
|
sl@0
|
304 |
{
|
sl@0
|
305 |
return 1;
|
sl@0
|
306 |
}
|
sl@0
|
307 |
break;
|
sl@0
|
308 |
}
|
sl@0
|
309 |
if (v/3 >= 1) key->flags |= RSA_FLAG_NO_CONSTTIME;
|
sl@0
|
310 |
|
sl@0
|
311 |
num = RSA_public_encrypt(plen, ptext_ex, ctext, key,
|
sl@0
|
312 |
RSA_PKCS1_PADDING);
|
sl@0
|
313 |
if(errno==ENOMEM)
|
sl@0
|
314 |
{
|
sl@0
|
315 |
return 1;
|
sl@0
|
316 |
}
|
sl@0
|
317 |
if (num != clen)
|
sl@0
|
318 |
{
|
sl@0
|
319 |
fprintf(stdout,"PKCS#1 v1.5 encryption failed!\n");
|
sl@0
|
320 |
err=1;
|
sl@0
|
321 |
goto oaep;
|
sl@0
|
322 |
}
|
sl@0
|
323 |
|
sl@0
|
324 |
num = RSA_private_decrypt(num, ctext, ptext, key,
|
sl@0
|
325 |
RSA_PKCS1_PADDING);
|
sl@0
|
326 |
if(errno==ENOMEM)
|
sl@0
|
327 |
{
|
sl@0
|
328 |
return 1;
|
sl@0
|
329 |
}
|
sl@0
|
330 |
if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
|
sl@0
|
331 |
{
|
sl@0
|
332 |
fprintf(stdout,"PKCS#1 v1.5 decryption failed!\n");
|
sl@0
|
333 |
err=1;
|
sl@0
|
334 |
}
|
sl@0
|
335 |
else
|
sl@0
|
336 |
fprintf(stdout,"PKCS #1 v1.5 encryption/decryption ok\n");
|
sl@0
|
337 |
|
sl@0
|
338 |
oaep:
|
sl@0
|
339 |
ERR_clear_error();
|
sl@0
|
340 |
if(errno==ENOMEM)
|
sl@0
|
341 |
{
|
sl@0
|
342 |
return 1;
|
sl@0
|
343 |
}
|
sl@0
|
344 |
num = RSA_public_encrypt(plen, ptext_ex, ctext, key,
|
sl@0
|
345 |
RSA_PKCS1_OAEP_PADDING);
|
sl@0
|
346 |
if(errno==ENOMEM)
|
sl@0
|
347 |
{
|
sl@0
|
348 |
return 1;
|
sl@0
|
349 |
}
|
sl@0
|
350 |
if (num == -1 && pad_unknown())
|
sl@0
|
351 |
{
|
sl@0
|
352 |
printf("No OAEP support\n");
|
sl@0
|
353 |
goto next;
|
sl@0
|
354 |
}
|
sl@0
|
355 |
if (num != clen)
|
sl@0
|
356 |
{
|
sl@0
|
357 |
fprintf(stdout,"OAEP encryption failed!\n");
|
sl@0
|
358 |
err=1;
|
sl@0
|
359 |
goto next;
|
sl@0
|
360 |
}
|
sl@0
|
361 |
|
sl@0
|
362 |
num = RSA_private_decrypt(num, ctext, ptext, key,
|
sl@0
|
363 |
RSA_PKCS1_OAEP_PADDING);
|
sl@0
|
364 |
if(errno==ENOMEM)
|
sl@0
|
365 |
{
|
sl@0
|
366 |
return 1;
|
sl@0
|
367 |
}
|
sl@0
|
368 |
if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
|
sl@0
|
369 |
{
|
sl@0
|
370 |
fprintf(stdout,"OAEP decryption (encrypted data) failed!\n");
|
sl@0
|
371 |
err=1;
|
sl@0
|
372 |
}
|
sl@0
|
373 |
else if (memcmp(ctext, ctext_ex, num) == 0)
|
sl@0
|
374 |
fprintf(stdout,"OAEP test vector %d passed!\n", v);
|
sl@0
|
375 |
|
sl@0
|
376 |
/* Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
|
sl@0
|
377 |
Try decrypting ctext_ex */
|
sl@0
|
378 |
|
sl@0
|
379 |
num = RSA_private_decrypt(clen, ctext_ex, ptext, key,
|
sl@0
|
380 |
RSA_PKCS1_OAEP_PADDING);
|
sl@0
|
381 |
if(errno==ENOMEM)
|
sl@0
|
382 |
{
|
sl@0
|
383 |
return 1;
|
sl@0
|
384 |
}
|
sl@0
|
385 |
if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
|
sl@0
|
386 |
{
|
sl@0
|
387 |
fprintf(stdout,"OAEP decryption (test vector data) failed!\n");
|
sl@0
|
388 |
err=1;
|
sl@0
|
389 |
}
|
sl@0
|
390 |
else
|
sl@0
|
391 |
fprintf(stdout,"OAEP encryption/decryption ok\n");
|
sl@0
|
392 |
/* Try decrypting corrupted ciphertexts */
|
sl@0
|
393 |
for(n = 0 ; n < clen ; ++n)
|
sl@0
|
394 |
{
|
sl@0
|
395 |
int b;
|
sl@0
|
396 |
unsigned char saved = ctext[n];
|
sl@0
|
397 |
for(b = 0 ; b < 256 ; ++b)
|
sl@0
|
398 |
{
|
sl@0
|
399 |
if(b == saved)
|
sl@0
|
400 |
continue;
|
sl@0
|
401 |
ctext[n] = b;
|
sl@0
|
402 |
num = RSA_private_decrypt(num, ctext, ptext, key,
|
sl@0
|
403 |
RSA_PKCS1_OAEP_PADDING);
|
sl@0
|
404 |
if(num > 0)
|
sl@0
|
405 |
{
|
sl@0
|
406 |
fprintf(stdout, "Corrupt data decrypted!\n");
|
sl@0
|
407 |
err = 1;
|
sl@0
|
408 |
}
|
sl@0
|
409 |
}
|
sl@0
|
410 |
}
|
sl@0
|
411 |
|
sl@0
|
412 |
next:
|
sl@0
|
413 |
RSA_free(key);
|
sl@0
|
414 |
if(errno==ENOMEM)
|
sl@0
|
415 |
{
|
sl@0
|
416 |
return 1;
|
sl@0
|
417 |
}
|
sl@0
|
418 |
}
|
sl@0
|
419 |
|
sl@0
|
420 |
CRYPTO_cleanup_all_ex_data();
|
sl@0
|
421 |
if(errno==ENOMEM)
|
sl@0
|
422 |
{
|
sl@0
|
423 |
return 1;
|
sl@0
|
424 |
}
|
sl@0
|
425 |
ERR_remove_state(0);
|
sl@0
|
426 |
if(errno==ENOMEM)
|
sl@0
|
427 |
{
|
sl@0
|
428 |
return 1;
|
sl@0
|
429 |
}
|
sl@0
|
430 |
CRYPTO_mem_leaks_fp(stderr);
|
sl@0
|
431 |
if(errno==ENOMEM)
|
sl@0
|
432 |
{
|
sl@0
|
433 |
return 1;
|
sl@0
|
434 |
}
|
sl@0
|
435 |
|
sl@0
|
436 |
#ifdef OPENSSL_SYS_NETWARE
|
sl@0
|
437 |
if (err) fprintf(stdout,"ERROR: %d\n", err);
|
sl@0
|
438 |
#endif
|
sl@0
|
439 |
fprintf(stdout,"Test case Passed\n");
|
sl@0
|
440 |
return err;
|
sl@0
|
441 |
}
|
sl@0
|
442 |
#endif
|