]>
Commit | Line | Data |
---|---|---|
440e5d80 | 1 | /* |
33388b44 | 2 | * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved. |
440e5d80 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
440e5d80 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
a4949896 BL |
10 | /* test vectors from p1ovect1.txt */ |
11 | ||
c5f87134 P |
12 | /* |
13 | * RSA low level APIs are deprecated for public use, but still ok for | |
14 | * internal use. | |
15 | */ | |
16 | #include "internal/deprecated.h" | |
17 | ||
a4949896 | 18 | #include <stdio.h> |
28847dd1 | 19 | #include <string.h> |
17e3dd1c | 20 | |
176db6dc | 21 | #include "internal/nelem.h" |
17e3dd1c | 22 | |
baac3b4b | 23 | #include <openssl/crypto.h> |
ec577822 | 24 | #include <openssl/err.h> |
105c0be0 | 25 | #include <openssl/rand.h> |
0f814687 | 26 | #include <openssl/bn.h> |
48f17396 | 27 | |
48f17396 JS |
28 | #include "testutil.h" |
29 | ||
cf1b7d96 | 30 | #ifdef OPENSSL_NO_RSA |
b158049c | 31 | int setup_tests(void) |
f5d7a031 | 32 | { |
48f17396 | 33 | /* No tests */ |
b158049c | 34 | return 1; |
f5d7a031 UM |
35 | } |
36 | #else | |
0f113f3e | 37 | # include <openssl/rsa.h> |
a4949896 | 38 | |
0f113f3e | 39 | # define SetKey \ |
9862e9aa RL |
40 | RSA_set0_key(key, \ |
41 | BN_bin2bn(n, sizeof(n)-1, NULL), \ | |
42 | BN_bin2bn(e, sizeof(e)-1, NULL), \ | |
43 | BN_bin2bn(d, sizeof(d)-1, NULL)); \ | |
44 | RSA_set0_factors(key, \ | |
45 | BN_bin2bn(p, sizeof(p)-1, NULL), \ | |
46 | BN_bin2bn(q, sizeof(q)-1, NULL)); \ | |
47 | RSA_set0_crt_params(key, \ | |
48 | BN_bin2bn(dmp1, sizeof(dmp1)-1, NULL), \ | |
49 | BN_bin2bn(dmq1, sizeof(dmq1)-1, NULL), \ | |
50 | BN_bin2bn(iqmp, sizeof(iqmp)-1, NULL)); \ | |
8240d5fa SL |
51 | if (c == NULL) \ |
52 | return 0; \ | |
9862e9aa | 53 | memcpy(c, ctext_ex, sizeof(ctext_ex) - 1); \ |
c2500f65 | 54 | return sizeof(ctext_ex) - 1; |
a4949896 | 55 | |
397f7038 | 56 | static int key1(RSA *key, unsigned char *c) |
0f113f3e | 57 | { |
7a01a45b | 58 | static unsigned char n[] = |
0f113f3e MC |
59 | "\x00\xAA\x36\xAB\xCE\x88\xAC\xFD\xFF\x55\x52\x3C\x7F\xC4\x52\x3F" |
60 | "\x90\xEF\xA0\x0D\xF3\x77\x4A\x25\x9F\x2E\x62\xB4\xC5\xD9\x9C\xB5" | |
61 | "\xAD\xB3\x00\xA0\x28\x5E\x53\x01\x93\x0E\x0C\x70\xFB\x68\x76\x93" | |
62 | "\x9C\xE6\x16\xCE\x62\x4A\x11\xE0\x08\x6D\x34\x1E\xBC\xAC\xA0\xA1" | |
63 | "\xF5"; | |
a4949896 | 64 | |
7a01a45b | 65 | static unsigned char e[] = "\x11"; |
a4949896 | 66 | |
7a01a45b | 67 | static unsigned char d[] = |
0f113f3e MC |
68 | "\x0A\x03\x37\x48\x62\x64\x87\x69\x5F\x5F\x30\xBC\x38\xB9\x8B\x44" |
69 | "\xC2\xCD\x2D\xFF\x43\x40\x98\xCD\x20\xD8\xA1\x38\xD0\x90\xBF\x64" | |
70 | "\x79\x7C\x3F\xA7\xA2\xCD\xCB\x3C\xD1\xE0\xBD\xBA\x26\x54\xB4\xF9" | |
71 | "\xDF\x8E\x8A\xE5\x9D\x73\x3D\x9F\x33\xB3\x01\x62\x4A\xFD\x1D\x51"; | |
a4949896 | 72 | |
7a01a45b | 73 | static unsigned char p[] = |
0f113f3e MC |
74 | "\x00\xD8\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5" |
75 | "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x12" | |
76 | "\x0D"; | |
77 | ||
7a01a45b | 78 | static unsigned char q[] = |
0f113f3e MC |
79 | "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9" |
80 | "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D" | |
81 | "\x89"; | |
31a674d8 | 82 | |
7a01a45b | 83 | static unsigned char dmp1[] = |
0f113f3e MC |
84 | "\x59\x0B\x95\x72\xA2\xC2\xA9\xC4\x06\x05\x9D\xC2\xAB\x2F\x1D\xAF" |
85 | "\xEB\x7E\x8B\x4F\x10\xA7\x54\x9E\x8E\xED\xF5\xB4\xFC\xE0\x9E\x05"; | |
31a674d8 | 86 | |
7a01a45b | 87 | static unsigned char dmq1[] = |
0f113f3e MC |
88 | "\x00\x8E\x3C\x05\x21\xFE\x15\xE0\xEA\x06\xA3\x6F\xF0\xF1\x0C\x99" |
89 | "\x52\xC3\x5B\x7A\x75\x14\xFD\x32\x38\xB8\x0A\xAD\x52\x98\x62\x8D" | |
90 | "\x51"; | |
31a674d8 | 91 | |
7a01a45b | 92 | static unsigned char iqmp[] = |
0f113f3e MC |
93 | "\x36\x3F\xF7\x18\x9D\xA8\xE9\x0B\x1D\x34\x1F\x71\xD0\x9B\x76\xA8" |
94 | "\xA9\x43\xE1\x1D\x10\xB2\x4D\x24\x9F\x2D\xEA\xFE\xF8\x0C\x18\x26"; | |
31a674d8 | 95 | |
7a01a45b | 96 | static unsigned char ctext_ex[] = |
0f113f3e MC |
97 | "\x1b\x8f\x05\xf9\xca\x1a\x79\x52\x6e\x53\xf3\xcc\x51\x4f\xdb\x89" |
98 | "\x2b\xfb\x91\x93\x23\x1e\x78\xb9\x92\xe6\x8d\x50\xa4\x80\xcb\x52" | |
99 | "\x33\x89\x5c\x74\x95\x8d\x5d\x02\xab\x8c\x0f\xd0\x40\xeb\x58\x44" | |
100 | "\xb0\x05\xc3\x9e\xd8\x27\x4a\x9d\xbf\xa8\x06\x71\x40\x94\x39\xd2"; | |
a4949896 BL |
101 | |
102 | SetKey; | |
0f113f3e | 103 | } |
a4949896 | 104 | |
397f7038 | 105 | static int key2(RSA *key, unsigned char *c) |
0f113f3e | 106 | { |
7a01a45b | 107 | static unsigned char n[] = |
0f113f3e MC |
108 | "\x00\xA3\x07\x9A\x90\xDF\x0D\xFD\x72\xAC\x09\x0C\xCC\x2A\x78\xB8" |
109 | "\x74\x13\x13\x3E\x40\x75\x9C\x98\xFA\xF8\x20\x4F\x35\x8A\x0B\x26" | |
110 | "\x3C\x67\x70\xE7\x83\xA9\x3B\x69\x71\xB7\x37\x79\xD2\x71\x7B\xE8" | |
111 | "\x34\x77\xCF"; | |
a4949896 | 112 | |
7a01a45b | 113 | static unsigned char e[] = "\x3"; |
a4949896 | 114 | |
7a01a45b | 115 | static unsigned char d[] = |
0f113f3e MC |
116 | "\x6C\xAF\xBC\x60\x94\xB3\xFE\x4C\x72\xB0\xB3\x32\xC6\xFB\x25\xA2" |
117 | "\xB7\x62\x29\x80\x4E\x68\x65\xFC\xA4\x5A\x74\xDF\x0F\x8F\xB8\x41" | |
118 | "\x3B\x52\xC0\xD0\xE5\x3D\x9B\x59\x0F\xF1\x9B\xE7\x9F\x49\xDD\x21" | |
119 | "\xE5\xEB"; | |
a4949896 | 120 | |
7a01a45b | 121 | static unsigned char p[] = |
0f113f3e MC |
122 | "\x00\xCF\x20\x35\x02\x8B\x9D\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92" |
123 | "\xEA\x0D\xA3\xB4\x32\x04\xB5\xCF\xCE\x91"; | |
a4949896 | 124 | |
7a01a45b | 125 | static unsigned char q[] = |
0f113f3e MC |
126 | "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9" |
127 | "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5F"; | |
128 | ||
7a01a45b | 129 | static unsigned char dmp1[] = |
0f113f3e MC |
130 | "\x00\x8A\x15\x78\xAC\x5D\x13\xAF\x10\x2B\x22\xB9\x99\xCD\x74\x61" |
131 | "\xF1\x5E\x6D\x22\xCC\x03\x23\xDF\xDF\x0B"; | |
a4949896 | 132 | |
7a01a45b | 133 | static unsigned char dmq1[] = |
0f113f3e MC |
134 | "\x00\x86\x55\x21\x4A\xC5\x4D\x8D\x4E\xCD\x61\x77\xF1\xC7\x36\x90" |
135 | "\xCE\x2A\x48\x2C\x8B\x05\x99\xCB\xE0\x3F"; | |
a4949896 | 136 | |
7a01a45b | 137 | static unsigned char iqmp[] = |
0f113f3e MC |
138 | "\x00\x83\xEF\xEF\xB8\xA9\xA4\x0D\x1D\xB6\xED\x98\xAD\x84\xED\x13" |
139 | "\x35\xDC\xC1\x08\xF3\x22\xD0\x57\xCF\x8D"; | |
a4949896 | 140 | |
7a01a45b | 141 | static unsigned char ctext_ex[] = |
0f113f3e MC |
142 | "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a" |
143 | "\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4" | |
144 | "\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52" | |
145 | "\x62\x51"; | |
a4949896 BL |
146 | |
147 | SetKey; | |
0f113f3e | 148 | } |
a4949896 | 149 | |
397f7038 | 150 | static int key3(RSA *key, unsigned char *c) |
0f113f3e | 151 | { |
7a01a45b | 152 | static unsigned char n[] = |
0f113f3e MC |
153 | "\x00\xBB\xF8\x2F\x09\x06\x82\xCE\x9C\x23\x38\xAC\x2B\x9D\xA8\x71" |
154 | "\xF7\x36\x8D\x07\xEE\xD4\x10\x43\xA4\x40\xD6\xB6\xF0\x74\x54\xF5" | |
155 | "\x1F\xB8\xDF\xBA\xAF\x03\x5C\x02\xAB\x61\xEA\x48\xCE\xEB\x6F\xCD" | |
156 | "\x48\x76\xED\x52\x0D\x60\xE1\xEC\x46\x19\x71\x9D\x8A\x5B\x8B\x80" | |
157 | "\x7F\xAF\xB8\xE0\xA3\xDF\xC7\x37\x72\x3E\xE6\xB4\xB7\xD9\x3A\x25" | |
158 | "\x84\xEE\x6A\x64\x9D\x06\x09\x53\x74\x88\x34\xB2\x45\x45\x98\x39" | |
159 | "\x4E\xE0\xAA\xB1\x2D\x7B\x61\xA5\x1F\x52\x7A\x9A\x41\xF6\xC1\x68" | |
160 | "\x7F\xE2\x53\x72\x98\xCA\x2A\x8F\x59\x46\xF8\xE5\xFD\x09\x1D\xBD" | |
161 | "\xCB"; | |
a4949896 | 162 | |
7a01a45b | 163 | static unsigned char e[] = "\x11"; |
a4949896 | 164 | |
7a01a45b | 165 | static unsigned char d[] = |
0f113f3e MC |
166 | "\x00\xA5\xDA\xFC\x53\x41\xFA\xF2\x89\xC4\xB9\x88\xDB\x30\xC1\xCD" |
167 | "\xF8\x3F\x31\x25\x1E\x06\x68\xB4\x27\x84\x81\x38\x01\x57\x96\x41" | |
168 | "\xB2\x94\x10\xB3\xC7\x99\x8D\x6B\xC4\x65\x74\x5E\x5C\x39\x26\x69" | |
169 | "\xD6\x87\x0D\xA2\xC0\x82\xA9\x39\xE3\x7F\xDC\xB8\x2E\xC9\x3E\xDA" | |
170 | "\xC9\x7F\xF3\xAD\x59\x50\xAC\xCF\xBC\x11\x1C\x76\xF1\xA9\x52\x94" | |
171 | "\x44\xE5\x6A\xAF\x68\xC5\x6C\x09\x2C\xD3\x8D\xC3\xBE\xF5\xD2\x0A" | |
172 | "\x93\x99\x26\xED\x4F\x74\xA1\x3E\xDD\xFB\xE1\xA1\xCE\xCC\x48\x94" | |
173 | "\xAF\x94\x28\xC2\xB7\xB8\x88\x3F\xE4\x46\x3A\x4B\xC8\x5B\x1C\xB3" | |
174 | "\xC1"; | |
31a674d8 | 175 | |
7a01a45b | 176 | static unsigned char p[] = |
0f113f3e MC |
177 | "\x00\xEE\xCF\xAE\x81\xB1\xB9\xB3\xC9\x08\x81\x0B\x10\xA1\xB5\x60" |
178 | "\x01\x99\xEB\x9F\x44\xAE\xF4\xFD\xA4\x93\xB8\x1A\x9E\x3D\x84\xF6" | |
179 | "\x32\x12\x4E\xF0\x23\x6E\x5D\x1E\x3B\x7E\x28\xFA\xE7\xAA\x04\x0A" | |
180 | "\x2D\x5B\x25\x21\x76\x45\x9D\x1F\x39\x75\x41\xBA\x2A\x58\xFB\x65" | |
181 | "\x99"; | |
31a674d8 | 182 | |
7a01a45b | 183 | static unsigned char q[] = |
0f113f3e MC |
184 | "\x00\xC9\x7F\xB1\xF0\x27\xF4\x53\xF6\x34\x12\x33\xEA\xAA\xD1\xD9" |
185 | "\x35\x3F\x6C\x42\xD0\x88\x66\xB1\xD0\x5A\x0F\x20\x35\x02\x8B\x9D" | |
186 | "\x86\x98\x40\xB4\x16\x66\xB4\x2E\x92\xEA\x0D\xA3\xB4\x32\x04\xB5" | |
187 | "\xCF\xCE\x33\x52\x52\x4D\x04\x16\xA5\xA4\x41\xE7\x00\xAF\x46\x15" | |
188 | "\x03"; | |
31a674d8 | 189 | |
7a01a45b | 190 | static unsigned char dmp1[] = |
0f113f3e MC |
191 | "\x54\x49\x4C\xA6\x3E\xBA\x03\x37\xE4\xE2\x40\x23\xFC\xD6\x9A\x5A" |
192 | "\xEB\x07\xDD\xDC\x01\x83\xA4\xD0\xAC\x9B\x54\xB0\x51\xF2\xB1\x3E" | |
193 | "\xD9\x49\x09\x75\xEA\xB7\x74\x14\xFF\x59\xC1\xF7\x69\x2E\x9A\x2E" | |
194 | "\x20\x2B\x38\xFC\x91\x0A\x47\x41\x74\xAD\xC9\x3C\x1F\x67\xC9\x81"; | |
31a674d8 | 195 | |
7a01a45b | 196 | static unsigned char dmq1[] = |
0f113f3e MC |
197 | "\x47\x1E\x02\x90\xFF\x0A\xF0\x75\x03\x51\xB7\xF8\x78\x86\x4C\xA9" |
198 | "\x61\xAD\xBD\x3A\x8A\x7E\x99\x1C\x5C\x05\x56\xA9\x4C\x31\x46\xA7" | |
199 | "\xF9\x80\x3F\x8F\x6F\x8A\xE3\x42\xE9\x31\xFD\x8A\xE4\x7A\x22\x0D" | |
200 | "\x1B\x99\xA4\x95\x84\x98\x07\xFE\x39\xF9\x24\x5A\x98\x36\xDA\x3D"; | |
201 | ||
7a01a45b | 202 | static unsigned char iqmp[] = |
0f113f3e MC |
203 | "\x00\xB0\x6C\x4F\xDA\xBB\x63\x01\x19\x8D\x26\x5B\xDB\xAE\x94\x23" |
204 | "\xB3\x80\xF2\x71\xF7\x34\x53\x88\x50\x93\x07\x7F\xCD\x39\xE2\x11" | |
205 | "\x9F\xC9\x86\x32\x15\x4F\x58\x83\xB1\x67\xA9\x67\xBF\x40\x2B\x4E" | |
206 | "\x9E\x2E\x0F\x96\x56\xE6\x98\xEA\x36\x66\xED\xFB\x25\x79\x80\x39" | |
207 | "\xF7"; | |
31a674d8 | 208 | |
7a01a45b | 209 | static unsigned char ctext_ex[] = |
0f113f3e MC |
210 | "\xb8\x24\x6b\x56\xa6\xed\x58\x81\xae\xb5\x85\xd9\xa2\x5b\x2a\xd7" |
211 | "\x90\xc4\x17\xe0\x80\x68\x1b\xf1\xac\x2b\xc3\xde\xb6\x9d\x8b\xce" | |
212 | "\xf0\xc4\x36\x6f\xec\x40\x0a\xf0\x52\xa7\x2e\x9b\x0e\xff\xb5\xb3" | |
213 | "\xf2\xf1\x92\xdb\xea\xca\x03\xc1\x27\x40\x05\x71\x13\xbf\x1f\x06" | |
214 | "\x69\xac\x22\xe9\xf3\xa7\x85\x2e\x3c\x15\xd9\x13\xca\xb0\xb8\x86" | |
215 | "\x3a\x95\xc9\x92\x94\xce\x86\x74\x21\x49\x54\x61\x03\x46\xf4\xd4" | |
216 | "\x74\xb2\x6f\x7c\x48\xb4\x2e\xe6\x8e\x1f\x57\x2a\x1f\xc4\x02\x6a" | |
217 | "\xc4\x56\xb4\xf5\x9f\x7b\x62\x1e\xa1\xb9\xd8\x8f\x64\x20\x2f\xb1"; | |
a4949896 BL |
218 | |
219 | SetKey; | |
0f113f3e | 220 | } |
a4949896 | 221 | |
397f7038 | 222 | static int pad_unknown(void) |
28847dd1 UM |
223 | { |
224 | unsigned long l; | |
225 | while ((l = ERR_get_error()) != 0) | |
0f113f3e | 226 | if (ERR_GET_REASON(l) == RSA_R_UNKNOWN_PADDING_TYPE) |
c2500f65 P |
227 | return 1; |
228 | return 0; | |
28847dd1 UM |
229 | } |
230 | ||
14281c47 JS |
231 | static int rsa_setkey(RSA** key, unsigned char* ctext, int idx) |
232 | { | |
233 | int clen = 0; | |
234 | *key = RSA_new(); | |
235 | switch (idx) { | |
236 | case 0: | |
237 | clen = key1(*key, ctext); | |
238 | break; | |
239 | case 1: | |
240 | clen = key2(*key, ctext); | |
241 | break; | |
242 | case 2: | |
243 | clen = key3(*key, ctext); | |
244 | break; | |
245 | } | |
246 | return clen; | |
247 | } | |
248 | ||
48f17396 | 249 | static int test_rsa_pkcs1(int idx) |
0f113f3e | 250 | { |
48f17396 | 251 | int ret = 0; |
a4949896 BL |
252 | RSA *key; |
253 | unsigned char ptext[256]; | |
254 | unsigned char ctext[256]; | |
7a01a45b | 255 | static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; |
a4949896 BL |
256 | unsigned char ctext_ex[256]; |
257 | int plen; | |
04fa4cb7 | 258 | int clen = 0; |
a4949896 BL |
259 | int num; |
260 | ||
48f17396 | 261 | plen = sizeof(ptext_ex) - 1; |
14281c47 | 262 | clen = rsa_setkey(&key, ctext_ex, idx); |
48f17396 JS |
263 | |
264 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, | |
265 | RSA_PKCS1_PADDING); | |
266 | if (!TEST_int_eq(num, clen)) | |
267 | goto err; | |
268 | ||
269 | num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING); | |
270 | if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) | |
271 | goto err; | |
272 | ||
273 | ret = 1; | |
274 | err: | |
275 | RSA_free(key); | |
276 | return ret; | |
277 | } | |
278 | ||
ac6fff70 BE |
279 | static int test_rsa_sslv23(int idx) |
280 | { | |
281 | int ret = 0; | |
282 | RSA *key; | |
283 | unsigned char ptext[256]; | |
284 | unsigned char ctext[256]; | |
285 | static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; | |
286 | unsigned char ctext_ex[256]; | |
287 | int plen; | |
288 | int clen = 0; | |
289 | int num; | |
290 | ||
291 | plen = sizeof(ptext_ex) - 1; | |
292 | clen = rsa_setkey(&key, ctext_ex, idx); | |
293 | ||
294 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, | |
295 | RSA_SSLV23_PADDING); | |
296 | if (!TEST_int_eq(num, clen)) | |
297 | goto err; | |
298 | ||
299 | num = RSA_private_decrypt(num, ctext, ptext, key, RSA_SSLV23_PADDING); | |
300 | if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) | |
301 | goto err; | |
302 | ||
303 | ret = 1; | |
304 | err: | |
305 | RSA_free(key); | |
306 | return ret; | |
307 | } | |
308 | ||
48f17396 JS |
309 | static int test_rsa_oaep(int idx) |
310 | { | |
311 | int ret = 0; | |
312 | RSA *key; | |
313 | unsigned char ptext[256]; | |
314 | unsigned char ctext[256]; | |
315 | static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; | |
316 | unsigned char ctext_ex[256]; | |
317 | int plen; | |
318 | int clen = 0; | |
319 | int num; | |
320 | int n; | |
cb78486d | 321 | |
a4949896 | 322 | plen = sizeof(ptext_ex) - 1; |
14281c47 | 323 | clen = rsa_setkey(&key, ctext_ex, idx); |
0f113f3e | 324 | |
48f17396 JS |
325 | num = RSA_public_encrypt(plen, ptext_ex, ctext, key, |
326 | RSA_PKCS1_OAEP_PADDING); | |
327 | if (num == -1 && pad_unknown()) { | |
328 | TEST_info("Skipping: No OAEP support"); | |
329 | ret = 1; | |
330 | goto err; | |
331 | } | |
332 | if (!TEST_int_eq(num, clen)) | |
333 | goto err; | |
334 | ||
335 | num = RSA_private_decrypt(num, ctext, ptext, key, | |
336 | RSA_PKCS1_OAEP_PADDING); | |
337 | if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) | |
338 | goto err; | |
339 | ||
340 | /* Different ciphertexts. Try decrypting ctext_ex */ | |
341 | num = RSA_private_decrypt(clen, ctext_ex, ptext, key, | |
342 | RSA_PKCS1_OAEP_PADDING); | |
343 | if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) | |
344 | goto err; | |
345 | ||
346 | /* Try decrypting corrupted ciphertexts. */ | |
347 | for (n = 0; n < clen; ++n) { | |
348 | ctext[n] ^= 1; | |
349 | num = RSA_private_decrypt(clen, ctext, ptext, key, | |
25d6b340 | 350 | RSA_PKCS1_OAEP_PADDING); |
48f17396 JS |
351 | if (!TEST_int_le(num, 0)) |
352 | goto err; | |
353 | ctext[n] ^= 1; | |
0f113f3e | 354 | } |
baac3b4b | 355 | |
48f17396 JS |
356 | /* Test truncated ciphertexts, as well as negative length. */ |
357 | for (n = -1; n < clen; ++n) { | |
358 | num = RSA_private_decrypt(n, ctext, ptext, key, | |
359 | RSA_PKCS1_OAEP_PADDING); | |
360 | if (!TEST_int_le(num, 0)) | |
361 | goto err; | |
362 | } | |
363 | ||
364 | ret = 1; | |
365 | err: | |
366 | RSA_free(key); | |
367 | return ret; | |
368 | } | |
baac3b4b | 369 | |
97b0b713 P |
370 | static const struct { |
371 | int bits; | |
372 | unsigned int r; | |
373 | } rsa_security_bits_cases[] = { | |
374 | /* NIST SP 800-56B rev 2 (draft) Appendix D Table 5 */ | |
375 | { 2048, 112 }, | |
376 | { 3072, 128 }, | |
377 | { 4096, 152 }, | |
378 | { 6144, 176 }, | |
379 | { 8192, 200 }, | |
380 | /* Older values */ | |
381 | { 256, 40 }, | |
382 | { 512, 56 }, | |
383 | { 1024, 80 }, | |
384 | /* Slightly different value to the 256 that NIST lists in their tables */ | |
385 | { 15360, 264 }, | |
386 | /* Some other values */ | |
387 | { 8888, 208 }, | |
388 | { 2468, 120 }, | |
389 | { 13456, 248 } | |
390 | }; | |
391 | ||
392 | static int test_rsa_security_bit(int n) | |
393 | { | |
394 | static const unsigned char vals[8] = { | |
395 | 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40 | |
396 | }; | |
397 | RSA *key = RSA_new(); | |
398 | const int bits = rsa_security_bits_cases[n].bits; | |
399 | const int result = rsa_security_bits_cases[n].r; | |
400 | const int bytes = (bits + 7) / 8; | |
401 | int r = 0; | |
402 | unsigned char num[2000]; | |
403 | ||
404 | if (!TEST_ptr(key) || !TEST_int_le(bytes, (int)sizeof(num))) | |
405 | goto err; | |
406 | ||
407 | /* | |
408 | * It is necessary to set the RSA key in order to ask for the strength. | |
409 | * A BN of an appropriate size is created, in general it won't have the | |
410 | * properties necessary for RSA to function. This is okay here since | |
411 | * the RSA key is never used. | |
412 | */ | |
413 | memset(num, vals[bits % 8], bytes); | |
414 | ||
415 | /* | |
416 | * The 'e' parameter is set to the same value as 'n'. This saves having | |
417 | * an extra BN to hold a sensible value for 'e'. This is safe since the | |
418 | * RSA key is not used. The 'd' parameter can be NULL safely. | |
419 | */ | |
420 | if (TEST_true(RSA_set0_key(key, BN_bin2bn(num, bytes, NULL), | |
421 | BN_bin2bn(num, bytes, NULL), NULL)) | |
422 | && TEST_uint_eq(RSA_security_bits(key), result)) | |
423 | r = 1; | |
424 | err: | |
425 | RSA_free(key); | |
426 | return r; | |
427 | } | |
428 | ||
ad887416 | 429 | int setup_tests(void) |
48f17396 JS |
430 | { |
431 | ADD_ALL_TESTS(test_rsa_pkcs1, 3); | |
ac6fff70 | 432 | ADD_ALL_TESTS(test_rsa_sslv23, 3); |
48f17396 | 433 | ADD_ALL_TESTS(test_rsa_oaep, 3); |
97b0b713 | 434 | ADD_ALL_TESTS(test_rsa_security_bit, OSSL_NELEM(rsa_security_bits_cases)); |
ad887416 | 435 | return 1; |
0f113f3e | 436 | } |
f5d7a031 | 437 | #endif |