1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * Copyright 2017 BaishanCloud. All rights reserved. 4e1051a39Sopenharmony_ci * 5e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 6e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 7e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 8e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 9e1051a39Sopenharmony_ci */ 10e1051a39Sopenharmony_ci 11e1051a39Sopenharmony_ci/* This aims to test the setting functions, including internal ones */ 12e1051a39Sopenharmony_ci 13e1051a39Sopenharmony_ci/* 14e1051a39Sopenharmony_ci * RSA low level APIs are deprecated for public use, but still ok for 15e1051a39Sopenharmony_ci * internal use. 16e1051a39Sopenharmony_ci */ 17e1051a39Sopenharmony_ci#include "internal/deprecated.h" 18e1051a39Sopenharmony_ci 19e1051a39Sopenharmony_ci#include <stdio.h> 20e1051a39Sopenharmony_ci#include <string.h> 21e1051a39Sopenharmony_ci 22e1051a39Sopenharmony_ci#include <openssl/crypto.h> 23e1051a39Sopenharmony_ci#include <openssl/err.h> 24e1051a39Sopenharmony_ci#include <openssl/rand.h> 25e1051a39Sopenharmony_ci#include <openssl/bn.h> 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ci#include "testutil.h" 28e1051a39Sopenharmony_ci 29e1051a39Sopenharmony_ci#include <openssl/rsa.h> 30e1051a39Sopenharmony_ci#include "crypto/rsa.h" 31e1051a39Sopenharmony_ci 32e1051a39Sopenharmony_ci#define NUM_EXTRA_PRIMES 1 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ciDEFINE_STACK_OF(BIGNUM) 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ci/* C90 requires string should <= 509 bytes */ 37e1051a39Sopenharmony_cistatic const unsigned char n[] = 38e1051a39Sopenharmony_ci "\x92\x60\xd0\x75\x0a\xe1\x17\xee\xe5\x5c\x3f\x3d\xea\xba\x74\x91" 39e1051a39Sopenharmony_ci "\x75\x21\xa2\x62\xee\x76\x00\x7c\xdf\x8a\x56\x75\x5a\xd7\x3a\x15" 40e1051a39Sopenharmony_ci "\x98\xa1\x40\x84\x10\xa0\x14\x34\xc3\xf5\xbc\x54\xa8\x8b\x57\xfa" 41e1051a39Sopenharmony_ci "\x19\xfc\x43\x28\xda\xea\x07\x50\xa4\xc4\x4e\x88\xcf\xf3\xb2\x38" 42e1051a39Sopenharmony_ci "\x26\x21\xb8\x0f\x67\x04\x64\x43\x3e\x43\x36\xe6\xd0\x03\xe8\xcd" 43e1051a39Sopenharmony_ci "\x65\xbf\xf2\x11\xda\x14\x4b\x88\x29\x1c\x22\x59\xa0\x0a\x72\xb7" 44e1051a39Sopenharmony_ci "\x11\xc1\x16\xef\x76\x86\xe8\xfe\xe3\x4e\x4d\x93\x3c\x86\x81\x87" 45e1051a39Sopenharmony_ci "\xbd\xc2\x6f\x7b\xe0\x71\x49\x3c\x86\xf7\xa5\x94\x1c\x35\x10\x80" 46e1051a39Sopenharmony_ci "\x6a\xd6\x7b\x0f\x94\xd8\x8f\x5c\xf5\xc0\x2a\x09\x28\x21\xd8\x62" 47e1051a39Sopenharmony_ci "\x6e\x89\x32\xb6\x5c\x5b\xd8\xc9\x20\x49\xc2\x10\x93\x2b\x7a\xfa" 48e1051a39Sopenharmony_ci "\x7a\xc5\x9c\x0e\x88\x6a\xe5\xc1\xed\xb0\x0d\x8c\xe2\xc5\x76\x33" 49e1051a39Sopenharmony_ci "\xdb\x26\xbd\x66\x39\xbf\xf7\x3c\xee\x82\xbe\x92\x75\xc4\x02\xb4" 50e1051a39Sopenharmony_ci "\xcf\x2a\x43\x88\xda\x8c\xf8\xc6\x4e\xef\xe1\xc5\xa0\xf5\xab\x80" 51e1051a39Sopenharmony_ci "\x57\xc3\x9f\xa5\xc0\x58\x9c\x3e\x25\x3f\x09\x60\x33\x23\x00\xf9" 52e1051a39Sopenharmony_ci "\x4b\xea\x44\x87\x7b\x58\x8e\x1e\xdb\xde\x97\xcf\x23\x60\x72\x7a" 53e1051a39Sopenharmony_ci "\x09\xb7\x75\x26\x2d\x7e\xe5\x52\xb3\x31\x9b\x92\x66\xf0\x5a\x25"; 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_cistatic const unsigned char e[] = "\x01\x00\x01"; 56e1051a39Sopenharmony_ci 57e1051a39Sopenharmony_cistatic const unsigned char d[] = 58e1051a39Sopenharmony_ci "\x6a\x7d\xf2\xca\x63\xea\xd4\xdd\xa1\x91\xd6\x14\xb6\xb3\x85\xe0" 59e1051a39Sopenharmony_ci "\xd9\x05\x6a\x3d\x6d\x5c\xfe\x07\xdb\x1d\xaa\xbe\xe0\x22\xdb\x08" 60e1051a39Sopenharmony_ci "\x21\x2d\x97\x61\x3d\x33\x28\xe0\x26\x7c\x9d\xd2\x3d\x78\x7a\xbd" 61e1051a39Sopenharmony_ci "\xe2\xaf\xcb\x30\x6a\xeb\x7d\xfc\xe6\x92\x46\xcc\x73\xf5\xc8\x7f" 62e1051a39Sopenharmony_ci "\xdf\x06\x03\x01\x79\xa2\x11\x4b\x76\x7d\xb1\xf0\x83\xff\x84\x1c" 63e1051a39Sopenharmony_ci "\x02\x5d\x7d\xc0\x0c\xd8\x24\x35\xb9\xa9\x0f\x69\x53\x69\xe9\x4d" 64e1051a39Sopenharmony_ci "\xf2\x3d\x2c\xe4\x58\xbc\x3b\x32\x83\xad\x8b\xba\x2b\x8f\xa1\xba" 65e1051a39Sopenharmony_ci "\x62\xe2\xdc\xe9\xac\xcf\xf3\x79\x9a\xae\x7c\x84\x00\x16\xf3\xba" 66e1051a39Sopenharmony_ci "\x8e\x00\x48\xc0\xb6\xcc\x43\x39\xaf\x71\x61\x00\x3a\x5b\xeb\x86" 67e1051a39Sopenharmony_ci "\x4a\x01\x64\xb2\xc1\xc9\x23\x7b\x64\xbc\x87\x55\x69\x94\x35\x1b" 68e1051a39Sopenharmony_ci "\x27\x50\x6c\x33\xd4\xbc\xdf\xce\x0f\x9c\x49\x1a\x7d\x6b\x06\x28" 69e1051a39Sopenharmony_ci "\xc7\xc8\x52\xbe\x4f\x0a\x9c\x31\x32\xb2\xed\x3a\x2c\x88\x81\xe9" 70e1051a39Sopenharmony_ci "\xaa\xb0\x7e\x20\xe1\x7d\xeb\x07\x46\x91\xbe\x67\x77\x76\xa7\x8b" 71e1051a39Sopenharmony_ci "\x5c\x50\x2e\x05\xd9\xbd\xde\x72\x12\x6b\x37\x38\x69\x5e\x2d\xd1" 72e1051a39Sopenharmony_ci "\xa0\xa9\x8a\x14\x24\x7c\x65\xd8\xa7\xee\x79\x43\x2a\x09\x2c\xb0" 73e1051a39Sopenharmony_ci "\x72\x1a\x12\xdf\x79\x8e\x44\xf7\xcf\xce\x0c\x49\x81\x47\xa9\xb1"; 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_cistatic const unsigned char p[] = 76e1051a39Sopenharmony_ci "\x06\x77\xcd\xd5\x46\x9b\xc1\xd5\x58\x00\x81\xe2\xf3\x0a\x36\xb1" 77e1051a39Sopenharmony_ci "\x6e\x29\x89\xd5\x2f\x31\x5f\x92\x22\x3b\x9b\x75\x30\x82\xfa\xc5" 78e1051a39Sopenharmony_ci "\xf5\xde\x8a\x36\xdb\xc6\xe5\x8f\xef\x14\x37\xd6\x00\xf9\xab\x90" 79e1051a39Sopenharmony_ci "\x9b\x5d\x57\x4c\xf5\x1f\x77\xc4\xbb\x8b\xdd\x9b\x67\x11\x45\xb2" 80e1051a39Sopenharmony_ci "\x64\xe8\xac\xa8\x03\x0f\x16\x0d\x5d\x2d\x53\x07\x23\xfb\x62\x0d" 81e1051a39Sopenharmony_ci "\xe6\x16\xd3\x23\xe8\xb3"; 82e1051a39Sopenharmony_ci 83e1051a39Sopenharmony_cistatic const unsigned char q[] = 84e1051a39Sopenharmony_ci "\x06\x66\x9a\x70\x53\xd6\x72\x74\xfd\xea\x45\xc3\xc0\x17\xae\xde" 85e1051a39Sopenharmony_ci "\x79\x17\xae\x79\xde\xfc\x0e\xf7\xa4\x3a\x8c\x43\x8f\xc7\x8a\xa2" 86e1051a39Sopenharmony_ci "\x2c\x51\xc4\xd0\x72\x89\x73\x5c\x61\xbe\xfd\x54\x3f\x92\x65\xde" 87e1051a39Sopenharmony_ci "\x4d\x65\x71\x70\xf6\xf2\xe5\x98\xb9\x0f\xd1\x0b\xe6\x95\x09\x4a" 88e1051a39Sopenharmony_ci "\x7a\xdf\xf3\x10\x16\xd0\x60\xfc\xa5\x10\x34\x97\x37\x6f\x0a\xd5" 89e1051a39Sopenharmony_ci "\x5d\x8f\xd4\xc3\xa0\x5b"; 90e1051a39Sopenharmony_ci 91e1051a39Sopenharmony_cistatic const unsigned char dmp1[] = 92e1051a39Sopenharmony_ci "\x05\x7c\x9e\x1c\xbd\x90\x25\xe7\x40\x86\xf5\xa8\x3b\x7a\x3f\x99" 93e1051a39Sopenharmony_ci "\x56\x95\x60\x3a\x7b\x95\x4b\xb8\xa0\xd7\xa5\xf1\xcc\xdc\x5f\xb5" 94e1051a39Sopenharmony_ci "\x8c\xf4\x62\x95\x54\xed\x2e\x12\x62\xc2\xe8\xf6\xde\xce\xed\x8e" 95e1051a39Sopenharmony_ci "\x77\x6d\xc0\x40\x25\x74\xb3\x5a\x2d\xaa\xe1\xac\x11\xcb\xe2\x2f" 96e1051a39Sopenharmony_ci "\x0a\x51\x23\x1e\x47\xb2\x05\x88\x02\xb2\x0f\x4b\xf0\x67\x30\xf0" 97e1051a39Sopenharmony_ci "\x0f\x6e\xef\x5f\xf7\xe7"; 98e1051a39Sopenharmony_ci 99e1051a39Sopenharmony_cistatic const unsigned char dmq1[] = 100e1051a39Sopenharmony_ci "\x01\xa5\x6b\xbc\xcd\xe3\x0e\x46\xc6\x72\xf5\x04\x56\x28\x01\x22" 101e1051a39Sopenharmony_ci "\x58\x74\x5d\xbc\x1c\x3c\x29\x41\x49\x6c\x81\x5c\x72\xe2\xf7\xe5" 102e1051a39Sopenharmony_ci "\xa3\x8e\x58\x16\xe0\x0e\x37\xac\x1f\xbb\x75\xfd\xaf\xe7\xdf\xe9" 103e1051a39Sopenharmony_ci "\x1f\x70\xa2\x8f\x52\x03\xc0\x46\xd9\xf9\x96\x63\x00\x27\x7e\x5f" 104e1051a39Sopenharmony_ci "\x38\x60\xd6\x6b\x61\xe2\xaf\xbe\xea\x58\xd3\x9d\xbc\x75\x03\x8d" 105e1051a39Sopenharmony_ci "\x42\x65\xd6\x6b\x85\x97"; 106e1051a39Sopenharmony_ci 107e1051a39Sopenharmony_cistatic const unsigned char iqmp[] = 108e1051a39Sopenharmony_ci "\x03\xa1\x8b\x80\xe4\xd8\x87\x25\x17\x5d\xcc\x8d\xa9\x8a\x22\x2b" 109e1051a39Sopenharmony_ci "\x6c\x15\x34\x6f\x80\xcc\x1c\x44\x04\x68\xbc\x03\xcd\x95\xbb\x69" 110e1051a39Sopenharmony_ci "\x37\x61\x48\xb4\x23\x13\x08\x16\x54\x6a\xa1\x7c\xf5\xd4\x3a\xe1" 111e1051a39Sopenharmony_ci "\x4f\xa4\x0c\xf5\xaf\x80\x85\x27\x06\x0d\x70\xc0\xc5\x19\x28\xfe" 112e1051a39Sopenharmony_ci "\xee\x8e\x86\x21\x98\x8a\x37\xb7\xe5\x30\x25\x70\x93\x51\x2d\x49" 113e1051a39Sopenharmony_ci "\x85\x56\xb3\x0c\x2b\x96"; 114e1051a39Sopenharmony_ci 115e1051a39Sopenharmony_cistatic const unsigned char ex_prime[] = 116e1051a39Sopenharmony_ci "\x03\x89\x22\xa0\xb7\x3a\x91\xcb\x5e\x0c\xfd\x73\xde\xa7\x38\xa9" 117e1051a39Sopenharmony_ci "\x47\x43\xd6\x02\xbf\x2a\xb9\x3c\x48\xf3\x06\xd6\x58\x35\x50\x56" 118e1051a39Sopenharmony_ci "\x16\x5c\x34\x9b\x61\x87\xc8\xaa\x0a\x5d\x8a\x0a\xcd\x9c\x41\xd9" 119e1051a39Sopenharmony_ci "\x96\x24\xe0\xa9\x9b\x26\xb7\xa8\x08\xc9\xea\xdc\xa7\x15\xfb\x62" 120e1051a39Sopenharmony_ci "\xa0\x2d\x90\xe6\xa7\x55\x6e\xc6\x6c\xff\xd6\x10\x6d\xfa\x2e\x04" 121e1051a39Sopenharmony_ci "\x50\xec\x5c\x66\xe4\x05"; 122e1051a39Sopenharmony_ci 123e1051a39Sopenharmony_cistatic const unsigned char ex_exponent[] = 124e1051a39Sopenharmony_ci "\x02\x0a\xcd\xc3\x82\xd2\x03\xb0\x31\xac\xd3\x20\x80\x34\x9a\x57" 125e1051a39Sopenharmony_ci "\xbc\x60\x04\x57\x25\xd0\x29\x9a\x16\x90\xb9\x1c\x49\x6a\xd1\xf2" 126e1051a39Sopenharmony_ci "\x47\x8c\x0e\x9e\xc9\x20\xc2\xd8\xe4\x8f\xce\xd2\x1a\x9c\xec\xb4" 127e1051a39Sopenharmony_ci "\x1f\x33\x41\xc8\xf5\x62\xd1\xa5\xef\x1d\xa1\xd8\xbd\x71\xc6\xf7" 128e1051a39Sopenharmony_ci "\xda\x89\x37\x2e\xe2\xec\x47\xc5\xb8\xe3\xb4\xe3\x5c\x82\xaa\xdd" 129e1051a39Sopenharmony_ci "\xb7\x58\x2e\xaf\x07\x79"; 130e1051a39Sopenharmony_ci 131e1051a39Sopenharmony_cistatic const unsigned char ex_coefficient[] = 132e1051a39Sopenharmony_ci "\x00\x9c\x09\x88\x9b\xc8\x57\x08\x69\x69\xab\x2d\x9e\x29\x1c\x3c" 133e1051a39Sopenharmony_ci "\x6d\x59\x33\x12\x0d\x2b\x09\x2e\xaf\x01\x2c\x27\x01\xfc\xbd\x26" 134e1051a39Sopenharmony_ci "\x13\xf9\x2d\x09\x22\x4e\x49\x11\x03\x82\x88\x87\xf4\x43\x1d\xac" 135e1051a39Sopenharmony_ci "\xca\xec\x86\xf7\x23\xf1\x64\xf3\xf5\x81\xf0\x37\x36\xcf\x67\xff" 136e1051a39Sopenharmony_ci "\x1a\xff\x7a\xc7\xf9\xf9\x67\x2d\xa0\x9d\x61\xf8\xf6\x47\x5c\x2f" 137e1051a39Sopenharmony_ci "\xe7\x66\xe8\x3c\x3a\xe8"; 138e1051a39Sopenharmony_ci 139e1051a39Sopenharmony_cistatic int key2048_key(RSA *key) 140e1051a39Sopenharmony_ci{ 141e1051a39Sopenharmony_ci if (!TEST_int_eq(RSA_set0_key(key, 142e1051a39Sopenharmony_ci BN_bin2bn(n, sizeof(n) - 1, NULL), 143e1051a39Sopenharmony_ci BN_bin2bn(e, sizeof(e) - 1, NULL), 144e1051a39Sopenharmony_ci BN_bin2bn(d, sizeof(d) - 1, NULL)), 1)) 145e1051a39Sopenharmony_ci return 0; 146e1051a39Sopenharmony_ci 147e1051a39Sopenharmony_ci return RSA_size(key); 148e1051a39Sopenharmony_ci} 149e1051a39Sopenharmony_ci 150e1051a39Sopenharmony_cistatic int key2048p3_v1(RSA *key) 151e1051a39Sopenharmony_ci{ 152e1051a39Sopenharmony_ci BIGNUM **pris = NULL, **exps = NULL, **coeffs = NULL; 153e1051a39Sopenharmony_ci int rv = RSA_size(key); 154e1051a39Sopenharmony_ci 155e1051a39Sopenharmony_ci 156e1051a39Sopenharmony_ci if (!TEST_int_eq(RSA_set0_factors(key, 157e1051a39Sopenharmony_ci BN_bin2bn(p, sizeof(p) - 1, NULL), 158e1051a39Sopenharmony_ci BN_bin2bn(q, sizeof(q) - 1, NULL)), 1)) 159e1051a39Sopenharmony_ci goto err; 160e1051a39Sopenharmony_ci 161e1051a39Sopenharmony_ci if (!TEST_int_eq(RSA_set0_crt_params(key, 162e1051a39Sopenharmony_ci BN_bin2bn(dmp1, sizeof(dmp1) - 1, NULL), 163e1051a39Sopenharmony_ci BN_bin2bn(dmq1, sizeof(dmq1) - 1, NULL), 164e1051a39Sopenharmony_ci BN_bin2bn(iqmp, sizeof(iqmp) - 1, 165e1051a39Sopenharmony_ci NULL)), 1)) 166e1051a39Sopenharmony_ci return 0; 167e1051a39Sopenharmony_ci 168e1051a39Sopenharmony_ci pris = OPENSSL_zalloc(sizeof(BIGNUM *)); 169e1051a39Sopenharmony_ci exps = OPENSSL_zalloc(sizeof(BIGNUM *)); 170e1051a39Sopenharmony_ci coeffs = OPENSSL_zalloc(sizeof(BIGNUM *)); 171e1051a39Sopenharmony_ci if (!TEST_ptr(pris) || !TEST_ptr(exps) || !TEST_ptr(coeffs)) 172e1051a39Sopenharmony_ci goto err; 173e1051a39Sopenharmony_ci 174e1051a39Sopenharmony_ci pris[0] = BN_bin2bn(ex_prime, sizeof(ex_prime) - 1, NULL); 175e1051a39Sopenharmony_ci exps[0] = BN_bin2bn(ex_exponent, sizeof(ex_exponent) - 1, NULL); 176e1051a39Sopenharmony_ci coeffs[0] = BN_bin2bn(ex_coefficient, sizeof(ex_coefficient) - 1, NULL); 177e1051a39Sopenharmony_ci if (!TEST_ptr(pris[0]) || !TEST_ptr(exps[0]) || !TEST_ptr(coeffs[0])) 178e1051a39Sopenharmony_ci goto err; 179e1051a39Sopenharmony_ci 180e1051a39Sopenharmony_ci if (!TEST_true(RSA_set0_multi_prime_params(key, pris, exps, 181e1051a39Sopenharmony_ci coeffs, NUM_EXTRA_PRIMES))) 182e1051a39Sopenharmony_ci goto err; 183e1051a39Sopenharmony_ci 184e1051a39Sopenharmony_ci ret: 185e1051a39Sopenharmony_ci OPENSSL_free(pris); 186e1051a39Sopenharmony_ci OPENSSL_free(exps); 187e1051a39Sopenharmony_ci OPENSSL_free(coeffs); 188e1051a39Sopenharmony_ci return rv; 189e1051a39Sopenharmony_ci err: 190e1051a39Sopenharmony_ci if (pris != NULL) 191e1051a39Sopenharmony_ci BN_free(pris[0]); 192e1051a39Sopenharmony_ci if (exps != NULL) 193e1051a39Sopenharmony_ci BN_free(exps[0]); 194e1051a39Sopenharmony_ci if (coeffs != NULL) 195e1051a39Sopenharmony_ci BN_free(coeffs[0]); 196e1051a39Sopenharmony_ci rv = 0; 197e1051a39Sopenharmony_ci goto ret; 198e1051a39Sopenharmony_ci} 199e1051a39Sopenharmony_ci 200e1051a39Sopenharmony_cistatic int key2048p3_v2(RSA *key) 201e1051a39Sopenharmony_ci{ 202e1051a39Sopenharmony_ci STACK_OF(BIGNUM) *primes = NULL, *exps = NULL, *coeffs = NULL; 203e1051a39Sopenharmony_ci BIGNUM *num = NULL; 204e1051a39Sopenharmony_ci int rv = RSA_size(key); 205e1051a39Sopenharmony_ci 206e1051a39Sopenharmony_ci if (!TEST_ptr(primes = sk_BIGNUM_new_null()) 207e1051a39Sopenharmony_ci || !TEST_ptr(exps = sk_BIGNUM_new_null()) 208e1051a39Sopenharmony_ci || !TEST_ptr(coeffs = sk_BIGNUM_new_null())) 209e1051a39Sopenharmony_ci goto err; 210e1051a39Sopenharmony_ci 211e1051a39Sopenharmony_ci if (!TEST_ptr(num = BN_bin2bn(p, sizeof(p) - 1, NULL)) 212e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0) 213e1051a39Sopenharmony_ci || !TEST_ptr(num = BN_bin2bn(q, sizeof(q) - 1, NULL)) 214e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0) 215e1051a39Sopenharmony_ci || !TEST_ptr(num = BN_bin2bn(ex_prime, sizeof(ex_prime) - 1, NULL)) 216e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(primes, num), 0)) 217e1051a39Sopenharmony_ci goto err; 218e1051a39Sopenharmony_ci 219e1051a39Sopenharmony_ci if (!TEST_ptr(num = BN_bin2bn(dmp1, sizeof(dmp1) - 1, NULL)) 220e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0) 221e1051a39Sopenharmony_ci || !TEST_ptr(num = BN_bin2bn(dmq1, sizeof(dmq1) - 1, NULL)) 222e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0) 223e1051a39Sopenharmony_ci || !TEST_ptr(num = BN_bin2bn(ex_exponent, sizeof(ex_exponent) - 1, NULL)) 224e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(exps, num), 0)) 225e1051a39Sopenharmony_ci goto err; 226e1051a39Sopenharmony_ci 227e1051a39Sopenharmony_ci if (!TEST_ptr(num = BN_bin2bn(iqmp, sizeof(iqmp) - 1, NULL)) 228e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(coeffs, num), 0) 229e1051a39Sopenharmony_ci || !TEST_ptr(num = BN_bin2bn(ex_coefficient, sizeof(ex_coefficient) - 1, NULL)) 230e1051a39Sopenharmony_ci || !TEST_int_ne(sk_BIGNUM_push(coeffs, num), 0)) 231e1051a39Sopenharmony_ci goto err; 232e1051a39Sopenharmony_ci 233e1051a39Sopenharmony_ci if (!TEST_true(ossl_rsa_set0_all_params(key, primes, exps, coeffs))) 234e1051a39Sopenharmony_ci goto err; 235e1051a39Sopenharmony_ci 236e1051a39Sopenharmony_ci ret: 237e1051a39Sopenharmony_ci sk_BIGNUM_free(primes); 238e1051a39Sopenharmony_ci sk_BIGNUM_free(exps); 239e1051a39Sopenharmony_ci sk_BIGNUM_free(coeffs); 240e1051a39Sopenharmony_ci return rv; 241e1051a39Sopenharmony_ci err: 242e1051a39Sopenharmony_ci sk_BIGNUM_pop_free(primes, BN_free); 243e1051a39Sopenharmony_ci sk_BIGNUM_pop_free(exps, BN_free); 244e1051a39Sopenharmony_ci sk_BIGNUM_pop_free(coeffs, BN_free); 245e1051a39Sopenharmony_ci primes = exps = coeffs = NULL; 246e1051a39Sopenharmony_ci rv = 0; 247e1051a39Sopenharmony_ci goto ret; 248e1051a39Sopenharmony_ci} 249e1051a39Sopenharmony_ci 250e1051a39Sopenharmony_cistatic int test_rsa_mp(int i) 251e1051a39Sopenharmony_ci{ 252e1051a39Sopenharmony_ci int ret = 0; 253e1051a39Sopenharmony_ci RSA *key; 254e1051a39Sopenharmony_ci unsigned char ptext[256]; 255e1051a39Sopenharmony_ci unsigned char ctext[256]; 256e1051a39Sopenharmony_ci static unsigned char ptext_ex[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a"; 257e1051a39Sopenharmony_ci int plen; 258e1051a39Sopenharmony_ci int clen = 0; 259e1051a39Sopenharmony_ci int num; 260e1051a39Sopenharmony_ci static int (*param_set[])(RSA *) = { 261e1051a39Sopenharmony_ci key2048p3_v1, 262e1051a39Sopenharmony_ci key2048p3_v2, 263e1051a39Sopenharmony_ci }; 264e1051a39Sopenharmony_ci 265e1051a39Sopenharmony_ci plen = sizeof(ptext_ex) - 1; 266e1051a39Sopenharmony_ci key = RSA_new(); 267e1051a39Sopenharmony_ci if (!TEST_ptr(key)) 268e1051a39Sopenharmony_ci goto err; 269e1051a39Sopenharmony_ci 270e1051a39Sopenharmony_ci if (!TEST_int_eq((clen = key2048_key(key)), 256) 271e1051a39Sopenharmony_ci || !TEST_int_eq((clen = param_set[i](key)), 256)) 272e1051a39Sopenharmony_ci goto err; 273e1051a39Sopenharmony_ci 274e1051a39Sopenharmony_ci if (!TEST_true(RSA_check_key_ex(key, NULL))) 275e1051a39Sopenharmony_ci goto err; 276e1051a39Sopenharmony_ci 277e1051a39Sopenharmony_ci num = RSA_public_encrypt(plen, ptext_ex, ctext, key, 278e1051a39Sopenharmony_ci RSA_PKCS1_PADDING); 279e1051a39Sopenharmony_ci if (!TEST_int_eq(num, clen)) 280e1051a39Sopenharmony_ci goto err; 281e1051a39Sopenharmony_ci 282e1051a39Sopenharmony_ci num = RSA_private_decrypt(num, ctext, ptext, key, RSA_PKCS1_PADDING); 283e1051a39Sopenharmony_ci if (!TEST_mem_eq(ptext, num, ptext_ex, plen)) 284e1051a39Sopenharmony_ci goto err; 285e1051a39Sopenharmony_ci 286e1051a39Sopenharmony_ci ret = 1; 287e1051a39Sopenharmony_cierr: 288e1051a39Sopenharmony_ci RSA_free(key); 289e1051a39Sopenharmony_ci return ret; 290e1051a39Sopenharmony_ci} 291e1051a39Sopenharmony_ci 292e1051a39Sopenharmony_ciint setup_tests(void) 293e1051a39Sopenharmony_ci{ 294e1051a39Sopenharmony_ci ADD_ALL_TESTS(test_rsa_mp, 2); 295e1051a39Sopenharmony_ci return 1; 296e1051a39Sopenharmony_ci} 297