xref: /freebsd/crypto/openssl/test/ffc_internal_test.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  * Copyright (c) 2019-2020, Oracle and/or its affiliates.  All rights reserved.
4*e0c4386eSCy Schubert  *
5*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
6*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
7*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
8*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
9*e0c4386eSCy Schubert  */
10*e0c4386eSCy Schubert 
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert  * This is an internal test that is intentionally using internal APIs. Some of
13*e0c4386eSCy Schubert  * those APIs are deprecated for public use.
14*e0c4386eSCy Schubert  */
15*e0c4386eSCy Schubert #include "internal/deprecated.h"
16*e0c4386eSCy Schubert 
17*e0c4386eSCy Schubert #include <stdio.h>
18*e0c4386eSCy Schubert #include <stdlib.h>
19*e0c4386eSCy Schubert #include <string.h>
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert #include "internal/nelem.h"
22*e0c4386eSCy Schubert #include <openssl/crypto.h>
23*e0c4386eSCy Schubert #include <openssl/bio.h>
24*e0c4386eSCy Schubert #include <openssl/bn.h>
25*e0c4386eSCy Schubert #include <openssl/rand.h>
26*e0c4386eSCy Schubert #include <openssl/err.h>
27*e0c4386eSCy Schubert #include "testutil.h"
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert #include "internal/ffc.h"
30*e0c4386eSCy Schubert #include "crypto/security_bits.h"
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
33*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha224_p[] = {
34*e0c4386eSCy Schubert     0x93, 0x57, 0x93, 0x62, 0x1b, 0x9a, 0x10, 0x9b, 0xc1, 0x56, 0x0f, 0x24,
35*e0c4386eSCy Schubert     0x71, 0x76, 0x4e, 0xd3, 0xed, 0x78, 0x78, 0x7a, 0xbf, 0x89, 0x71, 0x67,
36*e0c4386eSCy Schubert     0x8e, 0x03, 0xd8, 0x5b, 0xcd, 0x22, 0x8f, 0x70, 0x74, 0xff, 0x22, 0x05,
37*e0c4386eSCy Schubert     0x07, 0x0c, 0x4c, 0x60, 0xed, 0x41, 0xe1, 0x9e, 0x9c, 0xaa, 0x3e, 0x19,
38*e0c4386eSCy Schubert     0x5c, 0x3d, 0x80, 0x58, 0xb2, 0x7f, 0x5f, 0x89, 0xec, 0xb5, 0x19, 0xdb,
39*e0c4386eSCy Schubert     0x06, 0x11, 0xe9, 0x78, 0x5c, 0xf9, 0xa0, 0x9e, 0x70, 0x62, 0x14, 0x7b,
40*e0c4386eSCy Schubert     0xda, 0x92, 0xbf, 0xb2, 0x6b, 0x01, 0x6f, 0xb8, 0x68, 0x9c, 0x89, 0x36,
41*e0c4386eSCy Schubert     0x89, 0x72, 0x79, 0x49, 0x93, 0x3d, 0x14, 0xb2, 0x2d, 0xbb, 0xf0, 0xdf,
42*e0c4386eSCy Schubert     0x94, 0x45, 0x0b, 0x5f, 0xf1, 0x75, 0x37, 0xeb, 0x49, 0xb9, 0x2d, 0xce,
43*e0c4386eSCy Schubert     0xb7, 0xf4, 0x95, 0x77, 0xc2, 0xe9, 0x39, 0x1c, 0x4e, 0x0c, 0x40, 0x62,
44*e0c4386eSCy Schubert     0x33, 0x0a, 0xe6, 0x29, 0x6f, 0xba, 0xef, 0x02, 0xdd, 0x0d, 0xe4, 0x04,
45*e0c4386eSCy Schubert     0x01, 0x70, 0x40, 0xb9, 0xc9, 0x7e, 0x2f, 0x10, 0x37, 0xe9, 0xde, 0xb0,
46*e0c4386eSCy Schubert     0xf6, 0xeb, 0x71, 0x7f, 0x9c, 0x35, 0x16, 0xf3, 0x0d, 0xc4, 0xe8, 0x02,
47*e0c4386eSCy Schubert     0x37, 0x6c, 0xdd, 0xb3, 0x8d, 0x2d, 0x1e, 0x28, 0x13, 0x22, 0x89, 0x40,
48*e0c4386eSCy Schubert     0xe5, 0xfa, 0x16, 0x67, 0xd6, 0xda, 0x12, 0xa2, 0x38, 0x83, 0x25, 0xcc,
49*e0c4386eSCy Schubert     0x26, 0xc1, 0x27, 0x74, 0xfe, 0xf6, 0x7a, 0xb6, 0xa1, 0xe4, 0xe8, 0xdf,
50*e0c4386eSCy Schubert     0x5d, 0xd2, 0x9c, 0x2f, 0xec, 0xea, 0x08, 0xca, 0x48, 0xdb, 0x18, 0x4b,
51*e0c4386eSCy Schubert     0x12, 0xee, 0x16, 0x9b, 0xa6, 0x00, 0xa0, 0x18, 0x98, 0x7d, 0xce, 0x6c,
52*e0c4386eSCy Schubert     0x6d, 0xf8, 0xfc, 0x95, 0x51, 0x1b, 0x0a, 0x40, 0xb6, 0xfc, 0xe5, 0xe2,
53*e0c4386eSCy Schubert     0xb0, 0x26, 0x53, 0x4c, 0xd7, 0xfe, 0xaa, 0x6d, 0xbc, 0xdd, 0xc0, 0x61,
54*e0c4386eSCy Schubert     0x65, 0xe4, 0x89, 0x44, 0x18, 0x6f, 0xd5, 0x39, 0xcf, 0x75, 0x6d, 0x29,
55*e0c4386eSCy Schubert     0xcc, 0xf8, 0x40, 0xab
56*e0c4386eSCy Schubert };
57*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha224_q[] = {
58*e0c4386eSCy Schubert     0xf2, 0x5e, 0x4e, 0x9a, 0x15, 0xa8, 0x13, 0xdf, 0xa3, 0x17, 0x90, 0xc6,
59*e0c4386eSCy Schubert     0xd6, 0x5e, 0xb1, 0xfb, 0x31, 0xf8, 0xb5, 0xb1, 0x4b, 0xa7, 0x6d, 0xde,
60*e0c4386eSCy Schubert     0x57, 0x76, 0x6f, 0x11
61*e0c4386eSCy Schubert };
62*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha224_seed[] = {
63*e0c4386eSCy Schubert     0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85,
64*e0c4386eSCy Schubert     0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3,
65*e0c4386eSCy Schubert     0x36, 0x17, 0x06, 0xcf
66*e0c4386eSCy Schubert };
67*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha224_bad_seed[] = {
68*e0c4386eSCy Schubert     0xd2, 0xb1, 0x36, 0xd8, 0x5b, 0x8e, 0xa4, 0xb2, 0x6a, 0xab, 0x4e, 0x85,
69*e0c4386eSCy Schubert     0x8b, 0x49, 0xf9, 0xdd, 0xe6, 0xa1, 0xcd, 0xad, 0x49, 0x52, 0xe9, 0xb3,
70*e0c4386eSCy Schubert     0x36, 0x17, 0x06, 0xd0
71*e0c4386eSCy Schubert };
72*e0c4386eSCy Schubert static int dsa_2048_224_sha224_counter = 2878;
73*e0c4386eSCy Schubert 
74*e0c4386eSCy Schubert static const unsigned char dsa_3072_256_sha512_p[] = {
75*e0c4386eSCy Schubert     0x9a, 0x82, 0x8b, 0x8d, 0xea, 0xd0, 0x56, 0x23, 0x88, 0x2d, 0x5d, 0x41,
76*e0c4386eSCy Schubert     0x42, 0x4c, 0x13, 0x5a, 0x15, 0x81, 0x59, 0x02, 0xc5, 0x00, 0x82, 0x28,
77*e0c4386eSCy Schubert     0x01, 0xee, 0x8f, 0x99, 0xfd, 0x6a, 0x95, 0xf2, 0x0f, 0xae, 0x34, 0x77,
78*e0c4386eSCy Schubert     0x29, 0xcc, 0xc7, 0x50, 0x0e, 0x03, 0xef, 0xb0, 0x4d, 0xe5, 0x10, 0x00,
79*e0c4386eSCy Schubert     0xa8, 0x7b, 0xce, 0x8c, 0xc6, 0xb2, 0x01, 0x74, 0x23, 0x1b, 0x7f, 0xe8,
80*e0c4386eSCy Schubert     0xf9, 0x71, 0x28, 0x39, 0xcf, 0x18, 0x04, 0xb2, 0x95, 0x61, 0x2d, 0x11,
81*e0c4386eSCy Schubert     0x71, 0x6b, 0xdd, 0x0d, 0x0b, 0xf0, 0xe6, 0x97, 0x52, 0x29, 0x9d, 0x45,
82*e0c4386eSCy Schubert     0xb1, 0x23, 0xda, 0xb0, 0xd5, 0xcb, 0x51, 0x71, 0x8e, 0x40, 0x9c, 0x97,
83*e0c4386eSCy Schubert     0x13, 0xea, 0x1f, 0x4b, 0x32, 0x5d, 0x27, 0x74, 0x81, 0x8d, 0x47, 0x8a,
84*e0c4386eSCy Schubert     0x08, 0xce, 0xf4, 0xd1, 0x28, 0xa2, 0x0f, 0x9b, 0x2e, 0xc9, 0xa3, 0x0e,
85*e0c4386eSCy Schubert     0x5d, 0xde, 0x47, 0x19, 0x6d, 0x5f, 0x98, 0xe0, 0x8e, 0x7f, 0x60, 0x8f,
86*e0c4386eSCy Schubert     0x25, 0xa7, 0xa4, 0xeb, 0xb9, 0xf3, 0x24, 0xa4, 0x9e, 0xc1, 0xbd, 0x14,
87*e0c4386eSCy Schubert     0x27, 0x7c, 0x27, 0xc8, 0x4f, 0x5f, 0xed, 0xfd, 0x86, 0xc8, 0xf1, 0xd7,
88*e0c4386eSCy Schubert     0x82, 0xe2, 0xeb, 0xe5, 0xd2, 0xbe, 0xb0, 0x65, 0x28, 0xab, 0x99, 0x9e,
89*e0c4386eSCy Schubert     0xcd, 0xd5, 0x22, 0xf8, 0x1b, 0x3b, 0x01, 0xe9, 0x20, 0x3d, 0xe4, 0x98,
90*e0c4386eSCy Schubert     0x22, 0xfe, 0xfc, 0x09, 0x7e, 0x95, 0x20, 0xda, 0xb6, 0x12, 0x2c, 0x94,
91*e0c4386eSCy Schubert     0x5c, 0xea, 0x74, 0x71, 0xbd, 0x19, 0xac, 0x78, 0x43, 0x02, 0x51, 0xb8,
92*e0c4386eSCy Schubert     0x5f, 0x06, 0x1d, 0xea, 0xc8, 0xa4, 0x3b, 0xc9, 0x78, 0xa3, 0x2b, 0x09,
93*e0c4386eSCy Schubert     0xdc, 0x76, 0x74, 0xc4, 0x23, 0x14, 0x48, 0x2e, 0x84, 0x2b, 0xa3, 0x82,
94*e0c4386eSCy Schubert     0xc1, 0xba, 0x0b, 0x39, 0x2a, 0x9f, 0x24, 0x7b, 0xd6, 0xc2, 0xea, 0x5a,
95*e0c4386eSCy Schubert     0xb6, 0xbd, 0x15, 0x82, 0x21, 0x85, 0xe0, 0x6b, 0x12, 0x4f, 0x8d, 0x64,
96*e0c4386eSCy Schubert     0x75, 0xeb, 0x7e, 0xa1, 0xdb, 0xe0, 0x9d, 0x25, 0xae, 0x3b, 0xe9, 0x9b,
97*e0c4386eSCy Schubert     0x21, 0x7f, 0x9a, 0x3d, 0x66, 0xd0, 0x52, 0x1d, 0x39, 0x8b, 0xeb, 0xfc,
98*e0c4386eSCy Schubert     0xec, 0xbe, 0x72, 0x20, 0x5a, 0xdf, 0x1b, 0x00, 0xf1, 0x0e, 0xed, 0xc6,
99*e0c4386eSCy Schubert     0x78, 0x6f, 0xc9, 0xab, 0xe4, 0xd6, 0x81, 0x8b, 0xcc, 0xf6, 0xd4, 0x6a,
100*e0c4386eSCy Schubert     0x31, 0x62, 0x08, 0xd9, 0x38, 0x21, 0x8f, 0xda, 0x9e, 0xb1, 0x2b, 0x9c,
101*e0c4386eSCy Schubert     0xc0, 0xbe, 0xf7, 0x9a, 0x43, 0x2d, 0x07, 0x59, 0x46, 0x0e, 0xd5, 0x23,
102*e0c4386eSCy Schubert     0x4e, 0xaa, 0x4a, 0x04, 0xc2, 0xde, 0x33, 0xa6, 0x34, 0xba, 0xac, 0x4f,
103*e0c4386eSCy Schubert     0x78, 0xd8, 0xca, 0x76, 0xce, 0x5e, 0xd4, 0xf6, 0x85, 0x4c, 0x6a, 0x60,
104*e0c4386eSCy Schubert     0x08, 0x5d, 0x0e, 0x34, 0x8b, 0xf2, 0xb6, 0xe3, 0xb7, 0x51, 0xca, 0x43,
105*e0c4386eSCy Schubert     0xaa, 0x68, 0x7b, 0x0a, 0x6e, 0xea, 0xce, 0x1e, 0x2c, 0x34, 0x8e, 0x0f,
106*e0c4386eSCy Schubert     0xe2, 0xcc, 0x38, 0xf2, 0x9a, 0x98, 0xef, 0xe6, 0x7f, 0xf6, 0x62, 0xbb
107*e0c4386eSCy Schubert };
108*e0c4386eSCy Schubert static const unsigned char dsa_3072_256_sha512_q[] = {
109*e0c4386eSCy Schubert     0xc1, 0xdb, 0xc1, 0x21, 0x50, 0x49, 0x63, 0xa3, 0x77, 0x6d, 0x4c, 0x92,
110*e0c4386eSCy Schubert     0xed, 0x58, 0x9e, 0x98, 0xea, 0xac, 0x7a, 0x90, 0x13, 0x24, 0xf7, 0xcd,
111*e0c4386eSCy Schubert     0xd7, 0xe6, 0xd4, 0x8f, 0xf0, 0x45, 0x4b, 0xf7
112*e0c4386eSCy Schubert };
113*e0c4386eSCy Schubert static const unsigned char dsa_3072_256_sha512_seed[] = {
114*e0c4386eSCy Schubert     0x35, 0x24, 0xb5, 0x59, 0xd5, 0x27, 0x58, 0x10, 0xf6, 0xa2, 0x7c, 0x9a,
115*e0c4386eSCy Schubert     0x0d, 0xc2, 0x70, 0x8a, 0xb0, 0x41, 0x4a, 0x84, 0x0b, 0xfe, 0x66, 0xf5,
116*e0c4386eSCy Schubert     0x3a, 0xbf, 0x4a, 0xa9, 0xcb, 0xfc, 0xa6, 0x22
117*e0c4386eSCy Schubert };
118*e0c4386eSCy Schubert static int dsa_3072_256_sha512_counter = 1604;
119*e0c4386eSCy Schubert 
120*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha256_p[] = {
121*e0c4386eSCy Schubert     0xe9, 0x13, 0xbc, 0xf2, 0x14, 0x5d, 0xf9, 0x79, 0xd6, 0x6d, 0xf5, 0xc5,
122*e0c4386eSCy Schubert     0xbe, 0x7b, 0x6f, 0x90, 0x63, 0xd0, 0xfd, 0xee, 0x4f, 0xc4, 0x65, 0x83,
123*e0c4386eSCy Schubert     0xbf, 0xec, 0xc3, 0x2c, 0x5d, 0x30, 0xc8, 0xa4, 0x3b, 0x2f, 0x3b, 0x29,
124*e0c4386eSCy Schubert     0x43, 0x69, 0xfb, 0x6e, 0xa9, 0xa4, 0x07, 0x6c, 0xcd, 0xb0, 0xd2, 0xd9,
125*e0c4386eSCy Schubert     0xd3, 0xe6, 0xf4, 0x87, 0x16, 0xb7, 0xe5, 0x06, 0xb9, 0xba, 0xd6, 0x87,
126*e0c4386eSCy Schubert     0xbc, 0x01, 0x9e, 0xba, 0xc2, 0xcf, 0x39, 0xb6, 0xec, 0xdc, 0x75, 0x07,
127*e0c4386eSCy Schubert     0xc1, 0x39, 0x2d, 0x6a, 0x95, 0x31, 0x97, 0xda, 0x54, 0x20, 0x29, 0xe0,
128*e0c4386eSCy Schubert     0x1b, 0xf9, 0x74, 0x65, 0xaa, 0xc1, 0x47, 0xd3, 0x9e, 0xb4, 0x3c, 0x1d,
129*e0c4386eSCy Schubert     0xe0, 0xdc, 0x2d, 0x21, 0xab, 0x12, 0x3b, 0xa5, 0x51, 0x1e, 0xc6, 0xbc,
130*e0c4386eSCy Schubert     0x6b, 0x4c, 0x22, 0xd1, 0x7c, 0xc6, 0xce, 0xcb, 0x8c, 0x1d, 0x1f, 0xce,
131*e0c4386eSCy Schubert     0x1c, 0xe2, 0x75, 0x49, 0x6d, 0x2c, 0xee, 0x7f, 0x5f, 0xb8, 0x74, 0x42,
132*e0c4386eSCy Schubert     0x5c, 0x96, 0x77, 0x13, 0xff, 0x80, 0xf3, 0x05, 0xc7, 0xfe, 0x08, 0x3b,
133*e0c4386eSCy Schubert     0x25, 0x36, 0x46, 0xa2, 0xc4, 0x26, 0xb4, 0xb0, 0x3b, 0xd5, 0xb2, 0x4c,
134*e0c4386eSCy Schubert     0x13, 0x29, 0x0e, 0x47, 0x31, 0x66, 0x7d, 0x78, 0x57, 0xe6, 0xc2, 0xb5,
135*e0c4386eSCy Schubert     0x9f, 0x46, 0x17, 0xbc, 0xa9, 0x9a, 0x49, 0x1c, 0x0f, 0x45, 0xe0, 0x88,
136*e0c4386eSCy Schubert     0x97, 0xa1, 0x30, 0x7c, 0x42, 0xb7, 0x2c, 0x0a, 0xce, 0xb3, 0xa5, 0x7a,
137*e0c4386eSCy Schubert     0x61, 0x8e, 0xab, 0x44, 0xc1, 0xdc, 0x70, 0xe5, 0xda, 0x78, 0x2a, 0xb4,
138*e0c4386eSCy Schubert     0xe6, 0x3c, 0xa0, 0x58, 0xda, 0x62, 0x0a, 0xb2, 0xa9, 0x3d, 0xaa, 0x49,
139*e0c4386eSCy Schubert     0x7e, 0x7f, 0x9a, 0x19, 0x67, 0xee, 0xd6, 0xe3, 0x67, 0x13, 0xe8, 0x6f,
140*e0c4386eSCy Schubert     0x79, 0x50, 0x76, 0xfc, 0xb3, 0x9d, 0x7e, 0x9e, 0x3e, 0x6e, 0x47, 0xb1,
141*e0c4386eSCy Schubert     0x11, 0x5e, 0xc8, 0x83, 0x3a, 0x3c, 0xfc, 0x82, 0x5c, 0x9d, 0x34, 0x65,
142*e0c4386eSCy Schubert     0x73, 0xb4, 0x56, 0xd5
143*e0c4386eSCy Schubert };
144*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha256_q[] = {
145*e0c4386eSCy Schubert     0xb0, 0xdf, 0xa1, 0x7b, 0xa4, 0x77, 0x64, 0x0e, 0xb9, 0x28, 0xbb, 0xbc,
146*e0c4386eSCy Schubert     0xd4, 0x60, 0x02, 0xaf, 0x21, 0x8c, 0xb0, 0x69, 0x0f, 0x8a, 0x7b, 0xc6,
147*e0c4386eSCy Schubert     0x80, 0xcb, 0x0a, 0x45
148*e0c4386eSCy Schubert };
149*e0c4386eSCy Schubert static const unsigned char dsa_2048_224_sha256_g[] = {
150*e0c4386eSCy Schubert     0x11, 0x7c, 0x5f, 0xf6, 0x99, 0x44, 0x67, 0x5b, 0x69, 0xa3, 0x83, 0xef,
151*e0c4386eSCy Schubert     0xb5, 0x85, 0xa2, 0x19, 0x35, 0x18, 0x2a, 0xf2, 0x58, 0xf4, 0xc9, 0x58,
152*e0c4386eSCy Schubert     0x9e, 0xb9, 0xe8, 0x91, 0x17, 0x2f, 0xb0, 0x60, 0x85, 0x95, 0xa6, 0x62,
153*e0c4386eSCy Schubert     0x36, 0xd0, 0xff, 0x94, 0xb9, 0xa6, 0x50, 0xad, 0xa6, 0xf6, 0x04, 0x28,
154*e0c4386eSCy Schubert     0xc2, 0xc9, 0xb9, 0x75, 0xf3, 0x66, 0xb4, 0xeb, 0xf6, 0xd5, 0x06, 0x13,
155*e0c4386eSCy Schubert     0x01, 0x64, 0x82, 0xa9, 0xf1, 0xd5, 0x41, 0xdc, 0xf2, 0x08, 0xfc, 0x2f,
156*e0c4386eSCy Schubert     0xc4, 0xa1, 0x21, 0xee, 0x7d, 0xbc, 0xda, 0x5a, 0xa4, 0xa2, 0xb9, 0x68,
157*e0c4386eSCy Schubert     0x87, 0x36, 0xba, 0x53, 0x9e, 0x14, 0x4e, 0x76, 0x5c, 0xba, 0x79, 0x3d,
158*e0c4386eSCy Schubert     0x0f, 0xe5, 0x99, 0x1c, 0x27, 0xfc, 0xaf, 0x10, 0x63, 0x87, 0x68, 0x0e,
159*e0c4386eSCy Schubert     0x3e, 0x6e, 0xaa, 0xf3, 0xdf, 0x76, 0x7e, 0x02, 0x9a, 0x41, 0x96, 0xa1,
160*e0c4386eSCy Schubert     0x6c, 0xbb, 0x67, 0xee, 0x0c, 0xad, 0x72, 0x65, 0xf1, 0x70, 0xb0, 0x39,
161*e0c4386eSCy Schubert     0x9b, 0x54, 0x5f, 0xd7, 0x6c, 0xc5, 0x9a, 0x90, 0x53, 0x18, 0xde, 0x5e,
162*e0c4386eSCy Schubert     0x62, 0x89, 0xb9, 0x2f, 0x66, 0x59, 0x3a, 0x3d, 0x10, 0xeb, 0xa5, 0x99,
163*e0c4386eSCy Schubert     0xf6, 0x21, 0x7d, 0xf2, 0x7b, 0x42, 0x15, 0x1c, 0x55, 0x79, 0x15, 0xaa,
164*e0c4386eSCy Schubert     0xa4, 0x17, 0x2e, 0x48, 0xc3, 0xa8, 0x36, 0xf5, 0x1a, 0x97, 0xce, 0xbd,
165*e0c4386eSCy Schubert     0x72, 0xef, 0x1d, 0x50, 0x5b, 0xb1, 0x60, 0x0a, 0x5c, 0x0b, 0xa6, 0x21,
166*e0c4386eSCy Schubert     0x38, 0x28, 0x4e, 0x89, 0x33, 0x1d, 0xb5, 0x7e, 0x5c, 0xf1, 0x6b, 0x2c,
167*e0c4386eSCy Schubert     0xbd, 0xad, 0x84, 0xb2, 0x8e, 0x96, 0xe2, 0x30, 0xe7, 0x54, 0xb8, 0xc9,
168*e0c4386eSCy Schubert     0x70, 0xcb, 0x10, 0x30, 0x63, 0x90, 0xf4, 0x45, 0x64, 0x93, 0x09, 0x38,
169*e0c4386eSCy Schubert     0x6a, 0x47, 0x58, 0x31, 0x04, 0x1a, 0x18, 0x04, 0x1a, 0xe0, 0xd7, 0x0b,
170*e0c4386eSCy Schubert     0x3c, 0xbe, 0x2a, 0x9c, 0xec, 0xcc, 0x0d, 0x0c, 0xed, 0xde, 0x54, 0xbc,
171*e0c4386eSCy Schubert     0xe6, 0x93, 0x59, 0xfc
172*e0c4386eSCy Schubert };
173*e0c4386eSCy Schubert 
ffc_params_validate_g_unverified_test(void)174*e0c4386eSCy Schubert static int ffc_params_validate_g_unverified_test(void)
175*e0c4386eSCy Schubert {
176*e0c4386eSCy Schubert     int ret = 0, res;
177*e0c4386eSCy Schubert     FFC_PARAMS params;
178*e0c4386eSCy Schubert     BIGNUM *p = NULL, *q = NULL, *g = NULL;
179*e0c4386eSCy Schubert     BIGNUM *p1 = NULL, *g1 = NULL;
180*e0c4386eSCy Schubert 
181*e0c4386eSCy Schubert     ossl_ffc_params_init(&params);
182*e0c4386eSCy Schubert 
183*e0c4386eSCy Schubert     if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha256_p,
184*e0c4386eSCy Schubert                                 sizeof(dsa_2048_224_sha256_p), NULL)))
185*e0c4386eSCy Schubert         goto err;
186*e0c4386eSCy Schubert     p1 = p;
187*e0c4386eSCy Schubert     if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha256_q,
188*e0c4386eSCy Schubert                                 sizeof(dsa_2048_224_sha256_q), NULL)))
189*e0c4386eSCy Schubert         goto err;
190*e0c4386eSCy Schubert     if (!TEST_ptr(g = BN_bin2bn(dsa_2048_224_sha256_g,
191*e0c4386eSCy Schubert                                 sizeof(dsa_2048_224_sha256_g), NULL)))
192*e0c4386eSCy Schubert         goto err;
193*e0c4386eSCy Schubert     g1 = g;
194*e0c4386eSCy Schubert 
195*e0c4386eSCy Schubert     /* Fail if g is NULL */
196*e0c4386eSCy Schubert     ossl_ffc_params_set0_pqg(&params, p, q, NULL);
197*e0c4386eSCy Schubert     p = NULL;
198*e0c4386eSCy Schubert     q = NULL;
199*e0c4386eSCy Schubert     ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G);
200*e0c4386eSCy Schubert     ossl_ffc_set_digest(&params, "SHA256", NULL);
201*e0c4386eSCy Schubert 
202*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
203*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
204*e0c4386eSCy Schubert                                                        &res, NULL)))
205*e0c4386eSCy Schubert         goto err;
206*e0c4386eSCy Schubert 
207*e0c4386eSCy Schubert     ossl_ffc_params_set0_pqg(&params, p, q, g);
208*e0c4386eSCy Schubert     g = NULL;
209*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
210*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DSA,
211*e0c4386eSCy Schubert                                                       &res, NULL)))
212*e0c4386eSCy Schubert         goto err;
213*e0c4386eSCy Schubert 
214*e0c4386eSCy Schubert     /* incorrect g */
215*e0c4386eSCy Schubert     BN_add_word(g1, 1);
216*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
217*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
218*e0c4386eSCy Schubert                                                        &res, NULL)))
219*e0c4386eSCy Schubert         goto err;
220*e0c4386eSCy Schubert 
221*e0c4386eSCy Schubert     /* fail if g < 2 */
222*e0c4386eSCy Schubert     BN_set_word(g1, 1);
223*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
224*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
225*e0c4386eSCy Schubert                                                        &res, NULL)))
226*e0c4386eSCy Schubert         goto err;
227*e0c4386eSCy Schubert 
228*e0c4386eSCy Schubert     BN_copy(g1, p1);
229*e0c4386eSCy Schubert     /* Fail if g >= p */
230*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
231*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
232*e0c4386eSCy Schubert                                                        &res, NULL)))
233*e0c4386eSCy Schubert         goto err;
234*e0c4386eSCy Schubert 
235*e0c4386eSCy Schubert     ret = 1;
236*e0c4386eSCy Schubert err:
237*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&params);
238*e0c4386eSCy Schubert     BN_free(p);
239*e0c4386eSCy Schubert     BN_free(q);
240*e0c4386eSCy Schubert     BN_free(g);
241*e0c4386eSCy Schubert     return ret;
242*e0c4386eSCy Schubert }
243*e0c4386eSCy Schubert 
ffc_params_validate_pq_test(void)244*e0c4386eSCy Schubert static int ffc_params_validate_pq_test(void)
245*e0c4386eSCy Schubert {
246*e0c4386eSCy Schubert     int ret = 0, res = -1;
247*e0c4386eSCy Schubert     FFC_PARAMS params;
248*e0c4386eSCy Schubert     BIGNUM *p = NULL, *q = NULL;
249*e0c4386eSCy Schubert 
250*e0c4386eSCy Schubert     ossl_ffc_params_init(&params);
251*e0c4386eSCy Schubert     if (!TEST_ptr(p = BN_bin2bn(dsa_2048_224_sha224_p,
252*e0c4386eSCy Schubert                                    sizeof(dsa_2048_224_sha224_p),
253*e0c4386eSCy Schubert                                    NULL)))
254*e0c4386eSCy Schubert         goto err;
255*e0c4386eSCy Schubert     if (!TEST_ptr(q = BN_bin2bn(dsa_2048_224_sha224_q,
256*e0c4386eSCy Schubert                                    sizeof(dsa_2048_224_sha224_q),
257*e0c4386eSCy Schubert                                    NULL)))
258*e0c4386eSCy Schubert         goto err;
259*e0c4386eSCy Schubert 
260*e0c4386eSCy Schubert     /* No p */
261*e0c4386eSCy Schubert     ossl_ffc_params_set0_pqg(&params, NULL, q, NULL);
262*e0c4386eSCy Schubert     q = NULL;
263*e0c4386eSCy Schubert     ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_PQ);
264*e0c4386eSCy Schubert     ossl_ffc_set_digest(&params, "SHA224", NULL);
265*e0c4386eSCy Schubert 
266*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
267*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
268*e0c4386eSCy Schubert                                                        &res, NULL)))
269*e0c4386eSCy Schubert         goto err;
270*e0c4386eSCy Schubert 
271*e0c4386eSCy Schubert     /* Test valid case */
272*e0c4386eSCy Schubert     ossl_ffc_params_set0_pqg(&params, p, NULL, NULL);
273*e0c4386eSCy Schubert     p = NULL;
274*e0c4386eSCy Schubert     ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
275*e0c4386eSCy Schubert                                         sizeof(dsa_2048_224_sha224_seed),
276*e0c4386eSCy Schubert                                         dsa_2048_224_sha224_counter);
277*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
278*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DSA,
279*e0c4386eSCy Schubert                                                       &res, NULL)))
280*e0c4386eSCy Schubert         goto err;
281*e0c4386eSCy Schubert 
282*e0c4386eSCy Schubert     /* Bad counter - so p is not prime */
283*e0c4386eSCy Schubert     ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
284*e0c4386eSCy Schubert                                         sizeof(dsa_2048_224_sha224_seed),
285*e0c4386eSCy Schubert                                         1);
286*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
287*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
288*e0c4386eSCy Schubert                                                        &res, NULL)))
289*e0c4386eSCy Schubert         goto err;
290*e0c4386eSCy Schubert 
291*e0c4386eSCy Schubert     /* seedlen smaller than N */
292*e0c4386eSCy Schubert     ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_seed,
293*e0c4386eSCy Schubert                                         sizeof(dsa_2048_224_sha224_seed)-1,
294*e0c4386eSCy Schubert                                         dsa_2048_224_sha224_counter);
295*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
296*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
297*e0c4386eSCy Schubert                                                        &res, NULL)))
298*e0c4386eSCy Schubert         goto err;
299*e0c4386eSCy Schubert 
300*e0c4386eSCy Schubert     /* Provided seed doesnt produce a valid prime q */
301*e0c4386eSCy Schubert     ossl_ffc_params_set_validate_params(&params, dsa_2048_224_sha224_bad_seed,
302*e0c4386eSCy Schubert                                         sizeof(dsa_2048_224_sha224_bad_seed),
303*e0c4386eSCy Schubert                                         dsa_2048_224_sha224_counter);
304*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
305*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
306*e0c4386eSCy Schubert                                                        &res, NULL)))
307*e0c4386eSCy Schubert         goto err;
308*e0c4386eSCy Schubert 
309*e0c4386eSCy Schubert     if (!TEST_ptr(p = BN_bin2bn(dsa_3072_256_sha512_p,
310*e0c4386eSCy Schubert                                 sizeof(dsa_3072_256_sha512_p), NULL)))
311*e0c4386eSCy Schubert         goto err;
312*e0c4386eSCy Schubert     if (!TEST_ptr(q = BN_bin2bn(dsa_3072_256_sha512_q,
313*e0c4386eSCy Schubert                                 sizeof(dsa_3072_256_sha512_q),
314*e0c4386eSCy Schubert                                 NULL)))
315*e0c4386eSCy Schubert         goto err;
316*e0c4386eSCy Schubert 
317*e0c4386eSCy Schubert 
318*e0c4386eSCy Schubert     ossl_ffc_params_set0_pqg(&params, p, q, NULL);
319*e0c4386eSCy Schubert     p = q  = NULL;
320*e0c4386eSCy Schubert     ossl_ffc_set_digest(&params, "SHA512", NULL);
321*e0c4386eSCy Schubert     ossl_ffc_params_set_validate_params(&params, dsa_3072_256_sha512_seed,
322*e0c4386eSCy Schubert                                         sizeof(dsa_3072_256_sha512_seed),
323*e0c4386eSCy Schubert                                         dsa_3072_256_sha512_counter);
324*e0c4386eSCy Schubert     /* Q doesn't div P-1 */
325*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
326*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
327*e0c4386eSCy Schubert                                                        &res, NULL)))
328*e0c4386eSCy Schubert         goto err;
329*e0c4386eSCy Schubert 
330*e0c4386eSCy Schubert     /* Bad L/N for FIPS DH */
331*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
332*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DH,
333*e0c4386eSCy Schubert                                                        &res, NULL)))
334*e0c4386eSCy Schubert         goto err;
335*e0c4386eSCy Schubert 
336*e0c4386eSCy Schubert     ret = 1;
337*e0c4386eSCy Schubert err:
338*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&params);
339*e0c4386eSCy Schubert     BN_free(p);
340*e0c4386eSCy Schubert     BN_free(q);
341*e0c4386eSCy Schubert     return ret;
342*e0c4386eSCy Schubert }
343*e0c4386eSCy Schubert #endif /* OPENSSL_NO_DSA */
344*e0c4386eSCy Schubert 
345*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
ffc_params_gen_test(void)346*e0c4386eSCy Schubert static int ffc_params_gen_test(void)
347*e0c4386eSCy Schubert {
348*e0c4386eSCy Schubert     int ret = 0, res = -1;
349*e0c4386eSCy Schubert     FFC_PARAMS params;
350*e0c4386eSCy Schubert 
351*e0c4386eSCy Schubert     ossl_ffc_params_init(&params);
352*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params,
353*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
354*e0c4386eSCy Schubert                                                       2048, 256, &res, NULL)))
355*e0c4386eSCy Schubert         goto err;
356*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
357*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
358*e0c4386eSCy Schubert                                                       &res, NULL)))
359*e0c4386eSCy Schubert         goto err;
360*e0c4386eSCy Schubert 
361*e0c4386eSCy Schubert     ret = 1;
362*e0c4386eSCy Schubert err:
363*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&params);
364*e0c4386eSCy Schubert     return ret;
365*e0c4386eSCy Schubert }
366*e0c4386eSCy Schubert 
ffc_params_gen_canonicalg_test(void)367*e0c4386eSCy Schubert static int ffc_params_gen_canonicalg_test(void)
368*e0c4386eSCy Schubert {
369*e0c4386eSCy Schubert     int ret = 0, res = -1;
370*e0c4386eSCy Schubert     FFC_PARAMS params;
371*e0c4386eSCy Schubert 
372*e0c4386eSCy Schubert     ossl_ffc_params_init(&params);
373*e0c4386eSCy Schubert     params.gindex = 1;
374*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_generate(NULL, &params,
375*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
376*e0c4386eSCy Schubert                                                       2048, 256, &res, NULL)))
377*e0c4386eSCy Schubert         goto err;
378*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
379*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
380*e0c4386eSCy Schubert                                                       &res, NULL)))
381*e0c4386eSCy Schubert         goto err;
382*e0c4386eSCy Schubert 
383*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4)))
384*e0c4386eSCy Schubert         goto err;
385*e0c4386eSCy Schubert 
386*e0c4386eSCy Schubert     ret = 1;
387*e0c4386eSCy Schubert err:
388*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&params);
389*e0c4386eSCy Schubert     return ret;
390*e0c4386eSCy Schubert }
391*e0c4386eSCy Schubert 
ffc_params_fips186_2_gen_validate_test(void)392*e0c4386eSCy Schubert static int ffc_params_fips186_2_gen_validate_test(void)
393*e0c4386eSCy Schubert {
394*e0c4386eSCy Schubert     int ret = 0, res = -1;
395*e0c4386eSCy Schubert     FFC_PARAMS params;
396*e0c4386eSCy Schubert     BIGNUM *bn = NULL;
397*e0c4386eSCy Schubert 
398*e0c4386eSCy Schubert     ossl_ffc_params_init(&params);
399*e0c4386eSCy Schubert     if (!TEST_ptr(bn = BN_new()))
400*e0c4386eSCy Schubert         goto err;
401*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_2_generate(NULL, &params,
402*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
403*e0c4386eSCy Schubert                                                       1024, 160, &res, NULL)))
404*e0c4386eSCy Schubert         goto err;
405*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_FIPS186_2_validate(NULL, &params,
406*e0c4386eSCy Schubert                                                       FFC_PARAM_TYPE_DH,
407*e0c4386eSCy Schubert                                                       &res, NULL)))
408*e0c4386eSCy Schubert         goto err;
409*e0c4386eSCy Schubert 
410*e0c4386eSCy Schubert     /*
411*e0c4386eSCy Schubert      * The fips186-2 generation should produce a different q compared to
412*e0c4386eSCy Schubert      * fips 186-4 given the same seed value. So validation of q will fail.
413*e0c4386eSCy Schubert      */
414*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
415*e0c4386eSCy Schubert                                                        FFC_PARAM_TYPE_DSA,
416*e0c4386eSCy Schubert                                                        &res, NULL)))
417*e0c4386eSCy Schubert         goto err;
418*e0c4386eSCy Schubert     /* As the params are randomly generated the error is one of the following */
419*e0c4386eSCy Schubert     if (!TEST_true(res == FFC_CHECK_Q_MISMATCH || res == FFC_CHECK_Q_NOT_PRIME))
420*e0c4386eSCy Schubert         goto err;
421*e0c4386eSCy Schubert 
422*e0c4386eSCy Schubert     ossl_ffc_params_set_flags(&params, FFC_PARAM_FLAG_VALIDATE_G);
423*e0c4386eSCy Schubert     /* Partially valid g test will still pass */
424*e0c4386eSCy Schubert     if (!TEST_int_eq(ossl_ffc_params_FIPS186_4_validate(NULL, &params,
425*e0c4386eSCy Schubert                                                         FFC_PARAM_TYPE_DSA,
426*e0c4386eSCy Schubert                                                         &res, NULL), 2))
427*e0c4386eSCy Schubert         goto err;
428*e0c4386eSCy Schubert 
429*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_print(bio_out, &params, 4)))
430*e0c4386eSCy Schubert         goto err;
431*e0c4386eSCy Schubert 
432*e0c4386eSCy Schubert     ret = 1;
433*e0c4386eSCy Schubert err:
434*e0c4386eSCy Schubert     BN_free(bn);
435*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&params);
436*e0c4386eSCy Schubert     return ret;
437*e0c4386eSCy Schubert }
438*e0c4386eSCy Schubert 
439*e0c4386eSCy Schubert extern FFC_PARAMS *ossl_dh_get0_params(DH *dh);
440*e0c4386eSCy Schubert 
ffc_public_validate_test(void)441*e0c4386eSCy Schubert static int ffc_public_validate_test(void)
442*e0c4386eSCy Schubert {
443*e0c4386eSCy Schubert     int ret = 0, res = -1;
444*e0c4386eSCy Schubert     FFC_PARAMS *params;
445*e0c4386eSCy Schubert     BIGNUM *pub = NULL;
446*e0c4386eSCy Schubert     DH *dh = NULL;
447*e0c4386eSCy Schubert 
448*e0c4386eSCy Schubert     if (!TEST_ptr(pub = BN_new()))
449*e0c4386eSCy Schubert         goto err;
450*e0c4386eSCy Schubert 
451*e0c4386eSCy Schubert     if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
452*e0c4386eSCy Schubert         goto err;
453*e0c4386eSCy Schubert     params = ossl_dh_get0_params(dh);
454*e0c4386eSCy Schubert 
455*e0c4386eSCy Schubert     if (!TEST_true(BN_set_word(pub, 1)))
456*e0c4386eSCy Schubert         goto err;
457*e0c4386eSCy Schubert     BN_set_negative(pub, 1);
458*e0c4386eSCy Schubert     /* Check must succeed but set res if public key is negative */
459*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
460*e0c4386eSCy Schubert         goto err;
461*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
462*e0c4386eSCy Schubert         goto err;
463*e0c4386eSCy Schubert     if (!TEST_true(BN_set_word(pub, 0)))
464*e0c4386eSCy Schubert         goto err;
465*e0c4386eSCy Schubert     /* Check must succeed but set res if public key is zero */
466*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
467*e0c4386eSCy Schubert         goto err;
468*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
469*e0c4386eSCy Schubert         goto err;
470*e0c4386eSCy Schubert     /* Check must succeed but set res if public key is 1 */
471*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, BN_value_one(), &res)))
472*e0c4386eSCy Schubert         goto err;
473*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_SMALL, res))
474*e0c4386eSCy Schubert         goto err;
475*e0c4386eSCy Schubert     if (!TEST_true(BN_add_word(pub, 2)))
476*e0c4386eSCy Schubert         goto err;
477*e0c4386eSCy Schubert     /* Pass if public key >= 2 */
478*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
479*e0c4386eSCy Schubert         goto err;
480*e0c4386eSCy Schubert 
481*e0c4386eSCy Schubert     if (!TEST_ptr(BN_copy(pub, params->p)))
482*e0c4386eSCy Schubert         goto err;
483*e0c4386eSCy Schubert     /* Check must succeed but set res if public key = p */
484*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
485*e0c4386eSCy Schubert         goto err;
486*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res))
487*e0c4386eSCy Schubert         goto err;
488*e0c4386eSCy Schubert 
489*e0c4386eSCy Schubert     if (!TEST_true(BN_sub_word(pub, 1)))
490*e0c4386eSCy Schubert         goto err;
491*e0c4386eSCy Schubert     /* Check must succeed but set res if public key = p - 1 */
492*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
493*e0c4386eSCy Schubert         goto err;
494*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_TOO_LARGE, res))
495*e0c4386eSCy Schubert         goto err;
496*e0c4386eSCy Schubert 
497*e0c4386eSCy Schubert     if (!TEST_true(BN_sub_word(pub, 1)))
498*e0c4386eSCy Schubert         goto err;
499*e0c4386eSCy Schubert     /* Check must succeed but set res if public key is not related to p & q */
500*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
501*e0c4386eSCy Schubert         goto err;
502*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PUBKEY_INVALID, res))
503*e0c4386eSCy Schubert         goto err;
504*e0c4386eSCy Schubert 
505*e0c4386eSCy Schubert     if (!TEST_true(BN_sub_word(pub, 5)))
506*e0c4386eSCy Schubert         goto err;
507*e0c4386eSCy Schubert     /* Pass if public key is valid */
508*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
509*e0c4386eSCy Schubert         goto err;
510*e0c4386eSCy Schubert 
511*e0c4386eSCy Schubert     /* Check must succeed but set res if params is NULL */
512*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(NULL, pub, &res)))
513*e0c4386eSCy Schubert         goto err;
514*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
515*e0c4386eSCy Schubert         goto err;
516*e0c4386eSCy Schubert     res = -1;
517*e0c4386eSCy Schubert     /* Check must succeed but set res if pubkey is NULL */
518*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, NULL, &res)))
519*e0c4386eSCy Schubert         goto err;
520*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
521*e0c4386eSCy Schubert         goto err;
522*e0c4386eSCy Schubert     res = -1;
523*e0c4386eSCy Schubert 
524*e0c4386eSCy Schubert     BN_free(params->p);
525*e0c4386eSCy Schubert     params->p = NULL;
526*e0c4386eSCy Schubert     /* Check must succeed but set res if params->p is NULL */
527*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
528*e0c4386eSCy Schubert         goto err;
529*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
530*e0c4386eSCy Schubert         goto err;
531*e0c4386eSCy Schubert 
532*e0c4386eSCy Schubert     ret = 1;
533*e0c4386eSCy Schubert err:
534*e0c4386eSCy Schubert     DH_free(dh);
535*e0c4386eSCy Schubert     BN_free(pub);
536*e0c4386eSCy Schubert     return ret;
537*e0c4386eSCy Schubert }
538*e0c4386eSCy Schubert 
ffc_private_validate_test(void)539*e0c4386eSCy Schubert static int ffc_private_validate_test(void)
540*e0c4386eSCy Schubert {
541*e0c4386eSCy Schubert     int ret = 0, res = -1;
542*e0c4386eSCy Schubert     FFC_PARAMS *params;
543*e0c4386eSCy Schubert     BIGNUM *priv = NULL;
544*e0c4386eSCy Schubert     DH *dh = NULL;
545*e0c4386eSCy Schubert 
546*e0c4386eSCy Schubert     if (!TEST_ptr(priv = BN_new()))
547*e0c4386eSCy Schubert         goto err;
548*e0c4386eSCy Schubert 
549*e0c4386eSCy Schubert     if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
550*e0c4386eSCy Schubert         goto err;
551*e0c4386eSCy Schubert     params = ossl_dh_get0_params(dh);
552*e0c4386eSCy Schubert 
553*e0c4386eSCy Schubert     if (!TEST_true(BN_set_word(priv, 1)))
554*e0c4386eSCy Schubert         goto err;
555*e0c4386eSCy Schubert     BN_set_negative(priv, 1);
556*e0c4386eSCy Schubert     /* Fail if priv key is negative */
557*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
558*e0c4386eSCy Schubert         goto err;
559*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res))
560*e0c4386eSCy Schubert         goto err;
561*e0c4386eSCy Schubert 
562*e0c4386eSCy Schubert     if (!TEST_true(BN_set_word(priv, 0)))
563*e0c4386eSCy Schubert         goto err;
564*e0c4386eSCy Schubert     /* Fail if priv key is zero */
565*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
566*e0c4386eSCy Schubert         goto err;
567*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_SMALL, res))
568*e0c4386eSCy Schubert         goto err;
569*e0c4386eSCy Schubert 
570*e0c4386eSCy Schubert     /* Pass if priv key >= 1 */
571*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_private_key(params->q, BN_value_one(),
572*e0c4386eSCy Schubert                                                  &res)))
573*e0c4386eSCy Schubert         goto err;
574*e0c4386eSCy Schubert 
575*e0c4386eSCy Schubert     if (!TEST_ptr(BN_copy(priv, params->q)))
576*e0c4386eSCy Schubert         goto err;
577*e0c4386eSCy Schubert     /* Fail if priv key = upper */
578*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_validate_private_key(params->q, priv, &res)))
579*e0c4386eSCy Schubert         goto err;
580*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PRIVKEY_TOO_LARGE, res))
581*e0c4386eSCy Schubert         goto err;
582*e0c4386eSCy Schubert 
583*e0c4386eSCy Schubert     if (!TEST_true(BN_sub_word(priv, 1)))
584*e0c4386eSCy Schubert         goto err;
585*e0c4386eSCy Schubert     /* Pass if priv key <= upper - 1 */
586*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
587*e0c4386eSCy Schubert         goto err;
588*e0c4386eSCy Schubert 
589*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_validate_private_key(NULL, priv, &res)))
590*e0c4386eSCy Schubert         goto err;
591*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
592*e0c4386eSCy Schubert         goto err;
593*e0c4386eSCy Schubert     res = -1;
594*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_validate_private_key(params->q, NULL, &res)))
595*e0c4386eSCy Schubert         goto err;
596*e0c4386eSCy Schubert     if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
597*e0c4386eSCy Schubert         goto err;
598*e0c4386eSCy Schubert 
599*e0c4386eSCy Schubert     ret = 1;
600*e0c4386eSCy Schubert err:
601*e0c4386eSCy Schubert     DH_free(dh);
602*e0c4386eSCy Schubert     BN_free(priv);
603*e0c4386eSCy Schubert     return ret;
604*e0c4386eSCy Schubert }
605*e0c4386eSCy Schubert 
ffc_private_gen_test(int index)606*e0c4386eSCy Schubert static int ffc_private_gen_test(int index)
607*e0c4386eSCy Schubert {
608*e0c4386eSCy Schubert     int ret = 0, res = -1, N;
609*e0c4386eSCy Schubert     FFC_PARAMS *params;
610*e0c4386eSCy Schubert     BIGNUM *priv = NULL;
611*e0c4386eSCy Schubert     DH *dh = NULL;
612*e0c4386eSCy Schubert     BN_CTX *ctx = NULL;
613*e0c4386eSCy Schubert 
614*e0c4386eSCy Schubert     if (!TEST_ptr(ctx = BN_CTX_new_ex(NULL)))
615*e0c4386eSCy Schubert         goto err;
616*e0c4386eSCy Schubert 
617*e0c4386eSCy Schubert     if (!TEST_ptr(priv = BN_new()))
618*e0c4386eSCy Schubert         goto err;
619*e0c4386eSCy Schubert 
620*e0c4386eSCy Schubert     if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe2048)))
621*e0c4386eSCy Schubert         goto err;
622*e0c4386eSCy Schubert     params = ossl_dh_get0_params(dh);
623*e0c4386eSCy Schubert 
624*e0c4386eSCy Schubert     N = BN_num_bits(params->q);
625*e0c4386eSCy Schubert     /* Fail since N < 2*s - where s = 112*/
626*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, 220, 112, priv)))
627*e0c4386eSCy Schubert         goto err;
628*e0c4386eSCy Schubert     /* fail since N > len(q) */
629*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, N + 1, 112, priv)))
630*e0c4386eSCy Schubert         goto err;
631*e0c4386eSCy Schubert     /* s must be always set */
632*e0c4386eSCy Schubert     if (!TEST_false(ossl_ffc_generate_private_key(ctx, params, N, 0, priv)))
633*e0c4386eSCy Schubert         goto err;
634*e0c4386eSCy Schubert     /* pass since 2s <= N <= len(q) */
635*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N, 112, priv)))
636*e0c4386eSCy Schubert         goto err;
637*e0c4386eSCy Schubert     /* pass since N = len(q) */
638*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
639*e0c4386eSCy Schubert         goto err;
640*e0c4386eSCy Schubert     /* pass since 2s <= N < len(q) */
641*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, N / 2, 112, priv)))
642*e0c4386eSCy Schubert         goto err;
643*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
644*e0c4386eSCy Schubert         goto err;
645*e0c4386eSCy Schubert     /* N is ignored in this case */
646*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_generate_private_key(ctx, params, 0,
647*e0c4386eSCy Schubert                                                  ossl_ifc_ffc_compute_security_bits(BN_num_bits(params->p)),
648*e0c4386eSCy Schubert                                                  priv)))
649*e0c4386eSCy Schubert         goto err;
650*e0c4386eSCy Schubert     if (!TEST_int_le(BN_num_bits(priv), 225))
651*e0c4386eSCy Schubert         goto err;
652*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
653*e0c4386eSCy Schubert         goto err;
654*e0c4386eSCy Schubert 
655*e0c4386eSCy Schubert     ret = 1;
656*e0c4386eSCy Schubert err:
657*e0c4386eSCy Schubert     DH_free(dh);
658*e0c4386eSCy Schubert     BN_free(priv);
659*e0c4386eSCy Schubert     BN_CTX_free(ctx);
660*e0c4386eSCy Schubert     return ret;
661*e0c4386eSCy Schubert }
662*e0c4386eSCy Schubert 
ffc_params_copy_test(void)663*e0c4386eSCy Schubert static int ffc_params_copy_test(void)
664*e0c4386eSCy Schubert {
665*e0c4386eSCy Schubert     int ret = 0;
666*e0c4386eSCy Schubert     DH *dh = NULL;
667*e0c4386eSCy Schubert     FFC_PARAMS *params, copy;
668*e0c4386eSCy Schubert 
669*e0c4386eSCy Schubert     ossl_ffc_params_init(&copy);
670*e0c4386eSCy Schubert 
671*e0c4386eSCy Schubert     if (!TEST_ptr(dh = DH_new_by_nid(NID_ffdhe3072)))
672*e0c4386eSCy Schubert         goto err;
673*e0c4386eSCy Schubert     params = ossl_dh_get0_params(dh);
674*e0c4386eSCy Schubert 
675*e0c4386eSCy Schubert     if (!TEST_int_eq(params->keylength, 275))
676*e0c4386eSCy Schubert         goto err;
677*e0c4386eSCy Schubert 
678*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_copy(&copy, params)))
679*e0c4386eSCy Schubert         goto err;
680*e0c4386eSCy Schubert 
681*e0c4386eSCy Schubert     if (!TEST_int_eq(copy.keylength, 275))
682*e0c4386eSCy Schubert         goto err;
683*e0c4386eSCy Schubert 
684*e0c4386eSCy Schubert     if (!TEST_true(ossl_ffc_params_cmp(&copy, params, 0)))
685*e0c4386eSCy Schubert         goto err;
686*e0c4386eSCy Schubert 
687*e0c4386eSCy Schubert     ret = 1;
688*e0c4386eSCy Schubert err:
689*e0c4386eSCy Schubert     ossl_ffc_params_cleanup(&copy);
690*e0c4386eSCy Schubert     DH_free(dh);
691*e0c4386eSCy Schubert     return ret;
692*e0c4386eSCy Schubert }
693*e0c4386eSCy Schubert #endif /* OPENSSL_NO_DH */
694*e0c4386eSCy Schubert 
setup_tests(void)695*e0c4386eSCy Schubert int setup_tests(void)
696*e0c4386eSCy Schubert {
697*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DSA
698*e0c4386eSCy Schubert     ADD_TEST(ffc_params_validate_pq_test);
699*e0c4386eSCy Schubert     ADD_TEST(ffc_params_validate_g_unverified_test);
700*e0c4386eSCy Schubert #endif /* OPENSSL_NO_DSA */
701*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DH
702*e0c4386eSCy Schubert     ADD_TEST(ffc_params_gen_test);
703*e0c4386eSCy Schubert     ADD_TEST(ffc_params_gen_canonicalg_test);
704*e0c4386eSCy Schubert     ADD_TEST(ffc_params_fips186_2_gen_validate_test);
705*e0c4386eSCy Schubert     ADD_TEST(ffc_public_validate_test);
706*e0c4386eSCy Schubert     ADD_TEST(ffc_private_validate_test);
707*e0c4386eSCy Schubert     ADD_ALL_TESTS(ffc_private_gen_test, 10);
708*e0c4386eSCy Schubert     ADD_TEST(ffc_params_copy_test);
709*e0c4386eSCy Schubert #endif /* OPENSSL_NO_DH */
710*e0c4386eSCy Schubert     return 1;
711*e0c4386eSCy Schubert }
712