Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/rsa.h" |
Chris Jones | 66a4cd4 | 2021-03-09 16:04:12 +0000 | [diff] [blame] | 3 | #include "rsa_alt_helpers.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 4 | #include "mbedtls/md2.h" |
| 5 | #include "mbedtls/md4.h" |
| 6 | #include "mbedtls/md5.h" |
| 7 | #include "mbedtls/sha1.h" |
| 8 | #include "mbedtls/sha256.h" |
| 9 | #include "mbedtls/sha512.h" |
| 10 | #include "mbedtls/entropy.h" |
| 11 | #include "mbedtls/ctr_drbg.h" |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 12 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 13 | /* END_HEADER */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 14 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 15 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 17 | * END_DEPENDENCIES |
| 18 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 19 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 20 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
| 21 | void rsa_invalid_param( ) |
| 22 | { |
| 23 | mbedtls_rsa_context ctx; |
| 24 | const int valid_padding = MBEDTLS_RSA_PKCS_V21; |
| 25 | const int invalid_padding = 42; |
| 26 | const int valid_mode = MBEDTLS_RSA_PRIVATE; |
| 27 | const int invalid_mode = 42; |
| 28 | unsigned char buf[42] = { 0 }; |
| 29 | size_t olen; |
| 30 | |
| 31 | TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) ); |
| 32 | TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) ); |
| 33 | TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) ); |
| 34 | |
| 35 | /* No more variants because only the first argument must be non-NULL. */ |
| 36 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 37 | mbedtls_rsa_import( NULL, NULL, NULL, |
| 38 | NULL, NULL, NULL ) ); |
| 39 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 40 | mbedtls_rsa_import_raw( NULL, |
| 41 | NULL, 0, |
| 42 | NULL, 0, |
| 43 | NULL, 0, |
| 44 | NULL, 0, |
| 45 | NULL, 0 ) ); |
| 46 | |
| 47 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 48 | mbedtls_rsa_complete( NULL ) ); |
| 49 | |
| 50 | /* No more variants because only the first argument must be non-NULL. */ |
| 51 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 52 | mbedtls_rsa_export( NULL, NULL, NULL, |
| 53 | NULL, NULL, NULL ) ); |
| 54 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 55 | mbedtls_rsa_export_raw( NULL, |
| 56 | NULL, 0, |
| 57 | NULL, 0, |
| 58 | NULL, 0, |
| 59 | NULL, 0, |
| 60 | NULL, 0 ) ); |
| 61 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 62 | mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) ); |
| 63 | |
| 64 | TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL, |
| 65 | valid_padding, 0 ) ); |
| 66 | TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx, |
| 67 | invalid_padding, 0 ) ); |
| 68 | |
| 69 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 70 | mbedtls_rsa_gen_key( NULL, |
| 71 | mbedtls_test_rnd_std_rand, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 72 | NULL, 0, 0 ) ); |
| 73 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 74 | mbedtls_rsa_gen_key( &ctx, NULL, |
| 75 | NULL, 0, 0 ) ); |
| 76 | |
| 77 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 78 | mbedtls_rsa_check_pubkey( NULL ) ); |
| 79 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 80 | mbedtls_rsa_check_privkey( NULL ) ); |
| 81 | |
| 82 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 83 | mbedtls_rsa_check_pub_priv( NULL, &ctx ) ); |
| 84 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 85 | mbedtls_rsa_check_pub_priv( &ctx, NULL ) ); |
| 86 | |
| 87 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 88 | mbedtls_rsa_public( NULL, buf, buf ) ); |
| 89 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 90 | mbedtls_rsa_public( &ctx, NULL, buf ) ); |
| 91 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 92 | mbedtls_rsa_public( &ctx, buf, NULL ) ); |
| 93 | |
| 94 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 95 | mbedtls_rsa_private( NULL, NULL, NULL, |
| 96 | buf, buf ) ); |
| 97 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 98 | mbedtls_rsa_private( &ctx, NULL, NULL, |
| 99 | NULL, buf ) ); |
| 100 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 101 | mbedtls_rsa_private( &ctx, NULL, NULL, |
| 102 | buf, NULL ) ); |
| 103 | |
| 104 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 105 | mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 106 | sizeof( buf ), buf, |
| 107 | buf ) ); |
| 108 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 109 | mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 110 | sizeof( buf ), NULL, |
| 111 | buf ) ); |
| 112 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 113 | mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 114 | sizeof( buf ), buf, |
| 115 | NULL ) ); |
| 116 | |
| 117 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 118 | mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL, |
Thomas Daubney | 53e4ac6 | 2021-05-13 18:26:49 +0100 | [diff] [blame^] | 119 | NULL, sizeof( buf ), |
| 120 | buf, buf ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 121 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 122 | mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL, |
Thomas Daubney | 53e4ac6 | 2021-05-13 18:26:49 +0100 | [diff] [blame^] | 123 | NULL, sizeof( buf ), |
| 124 | NULL, buf ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 125 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 126 | mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL, |
Thomas Daubney | 53e4ac6 | 2021-05-13 18:26:49 +0100 | [diff] [blame^] | 127 | NULL, sizeof( buf ), |
| 128 | buf, NULL ) ); |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 129 | |
| 130 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 131 | mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL, |
| 132 | valid_mode, |
| 133 | buf, sizeof( buf ), |
| 134 | sizeof( buf ), buf, |
| 135 | buf ) ); |
| 136 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 137 | mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL, |
| 138 | invalid_mode, |
| 139 | buf, sizeof( buf ), |
| 140 | sizeof( buf ), buf, |
| 141 | buf ) ); |
| 142 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 143 | mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL, |
| 144 | valid_mode, |
| 145 | NULL, sizeof( buf ), |
| 146 | sizeof( buf ), buf, |
| 147 | buf ) ); |
| 148 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 149 | mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL, |
| 150 | valid_mode, |
| 151 | buf, sizeof( buf ), |
| 152 | sizeof( buf ), NULL, |
| 153 | buf ) ); |
| 154 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 155 | mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL, |
| 156 | valid_mode, |
| 157 | buf, sizeof( buf ), |
| 158 | sizeof( buf ), buf, |
| 159 | NULL ) ); |
| 160 | |
| 161 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 162 | mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 163 | &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 164 | buf, buf, 42 ) ); |
| 165 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 166 | mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 167 | NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 168 | buf, buf, 42 ) ); |
| 169 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 170 | mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 171 | &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 172 | NULL, buf, 42 ) ); |
| 173 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 174 | mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 175 | &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 176 | buf, NULL, 42 ) ); |
| 177 | |
| 178 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 179 | mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL, |
Thomas Daubney | 3473308 | 2021-05-12 09:24:29 +0100 | [diff] [blame] | 180 | NULL, &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 181 | buf, buf, 42 ) ); |
| 182 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 183 | mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL, |
Thomas Daubney | 3473308 | 2021-05-12 09:24:29 +0100 | [diff] [blame] | 184 | NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 185 | buf, buf, 42 ) ); |
| 186 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 187 | mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL, |
Thomas Daubney | 3473308 | 2021-05-12 09:24:29 +0100 | [diff] [blame] | 188 | NULL, &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 189 | NULL, buf, 42 ) ); |
| 190 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 191 | mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL, |
Thomas Daubney | 3473308 | 2021-05-12 09:24:29 +0100 | [diff] [blame] | 192 | NULL, &olen, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 193 | buf, NULL, 42 ) ); |
| 194 | |
| 195 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 196 | mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 197 | buf, sizeof( buf ), |
| 198 | &olen, |
| 199 | buf, buf, 42 ) ); |
| 200 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 201 | mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 202 | NULL, sizeof( buf ), |
| 203 | NULL, |
| 204 | buf, buf, 42 ) ); |
| 205 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 206 | mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 207 | buf, sizeof( buf ), |
| 208 | &olen, |
| 209 | NULL, buf, 42 ) ); |
| 210 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 211 | mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 212 | buf, sizeof( buf ), |
| 213 | &olen, |
| 214 | buf, NULL, 42 ) ); |
| 215 | |
| 216 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 217 | mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL, |
| 218 | valid_mode, |
| 219 | 0, sizeof( buf ), buf, |
| 220 | buf ) ); |
| 221 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 222 | mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL, |
| 223 | invalid_mode, |
| 224 | 0, sizeof( buf ), buf, |
| 225 | buf ) ); |
| 226 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 227 | mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL, |
| 228 | valid_mode, |
| 229 | 0, sizeof( buf ), NULL, |
| 230 | buf ) ); |
| 231 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 232 | mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL, |
| 233 | valid_mode, |
| 234 | 0, sizeof( buf ), buf, |
| 235 | NULL ) ); |
| 236 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 237 | mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL, |
| 238 | valid_mode, |
| 239 | MBEDTLS_MD_SHA1, |
| 240 | 0, NULL, |
| 241 | buf ) ); |
| 242 | |
| 243 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 244 | mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL, |
| 245 | valid_mode, |
| 246 | 0, sizeof( buf ), buf, |
| 247 | buf ) ); |
| 248 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 249 | mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL, |
| 250 | invalid_mode, |
| 251 | 0, sizeof( buf ), buf, |
| 252 | buf ) ); |
| 253 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 254 | mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL, |
| 255 | valid_mode, |
| 256 | 0, sizeof( buf ), NULL, |
| 257 | buf ) ); |
| 258 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 259 | mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL, |
| 260 | valid_mode, |
| 261 | 0, sizeof( buf ), buf, |
| 262 | NULL ) ); |
| 263 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 264 | mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL, |
| 265 | valid_mode, |
| 266 | MBEDTLS_MD_SHA1, |
| 267 | 0, NULL, |
| 268 | buf ) ); |
| 269 | |
| 270 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 271 | mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL, |
| 272 | valid_mode, |
| 273 | 0, sizeof( buf ), buf, |
| 274 | buf ) ); |
| 275 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 276 | mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL, |
| 277 | invalid_mode, |
| 278 | 0, sizeof( buf ), buf, |
| 279 | buf ) ); |
| 280 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 281 | mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL, |
| 282 | valid_mode, |
| 283 | 0, sizeof( buf ), NULL, |
| 284 | buf ) ); |
| 285 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 286 | mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL, |
| 287 | valid_mode, |
| 288 | 0, sizeof( buf ), buf, |
| 289 | NULL ) ); |
| 290 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 291 | mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL, |
| 292 | valid_mode, |
| 293 | MBEDTLS_MD_SHA1, |
| 294 | 0, NULL, |
| 295 | buf ) ); |
| 296 | |
| 297 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
Cédric Meuter | a05cbec | 2020-04-25 15:02:34 +0200 | [diff] [blame] | 298 | mbedtls_rsa_rsassa_pss_sign_ext( NULL, NULL, NULL, |
| 299 | 0, sizeof( buf ), buf, |
| 300 | MBEDTLS_RSA_SALT_LEN_ANY, |
| 301 | buf ) ); |
| 302 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 303 | mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL, |
| 304 | 0, sizeof( buf ), NULL, |
| 305 | MBEDTLS_RSA_SALT_LEN_ANY, |
| 306 | buf ) ); |
| 307 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 308 | mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL, |
| 309 | 0, sizeof( buf ), buf, |
| 310 | MBEDTLS_RSA_SALT_LEN_ANY, |
| 311 | NULL ) ); |
| 312 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 313 | mbedtls_rsa_rsassa_pss_sign_ext( &ctx, NULL, NULL, |
| 314 | MBEDTLS_MD_SHA1, |
| 315 | 0, NULL, |
| 316 | MBEDTLS_RSA_SALT_LEN_ANY, |
| 317 | buf ) ); |
| 318 | |
| 319 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 320 | mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL, |
| 321 | valid_mode, |
| 322 | 0, sizeof( buf ), buf, |
| 323 | buf ) ); |
| 324 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 325 | mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, |
| 326 | invalid_mode, |
| 327 | 0, sizeof( buf ), buf, |
| 328 | buf ) ); |
| 329 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 330 | mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, |
| 331 | valid_mode, |
| 332 | 0, sizeof( buf ), NULL, |
| 333 | buf ) ); |
| 334 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 335 | mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, |
| 336 | valid_mode, |
| 337 | 0, sizeof( buf ), buf, |
| 338 | NULL ) ); |
| 339 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 340 | mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, |
| 341 | valid_mode, |
| 342 | MBEDTLS_MD_SHA1, 0, NULL, |
| 343 | buf ) ); |
| 344 | |
| 345 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 346 | mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL, |
| 347 | NULL, |
| 348 | valid_mode, |
| 349 | 0, sizeof( buf ), buf, |
| 350 | buf ) ); |
| 351 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 352 | mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL, |
| 353 | NULL, |
| 354 | invalid_mode, |
| 355 | 0, sizeof( buf ), buf, |
| 356 | buf ) ); |
| 357 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 358 | mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL, |
| 359 | NULL, |
| 360 | valid_mode, |
| 361 | 0, sizeof( buf ), |
| 362 | NULL, buf ) ); |
| 363 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 364 | mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL, |
| 365 | NULL, |
| 366 | valid_mode, |
| 367 | 0, sizeof( buf ), buf, |
| 368 | NULL ) ); |
| 369 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 370 | mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL, |
| 371 | NULL, |
| 372 | valid_mode, |
| 373 | MBEDTLS_MD_SHA1, |
| 374 | 0, NULL, |
| 375 | buf ) ); |
| 376 | |
| 377 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 378 | mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL, |
| 379 | valid_mode, |
| 380 | 0, sizeof( buf ), |
| 381 | buf, buf ) ); |
| 382 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 383 | mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL, |
| 384 | invalid_mode, |
| 385 | 0, sizeof( buf ), |
| 386 | buf, buf ) ); |
| 387 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 388 | mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL, |
| 389 | valid_mode, |
| 390 | 0, sizeof( buf ), |
| 391 | NULL, buf ) ); |
| 392 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 393 | mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL, |
| 394 | valid_mode, |
| 395 | 0, sizeof( buf ), |
| 396 | buf, NULL ) ); |
| 397 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 398 | mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL, |
| 399 | valid_mode, |
| 400 | MBEDTLS_MD_SHA1, |
| 401 | 0, NULL, |
| 402 | buf ) ); |
| 403 | |
| 404 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 405 | mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL, |
| 406 | valid_mode, |
| 407 | 0, sizeof( buf ), |
| 408 | buf, |
| 409 | 0, 0, |
| 410 | buf ) ); |
| 411 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 412 | mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL, |
| 413 | invalid_mode, |
| 414 | 0, sizeof( buf ), |
| 415 | buf, |
| 416 | 0, 0, |
| 417 | buf ) ); |
| 418 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 419 | mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL, |
| 420 | valid_mode, |
| 421 | 0, sizeof( buf ), |
| 422 | NULL, 0, 0, |
| 423 | buf ) ); |
| 424 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 425 | mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL, |
| 426 | valid_mode, |
| 427 | 0, sizeof( buf ), |
| 428 | buf, 0, 0, |
| 429 | NULL ) ); |
| 430 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 431 | mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL, |
| 432 | valid_mode, |
| 433 | MBEDTLS_MD_SHA1, |
| 434 | 0, NULL, |
| 435 | 0, 0, |
| 436 | buf ) ); |
| 437 | |
| 438 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 439 | mbedtls_rsa_copy( NULL, &ctx ) ); |
| 440 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA, |
| 441 | mbedtls_rsa_copy( &ctx, NULL ) ); |
| 442 | |
| 443 | exit: |
| 444 | return; |
| 445 | } |
| 446 | /* END_CASE */ |
| 447 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 448 | /* BEGIN_CASE */ |
Gilles Peskine | 914afe1 | 2021-02-01 17:55:24 +0100 | [diff] [blame] | 449 | void rsa_init_free( int reinit ) |
| 450 | { |
| 451 | mbedtls_rsa_context ctx; |
| 452 | |
| 453 | /* Double free is not explicitly documented to work, but we rely on it |
| 454 | * even inside the library so that you can call mbedtls_rsa_free() |
| 455 | * unconditionally on an error path without checking whether it has |
| 456 | * already been called in the success path. */ |
| 457 | |
| 458 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 459 | mbedtls_rsa_free( &ctx ); |
| 460 | |
| 461 | if( reinit ) |
| 462 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 463 | mbedtls_rsa_free( &ctx ); |
| 464 | |
| 465 | /* This test case always succeeds, functionally speaking. A plausible |
| 466 | * bug might trigger an invalid pointer dereference or a memory leak. */ |
| 467 | goto exit; |
| 468 | } |
| 469 | /* END_CASE */ |
| 470 | |
| 471 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 472 | void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 473 | int digest, int mod, int radix_P, char * input_P, |
| 474 | int radix_Q, char * input_Q, int radix_N, |
| 475 | char * input_N, int radix_E, char * input_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 476 | data_t * result_str, int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 477 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 478 | unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; |
| 479 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 481 | mbedtls_mpi N, P, Q, E; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 482 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 483 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 484 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 485 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 487 | |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 488 | memset( hash_result, 0x00, sizeof( hash_result ) ); |
| 489 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 490 | memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 491 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 492 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 493 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 494 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 495 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 496 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 497 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 498 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 499 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 500 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 501 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 502 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 504 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 505 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 506 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand, |
| 507 | &rnd_info, MBEDTLS_RSA_PRIVATE, digest, |
| 508 | 0, hash_result, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 509 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 510 | { |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 511 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 512 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 513 | ctx.len, result_str->len ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 514 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 515 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 516 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 517 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 518 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 520 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 521 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 522 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 523 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 524 | void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 525 | int digest, int mod, int radix_N, |
| 526 | char * input_N, int radix_E, char * input_E, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 527 | data_t * result_str, int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 528 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 529 | unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | mbedtls_rsa_context ctx; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 531 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 532 | mbedtls_mpi N, E; |
| 533 | |
| 534 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 536 | memset( hash_result, 0x00, sizeof( hash_result ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 537 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 538 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 539 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 540 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 541 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 542 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 543 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 544 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 546 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 547 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 548 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 549 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 550 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 551 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 553 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 554 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 555 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 556 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 557 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 558 | void rsa_pkcs1_sign_raw( data_t * hash_result, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 559 | int padding_mode, int mod, int radix_P, |
| 560 | char * input_P, int radix_Q, char * input_Q, |
| 561 | int radix_N, char * input_N, int radix_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 562 | char * input_E, data_t * result_str ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 563 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 564 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 566 | mbedtls_mpi N, P, Q, E; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 567 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 568 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 570 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 571 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 572 | |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 573 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 574 | memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 575 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 576 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 577 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 578 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 579 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 580 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 581 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 582 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 583 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 585 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 586 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 587 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_pseudo_rand, |
| 588 | &rnd_info, MBEDTLS_RSA_PRIVATE, |
| 589 | MBEDTLS_MD_NONE, hash_result->len, |
| 590 | hash_result->x, output ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 591 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 592 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 593 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 594 | ctx.len, result_str->len ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 595 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 596 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 597 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 598 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
| 599 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 601 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 602 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 603 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 604 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 605 | void rsa_pkcs1_verify_raw( data_t * hash_result, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 606 | int padding_mode, int mod, int radix_N, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 607 | char * input_N, int radix_E, char * input_E, |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 608 | data_t * result_str, int correct ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 609 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 610 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | mbedtls_rsa_context ctx; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 612 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 613 | mbedtls_mpi N, E; |
| 614 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
| 615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 617 | memset( output, 0x00, sizeof( output ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 618 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 619 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 620 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 621 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 622 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 623 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 624 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 625 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 626 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 627 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 628 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 629 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 630 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 632 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 633 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 634 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 635 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 636 | void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 637 | int mod, int radix_N, char * input_N, |
| 638 | int radix_E, char * input_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 639 | data_t * result_str, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 640 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 641 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | mbedtls_rsa_context ctx; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 643 | mbedtls_test_rnd_pseudo_info rnd_info; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 644 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 645 | mbedtls_mpi N, E; |
| 646 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
| 647 | |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 648 | memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 651 | memset( output, 0x00, sizeof( output ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 652 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 653 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 654 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 655 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 656 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 657 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 659 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 660 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 661 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, |
| 662 | &mbedtls_test_rnd_pseudo_rand, |
Thomas Daubney | 2177277 | 2021-05-13 17:30:32 +0100 | [diff] [blame] | 663 | &rnd_info, message_str->len, |
| 664 | message_str->x, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 665 | output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 666 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 667 | { |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 668 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 669 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 670 | ctx.len, result_str->len ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 671 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 672 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 673 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 674 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 676 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 677 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 678 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 679 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 680 | void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 681 | int mod, int radix_N, char * input_N, |
| 682 | int radix_E, char * input_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 683 | data_t * result_str, int result ) |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 684 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 685 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | mbedtls_rsa_context ctx; |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 687 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 688 | mbedtls_mpi N, E; |
| 689 | |
| 690 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 692 | memset( output, 0x00, sizeof( output ) ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 693 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 694 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 695 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 696 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 697 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 698 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 699 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 700 | |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 701 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 702 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &mbedtls_test_rnd_zero_rand, |
Thomas Daubney | 2177277 | 2021-05-13 17:30:32 +0100 | [diff] [blame] | 703 | NULL, message_str->len, |
| 704 | message_str->x, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 705 | output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 706 | if( result == 0 ) |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 707 | { |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 708 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 709 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 710 | ctx.len, result_str->len ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 711 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 712 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 713 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 714 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 716 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 717 | /* END_CASE */ |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 718 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 719 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 720 | void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode, |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 721 | int mod, int radix_P, char * input_P, |
| 722 | int radix_Q, char * input_Q, int radix_N, |
| 723 | char * input_N, int radix_E, char * input_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 724 | int max_output, data_t * result_str, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 725 | int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 726 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 727 | unsigned char output[32]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | mbedtls_rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 729 | size_t output_len; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 730 | mbedtls_test_rnd_pseudo_info rnd_info; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 731 | mbedtls_mpi N, P, Q, E; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 732 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 733 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 734 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
| 735 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 737 | |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 738 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 739 | memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 740 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 741 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 742 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 743 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 744 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 745 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 746 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 747 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 748 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 749 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 751 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 752 | output_len = 0; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 753 | |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 754 | TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, mbedtls_test_rnd_pseudo_rand, |
Thomas Daubney | c7feaf3 | 2021-05-07 14:02:43 +0100 | [diff] [blame] | 755 | &rnd_info, |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 756 | &output_len, message_str->x, output, |
| 757 | max_output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 758 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 759 | { |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 760 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 761 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 762 | output_len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 763 | result_str->len ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 764 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 765 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 766 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 767 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 768 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 770 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 771 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 772 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 773 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 774 | void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 775 | char * input_N, int radix_E, char * input_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 776 | data_t * result_str, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 777 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 778 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 780 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 781 | mbedtls_mpi N, E; |
| 782 | |
| 783 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 784 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 785 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 786 | memset( output, 0x00, sizeof( output ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 787 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 788 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 789 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 790 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 791 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 792 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 793 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 794 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 795 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 796 | TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 797 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 798 | { |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 799 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 800 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 801 | ctx.len, result_str->len ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 802 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 803 | |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 804 | /* And now with the copy */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 805 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 806 | /* clear the original to be sure */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | mbedtls_rsa_free( &ctx ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 808 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 809 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 810 | |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 811 | memset( output, 0x00, sizeof( output ) ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 812 | TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 813 | if( result == 0 ) |
| 814 | { |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 815 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 816 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 817 | ctx.len, result_str->len ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 820 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 821 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 822 | mbedtls_rsa_free( &ctx ); |
| 823 | mbedtls_rsa_free( &ctx2 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 824 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 825 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 826 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 827 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 828 | void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 829 | char * input_P, int radix_Q, char * input_Q, |
| 830 | int radix_N, char * input_N, int radix_E, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 831 | char * input_E, data_t * result_str, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 832 | int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 833 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 834 | unsigned char output[256]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 835 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 836 | mbedtls_mpi N, P, Q, E; |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 837 | mbedtls_test_rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 838 | int i; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 839 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 840 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 841 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 843 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 844 | |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 845 | memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 846 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 847 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 848 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 849 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 850 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 851 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 852 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 853 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 854 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 856 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 857 | |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 858 | /* repeat three times to test updating of blinding values */ |
| 859 | for( i = 0; i < 3; i++ ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 860 | { |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 861 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 862 | TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_test_rnd_pseudo_rand, |
| 863 | &rnd_info, message_str->x, |
| 864 | output ) == result ); |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 865 | if( result == 0 ) |
| 866 | { |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 867 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 868 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 869 | ctx.len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 870 | result_str->len ) == 0 ); |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 871 | } |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 872 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 873 | |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 874 | /* And now one more time with the copy */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 876 | /* clear the original to be sure */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | mbedtls_rsa_free( &ctx ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 878 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 879 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 880 | |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 881 | memset( output, 0x00, sizeof( output ) ); |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 882 | TEST_ASSERT( mbedtls_rsa_private( &ctx2, mbedtls_test_rnd_pseudo_rand, |
| 883 | &rnd_info, message_str->x, |
| 884 | output ) == result ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 885 | if( result == 0 ) |
| 886 | { |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 887 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 888 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
Ronald Cron | 2dbba99 | 2020-06-10 11:42:32 +0200 | [diff] [blame] | 889 | ctx2.len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 890 | result_str->len ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 893 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 894 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 895 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
| 896 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 897 | mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 898 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 899 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 900 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 901 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 902 | void rsa_check_privkey_null( ) |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 903 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 904 | mbedtls_rsa_context ctx; |
| 905 | memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 906 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 907 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 908 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 909 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 910 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 911 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 912 | void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E, |
| 913 | char * input_E, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 914 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 915 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 916 | mbedtls_mpi N, E; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 917 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 918 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 920 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 921 | if( strlen( input_N ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 922 | { |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 923 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 924 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 925 | if( strlen( input_E ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 926 | { |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 927 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 930 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 932 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 933 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 934 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 936 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 937 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 938 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 939 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 940 | void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P, |
| 941 | int radix_Q, char * input_Q, int radix_N, |
| 942 | char * input_N, int radix_E, char * input_E, |
| 943 | int radix_D, char * input_D, int radix_DP, |
| 944 | char * input_DP, int radix_DQ, |
| 945 | char * input_DQ, int radix_QP, |
| 946 | char * input_QP, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 947 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | mbedtls_rsa_context ctx; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 949 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 951 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 952 | ctx.len = mod / 8; |
| 953 | if( strlen( input_P ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 954 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 955 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 956 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 957 | if( strlen( input_Q ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 958 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 960 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 961 | if( strlen( input_N ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 962 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 963 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 964 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 965 | if( strlen( input_E ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 966 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 967 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 968 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 969 | if( strlen( input_D ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 970 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 971 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 972 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 973 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 974 | if( strlen( input_DP ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 975 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 977 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 978 | if( strlen( input_DQ ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 979 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 980 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 981 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 982 | if( strlen( input_QP ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 983 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 984 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 985 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 986 | #else |
| 987 | ((void) radix_DP); ((void) input_DP); |
| 988 | ((void) radix_DQ); ((void) input_DQ); |
| 989 | ((void) radix_QP); ((void) input_QP); |
| 990 | #endif |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 991 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 993 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 994 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 996 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 997 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 998 | |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 999 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1000 | void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub, |
| 1001 | int radix_Epub, char * input_Epub, int radix_P, |
| 1002 | char * input_P, int radix_Q, char * input_Q, |
| 1003 | int radix_N, char * input_N, int radix_E, |
| 1004 | char * input_E, int radix_D, char * input_D, |
| 1005 | int radix_DP, char * input_DP, int radix_DQ, |
| 1006 | char * input_DQ, int radix_QP, char * input_QP, |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1007 | int result ) |
| 1008 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1009 | mbedtls_rsa_context pub, prv; |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1010 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 1012 | mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1013 | |
| 1014 | pub.len = mod / 8; |
| 1015 | prv.len = mod / 8; |
| 1016 | |
| 1017 | if( strlen( input_Npub ) ) |
| 1018 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1019 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1020 | } |
| 1021 | if( strlen( input_Epub ) ) |
| 1022 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1023 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if( strlen( input_P ) ) |
| 1027 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1029 | } |
| 1030 | if( strlen( input_Q ) ) |
| 1031 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1032 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1033 | } |
| 1034 | if( strlen( input_N ) ) |
| 1035 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1036 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1037 | } |
| 1038 | if( strlen( input_E ) ) |
| 1039 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1041 | } |
| 1042 | if( strlen( input_D ) ) |
| 1043 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1045 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 1046 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1047 | if( strlen( input_DP ) ) |
| 1048 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1049 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1050 | } |
| 1051 | if( strlen( input_DQ ) ) |
| 1052 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1053 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1054 | } |
| 1055 | if( strlen( input_QP ) ) |
| 1056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1058 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 1059 | #else |
| 1060 | ((void) radix_DP); ((void) input_DP); |
| 1061 | ((void) radix_DQ); ((void) input_DQ); |
| 1062 | ((void) radix_QP); ((void) input_QP); |
| 1063 | #endif |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1064 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1065 | TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1066 | |
| 1067 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1068 | mbedtls_rsa_free( &pub ); |
| 1069 | mbedtls_rsa_free( &prv ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1070 | } |
| 1071 | /* END_CASE */ |
| 1072 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 1073 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | void mbedtls_rsa_gen_key( int nrbits, int exponent, int result) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1075 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1076 | mbedtls_rsa_context ctx; |
| 1077 | mbedtls_entropy_context entropy; |
| 1078 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 1079 | const char *pers = "test_suite_rsa"; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1080 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 1081 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | 7e8e57c | 2017-07-23 10:19:29 +0100 | [diff] [blame] | 1083 | mbedtls_rsa_init ( &ctx, 0, 0 ); |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 1084 | |
Hanno Becker | a47023e | 2017-12-22 17:08:03 +0000 | [diff] [blame] | 1085 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 1086 | &entropy, (const unsigned char *) pers, |
| 1087 | strlen( pers ) ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1090 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1091 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 1093 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1094 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 1095 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1096 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1097 | mbedtls_rsa_free( &ctx ); |
| 1098 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1099 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1100 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1101 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 1102 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1103 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ |
Hanno Becker | 0f65e0c | 2017-10-03 14:39:16 +0100 | [diff] [blame] | 1104 | void mbedtls_rsa_deduce_primes( int radix_N, char *input_N, |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1105 | int radix_D, char *input_D, |
| 1106 | int radix_E, char *input_E, |
| 1107 | int radix_P, char *output_P, |
| 1108 | int radix_Q, char *output_Q, |
| 1109 | int corrupt, int result ) |
| 1110 | { |
| 1111 | mbedtls_mpi N, P, Pp, Q, Qp, D, E; |
| 1112 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1113 | mbedtls_mpi_init( &N ); |
| 1114 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1115 | mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp ); |
| 1116 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1117 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1118 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1119 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1120 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1121 | TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 ); |
| 1122 | TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 ); |
| 1123 | |
| 1124 | if( corrupt ) |
| 1125 | TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 ); |
| 1126 | |
| 1127 | /* Try to deduce P, Q from N, D, E only. */ |
Hanno Becker | f9e184b | 2017-10-10 16:49:26 +0100 | [diff] [blame] | 1128 | TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result ); |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1129 | |
| 1130 | if( !corrupt ) |
| 1131 | { |
| 1132 | /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */ |
| 1133 | TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) || |
| 1134 | ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) ); |
| 1135 | } |
| 1136 | |
| 1137 | exit: |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1138 | mbedtls_mpi_free( &N ); |
| 1139 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1140 | mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp ); |
| 1141 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 1142 | } |
| 1143 | /* END_CASE */ |
| 1144 | |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 1145 | /* BEGIN_CASE */ |
Hanno Becker | 8ba6ce4 | 2017-10-03 14:36:26 +0100 | [diff] [blame] | 1146 | void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P, |
| 1147 | int radix_Q, char *input_Q, |
| 1148 | int radix_E, char *input_E, |
| 1149 | int radix_D, char *output_D, |
| 1150 | int corrupt, int result ) |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 1151 | { |
| 1152 | mbedtls_mpi P, Q, D, Dp, E, R, Rp; |
| 1153 | |
| 1154 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1155 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp ); |
| 1156 | mbedtls_mpi_init( &E ); |
| 1157 | mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp ); |
| 1158 | |
| 1159 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1160 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1161 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1162 | TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 ); |
| 1163 | |
| 1164 | if( corrupt ) |
| 1165 | { |
| 1166 | /* Make E even */ |
| 1167 | TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 ); |
| 1168 | } |
| 1169 | |
| 1170 | /* Try to deduce D from N, P, Q, E. */ |
Hanno Becker | 8ba6ce4 | 2017-10-03 14:36:26 +0100 | [diff] [blame] | 1171 | TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q, |
| 1172 | &E, &D ) == result ); |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 1173 | |
| 1174 | if( !corrupt ) |
| 1175 | { |
| 1176 | /* |
| 1177 | * Check that D and Dp agree modulo LCM(P-1, Q-1). |
| 1178 | */ |
| 1179 | |
| 1180 | /* Replace P,Q by P-1, Q-1 */ |
| 1181 | TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 ); |
| 1182 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 ); |
| 1183 | |
| 1184 | /* Check D == Dp modulo P-1 */ |
| 1185 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 ); |
| 1186 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 ); |
| 1187 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); |
| 1188 | |
| 1189 | /* Check D == Dp modulo Q-1 */ |
| 1190 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 ); |
| 1191 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 ); |
| 1192 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); |
| 1193 | } |
| 1194 | |
| 1195 | exit: |
| 1196 | |
| 1197 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1198 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp ); |
| 1199 | mbedtls_mpi_free( &E ); |
| 1200 | mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp ); |
| 1201 | } |
| 1202 | /* END_CASE */ |
| 1203 | |
Hanno Becker | f40cdf9 | 2017-12-22 11:03:27 +0000 | [diff] [blame] | 1204 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1205 | void mbedtls_rsa_import( int radix_N, char *input_N, |
| 1206 | int radix_P, char *input_P, |
| 1207 | int radix_Q, char *input_Q, |
| 1208 | int radix_D, char *input_D, |
| 1209 | int radix_E, char *input_E, |
| 1210 | int successive, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1211 | int is_priv, |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1212 | int res_check, |
| 1213 | int res_complete ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1214 | { |
| 1215 | mbedtls_mpi N, P, Q, D, E; |
| 1216 | mbedtls_rsa_context ctx; |
| 1217 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1218 | /* Buffers used for encryption-decryption test */ |
| 1219 | unsigned char *buf_orig = NULL; |
| 1220 | unsigned char *buf_enc = NULL; |
| 1221 | unsigned char *buf_dec = NULL; |
| 1222 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1223 | mbedtls_entropy_context entropy; |
| 1224 | mbedtls_ctr_drbg_context ctr_drbg; |
| 1225 | const char *pers = "test_suite_rsa"; |
| 1226 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1227 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1228 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1229 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1230 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1231 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1232 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1233 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1234 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1235 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1236 | |
| 1237 | mbedtls_mpi_init( &N ); |
| 1238 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1239 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1240 | |
Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 1241 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
| 1242 | (const unsigned char *) pers, strlen( pers ) ) == 0 ); |
| 1243 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1244 | if( have_N ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1245 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1246 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1247 | if( have_P ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1248 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1249 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1250 | if( have_Q ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1251 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1252 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1253 | if( have_D ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1254 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1255 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1256 | if( have_E ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1257 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1258 | |
| 1259 | if( !successive ) |
| 1260 | { |
| 1261 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1262 | have_N ? &N : NULL, |
| 1263 | have_P ? &P : NULL, |
| 1264 | have_Q ? &Q : NULL, |
| 1265 | have_D ? &D : NULL, |
| 1266 | have_E ? &E : NULL ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1267 | } |
| 1268 | else |
| 1269 | { |
| 1270 | /* Import N, P, Q, D, E separately. |
| 1271 | * This should make no functional difference. */ |
| 1272 | |
| 1273 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1274 | have_N ? &N : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1275 | NULL, NULL, NULL, NULL ) == 0 ); |
| 1276 | |
| 1277 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1278 | NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1279 | have_P ? &P : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1280 | NULL, NULL, NULL ) == 0 ); |
| 1281 | |
| 1282 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1283 | NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1284 | have_Q ? &Q : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1285 | NULL, NULL ) == 0 ); |
| 1286 | |
| 1287 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1288 | NULL, NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1289 | have_D ? &D : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1290 | NULL ) == 0 ); |
| 1291 | |
| 1292 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1293 | NULL, NULL, NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 1294 | have_E ? &E : NULL ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1295 | } |
| 1296 | |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1297 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1298 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1299 | /* On expected success, perform some public and private |
| 1300 | * key operations to check if the key is working properly. */ |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1301 | if( res_complete == 0 ) |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1302 | { |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1303 | if( is_priv ) |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1304 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); |
| 1305 | else |
| 1306 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); |
| 1307 | |
| 1308 | if( res_check != 0 ) |
| 1309 | goto exit; |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1310 | |
| 1311 | buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1312 | buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1313 | buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1314 | if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) |
| 1315 | goto exit; |
| 1316 | |
| 1317 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, |
| 1318 | buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1319 | |
| 1320 | /* Make sure the number we're generating is smaller than the modulus */ |
| 1321 | buf_orig[0] = 0x00; |
| 1322 | |
| 1323 | TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); |
| 1324 | |
| 1325 | if( is_priv ) |
| 1326 | { |
| 1327 | TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, |
| 1328 | &ctr_drbg, buf_enc, |
| 1329 | buf_dec ) == 0 ); |
| 1330 | |
| 1331 | TEST_ASSERT( memcmp( buf_orig, buf_dec, |
| 1332 | mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1333 | } |
| 1334 | } |
| 1335 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1336 | exit: |
| 1337 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1338 | mbedtls_free( buf_orig ); |
| 1339 | mbedtls_free( buf_enc ); |
| 1340 | mbedtls_free( buf_dec ); |
| 1341 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1342 | mbedtls_rsa_free( &ctx ); |
| 1343 | |
| 1344 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1345 | mbedtls_entropy_free( &entropy ); |
| 1346 | |
| 1347 | mbedtls_mpi_free( &N ); |
| 1348 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1349 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1350 | } |
| 1351 | /* END_CASE */ |
| 1352 | |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1353 | /* BEGIN_CASE */ |
| 1354 | void mbedtls_rsa_export( int radix_N, char *input_N, |
| 1355 | int radix_P, char *input_P, |
| 1356 | int radix_Q, char *input_Q, |
| 1357 | int radix_D, char *input_D, |
| 1358 | int radix_E, char *input_E, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1359 | int is_priv, |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1360 | int successive ) |
| 1361 | { |
| 1362 | /* Original MPI's with which we set up the RSA context */ |
| 1363 | mbedtls_mpi N, P, Q, D, E; |
| 1364 | |
| 1365 | /* Exported MPI's */ |
| 1366 | mbedtls_mpi Ne, Pe, Qe, De, Ee; |
| 1367 | |
| 1368 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1369 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1370 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1371 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1372 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1373 | |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1374 | mbedtls_rsa_context ctx; |
| 1375 | |
| 1376 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1377 | |
| 1378 | mbedtls_mpi_init( &N ); |
| 1379 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1380 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1381 | |
| 1382 | mbedtls_mpi_init( &Ne ); |
| 1383 | mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe ); |
| 1384 | mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee ); |
| 1385 | |
| 1386 | /* Setup RSA context */ |
| 1387 | |
| 1388 | if( have_N ) |
| 1389 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1390 | |
| 1391 | if( have_P ) |
| 1392 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1393 | |
| 1394 | if( have_Q ) |
| 1395 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1396 | |
| 1397 | if( have_D ) |
| 1398 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1399 | |
| 1400 | if( have_E ) |
| 1401 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1402 | |
| 1403 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1404 | strlen( input_N ) ? &N : NULL, |
| 1405 | strlen( input_P ) ? &P : NULL, |
| 1406 | strlen( input_Q ) ? &Q : NULL, |
| 1407 | strlen( input_D ) ? &D : NULL, |
| 1408 | strlen( input_E ) ? &E : NULL ) == 0 ); |
| 1409 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 1410 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1411 | |
| 1412 | /* |
| 1413 | * Export parameters and compare to original ones. |
| 1414 | */ |
| 1415 | |
| 1416 | /* N and E must always be present. */ |
| 1417 | if( !successive ) |
| 1418 | { |
| 1419 | TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 ); |
| 1420 | } |
| 1421 | else |
| 1422 | { |
| 1423 | TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 ); |
| 1424 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 ); |
| 1425 | } |
| 1426 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 ); |
| 1427 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 ); |
| 1428 | |
| 1429 | /* If we were providing enough information to setup a complete private context, |
| 1430 | * we expect to be able to export all core parameters. */ |
| 1431 | |
| 1432 | if( is_priv ) |
| 1433 | { |
| 1434 | if( !successive ) |
| 1435 | { |
| 1436 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe, |
| 1437 | &De, NULL ) == 0 ); |
| 1438 | } |
| 1439 | else |
| 1440 | { |
| 1441 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL, |
| 1442 | NULL, NULL ) == 0 ); |
| 1443 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe, |
| 1444 | NULL, NULL ) == 0 ); |
| 1445 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, |
| 1446 | &De, NULL ) == 0 ); |
| 1447 | } |
| 1448 | |
| 1449 | if( have_P ) |
| 1450 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 ); |
| 1451 | |
| 1452 | if( have_Q ) |
| 1453 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 ); |
| 1454 | |
| 1455 | if( have_D ) |
| 1456 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 ); |
| 1457 | |
| 1458 | /* While at it, perform a sanity check */ |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1459 | TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee, |
| 1460 | NULL, NULL ) == 0 ); |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1461 | } |
| 1462 | |
| 1463 | exit: |
| 1464 | |
| 1465 | mbedtls_rsa_free( &ctx ); |
| 1466 | |
| 1467 | mbedtls_mpi_free( &N ); |
| 1468 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1469 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1470 | |
| 1471 | mbedtls_mpi_free( &Ne ); |
| 1472 | mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe ); |
| 1473 | mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee ); |
| 1474 | } |
| 1475 | /* END_CASE */ |
| 1476 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 1477 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1478 | void mbedtls_rsa_validate_params( int radix_N, char *input_N, |
| 1479 | int radix_P, char *input_P, |
| 1480 | int radix_Q, char *input_Q, |
| 1481 | int radix_D, char *input_D, |
| 1482 | int radix_E, char *input_E, |
| 1483 | int prng, int result ) |
Hanno Becker | ce00263 | 2017-08-23 13:22:36 +0100 | [diff] [blame] | 1484 | { |
| 1485 | /* Original MPI's with which we set up the RSA context */ |
| 1486 | mbedtls_mpi N, P, Q, D, E; |
| 1487 | |
| 1488 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1489 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1490 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1491 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1492 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1493 | |
| 1494 | mbedtls_entropy_context entropy; |
| 1495 | mbedtls_ctr_drbg_context ctr_drbg; |
| 1496 | const char *pers = "test_suite_rsa"; |
| 1497 | |
| 1498 | mbedtls_mpi_init( &N ); |
| 1499 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1500 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1501 | |
| 1502 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 1503 | mbedtls_entropy_init( &entropy ); |
| 1504 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 1505 | &entropy, (const unsigned char *) pers, |
| 1506 | strlen( pers ) ) == 0 ); |
| 1507 | |
| 1508 | if( have_N ) |
| 1509 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1510 | |
| 1511 | if( have_P ) |
| 1512 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1513 | |
| 1514 | if( have_Q ) |
| 1515 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1516 | |
| 1517 | if( have_D ) |
| 1518 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1519 | |
| 1520 | if( have_E ) |
| 1521 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1522 | |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1523 | TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL, |
| 1524 | have_P ? &P : NULL, |
| 1525 | have_Q ? &Q : NULL, |
| 1526 | have_D ? &D : NULL, |
| 1527 | have_E ? &E : NULL, |
| 1528 | prng ? mbedtls_ctr_drbg_random : NULL, |
| 1529 | prng ? &ctr_drbg : NULL ) == result ); |
Hanno Becker | ce00263 | 2017-08-23 13:22:36 +0100 | [diff] [blame] | 1530 | exit: |
| 1531 | |
| 1532 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1533 | mbedtls_entropy_free( &entropy ); |
| 1534 | |
| 1535 | mbedtls_mpi_free( &N ); |
| 1536 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1537 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1538 | } |
| 1539 | /* END_CASE */ |
| 1540 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1541 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 1542 | void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P, |
| 1543 | data_t *input_Q, data_t *input_D, |
| 1544 | data_t *input_E, int is_priv, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1545 | int successive ) |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1546 | { |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1547 | /* Exported buffers */ |
Ron Eldor | fdc15bd | 2018-11-22 15:47:51 +0200 | [diff] [blame] | 1548 | unsigned char bufNe[256]; |
| 1549 | unsigned char bufPe[128]; |
| 1550 | unsigned char bufQe[128]; |
| 1551 | unsigned char bufDe[256]; |
| 1552 | unsigned char bufEe[1]; |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1553 | |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1554 | mbedtls_rsa_context ctx; |
| 1555 | |
| 1556 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1557 | |
| 1558 | /* Setup RSA context */ |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1559 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1560 | input_N->len ? input_N->x : NULL, input_N->len, |
| 1561 | input_P->len ? input_P->x : NULL, input_P->len, |
| 1562 | input_Q->len ? input_Q->x : NULL, input_Q->len, |
| 1563 | input_D->len ? input_D->x : NULL, input_D->len, |
| 1564 | input_E->len ? input_E->x : NULL, input_E->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1565 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 1566 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1567 | |
| 1568 | /* |
| 1569 | * Export parameters and compare to original ones. |
| 1570 | */ |
| 1571 | |
| 1572 | /* N and E must always be present. */ |
| 1573 | if( !successive ) |
| 1574 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1575 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len, |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1576 | NULL, 0, NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1577 | bufEe, input_E->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1578 | } |
| 1579 | else |
| 1580 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1581 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len, |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1582 | NULL, 0, NULL, 0, NULL, 0, |
| 1583 | NULL, 0 ) == 0 ); |
| 1584 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
| 1585 | NULL, 0, NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1586 | bufEe, input_E->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1587 | } |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1588 | TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 ); |
| 1589 | TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1590 | |
| 1591 | /* If we were providing enough information to setup a complete private context, |
| 1592 | * we expect to be able to export all core parameters. */ |
| 1593 | |
| 1594 | if( is_priv ) |
| 1595 | { |
| 1596 | if( !successive ) |
| 1597 | { |
| 1598 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1599 | bufPe, input_P->len ? input_P->len : sizeof( bufPe ), |
| 1600 | bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ), |
| 1601 | bufDe, input_D->len ? input_D->len : sizeof( bufDe ), |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1602 | NULL, 0 ) == 0 ); |
| 1603 | } |
| 1604 | else |
| 1605 | { |
| 1606 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1607 | bufPe, input_P->len ? input_P->len : sizeof( bufPe ), |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1608 | NULL, 0, NULL, 0, |
| 1609 | NULL, 0 ) == 0 ); |
| 1610 | |
| 1611 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1612 | bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ), |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1613 | NULL, 0, NULL, 0 ) == 0 ); |
| 1614 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1615 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0, |
| 1616 | bufDe, input_D->len ? input_D->len : sizeof( bufDe ), |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1617 | NULL, 0 ) == 0 ); |
| 1618 | } |
| 1619 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1620 | if( input_P->len ) |
| 1621 | TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1622 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1623 | if( input_Q->len ) |
| 1624 | TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1625 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1626 | if( input_D->len ) |
| 1627 | TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1628 | |
| 1629 | } |
| 1630 | |
| 1631 | exit: |
| 1632 | mbedtls_rsa_free( &ctx ); |
| 1633 | } |
| 1634 | /* END_CASE */ |
| 1635 | |
Hanno Becker | f40cdf9 | 2017-12-22 11:03:27 +0000 | [diff] [blame] | 1636 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 1637 | void mbedtls_rsa_import_raw( data_t *input_N, |
| 1638 | data_t *input_P, data_t *input_Q, |
| 1639 | data_t *input_D, data_t *input_E, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1640 | int successive, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1641 | int is_priv, |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1642 | int res_check, |
| 1643 | int res_complete ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1644 | { |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1645 | /* Buffers used for encryption-decryption test */ |
| 1646 | unsigned char *buf_orig = NULL; |
| 1647 | unsigned char *buf_enc = NULL; |
| 1648 | unsigned char *buf_dec = NULL; |
| 1649 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1650 | mbedtls_rsa_context ctx; |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1651 | mbedtls_entropy_context entropy; |
| 1652 | mbedtls_ctr_drbg_context ctr_drbg; |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1653 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1654 | const char *pers = "test_suite_rsa"; |
| 1655 | |
| 1656 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1657 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1658 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1659 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1660 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 1661 | &entropy, (const unsigned char *) pers, |
| 1662 | strlen( pers ) ) == 0 ); |
| 1663 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1664 | if( !successive ) |
| 1665 | { |
| 1666 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1667 | ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len, |
| 1668 | ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len, |
| 1669 | ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len, |
| 1670 | ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len, |
| 1671 | ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1672 | } |
| 1673 | else |
| 1674 | { |
| 1675 | /* Import N, P, Q, D, E separately. |
| 1676 | * This should make no functional difference. */ |
| 1677 | |
| 1678 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1679 | ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1680 | NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 ); |
| 1681 | |
| 1682 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1683 | NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1684 | ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1685 | NULL, 0, NULL, 0, NULL, 0 ) == 0 ); |
| 1686 | |
| 1687 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1688 | NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1689 | ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1690 | NULL, 0, NULL, 0 ) == 0 ); |
| 1691 | |
| 1692 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1693 | NULL, 0, NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1694 | ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1695 | NULL, 0 ) == 0 ); |
| 1696 | |
| 1697 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1698 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1699 | ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1700 | } |
| 1701 | |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1702 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1703 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1704 | /* On expected success, perform some public and private |
| 1705 | * key operations to check if the key is working properly. */ |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1706 | if( res_complete == 0 ) |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1707 | { |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1708 | if( is_priv ) |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1709 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); |
| 1710 | else |
| 1711 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); |
| 1712 | |
| 1713 | if( res_check != 0 ) |
| 1714 | goto exit; |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1715 | |
| 1716 | buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1717 | buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1718 | buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1719 | if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) |
| 1720 | goto exit; |
| 1721 | |
| 1722 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, |
| 1723 | buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1724 | |
| 1725 | /* Make sure the number we're generating is smaller than the modulus */ |
| 1726 | buf_orig[0] = 0x00; |
| 1727 | |
| 1728 | TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); |
| 1729 | |
| 1730 | if( is_priv ) |
| 1731 | { |
| 1732 | TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, |
| 1733 | &ctr_drbg, buf_enc, |
| 1734 | buf_dec ) == 0 ); |
| 1735 | |
| 1736 | TEST_ASSERT( memcmp( buf_orig, buf_dec, |
| 1737 | mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1738 | } |
| 1739 | } |
| 1740 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1741 | exit: |
| 1742 | |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1743 | mbedtls_free( buf_orig ); |
| 1744 | mbedtls_free( buf_enc ); |
| 1745 | mbedtls_free( buf_dec ); |
| 1746 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1747 | mbedtls_rsa_free( &ctx ); |
| 1748 | |
| 1749 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1750 | mbedtls_entropy_free( &entropy ); |
| 1751 | |
| 1752 | } |
| 1753 | /* END_CASE */ |
| 1754 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1755 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1756 | void rsa_selftest( ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1757 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 1758 | TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1759 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1760 | /* END_CASE */ |