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