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