1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * RSA low level APIs are deprecated for public use, but still ok for
12e1051a39Sopenharmony_ci * internal use.
13e1051a39Sopenharmony_ci */
14e1051a39Sopenharmony_ci#include "internal/deprecated.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <stdio.h>
17e1051a39Sopenharmony_ci#include <string.h>
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ci#include "internal/nelem.h"
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci#include <openssl/crypto.h>
22e1051a39Sopenharmony_ci#include <openssl/err.h>
23e1051a39Sopenharmony_ci#include <openssl/rand.h>
24e1051a39Sopenharmony_ci#include <openssl/bn.h>
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ci#include "testutil.h"
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ci#include "rsa_local.h"
29e1051a39Sopenharmony_ci#include <openssl/rsa.h>
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_ci/* taken from RSA2 cavs data */
32e1051a39Sopenharmony_cistatic const unsigned char cav_e[] = {
33e1051a39Sopenharmony_ci    0x01,0x00,0x01
34e1051a39Sopenharmony_ci};
35e1051a39Sopenharmony_cistatic const unsigned char cav_p[] = {
36e1051a39Sopenharmony_ci    0xcf,0x72,0x1b,0x9a,0xfd,0x0d,0x22,0x1a,0x74,0x50,0x97,0x22,0x76,0xd8,0xc0,
37e1051a39Sopenharmony_ci    0xc2,0xfd,0x08,0x81,0x05,0xdd,0x18,0x21,0x99,0x96,0xd6,0x5c,0x79,0xe3,0x02,
38e1051a39Sopenharmony_ci    0x81,0xd7,0x0e,0x3f,0x3b,0x34,0xda,0x61,0xc9,0x2d,0x84,0x86,0x62,0x1e,0x3d,
39e1051a39Sopenharmony_ci    0x5d,0xbf,0x92,0x2e,0xcd,0x35,0x3d,0x6e,0xb9,0x59,0x16,0xc9,0x82,0x50,0x41,
40e1051a39Sopenharmony_ci    0x30,0x45,0x67,0xaa,0xb7,0xbe,0xec,0xea,0x4b,0x9e,0xa0,0xc3,0x05,0xbc,0x4c,
41e1051a39Sopenharmony_ci    0x01,0xa5,0x4b,0xbd,0xa4,0x20,0xb5,0x20,0xd5,0x59,0x6f,0x82,0x5c,0x8f,0x4f,
42e1051a39Sopenharmony_ci    0xe0,0x3a,0x4e,0x7e,0xfe,0x44,0xf3,0x3c,0xc0,0x0e,0x14,0x2b,0x32,0xe6,0x28,
43e1051a39Sopenharmony_ci    0x8b,0x63,0x87,0x00,0xc3,0x53,0x4a,0x5b,0x71,0x7a,0x5b,0x28,0x40,0xc4,0x18,
44e1051a39Sopenharmony_ci    0xb6,0x77,0x0b,0xab,0x59,0xa4,0x96,0x7d
45e1051a39Sopenharmony_ci};
46e1051a39Sopenharmony_cistatic const unsigned char cav_q[] = {
47e1051a39Sopenharmony_ci    0xfe,0xab,0xf2,0x7c,0x16,0x4a,0xf0,0x8d,0x31,0xc6,0x0a,0x82,0xe2,0xae,0xbb,
48e1051a39Sopenharmony_ci    0x03,0x7e,0x7b,0x20,0x4e,0x64,0xb0,0x16,0xad,0x3c,0x01,0x1a,0xd3,0x54,0xbf,
49e1051a39Sopenharmony_ci    0x2b,0xa4,0x02,0x9e,0xc3,0x0d,0x60,0x3d,0x1f,0xb9,0xc0,0x0d,0xe6,0x97,0x68,
50e1051a39Sopenharmony_ci    0xbb,0x8c,0x81,0xd5,0xc1,0x54,0x96,0x0f,0x99,0xf0,0xa8,0xa2,0xf3,0xc6,0x8e,
51e1051a39Sopenharmony_ci    0xec,0xbc,0x31,0x17,0x70,0x98,0x24,0xa3,0x36,0x51,0xa8,0x54,0xc4,0x44,0xdd,
52e1051a39Sopenharmony_ci    0xf7,0x7e,0xda,0x47,0x4a,0x67,0x44,0x5d,0x4e,0x75,0xf0,0x4d,0x00,0x68,0xe1,
53e1051a39Sopenharmony_ci    0x4a,0xec,0x1f,0x45,0xf9,0xe6,0xca,0x38,0x95,0x48,0x6f,0xdc,0x9d,0x1b,0xa3,
54e1051a39Sopenharmony_ci    0x4b,0xfd,0x08,0x4b,0x54,0xcd,0xeb,0x3d,0xef,0x33,0x11,0x6e,0xce,0xe4,0x5d,
55e1051a39Sopenharmony_ci    0xef,0xa9,0x58,0x5c,0x87,0x4d,0xc8,0xcf
56e1051a39Sopenharmony_ci};
57e1051a39Sopenharmony_cistatic const unsigned char cav_n[] = {
58e1051a39Sopenharmony_ci    0xce,0x5e,0x8d,0x1a,0xa3,0x08,0x7a,0x2d,0xb4,0x49,0x48,0xf0,0x06,0xb6,0xfe,
59e1051a39Sopenharmony_ci    0xba,0x2f,0x39,0x7c,0x7b,0xe0,0x5d,0x09,0x2d,0x57,0x4e,0x54,0x60,0x9c,0xe5,
60e1051a39Sopenharmony_ci    0x08,0x4b,0xe1,0x1a,0x73,0xc1,0x5e,0x2f,0xb6,0x46,0xd7,0x81,0xca,0xbc,0x98,
61e1051a39Sopenharmony_ci    0xd2,0xf9,0xef,0x1c,0x92,0x8c,0x8d,0x99,0x85,0x28,0x52,0xd6,0xd5,0xab,0x70,
62e1051a39Sopenharmony_ci    0x7e,0x9e,0xa9,0x87,0x82,0xc8,0x95,0x64,0xeb,0xf0,0x6c,0x0f,0x3f,0xe9,0x02,
63e1051a39Sopenharmony_ci    0x29,0x2e,0x6d,0xa1,0xec,0xbf,0xdc,0x23,0xdf,0x82,0x4f,0xab,0x39,0x8d,0xcc,
64e1051a39Sopenharmony_ci    0xac,0x21,0x51,0x14,0xf8,0xef,0xec,0x73,0x80,0x86,0xa3,0xcf,0x8f,0xd5,0xcf,
65e1051a39Sopenharmony_ci    0x22,0x1f,0xcc,0x23,0x2f,0xba,0xcb,0xf6,0x17,0xcd,0x3a,0x1f,0xd9,0x84,0xb9,
66e1051a39Sopenharmony_ci    0x88,0xa7,0x78,0x0f,0xaa,0xc9,0x04,0x01,0x20,0x72,0x5d,0x2a,0xfe,0x5b,0xdd,
67e1051a39Sopenharmony_ci    0x16,0x5a,0xed,0x83,0x02,0x96,0x39,0x46,0x37,0x30,0xc1,0x0d,0x87,0xc2,0xc8,
68e1051a39Sopenharmony_ci    0x33,0x38,0xed,0x35,0x72,0xe5,0x29,0xf8,0x1f,0x23,0x60,0xe1,0x2a,0x5b,0x1d,
69e1051a39Sopenharmony_ci    0x6b,0x53,0x3f,0x07,0xc4,0xd9,0xbb,0x04,0x0c,0x5c,0x3f,0x0b,0xc4,0xd4,0x61,
70e1051a39Sopenharmony_ci    0x96,0x94,0xf1,0x0f,0x4a,0x49,0xac,0xde,0xd2,0xe8,0x42,0xb3,0x4a,0x0b,0x64,
71e1051a39Sopenharmony_ci    0x7a,0x32,0x5f,0x2b,0x5b,0x0f,0x8b,0x8b,0xe0,0x33,0x23,0x34,0x64,0xf8,0xb5,
72e1051a39Sopenharmony_ci    0x7f,0x69,0x60,0xb8,0x71,0xe9,0xff,0x92,0x42,0xb1,0xf7,0x23,0xa8,0xa7,0x92,
73e1051a39Sopenharmony_ci    0x04,0x3d,0x6b,0xff,0xf7,0xab,0xbb,0x14,0x1f,0x4c,0x10,0x97,0xd5,0x6b,0x71,
74e1051a39Sopenharmony_ci    0x12,0xfd,0x93,0xa0,0x4a,0x3b,0x75,0x72,0x40,0x96,0x1c,0x5f,0x40,0x40,0x57,
75e1051a39Sopenharmony_ci    0x13
76e1051a39Sopenharmony_ci};
77e1051a39Sopenharmony_cistatic const unsigned char cav_d[] = {
78e1051a39Sopenharmony_ci    0x47,0x47,0x49,0x1d,0x66,0x2a,0x4b,0x68,0xf5,0xd8,0x4a,0x24,0xfd,0x6c,0xbf,
79e1051a39Sopenharmony_ci    0x56,0xb7,0x70,0xf7,0x9a,0x21,0xc8,0x80,0x9e,0xf4,0x84,0xcd,0x88,0x01,0x28,
80e1051a39Sopenharmony_ci    0xea,0x50,0xab,0x13,0x63,0xdf,0xea,0x14,0x38,0xb5,0x07,0x42,0x81,0x2f,0xda,
81e1051a39Sopenharmony_ci    0xe9,0x24,0x02,0x7e,0xaf,0xef,0x74,0x09,0x0e,0x80,0xfa,0xfb,0xd1,0x19,0x41,
82e1051a39Sopenharmony_ci    0xe5,0xba,0x0f,0x7c,0x0a,0xa4,0x15,0x55,0xa2,0x58,0x8c,0x3a,0x48,0x2c,0xc6,
83e1051a39Sopenharmony_ci    0xde,0x4a,0x76,0xfb,0x72,0xb6,0x61,0xe6,0xd2,0x10,0x44,0x4c,0x33,0xb8,0xd2,
84e1051a39Sopenharmony_ci    0x74,0xb1,0x9d,0x3b,0xcd,0x2f,0xb1,0x4f,0xc3,0x98,0xbd,0x83,0xb7,0x7e,0x75,
85e1051a39Sopenharmony_ci    0xe8,0xa7,0x6a,0xee,0xcc,0x51,0x8c,0x99,0x17,0x67,0x7f,0x27,0xf9,0x0d,0x6a,
86e1051a39Sopenharmony_ci    0xb7,0xd4,0x80,0x17,0x89,0x39,0x9c,0xf3,0xd7,0x0f,0xdf,0xb0,0x55,0x80,0x1d,
87e1051a39Sopenharmony_ci    0xaf,0x57,0x2e,0xd0,0xf0,0x4f,0x42,0x69,0x55,0xbc,0x83,0xd6,0x97,0x83,0x7a,
88e1051a39Sopenharmony_ci    0xe6,0xc6,0x30,0x6d,0x3d,0xb5,0x21,0xa7,0xc4,0x62,0x0a,0x20,0xce,0x5e,0x5a,
89e1051a39Sopenharmony_ci    0x17,0x98,0xb3,0x6f,0x6b,0x9a,0xeb,0x6b,0xa3,0xc4,0x75,0xd8,0x2b,0xdc,0x5c,
90e1051a39Sopenharmony_ci    0x6f,0xec,0x5d,0x49,0xac,0xa8,0xa4,0x2f,0xb8,0x8c,0x4f,0x2e,0x46,0x21,0xee,
91e1051a39Sopenharmony_ci    0x72,0x6a,0x0e,0x22,0x80,0x71,0xc8,0x76,0x40,0x44,0x61,0x16,0xbf,0xa5,0xf8,
92e1051a39Sopenharmony_ci    0x89,0xc7,0xe9,0x87,0xdf,0xbd,0x2e,0x4b,0x4e,0xc2,0x97,0x53,0xe9,0x49,0x1c,
93e1051a39Sopenharmony_ci    0x05,0xb0,0x0b,0x9b,0x9f,0x21,0x19,0x41,0xe9,0xf5,0x61,0xd7,0x33,0x2e,0x2c,
94e1051a39Sopenharmony_ci    0x94,0xb8,0xa8,0x9a,0x3a,0xcc,0x6a,0x24,0x8d,0x19,0x13,0xee,0xb9,0xb0,0x48,
95e1051a39Sopenharmony_ci    0x61
96e1051a39Sopenharmony_ci};
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci/* helper function */
99e1051a39Sopenharmony_cistatic BIGNUM *bn_load_new(const unsigned char *data, int sz)
100e1051a39Sopenharmony_ci{
101e1051a39Sopenharmony_ci    BIGNUM *ret = BN_new();
102e1051a39Sopenharmony_ci    if (ret != NULL)
103e1051a39Sopenharmony_ci        BN_bin2bn(data, sz, ret);
104e1051a39Sopenharmony_ci    return ret;
105e1051a39Sopenharmony_ci}
106e1051a39Sopenharmony_ci
107e1051a39Sopenharmony_ci/* Check that small rsa exponents are allowed in non FIPS mode */
108e1051a39Sopenharmony_cistatic int test_check_public_exponent(void)
109e1051a39Sopenharmony_ci{
110e1051a39Sopenharmony_ci    int ret = 0;
111e1051a39Sopenharmony_ci    BIGNUM *e = NULL;
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_ci    ret = TEST_ptr(e = BN_new())
114e1051a39Sopenharmony_ci          /* e is too small will fail */
115e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 1))
116e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_public_exponent(e))
117e1051a39Sopenharmony_ci          /* e is even will fail */
118e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 65536))
119e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_public_exponent(e))
120e1051a39Sopenharmony_ci          /* e is ok */
121e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 3))
122e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_public_exponent(e))
123e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 17))
124e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_public_exponent(e))
125e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 65537))
126e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_public_exponent(e))
127e1051a39Sopenharmony_ci          /* e = 2^256 + 1 is ok */
128e1051a39Sopenharmony_ci          && TEST_true(BN_lshift(e, BN_value_one(), 256))
129e1051a39Sopenharmony_ci          && TEST_true(BN_add(e, e, BN_value_one()))
130e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_public_exponent(e));
131e1051a39Sopenharmony_ci    BN_free(e);
132e1051a39Sopenharmony_ci    return ret;
133e1051a39Sopenharmony_ci}
134e1051a39Sopenharmony_ci
135e1051a39Sopenharmony_cistatic int test_check_prime_factor_range(void)
136e1051a39Sopenharmony_ci{
137e1051a39Sopenharmony_ci    int ret = 0;
138e1051a39Sopenharmony_ci    BN_CTX *ctx = NULL;
139e1051a39Sopenharmony_ci    BIGNUM *p = NULL;
140e1051a39Sopenharmony_ci    BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL, *bn_p4 = NULL;
141e1051a39Sopenharmony_ci    /* Some range checks that are larger than 32 bits */
142e1051a39Sopenharmony_ci    static const unsigned char p1[] = { 0x0B, 0x50, 0x4F, 0x33, 0x3F };
143e1051a39Sopenharmony_ci    static const unsigned char p2[] = { 0x10, 0x00, 0x00, 0x00, 0x00 };
144e1051a39Sopenharmony_ci    static const unsigned char p3[] = { 0x0B, 0x50, 0x4F, 0x33, 0x40 };
145e1051a39Sopenharmony_ci    static const unsigned char p4[] = { 0x0F, 0xFF, 0xFF, 0xFF, 0xFF };
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_ci    /* (√2)(2^(nbits/2 - 1) <= p <= 2^(nbits/2) - 1
148e1051a39Sopenharmony_ci     * For 8 bits:   0xB.504F <= p <= 0xF
149e1051a39Sopenharmony_ci     * for 72 bits:  0xB504F333F. <= p <= 0xF_FFFF_FFFF
150e1051a39Sopenharmony_ci     */
151e1051a39Sopenharmony_ci    ret = TEST_ptr(p = BN_new())
152e1051a39Sopenharmony_ci          && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
153e1051a39Sopenharmony_ci          && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
154e1051a39Sopenharmony_ci          && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
155e1051a39Sopenharmony_ci          && TEST_ptr(bn_p4 = bn_load_new(p4, sizeof(p4)))
156e1051a39Sopenharmony_ci          && TEST_ptr(ctx = BN_CTX_new())
157e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 0xA))
158e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
159e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 0x10))
160e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
161e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 0xB))
162e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor_range(p, 8, ctx))
163e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 0xC))
164e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
165e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 0xF))
166e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_prime_factor_range(p, 8, ctx))
167e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor_range(bn_p1, 72, ctx))
168e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor_range(bn_p2, 72, ctx))
169e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_prime_factor_range(bn_p3, 72, ctx))
170e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_prime_factor_range(bn_p4, 72, ctx));
171e1051a39Sopenharmony_ci
172e1051a39Sopenharmony_ci    BN_free(bn_p4);
173e1051a39Sopenharmony_ci    BN_free(bn_p3);
174e1051a39Sopenharmony_ci    BN_free(bn_p2);
175e1051a39Sopenharmony_ci    BN_free(bn_p1);
176e1051a39Sopenharmony_ci    BN_free(p);
177e1051a39Sopenharmony_ci    BN_CTX_free(ctx);
178e1051a39Sopenharmony_ci    return ret;
179e1051a39Sopenharmony_ci}
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_cistatic int test_check_prime_factor(void)
182e1051a39Sopenharmony_ci{
183e1051a39Sopenharmony_ci    int ret = 0;
184e1051a39Sopenharmony_ci    BN_CTX *ctx = NULL;
185e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *e = NULL;
186e1051a39Sopenharmony_ci    BIGNUM *bn_p1 = NULL, *bn_p2 = NULL, *bn_p3 = NULL;
187e1051a39Sopenharmony_ci
188e1051a39Sopenharmony_ci    /* Some range checks that are larger than 32 bits */
189e1051a39Sopenharmony_ci    static const unsigned char p1[] = { 0x0B, 0x50, 0x4f, 0x33, 0x73 };
190e1051a39Sopenharmony_ci    static const unsigned char p2[] = { 0x0B, 0x50, 0x4f, 0x33, 0x75 };
191e1051a39Sopenharmony_ci    static const unsigned char p3[] = { 0x0F, 0x50, 0x00, 0x03, 0x75 };
192e1051a39Sopenharmony_ci
193e1051a39Sopenharmony_ci    ret = TEST_ptr(p = BN_new())
194e1051a39Sopenharmony_ci          && TEST_ptr(bn_p1 = bn_load_new(p1, sizeof(p1)))
195e1051a39Sopenharmony_ci          && TEST_ptr(bn_p2 = bn_load_new(p2, sizeof(p2)))
196e1051a39Sopenharmony_ci          && TEST_ptr(bn_p3 = bn_load_new(p3, sizeof(p3)))
197e1051a39Sopenharmony_ci          && TEST_ptr(e = BN_new())
198e1051a39Sopenharmony_ci          && TEST_ptr(ctx = BN_CTX_new())
199e1051a39Sopenharmony_ci          /* Fails the prime test */
200e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 0x1))
201e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor(bn_p1, e, 72, ctx))
202e1051a39Sopenharmony_ci          /* p is prime and in range and gcd(p-1, e) = 1 */
203e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_prime_factor(bn_p2, e, 72, ctx))
204e1051a39Sopenharmony_ci          /* gcd(p-1,e) = 1 test fails */
205e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 0x2))
206e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor(p, e, 72, ctx))
207e1051a39Sopenharmony_ci          /* p fails the range check */
208e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 0x1))
209e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_prime_factor(bn_p3, e, 72, ctx));
210e1051a39Sopenharmony_ci
211e1051a39Sopenharmony_ci    BN_free(bn_p3);
212e1051a39Sopenharmony_ci    BN_free(bn_p2);
213e1051a39Sopenharmony_ci    BN_free(bn_p1);
214e1051a39Sopenharmony_ci    BN_free(e);
215e1051a39Sopenharmony_ci    BN_free(p);
216e1051a39Sopenharmony_ci    BN_CTX_free(ctx);
217e1051a39Sopenharmony_ci    return ret;
218e1051a39Sopenharmony_ci}
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ci/* This test uses legacy functions because they can take invalid numbers */
221e1051a39Sopenharmony_cistatic int test_check_private_exponent(void)
222e1051a39Sopenharmony_ci{
223e1051a39Sopenharmony_ci    int ret = 0;
224e1051a39Sopenharmony_ci    RSA *key = NULL;
225e1051a39Sopenharmony_ci    BN_CTX *ctx = NULL;
226e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *e = NULL, *d = NULL, *n = NULL;
227e1051a39Sopenharmony_ci
228e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
229e1051a39Sopenharmony_ci          && TEST_ptr(ctx = BN_CTX_new())
230e1051a39Sopenharmony_ci          && TEST_ptr(p = BN_new())
231e1051a39Sopenharmony_ci          && TEST_ptr(q = BN_new())
232e1051a39Sopenharmony_ci          /* lcm(15-1,17-1) = 14*16 / 2 = 112 */
233e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 15))
234e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(q, 17))
235e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_factors(key, p, q));
236e1051a39Sopenharmony_ci    if (!ret) {
237e1051a39Sopenharmony_ci        BN_free(p);
238e1051a39Sopenharmony_ci        BN_free(q);
239e1051a39Sopenharmony_ci        goto end;
240e1051a39Sopenharmony_ci    }
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_ci    ret = TEST_ptr(e = BN_new())
243e1051a39Sopenharmony_ci          && TEST_ptr(d = BN_new())
244e1051a39Sopenharmony_ci          && TEST_ptr(n = BN_new())
245e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, 5))
246e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(d, 157))
247e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(n, 15*17))
248e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_key(key, n, e, d));
249e1051a39Sopenharmony_ci    if (!ret) {
250e1051a39Sopenharmony_ci        BN_free(e);
251e1051a39Sopenharmony_ci        BN_free(d);
252e1051a39Sopenharmony_ci        BN_free(n);
253e1051a39Sopenharmony_ci        goto end;
254e1051a39Sopenharmony_ci    }
255e1051a39Sopenharmony_ci    /* fails since d >= lcm(p-1, q-1) */
256e1051a39Sopenharmony_ci    ret = TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
257e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(d, 45))
258e1051a39Sopenharmony_ci          /* d is correct size and 1 = e.d mod lcm(p-1, q-1) */
259e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_private_exponent(key, 8, ctx))
260e1051a39Sopenharmony_ci          /* d is too small compared to nbits */
261e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_private_exponent(key, 16, ctx))
262e1051a39Sopenharmony_ci          /* d is too small compared to nbits */
263e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(d, 16))
264e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx))
265e1051a39Sopenharmony_ci          /* fail if 1 != e.d mod lcm(p-1, q-1) */
266e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(d, 46))
267e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_private_exponent(key, 8, ctx));
268e1051a39Sopenharmony_ciend:
269e1051a39Sopenharmony_ci    RSA_free(key);
270e1051a39Sopenharmony_ci    BN_CTX_free(ctx);
271e1051a39Sopenharmony_ci    return ret;
272e1051a39Sopenharmony_ci}
273e1051a39Sopenharmony_ci
274e1051a39Sopenharmony_cistatic int test_check_crt_components(void)
275e1051a39Sopenharmony_ci{
276e1051a39Sopenharmony_ci    const int P = 15;
277e1051a39Sopenharmony_ci    const int Q = 17;
278e1051a39Sopenharmony_ci    const int E = 5;
279e1051a39Sopenharmony_ci    const int N = P*Q;
280e1051a39Sopenharmony_ci    const int DP = 3;
281e1051a39Sopenharmony_ci    const int DQ = 13;
282e1051a39Sopenharmony_ci    const int QINV = 8;
283e1051a39Sopenharmony_ci
284e1051a39Sopenharmony_ci    int ret = 0;
285e1051a39Sopenharmony_ci    RSA *key = NULL;
286e1051a39Sopenharmony_ci    BN_CTX *ctx = NULL;
287e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *e = NULL;
288e1051a39Sopenharmony_ci
289e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
290e1051a39Sopenharmony_ci          && TEST_ptr(ctx = BN_CTX_new())
291e1051a39Sopenharmony_ci          && TEST_ptr(p = BN_new())
292e1051a39Sopenharmony_ci          && TEST_ptr(q = BN_new())
293e1051a39Sopenharmony_ci          && TEST_ptr(e = BN_new())
294e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, P))
295e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(q, Q))
296e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(e, E))
297e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_factors(key, p, q));
298e1051a39Sopenharmony_ci    if (!ret) {
299e1051a39Sopenharmony_ci        BN_free(p);
300e1051a39Sopenharmony_ci        BN_free(q);
301e1051a39Sopenharmony_ci        goto end;
302e1051a39Sopenharmony_ci    }
303e1051a39Sopenharmony_ci    ret = TEST_true(ossl_rsa_sp800_56b_derive_params_from_pq(key, 8, e, ctx))
304e1051a39Sopenharmony_ci          && TEST_BN_eq_word(key->n, N)
305e1051a39Sopenharmony_ci          && TEST_BN_eq_word(key->dmp1, DP)
306e1051a39Sopenharmony_ci          && TEST_BN_eq_word(key->dmq1, DQ)
307e1051a39Sopenharmony_ci          && TEST_BN_eq_word(key->iqmp, QINV)
308e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_crt_components(key, ctx))
309e1051a39Sopenharmony_ci          /* (a) 1 < dP < (p – 1). */
310e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmp1, 1))
311e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
312e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmp1, P-1))
313e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
314e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmp1, DP))
315e1051a39Sopenharmony_ci          /* (b) 1 < dQ < (q - 1). */
316e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmq1, 1))
317e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
318e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmq1, Q-1))
319e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
320e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmq1, DQ))
321e1051a39Sopenharmony_ci          /* (c) 1 < qInv < p */
322e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->iqmp, 1))
323e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
324e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->iqmp, P))
325e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
326e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->iqmp, QINV))
327e1051a39Sopenharmony_ci          /* (d) 1 = (dP . e) mod (p - 1)*/
328e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmp1, DP+1))
329e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
330e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmp1, DP))
331e1051a39Sopenharmony_ci          /* (e) 1 = (dQ . e) mod (q - 1) */
332e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmq1, DQ-1))
333e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
334e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->dmq1, DQ))
335e1051a39Sopenharmony_ci          /* (f) 1 = (qInv . q) mod p */
336e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->iqmp, QINV+1))
337e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_crt_components(key, ctx))
338e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(key->iqmp, QINV))
339e1051a39Sopenharmony_ci          /* check defaults are still valid */
340e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_crt_components(key, ctx));
341e1051a39Sopenharmony_ciend:
342e1051a39Sopenharmony_ci    BN_free(e);
343e1051a39Sopenharmony_ci    RSA_free(key);
344e1051a39Sopenharmony_ci    BN_CTX_free(ctx);
345e1051a39Sopenharmony_ci    return ret;
346e1051a39Sopenharmony_ci}
347e1051a39Sopenharmony_ci
348e1051a39Sopenharmony_cistatic int test_pq_diff(void)
349e1051a39Sopenharmony_ci{
350e1051a39Sopenharmony_ci    int ret = 0;
351e1051a39Sopenharmony_ci    BIGNUM *tmp = NULL, *p = NULL, *q = NULL;
352e1051a39Sopenharmony_ci
353e1051a39Sopenharmony_ci    ret = TEST_ptr(tmp = BN_new())
354e1051a39Sopenharmony_ci          && TEST_ptr(p = BN_new())
355e1051a39Sopenharmony_ci          && TEST_ptr(q = BN_new())
356e1051a39Sopenharmony_ci          /* |1-(2+1)| > 2^1 */
357e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 1))
358e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(q, 1+2))
359e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
360e1051a39Sopenharmony_ci          /* Check |p - q| > 2^(nbits/2 - 100) */
361e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(q, 1+3))
362e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202))
363e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(p, 1+3))
364e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(q, 1))
365e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_check_pminusq_diff(tmp, p, q, 202));
366e1051a39Sopenharmony_ci    BN_free(p);
367e1051a39Sopenharmony_ci    BN_free(q);
368e1051a39Sopenharmony_ci    BN_free(tmp);
369e1051a39Sopenharmony_ci    return ret;
370e1051a39Sopenharmony_ci}
371e1051a39Sopenharmony_ci
372e1051a39Sopenharmony_cistatic int test_invalid_keypair(void)
373e1051a39Sopenharmony_ci{
374e1051a39Sopenharmony_ci    int ret = 0;
375e1051a39Sopenharmony_ci    RSA *key = NULL;
376e1051a39Sopenharmony_ci    BN_CTX *ctx = NULL;
377e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;
378e1051a39Sopenharmony_ci
379e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
380e1051a39Sopenharmony_ci          && TEST_ptr(ctx = BN_CTX_new())
381e1051a39Sopenharmony_ci          /* NULL parameters */
382e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
383e1051a39Sopenharmony_ci          /* load key */
384e1051a39Sopenharmony_ci          && TEST_ptr(p = bn_load_new(cav_p, sizeof(cav_p)))
385e1051a39Sopenharmony_ci          && TEST_ptr(q = bn_load_new(cav_q, sizeof(cav_q)))
386e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_factors(key, p, q));
387e1051a39Sopenharmony_ci    if (!ret) {
388e1051a39Sopenharmony_ci        BN_free(p);
389e1051a39Sopenharmony_ci        BN_free(q);
390e1051a39Sopenharmony_ci        goto end;
391e1051a39Sopenharmony_ci    }
392e1051a39Sopenharmony_ci
393e1051a39Sopenharmony_ci    ret = TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
394e1051a39Sopenharmony_ci          && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
395e1051a39Sopenharmony_ci          && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
396e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_key(key, n, e, d));
397e1051a39Sopenharmony_ci    if (!ret) {
398e1051a39Sopenharmony_ci        BN_free(e);
399e1051a39Sopenharmony_ci        BN_free(n);
400e1051a39Sopenharmony_ci        BN_free(d);
401e1051a39Sopenharmony_ci        goto end;
402e1051a39Sopenharmony_ci    }
403e1051a39Sopenharmony_ci          /* bad strength/key size */
404e1051a39Sopenharmony_ci    ret = TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 100, 2048))
405e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 112, 1024))
406e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 128, 2048))
407e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, 140, 3072))
408e1051a39Sopenharmony_ci          /* mismatching exponent */
409e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, BN_value_one(),
410e1051a39Sopenharmony_ci                                                         -1, 2048))
411e1051a39Sopenharmony_ci          /* bad exponent */
412e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(e, 1))
413e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
414e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(e, 1))
415e1051a39Sopenharmony_ci
416e1051a39Sopenharmony_ci          /* mismatch between bits and modulus */
417e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 3072))
418e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, e, 112, 2048))
419e1051a39Sopenharmony_ci          /* check n == pq failure */
420e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(n, 1))
421e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
422e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(n, 1))
423e1051a39Sopenharmony_ci          /* check p  */
424e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(p, 2))
425e1051a39Sopenharmony_ci          && TEST_true(BN_mul(n, p, q, ctx))
426e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
427e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(p, 2))
428e1051a39Sopenharmony_ci          && TEST_true(BN_mul(n, p, q, ctx))
429e1051a39Sopenharmony_ci          /* check q  */
430e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(q, 2))
431e1051a39Sopenharmony_ci          && TEST_true(BN_mul(n, p, q, ctx))
432e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, 2048))
433e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(q, 2))
434e1051a39Sopenharmony_ci          && TEST_true(BN_mul(n, p, q, ctx));
435e1051a39Sopenharmony_ciend:
436e1051a39Sopenharmony_ci    RSA_free(key);
437e1051a39Sopenharmony_ci    BN_CTX_free(ctx);
438e1051a39Sopenharmony_ci    return ret;
439e1051a39Sopenharmony_ci}
440e1051a39Sopenharmony_ci
441e1051a39Sopenharmony_cistatic int keygen_size[] =
442e1051a39Sopenharmony_ci{
443e1051a39Sopenharmony_ci    2048, 3072
444e1051a39Sopenharmony_ci};
445e1051a39Sopenharmony_ci
446e1051a39Sopenharmony_cistatic int test_sp80056b_keygen(int id)
447e1051a39Sopenharmony_ci{
448e1051a39Sopenharmony_ci    RSA *key = NULL;
449e1051a39Sopenharmony_ci    int ret;
450e1051a39Sopenharmony_ci    int sz = keygen_size[id];
451e1051a39Sopenharmony_ci
452e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
453e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
454e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_sp800_56b_check_public(key))
455e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_sp800_56b_check_private(key))
456e1051a39Sopenharmony_ci          && TEST_true(ossl_rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
457e1051a39Sopenharmony_ci
458e1051a39Sopenharmony_ci    RSA_free(key);
459e1051a39Sopenharmony_ci    return ret;
460e1051a39Sopenharmony_ci}
461e1051a39Sopenharmony_ci
462e1051a39Sopenharmony_cistatic int test_check_private_key(void)
463e1051a39Sopenharmony_ci{
464e1051a39Sopenharmony_ci    int ret = 0;
465e1051a39Sopenharmony_ci    BIGNUM *n = NULL, *d = NULL, *e = NULL;
466e1051a39Sopenharmony_ci    RSA *key = NULL;
467e1051a39Sopenharmony_ci
468e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
469e1051a39Sopenharmony_ci          /* check NULL pointers fail */
470e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_private(key))
471e1051a39Sopenharmony_ci          /* load private key */
472e1051a39Sopenharmony_ci          && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
473e1051a39Sopenharmony_ci          && TEST_ptr(d = bn_load_new(cav_d, sizeof(cav_d)))
474e1051a39Sopenharmony_ci          && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
475e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_key(key, n, e, d));
476e1051a39Sopenharmony_ci    if (!ret) {
477e1051a39Sopenharmony_ci        BN_free(n);
478e1051a39Sopenharmony_ci        BN_free(e);
479e1051a39Sopenharmony_ci        BN_free(d);
480e1051a39Sopenharmony_ci        goto end;
481e1051a39Sopenharmony_ci    }
482e1051a39Sopenharmony_ci    /* check d is in range */
483e1051a39Sopenharmony_ci    ret = TEST_true(ossl_rsa_sp800_56b_check_private(key))
484e1051a39Sopenharmony_ci          /* check d is too low */
485e1051a39Sopenharmony_ci          && TEST_true(BN_set_word(d, 0))
486e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_private(key))
487e1051a39Sopenharmony_ci          /* check d is too high */
488e1051a39Sopenharmony_ci          && TEST_ptr(BN_copy(d, n))
489e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_private(key));
490e1051a39Sopenharmony_ciend:
491e1051a39Sopenharmony_ci    RSA_free(key);
492e1051a39Sopenharmony_ci    return ret;
493e1051a39Sopenharmony_ci}
494e1051a39Sopenharmony_ci
495e1051a39Sopenharmony_cistatic int test_check_public_key(void)
496e1051a39Sopenharmony_ci{
497e1051a39Sopenharmony_ci    int ret = 0;
498e1051a39Sopenharmony_ci    BIGNUM *n = NULL, *e = NULL;
499e1051a39Sopenharmony_ci    RSA *key = NULL;
500e1051a39Sopenharmony_ci
501e1051a39Sopenharmony_ci    ret = TEST_ptr(key = RSA_new())
502e1051a39Sopenharmony_ci          /* check NULL pointers fail */
503e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_public(key))
504e1051a39Sopenharmony_ci          /* load public key */
505e1051a39Sopenharmony_ci          && TEST_ptr(e = bn_load_new(cav_e, sizeof(cav_e)))
506e1051a39Sopenharmony_ci          && TEST_ptr(n = bn_load_new(cav_n, sizeof(cav_n)))
507e1051a39Sopenharmony_ci          && TEST_true(RSA_set0_key(key, n, e, NULL));
508e1051a39Sopenharmony_ci    if (!ret) {
509e1051a39Sopenharmony_ci        BN_free(e);
510e1051a39Sopenharmony_ci        BN_free(n);
511e1051a39Sopenharmony_ci        goto end;
512e1051a39Sopenharmony_ci    }
513e1051a39Sopenharmony_ci    /* check public key is valid */
514e1051a39Sopenharmony_ci    ret = TEST_true(ossl_rsa_sp800_56b_check_public(key))
515e1051a39Sopenharmony_ci          /* check fail if n is even */
516e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(n, 1))
517e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_public(key))
518e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(n, 1))
519e1051a39Sopenharmony_ci          /* check fail if n is wrong number of bits */
520e1051a39Sopenharmony_ci          && TEST_true(BN_lshift1(n, n))
521e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_public(key))
522e1051a39Sopenharmony_ci          && TEST_true(BN_rshift1(n, n))
523e1051a39Sopenharmony_ci          /* test odd exponent fails */
524e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(e, 1))
525e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_public(key))
526e1051a39Sopenharmony_ci          && TEST_true(BN_sub_word(e, 1))
527e1051a39Sopenharmony_ci          /* modulus fails composite check */
528e1051a39Sopenharmony_ci          && TEST_true(BN_add_word(n, 2))
529e1051a39Sopenharmony_ci          && TEST_false(ossl_rsa_sp800_56b_check_public(key));
530e1051a39Sopenharmony_ciend:
531e1051a39Sopenharmony_ci    RSA_free(key);
532e1051a39Sopenharmony_ci    return ret;
533e1051a39Sopenharmony_ci}
534e1051a39Sopenharmony_ci
535e1051a39Sopenharmony_ciint setup_tests(void)
536e1051a39Sopenharmony_ci{
537e1051a39Sopenharmony_ci    ADD_TEST(test_check_public_exponent);
538e1051a39Sopenharmony_ci    ADD_TEST(test_check_prime_factor_range);
539e1051a39Sopenharmony_ci    ADD_TEST(test_check_prime_factor);
540e1051a39Sopenharmony_ci    ADD_TEST(test_check_private_exponent);
541e1051a39Sopenharmony_ci    ADD_TEST(test_check_crt_components);
542e1051a39Sopenharmony_ci    ADD_TEST(test_check_private_key);
543e1051a39Sopenharmony_ci    ADD_TEST(test_check_public_key);
544e1051a39Sopenharmony_ci    ADD_TEST(test_invalid_keypair);
545e1051a39Sopenharmony_ci    ADD_TEST(test_pq_diff);
546e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_sp80056b_keygen, (int)OSSL_NELEM(keygen_size));
547e1051a39Sopenharmony_ci    return 1;
548e1051a39Sopenharmony_ci}
549