Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /* We need to use some deprecated APIs */
     11      1.1  christos #define OPENSSL_SUPPRESS_DEPRECATED
     12      1.1  christos 
     13      1.1  christos #include <stdio.h>
     14      1.1  christos #include <stdlib.h>
     15      1.1  christos #include <string.h>
     16      1.1  christos #include <openssl/bio.h>
     17      1.1  christos #include <openssl/conf.h>
     18      1.1  christos #include <openssl/crypto.h>
     19      1.1  christos #include <openssl/err.h>
     20      1.1  christos #include <openssl/evp.h>
     21      1.1  christos #include <openssl/x509.h>
     22      1.1  christos #include <openssl/pem.h>
     23      1.1  christos #include <openssl/kdf.h>
     24      1.1  christos #include <openssl/provider.h>
     25      1.1  christos #include <openssl/core_names.h>
     26      1.1  christos #include <openssl/params.h>
     27      1.1  christos #include <openssl/param_build.h>
     28      1.1  christos #include <openssl/dsa.h>
     29      1.1  christos #include <openssl/dh.h>
     30      1.1  christos #include <openssl/aes.h>
     31      1.1  christos #include <openssl/decoder.h>
     32      1.1  christos #include <openssl/rsa.h>
     33      1.1  christos #include <openssl/engine.h>
     34      1.1  christos #include <openssl/proverr.h>
     35      1.1  christos #include <openssl/rand.h>
     36  1.1.1.2  christos #include <crypto/ml_kem.h>
     37      1.1  christos #include "testutil.h"
     38      1.1  christos #include "internal/nelem.h"
     39      1.1  christos #include "internal/sizes.h"
     40      1.1  christos #include "crypto/evp.h"
     41      1.1  christos #include "fake_rsaprov.h"
     42      1.1  christos #include "fake_pipelineprov.h"
     43      1.1  christos 
     44      1.1  christos #ifdef STATIC_LEGACY
     45      1.1  christos OSSL_provider_init_fn ossl_legacy_provider_init;
     46      1.1  christos #endif
     47      1.1  christos 
     48      1.1  christos static OSSL_LIB_CTX *testctx = NULL;
     49      1.1  christos static char *testpropq = NULL;
     50      1.1  christos 
     51      1.1  christos static OSSL_PROVIDER *nullprov = NULL;
     52      1.1  christos static OSSL_PROVIDER *deflprov = NULL;
     53      1.1  christos static OSSL_PROVIDER *lgcyprov = NULL;
     54      1.1  christos 
     55      1.1  christos /*
     56      1.1  christos  * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
     57      1.1  christos  * should never use this key anywhere but in an example.
     58      1.1  christos  */
     59      1.1  christos static const unsigned char kExampleRSAKeyDER[] = {
     60      1.1  christos     0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
     61      1.1  christos     0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
     62      1.1  christos     0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
     63      1.1  christos     0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
     64      1.1  christos     0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
     65      1.1  christos     0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
     66      1.1  christos     0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
     67      1.1  christos     0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
     68      1.1  christos     0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
     69      1.1  christos     0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
     70      1.1  christos     0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
     71      1.1  christos     0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
     72      1.1  christos     0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
     73      1.1  christos     0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
     74      1.1  christos     0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
     75      1.1  christos     0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
     76      1.1  christos     0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
     77      1.1  christos     0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
     78      1.1  christos     0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
     79      1.1  christos     0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
     80      1.1  christos     0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
     81      1.1  christos     0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
     82      1.1  christos     0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
     83      1.1  christos     0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
     84      1.1  christos     0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
     85      1.1  christos     0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
     86      1.1  christos     0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
     87      1.1  christos     0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
     88      1.1  christos     0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
     89      1.1  christos     0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
     90      1.1  christos     0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
     91      1.1  christos     0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
     92      1.1  christos     0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
     93      1.1  christos     0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
     94      1.1  christos     0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
     95      1.1  christos     0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
     96      1.1  christos     0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
     97      1.1  christos     0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
     98      1.1  christos     0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
     99      1.1  christos     0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
    100      1.1  christos     0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
    101      1.1  christos     0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
    102      1.1  christos     0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
    103      1.1  christos     0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
    104      1.1  christos     0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
    105      1.1  christos     0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
    106      1.1  christos     0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
    107      1.1  christos     0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
    108      1.1  christos     0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
    109      1.1  christos     0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
    110  1.1.1.2  christos     0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf
    111      1.1  christos };
    112      1.1  christos 
    113      1.1  christos /* An invalid key whose prime factors exceed modulus n. */
    114      1.1  christos static const unsigned char kInvalidRSAKeyDER[] = {
    115      1.1  christos     0x30, 0x80, 0x02, 0x00, 0x02, 0x02, 0xb6, 0x00, 0x02, 0x02, 0x04, 0x80,
    116      1.1  christos     0x02, 0x00, 0x02, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00,
    117      1.1  christos     0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    118      1.1  christos     0x02, 0x00, 0x00, 0x00, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    119      1.1  christos     0xff, 0x01, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
    120      1.1  christos     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
    121      1.1  christos     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xaa, 0xaa, 0xaa, 0xaa,
    122      1.1  christos     0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    123      1.1  christos     0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x2b,
    124      1.1  christos     0x31, 0xff, 0x44, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    125      1.1  christos     0xaa, 0xaa, 0xaa, 0x29, 0xaa, 0xaa, 0xaa, 0xd9, 0xd9, 0xbf, 0x02, 0x01,
    126      1.1  christos     0xc8, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0xee,
    127      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    128      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15, 0x15,
    129      1.1  christos     0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x15, 0x07,
    130      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x29, 0x0f, 0x07, 0x07, 0x4d, 0x00, 0x07, 0x07,
    131      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    132      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x14, 0x15, 0x15,
    133      1.1  christos     0xec, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    134      1.1  christos     0x15, 0xe6, 0x15, 0x15, 0x15, 0x15, 0xff, 0x03, 0x00, 0x0a, 0x00, 0x00,
    135      1.1  christos     0x00, 0x00, 0x55, 0x15, 0x15, 0x15, 0x15, 0x11, 0x05, 0x15, 0x15, 0x15,
    136      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x4d, 0xf9, 0xf8, 0xf9,
    137      1.1  christos     0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0b, 0x07, 0x07, 0x07,
    138      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    139      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15, 0x15,
    140      1.1  christos     0x00, 0x02, 0x00, 0x6d, 0x61, 0x78, 0x00, 0x02, 0x00, 0x02, 0x15, 0x59,
    141      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    142      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    143      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    144      1.1  christos     0x59, 0x59, 0x51, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0xa5, 0x59, 0x59,
    145      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    146      1.1  christos     0x5d, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    147      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15,
    148      1.1  christos     0x15, 0x15, 0x00, 0x02, 0x01, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
    149      1.1  christos     0x00, 0x22, 0x00, 0x15, 0x15, 0x15, 0xeb, 0xea, 0xea, 0xea, 0xea, 0xea,
    150      1.1  christos     0xea, 0xf1, 0x15, 0x15, 0x15, 0x15, 0x15, 0x40, 0x55, 0x15, 0x15, 0x15,
    151      1.1  christos     0x15, 0x15, 0x05, 0x15, 0x15, 0x30, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07,
    152      1.1  christos     0x00, 0x00, 0x00, 0x4d, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    153      1.1  christos     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0x07, 0x07, 0x07, 0x07,
    154      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    155      1.1  christos     0x07, 0x07, 0x07, 0x15, 0x15, 0x15, 0x15, 0x00, 0x02, 0x00, 0x02, 0x00,
    156      1.1  christos     0x02, 0x00, 0x02, 0x00, 0x02, 0x15, 0x07, 0x07, 0x07, 0x07, 0x07, 0x29,
    157      1.1  christos     0x07, 0x07, 0x07, 0x4d, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    158      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    159      1.1  christos     0x07, 0x07, 0x07, 0x15, 0x14, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    160      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    161      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x55, 0x15, 0x59,
    162      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    163      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    164      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    165      1.1  christos     0x59, 0x59, 0x59, 0x02, 0x02, 0xb6, 0x00, 0x02, 0x02, 0x04, 0x80, 0x02,
    166      1.1  christos     0x00, 0x02, 0x82, 0x08, 0x01, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00,
    167      1.1  christos     0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
    168      1.1  christos     0x00, 0x00, 0x00, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    169      1.1  christos     0x01, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
    170      1.1  christos     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
    171      1.1  christos     0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    172      1.1  christos     0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    173      1.1  christos     0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0x2b, 0x31,
    174      1.1  christos     0xff, 0x44, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    175      1.1  christos     0xaa, 0xaa, 0x29, 0xaa, 0xaa, 0xaa, 0xd9, 0xd9, 0xbf, 0x02, 0x01, 0xc8,
    176      1.1  christos     0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x88, 0x88, 0x88, 0xee, 0x07,
    177      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    178      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15, 0x15, 0x00,
    179      1.1  christos     0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x15, 0x07, 0x07,
    180      1.1  christos     0x07, 0x07, 0x07, 0x29, 0x0f, 0x07, 0x07, 0x4d, 0x00, 0x07, 0x07, 0x07,
    181      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    182      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x14, 0x15, 0x15, 0xec,
    183      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    184      1.1  christos     0xe6, 0x15, 0x15, 0x15, 0x15, 0xff, 0x03, 0x00, 0x0a, 0x00, 0x00, 0x00,
    185      1.1  christos     0x00, 0x55, 0x15, 0x15, 0x15, 0x15, 0x11, 0x05, 0x15, 0x15, 0x15, 0x07,
    186      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x4d, 0xf9, 0xf8, 0xf9, 0x02,
    187      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0b, 0x07, 0x07, 0x07, 0x07,
    188      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    189      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15, 0x15, 0x00,
    190      1.1  christos     0x02, 0x00, 0x6d, 0x61, 0x78, 0x00, 0x02, 0x00, 0x02, 0x15, 0x59, 0x59,
    191      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    192      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    193      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    194      1.1  christos     0x59, 0x51, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0xa5, 0x59, 0x59, 0x59,
    195      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x5d,
    196      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    197      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15,
    198      1.1  christos     0x15, 0x00, 0x02, 0x01, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00,
    199      1.1  christos     0x22, 0x00, 0x15, 0x15, 0x15, 0xeb, 0xea, 0xea, 0xea, 0xea, 0xea, 0xea,
    200      1.1  christos     0xf1, 0x15, 0x15, 0x15, 0x15, 0x15, 0x40, 0x55, 0x15, 0x15, 0x15, 0x15,
    201      1.1  christos     0x15, 0x05, 0x15, 0x15, 0x30, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00,
    202      1.1  christos     0x00, 0x00, 0x4d, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xff,
    203      1.1  christos     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x11, 0x07, 0x07, 0x07, 0x07, 0x07,
    204      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    205      1.1  christos     0x07, 0x07, 0x15, 0x15, 0x15, 0x15, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02,
    206      1.1  christos     0x00, 0x02, 0x00, 0x02, 0x15, 0x07, 0x07, 0x07, 0x07, 0x07, 0x29, 0x07,
    207      1.1  christos     0x07, 0x07, 0x4d, 0x00, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    208      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    209      1.1  christos     0x07, 0x07, 0x15, 0x14, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    210      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    211      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x55, 0x15, 0x59, 0x59,
    212      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    213      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    214      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    215      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    216      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    217      1.1  christos     0x59, 0x59, 0x59, 0x06, 0xce, 0x15, 0x00, 0xfe, 0xf7, 0x52, 0x53, 0x41,
    218      1.1  christos     0x31, 0x01, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    219      1.1  christos     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    220      1.1  christos     0xff, 0xff, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    221      1.1  christos     0x2b, 0x31, 0xff, 0x44, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    222      1.1  christos     0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xd9, 0xd9, 0xbf, 0x02,
    223      1.1  christos     0x01, 0xc8, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x88, 0x88, 0x88,
    224      1.1  christos     0xee, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    225      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15,
    226      1.1  christos     0x15, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x15,
    227      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x29, 0x07, 0x07, 0x07, 0x4d, 0x00, 0x07,
    228      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    229      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x14, 0x15,
    230      1.1  christos     0x15, 0xec, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    231      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    232      1.1  christos     0x15, 0x15, 0x15, 0x55, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, 0x15, 0x15,
    233      1.1  christos     0x15, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00, 0x00, 0x4d, 0x07, 0x07,
    234      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x0b, 0x07, 0x07,
    235      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    236      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x15, 0x15, 0x15,
    237      1.1  christos     0x15, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x15,
    238      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59,
    239      1.1  christos     0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x59, 0x8f,
    240      1.1  christos     0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f,
    241      1.1  christos     0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f,
    242      1.1  christos     0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f, 0x8f,
    243      1.1  christos     0x8f, 0x8f, 0x8f, 0x8f, 0x59, 0x59, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00,
    244      1.1  christos     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x3d, 0xc1, 0xc1,
    245      1.1  christos     0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1, 0xc1,
    246      1.1  christos     0xc1, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    247      1.1  christos     0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    248      1.1  christos     0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x42, 0xa5, 0x02, 0x02, 0x42, 0x02,
    249      1.1  christos     0x02, 0x51, 0x01, 0x02, 0x02, 0xd2, 0x42, 0x02, 0xe8, 0xe8, 0xe8, 0xe8,
    250      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8,
    251      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8,
    252      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8,
    253      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8,
    254      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8,
    255      1.1  christos     0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0xe8, 0x02,
    256      1.1  christos     0x02, 0x42, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    257      1.1  christos     0x21, 0x2b, 0x02, 0x02, 0x02, 0x02, 0x02, 0x21, 0x02, 0x02, 0x32, 0x80,
    258      1.1  christos     0x02, 0x02, 0x7f, 0x1b, 0x02, 0x00, 0x1f, 0x04, 0xff, 0x80, 0x02, 0x02,
    259      1.1  christos     0x02, 0x02, 0x42, 0x02, 0x12, 0x02, 0x42, 0x02, 0x79, 0x70, 0x65, 0x36,
    260      1.1  christos     0x28, 0xc8, 0x02, 0x01, 0x81, 0x08, 0xfe, 0x00, 0xf9, 0x02, 0x42, 0x10,
    261      1.1  christos     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
    262      1.1  christos     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
    263      1.1  christos     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
    264      1.1  christos     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
    265      1.1  christos     0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xa5, 0x02,
    266      1.1  christos     0x02, 0x42, 0x02, 0x02, 0x51, 0x01, 0x02, 0x02, 0xd2, 0x42, 0x02, 0x02,
    267      1.1  christos     0x02, 0x42, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
    268      1.1  christos     0x21, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x32, 0x80, 0x02, 0x02,
    269      1.1  christos     0x7f, 0x1b, 0x02, 0x00, 0x1f, 0x04, 0xff, 0x80, 0x02, 0x02, 0x02, 0x02,
    270      1.1  christos     0x42, 0x02, 0x12, 0x02, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    271      1.1  christos     0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x47,
    272      1.1  christos     0x4f, 0x53, 0x54, 0x20, 0x52, 0x20, 0x33, 0x34, 0x2e, 0x31, 0x31, 0x2d,
    273      1.1  christos     0x32, 0x30, 0x31, 0x32, 0x20, 0x77, 0x69, 0x74, 0x68, 0x30, 0x80, 0x60,
    274      1.1  christos     0x02, 0x82, 0x24, 0x02, 0x02, 0x41, 0x52, 0x49, 0x41, 0x2d, 0x31, 0x32,
    275      1.1  christos     0x38, 0x2d, 0x43, 0x46, 0x42, 0x38, 0xff, 0xff, 0xff, 0x3a, 0x5b, 0xff,
    276      1.1  christos     0xff, 0x7f, 0x49, 0x74, 0x84, 0x00, 0x00, 0x70, 0x65, 0x00, 0x00, 0x30,
    277      1.1  christos     0x80, 0x60, 0x02, 0x82, 0x24, 0x02, 0x02, 0x41, 0x52, 0x49, 0x41, 0x2d,
    278      1.1  christos     0x31, 0x32, 0x38, 0x2d, 0x43, 0x46, 0x42, 0x38, 0xff, 0xff, 0xff, 0x3a,
    279      1.1  christos     0x5b, 0xff, 0xff, 0x7f, 0x49, 0x74, 0x84, 0x00, 0x00, 0x70, 0x65, 0x33,
    280      1.1  christos     0x28, 0xc8, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15,
    281      1.1  christos     0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x55, 0x15, 0x15, 0x15, 0x15,
    282      1.1  christos     0x15, 0x05, 0x15, 0x95, 0x15, 0x07, 0x07, 0x07, 0x07, 0x07, 0x00, 0x00,
    283      1.1  christos     0x00, 0x4d, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    284      1.1  christos     0x07, 0x0b, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    285      1.1  christos     0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
    286      1.1  christos     0x07, 0x15, 0x15, 0x15, 0x15, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00,
    287      1.1  christos     0x02, 0x00, 0x25, 0x02, 0x02, 0x22, 0x3a, 0x02, 0x02, 0x02, 0x42, 0x02,
    288      1.1  christos     0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
    289  1.1.1.2  christos     0xe2, 0x80
    290      1.1  christos };
    291      1.1  christos 
    292      1.1  christos /*
    293      1.1  christos  * kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
    294      1.1  christos  * should never use this key anywhere but in an example.
    295      1.1  christos  */
    296      1.1  christos #ifndef OPENSSL_NO_DSA
    297      1.1  christos static const unsigned char kExampleDSAKeyDER[] = {
    298      1.1  christos     0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
    299      1.1  christos     0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
    300      1.1  christos     0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
    301      1.1  christos     0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
    302      1.1  christos     0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
    303      1.1  christos     0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
    304      1.1  christos     0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
    305      1.1  christos     0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
    306      1.1  christos     0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
    307      1.1  christos     0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
    308      1.1  christos     0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
    309      1.1  christos     0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
    310      1.1  christos     0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
    311      1.1  christos     0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
    312      1.1  christos     0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
    313      1.1  christos     0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
    314      1.1  christos     0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
    315      1.1  christos     0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
    316      1.1  christos     0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
    317      1.1  christos     0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
    318      1.1  christos     0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
    319      1.1  christos     0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
    320      1.1  christos     0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
    321      1.1  christos     0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
    322      1.1  christos     0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
    323      1.1  christos     0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
    324      1.1  christos     0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
    325      1.1  christos     0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
    326      1.1  christos     0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
    327      1.1  christos     0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
    328      1.1  christos     0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
    329      1.1  christos     0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
    330      1.1  christos     0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
    331      1.1  christos     0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
    332      1.1  christos     0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
    333      1.1  christos     0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
    334      1.1  christos     0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
    335      1.1  christos     0x40, 0x48
    336      1.1  christos };
    337      1.1  christos #endif
    338      1.1  christos 
    339      1.1  christos /*
    340      1.1  christos  * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
    341      1.1  christos  * components are not correct.
    342      1.1  christos  */
    343      1.1  christos static const unsigned char kExampleBadRSAKeyDER[] = {
    344      1.1  christos     0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
    345      1.1  christos     0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
    346      1.1  christos     0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
    347      1.1  christos     0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
    348      1.1  christos     0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
    349      1.1  christos     0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
    350      1.1  christos     0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
    351      1.1  christos     0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
    352      1.1  christos     0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
    353      1.1  christos     0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
    354      1.1  christos     0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
    355      1.1  christos     0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
    356      1.1  christos     0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
    357      1.1  christos     0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
    358      1.1  christos     0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
    359      1.1  christos     0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
    360      1.1  christos     0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
    361      1.1  christos     0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
    362      1.1  christos     0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
    363      1.1  christos     0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
    364      1.1  christos     0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
    365      1.1  christos     0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
    366      1.1  christos     0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
    367      1.1  christos     0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
    368      1.1  christos     0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
    369      1.1  christos     0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
    370      1.1  christos     0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
    371      1.1  christos     0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
    372      1.1  christos     0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
    373      1.1  christos     0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
    374      1.1  christos     0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
    375      1.1  christos     0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
    376      1.1  christos     0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
    377      1.1  christos     0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
    378      1.1  christos     0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
    379      1.1  christos     0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
    380      1.1  christos     0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
    381      1.1  christos     0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
    382      1.1  christos     0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
    383      1.1  christos     0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
    384      1.1  christos     0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
    385      1.1  christos     0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
    386      1.1  christos     0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
    387      1.1  christos     0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
    388      1.1  christos     0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
    389      1.1  christos     0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
    390      1.1  christos     0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
    391      1.1  christos     0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
    392      1.1  christos     0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
    393      1.1  christos     0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
    394      1.1  christos     0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
    395      1.1  christos     0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
    396      1.1  christos     0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
    397      1.1  christos     0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
    398      1.1  christos     0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
    399      1.1  christos     0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
    400      1.1  christos     0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
    401      1.1  christos     0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
    402      1.1  christos     0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
    403      1.1  christos     0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
    404      1.1  christos     0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
    405      1.1  christos     0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
    406      1.1  christos     0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
    407      1.1  christos     0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
    408      1.1  christos     0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
    409      1.1  christos     0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
    410      1.1  christos     0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
    411      1.1  christos     0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
    412      1.1  christos     0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
    413      1.1  christos     0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
    414      1.1  christos     0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
    415      1.1  christos     0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
    416      1.1  christos     0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
    417      1.1  christos     0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
    418      1.1  christos     0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
    419      1.1  christos     0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
    420      1.1  christos     0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
    421      1.1  christos     0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
    422      1.1  christos     0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
    423      1.1  christos     0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
    424      1.1  christos     0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
    425      1.1  christos     0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
    426      1.1  christos     0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
    427      1.1  christos     0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
    428      1.1  christos     0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
    429      1.1  christos     0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
    430      1.1  christos     0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
    431      1.1  christos     0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
    432  1.1.1.2  christos     0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce
    433      1.1  christos };
    434      1.1  christos 
    435      1.1  christos /*
    436      1.1  christos  * kExampleBad2RSAKeyDER is an RSA private key in ASN.1, DER format. All
    437      1.1  christos  * values are 0.
    438      1.1  christos  */
    439      1.1  christos static const unsigned char kExampleBad2RSAKeyDER[] = {
    440      1.1  christos     0x30, 0x1b, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
    441      1.1  christos     0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
    442      1.1  christos     0x01, 0x00, 0x02, 0x01, 0x00
    443      1.1  christos };
    444      1.1  christos 
    445      1.1  christos static const unsigned char kMsg[] = { 1, 2, 3, 4 };
    446      1.1  christos 
    447      1.1  christos static const unsigned char kSignature[] = {
    448      1.1  christos     0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
    449      1.1  christos     0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
    450      1.1  christos     0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
    451      1.1  christos     0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
    452      1.1  christos     0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
    453      1.1  christos     0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
    454      1.1  christos     0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
    455      1.1  christos     0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
    456      1.1  christos     0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
    457      1.1  christos     0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
    458  1.1.1.2  christos     0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42
    459      1.1  christos };
    460      1.1  christos 
    461      1.1  christos /*
    462      1.1  christos  * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS#8 v1
    463      1.1  christos  * PrivateKeyInfo.
    464      1.1  christos  */
    465      1.1  christos static const unsigned char kExampleRSAKeyPKCS8[] = {
    466      1.1  christos     0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
    467      1.1  christos     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
    468      1.1  christos     0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
    469      1.1  christos     0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
    470      1.1  christos     0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
    471      1.1  christos     0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
    472      1.1  christos     0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
    473      1.1  christos     0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
    474      1.1  christos     0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
    475      1.1  christos     0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
    476      1.1  christos     0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
    477      1.1  christos     0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
    478      1.1  christos     0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
    479      1.1  christos     0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
    480      1.1  christos     0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
    481      1.1  christos     0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
    482      1.1  christos     0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
    483      1.1  christos     0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
    484      1.1  christos     0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
    485      1.1  christos     0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
    486      1.1  christos     0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
    487      1.1  christos     0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
    488      1.1  christos     0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
    489      1.1  christos     0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
    490      1.1  christos     0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
    491      1.1  christos     0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
    492      1.1  christos     0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
    493      1.1  christos     0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
    494      1.1  christos     0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
    495      1.1  christos     0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
    496      1.1  christos     0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
    497      1.1  christos     0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
    498      1.1  christos     0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
    499      1.1  christos     0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
    500      1.1  christos     0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
    501      1.1  christos     0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
    502      1.1  christos     0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
    503      1.1  christos     0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
    504      1.1  christos     0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
    505      1.1  christos     0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
    506      1.1  christos     0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
    507      1.1  christos     0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
    508      1.1  christos     0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
    509      1.1  christos     0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
    510      1.1  christos     0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
    511      1.1  christos     0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
    512      1.1  christos     0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
    513      1.1  christos     0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
    514      1.1  christos     0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
    515      1.1  christos     0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
    516      1.1  christos     0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
    517      1.1  christos     0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
    518  1.1.1.2  christos     0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf
    519      1.1  christos };
    520      1.1  christos 
    521      1.1  christos /*
    522      1.1  christos  * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS#8 v2
    523      1.1  christos  * PrivateKeyInfo (with an optional public key).
    524      1.1  christos  */
    525      1.1  christos static const unsigned char kExampleRSAKeyPKCS8_v2[] = {
    526      1.1  christos     0x30, 0x82, 0x03, 0x06, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a,
    527      1.1  christos     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
    528      1.1  christos     0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
    529      1.1  christos     0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
    530      1.1  christos     0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
    531      1.1  christos     0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
    532      1.1  christos     0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
    533      1.1  christos     0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
    534      1.1  christos     0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
    535      1.1  christos     0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
    536      1.1  christos     0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
    537      1.1  christos     0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
    538      1.1  christos     0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
    539      1.1  christos     0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
    540      1.1  christos     0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
    541      1.1  christos     0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
    542      1.1  christos     0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
    543      1.1  christos     0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
    544      1.1  christos     0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
    545      1.1  christos     0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
    546      1.1  christos     0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
    547      1.1  christos     0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
    548      1.1  christos     0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
    549      1.1  christos     0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
    550      1.1  christos     0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
    551      1.1  christos     0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
    552      1.1  christos     0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
    553      1.1  christos     0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
    554      1.1  christos     0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
    555      1.1  christos     0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
    556      1.1  christos     0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
    557      1.1  christos     0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
    558      1.1  christos     0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
    559      1.1  christos     0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
    560      1.1  christos     0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
    561      1.1  christos     0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
    562      1.1  christos     0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
    563      1.1  christos     0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
    564      1.1  christos     0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
    565      1.1  christos     0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
    566      1.1  christos     0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
    567      1.1  christos     0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
    568      1.1  christos     0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
    569      1.1  christos     0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
    570      1.1  christos     0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
    571      1.1  christos     0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
    572      1.1  christos     0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
    573      1.1  christos     0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
    574      1.1  christos     0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
    575      1.1  christos     0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
    576      1.1  christos     0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
    577      1.1  christos     0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
    578      1.1  christos     0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
    579      1.1  christos     /* Implicit optional Public key BIT STRING */
    580  1.1.1.2  christos     0x81,
    581  1.1.1.2  christos     0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xf8,
    582      1.1  christos     0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
    583      1.1  christos     0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
    584      1.1  christos     0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
    585      1.1  christos     0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
    586      1.1  christos     0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
    587      1.1  christos     0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
    588      1.1  christos     0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
    589      1.1  christos     0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
    590      1.1  christos     0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
    591      1.1  christos     0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
    592      1.1  christos     0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01
    593      1.1  christos };
    594      1.1  christos 
    595      1.1  christos #ifndef OPENSSL_NO_EC
    596      1.1  christos /*
    597      1.1  christos  * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
    598      1.1  christos  * structure.
    599      1.1  christos  */
    600      1.1  christos static const unsigned char kExampleECKeyDER[] = {
    601      1.1  christos     0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
    602      1.1  christos     0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
    603      1.1  christos     0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
    604      1.1  christos     0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
    605      1.1  christos     0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
    606      1.1  christos     0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
    607      1.1  christos     0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
    608      1.1  christos     0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
    609      1.1  christos     0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
    610      1.1  christos     0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
    611  1.1.1.2  christos     0xc1
    612      1.1  christos };
    613      1.1  christos 
    614      1.1  christos static const unsigned char kExampleECKeyPKCS8_v2[] = {
    615      1.1  christos     0x30, 0x81, 0xcb, 0x02, 0x01, 0x01, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
    616      1.1  christos     0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
    617      1.1  christos     0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
    618      1.1  christos     0x07, 0x0f, 0x08, 0x72, 0x7a, 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9,
    619      1.1  christos     0x4d, 0x89, 0x68, 0x77, 0x08, 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e,
    620      1.1  christos     0xe8, 0xd1, 0xc9, 0xce, 0x0a, 0x8b, 0xb4, 0x6a, 0xa1, 0x44, 0x03, 0x42,
    621      1.1  christos     0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f,
    622      1.1  christos     0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d,
    623      1.1  christos     0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7,
    624      1.1  christos     0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2,
    625      1.1  christos     0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94,
    626      1.1  christos     0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1,
    627      1.1  christos     /* Optional implicit public key BIT STRING */
    628  1.1.1.2  christos     0x81,
    629  1.1.1.2  christos     0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69, 0xe2, 0xbf, 0x65, 0x9f, 0x97,
    630      1.1  christos     0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c, 0xd5, 0x97, 0x6b, 0xb7, 0xa9,
    631      1.1  christos     0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a,
    632      1.1  christos     0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e,
    633      1.1  christos     0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e,
    634      1.1  christos     0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1
    635      1.1  christos };
    636      1.1  christos 
    637      1.1  christos /*
    638      1.1  christos  * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
    639      1.1  christos  * structure. The private key is equal to the order and will fail to import
    640      1.1  christos  */
    641      1.1  christos static const unsigned char kExampleBadECKeyDER[] = {
    642      1.1  christos     0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
    643      1.1  christos     0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
    644      1.1  christos     0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
    645      1.1  christos     0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    646      1.1  christos     0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
    647      1.1  christos     0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
    648      1.1  christos     0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    649      1.1  christos     0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
    650      1.1  christos     0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
    651      1.1  christos };
    652      1.1  christos 
    653      1.1  christos /* prime256v1 */
    654      1.1  christos static const unsigned char kExampleECPubKeyDER[] = {
    655      1.1  christos     0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
    656      1.1  christos     0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
    657      1.1  christos     0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
    658      1.1  christos     0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
    659      1.1  christos     0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
    660      1.1  christos     0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
    661      1.1  christos     0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
    662      1.1  christos     0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
    663      1.1  christos };
    664      1.1  christos 
    665      1.1  christos /*
    666      1.1  christos  * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
    667      1.1  christos  * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
    668      1.1  christos  */
    669      1.1  christos static const unsigned char kExampleBadECPubKeyDER[] = {
    670      1.1  christos     0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
    671      1.1  christos     0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
    672      1.1  christos     0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
    673      1.1  christos     0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
    674      1.1  christos     0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
    675      1.1  christos     0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
    676      1.1  christos     0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
    677      1.1  christos     0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
    678      1.1  christos };
    679      1.1  christos 
    680      1.1  christos static const unsigned char pExampleECParamDER[] = {
    681      1.1  christos     0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
    682      1.1  christos };
    683      1.1  christos 
    684  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
    685      1.1  christos static const unsigned char kExampleED25519KeyDER[] = {
    686      1.1  christos     0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
    687      1.1  christos     0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a,
    688      1.1  christos     0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46,
    689      1.1  christos     0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50
    690      1.1  christos };
    691      1.1  christos 
    692      1.1  christos static const unsigned char kExampleED25519PubKeyDER[] = {
    693      1.1  christos     0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
    694      1.1  christos     0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45,
    695      1.1  christos     0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40,
    696      1.1  christos     0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29
    697      1.1  christos };
    698      1.1  christos 
    699  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
    700      1.1  christos static const unsigned char kExampleX25519KeyDER[] = {
    701      1.1  christos     0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
    702      1.1  christos     0x04, 0x22, 0x04, 0x20, 0xa0, 0x24, 0x3a, 0x31, 0x24, 0xc3, 0x3f, 0xf6,
    703      1.1  christos     0x7b, 0x96, 0x0b, 0xd4, 0x8f, 0xd1, 0xee, 0x67, 0xf2, 0x9b, 0x88, 0xac,
    704      1.1  christos     0x50, 0xce, 0x97, 0x36, 0xdd, 0xaf, 0x25, 0xf6, 0x10, 0x34, 0x96, 0x6e
    705      1.1  christos };
    706  1.1.1.2  christos #endif
    707  1.1.1.2  christos #endif
    708      1.1  christos #endif
    709      1.1  christos 
    710      1.1  christos /* kExampleDHKeyDER is a DH private key in ASN.1, DER format. */
    711      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
    712  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    713      1.1  christos static const unsigned char kExampleDHKeyDER[] = {
    714      1.1  christos     0x30, 0x82, 0x01, 0x21, 0x02, 0x01, 0x00, 0x30, 0x81, 0x95, 0x06, 0x09,
    715      1.1  christos     0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x81, 0x87,
    716      1.1  christos     0x02, 0x81, 0x81, 0x00, 0xf7, 0x52, 0xc2, 0x68, 0xcc, 0x66, 0xc4, 0x8d,
    717      1.1  christos     0x03, 0x3f, 0xfa, 0x9c, 0x52, 0xd0, 0xd8, 0x33, 0xf2, 0xe1, 0xc9, 0x9e,
    718      1.1  christos     0xb7, 0xe7, 0x6e, 0x90, 0x97, 0xeb, 0x92, 0x91, 0x6a, 0x9a, 0x85, 0x63,
    719      1.1  christos     0x92, 0x79, 0xab, 0xb6, 0x3d, 0x23, 0x58, 0x5a, 0xe8, 0x45, 0x06, 0x81,
    720      1.1  christos     0x97, 0x77, 0xe1, 0xcc, 0x34, 0x4e, 0xae, 0x36, 0x80, 0xf2, 0xc4, 0x7f,
    721      1.1  christos     0x8a, 0x52, 0xb8, 0xdb, 0x58, 0xc8, 0x4b, 0x12, 0x4c, 0xf1, 0x4c, 0x53,
    722      1.1  christos     0xc1, 0x89, 0x39, 0x8d, 0xb6, 0x06, 0xd8, 0xea, 0x7f, 0x2d, 0x36, 0x53,
    723      1.1  christos     0x96, 0x29, 0xbe, 0xb6, 0x75, 0xfc, 0xe7, 0xf3, 0x36, 0xd6, 0xf4, 0x8f,
    724      1.1  christos     0x16, 0xa6, 0xc7, 0xec, 0x7b, 0xce, 0x42, 0x8d, 0x48, 0x2e, 0xb7, 0x74,
    725      1.1  christos     0x00, 0x11, 0x52, 0x61, 0xb4, 0x19, 0x35, 0xec, 0x5c, 0xe4, 0xbe, 0x34,
    726      1.1  christos     0xc6, 0x59, 0x64, 0x5e, 0x42, 0x61, 0x70, 0x54, 0xf4, 0xe9, 0x6b, 0x53,
    727      1.1  christos     0x02, 0x01, 0x02, 0x04, 0x81, 0x83, 0x02, 0x81, 0x80, 0x64, 0xc2, 0xe3,
    728      1.1  christos     0x09, 0x69, 0x37, 0x3c, 0xd2, 0x4a, 0xba, 0xc3, 0x78, 0x6a, 0x9b, 0x8a,
    729      1.1  christos     0x2a, 0xdb, 0xe7, 0xe6, 0xc0, 0xfa, 0x3a, 0xbe, 0x39, 0x67, 0xc0, 0xa9,
    730      1.1  christos     0x2a, 0xf0, 0x0a, 0xc1, 0x53, 0x1c, 0xdb, 0xfa, 0x1a, 0x26, 0x98, 0xb0,
    731      1.1  christos     0x8c, 0xc6, 0x06, 0x4a, 0xa2, 0x48, 0xd3, 0xa4, 0x3b, 0xbd, 0x05, 0x48,
    732      1.1  christos     0xea, 0x59, 0xdb, 0x18, 0xa4, 0xca, 0x66, 0xd9, 0x5d, 0xb8, 0x95, 0xd1,
    733      1.1  christos     0xeb, 0x97, 0x3d, 0x66, 0x97, 0x5c, 0x86, 0x8f, 0x7e, 0x90, 0xd3, 0x43,
    734      1.1  christos     0xd1, 0xa2, 0x0d, 0xcb, 0xe7, 0xeb, 0x90, 0xea, 0x09, 0x40, 0xb1, 0x6f,
    735      1.1  christos     0xf7, 0x4c, 0xf2, 0x41, 0x83, 0x1d, 0xd0, 0x76, 0xef, 0xaf, 0x55, 0x6f,
    736      1.1  christos     0x5d, 0xa9, 0xa3, 0x55, 0x81, 0x2a, 0xd1, 0x5d, 0x9d, 0x22, 0x77, 0x97,
    737      1.1  christos     0x83, 0xde, 0xad, 0xb6, 0x5d, 0x19, 0xc1, 0x53, 0xec, 0xfb, 0xaf, 0x06,
    738      1.1  christos     0x2e, 0x87, 0x2a, 0x0b, 0x7a
    739      1.1  christos };
    740  1.1.1.2  christos #endif
    741      1.1  christos #endif
    742      1.1  christos 
    743      1.1  christos static const unsigned char kCFBDefaultKey[] = {
    744      1.1  christos     0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,
    745      1.1  christos     0x09, 0xCF, 0x4F, 0x3C
    746      1.1  christos };
    747      1.1  christos 
    748      1.1  christos static const unsigned char kGCMDefaultKey[32] = { 0 };
    749      1.1  christos 
    750      1.1  christos static const unsigned char kGCMResetKey[] = {
    751      1.1  christos     0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
    752      1.1  christos     0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
    753      1.1  christos     0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
    754      1.1  christos };
    755      1.1  christos 
    756      1.1  christos static const unsigned char iCFBIV[] = {
    757      1.1  christos     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
    758      1.1  christos     0x0C, 0x0D, 0x0E, 0x0F
    759      1.1  christos };
    760      1.1  christos 
    761      1.1  christos static const unsigned char iGCMDefaultIV[12] = { 0 };
    762      1.1  christos 
    763      1.1  christos static const unsigned char iGCMResetIV1[] = {
    764      1.1  christos     0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
    765      1.1  christos };
    766      1.1  christos 
    767      1.1  christos static const unsigned char iGCMResetIV2[] = {
    768      1.1  christos     0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88
    769      1.1  christos };
    770      1.1  christos 
    771      1.1  christos static const unsigned char cfbPlaintext[] = {
    772      1.1  christos     0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
    773      1.1  christos     0x73, 0x93, 0x17, 0x2A
    774      1.1  christos };
    775      1.1  christos static const unsigned char cfbPlaintext_partial[] = {
    776      1.1  christos     0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
    777  1.1.1.2  christos     0x73, 0x93, 0x17, 0x2A, 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96
    778      1.1  christos };
    779      1.1  christos 
    780      1.1  christos static const unsigned char gcmDefaultPlaintext[16] = { 0 };
    781      1.1  christos 
    782      1.1  christos static const unsigned char gcmResetPlaintext[] = {
    783      1.1  christos     0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
    784      1.1  christos     0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
    785      1.1  christos     0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
    786      1.1  christos     0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
    787      1.1  christos     0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39
    788      1.1  christos };
    789      1.1  christos 
    790      1.1  christos static const unsigned char cfbCiphertext[] = {
    791      1.1  christos     0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
    792      1.1  christos     0xE8, 0x3C, 0xFB, 0x4A
    793      1.1  christos };
    794      1.1  christos 
    795      1.1  christos static const unsigned char cfbCiphertext_partial[] = {
    796      1.1  christos     0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
    797      1.1  christos     0xE8, 0x3C, 0xFB, 0x4A, 0x0D, 0x4A, 0x71, 0x82, 0x90, 0xF0, 0x9A, 0x35
    798      1.1  christos };
    799      1.1  christos 
    800      1.1  christos static const unsigned char ofbCiphertext_partial[] = {
    801      1.1  christos     0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
    802      1.1  christos     0xE8, 0x3C, 0xFB, 0x4A, 0xB2, 0x65, 0x64, 0x38, 0x26, 0xD2, 0xBC, 0x09
    803      1.1  christos };
    804      1.1  christos 
    805      1.1  christos static const unsigned char gcmDefaultCiphertext[] = {
    806      1.1  christos     0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
    807      1.1  christos     0xba, 0xf3, 0x9d, 0x18
    808      1.1  christos };
    809      1.1  christos 
    810      1.1  christos static const unsigned char gcmResetCiphertext1[] = {
    811      1.1  christos     0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b,
    812      1.1  christos     0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
    813      1.1  christos     0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3,
    814      1.1  christos     0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
    815      1.1  christos     0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f
    816      1.1  christos };
    817      1.1  christos 
    818      1.1  christos static const unsigned char gcmResetCiphertext2[] = {
    819      1.1  christos     0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3,
    820      1.1  christos     0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
    821      1.1  christos     0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48,
    822      1.1  christos     0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
    823      1.1  christos     0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62
    824      1.1  christos };
    825      1.1  christos 
    826      1.1  christos static const unsigned char gcmAAD[] = {
    827      1.1  christos     0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce,
    828      1.1  christos     0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2
    829      1.1  christos };
    830      1.1  christos 
    831      1.1  christos static const unsigned char gcmDefaultTag[] = {
    832      1.1  christos     0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5,
    833      1.1  christos     0xd4, 0x8a, 0xb9, 0x19
    834      1.1  christos };
    835      1.1  christos 
    836      1.1  christos static const unsigned char gcmResetTag1[] = {
    837      1.1  christos     0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13,
    838      1.1  christos     0xfe, 0x2e, 0xa8, 0xf2
    839      1.1  christos };
    840      1.1  christos 
    841      1.1  christos static const unsigned char gcmResetTag2[] = {
    842      1.1  christos     0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53,
    843      1.1  christos     0xbb, 0x2d, 0x55, 0x1b
    844      1.1  christos };
    845      1.1  christos 
    846      1.1  christos typedef struct APK_DATA_st {
    847      1.1  christos     const unsigned char *kder;
    848      1.1  christos     size_t size;
    849      1.1  christos     const char *keytype;
    850      1.1  christos     int evptype;
    851      1.1  christos     int check;
    852      1.1  christos     int pub_check;
    853      1.1  christos     int param_check;
    854      1.1  christos     int type; /* 0 for private, 1 for public, 2 for params */
    855      1.1  christos } APK_DATA;
    856      1.1  christos 
    857      1.1  christos static APK_DATA keydata[] = {
    858  1.1.1.2  christos     { kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA },
    859  1.1.1.2  christos     { kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA },
    860      1.1  christos #ifndef OPENSSL_NO_EC
    861  1.1.1.2  christos     { kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC }
    862      1.1  christos #endif
    863      1.1  christos };
    864      1.1  christos 
    865      1.1  christos static APK_DATA keydata_v2[] = {
    866  1.1.1.2  christos     { kExampleRSAKeyPKCS8_v2, sizeof(kExampleRSAKeyPKCS8_v2), "RSA", EVP_PKEY_RSA },
    867      1.1  christos #ifndef OPENSSL_NO_EC
    868  1.1.1.2  christos     { kExampleECKeyPKCS8_v2, sizeof(kExampleECKeyPKCS8_v2), "EC", EVP_PKEY_EC }
    869      1.1  christos #endif
    870      1.1  christos };
    871      1.1  christos 
    872      1.1  christos static APK_DATA keycheckdata[] = {
    873  1.1.1.2  christos     { kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1,
    874  1.1.1.2  christos         0 },
    875  1.1.1.2  christos     { kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA,
    876  1.1.1.2  christos         0, 1, 1, 0 },
    877  1.1.1.2  christos     { kExampleBad2RSAKeyDER, sizeof(kExampleBad2RSAKeyDER), "RSA", EVP_PKEY_RSA,
    878  1.1.1.2  christos         0, 0, 1 /* Since there are no "params" in an RSA key this passes */, 0 },
    879      1.1  christos #ifndef OPENSSL_NO_EC
    880  1.1.1.2  christos     { kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0 },
    881      1.1  christos     /* group is also associated in our pub key */
    882  1.1.1.2  christos     { kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1,
    883  1.1.1.2  christos         1, 1 },
    884  1.1.1.2  christos     { pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1,
    885  1.1.1.2  christos         2 },
    886  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
    887  1.1.1.2  christos     { kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519",
    888  1.1.1.2  christos         EVP_PKEY_ED25519, 1, 1, 1, 0 },
    889  1.1.1.2  christos     { kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519",
    890  1.1.1.2  christos         EVP_PKEY_ED25519, 0, 1, 1, 1 },
    891  1.1.1.2  christos #endif
    892      1.1  christos #endif
    893      1.1  christos };
    894      1.1  christos 
    895      1.1  christos static EVP_PKEY *load_example_key(const char *keytype,
    896  1.1.1.2  christos     const unsigned char *data, size_t data_len)
    897      1.1  christos {
    898      1.1  christos     const unsigned char **pdata = &data;
    899      1.1  christos     EVP_PKEY *pkey = NULL;
    900  1.1.1.2  christos     OSSL_DECODER_CTX *dctx = OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0,
    901  1.1.1.2  christos         testctx, testpropq);
    902      1.1  christos 
    903      1.1  christos     /* |pkey| will be NULL on error */
    904      1.1  christos     (void)OSSL_DECODER_from_data(dctx, pdata, &data_len);
    905      1.1  christos     OSSL_DECODER_CTX_free(dctx);
    906      1.1  christos     return pkey;
    907      1.1  christos }
    908      1.1  christos 
    909      1.1  christos static EVP_PKEY *load_example_rsa_key(void)
    910      1.1  christos {
    911      1.1  christos     return load_example_key("RSA", kExampleRSAKeyDER,
    912  1.1.1.2  christos         sizeof(kExampleRSAKeyDER));
    913      1.1  christos }
    914      1.1  christos 
    915      1.1  christos #ifndef OPENSSL_NO_DSA
    916      1.1  christos static EVP_PKEY *load_example_dsa_key(void)
    917      1.1  christos {
    918      1.1  christos     return load_example_key("DSA", kExampleDSAKeyDER,
    919  1.1.1.2  christos         sizeof(kExampleDSAKeyDER));
    920      1.1  christos }
    921      1.1  christos #endif
    922      1.1  christos 
    923      1.1  christos #ifndef OPENSSL_NO_EC
    924      1.1  christos static EVP_PKEY *load_example_ec_key(void)
    925      1.1  christos {
    926      1.1  christos     return load_example_key("EC", kExampleECKeyDER,
    927  1.1.1.2  christos         sizeof(kExampleECKeyDER));
    928      1.1  christos }
    929      1.1  christos #endif
    930      1.1  christos 
    931      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
    932  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    933      1.1  christos static EVP_PKEY *load_example_dh_key(void)
    934      1.1  christos {
    935      1.1  christos     return load_example_key("DH", kExampleDHKeyDER,
    936  1.1.1.2  christos         sizeof(kExampleDHKeyDER));
    937      1.1  christos }
    938  1.1.1.2  christos #endif
    939      1.1  christos 
    940  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
    941      1.1  christos static EVP_PKEY *load_example_ed25519_key(void)
    942      1.1  christos {
    943      1.1  christos     return load_example_key("ED25519", kExampleED25519KeyDER,
    944  1.1.1.2  christos         sizeof(kExampleED25519KeyDER));
    945      1.1  christos }
    946      1.1  christos 
    947      1.1  christos static EVP_PKEY *load_example_x25519_key(void)
    948      1.1  christos {
    949      1.1  christos     return load_example_key("X25519", kExampleX25519KeyDER,
    950  1.1.1.2  christos         sizeof(kExampleX25519KeyDER));
    951      1.1  christos }
    952  1.1.1.2  christos #endif
    953      1.1  christos #endif /* OPENSSL_NO_DEPRECATED_3_0 */
    954      1.1  christos 
    955      1.1  christos static EVP_PKEY *load_example_hmac_key(void)
    956      1.1  christos {
    957      1.1  christos     EVP_PKEY *pkey = NULL;
    958      1.1  christos     unsigned char key[] = {
    959      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
    960      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    961      1.1  christos         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
    962      1.1  christos     };
    963      1.1  christos 
    964      1.1  christos     pkey = EVP_PKEY_new_raw_private_key_ex(testctx, "HMAC",
    965  1.1.1.2  christos         NULL, key, sizeof(key));
    966      1.1  christos     if (!TEST_ptr(pkey))
    967      1.1  christos         return NULL;
    968      1.1  christos 
    969      1.1  christos     return pkey;
    970      1.1  christos }
    971      1.1  christos 
    972      1.1  christos static int test_EVP_set_config_properties(void)
    973      1.1  christos {
    974      1.1  christos     char *fetched_properties = NULL;
    975      1.1  christos     const char test_propq[] = "test.fizzbuzz=buzzfizz";
    976      1.1  christos     int res = 0;
    977      1.1  christos 
    978      1.1  christos     fetched_properties = EVP_get1_default_properties(OSSL_LIB_CTX_get0_global_default());
    979      1.1  christos     if (!TEST_ptr(fetched_properties)
    980  1.1.1.2  christos         || !TEST_str_eq(fetched_properties, test_propq))
    981      1.1  christos         goto err;
    982      1.1  christos     OPENSSL_free(fetched_properties);
    983      1.1  christos     fetched_properties = NULL;
    984      1.1  christos 
    985      1.1  christos     res = 1;
    986      1.1  christos err:
    987      1.1  christos     OPENSSL_free(fetched_properties);
    988      1.1  christos     return res;
    989      1.1  christos }
    990      1.1  christos 
    991      1.1  christos static int test_EVP_set_default_properties(void)
    992      1.1  christos {
    993      1.1  christos     OSSL_LIB_CTX *ctx;
    994      1.1  christos     EVP_MD *md = NULL;
    995      1.1  christos     int res = 0;
    996      1.1  christos     char *fetched_properties = NULL;
    997      1.1  christos     const char test_propq[] = "provider=fizzbang";
    998      1.1  christos     const char test_fips_propq[] = "fips=yes,provider=fizzbang";
    999      1.1  christos 
   1000      1.1  christos     if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
   1001  1.1.1.2  christos         || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
   1002      1.1  christos         goto err;
   1003      1.1  christos     EVP_MD_free(md);
   1004      1.1  christos     md = NULL;
   1005      1.1  christos 
   1006      1.1  christos     if (!TEST_true(EVP_set_default_properties(ctx, test_propq))
   1007  1.1.1.2  christos         || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
   1008  1.1.1.2  christos         || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
   1009      1.1  christos         goto err;
   1010      1.1  christos     EVP_MD_free(md);
   1011      1.1  christos     md = NULL;
   1012      1.1  christos 
   1013      1.1  christos     fetched_properties = EVP_get1_default_properties(ctx);
   1014      1.1  christos     if (!TEST_ptr(fetched_properties)
   1015  1.1.1.2  christos         || !TEST_str_eq(fetched_properties, test_propq))
   1016      1.1  christos         goto err;
   1017      1.1  christos     OPENSSL_free(fetched_properties);
   1018      1.1  christos     fetched_properties = NULL;
   1019      1.1  christos 
   1020      1.1  christos     if (!TEST_true(EVP_default_properties_enable_fips(ctx, 1)))
   1021      1.1  christos         goto err;
   1022      1.1  christos     fetched_properties = EVP_get1_default_properties(ctx);
   1023      1.1  christos     if (!TEST_ptr(fetched_properties)
   1024  1.1.1.2  christos         || !TEST_str_eq(fetched_properties, test_fips_propq))
   1025      1.1  christos         goto err;
   1026      1.1  christos     OPENSSL_free(fetched_properties);
   1027      1.1  christos     fetched_properties = NULL;
   1028      1.1  christos 
   1029      1.1  christos     if (!TEST_true(EVP_default_properties_enable_fips(ctx, 0)))
   1030      1.1  christos         goto err;
   1031      1.1  christos 
   1032      1.1  christos     if (!TEST_true(EVP_set_default_properties(ctx, NULL))
   1033  1.1.1.2  christos         || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
   1034      1.1  christos         goto err;
   1035      1.1  christos     res = 1;
   1036      1.1  christos err:
   1037      1.1  christos     OPENSSL_free(fetched_properties);
   1038      1.1  christos     EVP_MD_free(md);
   1039      1.1  christos     OSSL_LIB_CTX_free(ctx);
   1040      1.1  christos     return res;
   1041      1.1  christos }
   1042      1.1  christos 
   1043      1.1  christos #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
   1044      1.1  christos static EVP_PKEY *make_key_fromdata(char *keytype, OSSL_PARAM *params)
   1045      1.1  christos {
   1046      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   1047      1.1  christos     EVP_PKEY *tmp_pkey = NULL, *pkey = NULL;
   1048      1.1  christos 
   1049      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq)))
   1050      1.1  christos         goto err;
   1051      1.1  christos     /* Check that premature EVP_PKEY_CTX_set_params() fails gracefully */
   1052      1.1  christos     if (!TEST_int_eq(EVP_PKEY_CTX_set_params(pctx, params), 0)
   1053      1.1  christos         || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
   1054      1.1  christos         || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &tmp_pkey, EVP_PKEY_KEYPAIR,
   1055  1.1.1.2  christos                             params),
   1056  1.1.1.2  christos             0))
   1057      1.1  christos         goto err;
   1058      1.1  christos 
   1059      1.1  christos     if (!TEST_ptr(tmp_pkey))
   1060      1.1  christos         goto err;
   1061      1.1  christos 
   1062      1.1  christos     pkey = tmp_pkey;
   1063      1.1  christos     tmp_pkey = NULL;
   1064  1.1.1.2  christos err:
   1065      1.1  christos     EVP_PKEY_free(tmp_pkey);
   1066      1.1  christos     EVP_PKEY_CTX_free(pctx);
   1067      1.1  christos     return pkey;
   1068      1.1  christos }
   1069      1.1  christos 
   1070      1.1  christos static int test_selection(EVP_PKEY *pkey, int selection)
   1071      1.1  christos {
   1072      1.1  christos     int testresult = 0;
   1073      1.1  christos     int ret;
   1074      1.1  christos     BIO *bio = BIO_new(BIO_s_mem());
   1075      1.1  christos 
   1076      1.1  christos     ret = PEM_write_bio_PUBKEY(bio, pkey);
   1077      1.1  christos     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
   1078      1.1  christos         if (!TEST_true(ret))
   1079      1.1  christos             goto err;
   1080      1.1  christos     } else {
   1081      1.1  christos         if (!TEST_false(ret))
   1082      1.1  christos             goto err;
   1083      1.1  christos     }
   1084      1.1  christos     ret = PEM_write_bio_PrivateKey_ex(bio, pkey, NULL, NULL, 0, NULL, NULL,
   1085  1.1.1.2  christos         testctx, NULL);
   1086      1.1  christos     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
   1087      1.1  christos         if (!TEST_true(ret))
   1088      1.1  christos             goto err;
   1089      1.1  christos     } else {
   1090      1.1  christos         if (!TEST_false(ret))
   1091      1.1  christos             goto err;
   1092      1.1  christos     }
   1093      1.1  christos 
   1094      1.1  christos     testresult = 1;
   1095  1.1.1.2  christos err:
   1096      1.1  christos     BIO_free(bio);
   1097      1.1  christos 
   1098      1.1  christos     return testresult;
   1099      1.1  christos }
   1100      1.1  christos #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA || !OPENSSL_NO_EC */
   1101      1.1  christos 
   1102      1.1  christos /*
   1103      1.1  christos  * Test combinations of private, public, missing and private + public key
   1104      1.1  christos  * params to ensure they are all accepted
   1105      1.1  christos  */
   1106      1.1  christos #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
   1107      1.1  christos static int test_EVP_PKEY_ffc_priv_pub(char *keytype)
   1108      1.1  christos {
   1109      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1110      1.1  christos     OSSL_PARAM *params = NULL;
   1111      1.1  christos     EVP_PKEY *just_params = NULL;
   1112      1.1  christos     EVP_PKEY *params_and_priv = NULL;
   1113      1.1  christos     EVP_PKEY *params_and_pub = NULL;
   1114      1.1  christos     EVP_PKEY *params_and_keypair = NULL;
   1115      1.1  christos     BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
   1116      1.1  christos     int ret = 0;
   1117      1.1  christos 
   1118      1.1  christos     /*
   1119      1.1  christos      * Setup the parameters for our pkey object. For our purposes they don't
   1120      1.1  christos      * have to actually be *valid* parameters. We just need to set something.
   1121      1.1  christos      */
   1122      1.1  christos     if (!TEST_ptr(p = BN_new())
   1123      1.1  christos         || !TEST_ptr(q = BN_new())
   1124      1.1  christos         || !TEST_ptr(g = BN_new())
   1125      1.1  christos         || !TEST_ptr(pub = BN_new())
   1126      1.1  christos         || !TEST_ptr(priv = BN_new()))
   1127      1.1  christos         goto err;
   1128      1.1  christos 
   1129      1.1  christos     /* Test !priv and !pub */
   1130      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1131      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   1132      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   1133      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)))
   1134      1.1  christos         goto err;
   1135      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1136      1.1  christos         || !TEST_ptr(just_params = make_key_fromdata(keytype, params)))
   1137      1.1  christos         goto err;
   1138      1.1  christos 
   1139      1.1  christos     OSSL_PARAM_free(params);
   1140      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1141      1.1  christos     params = NULL;
   1142      1.1  christos     bld = NULL;
   1143      1.1  christos 
   1144      1.1  christos     if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
   1145      1.1  christos         || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
   1146      1.1  christos         goto err;
   1147      1.1  christos 
   1148      1.1  christos     /* Test priv and !pub */
   1149      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1150      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   1151      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   1152      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
   1153      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   1154  1.1.1.2  christos             priv)))
   1155      1.1  christos         goto err;
   1156      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1157      1.1  christos         || !TEST_ptr(params_and_priv = make_key_fromdata(keytype, params)))
   1158      1.1  christos         goto err;
   1159      1.1  christos 
   1160      1.1  christos     OSSL_PARAM_free(params);
   1161      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1162      1.1  christos     params = NULL;
   1163      1.1  christos     bld = NULL;
   1164      1.1  christos 
   1165      1.1  christos     if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
   1166      1.1  christos         || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
   1167      1.1  christos         goto err;
   1168      1.1  christos 
   1169      1.1  christos     /* Test !priv and pub */
   1170      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1171      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   1172      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   1173      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
   1174      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
   1175  1.1.1.2  christos             pub)))
   1176      1.1  christos         goto err;
   1177      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1178      1.1  christos         || !TEST_ptr(params_and_pub = make_key_fromdata(keytype, params)))
   1179      1.1  christos         goto err;
   1180      1.1  christos 
   1181      1.1  christos     OSSL_PARAM_free(params);
   1182      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1183      1.1  christos     params = NULL;
   1184      1.1  christos     bld = NULL;
   1185      1.1  christos 
   1186      1.1  christos     if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
   1187      1.1  christos         || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
   1188      1.1  christos         goto err;
   1189      1.1  christos 
   1190      1.1  christos     /* Test priv and pub */
   1191      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1192      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   1193      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   1194      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
   1195      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
   1196  1.1.1.2  christos             pub))
   1197      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   1198  1.1.1.2  christos             priv)))
   1199      1.1  christos         goto err;
   1200      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1201      1.1  christos         || !TEST_ptr(params_and_keypair = make_key_fromdata(keytype, params)))
   1202      1.1  christos         goto err;
   1203      1.1  christos 
   1204      1.1  christos     if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
   1205      1.1  christos         goto err;
   1206      1.1  christos 
   1207      1.1  christos     ret = 1;
   1208  1.1.1.2  christos err:
   1209      1.1  christos     OSSL_PARAM_free(params);
   1210      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1211      1.1  christos     EVP_PKEY_free(just_params);
   1212      1.1  christos     EVP_PKEY_free(params_and_priv);
   1213      1.1  christos     EVP_PKEY_free(params_and_pub);
   1214      1.1  christos     EVP_PKEY_free(params_and_keypair);
   1215      1.1  christos     BN_free(p);
   1216      1.1  christos     BN_free(q);
   1217      1.1  christos     BN_free(g);
   1218      1.1  christos     BN_free(pub);
   1219      1.1  christos     BN_free(priv);
   1220      1.1  christos 
   1221      1.1  christos     return ret;
   1222      1.1  christos }
   1223      1.1  christos #endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA */
   1224      1.1  christos 
   1225      1.1  christos /*
   1226      1.1  christos  * Test combinations of private, public, missing and private + public key
   1227      1.1  christos  * params to ensure they are all accepted for EC keys
   1228      1.1  christos  */
   1229      1.1  christos #ifndef OPENSSL_NO_EC
   1230      1.1  christos static unsigned char ec_priv[] = {
   1231      1.1  christos     0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23,
   1232      1.1  christos     0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5,
   1233      1.1  christos     0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16
   1234      1.1  christos };
   1235      1.1  christos static unsigned char ec_pub[] = {
   1236      1.1  christos     0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97,
   1237      1.1  christos     0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81,
   1238      1.1  christos     0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d,
   1239      1.1  christos     0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e,
   1240      1.1  christos     0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1,
   1241      1.1  christos     0x08, 0x09, 0xb8, 0xdb, 0x03
   1242      1.1  christos };
   1243      1.1  christos 
   1244      1.1  christos static int test_EC_priv_pub(void)
   1245      1.1  christos {
   1246      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1247      1.1  christos     OSSL_PARAM *params = NULL;
   1248      1.1  christos     EVP_PKEY *just_params = NULL;
   1249      1.1  christos     EVP_PKEY *params_and_priv = NULL;
   1250      1.1  christos     EVP_PKEY *params_and_pub = NULL;
   1251      1.1  christos     EVP_PKEY *params_and_keypair = NULL;
   1252      1.1  christos     BIGNUM *priv = NULL;
   1253      1.1  christos     int ret = 0;
   1254      1.1  christos     unsigned char *encoded = NULL;
   1255      1.1  christos     size_t len = 0;
   1256      1.1  christos     unsigned char buffer[128];
   1257      1.1  christos 
   1258      1.1  christos     /*
   1259      1.1  christos      * Setup the parameters for our pkey object. For our purposes they don't
   1260      1.1  christos      * have to actually be *valid* parameters. We just need to set something.
   1261      1.1  christos      */
   1262      1.1  christos     if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
   1263      1.1  christos         goto err;
   1264      1.1  christos 
   1265      1.1  christos     /* Test !priv and !pub */
   1266      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1267      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1268  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1269  1.1.1.2  christos             "P-256", 0)))
   1270      1.1  christos         goto err;
   1271      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1272      1.1  christos         || !TEST_ptr(just_params = make_key_fromdata("EC", params)))
   1273      1.1  christos         goto err;
   1274      1.1  christos 
   1275      1.1  christos     OSSL_PARAM_free(params);
   1276      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1277      1.1  christos     params = NULL;
   1278      1.1  christos     bld = NULL;
   1279      1.1  christos 
   1280      1.1  christos     if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
   1281      1.1  christos         || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
   1282      1.1  christos         goto err;
   1283      1.1  christos 
   1284      1.1  christos     /* Test priv and !pub */
   1285      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1286      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1287  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1288  1.1.1.2  christos             "P-256", 0))
   1289      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   1290  1.1.1.2  christos             priv)))
   1291      1.1  christos         goto err;
   1292      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1293      1.1  christos         || !TEST_ptr(params_and_priv = make_key_fromdata("EC", params)))
   1294      1.1  christos         goto err;
   1295      1.1  christos 
   1296      1.1  christos     OSSL_PARAM_free(params);
   1297      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1298      1.1  christos     params = NULL;
   1299      1.1  christos     bld = NULL;
   1300      1.1  christos 
   1301      1.1  christos     /*
   1302      1.1  christos      * We indicate only parameters here, in spite of having built a key that
   1303      1.1  christos      * has a private part, because the PEM_write_bio_PrivateKey_ex call is
   1304      1.1  christos      * expected to fail because it does not support exporting a private EC
   1305      1.1  christos      * key without a corresponding public key
   1306      1.1  christos      */
   1307      1.1  christos     if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
   1308      1.1  christos         || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
   1309      1.1  christos         goto err;
   1310      1.1  christos 
   1311      1.1  christos     /* Test !priv and pub */
   1312      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1313      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1314  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1315  1.1.1.2  christos             "P-256", 0))
   1316      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
   1317  1.1.1.2  christos             OSSL_PKEY_PARAM_PUB_KEY,
   1318  1.1.1.2  christos             ec_pub, sizeof(ec_pub))))
   1319      1.1  christos         goto err;
   1320      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1321      1.1  christos         || !TEST_ptr(params_and_pub = make_key_fromdata("EC", params)))
   1322      1.1  christos         goto err;
   1323      1.1  christos 
   1324      1.1  christos     OSSL_PARAM_free(params);
   1325      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1326      1.1  christos     params = NULL;
   1327      1.1  christos     bld = NULL;
   1328      1.1  christos 
   1329      1.1  christos     if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
   1330      1.1  christos         || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
   1331      1.1  christos         goto err;
   1332      1.1  christos 
   1333      1.1  christos     /* Test priv and pub */
   1334      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1335      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1336  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1337  1.1.1.2  christos             "P-256", 0))
   1338      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
   1339  1.1.1.2  christos             OSSL_PKEY_PARAM_PUB_KEY,
   1340  1.1.1.2  christos             ec_pub, sizeof(ec_pub)))
   1341      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   1342  1.1.1.2  christos             priv)))
   1343      1.1  christos         goto err;
   1344      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1345      1.1  christos         || !TEST_ptr(params_and_keypair = make_key_fromdata("EC", params)))
   1346      1.1  christos         goto err;
   1347      1.1  christos 
   1348      1.1  christos     if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
   1349      1.1  christos         goto err;
   1350      1.1  christos 
   1351      1.1  christos     /* Try key equality */
   1352      1.1  christos     if (!TEST_int_gt(EVP_PKEY_parameters_eq(just_params, just_params), 0)
   1353      1.1  christos         || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_pub),
   1354  1.1.1.2  christos             0)
   1355      1.1  christos         || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_priv),
   1356  1.1.1.2  christos             0)
   1357      1.1  christos         || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_keypair),
   1358  1.1.1.2  christos             0)
   1359      1.1  christos         || !TEST_int_gt(EVP_PKEY_eq(params_and_pub, params_and_pub), 0)
   1360      1.1  christos         || !TEST_int_gt(EVP_PKEY_eq(params_and_priv, params_and_priv), 0)
   1361      1.1  christos         || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_pub), 0)
   1362      1.1  christos         || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_priv), 0))
   1363      1.1  christos         goto err;
   1364      1.1  christos 
   1365      1.1  christos     /* Positive and negative testcase for EVP_PKEY_get1_encoded_public_key */
   1366      1.1  christos     if (!TEST_int_gt(EVP_PKEY_get1_encoded_public_key(params_and_pub, &encoded), 0))
   1367      1.1  christos         goto err;
   1368      1.1  christos     OPENSSL_free(encoded);
   1369      1.1  christos     encoded = NULL;
   1370      1.1  christos     if (!TEST_int_eq(EVP_PKEY_get1_encoded_public_key(just_params, &encoded), 0)) {
   1371      1.1  christos         OPENSSL_free(encoded);
   1372      1.1  christos         encoded = NULL;
   1373      1.1  christos         goto err;
   1374      1.1  christos     }
   1375      1.1  christos 
   1376      1.1  christos     /* Positive and negative testcase for EVP_PKEY_get_octet_string_param */
   1377      1.1  christos     if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
   1378  1.1.1.2  christos                          OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
   1379  1.1.1.2  christos                          buffer, sizeof(buffer), &len),
   1380  1.1.1.2  christos             1)
   1381      1.1  christos         || !TEST_int_eq(len, 65))
   1382      1.1  christos         goto err;
   1383      1.1  christos 
   1384      1.1  christos     len = 0;
   1385      1.1  christos     if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
   1386  1.1.1.2  christos                          OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
   1387  1.1.1.2  christos                          NULL, 0, &len),
   1388  1.1.1.2  christos             1)
   1389      1.1  christos         || !TEST_int_eq(len, 65))
   1390      1.1  christos         goto err;
   1391      1.1  christos 
   1392      1.1  christos     /* too-short buffer len*/
   1393      1.1  christos     if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
   1394  1.1.1.2  christos                          OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
   1395  1.1.1.2  christos                          buffer, 10, &len),
   1396  1.1.1.2  christos             0))
   1397      1.1  christos         goto err;
   1398      1.1  christos 
   1399      1.1  christos     ret = 1;
   1400  1.1.1.2  christos err:
   1401      1.1  christos     OSSL_PARAM_free(params);
   1402      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1403      1.1  christos     EVP_PKEY_free(just_params);
   1404      1.1  christos     EVP_PKEY_free(params_and_priv);
   1405      1.1  christos     EVP_PKEY_free(params_and_pub);
   1406      1.1  christos     EVP_PKEY_free(params_and_keypair);
   1407      1.1  christos     BN_free(priv);
   1408      1.1  christos 
   1409      1.1  christos     return ret;
   1410      1.1  christos }
   1411      1.1  christos 
   1412      1.1  christos /* Also test that we can read the EC PUB affine coordinates */
   1413      1.1  christos static int test_evp_get_ec_pub(void)
   1414      1.1  christos {
   1415      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   1416      1.1  christos     OSSL_PARAM *params = NULL;
   1417      1.1  christos     unsigned char *pad = NULL;
   1418      1.1  christos     EVP_PKEY *keypair = NULL;
   1419      1.1  christos     BIGNUM *priv = NULL;
   1420      1.1  christos     BIGNUM *x = NULL;
   1421      1.1  christos     BIGNUM *y = NULL;
   1422      1.1  christos     int ret = 0;
   1423      1.1  christos 
   1424      1.1  christos     if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
   1425      1.1  christos         goto err;
   1426      1.1  christos 
   1427      1.1  christos     if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
   1428      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
   1429  1.1.1.2  christos             OSSL_PKEY_PARAM_GROUP_NAME,
   1430  1.1.1.2  christos             "P-256", 0))
   1431      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
   1432  1.1.1.2  christos             OSSL_PKEY_PARAM_PUB_KEY,
   1433  1.1.1.2  christos             ec_pub, sizeof(ec_pub)))
   1434      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   1435  1.1.1.2  christos             priv)))
   1436      1.1  christos         goto err;
   1437      1.1  christos 
   1438      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
   1439      1.1  christos         || !TEST_ptr(keypair = make_key_fromdata("EC", params)))
   1440      1.1  christos         goto err;
   1441      1.1  christos 
   1442      1.1  christos     if (!test_selection(keypair, EVP_PKEY_KEYPAIR))
   1443      1.1  christos         goto err;
   1444      1.1  christos 
   1445      1.1  christos     if (!EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_X, &x)
   1446      1.1  christos         || !EVP_PKEY_get_bn_param(keypair, OSSL_PKEY_PARAM_EC_PUB_Y, &y))
   1447      1.1  christos         goto err;
   1448      1.1  christos 
   1449      1.1  christos     if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub))))
   1450      1.1  christos         goto err;
   1451      1.1  christos 
   1452      1.1  christos     pad[0] = ec_pub[0];
   1453      1.1  christos     BN_bn2bin(x, &pad[1]);
   1454      1.1  christos     BN_bn2bin(y, &pad[33]);
   1455      1.1  christos     if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0))
   1456      1.1  christos         goto err;
   1457      1.1  christos 
   1458      1.1  christos     ret = 1;
   1459      1.1  christos 
   1460      1.1  christos err:
   1461      1.1  christos     OSSL_PARAM_free(params);
   1462      1.1  christos     OSSL_PARAM_BLD_free(bld);
   1463      1.1  christos     EVP_PKEY_free(keypair);
   1464      1.1  christos     OPENSSL_free(pad);
   1465      1.1  christos     BN_free(priv);
   1466      1.1  christos     BN_free(x);
   1467      1.1  christos     BN_free(y);
   1468      1.1  christos     return ret;
   1469      1.1  christos }
   1470      1.1  christos 
   1471      1.1  christos /* Test that using a legacy EC key with only a private key in it works */
   1472  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   1473      1.1  christos static int test_EC_priv_only_legacy(void)
   1474      1.1  christos {
   1475      1.1  christos     BIGNUM *priv = NULL;
   1476      1.1  christos     int ret = 0;
   1477      1.1  christos     EC_KEY *eckey = NULL;
   1478      1.1  christos     EVP_PKEY *pkey = NULL, *dup_pk = NULL;
   1479      1.1  christos     EVP_MD_CTX *ctx = NULL;
   1480      1.1  christos 
   1481      1.1  christos     /* Create the low level EC_KEY */
   1482      1.1  christos     if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
   1483      1.1  christos         goto err;
   1484      1.1  christos 
   1485      1.1  christos     eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
   1486      1.1  christos     if (!TEST_ptr(eckey))
   1487      1.1  christos         goto err;
   1488      1.1  christos 
   1489      1.1  christos     if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
   1490      1.1  christos         goto err;
   1491      1.1  christos 
   1492      1.1  christos     pkey = EVP_PKEY_new();
   1493      1.1  christos     if (!TEST_ptr(pkey))
   1494      1.1  christos         goto err;
   1495      1.1  christos 
   1496      1.1  christos     if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
   1497      1.1  christos         goto err;
   1498      1.1  christos     eckey = NULL;
   1499      1.1  christos 
   1500      1.1  christos     for (;;) {
   1501      1.1  christos         ret = 0;
   1502      1.1  christos         ctx = EVP_MD_CTX_new();
   1503      1.1  christos         if (!TEST_ptr(ctx))
   1504      1.1  christos             goto err;
   1505      1.1  christos 
   1506      1.1  christos         /*
   1507      1.1  christos          * The EVP_DigestSignInit function should create the key on the
   1508      1.1  christos          * provider side which is sufficient for this test.
   1509      1.1  christos          */
   1510      1.1  christos         if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx,
   1511  1.1.1.2  christos                 testpropq, pkey, NULL)))
   1512      1.1  christos             goto err;
   1513      1.1  christos         EVP_MD_CTX_free(ctx);
   1514      1.1  christos         ctx = NULL;
   1515      1.1  christos 
   1516      1.1  christos         if (dup_pk != NULL)
   1517      1.1  christos             break;
   1518      1.1  christos 
   1519      1.1  christos         if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
   1520      1.1  christos             goto err;
   1521      1.1  christos         /* EVP_PKEY_eq() returns -2 with missing public keys */
   1522      1.1  christos         ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2);
   1523      1.1  christos         EVP_PKEY_free(pkey);
   1524      1.1  christos         pkey = dup_pk;
   1525      1.1  christos         if (!ret)
   1526      1.1  christos             goto err;
   1527      1.1  christos     }
   1528      1.1  christos     ret = 1;
   1529      1.1  christos 
   1530  1.1.1.2  christos err:
   1531      1.1  christos     EVP_MD_CTX_free(ctx);
   1532      1.1  christos     EVP_PKEY_free(pkey);
   1533      1.1  christos     EC_KEY_free(eckey);
   1534      1.1  christos     BN_free(priv);
   1535      1.1  christos 
   1536      1.1  christos     return ret;
   1537      1.1  christos }
   1538      1.1  christos 
   1539      1.1  christos static int test_evp_get_ec_pub_legacy(void)
   1540      1.1  christos {
   1541      1.1  christos     OSSL_LIB_CTX *libctx = NULL;
   1542      1.1  christos     unsigned char *pad = NULL;
   1543      1.1  christos     EVP_PKEY *pkey = NULL;
   1544      1.1  christos     EC_KEY *eckey = NULL;
   1545      1.1  christos     BIGNUM *priv = NULL;
   1546      1.1  christos     BIGNUM *x = NULL;
   1547      1.1  christos     BIGNUM *y = NULL;
   1548      1.1  christos     int ret = 0;
   1549      1.1  christos 
   1550      1.1  christos     if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()))
   1551      1.1  christos         goto err;
   1552      1.1  christos 
   1553      1.1  christos     /* Create the legacy key */
   1554      1.1  christos     if (!TEST_ptr(eckey = EC_KEY_new_by_curve_name_ex(libctx, NULL,
   1555  1.1.1.2  christos                       NID_X9_62_prime256v1)))
   1556      1.1  christos         goto err;
   1557      1.1  christos 
   1558      1.1  christos     if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
   1559      1.1  christos         goto err;
   1560      1.1  christos 
   1561      1.1  christos     if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
   1562      1.1  christos         goto err;
   1563      1.1  christos 
   1564      1.1  christos     if (!TEST_ptr(x = BN_bin2bn(&ec_pub[1], 32, NULL)))
   1565      1.1  christos         goto err;
   1566      1.1  christos 
   1567      1.1  christos     if (!TEST_ptr(y = BN_bin2bn(&ec_pub[33], 32, NULL)))
   1568      1.1  christos         goto err;
   1569      1.1  christos 
   1570      1.1  christos     if (!TEST_true(EC_KEY_set_public_key_affine_coordinates(eckey, x, y)))
   1571      1.1  christos         goto err;
   1572      1.1  christos 
   1573      1.1  christos     if (!TEST_ptr(pkey = EVP_PKEY_new()))
   1574      1.1  christos         goto err;
   1575      1.1  christos 
   1576      1.1  christos     /* Transfer the legacy key */
   1577      1.1  christos     if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
   1578      1.1  christos         goto err;
   1579      1.1  christos     eckey = NULL;
   1580      1.1  christos 
   1581      1.1  christos     if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_X, &x))
   1582      1.1  christos         || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_EC_PUB_Y, &y)))
   1583      1.1  christos         goto err;
   1584      1.1  christos 
   1585      1.1  christos     if (!TEST_ptr(pad = OPENSSL_zalloc(sizeof(ec_pub))))
   1586      1.1  christos         goto err;
   1587      1.1  christos 
   1588      1.1  christos     pad[0] = ec_pub[0];
   1589      1.1  christos     BN_bn2bin(x, &pad[1]);
   1590      1.1  christos     BN_bn2bin(y, &pad[33]);
   1591      1.1  christos 
   1592      1.1  christos     if (!TEST_true(memcmp(ec_pub, pad, sizeof(ec_pub)) == 0))
   1593      1.1  christos         goto err;
   1594      1.1  christos 
   1595      1.1  christos     ret = 1;
   1596      1.1  christos 
   1597      1.1  christos err:
   1598      1.1  christos     OSSL_LIB_CTX_free(libctx);
   1599      1.1  christos     EVP_PKEY_free(pkey);
   1600      1.1  christos     EC_KEY_free(eckey);
   1601      1.1  christos     OPENSSL_free(pad);
   1602      1.1  christos     BN_free(priv);
   1603      1.1  christos     BN_free(x);
   1604      1.1  christos     BN_free(y);
   1605      1.1  christos 
   1606      1.1  christos     return ret;
   1607      1.1  christos }
   1608  1.1.1.2  christos #endif /* OPENSSL_NO_DEPRECATED_3_0 */
   1609      1.1  christos #endif /* OPENSSL_NO_EC */
   1610      1.1  christos 
   1611      1.1  christos static int test_EVP_PKEY_sign(int tst)
   1612      1.1  christos {
   1613      1.1  christos     int ret = 0;
   1614      1.1  christos     EVP_PKEY *pkey = NULL;
   1615      1.1  christos     unsigned char *sig = NULL;
   1616      1.1  christos     size_t sig_len = 0, shortsig_len = 1;
   1617      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   1618      1.1  christos     unsigned char tbs[] = {
   1619      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   1620      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
   1621      1.1  christos     };
   1622      1.1  christos 
   1623      1.1  christos     if (tst == 0) {
   1624      1.1  christos         if (!TEST_ptr(pkey = load_example_rsa_key()))
   1625      1.1  christos             goto out;
   1626      1.1  christos     } else if (tst == 1) {
   1627      1.1  christos #ifndef OPENSSL_NO_DSA
   1628      1.1  christos         if (!TEST_ptr(pkey = load_example_dsa_key()))
   1629      1.1  christos             goto out;
   1630      1.1  christos #else
   1631      1.1  christos         ret = 1;
   1632      1.1  christos         goto out;
   1633      1.1  christos #endif
   1634      1.1  christos     } else {
   1635      1.1  christos #ifndef OPENSSL_NO_EC
   1636      1.1  christos         if (!TEST_ptr(pkey = load_example_ec_key()))
   1637      1.1  christos             goto out;
   1638      1.1  christos #else
   1639      1.1  christos         ret = 1;
   1640      1.1  christos         goto out;
   1641      1.1  christos #endif
   1642      1.1  christos     }
   1643      1.1  christos 
   1644      1.1  christos     ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL);
   1645      1.1  christos     if (!TEST_ptr(ctx)
   1646  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)
   1647  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs,
   1648  1.1.1.2  christos                             sizeof(tbs)),
   1649  1.1.1.2  christos             0))
   1650      1.1  christos         goto out;
   1651      1.1  christos     sig = OPENSSL_malloc(sig_len);
   1652      1.1  christos     if (!TEST_ptr(sig)
   1653  1.1.1.2  christos         /* Test sending a signature buffer that is too short is rejected */
   1654  1.1.1.2  christos         || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs,
   1655  1.1.1.2  christos                             sizeof(tbs)),
   1656  1.1.1.2  christos             0)
   1657  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)),
   1658  1.1.1.2  christos             0)
   1659  1.1.1.2  christos         /* Test the signature round-trips */
   1660  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
   1661  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)),
   1662  1.1.1.2  christos             0))
   1663      1.1  christos         goto out;
   1664      1.1  christos 
   1665      1.1  christos     ret = 1;
   1666  1.1.1.2  christos out:
   1667      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1668      1.1  christos     OPENSSL_free(sig);
   1669      1.1  christos     EVP_PKEY_free(pkey);
   1670      1.1  christos     return ret;
   1671      1.1  christos }
   1672      1.1  christos 
   1673      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   1674      1.1  christos static int test_EVP_PKEY_sign_with_app_method(int tst)
   1675      1.1  christos {
   1676      1.1  christos     int ret = 0;
   1677      1.1  christos     EVP_PKEY *pkey = NULL;
   1678      1.1  christos     RSA *rsa = NULL;
   1679      1.1  christos     RSA_METHOD *rsa_meth = NULL;
   1680      1.1  christos #ifndef OPENSSL_NO_DSA
   1681      1.1  christos     DSA *dsa = NULL;
   1682      1.1  christos     DSA_METHOD *dsa_meth = NULL;
   1683      1.1  christos #endif
   1684      1.1  christos     unsigned char *sig = NULL;
   1685      1.1  christos     size_t sig_len = 0, shortsig_len = 1;
   1686      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   1687      1.1  christos     unsigned char tbs[] = {
   1688      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   1689      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
   1690      1.1  christos     };
   1691      1.1  christos 
   1692      1.1  christos     if (tst == 0) {
   1693      1.1  christos         if (!TEST_ptr(pkey = load_example_rsa_key()))
   1694      1.1  christos             goto out;
   1695      1.1  christos         if (!TEST_ptr(rsa_meth = RSA_meth_dup(RSA_get_default_method())))
   1696      1.1  christos             goto out;
   1697      1.1  christos 
   1698      1.1  christos         if (!TEST_ptr(rsa = EVP_PKEY_get1_RSA(pkey))
   1699      1.1  christos             || !TEST_int_gt(RSA_set_method(rsa, rsa_meth), 0)
   1700      1.1  christos             || !TEST_int_gt(EVP_PKEY_assign_RSA(pkey, rsa), 0))
   1701      1.1  christos             goto out;
   1702      1.1  christos         rsa = NULL; /* now owned by the pkey */
   1703      1.1  christos     } else {
   1704      1.1  christos #ifndef OPENSSL_NO_DSA
   1705      1.1  christos         if (!TEST_ptr(pkey = load_example_dsa_key()))
   1706  1.1.1.2  christos             goto out;
   1707      1.1  christos         if (!TEST_ptr(dsa_meth = DSA_meth_dup(DSA_get_default_method())))
   1708      1.1  christos             goto out;
   1709      1.1  christos 
   1710      1.1  christos         if (!TEST_ptr(dsa = EVP_PKEY_get1_DSA(pkey))
   1711      1.1  christos             || !TEST_int_gt(DSA_set_method(dsa, dsa_meth), 0)
   1712      1.1  christos             || !TEST_int_gt(EVP_PKEY_assign_DSA(pkey, dsa), 0))
   1713      1.1  christos             goto out;
   1714      1.1  christos         dsa = NULL; /* now owned by the pkey */
   1715      1.1  christos #else
   1716      1.1  christos         ret = 1;
   1717      1.1  christos         goto out;
   1718      1.1  christos #endif
   1719      1.1  christos     }
   1720      1.1  christos 
   1721      1.1  christos     ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL);
   1722      1.1  christos     if (!TEST_ptr(ctx)
   1723  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)
   1724  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs,
   1725  1.1.1.2  christos                             sizeof(tbs)),
   1726  1.1.1.2  christos             0))
   1727      1.1  christos         goto out;
   1728      1.1  christos     sig = OPENSSL_malloc(sig_len);
   1729      1.1  christos     if (!TEST_ptr(sig)
   1730  1.1.1.2  christos         /* Test sending a signature buffer that is too short is rejected */
   1731  1.1.1.2  christos         || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs,
   1732  1.1.1.2  christos                             sizeof(tbs)),
   1733  1.1.1.2  christos             0)
   1734  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)),
   1735  1.1.1.2  christos             0)
   1736  1.1.1.2  christos         /* Test the signature round-trips */
   1737  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
   1738  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)),
   1739  1.1.1.2  christos             0))
   1740      1.1  christos         goto out;
   1741      1.1  christos 
   1742      1.1  christos     ret = 1;
   1743  1.1.1.2  christos out:
   1744      1.1  christos     EVP_PKEY_CTX_free(ctx);
   1745      1.1  christos     OPENSSL_free(sig);
   1746      1.1  christos     EVP_PKEY_free(pkey);
   1747      1.1  christos     RSA_free(rsa);
   1748      1.1  christos     RSA_meth_free(rsa_meth);
   1749      1.1  christos #ifndef OPENSSL_NO_DSA
   1750      1.1  christos     DSA_free(dsa);
   1751      1.1  christos     DSA_meth_free(dsa_meth);
   1752      1.1  christos #endif
   1753      1.1  christos     return ret;
   1754      1.1  christos }
   1755      1.1  christos #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
   1756      1.1  christos 
   1757      1.1  christos /*
   1758      1.1  christos  * n = 0 => test using legacy cipher
   1759      1.1  christos  * n = 1 => test using fetched cipher
   1760      1.1  christos  */
   1761      1.1  christos static int test_EVP_Enveloped(int n)
   1762      1.1  christos {
   1763      1.1  christos     int ret = 0;
   1764      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   1765      1.1  christos     EVP_PKEY *keypair = NULL;
   1766      1.1  christos     unsigned char *kek = NULL;
   1767      1.1  christos     unsigned char iv[EVP_MAX_IV_LENGTH];
   1768      1.1  christos     static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
   1769      1.1  christos     int len, kek_len, ciphertext_len, plaintext_len;
   1770      1.1  christos     unsigned char ciphertext[32], plaintext[16];
   1771      1.1  christos     EVP_CIPHER *type = NULL;
   1772      1.1  christos 
   1773      1.1  christos     if (nullprov != NULL)
   1774      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   1775      1.1  christos 
   1776      1.1  christos     if (n == 0)
   1777      1.1  christos         type = (EVP_CIPHER *)EVP_aes_256_cbc();
   1778      1.1  christos     else if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "AES-256-CBC",
   1779  1.1.1.2  christos                            testpropq)))
   1780      1.1  christos         goto err;
   1781      1.1  christos 
   1782      1.1  christos     if (!TEST_ptr(keypair = load_example_rsa_key())
   1783  1.1.1.2  christos         || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_get_size(keypair)))
   1784  1.1.1.2  christos         || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   1785  1.1.1.2  christos         || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
   1786  1.1.1.2  christos             &keypair, 1))
   1787  1.1.1.2  christos         || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
   1788  1.1.1.2  christos             msg, sizeof(msg)))
   1789  1.1.1.2  christos         || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
   1790  1.1.1.2  christos             &len)))
   1791      1.1  christos         goto err;
   1792      1.1  christos 
   1793      1.1  christos     ciphertext_len += len;
   1794      1.1  christos 
   1795      1.1  christos     if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
   1796  1.1.1.2  christos         || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
   1797  1.1.1.2  christos             ciphertext, ciphertext_len))
   1798  1.1.1.2  christos         || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
   1799      1.1  christos         goto err;
   1800      1.1  christos 
   1801      1.1  christos     plaintext_len += len;
   1802      1.1  christos     if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
   1803      1.1  christos         goto err;
   1804      1.1  christos 
   1805      1.1  christos     ret = 1;
   1806      1.1  christos err:
   1807      1.1  christos     if (n != 0)
   1808      1.1  christos         EVP_CIPHER_free(type);
   1809      1.1  christos     OPENSSL_free(kek);
   1810      1.1  christos     EVP_PKEY_free(keypair);
   1811      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   1812      1.1  christos     return ret;
   1813      1.1  christos }
   1814      1.1  christos 
   1815      1.1  christos /*
   1816      1.1  christos  * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
   1817      1.1  christos  * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
   1818      1.1  christos  * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
   1819      1.1  christos  * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
   1820      1.1  christos  * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
   1821      1.1  christos  * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
   1822      1.1  christos  * Test 6: Use an MD BIO to do the Update calls instead (RSA)
   1823      1.1  christos  * Test 7: Use an MD BIO to do the Update calls instead (DSA)
   1824      1.1  christos  * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
   1825      1.1  christos  * Test 9: Use EVP_DigestSign (Implicit fetch digest, RSA, short sig)
   1826      1.1  christos  * Test 10: Use EVP_DigestSign (Implicit fetch digest, DSA, short sig)
   1827      1.1  christos  * Test 11: Use EVP_DigestSign (Implicit fetch digest, HMAC, short sig)
   1828      1.1  christos  * Test 12: Use EVP_DigestSign (Implicit fetch digest, RSA)
   1829      1.1  christos  * Test 13: Use EVP_DigestSign (Implicit fetch digest, DSA)
   1830      1.1  christos  * Test 14: Use EVP_DigestSign (Implicit fetch digest, HMAC)
   1831      1.1  christos  * Test 15-29: Same as above with reinitialization
   1832      1.1  christos  */
   1833      1.1  christos static int test_EVP_DigestSignInit(int tst)
   1834      1.1  christos {
   1835      1.1  christos     int ret = 0;
   1836      1.1  christos     EVP_PKEY *pkey = NULL;
   1837      1.1  christos     unsigned char *sig = NULL, *sig2 = NULL;
   1838      1.1  christos     size_t sig_len = 0, sig2_len = 0, shortsig_len = 1;
   1839      1.1  christos     EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
   1840      1.1  christos     EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
   1841      1.1  christos     BIO *mdbio = NULL, *membio = NULL;
   1842      1.1  christos     size_t written;
   1843      1.1  christos     const EVP_MD *md;
   1844      1.1  christos     EVP_MD *mdexp = NULL;
   1845      1.1  christos     int reinit = 0;
   1846      1.1  christos 
   1847      1.1  christos     if (nullprov != NULL)
   1848      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   1849      1.1  christos 
   1850      1.1  christos     if (tst >= 15) {
   1851      1.1  christos         reinit = 1;
   1852      1.1  christos         tst -= 15;
   1853      1.1  christos     }
   1854      1.1  christos 
   1855      1.1  christos     if (tst >= 6 && tst <= 8) {
   1856      1.1  christos         membio = BIO_new(BIO_s_mem());
   1857      1.1  christos         mdbio = BIO_new(BIO_f_md());
   1858      1.1  christos         if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
   1859      1.1  christos             goto out;
   1860      1.1  christos         BIO_push(mdbio, membio);
   1861      1.1  christos         if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
   1862      1.1  christos             goto out;
   1863      1.1  christos     } else {
   1864      1.1  christos         if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
   1865  1.1.1.2  christos             || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
   1866      1.1  christos             goto out;
   1867      1.1  christos     }
   1868      1.1  christos 
   1869      1.1  christos     if (tst % 3 == 0) {
   1870      1.1  christos         if (!TEST_ptr(pkey = load_example_rsa_key()))
   1871  1.1.1.2  christos             goto out;
   1872      1.1  christos     } else if (tst % 3 == 1) {
   1873      1.1  christos #ifndef OPENSSL_NO_DSA
   1874      1.1  christos         if (!TEST_ptr(pkey = load_example_dsa_key()))
   1875  1.1.1.2  christos             goto out;
   1876      1.1  christos #else
   1877      1.1  christos         ret = 1;
   1878      1.1  christos         goto out;
   1879      1.1  christos #endif
   1880      1.1  christos     } else {
   1881      1.1  christos         if (!TEST_ptr(pkey = load_example_hmac_key()))
   1882  1.1.1.2  christos             goto out;
   1883      1.1  christos     }
   1884      1.1  christos 
   1885      1.1  christos     if (tst >= 3 && tst <= 5)
   1886      1.1  christos         md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
   1887      1.1  christos     else
   1888      1.1  christos         md = EVP_sha256();
   1889      1.1  christos 
   1890      1.1  christos     if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
   1891      1.1  christos         goto out;
   1892      1.1  christos 
   1893      1.1  christos     if (reinit && !TEST_true(EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, NULL)))
   1894      1.1  christos         goto out;
   1895      1.1  christos 
   1896      1.1  christos     if (tst >= 6 && tst <= 8) {
   1897      1.1  christos         if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
   1898      1.1  christos             goto out;
   1899      1.1  christos     } else if (tst < 6) {
   1900      1.1  christos         if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
   1901      1.1  christos             goto out;
   1902      1.1  christos     }
   1903      1.1  christos 
   1904      1.1  christos     if (tst >= 9) {
   1905      1.1  christos         /* Determine the size of the signature. */
   1906      1.1  christos         if (!TEST_true(EVP_DigestSign(md_ctx, NULL, &sig_len, kMsg,
   1907  1.1.1.2  christos                 sizeof(kMsg)))
   1908  1.1.1.2  christos             || !TEST_ptr(sig = OPENSSL_malloc(sig_len)))
   1909      1.1  christos             goto out;
   1910      1.1  christos         if (tst <= 11) {
   1911      1.1  christos             /* Test that supply a short sig buffer fails */
   1912      1.1  christos             if (!TEST_false(EVP_DigestSign(md_ctx, sig, &shortsig_len, kMsg,
   1913  1.1.1.2  christos                     sizeof(kMsg))))
   1914      1.1  christos                 goto out;
   1915      1.1  christos             /*
   1916      1.1  christos              * We end here because once EVP_DigestSign() has failed you should
   1917      1.1  christos              * not call it again without re-initing the ctx
   1918      1.1  christos              */
   1919      1.1  christos             ret = 1;
   1920      1.1  christos             goto out;
   1921      1.1  christos         }
   1922      1.1  christos         if (!TEST_true(EVP_DigestSign(md_ctx, sig, &sig_len, kMsg,
   1923  1.1.1.2  christos                 sizeof(kMsg))))
   1924      1.1  christos             goto out;
   1925      1.1  christos     } else {
   1926      1.1  christos         /* Determine the size of the signature. */
   1927      1.1  christos         if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
   1928  1.1.1.2  christos             || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
   1929  1.1.1.2  christos             /*
   1930  1.1.1.2  christos              * Trying to create a signature with a deliberately short
   1931  1.1.1.2  christos              * buffer should fail.
   1932  1.1.1.2  christos              */
   1933  1.1.1.2  christos             || !TEST_false(EVP_DigestSignFinal(md_ctx, sig, &shortsig_len))
   1934  1.1.1.2  christos             || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
   1935      1.1  christos             goto out;
   1936      1.1  christos     }
   1937      1.1  christos 
   1938      1.1  christos     /*
   1939      1.1  christos      * Ensure that the signature round-trips (Verification isn't supported for
   1940      1.1  christos      * HMAC via EVP_DigestVerify*)
   1941      1.1  christos      */
   1942      1.1  christos     if (tst % 3 != 2) {
   1943      1.1  christos         if (tst >= 6 && tst <= 8) {
   1944      1.1  christos             if (!TEST_int_gt(BIO_reset(mdbio), 0)
   1945      1.1  christos                 || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
   1946      1.1  christos                 goto out;
   1947      1.1  christos         }
   1948      1.1  christos 
   1949      1.1  christos         if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
   1950  1.1.1.2  christos                 NULL, pkey)))
   1951      1.1  christos             goto out;
   1952      1.1  christos 
   1953      1.1  christos         if (tst >= 6 && tst <= 8) {
   1954      1.1  christos             if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
   1955      1.1  christos                 goto out;
   1956      1.1  christos         } else {
   1957      1.1  christos             if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
   1958  1.1.1.2  christos                     sizeof(kMsg))))
   1959      1.1  christos                 goto out;
   1960      1.1  christos         }
   1961      1.1  christos         if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
   1962      1.1  christos             goto out;
   1963      1.1  christos 
   1964      1.1  christos         /* Multiple calls to EVP_DigestVerifyFinal should work */
   1965      1.1  christos         if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
   1966      1.1  christos             goto out;
   1967      1.1  christos     } else {
   1968      1.1  christos         /*
   1969      1.1  christos          * For HMAC a doubled call to DigestSignFinal should produce the same
   1970      1.1  christos          * value as finalization should not happen.
   1971      1.1  christos          */
   1972      1.1  christos         if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig2_len))
   1973      1.1  christos             || !TEST_ptr(sig2 = OPENSSL_malloc(sig2_len))
   1974      1.1  christos             || !TEST_true(EVP_DigestSignFinal(md_ctx, sig2, &sig2_len)))
   1975      1.1  christos             goto out;
   1976      1.1  christos 
   1977      1.1  christos         if (!TEST_mem_eq(sig, sig_len, sig2, sig2_len))
   1978      1.1  christos             goto out;
   1979      1.1  christos     }
   1980      1.1  christos 
   1981      1.1  christos     ret = 1;
   1982      1.1  christos 
   1983  1.1.1.2  christos out:
   1984      1.1  christos     BIO_free(membio);
   1985      1.1  christos     BIO_free(mdbio);
   1986      1.1  christos     EVP_MD_CTX_free(a_md_ctx);
   1987      1.1  christos     EVP_MD_CTX_free(a_md_ctx_verify);
   1988      1.1  christos     EVP_PKEY_free(pkey);
   1989      1.1  christos     OPENSSL_free(sig);
   1990      1.1  christos     OPENSSL_free(sig2);
   1991      1.1  christos     EVP_MD_free(mdexp);
   1992      1.1  christos 
   1993      1.1  christos     return ret;
   1994      1.1  christos }
   1995      1.1  christos 
   1996      1.1  christos static int test_EVP_DigestVerifyInit(void)
   1997      1.1  christos {
   1998      1.1  christos     int ret = 0;
   1999      1.1  christos     EVP_PKEY *pkey = NULL;
   2000      1.1  christos     EVP_MD_CTX *md_ctx = NULL;
   2001      1.1  christos 
   2002      1.1  christos     if (nullprov != NULL)
   2003      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   2004      1.1  christos 
   2005      1.1  christos     if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
   2006  1.1.1.2  christos         || !TEST_ptr(pkey = load_example_rsa_key()))
   2007      1.1  christos         goto out;
   2008      1.1  christos 
   2009      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
   2010  1.1.1.2  christos         || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
   2011  1.1.1.2  christos         || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
   2012  1.1.1.2  christos                             sizeof(kSignature)),
   2013  1.1.1.2  christos             0))
   2014      1.1  christos         goto out;
   2015      1.1  christos 
   2016      1.1  christos     /* test with reinitialization */
   2017      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, NULL))
   2018  1.1.1.2  christos         || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
   2019  1.1.1.2  christos         || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
   2020  1.1.1.2  christos                             sizeof(kSignature)),
   2021  1.1.1.2  christos             0))
   2022      1.1  christos         goto out;
   2023      1.1  christos     ret = 1;
   2024      1.1  christos 
   2025  1.1.1.2  christos out:
   2026      1.1  christos     EVP_MD_CTX_free(md_ctx);
   2027      1.1  christos     EVP_PKEY_free(pkey);
   2028      1.1  christos     return ret;
   2029      1.1  christos }
   2030      1.1  christos 
   2031      1.1  christos #ifndef OPENSSL_NO_EC
   2032      1.1  christos static int test_ecdsa_digestsign_keccak(void)
   2033      1.1  christos {
   2034      1.1  christos     int ret = 0;
   2035      1.1  christos     EVP_PKEY *pkey = NULL;
   2036      1.1  christos     EVP_MD_CTX *ctx = NULL;
   2037      1.1  christos     EVP_MD *md = NULL;
   2038      1.1  christos 
   2039      1.1  christos     if (nullprov != NULL)
   2040      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   2041      1.1  christos 
   2042      1.1  christos     pkey = load_example_ec_key();
   2043      1.1  christos     if (!TEST_ptr(pkey))
   2044      1.1  christos         goto err;
   2045      1.1  christos 
   2046      1.1  christos     /* This would not work with FIPS provider so just use NULL libctx */
   2047      1.1  christos     md = EVP_MD_fetch(NULL, "KECCAK-256", NULL);
   2048      1.1  christos     if (!TEST_ptr(md))
   2049      1.1  christos         goto err;
   2050      1.1  christos 
   2051      1.1  christos     ctx = EVP_MD_CTX_new();
   2052      1.1  christos     if (!TEST_ptr(ctx))
   2053      1.1  christos         goto err;
   2054      1.1  christos 
   2055      1.1  christos     /*
   2056      1.1  christos      * Just check EVP_DigestSignInit_ex() works.
   2057      1.1  christos      */
   2058      1.1  christos     if (!TEST_true(EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)))
   2059  1.1.1.2  christos         goto err;
   2060      1.1  christos 
   2061      1.1  christos     ret = 1;
   2062  1.1.1.2  christos err:
   2063      1.1  christos     EVP_MD_CTX_free(ctx);
   2064      1.1  christos     EVP_PKEY_free(pkey);
   2065      1.1  christos     EVP_MD_free(md);
   2066      1.1  christos 
   2067      1.1  christos     return ret;
   2068      1.1  christos }
   2069      1.1  christos #endif
   2070      1.1  christos 
   2071      1.1  christos #ifndef OPENSSL_NO_SIPHASH
   2072      1.1  christos /* test SIPHASH MAC via EVP_PKEY with non-default parameters and reinit */
   2073      1.1  christos static int test_siphash_digestsign(void)
   2074      1.1  christos {
   2075      1.1  christos     unsigned char key[16];
   2076      1.1  christos     unsigned char buf[8], digest[8];
   2077      1.1  christos     unsigned char expected[8] = {
   2078      1.1  christos         0x6d, 0x3e, 0x54, 0xc2, 0x2f, 0xf1, 0xfe, 0xe2
   2079      1.1  christos     };
   2080      1.1  christos     EVP_PKEY *pkey = NULL;
   2081      1.1  christos     EVP_MD_CTX *mdctx = NULL;
   2082      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   2083      1.1  christos     int ret = 0;
   2084      1.1  christos     size_t len = 8;
   2085      1.1  christos 
   2086      1.1  christos     if (nullprov != NULL)
   2087      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   2088      1.1  christos 
   2089      1.1  christos     memset(buf, 0, 8);
   2090      1.1  christos     memset(key, 1, 16);
   2091      1.1  christos     if (!TEST_ptr(pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_SIPHASH, NULL,
   2092  1.1.1.2  christos                       key, 16)))
   2093      1.1  christos         goto out;
   2094      1.1  christos 
   2095      1.1  christos     if (!TEST_ptr(mdctx = EVP_MD_CTX_create()))
   2096      1.1  christos         goto out;
   2097      1.1  christos 
   2098      1.1  christos     if (!TEST_true(EVP_DigestSignInit(mdctx, &ctx, NULL, NULL, pkey)))
   2099      1.1  christos         goto out;
   2100      1.1  christos     if (!TEST_int_eq(EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_SIGNCTX,
   2101  1.1.1.2  christos                          EVP_PKEY_CTRL_SET_DIGEST_SIZE,
   2102  1.1.1.2  christos                          8, NULL),
   2103  1.1.1.2  christos             1))
   2104      1.1  christos         goto out;
   2105      1.1  christos     /* reinitialize */
   2106      1.1  christos     if (!TEST_true(EVP_DigestSignInit(mdctx, NULL, NULL, NULL, NULL)))
   2107      1.1  christos         goto out;
   2108      1.1  christos     if (!TEST_true(EVP_DigestSignUpdate(mdctx, buf, 8)))
   2109      1.1  christos         goto out;
   2110      1.1  christos     if (!TEST_true(EVP_DigestSignFinal(mdctx, digest, &len)))
   2111      1.1  christos         goto out;
   2112      1.1  christos     if (!TEST_mem_eq(digest, len, expected, sizeof(expected)))
   2113      1.1  christos         goto out;
   2114      1.1  christos 
   2115      1.1  christos     ret = 1;
   2116  1.1.1.2  christos out:
   2117      1.1  christos     EVP_PKEY_free(pkey);
   2118      1.1  christos     EVP_MD_CTX_free(mdctx);
   2119      1.1  christos     return ret;
   2120      1.1  christos }
   2121      1.1  christos #endif
   2122      1.1  christos 
   2123      1.1  christos /*
   2124      1.1  christos  * Test corner cases of EVP_DigestInit/Update/Final API call behavior.
   2125      1.1  christos  */
   2126      1.1  christos static int test_EVP_Digest(void)
   2127      1.1  christos {
   2128      1.1  christos     int ret = 0;
   2129      1.1  christos     EVP_MD_CTX *md_ctx = NULL;
   2130      1.1  christos     unsigned char md[EVP_MAX_MD_SIZE];
   2131      1.1  christos     EVP_MD *sha256 = NULL;
   2132      1.1  christos     EVP_MD *shake256 = NULL;
   2133      1.1  christos 
   2134      1.1  christos     if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
   2135      1.1  christos         goto out;
   2136      1.1  christos 
   2137      1.1  christos     if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq))
   2138  1.1.1.2  christos         || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq)))
   2139      1.1  christos         goto out;
   2140      1.1  christos 
   2141      1.1  christos     if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
   2142  1.1.1.2  christos         || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
   2143  1.1.1.2  christos         || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL))
   2144  1.1.1.2  christos         /* EVP_DigestFinal resets the EVP_MD_CTX. */
   2145  1.1.1.2  christos         || !TEST_ptr_eq(EVP_MD_CTX_get0_md(md_ctx), NULL))
   2146      1.1  christos         goto out;
   2147      1.1  christos 
   2148      1.1  christos     if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
   2149  1.1.1.2  christos         || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
   2150  1.1.1.2  christos         || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL))
   2151  1.1.1.2  christos         /* EVP_DigestFinal_ex does not reset the EVP_MD_CTX. */
   2152  1.1.1.2  christos         || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
   2153  1.1.1.2  christos         /*
   2154  1.1.1.2  christos          * EVP_DigestInit_ex with NULL type should work on
   2155  1.1.1.2  christos          * pre-initialized context.
   2156  1.1.1.2  christos          */
   2157  1.1.1.2  christos         || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
   2158      1.1  christos         goto out;
   2159      1.1  christos 
   2160      1.1  christos     if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL))
   2161  1.1.1.2  christos         || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
   2162  1.1.1.2  christos         || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md)))
   2163  1.1.1.2  christos         /* EVP_DigestFinalXOF does not reset the EVP_MD_CTX. */
   2164  1.1.1.2  christos         || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
   2165  1.1.1.2  christos         || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
   2166      1.1  christos         goto out;
   2167      1.1  christos     ret = 1;
   2168      1.1  christos 
   2169  1.1.1.2  christos out:
   2170      1.1  christos     EVP_MD_CTX_free(md_ctx);
   2171      1.1  christos     EVP_MD_free(sha256);
   2172      1.1  christos     EVP_MD_free(shake256);
   2173      1.1  christos     return ret;
   2174      1.1  christos }
   2175      1.1  christos 
   2176      1.1  christos static int test_EVP_md_null(void)
   2177      1.1  christos {
   2178      1.1  christos     int ret = 0;
   2179      1.1  christos     EVP_MD_CTX *md_ctx = NULL;
   2180      1.1  christos     const EVP_MD *md_null = EVP_md_null();
   2181      1.1  christos     unsigned char md_value[EVP_MAX_MD_SIZE];
   2182      1.1  christos     unsigned int md_len = sizeof(md_value);
   2183      1.1  christos 
   2184      1.1  christos     if (nullprov != NULL)
   2185      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   2186      1.1  christos 
   2187      1.1  christos     if (!TEST_ptr(md_null)
   2188      1.1  christos         || !TEST_ptr(md_ctx = EVP_MD_CTX_new()))
   2189      1.1  christos         goto out;
   2190      1.1  christos 
   2191      1.1  christos     if (!TEST_true(EVP_DigestInit_ex(md_ctx, md_null, NULL))
   2192      1.1  christos         || !TEST_true(EVP_DigestUpdate(md_ctx, "test", 4))
   2193      1.1  christos         || !TEST_true(EVP_DigestFinal_ex(md_ctx, md_value, &md_len)))
   2194      1.1  christos         goto out;
   2195      1.1  christos 
   2196      1.1  christos     if (!TEST_uint_eq(md_len, 0))
   2197      1.1  christos         goto out;
   2198      1.1  christos 
   2199      1.1  christos     ret = 1;
   2200  1.1.1.2  christos out:
   2201      1.1  christos     EVP_MD_CTX_free(md_ctx);
   2202      1.1  christos     return ret;
   2203      1.1  christos }
   2204      1.1  christos 
   2205      1.1  christos static int test_d2i_AutoPrivateKey(int i)
   2206      1.1  christos {
   2207      1.1  christos     int ret = 0;
   2208      1.1  christos     const unsigned char *p;
   2209      1.1  christos     EVP_PKEY *pkey = NULL;
   2210      1.1  christos     const APK_DATA *ak = &keydata[i];
   2211      1.1  christos     const unsigned char *input = ak->kder;
   2212      1.1  christos     size_t input_len = ak->size;
   2213      1.1  christos     int expected_id = ak->evptype;
   2214      1.1  christos 
   2215      1.1  christos     p = input;
   2216      1.1  christos     if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
   2217  1.1.1.2  christos         || !TEST_ptr_eq(p, input + input_len)
   2218  1.1.1.2  christos         || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
   2219      1.1  christos         goto done;
   2220      1.1  christos 
   2221      1.1  christos     ret = 1;
   2222      1.1  christos 
   2223  1.1.1.2  christos done:
   2224      1.1  christos     EVP_PKEY_free(pkey);
   2225      1.1  christos     return ret;
   2226      1.1  christos }
   2227      1.1  christos 
   2228      1.1  christos #ifndef OPENSSL_NO_EC
   2229      1.1  christos 
   2230      1.1  christos static const unsigned char ec_public_sect163k1_validxy[] = {
   2231      1.1  christos     0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
   2232      1.1  christos     0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
   2233      1.1  christos     0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
   2234      1.1  christos     0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
   2235      1.1  christos     0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
   2236      1.1  christos     0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
   2237      1.1  christos };
   2238      1.1  christos 
   2239      1.1  christos static const unsigned char ec_public_sect163k1_badx[] = {
   2240      1.1  christos     0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
   2241      1.1  christos     0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
   2242      1.1  christos     0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
   2243      1.1  christos     0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
   2244      1.1  christos     0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
   2245      1.1  christos     0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
   2246      1.1  christos };
   2247      1.1  christos 
   2248      1.1  christos static const unsigned char ec_public_sect163k1_bady[] = {
   2249      1.1  christos     0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
   2250      1.1  christos     0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
   2251      1.1  christos     0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
   2252      1.1  christos     0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
   2253      1.1  christos     0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
   2254      1.1  christos     0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
   2255      1.1  christos };
   2256      1.1  christos 
   2257      1.1  christos static struct ec_der_pub_keys_st {
   2258      1.1  christos     const unsigned char *der;
   2259      1.1  christos     size_t len;
   2260      1.1  christos     int valid;
   2261      1.1  christos } ec_der_pub_keys[] = {
   2262      1.1  christos     { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
   2263      1.1  christos     { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
   2264      1.1  christos     { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
   2265      1.1  christos };
   2266      1.1  christos 
   2267      1.1  christos /*
   2268      1.1  christos  * Tests the range of the decoded EC char2 public point.
   2269      1.1  christos  * See ec_GF2m_simple_oct2point().
   2270      1.1  christos  */
   2271      1.1  christos static int test_invalide_ec_char2_pub_range_decode(int id)
   2272      1.1  christos {
   2273      1.1  christos     int ret = 0;
   2274      1.1  christos     EVP_PKEY *pkey;
   2275      1.1  christos 
   2276      1.1  christos     pkey = load_example_key("EC", ec_der_pub_keys[id].der,
   2277  1.1.1.2  christos         ec_der_pub_keys[id].len);
   2278      1.1  christos 
   2279      1.1  christos     ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey))
   2280  1.1.1.2  christos         || TEST_ptr_null(pkey);
   2281      1.1  christos     EVP_PKEY_free(pkey);
   2282      1.1  christos     return ret;
   2283      1.1  christos }
   2284      1.1  christos 
   2285      1.1  christos static int test_EVP_PKCS82PKEY(void)
   2286      1.1  christos {
   2287      1.1  christos     int ret = 0;
   2288      1.1  christos     const unsigned char *derp = kExampleBadECKeyDER;
   2289      1.1  christos     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
   2290      1.1  christos     EVP_PKEY *pkey = NULL;
   2291      1.1  christos 
   2292      1.1  christos     if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
   2293  1.1.1.2  christos                       sizeof(kExampleBadECKeyDER))))
   2294      1.1  christos         goto done;
   2295      1.1  christos 
   2296      1.1  christos     if (!TEST_ptr_eq(derp,
   2297  1.1.1.2  christos             kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
   2298      1.1  christos         goto done;
   2299      1.1  christos 
   2300      1.1  christos     if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
   2301      1.1  christos         goto done;
   2302      1.1  christos 
   2303      1.1  christos     ret = 1;
   2304      1.1  christos 
   2305  1.1.1.2  christos done:
   2306      1.1  christos     PKCS8_PRIV_KEY_INFO_free(p8inf);
   2307      1.1  christos     EVP_PKEY_free(pkey);
   2308      1.1  christos 
   2309      1.1  christos     return ret;
   2310      1.1  christos }
   2311      1.1  christos 
   2312      1.1  christos #endif
   2313      1.1  christos 
   2314      1.1  christos static int test_EVP_PKCS82PKEY_v2(int i)
   2315      1.1  christos {
   2316      1.1  christos     int ret = 0;
   2317      1.1  christos     const unsigned char *p;
   2318      1.1  christos     const APK_DATA *ak = &keydata_v2[i];
   2319      1.1  christos     const unsigned char *input = ak->kder;
   2320      1.1  christos     size_t input_len = ak->size;
   2321      1.1  christos     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
   2322      1.1  christos 
   2323      1.1  christos     /* Can we parse PKCS#8 v2, ignoring the public key for now? */
   2324      1.1  christos     p = input;
   2325      1.1  christos     p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, input_len);
   2326      1.1  christos     if (!TEST_ptr(p8inf)
   2327      1.1  christos         || !TEST_true(p == input + input_len))
   2328      1.1  christos         goto done;
   2329      1.1  christos 
   2330      1.1  christos     ret = 1;
   2331  1.1.1.2  christos done:
   2332      1.1  christos     PKCS8_PRIV_KEY_INFO_free(p8inf);
   2333      1.1  christos     return ret;
   2334      1.1  christos }
   2335      1.1  christos 
   2336      1.1  christos /* Tests loading a bad key in PKCS8 format */
   2337      1.1  christos static int test_EVP_PKCS82PKEY_wrong_tag(void)
   2338      1.1  christos {
   2339      1.1  christos     EVP_PKEY *pkey = NULL;
   2340      1.1  christos     EVP_PKEY *pkey2 = NULL;
   2341      1.1  christos     BIO *membio = NULL;
   2342      1.1  christos     char *membuf = NULL;
   2343      1.1  christos     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
   2344      1.1  christos     int ok = 0;
   2345      1.1  christos 
   2346      1.1  christos     if (testctx != NULL)
   2347      1.1  christos         /* test not supported with non-default context */
   2348      1.1  christos         return 1;
   2349      1.1  christos 
   2350      1.1  christos     if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
   2351      1.1  christos         || !TEST_ptr(pkey = load_example_rsa_key())
   2352      1.1  christos         || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
   2353  1.1.1.2  christos                             NULL, 0, NULL, NULL),
   2354  1.1.1.2  christos             0)
   2355      1.1  christos         || !TEST_int_gt(BIO_get_mem_data(membio, &membuf), 0)
   2356      1.1  christos         || !TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(membio, NULL))
   2357      1.1  christos         || !TEST_ptr(pkey2 = EVP_PKCS82PKEY(p8inf))
   2358      1.1  christos         || !TEST_int_eq(ERR_peek_last_error(), 0)) {
   2359      1.1  christos         goto done;
   2360      1.1  christos     }
   2361      1.1  christos 
   2362      1.1  christos     ok = 1;
   2363  1.1.1.2  christos done:
   2364      1.1  christos     EVP_PKEY_free(pkey);
   2365      1.1  christos     EVP_PKEY_free(pkey2);
   2366      1.1  christos     PKCS8_PRIV_KEY_INFO_free(p8inf);
   2367      1.1  christos     BIO_free_all(membio);
   2368      1.1  christos     return ok;
   2369      1.1  christos }
   2370      1.1  christos 
   2371      1.1  christos /* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
   2372      1.1  christos static int test_privatekey_to_pkcs8(void)
   2373      1.1  christos {
   2374      1.1  christos     EVP_PKEY *pkey = NULL;
   2375      1.1  christos     BIO *membio = NULL;
   2376      1.1  christos     char *membuf = NULL;
   2377      1.1  christos     long membuf_len = 0;
   2378      1.1  christos     int ok = 0;
   2379      1.1  christos 
   2380      1.1  christos     if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
   2381      1.1  christos         || !TEST_ptr(pkey = load_example_rsa_key())
   2382      1.1  christos         || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
   2383  1.1.1.2  christos                             NULL, 0, NULL, NULL),
   2384  1.1.1.2  christos             0)
   2385      1.1  christos         || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
   2386      1.1  christos         || !TEST_ptr(membuf)
   2387      1.1  christos         || !TEST_mem_eq(membuf, (size_t)membuf_len,
   2388  1.1.1.2  christos             kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
   2389      1.1  christos         /*
   2390      1.1  christos          * We try to write PEM as well, just to see that it doesn't err, but
   2391      1.1  christos          * assume that the result is correct.
   2392      1.1  christos          */
   2393      1.1  christos         || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
   2394  1.1.1.2  christos                             NULL, 0, NULL, NULL),
   2395  1.1.1.2  christos             0))
   2396      1.1  christos         goto done;
   2397      1.1  christos 
   2398      1.1  christos     ok = 1;
   2399  1.1.1.2  christos done:
   2400      1.1  christos     EVP_PKEY_free(pkey);
   2401      1.1  christos     BIO_free_all(membio);
   2402      1.1  christos     return ok;
   2403      1.1  christos }
   2404      1.1  christos 
   2405      1.1  christos #ifndef OPENSSL_NO_EC
   2406      1.1  christos static const struct {
   2407      1.1  christos     int encoding;
   2408      1.1  christos     const char *encoding_name;
   2409      1.1  christos } ec_encodings[] = {
   2410      1.1  christos     { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
   2411  1.1.1.2  christos     { OPENSSL_EC_NAMED_CURVE, OSSL_PKEY_EC_ENCODING_GROUP }
   2412      1.1  christos };
   2413      1.1  christos 
   2414      1.1  christos static int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
   2415      1.1  christos {
   2416      1.1  christos     const OSSL_PARAM *p;
   2417      1.1  christos     const char *enc_name = NULL;
   2418      1.1  christos     int *enc = arg;
   2419      1.1  christos     size_t i;
   2420      1.1  christos 
   2421      1.1  christos     *enc = -1;
   2422      1.1  christos 
   2423      1.1  christos     if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
   2424  1.1.1.2  christos                       OSSL_PKEY_PARAM_EC_ENCODING))
   2425      1.1  christos         || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
   2426      1.1  christos         return 0;
   2427      1.1  christos 
   2428      1.1  christos     for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
   2429      1.1  christos         if (OPENSSL_strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
   2430      1.1  christos             *enc = ec_encodings[i].encoding;
   2431      1.1  christos             break;
   2432      1.1  christos         }
   2433      1.1  christos     }
   2434      1.1  christos 
   2435      1.1  christos     return (*enc != -1);
   2436      1.1  christos }
   2437      1.1  christos 
   2438      1.1  christos static int test_EC_keygen_with_enc(int idx)
   2439      1.1  christos {
   2440      1.1  christos     EVP_PKEY *params = NULL, *key = NULL;
   2441      1.1  christos     EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
   2442      1.1  christos     int enc;
   2443      1.1  christos     int ret = 0;
   2444      1.1  christos 
   2445      1.1  christos     enc = ec_encodings[idx].encoding;
   2446      1.1  christos 
   2447      1.1  christos     /* Create key parameters */
   2448      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
   2449      1.1  christos         || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
   2450      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_group_name(pctx, "P-256"), 0)
   2451      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc), 0)
   2452      1.1  christos         || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
   2453      1.1  christos         || !TEST_ptr(params))
   2454      1.1  christos         goto done;
   2455      1.1  christos 
   2456      1.1  christos     /* Create key */
   2457      1.1  christos     if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
   2458      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
   2459      1.1  christos         || !TEST_true(EVP_PKEY_keygen(kctx, &key))
   2460      1.1  christos         || !TEST_ptr(key))
   2461      1.1  christos         goto done;
   2462      1.1  christos 
   2463      1.1  christos     /* Check that the encoding got all the way into the key */
   2464      1.1  christos     if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
   2465  1.1.1.2  christos             ec_export_get_encoding_cb, &enc))
   2466      1.1  christos         || !TEST_int_eq(enc, ec_encodings[idx].encoding))
   2467      1.1  christos         goto done;
   2468      1.1  christos 
   2469      1.1  christos     ret = 1;
   2470  1.1.1.2  christos done:
   2471      1.1  christos     EVP_PKEY_free(key);
   2472      1.1  christos     EVP_PKEY_free(params);
   2473      1.1  christos     EVP_PKEY_CTX_free(kctx);
   2474      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2475      1.1  christos     return ret;
   2476      1.1  christos }
   2477      1.1  christos #endif
   2478      1.1  christos 
   2479      1.1  christos #if !defined(OPENSSL_NO_SM2)
   2480      1.1  christos 
   2481      1.1  christos static int test_EVP_SM2_verify(void)
   2482      1.1  christos {
   2483  1.1.1.2  christos     const char *pubkey = "-----BEGIN PUBLIC KEY-----\n"
   2484  1.1.1.2  christos                          "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
   2485  1.1.1.2  christos                          "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
   2486  1.1.1.2  christos                          "-----END PUBLIC KEY-----\n";
   2487      1.1  christos 
   2488      1.1  christos     const char *msg = "message digest";
   2489      1.1  christos     const char *id = "ALICE123 (at) YAHOO.COM";
   2490      1.1  christos 
   2491      1.1  christos     const uint8_t signature[] = {
   2492      1.1  christos         0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
   2493      1.1  christos         0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
   2494      1.1  christos         0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
   2495      1.1  christos         0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
   2496      1.1  christos         0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
   2497      1.1  christos         0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
   2498      1.1  christos         0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
   2499      1.1  christos     };
   2500      1.1  christos 
   2501      1.1  christos     int rc = 0;
   2502      1.1  christos     BIO *bio = NULL;
   2503      1.1  christos     EVP_PKEY *pkey = NULL;
   2504      1.1  christos     EVP_MD_CTX *mctx = NULL;
   2505      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   2506      1.1  christos     EVP_MD *sm3 = NULL;
   2507      1.1  christos 
   2508      1.1  christos     bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
   2509      1.1  christos     if (!TEST_true(bio != NULL))
   2510      1.1  christos         goto done;
   2511      1.1  christos 
   2512      1.1  christos     pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq);
   2513      1.1  christos     if (!TEST_true(pkey != NULL))
   2514      1.1  christos         goto done;
   2515      1.1  christos 
   2516      1.1  christos     if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
   2517      1.1  christos         goto done;
   2518      1.1  christos 
   2519      1.1  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
   2520      1.1  christos         goto done;
   2521      1.1  christos 
   2522      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
   2523      1.1  christos         goto done;
   2524      1.1  christos 
   2525      1.1  christos     EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
   2526      1.1  christos 
   2527      1.1  christos     if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
   2528      1.1  christos         goto done;
   2529      1.1  christos 
   2530      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey)))
   2531      1.1  christos         goto done;
   2532      1.1  christos 
   2533      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
   2534      1.1  christos         goto done;
   2535      1.1  christos 
   2536      1.1  christos     if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
   2537      1.1  christos         goto done;
   2538      1.1  christos 
   2539      1.1  christos     if (!TEST_int_gt(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature)), 0))
   2540      1.1  christos         goto done;
   2541      1.1  christos     rc = 1;
   2542      1.1  christos 
   2543  1.1.1.2  christos done:
   2544      1.1  christos     BIO_free(bio);
   2545      1.1  christos     EVP_PKEY_free(pkey);
   2546      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2547      1.1  christos     EVP_MD_CTX_free(mctx);
   2548      1.1  christos     EVP_MD_free(sm3);
   2549      1.1  christos     return rc;
   2550      1.1  christos }
   2551      1.1  christos 
   2552      1.1  christos static int test_EVP_SM2(void)
   2553      1.1  christos {
   2554      1.1  christos     int ret = 0;
   2555      1.1  christos     EVP_PKEY *pkey = NULL;
   2556      1.1  christos     EVP_PKEY *pkeyparams = NULL;
   2557      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   2558      1.1  christos     EVP_PKEY_CTX *kctx = NULL;
   2559      1.1  christos     EVP_PKEY_CTX *sctx = NULL;
   2560      1.1  christos     size_t sig_len = 0;
   2561      1.1  christos     unsigned char *sig = NULL;
   2562      1.1  christos     EVP_MD_CTX *md_ctx = NULL;
   2563      1.1  christos     EVP_MD_CTX *md_ctx_verify = NULL;
   2564      1.1  christos     EVP_PKEY_CTX *cctx = NULL;
   2565      1.1  christos     EVP_MD *check_md = NULL;
   2566      1.1  christos 
   2567      1.1  christos     uint8_t ciphertext[128];
   2568      1.1  christos     size_t ctext_len = sizeof(ciphertext);
   2569      1.1  christos 
   2570      1.1  christos     uint8_t plaintext[8];
   2571      1.1  christos     size_t ptext_len = sizeof(plaintext);
   2572      1.1  christos 
   2573  1.1.1.2  christos     uint8_t sm2_id[] = { 1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r' };
   2574      1.1  christos 
   2575  1.1.1.2  christos     OSSL_PARAM sparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   2576  1.1.1.2  christos     OSSL_PARAM gparams[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   2577      1.1  christos     int i;
   2578      1.1  christos     char mdname[OSSL_MAX_NAME_SIZE];
   2579      1.1  christos 
   2580      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx,
   2581  1.1.1.2  christos                       "SM2", testpropq)))
   2582      1.1  christos         goto done;
   2583      1.1  christos 
   2584      1.1  christos     if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
   2585      1.1  christos         goto done;
   2586      1.1  christos 
   2587      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2), 0))
   2588      1.1  christos         goto done;
   2589      1.1  christos 
   2590      1.1  christos     if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
   2591      1.1  christos         goto done;
   2592      1.1  christos 
   2593      1.1  christos     if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx,
   2594  1.1.1.2  christos                       pkeyparams, testpropq)))
   2595      1.1  christos         goto done;
   2596      1.1  christos 
   2597      1.1  christos     if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0))
   2598      1.1  christos         goto done;
   2599      1.1  christos 
   2600      1.1  christos     if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
   2601      1.1  christos         goto done;
   2602      1.1  christos 
   2603      1.1  christos     if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
   2604      1.1  christos         goto done;
   2605      1.1  christos 
   2606      1.1  christos     if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
   2607      1.1  christos         goto done;
   2608      1.1  christos 
   2609      1.1  christos     if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
   2610      1.1  christos         goto done;
   2611      1.1  christos 
   2612      1.1  christos     EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
   2613      1.1  christos     EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
   2614      1.1  christos 
   2615      1.1  christos     if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, "sm3", testpropq)))
   2616      1.1  christos         goto done;
   2617      1.1  christos 
   2618      1.1  christos     if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey)))
   2619      1.1  christos         goto done;
   2620      1.1  christos 
   2621      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
   2622      1.1  christos         goto done;
   2623      1.1  christos 
   2624      1.1  christos     if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
   2625      1.1  christos         goto done;
   2626      1.1  christos 
   2627      1.1  christos     /* Determine the size of the signature. */
   2628      1.1  christos     if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
   2629      1.1  christos         goto done;
   2630      1.1  christos 
   2631      1.1  christos     if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
   2632      1.1  christos         goto done;
   2633      1.1  christos 
   2634      1.1  christos     if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
   2635      1.1  christos         goto done;
   2636      1.1  christos 
   2637      1.1  christos     /* Ensure that the signature round-trips. */
   2638      1.1  christos 
   2639      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
   2640  1.1.1.2  christos             pkey)))
   2641      1.1  christos         goto done;
   2642      1.1  christos 
   2643      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
   2644      1.1  christos         goto done;
   2645      1.1  christos 
   2646      1.1  christos     if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
   2647      1.1  christos         goto done;
   2648      1.1  christos 
   2649      1.1  christos     if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
   2650      1.1  christos         goto done;
   2651      1.1  christos 
   2652      1.1  christos     /*
   2653      1.1  christos      * Try verify again with non-matching 0 length id but ensure that it can
   2654      1.1  christos      * be set on the context and overrides the previous value.
   2655      1.1  christos      */
   2656      1.1  christos 
   2657      1.1  christos     if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
   2658  1.1.1.2  christos             pkey)))
   2659      1.1  christos         goto done;
   2660      1.1  christos 
   2661      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, NULL, 0), 0))
   2662      1.1  christos         goto done;
   2663      1.1  christos 
   2664      1.1  christos     if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
   2665      1.1  christos         goto done;
   2666      1.1  christos 
   2667      1.1  christos     if (!TEST_int_eq(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
   2668      1.1  christos         goto done;
   2669      1.1  christos 
   2670      1.1  christos     /* now check encryption/decryption */
   2671      1.1  christos 
   2672      1.1  christos     gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
   2673  1.1.1.2  christos         mdname, sizeof(mdname));
   2674      1.1  christos     for (i = 0; i < 2; i++) {
   2675      1.1  christos         const char *mdnames[] = {
   2676      1.1  christos #ifndef OPENSSL_NO_SM3
   2677      1.1  christos             "SM3",
   2678      1.1  christos #else
   2679      1.1  christos             NULL,
   2680      1.1  christos #endif
   2681  1.1.1.2  christos             "SHA2-256"
   2682  1.1.1.2  christos         };
   2683      1.1  christos         EVP_PKEY_CTX_free(cctx);
   2684      1.1  christos 
   2685      1.1  christos         if (mdnames[i] == NULL)
   2686      1.1  christos             continue;
   2687      1.1  christos 
   2688  1.1.1.2  christos         sparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
   2689  1.1.1.2  christos             (char *)mdnames[i], 0);
   2690      1.1  christos 
   2691      1.1  christos         if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx,
   2692  1.1.1.2  christos                           pkey, testpropq)))
   2693      1.1  christos             goto done;
   2694      1.1  christos 
   2695      1.1  christos         if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
   2696      1.1  christos             goto done;
   2697      1.1  christos 
   2698      1.1  christos         if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
   2699      1.1  christos             goto done;
   2700      1.1  christos 
   2701      1.1  christos         if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
   2702  1.1.1.2  christos                 sizeof(kMsg))))
   2703      1.1  christos             goto done;
   2704      1.1  christos 
   2705      1.1  christos         if (!TEST_int_gt(EVP_PKEY_decrypt_init(cctx), 0))
   2706      1.1  christos             goto done;
   2707      1.1  christos 
   2708      1.1  christos         if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
   2709      1.1  christos             goto done;
   2710      1.1  christos 
   2711      1.1  christos         if (!TEST_int_gt(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
   2712  1.1.1.2  christos                              ctext_len),
   2713  1.1.1.2  christos                 0))
   2714      1.1  christos             goto done;
   2715      1.1  christos 
   2716      1.1  christos         if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
   2717      1.1  christos             goto done;
   2718      1.1  christos 
   2719      1.1  christos         /*
   2720      1.1  christos          * Test we're still using the digest we think we are.
   2721      1.1  christos          * Because of aliases, the easiest is to fetch the digest and
   2722      1.1  christos          * check the name with EVP_MD_is_a().
   2723      1.1  christos          */
   2724      1.1  christos         EVP_MD_free(check_md);
   2725      1.1  christos         if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, mdname, testpropq)))
   2726      1.1  christos             goto done;
   2727      1.1  christos         if (!TEST_true(EVP_MD_is_a(check_md, mdnames[i]))) {
   2728      1.1  christos             TEST_info("Fetched md %s isn't %s", mdname, mdnames[i]);
   2729      1.1  christos             goto done;
   2730      1.1  christos         }
   2731      1.1  christos 
   2732      1.1  christos         if (!TEST_true(ptext_len == sizeof(kMsg)))
   2733      1.1  christos             goto done;
   2734      1.1  christos 
   2735      1.1  christos         if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
   2736      1.1  christos             goto done;
   2737      1.1  christos     }
   2738      1.1  christos 
   2739      1.1  christos     ret = 1;
   2740      1.1  christos done:
   2741      1.1  christos     EVP_PKEY_CTX_free(pctx);
   2742      1.1  christos     EVP_PKEY_CTX_free(kctx);
   2743      1.1  christos     EVP_PKEY_CTX_free(sctx);
   2744      1.1  christos     EVP_PKEY_CTX_free(cctx);
   2745      1.1  christos     EVP_PKEY_free(pkey);
   2746      1.1  christos     EVP_PKEY_free(pkeyparams);
   2747      1.1  christos     EVP_MD_CTX_free(md_ctx);
   2748      1.1  christos     EVP_MD_CTX_free(md_ctx_verify);
   2749      1.1  christos     EVP_MD_free(check_md);
   2750      1.1  christos     OPENSSL_free(sig);
   2751      1.1  christos     return ret;
   2752      1.1  christos }
   2753      1.1  christos 
   2754      1.1  christos #endif
   2755      1.1  christos 
   2756      1.1  christos #ifndef OPENSSL_NO_ML_KEM
   2757      1.1  christos static const uint8_t ml_kem_seed[] = {
   2758  1.1.1.2  christos     0x7c, 0x99, 0x35, 0xa0, 0xb0, 0x76, 0x94, 0xaa, 0x0c, 0x6d, 0x10, 0xe4,
   2759  1.1.1.2  christos     0xdb, 0x6b, 0x1a, 0xdd, 0x2f, 0xd8, 0x1a, 0x25, 0xcc, 0xb1, 0x48, 0x03,
   2760  1.1.1.2  christos     0x2d, 0xcd, 0x73, 0x99, 0x36, 0x73, 0x7f, 0x2d, 0x86, 0x26, 0xed, 0x79,
   2761  1.1.1.2  christos     0xd4, 0x51, 0x14, 0x08, 0x00, 0xe0, 0x3b, 0x59, 0xb9, 0x56, 0xf8, 0x21,
   2762  1.1.1.2  christos     0x0e, 0x55, 0x60, 0x67, 0x40, 0x7d, 0x13, 0xdc, 0x90, 0xfa, 0x9e, 0x8b,
   2763  1.1.1.2  christos     0x87, 0x2b, 0xfb, 0x8f
   2764      1.1  christos };
   2765      1.1  christos static const uint8_t ml_kem_512_pubkey[] = {
   2766  1.1.1.2  christos     0x40, 0x08, 0x65, 0xed, 0x10, 0xb6, 0x19, 0xaa, 0x58, 0x11, 0x13, 0x9b,
   2767  1.1.1.2  christos     0xc0, 0x86, 0x82, 0x57, 0x82, 0xb2, 0xb7, 0x12, 0x4f, 0x75, 0x7c, 0x83,
   2768  1.1.1.2  christos     0xae, 0x79, 0x44, 0x44, 0xbc, 0x78, 0xa4, 0x78, 0x96, 0xac, 0xf1, 0x26,
   2769  1.1.1.2  christos     0x2c, 0x81, 0x35, 0x10, 0x77, 0x89, 0x3b, 0xfc, 0x56, 0xf9, 0x04, 0x49,
   2770  1.1.1.2  christos     0xc2, 0xfa, 0x5f, 0x6e, 0x58, 0x6d, 0xd3, 0x7c, 0x0b, 0x9b, 0x58, 0x19,
   2771  1.1.1.2  christos     0x92, 0x63, 0x8c, 0xb7, 0xe7, 0xbc, 0xbb, 0xb9, 0x9a, 0xfe, 0x47, 0x81,
   2772  1.1.1.2  christos     0xd8, 0x0a, 0x50, 0xe6, 0x94, 0x63, 0xfb, 0xd9, 0x88, 0x72, 0x2c, 0x36,
   2773  1.1.1.2  christos     0x35, 0x42, 0x3e, 0x27, 0x46, 0x6c, 0x71, 0xdc, 0xc6, 0x74, 0x52, 0x7c,
   2774  1.1.1.2  christos     0xcd, 0x72, 0x89, 0x68, 0xcb, 0xcd, 0xc0, 0x0c, 0x5c, 0x90, 0x35, 0xbb,
   2775  1.1.1.2  christos     0x0a, 0xf2, 0xc9, 0x92, 0x2c, 0x78, 0x81, 0xa4, 0x1d, 0xd2, 0x87, 0x52,
   2776  1.1.1.2  christos     0x73, 0x92, 0x51, 0x31, 0x23, 0x0f, 0x6c, 0xa5, 0x9e, 0x91, 0x36, 0xb3,
   2777  1.1.1.2  christos     0x9f, 0x95, 0x6c, 0x93, 0xb3, 0xb2, 0xd1, 0x4c, 0x64, 0x1b, 0x08, 0x9e,
   2778  1.1.1.2  christos     0x07, 0xd0, 0xa8, 0x40, 0xc8, 0x93, 0xec, 0xd7, 0x6b, 0xbf, 0x92, 0xc8,
   2779  1.1.1.2  christos     0x05, 0x45, 0x66, 0x68, 0xd0, 0x7c, 0x62, 0x14, 0x91, 0xc5, 0xc0, 0x54,
   2780  1.1.1.2  christos     0x99, 0x1a, 0x65, 0x6f, 0x51, 0x16, 0x19, 0x55, 0x6e, 0xb9, 0x77, 0x82,
   2781  1.1.1.2  christos     0xe2, 0x7a, 0x3c, 0x78, 0x51, 0x24, 0xc7, 0x0b, 0x0d, 0xab, 0xa6, 0xc6,
   2782  1.1.1.2  christos     0x24, 0xd1, 0x8e, 0x0f, 0x97, 0x93, 0xf9, 0x6b, 0xa9, 0xe1, 0x59, 0x9b,
   2783  1.1.1.2  christos     0x17, 0xb3, 0x0d, 0xcc, 0xc0, 0xb4, 0xf3, 0x76, 0x6a, 0x07, 0xb2, 0x3b,
   2784  1.1.1.2  christos     0x25, 0x73, 0x09, 0xcd, 0x76, 0xab, 0xa0, 0x72, 0xc2, 0xb9, 0xc9, 0x74,
   2785  1.1.1.2  christos     0x43, 0x94, 0xc6, 0xab, 0x9c, 0xb6, 0xc5, 0x4a, 0x97, 0xb5, 0xc5, 0x78,
   2786  1.1.1.2  christos     0x61, 0xa5, 0x8d, 0xc0, 0xa0, 0x35, 0x19, 0x83, 0x2e, 0xe3, 0x2a, 0x07,
   2787  1.1.1.2  christos     0x65, 0x4a, 0x07, 0x0c, 0x0c, 0x8c, 0x4e, 0x86, 0x48, 0xad, 0xdc, 0x35,
   2788  1.1.1.2  christos     0x5f, 0x27, 0x4f, 0xc6, 0xb9, 0x2a, 0x08, 0x7b, 0x3f, 0x97, 0x51, 0x92,
   2789  1.1.1.2  christos     0x3e, 0x44, 0x27, 0x4f, 0x85, 0x8c, 0x49, 0xca, 0xba, 0x72, 0xb6, 0x58,
   2790  1.1.1.2  christos     0x51, 0xb3, 0xad, 0xc4, 0x89, 0x36, 0x95, 0x50, 0x97, 0xca, 0xd9, 0x55,
   2791  1.1.1.2  christos     0x3f, 0x5a, 0x26, 0x3f, 0x18, 0x44, 0xb5, 0x2a, 0x02, 0x0f, 0xf7, 0xca,
   2792  1.1.1.2  christos     0x89, 0xe8, 0x81, 0xa0, 0x1b, 0x95, 0xd9, 0x57, 0xa3, 0x15, 0x3c, 0x0a,
   2793  1.1.1.2  christos     0x5e, 0x0a, 0x1c, 0xcd, 0x66, 0xb1, 0x82, 0x1a, 0x2b, 0x86, 0x32, 0x54,
   2794  1.1.1.2  christos     0x6e, 0x24, 0xc7, 0xcb, 0xbc, 0x4c, 0xb0, 0x88, 0x08, 0xca, 0xc3, 0x7f,
   2795  1.1.1.2  christos     0x7d, 0xa6, 0xb1, 0x6f, 0x8a, 0xce, 0xd0, 0x52, 0xcd, 0xb2, 0x56, 0x49,
   2796  1.1.1.2  christos     0x48, 0xf1, 0xab, 0x0f, 0x76, 0x8a, 0x0d, 0x32, 0x86, 0xcc, 0xc7, 0xc3,
   2797  1.1.1.2  christos     0x74, 0x9c, 0x63, 0xc7, 0x81, 0x53, 0x0f, 0xa1, 0xae, 0x67, 0x05, 0x42,
   2798  1.1.1.2  christos     0x85, 0x50, 0x04, 0xa6, 0x45, 0xb5, 0x22, 0x88, 0x1e, 0xc1, 0x41, 0x2b,
   2799  1.1.1.2  christos     0xda, 0xe3, 0x42, 0x08, 0x5a, 0x9d, 0xd5, 0xf8, 0x12, 0x6a, 0xf9, 0x6b,
   2800  1.1.1.2  christos     0xbd, 0xb0, 0xc1, 0xaf, 0x69, 0xa1, 0x55, 0x62, 0xcb, 0x2a, 0x15, 0x5a,
   2801  1.1.1.2  christos     0x10, 0x03, 0x09, 0xd1, 0xb6, 0x41, 0xd0, 0x8b, 0x2d, 0x4e, 0xd1, 0x7b,
   2802  1.1.1.2  christos     0xfb, 0xf0, 0xbc, 0x04, 0x26, 0x5f, 0x9b, 0x10, 0xc1, 0x08, 0xf8, 0x50,
   2803  1.1.1.2  christos     0x30, 0x95, 0x04, 0xd7, 0x72, 0x81, 0x1b, 0xba, 0x8e, 0x2b, 0xe1, 0x62,
   2804  1.1.1.2  christos     0x49, 0xaa, 0x73, 0x7d, 0x87, 0x9f, 0xc7, 0xfb, 0x25, 0x5e, 0xe7, 0xa6,
   2805  1.1.1.2  christos     0xa0, 0xa7, 0x53, 0xbd, 0x93, 0x74, 0x1c, 0x61, 0x65, 0x8e, 0xc0, 0x74,
   2806  1.1.1.2  christos     0xf6, 0xe0, 0x02, 0xb0, 0x19, 0x34, 0x57, 0x69, 0x11, 0x3c, 0xc0, 0x13,
   2807  1.1.1.2  christos     0xff, 0x74, 0x94, 0xba, 0x83, 0x78, 0xb1, 0x1a, 0x17, 0x22, 0x60, 0xaa,
   2808  1.1.1.2  christos     0xa5, 0x34, 0x21, 0xbd, 0xe0, 0x3a, 0x35, 0x58, 0x9d, 0x57, 0xe3, 0x22,
   2809  1.1.1.2  christos     0xfe, 0xfa, 0x41, 0x00, 0xa4, 0x74, 0x39, 0x26, 0xab, 0x7d, 0x62, 0x25,
   2810  1.1.1.2  christos     0x8b, 0x87, 0xb3, 0x1c, 0xcb, 0xb5, 0xe6, 0xb8, 0x9c, 0xb1, 0x0b, 0x27,
   2811  1.1.1.2  christos     0x1a, 0xa0, 0x5d, 0x99, 0x4b, 0xb5, 0x70, 0x8b, 0x23, 0xab, 0x32, 0x7e,
   2812  1.1.1.2  christos     0xcb, 0x93, 0xc0, 0xf3, 0x15, 0x68, 0x69, 0xf0, 0x88, 0x3d, 0xa2, 0x06,
   2813  1.1.1.2  christos     0x4f, 0x79, 0x5e, 0x0e, 0x2a, 0xb7, 0xd3, 0xc6, 0x4d, 0x61, 0xd2, 0x30,
   2814  1.1.1.2  christos     0x3f, 0xc3, 0xa2, 0x9e, 0x16, 0x19, 0x92, 0x3c, 0xa8, 0x01, 0xe5, 0x9f,
   2815  1.1.1.2  christos     0xd7, 0x52, 0xca, 0x6e, 0x76, 0x49, 0xd3, 0x03, 0xc9, 0xd2, 0x07, 0x88,
   2816  1.1.1.2  christos     0xe1, 0x21, 0x46, 0x51, 0xb0, 0x69, 0x95, 0xeb, 0x26, 0x0c, 0x92, 0x9a,
   2817  1.1.1.2  christos     0x13, 0x44, 0xa8, 0x49, 0xb2, 0x5c, 0xa0, 0xa0, 0x1f, 0x1e, 0xb5, 0x29,
   2818  1.1.1.2  christos     0x13, 0x68, 0x6b, 0xba, 0x61, 0x9e, 0x23, 0x71, 0x44, 0x64, 0x03, 0x1a,
   2819  1.1.1.2  christos     0x78, 0x43, 0x92, 0x87, 0xfc, 0xa7, 0x8f, 0x4c, 0x04, 0x76, 0x22, 0x3e,
   2820  1.1.1.2  christos     0xea, 0x61, 0xb7, 0xf2, 0x5a, 0x7c, 0xe4, 0x2c, 0xca, 0x90, 0x1b, 0x2a,
   2821  1.1.1.2  christos     0xea, 0x12, 0x98, 0x17, 0x89, 0x4b, 0xa3, 0x47, 0x08, 0x23, 0x85, 0x4f,
   2822  1.1.1.2  christos     0x3e, 0x5b, 0x28, 0xd8, 0x6b, 0xa9, 0x79, 0xe5, 0x46, 0x71, 0x86, 0x2d,
   2823  1.1.1.2  christos     0x90, 0x47, 0x0b, 0x1e, 0x78, 0x38, 0x97, 0x2a, 0x81, 0xa4, 0x81, 0x07,
   2824  1.1.1.2  christos     0xd6, 0xac, 0x06, 0x11, 0x40, 0x6b, 0x21, 0xfb, 0xcc, 0xe1, 0xdb, 0x77,
   2825  1.1.1.2  christos     0x02, 0xea, 0x9d, 0xd6, 0xba, 0x6e, 0x40, 0x52, 0x7b, 0x9d, 0xc6, 0x63,
   2826  1.1.1.2  christos     0xf3, 0xc9, 0x3b, 0xad, 0x05, 0x6d, 0xc2, 0x85, 0x11, 0xf6, 0x6c, 0x3e,
   2827  1.1.1.2  christos     0x0b, 0x92, 0x8d, 0xb8, 0x87, 0x9d, 0x22, 0xc5, 0x92, 0x68, 0x5c, 0xc7,
   2828  1.1.1.2  christos     0x75, 0xa6, 0xcd, 0x57, 0x4a, 0xc3, 0xbc, 0xe3, 0xb2, 0x75, 0x91, 0xc8,
   2829  1.1.1.2  christos     0x21, 0x92, 0x90, 0x76, 0x35, 0x8a, 0x22, 0x00, 0xb3, 0x77, 0x36, 0x5f,
   2830  1.1.1.2  christos     0x7e, 0xfb, 0x9e, 0x40, 0xc3, 0xbf, 0x0f, 0xf0, 0x43, 0x29, 0x86, 0xae,
   2831  1.1.1.2  christos     0x4b, 0xc1, 0xa2, 0x42, 0xce, 0x99, 0x21, 0xaa, 0x9e, 0x22, 0x44, 0x88,
   2832  1.1.1.2  christos     0x19, 0x58, 0x5d, 0xea, 0x30, 0x8e, 0xb0, 0x39
   2833      1.1  christos };
   2834      1.1  christos static const uint8_t ml_kem_768_pubkey[] = {
   2835  1.1.1.2  christos     0xa8, 0xe6, 0x51, 0xa1, 0xe6, 0x85, 0xf2, 0x24, 0x78, 0xa8, 0x95, 0x4f,
   2836  1.1.1.2  christos     0x00, 0x7b, 0xc7, 0x71, 0x1b, 0x93, 0x07, 0x72, 0xc7, 0x8f, 0x09, 0x2e,
   2837  1.1.1.2  christos     0x82, 0x87, 0x8e, 0x3e, 0x93, 0x7f, 0x36, 0x79, 0x67, 0x53, 0x29, 0x13,
   2838  1.1.1.2  christos     0xa8, 0xd5, 0x3d, 0xfd, 0xf4, 0xbf, 0xb1, 0xf8, 0x84, 0x67, 0x46, 0x59,
   2839  1.1.1.2  christos     0x67, 0x05, 0xcf, 0x34, 0x51, 0x42, 0xb9, 0x72, 0xa3, 0xf1, 0x63, 0x25,
   2840  1.1.1.2  christos     0xc4, 0x0c, 0x29, 0x52, 0xa3, 0x7b, 0x25, 0x89, 0x7e, 0x5e, 0xf3, 0x5f,
   2841  1.1.1.2  christos     0xba, 0xeb, 0x73, 0xa4, 0xac, 0xbe, 0xb6, 0xa0, 0xb8, 0x99, 0x42, 0xce,
   2842  1.1.1.2  christos     0xb1, 0x95, 0x53, 0x1c, 0xfc, 0x0a, 0x07, 0x99, 0x39, 0x54, 0x48, 0x3e,
   2843  1.1.1.2  christos     0x6c, 0xbc, 0x87, 0xc0, 0x6a, 0xa7, 0x4f, 0xf0, 0xca, 0xc5, 0x20, 0x7e,
   2844  1.1.1.2  christos     0x53, 0x5b, 0x26, 0x0a, 0xa9, 0x8d, 0x11, 0x98, 0xc0, 0x7d, 0xa6, 0x05,
   2845  1.1.1.2  christos     0xc4, 0xd1, 0x10, 0x20, 0xf6, 0xc9, 0xf7, 0xbb, 0x68, 0xbb, 0x34, 0x56,
   2846  1.1.1.2  christos     0xc7, 0x3a, 0x01, 0xb7, 0x10, 0xbc, 0x99, 0xd1, 0x77, 0x39, 0xa5, 0x17,
   2847  1.1.1.2  christos     0x16, 0xaa, 0x01, 0x66, 0x0c, 0x8b, 0x62, 0x8b, 0x2f, 0x56, 0x02, 0xba,
   2848  1.1.1.2  christos     0x65, 0xf0, 0x7e, 0xa9, 0x93, 0x33, 0x6e, 0x89, 0x6e, 0x83, 0xf2, 0xc5,
   2849  1.1.1.2  christos     0x73, 0x1b, 0xbf, 0x03, 0x46, 0x0c, 0x5b, 0x6c, 0x8a, 0xfe, 0xcb, 0x74,
   2850  1.1.1.2  christos     0x8e, 0xe3, 0x91, 0xe9, 0x89, 0x34, 0xa2, 0xc5, 0x7d, 0x4d, 0x06, 0x9f,
   2851  1.1.1.2  christos     0x50, 0xd8, 0x8b, 0x30, 0xd6, 0x96, 0x6f, 0x38, 0xc3, 0x7b, 0xc6, 0x49,
   2852  1.1.1.2  christos     0xb8, 0x26, 0x34, 0xce, 0x77, 0x22, 0x64, 0x5c, 0xcd, 0x62, 0x50, 0x63,
   2853  1.1.1.2  christos     0x36, 0x46, 0x46, 0xd6, 0xd6, 0x99, 0xdb, 0x57, 0xb4, 0x5e, 0xb6, 0x74,
   2854  1.1.1.2  christos     0x65, 0xe1, 0x6d, 0xe4, 0xd4, 0x06, 0xa8, 0x18, 0xb9, 0xea, 0xe1, 0xca,
   2855  1.1.1.2  christos     0x91, 0x6a, 0x25, 0x94, 0x48, 0x97, 0x08, 0xa4, 0x3c, 0xea, 0x88, 0xb0,
   2856  1.1.1.2  christos     0x2a, 0x4c, 0x03, 0xd0, 0x9b, 0x44, 0x81, 0x5c, 0x97, 0x10, 0x1c, 0xaf,
   2857  1.1.1.2  christos     0x50, 0x48, 0xbb, 0xcb, 0x24, 0x7a, 0xe2, 0x36, 0x6c, 0xdc, 0x25, 0x4b,
   2858  1.1.1.2  christos     0xa2, 0x21, 0x29, 0xf4, 0x5b, 0x3b, 0x0e, 0xb3, 0x99, 0xca, 0x91, 0xa3,
   2859  1.1.1.2  christos     0x03, 0x40, 0x28, 0x30, 0xec, 0x01, 0xdb, 0x7b, 0x2c, 0xa4, 0x80, 0xcf,
   2860  1.1.1.2  christos     0x35, 0x04, 0x09, 0xb2, 0x16, 0x09, 0x4b, 0x7b, 0x0c, 0x3a, 0xe3, 0x3c,
   2861  1.1.1.2  christos     0xe1, 0x0a, 0x91, 0x24, 0xe8, 0x96, 0x51, 0xab, 0x90, 0x1e, 0xa2, 0x53,
   2862  1.1.1.2  christos     0xc8, 0x41, 0x5b, 0xd7, 0x82, 0x5f, 0x02, 0xbb, 0x22, 0x93, 0x69, 0xaf,
   2863  1.1.1.2  christos     0x97, 0x20, 0x28, 0xf2, 0x28, 0x75, 0xea, 0x55, 0xaf, 0x16, 0xd3, 0xbc,
   2864  1.1.1.2  christos     0x69, 0xf7, 0x0c, 0x2e, 0xe8, 0xb7, 0x5f, 0x28, 0xb4, 0x7d, 0xd3, 0x91,
   2865  1.1.1.2  christos     0xf9, 0x89, 0xad, 0xe3, 0x14, 0x72, 0x9c, 0x33, 0x1f, 0xa0, 0x4c, 0x19,
   2866  1.1.1.2  christos     0x17, 0xb2, 0x78, 0xc3, 0xeb, 0x60, 0x28, 0x68, 0x51, 0x28, 0x21, 0xad,
   2867  1.1.1.2  christos     0xc8, 0x25, 0xc6, 0x45, 0x77, 0xce, 0x1e, 0x63, 0xb1, 0xd9, 0x64, 0x4a,
   2868  1.1.1.2  christos     0x61, 0x29, 0x48, 0xa3, 0x48, 0x3c, 0x7f, 0x1b, 0x9a, 0x25, 0x80, 0x00,
   2869  1.1.1.2  christos     0xe3, 0x01, 0x96, 0x94, 0x4a, 0x40, 0x36, 0x27, 0x60, 0x9c, 0x76, 0xc7,
   2870  1.1.1.2  christos     0xea, 0x6b, 0x5d, 0xe0, 0x17, 0x64, 0xd2, 0x43, 0x79, 0x11, 0x7b, 0x9e,
   2871  1.1.1.2  christos     0xa2, 0x98, 0x48, 0xdc, 0x55, 0x5c, 0x45, 0x4b, 0xce, 0xae, 0x1b, 0xa5,
   2872  1.1.1.2  christos     0xcc, 0x72, 0xc7, 0x4a, 0xb9, 0x6b, 0x9c, 0x91, 0xb9, 0x10, 0xd2, 0x6b,
   2873  1.1.1.2  christos     0x88, 0xb2, 0x56, 0x39, 0xd4, 0x77, 0x8a, 0xe2, 0x6c, 0x7c, 0x61, 0x51,
   2874  1.1.1.2  christos     0xa1, 0x9c, 0x6c, 0xd7, 0x93, 0x84, 0x54, 0x37, 0x24, 0x65, 0xe4, 0xc5,
   2875  1.1.1.2  christos     0xec, 0x29, 0x24, 0x5a, 0xcb, 0x3d, 0xb5, 0x37, 0x9d, 0xe3, 0xda, 0xbf,
   2876  1.1.1.2  christos     0xa6, 0x29, 0xa7, 0xc0, 0x4a, 0x83, 0x53, 0xa8, 0x53, 0x0c, 0x95, 0xac,
   2877  1.1.1.2  christos     0xb7, 0x32, 0xbb, 0x4b, 0xb8, 0x19, 0x32, 0xbb, 0x2c, 0xa7, 0xa8, 0x48,
   2878  1.1.1.2  christos     0xcd, 0x36, 0x68, 0x01, 0x44, 0x4a, 0xbe, 0x23, 0xc8, 0x3b, 0x36, 0x6a,
   2879  1.1.1.2  christos     0x87, 0xd6, 0xa3, 0xcf, 0x36, 0x09, 0x24, 0xc0, 0x02, 0xba, 0xe9, 0x0a,
   2880  1.1.1.2  christos     0xf6, 0x5c, 0x48, 0x06, 0x0b, 0x37, 0x52, 0xf2, 0xba, 0xdf, 0x1a, 0xb2,
   2881  1.1.1.2  christos     0x72, 0x20, 0x72, 0x55, 0x4a, 0x50, 0x59, 0x75, 0x35, 0x94, 0xe6, 0xa7,
   2882  1.1.1.2  christos     0x02, 0x76, 0x1f, 0xc9, 0x76, 0x84, 0xc8, 0xc4, 0xa7, 0x54, 0x0a, 0x6b,
   2883  1.1.1.2  christos     0x07, 0xfb, 0xc9, 0xde, 0x87, 0xc9, 0x74, 0xaa, 0x88, 0x09, 0xd9, 0x28,
   2884  1.1.1.2  christos     0xc7, 0xf4, 0xcb, 0xbf, 0x80, 0x45, 0xae, 0xa5, 0xbc, 0x66, 0x78, 0x25,
   2885  1.1.1.2  christos     0xfd, 0x05, 0xa5, 0x21, 0xf1, 0xa4, 0xbf, 0x53, 0x92, 0x10, 0xc7, 0x11,
   2886  1.1.1.2  christos     0x3b, 0xc3, 0x7b, 0x3e, 0x58, 0xb0, 0xcb, 0xfc, 0x53, 0xc8, 0x41, 0xcb,
   2887  1.1.1.2  christos     0xb0, 0x37, 0x1d, 0xe2, 0xe5, 0x11, 0xb9, 0x89, 0xcb, 0x7c, 0x70, 0xc0,
   2888  1.1.1.2  christos     0x23, 0x36, 0x6d, 0x78, 0xf9, 0xc3, 0x7e, 0xf0, 0x47, 0xf8, 0x72, 0x0b,
   2889  1.1.1.2  christos     0xe1, 0xc7, 0x59, 0xa8, 0xd9, 0x6b, 0x93, 0xf6, 0x5a, 0x94, 0x11, 0x4f,
   2890  1.1.1.2  christos     0xfa, 0xf6, 0x0d, 0x9a, 0x81, 0x79, 0x5e, 0x99, 0x5c, 0x71, 0x15, 0x2a,
   2891  1.1.1.2  christos     0x46, 0x91, 0xa5, 0xa6, 0x02, 0xa9, 0xe1, 0xf3, 0x59, 0x9e, 0x37, 0xc7,
   2892  1.1.1.2  christos     0x68, 0xc7, 0xbc, 0x10, 0x89, 0x94, 0xc0, 0x66, 0x9f, 0x3a, 0xdc, 0x95,
   2893  1.1.1.2  christos     0x7d, 0x46, 0xb4, 0xb6, 0x25, 0x69, 0x68, 0xe2, 0x90, 0xd7, 0x89, 0x2e,
   2894  1.1.1.2  christos     0xa8, 0x54, 0x64, 0xee, 0x7a, 0x75, 0x0f, 0x39, 0xc5, 0xe3, 0x15, 0x2c,
   2895  1.1.1.2  christos     0x2d, 0xfc, 0x56, 0xd8, 0xb0, 0xc9, 0x24, 0xba, 0x8a, 0x95, 0x9a, 0x68,
   2896  1.1.1.2  christos     0x09, 0x65, 0x47, 0xf6, 0x64, 0x23, 0xc8, 0x38, 0x98, 0x2a, 0x57, 0x94,
   2897  1.1.1.2  christos     0xb9, 0xe1, 0x53, 0x37, 0x71, 0x33, 0x1a, 0x9a, 0x65, 0x6c, 0x28, 0x82,
   2898  1.1.1.2  christos     0x8b, 0xeb, 0x91, 0x26, 0xa6, 0x0e, 0x95, 0xe8, 0xc5, 0xd9, 0x06, 0x83,
   2899  1.1.1.2  christos     0x2c, 0x77, 0x10, 0x70, 0x55, 0x76, 0xb1, 0xfb, 0x95, 0x07, 0x26, 0x9d,
   2900  1.1.1.2  christos     0xda, 0xf8, 0xc9, 0x5c, 0xe9, 0x71, 0x9b, 0x2c, 0xa8, 0xdd, 0x11, 0x2b,
   2901  1.1.1.2  christos     0xe1, 0x0b, 0xcc, 0x9f, 0x4a, 0x37, 0xbd, 0x1b, 0x1e, 0xee, 0xb3, 0x3e,
   2902  1.1.1.2  christos     0xcd, 0xa7, 0x6a, 0xe9, 0xf6, 0x9a, 0x5d, 0x4b, 0x29, 0x23, 0xa8, 0x69,
   2903  1.1.1.2  christos     0x57, 0x67, 0x1d, 0x61, 0x93, 0x35, 0xbe, 0x1c, 0x4c, 0x2c, 0x77, 0xce,
   2904  1.1.1.2  christos     0x87, 0xc4, 0x1f, 0x98, 0xa8, 0xcc, 0x46, 0x64, 0x60, 0xfa, 0x30, 0x0a,
   2905  1.1.1.2  christos     0xaf, 0x5b, 0x30, 0x1f, 0x0a, 0x1d, 0x09, 0xc8, 0x8e, 0x65, 0xda, 0x4d,
   2906  1.1.1.2  christos     0x8e, 0xe6, 0x4f, 0x68, 0xc0, 0x21, 0x89, 0xbb, 0xb3, 0x58, 0x4b, 0xaf,
   2907  1.1.1.2  christos     0xf7, 0x16, 0xc8, 0x5d, 0xb6, 0x54, 0x04, 0x8a, 0x00, 0x43, 0x33, 0x48,
   2908  1.1.1.2  christos     0x93, 0x93, 0xa0, 0x74, 0x27, 0xcd, 0x3e, 0x21, 0x7e, 0x6a, 0x34, 0x5f,
   2909  1.1.1.2  christos     0x6c, 0x2c, 0x2b, 0x13, 0xc2, 0x7b, 0x33, 0x72, 0x71, 0xc0, 0xb2, 0x7b,
   2910  1.1.1.2  christos     0x2d, 0xba, 0xa0, 0x0d, 0x23, 0x76, 0x00, 0xb5, 0xb5, 0x94, 0xe8, 0xcf,
   2911  1.1.1.2  christos     0x2d, 0xd6, 0x25, 0xea, 0x76, 0xcf, 0x0e, 0xd8, 0x99, 0x12, 0x2c, 0x97,
   2912  1.1.1.2  christos     0x96, 0xb4, 0xb0, 0x18, 0x70, 0x04, 0x25, 0x80, 0x49, 0xa4, 0x77, 0xcd,
   2913  1.1.1.2  christos     0x11, 0xd6, 0x8c, 0x49, 0xb9, 0xa0, 0xe7, 0xb0, 0x0b, 0xce, 0x8c, 0xac,
   2914  1.1.1.2  christos     0x78, 0x64, 0xcb, 0xb3, 0x75, 0x14, 0x00, 0x84, 0x74, 0x4c, 0x93, 0x06,
   2915  1.1.1.2  christos     0x26, 0x94, 0xca, 0x79, 0x5c, 0x4f, 0x40, 0xe7, 0xac, 0xc9, 0xc5, 0xa1,
   2916  1.1.1.2  christos     0x88, 0x40, 0x72, 0xd8, 0xc3, 0x8d, 0xaf, 0xb5, 0x01, 0xee, 0x41, 0x84,
   2917  1.1.1.2  christos     0xdd, 0x5a, 0x81, 0x9e, 0xc2, 0x4e, 0xc1, 0x65, 0x12, 0x61, 0xf9, 0x62,
   2918  1.1.1.2  christos     0xb1, 0x7a, 0x72, 0x15, 0xaa, 0x4a, 0x74, 0x8c, 0x15, 0x83, 0x6c, 0x38,
   2919  1.1.1.2  christos     0x91, 0x37, 0x67, 0x82, 0x04, 0x83, 0x8d, 0x71, 0x95, 0xa8, 0x5b, 0x4f,
   2920  1.1.1.2  christos     0x98, 0xa1, 0xb5, 0x74, 0xc4, 0xcd, 0x79, 0x09, 0xcd, 0x1f, 0x83, 0x3e,
   2921  1.1.1.2  christos     0xff, 0xd1, 0x48, 0x55, 0x43, 0x22, 0x9d, 0x37, 0x48, 0xd9, 0xb5, 0xcd,
   2922  1.1.1.2  christos     0x6c, 0x17, 0xb9, 0xb3, 0xb8, 0x4a, 0xef, 0x8b, 0xce, 0x13, 0xe6, 0x83,
   2923  1.1.1.2  christos     0x73, 0x36, 0x59, 0xc7, 0x95, 0x42, 0xd6, 0x15, 0x78, 0x2a, 0x71, 0xcd,
   2924  1.1.1.2  christos     0xee, 0xe7, 0x92, 0xba, 0xb5, 0x1b, 0xdc, 0x4b, 0xbf, 0xe8, 0x30, 0x8e,
   2925  1.1.1.2  christos     0x66, 0x31, 0x44, 0xed, 0xe8, 0x49, 0x18, 0x30, 0xad, 0x98, 0xb4, 0x63,
   2926  1.1.1.2  christos     0x4f, 0x64, 0xab, 0xa8, 0xb9, 0xc0, 0x42, 0x27, 0x26, 0x53, 0x92, 0x0f,
   2927  1.1.1.2  christos     0x38, 0x0c, 0x1a, 0x17, 0xca, 0x87, 0xce, 0xd7, 0xaa, 0xc4, 0x1c, 0x82,
   2928  1.1.1.2  christos     0x88, 0x87, 0x93, 0x18, 0x1a, 0x6f, 0x76, 0xe1, 0x97, 0xb7, 0xb9, 0x0e,
   2929  1.1.1.2  christos     0xf9, 0x09, 0x43, 0xbb, 0x38, 0x44, 0x91, 0x29, 0x11, 0xd8, 0x55, 0x1e,
   2930  1.1.1.2  christos     0x54, 0x66, 0xc5, 0x76, 0x7a, 0xb0, 0xbc, 0x61, 0xa1, 0xa3, 0xf7, 0x36,
   2931  1.1.1.2  christos     0x16, 0x2e, 0xc0, 0x98, 0xa9, 0x00, 0xb1, 0x2d, 0xd8, 0xfa, 0xbb, 0xfb,
   2932  1.1.1.2  christos     0x3f, 0xe8, 0xcb, 0x1d, 0xc4, 0xe8, 0x31, 0x5f, 0x2a, 0xf0, 0xd3, 0x2f,
   2933  1.1.1.2  christos     0x00, 0x17, 0xae, 0x13, 0x6e, 0x19, 0xf0, 0x28
   2934      1.1  christos };
   2935      1.1  christos static const uint8_t ml_kem_1024_pubkey[] = {
   2936  1.1.1.2  christos     0x53, 0x79, 0x11, 0x95, 0x7c, 0x12, 0x51, 0x48, 0xa8, 0x7f, 0x41, 0x58,
   2937  1.1.1.2  christos     0x9c, 0xb2, 0x22, 0xd0, 0xd1, 0x92, 0x29, 0xe2, 0xcb, 0x55, 0xe1, 0xa0,
   2938  1.1.1.2  christos     0x44, 0x79, 0x1e, 0x7c, 0xa6, 0x11, 0x92, 0xa4, 0x64, 0x60, 0xc3, 0x18,
   2939  1.1.1.2  christos     0x3d, 0x2b, 0xcd, 0x6d, 0xe0, 0x8a, 0x5e, 0x76, 0x51, 0x60, 0x3a, 0xcc,
   2940  1.1.1.2  christos     0x34, 0x9c, 0xa1, 0x6c, 0xba, 0x18, 0xab, 0xb2, 0x3a, 0x3e, 0x8c, 0x33,
   2941  1.1.1.2  christos     0x0d, 0x74, 0x21, 0x59, 0x8a, 0x62, 0x78, 0xec, 0x7e, 0xbf, 0xab, 0xca,
   2942  1.1.1.2  christos     0x0e, 0xf4, 0x88, 0xb2, 0x29, 0x05, 0x54, 0x75, 0x34, 0x99, 0xc0, 0x45,
   2943  1.1.1.2  christos     0x2e, 0x45, 0x38, 0x15, 0x30, 0x99, 0x55, 0xb8, 0x15, 0x0f, 0xa1, 0xa1,
   2944  1.1.1.2  christos     0xe3, 0x93, 0x38, 0x6d, 0xc1, 0x2f, 0xdb, 0x27, 0xb3, 0x8c, 0x67, 0x45,
   2945  1.1.1.2  christos     0xf2, 0x94, 0x40, 0x16, 0xec, 0x45, 0x7f, 0x39, 0xb1, 0x8d, 0x60, 0x4a,
   2946  1.1.1.2  christos     0x07, 0xa1, 0xab, 0xe0, 0x7b, 0xc8, 0x44, 0x05, 0x0f, 0xfa, 0x8a, 0x06,
   2947  1.1.1.2  christos     0xfa, 0x15, 0x4a, 0x49, 0xd8, 0x8f, 0xac, 0x77, 0x54, 0x52, 0xd6, 0xa7,
   2948  1.1.1.2  christos     0xc0, 0xe5, 0x89, 0xbf, 0xb5, 0xc3, 0x70, 0xc2, 0xc4, 0xb6, 0x20, 0x1d,
   2949  1.1.1.2  christos     0xda, 0x80, 0xc9, 0xab, 0x20, 0x76, 0xec, 0xc0, 0x8b, 0x44, 0x52, 0x2f,
   2950  1.1.1.2  christos     0xda, 0x33, 0x26, 0xf0, 0x33, 0x80, 0x6d, 0xd2, 0x69, 0x3f, 0x31, 0x97,
   2951  1.1.1.2  christos     0x39, 0xf4, 0x0c, 0x4f, 0x42, 0xb2, 0x4a, 0xca, 0x70, 0x98, 0xfb, 0x8f,
   2952  1.1.1.2  christos     0xf5, 0xf9, 0xac, 0x20, 0x29, 0x2d, 0x02, 0xb5, 0x6a, 0xc7, 0x46, 0x80,
   2953  1.1.1.2  christos     0x1a, 0xcc, 0xcc, 0x84, 0x86, 0x3d, 0xee, 0x32, 0x87, 0x84, 0x97, 0xb6,
   2954  1.1.1.2  christos     0x94, 0x38, 0xbf, 0x99, 0x17, 0x76, 0x28, 0x66, 0x50, 0x48, 0x2c, 0x8d,
   2955  1.1.1.2  christos     0x9d, 0x95, 0x87, 0xbc, 0x6a, 0x55, 0xb8, 0x5c, 0x4d, 0x7f, 0xa7, 0x4d,
   2956  1.1.1.2  christos     0x02, 0x65, 0x6b, 0x42, 0x1c, 0x9e, 0x23, 0xe0, 0x3a, 0x48, 0xd4, 0xb7,
   2957  1.1.1.2  christos     0x44, 0x25, 0xc2, 0x6e, 0x4a, 0x20, 0xdd, 0x95, 0x62, 0xa4, 0xda, 0x07,
   2958  1.1.1.2  christos     0x93, 0xf3, 0xa3, 0x52, 0xcc, 0xc0, 0xf1, 0x82, 0x17, 0xd8, 0x68, 0xc7,
   2959  1.1.1.2  christos     0xf5, 0x00, 0x2a, 0xbe, 0x76, 0x8b, 0x1f, 0xc7, 0x3f, 0x05, 0x74, 0x4e,
   2960  1.1.1.2  christos     0x7c, 0xc2, 0x8f, 0x10, 0x34, 0x40, 0x62, 0xc1, 0x0e, 0x08, 0xec, 0xcc,
   2961  1.1.1.2  christos     0xed, 0x3c, 0x1f, 0x7d, 0x39, 0x2c, 0x01, 0xd9, 0x79, 0xdd, 0x71, 0x8d,
   2962  1.1.1.2  christos     0x83, 0x98, 0x37, 0x46, 0x65, 0xa1, 0x6a, 0x98, 0x70, 0x58, 0x5c, 0x39,
   2963  1.1.1.2  christos     0xd5, 0x58, 0x9a, 0x50, 0xe1, 0x33, 0x38, 0x9c, 0x9b, 0x9a, 0x27, 0x6c,
   2964  1.1.1.2  christos     0x02, 0x42, 0x60, 0xd9, 0xfc, 0x77, 0x11, 0xc8, 0x1b, 0x63, 0x37, 0xb5,
   2965  1.1.1.2  christos     0x7d, 0xa3, 0xc3, 0x76, 0xd0, 0xcd, 0x74, 0xe1, 0x4c, 0x73, 0x72, 0x7b,
   2966  1.1.1.2  christos     0x27, 0x66, 0x56, 0xb9, 0xd8, 0xa4, 0xeb, 0x71, 0x89, 0x6f, 0xf5, 0x89,
   2967  1.1.1.2  christos     0xd4, 0xb8, 0x93, 0xe7, 0x11, 0x0f, 0x3b, 0xb9, 0x48, 0xec, 0xe2, 0x91,
   2968  1.1.1.2  christos     0xdd, 0x86, 0xc0, 0xb7, 0x46, 0x8a, 0x67, 0x8c, 0x74, 0x69, 0x80, 0xc1,
   2969  1.1.1.2  christos     0x2a, 0xa6, 0xb9, 0x5e, 0x2b, 0x0c, 0xbe, 0x43, 0x31, 0xbb, 0x24, 0xa3,
   2970  1.1.1.2  christos     0x3a, 0x27, 0x01, 0x53, 0xaa, 0x47, 0x2c, 0x47, 0x31, 0x23, 0x82, 0xca,
   2971  1.1.1.2  christos     0x36, 0x5c, 0x5f, 0x35, 0x25, 0x9d, 0x02, 0x57, 0x46, 0xfc, 0x65, 0x95,
   2972  1.1.1.2  christos     0xfe, 0x63, 0x6c, 0x76, 0x75, 0x10, 0xa6, 0x9c, 0x1e, 0x8a, 0x17, 0x6b,
   2973  1.1.1.2  christos     0x79, 0x49, 0x95, 0x8f, 0x26, 0x97, 0x39, 0x94, 0x97, 0xa2, 0xfc, 0x73,
   2974  1.1.1.2  christos     0x64, 0xa1, 0x2c, 0x81, 0x98, 0x29, 0x52, 0x39, 0xc8, 0x26, 0xcb, 0x50,
   2975  1.1.1.2  christos     0x82, 0x08, 0x60, 0x77, 0x28, 0x2e, 0xd6, 0x28, 0x65, 0x1f, 0xc0, 0x4c,
   2976  1.1.1.2  christos     0x63, 0x9b, 0x43, 0x85, 0x22, 0xa9, 0xde, 0x30, 0x9b, 0x14, 0xb0, 0x86,
   2977  1.1.1.2  christos     0xd6, 0xe9, 0x23, 0xc5, 0x51, 0x62, 0x3b, 0xd7, 0x2a, 0x73, 0x3c, 0xb0,
   2978  1.1.1.2  christos     0xda, 0xbc, 0x54, 0xa9, 0x41, 0x6a, 0x99, 0xe7, 0x2c, 0x9f, 0xda, 0x1c,
   2979  1.1.1.2  christos     0xb3, 0xfb, 0x9b, 0xa0, 0x6b, 0x8a, 0xdb, 0x24, 0x22, 0xd6, 0x8c, 0xad,
   2980  1.1.1.2  christos     0xc5, 0x53, 0xc9, 0x82, 0x02, 0xa1, 0x76, 0x56, 0x47, 0x8a, 0xc0, 0x44,
   2981  1.1.1.2  christos     0xef, 0x34, 0x56, 0x37, 0x8a, 0xbc, 0xe9, 0x99, 0x1e, 0x01, 0x41, 0xba,
   2982  1.1.1.2  christos     0x79, 0x09, 0x4f, 0xa8, 0xf7, 0x7a, 0x30, 0x08, 0x05, 0xd2, 0xd3, 0x2f,
   2983  1.1.1.2  christos     0xfc, 0x62, 0xbf, 0x0c, 0xa4, 0x55, 0x4c, 0x33, 0x0c, 0x2b, 0xb7, 0x04,
   2984  1.1.1.2  christos     0x2d, 0xb3, 0x51, 0x02, 0xf6, 0x8b, 0x1a, 0x00, 0x62, 0x58, 0x38, 0x65,
   2985  1.1.1.2  christos     0x38, 0x1c, 0x74, 0xdd, 0x91, 0x3a, 0xf7, 0x0b, 0x26, 0xcf, 0x09, 0x23,
   2986  1.1.1.2  christos     0xd0, 0xc4, 0xcb, 0x97, 0x16, 0x92, 0x22, 0x25, 0x52, 0xa8, 0xf4, 0xb7,
   2987  1.1.1.2  christos     0x88, 0xb4, 0xaf, 0xd1, 0x34, 0x1a, 0x9d, 0xf4, 0x15, 0xcf, 0x20, 0x39,
   2988  1.1.1.2  christos     0x00, 0xf5, 0xcc, 0xf7, 0xf6, 0x59, 0x88, 0x94, 0x9a, 0x75, 0x58, 0x0d,
   2989  1.1.1.2  christos     0x04, 0x96, 0x39, 0x85, 0x31, 0x00, 0x85, 0x4b, 0x21, 0xf4, 0x01, 0x80,
   2990  1.1.1.2  christos     0x03, 0x50, 0x2b, 0xb1, 0xba, 0x95, 0xf5, 0x56, 0xa5, 0xd6, 0x7c, 0x7e,
   2991  1.1.1.2  christos     0xb5, 0x24, 0x10, 0xeb, 0xa2, 0x88, 0xa6, 0xd0, 0x63, 0x5c, 0xa8, 0xa4,
   2992  1.1.1.2  christos     0xf6, 0xd6, 0x96, 0xd0, 0xa0, 0x20, 0xc8, 0x26, 0x93, 0x8d, 0x34, 0x94,
   2993  1.1.1.2  christos     0x3c, 0x38, 0x08, 0xc7, 0x9c, 0xc0, 0x07, 0x76, 0x85, 0x33, 0x21, 0x6b,
   2994  1.1.1.2  christos     0xc1, 0xb2, 0x9d, 0xa6, 0xc8, 0x12, 0xef, 0xf3, 0x34, 0x0b, 0xaa, 0x8d,
   2995  1.1.1.2  christos     0x2e, 0x65, 0x34, 0x4f, 0x09, 0xbd, 0x47, 0x89, 0x4f, 0x5a, 0x3a, 0x41,
   2996  1.1.1.2  christos     0x18, 0x71, 0x5b, 0x3c, 0x50, 0x20, 0x67, 0x93, 0x27, 0xf9, 0x18, 0x9f,
   2997  1.1.1.2  christos     0x7e, 0x10, 0x85, 0x6b, 0x23, 0x8b, 0xb9, 0xb0, 0xab, 0x4c, 0xa8, 0x5a,
   2998  1.1.1.2  christos     0xbf, 0x4b, 0x21, 0xf5, 0xc7, 0x6b, 0xcc, 0xd7, 0x18, 0x50, 0xb2, 0x2e,
   2999  1.1.1.2  christos     0x04, 0x59, 0x28, 0x27, 0x6a, 0x0f, 0x2e, 0x95, 0x1d, 0xb0, 0x70, 0x7c,
   3000  1.1.1.2  christos     0x6a, 0x11, 0x6d, 0xc1, 0x91, 0x13, 0xfa, 0x76, 0x2d, 0xc5, 0xf2, 0x0b,
   3001  1.1.1.2  christos     0xd5, 0xd2, 0xab, 0x5b, 0xe7, 0x17, 0x44, 0xdc, 0x9c, 0xbd, 0xb5, 0x1e,
   3002  1.1.1.2  christos     0xa7, 0x57, 0x96, 0x3a, 0xac, 0x56, 0xa9, 0x0a, 0x0d, 0x80, 0x23, 0xbe,
   3003  1.1.1.2  christos     0xd1, 0xf5, 0xca, 0xe8, 0xa6, 0x4d, 0xa0, 0x47, 0x27, 0x9b, 0x35, 0x3a,
   3004  1.1.1.2  christos     0x09, 0x6a, 0x83, 0x5b, 0x0b, 0x2b, 0x02, 0x3b, 0x6a, 0xa0, 0x48, 0x98,
   3005  1.1.1.2  christos     0x92, 0x33, 0x07, 0x9a, 0xeb, 0x46, 0x7e, 0x52, 0x2f, 0xa2, 0x7a, 0x58,
   3006  1.1.1.2  christos     0x22, 0x92, 0x1e, 0x5c, 0x55, 0x1b, 0x4f, 0x53, 0x75, 0x36, 0xe4, 0x6f,
   3007  1.1.1.2  christos     0x3a, 0x6a, 0x97, 0xe7, 0x2c, 0x3b, 0x06, 0x31, 0x04, 0xe0, 0x9a, 0x04,
   3008  1.1.1.2  christos     0x05, 0x98, 0x94, 0x0d, 0x87, 0x2f, 0x6d, 0x87, 0x1f, 0x5e, 0xf9, 0xb4,
   3009  1.1.1.2  christos     0x35, 0x50, 0x73, 0xb5, 0x47, 0x69, 0xe4, 0x54, 0x54, 0xe6, 0xa0, 0x81,
   3010  1.1.1.2  christos     0x95, 0x99, 0x40, 0x86, 0x21, 0xab, 0x44, 0x13, 0xb3, 0x55, 0x07, 0xb0,
   3011  1.1.1.2  christos     0xdf, 0x57, 0x8c, 0xe2, 0xd5, 0x11, 0xd5, 0x20, 0x58, 0xd5, 0x74, 0x9d,
   3012  1.1.1.2  christos     0xf3, 0x8b, 0x29, 0xd6, 0xcc, 0x58, 0x87, 0x0c, 0xaf, 0x92, 0xf6, 0x9a,
   3013  1.1.1.2  christos     0x75, 0x16, 0x14, 0x06, 0xe7, 0x1c, 0x5f, 0xf9, 0x24, 0x51, 0xa7, 0x75,
   3014  1.1.1.2  christos     0x22, 0xb8, 0xb2, 0x96, 0x7a, 0x2d, 0x58, 0xa4, 0x9a, 0x81, 0x66, 0x1a,
   3015  1.1.1.2  christos     0xa6, 0x5a, 0xc0, 0x9b, 0x08, 0xc9, 0xfe, 0x45, 0xab, 0xc3, 0x85, 0x1f,
   3016  1.1.1.2  christos     0x99, 0xc7, 0x30, 0xc4, 0x50, 0x03, 0xac, 0xa2, 0xbf, 0x0f, 0x84, 0x24,
   3017  1.1.1.2  christos     0xa1, 0x9b, 0x74, 0x08, 0xa5, 0x37, 0xd5, 0x41, 0xc1, 0x6f, 0x56, 0x82,
   3018  1.1.1.2  christos     0xbf, 0xe3, 0xa7, 0xfa, 0xea, 0x56, 0x4f, 0x12, 0x98, 0x61, 0x1a, 0x7f,
   3019  1.1.1.2  christos     0x5f, 0x60, 0x92, 0x2b, 0xa1, 0x9d, 0xe7, 0x3b, 0x19, 0x17, 0xf1, 0x85,
   3020  1.1.1.2  christos     0x32, 0x73, 0x55, 0x51, 0x99, 0xa6, 0x49, 0x31, 0x8b, 0x50, 0x77, 0x33,
   3021  1.1.1.2  christos     0x45, 0xc9, 0x97, 0x46, 0x08, 0x56, 0x97, 0x2a, 0xcb, 0x43, 0xfc, 0x81,
   3022  1.1.1.2  christos     0xab, 0x63, 0x21, 0xb1, 0xc3, 0x3c, 0x2b, 0xb5, 0x09, 0x8b, 0xd4, 0x89,
   3023  1.1.1.2  christos     0xd6, 0x96, 0xa0, 0xf7, 0x06, 0x79, 0xc1, 0x21, 0x38, 0x73, 0xd0, 0x8b,
   3024  1.1.1.2  christos     0xda, 0xd4, 0x28, 0x44, 0x92, 0x72, 0x16, 0x04, 0x72, 0x05, 0x63, 0x32,
   3025  1.1.1.2  christos     0x12, 0x31, 0x0e, 0xe9, 0xa0, 0x6c, 0xb1, 0x00, 0x16, 0xc8, 0x05, 0x50,
   3026  1.1.1.2  christos     0x3c, 0x34, 0x1a, 0x36, 0xd8, 0x7e, 0x56, 0x07, 0x2e, 0xab, 0xe2, 0x37,
   3027  1.1.1.2  christos     0x31, 0xe3, 0x4a, 0xf7, 0xe2, 0x32, 0x8f, 0x85, 0xcd, 0xb3, 0x70, 0xcc,
   3028  1.1.1.2  christos     0xaf, 0x00, 0x51, 0x5b, 0x64, 0xc9, 0xc5, 0x4b, 0xc8, 0x37, 0x57, 0x84,
   3029  1.1.1.2  christos     0x47, 0xaa, 0xcf, 0xae, 0xd5, 0x96, 0x9a, 0xa3, 0x51, 0xe7, 0xda, 0x4e,
   3030  1.1.1.2  christos     0xfa, 0x7b, 0x11, 0x5c, 0x4c, 0x51, 0xf4, 0xa6, 0x99, 0x77, 0x98, 0x50,
   3031  1.1.1.2  christos     0x29, 0x5c, 0xa7, 0x2d, 0x78, 0x1a, 0xd4, 0x1b, 0xc6, 0x80, 0x53, 0x2b,
   3032  1.1.1.2  christos     0x89, 0xe7, 0x10, 0xe2, 0x18, 0x9e, 0xb3, 0xc5, 0x08, 0x17, 0xba, 0x25,
   3033  1.1.1.2  christos     0x5c, 0x74, 0x74, 0xc9, 0x5c, 0xa9, 0x11, 0x0c, 0xc4, 0x3b, 0x8b, 0xa8,
   3034  1.1.1.2  christos     0xe6, 0x82, 0xc7, 0xfb, 0x7b, 0x0f, 0xdc, 0x26, 0x5c, 0x04, 0x83, 0xa6,
   3035  1.1.1.2  christos     0x5c, 0xa4, 0x51, 0x4e, 0xe4, 0xb8, 0x32, 0xaa, 0xc5, 0x80, 0x0c, 0x3b,
   3036  1.1.1.2  christos     0x08, 0xe7, 0x4f, 0x56, 0x39, 0x51, 0xc1, 0xfb, 0xb2, 0x10, 0x35, 0x3e,
   3037  1.1.1.2  christos     0xfa, 0x1a, 0xa8, 0x66, 0x85, 0x6b, 0xc1, 0xe0, 0x34, 0x73, 0x3b, 0x04,
   3038  1.1.1.2  christos     0x85, 0xda, 0xb1, 0xd0, 0x20, 0xc6, 0xbf, 0x76, 0x5f, 0xf6, 0x0b, 0x3b,
   3039  1.1.1.2  christos     0x80, 0x19, 0x84, 0xa9, 0x0c, 0x2f, 0xe9, 0x70, 0xbf, 0x1d, 0xe9, 0x70,
   3040  1.1.1.2  christos     0x04, 0xa6, 0xcf, 0x44, 0xb4, 0x98, 0x4a, 0xb5, 0x82, 0x58, 0xb4, 0xaf,
   3041  1.1.1.2  christos     0x71, 0x22, 0x1c, 0xd1, 0x75, 0x30, 0xa7, 0x00, 0xc3, 0x29, 0x59, 0xc9,
   3042  1.1.1.2  christos     0x43, 0x63, 0x44, 0xb5, 0x31, 0x6f, 0x09, 0xcc, 0xca, 0x70, 0x29, 0xa2,
   3043  1.1.1.2  christos     0x30, 0xd6, 0x39, 0xdc, 0xb0, 0x22, 0xd8, 0xba, 0x79, 0xba, 0x91, 0xcd,
   3044  1.1.1.2  christos     0x6a, 0xb1, 0x2a, 0xe1, 0x57, 0x9c, 0x50, 0xc7, 0xbb, 0x10, 0xe3, 0x03,
   3045  1.1.1.2  christos     0x01, 0xa6, 0x5c, 0xae, 0x31, 0x01, 0xd4, 0x0c, 0x7b, 0xa9, 0x27, 0xbb,
   3046  1.1.1.2  christos     0x55, 0x31, 0x48, 0xd1, 0x64, 0x70, 0x24, 0xd4, 0xa0, 0x6c, 0x81, 0x66,
   3047  1.1.1.2  christos     0xd0, 0xb0, 0xb8, 0x12, 0x69, 0xb7, 0xd5, 0xf4, 0xb3, 0x4f, 0xb0, 0x22,
   3048  1.1.1.2  christos     0xf6, 0x91, 0x52, 0xf5, 0x14, 0x00, 0x4a, 0x7c, 0x68, 0x53, 0x68, 0x55,
   3049  1.1.1.2  christos     0x23, 0x43, 0xbb, 0x60, 0x36, 0x0f, 0xbb, 0x99, 0x45, 0xed, 0xf4, 0x46,
   3050  1.1.1.2  christos     0xd3, 0x45, 0xbd, 0xca, 0xa7, 0x45, 0x5c, 0x74, 0xba, 0x0a, 0x55, 0x1e,
   3051  1.1.1.2  christos     0x18, 0x46, 0x20, 0xfe, 0xf9, 0x76, 0x88, 0x77, 0x3d, 0x50, 0xb6, 0x43,
   3052  1.1.1.2  christos     0x3c, 0xa7, 0xa7, 0xac, 0x5c, 0xb6, 0xb7, 0xf6, 0x71, 0xa1, 0x53, 0x76,
   3053  1.1.1.2  christos     0xe5, 0xa6, 0x74, 0x7a, 0x62, 0x3f, 0xa7, 0xbc, 0x66, 0x30, 0x37, 0x3f,
   3054  1.1.1.2  christos     0x5b, 0x1b, 0x51, 0x26, 0x90, 0xa6, 0x61, 0x37, 0x78, 0x70, 0xa6, 0x0a,
   3055  1.1.1.2  christos     0x7a, 0x18, 0x96, 0x83, 0xf9, 0xb0, 0xcf, 0x04, 0x66, 0xe1, 0xf7, 0x50,
   3056  1.1.1.2  christos     0x76, 0x26, 0x31, 0xc4, 0xab, 0x09, 0xf5, 0x05, 0xc4, 0x2d, 0xd2, 0x86,
   3057  1.1.1.2  christos     0x33, 0x56, 0x94, 0x72, 0x73, 0x54, 0x42, 0x85, 0x1e, 0x32, 0x16, 0x16,
   3058  1.1.1.2  christos     0xd4, 0x00, 0x98, 0x10, 0x77, 0x7b, 0x6b, 0xd4, 0x6f, 0xa7, 0x22, 0x44,
   3059  1.1.1.2  christos     0x61, 0xa5, 0xcc, 0x27, 0x40, 0x5d, 0xfb, 0xac, 0x0d, 0x39, 0xb0, 0x02,
   3060  1.1.1.2  christos     0xca, 0xb3, 0x34, 0x33, 0xf2, 0xa8, 0x6e, 0xb8, 0xce, 0x91, 0xc1, 0x34,
   3061  1.1.1.2  christos     0xa6, 0x38, 0x6f, 0x86, 0x0a, 0x19, 0x94, 0xeb, 0x4b, 0x68, 0x75, 0xa4,
   3062  1.1.1.2  christos     0x6d, 0x19, 0x55, 0x81, 0xd1, 0x73, 0x85, 0x4b, 0x53, 0xd2, 0x29, 0x3d,
   3063  1.1.1.2  christos     0xf3, 0xe9, 0xa8, 0x22, 0x75, 0x6c, 0xd8, 0xf2, 0x12, 0xb3, 0x25, 0xca,
   3064  1.1.1.2  christos     0x29, 0xb4, 0xf9, 0xf8, 0xcf, 0xba, 0xdf, 0x2e, 0x41, 0x86, 0x9a, 0xbf,
   3065  1.1.1.2  christos     0xba, 0xd1, 0x07, 0x38, 0xad, 0x04, 0xcc, 0x75, 0x2b, 0xc2, 0x0c, 0x39,
   3066  1.1.1.2  christos     0x47, 0x46, 0x85, 0x0e, 0x0c, 0x48, 0x47, 0xdb
   3067      1.1  christos };
   3068      1.1  christos #endif
   3069      1.1  christos 
   3070      1.1  christos static struct keys_st {
   3071      1.1  christos     int type;
   3072      1.1  christos     const uint8_t *priv;
   3073      1.1  christos     const uint8_t *pub;
   3074      1.1  christos     /* If "name" is non-NULL, create via non-legacy _ex() API */
   3075      1.1  christos     char *name;
   3076  1.1.1.2  christos     int privlen;
   3077  1.1.1.2  christos     int publen;
   3078      1.1  christos } keys[] = {
   3079  1.1.1.2  christos     { EVP_PKEY_HMAC,
   3080      1.1  christos         (const uint8_t *)"0123456789",
   3081      1.1  christos         NULL,
   3082  1.1.1.2  christos         NULL, 10, 0 },
   3083  1.1.1.2  christos     { EVP_PKEY_HMAC, (const uint8_t *)"", NULL,
   3084      1.1  christos         NULL, 0, 0
   3085      1.1  christos #ifndef OPENSSL_NO_POLY1305
   3086  1.1.1.2  christos     },
   3087  1.1.1.2  christos     { EVP_PKEY_POLY1305,
   3088      1.1  christos         (const uint8_t *)"01234567890123456789012345678901",
   3089      1.1  christos         NULL,
   3090      1.1  christos         NULL, 32, 0
   3091      1.1  christos #endif
   3092      1.1  christos #ifndef OPENSSL_NO_SIPHASH
   3093  1.1.1.2  christos     },
   3094  1.1.1.2  christos     { EVP_PKEY_SIPHASH,
   3095      1.1  christos         (const uint8_t *)"0123456789012345",
   3096      1.1  christos         NULL,
   3097      1.1  christos         NULL, 16, 0
   3098      1.1  christos #endif
   3099      1.1  christos     },
   3100      1.1  christos #ifndef OPENSSL_NO_ECX
   3101  1.1.1.2  christos     { EVP_PKEY_X25519,
   3102      1.1  christos         (const uint8_t *)"01234567890123456789012345678901",
   3103      1.1  christos         (const unsigned char *)"abcdefghijklmnopqrstuvwxyzabcdef",
   3104  1.1.1.2  christos         NULL, 32, 32 },
   3105  1.1.1.2  christos     { EVP_PKEY_ED25519,
   3106      1.1  christos         (const uint8_t *)"01234567890123456789012345678901",
   3107      1.1  christos         (const uint8_t *)"abcdefghijklmnopqrstuvwxyzabcdef",
   3108  1.1.1.2  christos         NULL, 32, 32 },
   3109  1.1.1.2  christos     { EVP_PKEY_X448,
   3110      1.1  christos         (const uint8_t *)"01234567890123456789012345678901234567890123456789012345",
   3111      1.1  christos         (const uint8_t *)"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd",
   3112  1.1.1.2  christos         NULL, 56, 56 },
   3113  1.1.1.2  christos     { EVP_PKEY_ED448,
   3114      1.1  christos         (const uint8_t *)"012345678901234567890123456789012345678901234567890123456",
   3115      1.1  christos         (const uint8_t *)"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde",
   3116  1.1.1.2  christos         NULL, 57, 57 },
   3117      1.1  christos #endif
   3118      1.1  christos #ifndef OPENSSL_NO_ML_KEM
   3119  1.1.1.2  christos     { NID_undef, ml_kem_seed, ml_kem_512_pubkey,
   3120  1.1.1.2  christos         "ML-KEM-512", ML_KEM_SEED_BYTES, sizeof(ml_kem_512_pubkey) },
   3121  1.1.1.2  christos     { NID_undef, ml_kem_seed, ml_kem_768_pubkey,
   3122  1.1.1.2  christos         "ML-KEM-768", ML_KEM_SEED_BYTES, sizeof(ml_kem_768_pubkey) },
   3123  1.1.1.2  christos     { NID_undef, ml_kem_seed, ml_kem_1024_pubkey,
   3124  1.1.1.2  christos         "ML-KEM-1024", ML_KEM_SEED_BYTES, sizeof(ml_kem_1024_pubkey) },
   3125      1.1  christos #endif
   3126      1.1  christos };
   3127      1.1  christos 
   3128      1.1  christos #ifndef OPENSSL_NO_ML_KEM
   3129      1.1  christos static int
   3130      1.1  christos ml_kem_seed_to_priv(const char *alg, const unsigned char *seed, int seedlen,
   3131  1.1.1.2  christos     unsigned char **ret, size_t *retlen)
   3132      1.1  christos {
   3133      1.1  christos     OSSL_PARAM parr[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   3134      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   3135      1.1  christos     EVP_PKEY *pkey = NULL;
   3136      1.1  christos     const OSSL_PARAM *p;
   3137      1.1  christos     OSSL_PARAM *params = NULL;
   3138      1.1  christos     int selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
   3139      1.1  christos     int ok = 0;
   3140      1.1  christos 
   3141      1.1  christos     /* Import the seed to generate a key */
   3142      1.1  christos     ctx = EVP_PKEY_CTX_new_from_name(testctx, alg, NULL);
   3143      1.1  christos     if (!TEST_ptr(ctx)
   3144      1.1  christos         || !TEST_int_gt(EVP_PKEY_fromdata_init(ctx), 0))
   3145      1.1  christos         goto done;
   3146      1.1  christos     parr[0] = OSSL_PARAM_construct_octet_string(
   3147      1.1  christos         OSSL_PKEY_PARAM_ML_KEM_SEED, (unsigned char *)seed, seedlen);
   3148      1.1  christos     if (!TEST_int_gt(EVP_PKEY_fromdata(ctx, &pkey, selection, parr), 0))
   3149      1.1  christos         goto done;
   3150      1.1  christos 
   3151      1.1  christos     /* Export the key to get the encoded form */
   3152      1.1  christos     if (!TEST_true(EVP_PKEY_todata(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, &params)))
   3153      1.1  christos         goto done;
   3154      1.1  christos 
   3155      1.1  christos     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
   3156      1.1  christos     if (!TEST_ptr(p)
   3157      1.1  christos         || !TEST_true(OSSL_PARAM_get_octet_string(p, (void **)ret, 0, retlen)))
   3158      1.1  christos         goto done;
   3159      1.1  christos     ok = 1;
   3160      1.1  christos 
   3161  1.1.1.2  christos done:
   3162      1.1  christos     EVP_PKEY_free(pkey);
   3163      1.1  christos     OSSL_PARAM_free(params);
   3164      1.1  christos     EVP_PKEY_CTX_free(ctx);
   3165      1.1  christos     return ok;
   3166      1.1  christos }
   3167      1.1  christos #endif
   3168      1.1  christos 
   3169      1.1  christos static int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
   3170      1.1  christos {
   3171      1.1  christos     int ret = 0;
   3172      1.1  christos     uint8_t *buf = NULL;
   3173      1.1  christos     const uint8_t *in;
   3174      1.1  christos     uint8_t shortbuf[1];
   3175      1.1  christos     size_t inlen, len = 0, shortlen = sizeof(shortbuf);
   3176      1.1  christos     EVP_PKEY *pkey = NULL;
   3177      1.1  christos     unsigned char *privalloc = NULL;
   3178      1.1  christos     const char *name;
   3179      1.1  christos 
   3180      1.1  christos     /* Check if this algorithm supports public keys */
   3181      1.1  christos     if (pub && keys[tst].pub == NULL)
   3182      1.1  christos         return 1;
   3183      1.1  christos 
   3184  1.1.1.2  christos     name = keys[tst].name ? keys[tst].name : OBJ_nid2sn(keys[tst].type);
   3185      1.1  christos 
   3186      1.1  christos     if (pub) {
   3187      1.1  christos #ifndef OPENSSL_NO_EC
   3188      1.1  christos         inlen = keys[tst].publen;
   3189      1.1  christos         in = keys[tst].pub;
   3190      1.1  christos         if (uselibctx || keys[tst].name != NULL) {
   3191      1.1  christos             pkey = EVP_PKEY_new_raw_public_key_ex(
   3192  1.1.1.2  christos                 testctx,
   3193  1.1.1.2  christos                 name,
   3194  1.1.1.2  christos                 NULL,
   3195  1.1.1.2  christos                 in,
   3196  1.1.1.2  christos                 inlen);
   3197      1.1  christos         } else {
   3198      1.1  christos             pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
   3199  1.1.1.2  christos                 NULL,
   3200  1.1.1.2  christos                 in,
   3201  1.1.1.2  christos                 inlen);
   3202      1.1  christos         }
   3203      1.1  christos #else
   3204      1.1  christos         return 1;
   3205      1.1  christos #endif
   3206      1.1  christos     } else {
   3207      1.1  christos         inlen = keys[tst].privlen;
   3208      1.1  christos         in = keys[tst].priv;
   3209      1.1  christos #ifndef OPENSSL_NO_ML_KEM
   3210      1.1  christos         if (in == ml_kem_seed) {
   3211      1.1  christos             if (!TEST_true(ml_kem_seed_to_priv(name, in, inlen,
   3212  1.1.1.2  christos                     &privalloc, &inlen)))
   3213      1.1  christos                 goto done;
   3214      1.1  christos             in = privalloc;
   3215      1.1  christos         }
   3216      1.1  christos #endif
   3217      1.1  christos         if (uselibctx || keys[tst].name != NULL) {
   3218      1.1  christos             pkey = EVP_PKEY_new_raw_private_key_ex(
   3219  1.1.1.2  christos                 testctx,
   3220  1.1.1.2  christos                 name,
   3221  1.1.1.2  christos                 NULL,
   3222  1.1.1.2  christos                 in,
   3223  1.1.1.2  christos                 inlen);
   3224      1.1  christos         } else {
   3225      1.1  christos             pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
   3226  1.1.1.2  christos                 NULL,
   3227  1.1.1.2  christos                 in,
   3228  1.1.1.2  christos                 inlen);
   3229      1.1  christos         }
   3230      1.1  christos     }
   3231      1.1  christos 
   3232      1.1  christos     if (!TEST_ptr(pkey)
   3233  1.1.1.2  christos         || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey), 1)
   3234  1.1.1.2  christos         || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
   3235  1.1.1.2  christos         || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
   3236  1.1.1.2  christos         || !TEST_true(len == inlen))
   3237      1.1  christos         goto done;
   3238      1.1  christos     if (tst != 1) {
   3239      1.1  christos         /*
   3240      1.1  christos          * Test that supplying a buffer that is too small fails. Doesn't apply
   3241      1.1  christos          * to HMAC with a zero length key
   3242      1.1  christos          */
   3243  1.1.1.2  christos         if ((!pub && !TEST_false(EVP_PKEY_get_raw_private_key(pkey, shortbuf, &shortlen)))
   3244  1.1.1.2  christos             || (pub && !TEST_false(EVP_PKEY_get_raw_public_key(pkey, shortbuf, &shortlen))))
   3245      1.1  christos             goto done;
   3246      1.1  christos     }
   3247      1.1  christos     if (!TEST_ptr(buf = OPENSSL_zalloc(len <= 80 ? 80 : len)))
   3248      1.1  christos         goto done;
   3249      1.1  christos     if ((!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
   3250  1.1.1.2  christos         || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
   3251  1.1.1.2  christos         || !TEST_mem_eq(in, inlen, buf, len))
   3252      1.1  christos         goto done;
   3253      1.1  christos 
   3254      1.1  christos     ret = 1;
   3255  1.1.1.2  christos done:
   3256      1.1  christos     OPENSSL_free(privalloc);
   3257      1.1  christos     OPENSSL_free(buf);
   3258      1.1  christos     EVP_PKEY_free(pkey);
   3259      1.1  christos     return ret;
   3260      1.1  christos }
   3261      1.1  christos 
   3262      1.1  christos static int test_set_get_raw_keys(int tst)
   3263      1.1  christos {
   3264      1.1  christos     return (nullprov != NULL || test_set_get_raw_keys_int(tst, 0, 0))
   3265  1.1.1.2  christos         && test_set_get_raw_keys_int(tst, 0, 1)
   3266  1.1.1.2  christos         && (nullprov != NULL || test_set_get_raw_keys_int(tst, 1, 0))
   3267  1.1.1.2  christos         && test_set_get_raw_keys_int(tst, 1, 1);
   3268      1.1  christos }
   3269      1.1  christos 
   3270      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   3271      1.1  christos static int pkey_custom_check(EVP_PKEY *pkey)
   3272      1.1  christos {
   3273      1.1  christos     return 0xbeef;
   3274      1.1  christos }
   3275      1.1  christos 
   3276      1.1  christos static int pkey_custom_pub_check(EVP_PKEY *pkey)
   3277      1.1  christos {
   3278      1.1  christos     return 0xbeef;
   3279      1.1  christos }
   3280      1.1  christos 
   3281      1.1  christos static int pkey_custom_param_check(EVP_PKEY *pkey)
   3282      1.1  christos {
   3283      1.1  christos     return 0xbeef;
   3284      1.1  christos }
   3285      1.1  christos 
   3286      1.1  christos static EVP_PKEY_METHOD *custom_pmeth;
   3287      1.1  christos #endif
   3288      1.1  christos 
   3289      1.1  christos static int test_EVP_PKEY_check(int i)
   3290      1.1  christos {
   3291      1.1  christos     int ret = 0;
   3292      1.1  christos     EVP_PKEY *pkey = NULL;
   3293      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   3294      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   3295      1.1  christos     EVP_PKEY_CTX *ctx2 = NULL;
   3296      1.1  christos #endif
   3297      1.1  christos     const APK_DATA *ak = &keycheckdata[i];
   3298      1.1  christos     const unsigned char *input = ak->kder;
   3299      1.1  christos     size_t input_len = ak->size;
   3300      1.1  christos     int expected_id = ak->evptype;
   3301      1.1  christos     int expected_check = ak->check;
   3302      1.1  christos     int expected_pub_check = ak->pub_check;
   3303      1.1  christos     int expected_param_check = ak->param_check;
   3304      1.1  christos     int type = ak->type;
   3305      1.1  christos 
   3306      1.1  christos     if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len)))
   3307      1.1  christos         goto done;
   3308      1.1  christos     if (type == 0
   3309      1.1  christos         && !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
   3310      1.1  christos         goto done;
   3311      1.1  christos 
   3312      1.1  christos     if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
   3313      1.1  christos         goto done;
   3314      1.1  christos 
   3315      1.1  christos     if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
   3316      1.1  christos         goto done;
   3317      1.1  christos 
   3318      1.1  christos     if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
   3319      1.1  christos         goto done;
   3320      1.1  christos 
   3321      1.1  christos     if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
   3322      1.1  christos         goto done;
   3323      1.1  christos 
   3324      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   3325      1.1  christos     ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
   3326      1.1  christos     /* assign the pkey directly, as an internal test */
   3327      1.1  christos     if (!EVP_PKEY_up_ref(pkey))
   3328      1.1  christos         goto done;
   3329      1.1  christos 
   3330      1.1  christos     ctx2->pkey = pkey;
   3331      1.1  christos 
   3332      1.1  christos     if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
   3333      1.1  christos         goto done;
   3334      1.1  christos 
   3335      1.1  christos     if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
   3336      1.1  christos         goto done;
   3337      1.1  christos 
   3338      1.1  christos     if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
   3339      1.1  christos         goto done;
   3340      1.1  christos #endif
   3341      1.1  christos 
   3342      1.1  christos     ret = 1;
   3343      1.1  christos 
   3344  1.1.1.2  christos done:
   3345      1.1  christos     EVP_PKEY_CTX_free(ctx);
   3346      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   3347      1.1  christos     EVP_PKEY_CTX_free(ctx2);
   3348      1.1  christos #endif
   3349      1.1  christos     EVP_PKEY_free(pkey);
   3350      1.1  christos     return ret;
   3351      1.1  christos }
   3352      1.1  christos 
   3353      1.1  christos #ifndef OPENSSL_NO_CMAC
   3354      1.1  christos static int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
   3355      1.1  christos {
   3356      1.1  christos     EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
   3357      1.1  christos     const char msg[] = "Hello World";
   3358      1.1  christos     size_t maclen = AES_BLOCK_SIZE;
   3359      1.1  christos     int ret = 1;
   3360      1.1  christos 
   3361      1.1  christos     if (!TEST_ptr(mdctx)
   3362  1.1.1.2  christos         || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx,
   3363  1.1.1.2  christos             testpropq, pkey, NULL))
   3364  1.1.1.2  christos         || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
   3365  1.1.1.2  christos         || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
   3366  1.1.1.2  christos         || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
   3367      1.1  christos         ret = 0;
   3368      1.1  christos 
   3369      1.1  christos     EVP_MD_CTX_free(mdctx);
   3370      1.1  christos 
   3371      1.1  christos     return ret;
   3372      1.1  christos }
   3373      1.1  christos static int test_CMAC_keygen(void)
   3374      1.1  christos {
   3375      1.1  christos     static unsigned char key[] = {
   3376      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   3377      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   3378      1.1  christos         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
   3379      1.1  christos     };
   3380      1.1  christos     EVP_PKEY_CTX *kctx = NULL;
   3381      1.1  christos     int ret = 0;
   3382      1.1  christos     EVP_PKEY *pkey = NULL;
   3383      1.1  christos     unsigned char mac[AES_BLOCK_SIZE];
   3384  1.1.1.2  christos #if !defined(OPENSSL_NO_DEPRECATED_3_0)
   3385      1.1  christos     unsigned char mac2[AES_BLOCK_SIZE];
   3386  1.1.1.2  christos #endif
   3387      1.1  christos 
   3388      1.1  christos     if (nullprov != NULL)
   3389      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   3390      1.1  christos 
   3391      1.1  christos     /*
   3392      1.1  christos      * This is a legacy method for CMACs, but should still work.
   3393      1.1  christos      * This verifies that it works without an ENGINE.
   3394      1.1  christos      */
   3395      1.1  christos     kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
   3396      1.1  christos 
   3397      1.1  christos     /* Test a CMAC key created using the "generated" method */
   3398      1.1  christos     if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
   3399  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
   3400  1.1.1.2  christos                             EVP_PKEY_CTRL_CIPHER,
   3401  1.1.1.2  christos                             0, (void *)EVP_aes_256_cbc()),
   3402  1.1.1.2  christos             0)
   3403  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
   3404  1.1.1.2  christos                             EVP_PKEY_CTRL_SET_MAC_KEY,
   3405  1.1.1.2  christos                             sizeof(key), (void *)key),
   3406  1.1.1.2  christos             0)
   3407  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
   3408  1.1.1.2  christos         || !TEST_ptr(pkey)
   3409  1.1.1.2  christos         || !TEST_true(get_cmac_val(pkey, mac)))
   3410      1.1  christos         goto done;
   3411      1.1  christos 
   3412  1.1.1.2  christos #if !defined(OPENSSL_NO_DEPRECATED_3_0)
   3413      1.1  christos     EVP_PKEY_free(pkey);
   3414      1.1  christos 
   3415      1.1  christos     /*
   3416      1.1  christos      * Test a CMAC key using the direct method, and compare with the mac
   3417      1.1  christos      * created above.
   3418      1.1  christos      */
   3419      1.1  christos     pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_cbc());
   3420      1.1  christos     if (!TEST_ptr(pkey)
   3421  1.1.1.2  christos         || !TEST_true(get_cmac_val(pkey, mac2))
   3422  1.1.1.2  christos         || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
   3423      1.1  christos         goto done;
   3424  1.1.1.2  christos #endif
   3425      1.1  christos 
   3426      1.1  christos     ret = 1;
   3427      1.1  christos 
   3428  1.1.1.2  christos done:
   3429      1.1  christos     EVP_PKEY_free(pkey);
   3430      1.1  christos     EVP_PKEY_CTX_free(kctx);
   3431      1.1  christos     return ret;
   3432      1.1  christos }
   3433      1.1  christos #endif
   3434      1.1  christos 
   3435      1.1  christos static int test_HKDF(void)
   3436      1.1  christos {
   3437      1.1  christos     EVP_PKEY_CTX *pctx;
   3438      1.1  christos     unsigned char out[20];
   3439      1.1  christos     size_t outlen;
   3440      1.1  christos     int i, ret = 0;
   3441      1.1  christos     unsigned char salt[] = "0123456789";
   3442      1.1  christos     unsigned char key[] = "012345678901234567890123456789";
   3443      1.1  christos     unsigned char info[] = "infostring";
   3444      1.1  christos     const unsigned char expected[] = {
   3445      1.1  christos         0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
   3446      1.1  christos         0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
   3447      1.1  christos     };
   3448      1.1  christos     size_t expectedlen = sizeof(expected);
   3449      1.1  christos 
   3450      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
   3451      1.1  christos         goto done;
   3452      1.1  christos 
   3453      1.1  christos     /* We do this twice to test reuse of the EVP_PKEY_CTX */
   3454      1.1  christos     for (i = 0; i < 2; i++) {
   3455      1.1  christos         outlen = sizeof(out);
   3456      1.1  christos         memset(out, 0, outlen);
   3457      1.1  christos 
   3458      1.1  christos         if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
   3459  1.1.1.2  christos             || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
   3460  1.1.1.2  christos             || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
   3461  1.1.1.2  christos                                 sizeof(salt) - 1),
   3462  1.1.1.2  christos                 0)
   3463  1.1.1.2  christos             || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
   3464  1.1.1.2  christos                                 sizeof(key) - 1),
   3465  1.1.1.2  christos                 0)
   3466  1.1.1.2  christos             || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
   3467  1.1.1.2  christos                                 sizeof(info) - 1),
   3468  1.1.1.2  christos                 0)
   3469  1.1.1.2  christos             || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
   3470  1.1.1.2  christos             || !TEST_mem_eq(out, outlen, expected, expectedlen))
   3471      1.1  christos             goto done;
   3472      1.1  christos     }
   3473      1.1  christos 
   3474      1.1  christos     ret = 1;
   3475      1.1  christos 
   3476  1.1.1.2  christos done:
   3477      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3478      1.1  christos 
   3479      1.1  christos     return ret;
   3480      1.1  christos }
   3481      1.1  christos 
   3482      1.1  christos static int test_emptyikm_HKDF(void)
   3483      1.1  christos {
   3484      1.1  christos     EVP_PKEY_CTX *pctx;
   3485      1.1  christos     unsigned char out[20];
   3486      1.1  christos     size_t outlen;
   3487      1.1  christos     int ret = 0;
   3488      1.1  christos     unsigned char salt[] = "9876543210";
   3489      1.1  christos     unsigned char key[] = "";
   3490      1.1  christos     unsigned char info[] = "stringinfo";
   3491      1.1  christos     const unsigned char expected[] = {
   3492      1.1  christos         0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
   3493      1.1  christos         0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
   3494      1.1  christos     };
   3495      1.1  christos     size_t expectedlen = sizeof(expected);
   3496      1.1  christos 
   3497      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
   3498      1.1  christos         goto done;
   3499      1.1  christos 
   3500      1.1  christos     outlen = sizeof(out);
   3501      1.1  christos     memset(out, 0, outlen);
   3502      1.1  christos 
   3503      1.1  christos     if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
   3504  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
   3505  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
   3506  1.1.1.2  christos                             sizeof(salt) - 1),
   3507  1.1.1.2  christos             0)
   3508  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
   3509  1.1.1.2  christos                             sizeof(key) - 1),
   3510  1.1.1.2  christos             0)
   3511  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
   3512  1.1.1.2  christos                             sizeof(info) - 1),
   3513  1.1.1.2  christos             0)
   3514  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
   3515  1.1.1.2  christos         || !TEST_mem_eq(out, outlen, expected, expectedlen))
   3516      1.1  christos         goto done;
   3517      1.1  christos 
   3518      1.1  christos     ret = 1;
   3519      1.1  christos 
   3520  1.1.1.2  christos done:
   3521      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3522      1.1  christos 
   3523      1.1  christos     return ret;
   3524      1.1  christos }
   3525      1.1  christos 
   3526      1.1  christos static int test_empty_salt_info_HKDF(void)
   3527      1.1  christos {
   3528      1.1  christos     EVP_PKEY_CTX *pctx;
   3529      1.1  christos     unsigned char out[20];
   3530      1.1  christos     size_t outlen;
   3531      1.1  christos     int ret = 0;
   3532      1.1  christos     unsigned char salt[] = "";
   3533      1.1  christos     unsigned char fake[] = "0123456789";
   3534      1.1  christos     unsigned char key[] = "012345678901234567890123456789";
   3535      1.1  christos     unsigned char info[] = "";
   3536      1.1  christos     const unsigned char expected[] = {
   3537  1.1.1.2  christos         0x67, 0x12, 0xf9, 0x27, 0x8a, 0x8a, 0x3a, 0x8f, 0x7d, 0x2c, 0xa3, 0x6a,
   3538  1.1.1.2  christos         0xaa, 0xe9, 0xb3, 0xb9, 0x52, 0x5f, 0xe0, 0x06
   3539      1.1  christos     };
   3540      1.1  christos     size_t expectedlen = sizeof(expected);
   3541      1.1  christos 
   3542      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
   3543      1.1  christos         goto done;
   3544      1.1  christos 
   3545      1.1  christos     outlen = sizeof(out);
   3546      1.1  christos     memset(out, 0, outlen);
   3547      1.1  christos 
   3548      1.1  christos     if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
   3549  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
   3550  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, fake,
   3551  1.1.1.2  christos                             sizeof(fake) - 1),
   3552  1.1.1.2  christos             0)
   3553  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
   3554  1.1.1.2  christos                             sizeof(salt) - 1),
   3555  1.1.1.2  christos             0)
   3556  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
   3557  1.1.1.2  christos                             sizeof(key) - 1),
   3558  1.1.1.2  christos             0)
   3559  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
   3560  1.1.1.2  christos                             sizeof(info) - 1),
   3561  1.1.1.2  christos             0)
   3562  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
   3563  1.1.1.2  christos         || !TEST_mem_eq(out, outlen, expected, expectedlen))
   3564      1.1  christos         goto done;
   3565      1.1  christos 
   3566      1.1  christos     ret = 1;
   3567      1.1  christos 
   3568  1.1.1.2  christos done:
   3569      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3570      1.1  christos 
   3571      1.1  christos     return ret;
   3572      1.1  christos }
   3573      1.1  christos 
   3574      1.1  christos #ifndef OPENSSL_NO_EC
   3575      1.1  christos static int test_X509_PUBKEY_inplace(void)
   3576      1.1  christos {
   3577      1.1  christos     int ret = 0;
   3578      1.1  christos     X509_PUBKEY *xp = X509_PUBKEY_new_ex(testctx, testpropq);
   3579      1.1  christos     const unsigned char *p = kExampleECPubKeyDER;
   3580      1.1  christos     size_t input_len = sizeof(kExampleECPubKeyDER);
   3581      1.1  christos 
   3582      1.1  christos     if (!TEST_ptr(xp))
   3583      1.1  christos         goto done;
   3584      1.1  christos     if (!TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len)))
   3585      1.1  christos         goto done;
   3586      1.1  christos 
   3587      1.1  christos     if (!TEST_ptr(X509_PUBKEY_get0(xp)))
   3588      1.1  christos         goto done;
   3589      1.1  christos 
   3590      1.1  christos     p = kExampleBadECPubKeyDER;
   3591      1.1  christos     input_len = sizeof(kExampleBadECPubKeyDER);
   3592      1.1  christos 
   3593      1.1  christos     if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
   3594      1.1  christos         goto done;
   3595      1.1  christos 
   3596      1.1  christos     if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
   3597      1.1  christos         goto done;
   3598      1.1  christos 
   3599      1.1  christos     ret = 1;
   3600      1.1  christos 
   3601  1.1.1.2  christos done:
   3602      1.1  christos     X509_PUBKEY_free(xp);
   3603      1.1  christos     return ret;
   3604      1.1  christos }
   3605      1.1  christos 
   3606      1.1  christos static int test_X509_PUBKEY_dup(void)
   3607      1.1  christos {
   3608      1.1  christos     int ret = 0;
   3609      1.1  christos     X509_PUBKEY *xp = NULL, *xq = NULL;
   3610      1.1  christos     const unsigned char *p = kExampleECPubKeyDER;
   3611      1.1  christos     size_t input_len = sizeof(kExampleECPubKeyDER);
   3612      1.1  christos 
   3613      1.1  christos     xp = X509_PUBKEY_new_ex(testctx, testpropq);
   3614      1.1  christos     if (!TEST_ptr(xp)
   3615  1.1.1.2  christos         || !TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len))
   3616  1.1.1.2  christos         || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
   3617  1.1.1.2  christos         || !TEST_ptr_ne(xp, xq))
   3618      1.1  christos         goto done;
   3619      1.1  christos 
   3620      1.1  christos     if (!TEST_ptr(X509_PUBKEY_get0(xq))
   3621  1.1.1.2  christos         || !TEST_ptr(X509_PUBKEY_get0(xp))
   3622  1.1.1.2  christos         || !TEST_ptr_ne(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
   3623      1.1  christos         goto done;
   3624      1.1  christos 
   3625      1.1  christos     X509_PUBKEY_free(xq);
   3626      1.1  christos     xq = NULL;
   3627      1.1  christos     p = kExampleBadECPubKeyDER;
   3628      1.1  christos     input_len = sizeof(kExampleBadECPubKeyDER);
   3629      1.1  christos 
   3630      1.1  christos     if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
   3631  1.1.1.2  christos         || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
   3632      1.1  christos         goto done;
   3633      1.1  christos 
   3634      1.1  christos     X509_PUBKEY_free(xp);
   3635      1.1  christos     xp = NULL;
   3636      1.1  christos     if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
   3637      1.1  christos         goto done;
   3638      1.1  christos 
   3639      1.1  christos     ret = 1;
   3640      1.1  christos 
   3641  1.1.1.2  christos done:
   3642      1.1  christos     X509_PUBKEY_free(xp);
   3643      1.1  christos     X509_PUBKEY_free(xq);
   3644      1.1  christos     return ret;
   3645      1.1  christos }
   3646      1.1  christos #endif /* OPENSSL_NO_EC */
   3647      1.1  christos 
   3648      1.1  christos /* Test getting and setting parameters on an EVP_PKEY_CTX */
   3649      1.1  christos static int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
   3650      1.1  christos {
   3651      1.1  christos     EVP_MD_CTX *mdctx = NULL;
   3652      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   3653      1.1  christos     const OSSL_PARAM *params;
   3654      1.1  christos     OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
   3655      1.1  christos     int ret = 0;
   3656      1.1  christos     const EVP_MD *md;
   3657      1.1  christos     char mdname[OSSL_MAX_NAME_SIZE];
   3658      1.1  christos     char ssl3ms[48];
   3659      1.1  christos 
   3660      1.1  christos     /* Initialise a sign operation */
   3661      1.1  christos     ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
   3662      1.1  christos     if (!TEST_ptr(ctx)
   3663  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
   3664      1.1  christos         goto err;
   3665      1.1  christos 
   3666      1.1  christos     /*
   3667      1.1  christos      * We should be able to query the parameters now.
   3668      1.1  christos      */
   3669      1.1  christos     params = EVP_PKEY_CTX_settable_params(ctx);
   3670      1.1  christos     if (!TEST_ptr(params)
   3671      1.1  christos         || !TEST_ptr(OSSL_PARAM_locate_const(params,
   3672  1.1.1.2  christos             OSSL_SIGNATURE_PARAM_DIGEST)))
   3673      1.1  christos         goto err;
   3674      1.1  christos 
   3675      1.1  christos     params = EVP_PKEY_CTX_gettable_params(ctx);
   3676      1.1  christos     if (!TEST_ptr(params)
   3677      1.1  christos         || !TEST_ptr(OSSL_PARAM_locate_const(params,
   3678  1.1.1.2  christos             OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
   3679      1.1  christos         || !TEST_ptr(OSSL_PARAM_locate_const(params,
   3680  1.1.1.2  christos             OSSL_SIGNATURE_PARAM_DIGEST)))
   3681      1.1  christos         goto err;
   3682      1.1  christos 
   3683      1.1  christos     /*
   3684      1.1  christos      * Test getting and setting params via EVP_PKEY_CTX_set_params() and
   3685      1.1  christos      * EVP_PKEY_CTX_get_params()
   3686      1.1  christos      */
   3687      1.1  christos     strcpy(mdname, "SHA512");
   3688      1.1  christos     param_md = param;
   3689      1.1  christos     *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
   3690  1.1.1.2  christos         mdname, 0);
   3691      1.1  christos     *param++ = OSSL_PARAM_construct_end();
   3692      1.1  christos 
   3693      1.1  christos     if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
   3694      1.1  christos         goto err;
   3695      1.1  christos 
   3696      1.1  christos     mdname[0] = '\0';
   3697      1.1  christos     *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
   3698  1.1.1.2  christos         mdname, sizeof(mdname));
   3699      1.1  christos     if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
   3700  1.1.1.2  christos         || !TEST_str_eq(mdname, "SHA512"))
   3701      1.1  christos         goto err;
   3702      1.1  christos 
   3703      1.1  christos     /*
   3704      1.1  christos      * Test the TEST_PKEY_CTX_set_signature_md() and
   3705      1.1  christos      * TEST_PKEY_CTX_get_signature_md() functions
   3706      1.1  christos      */
   3707      1.1  christos     if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
   3708  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
   3709  1.1.1.2  christos         || !TEST_ptr_eq(md, EVP_sha256()))
   3710      1.1  christos         goto err;
   3711      1.1  christos 
   3712      1.1  christos     /*
   3713      1.1  christos      * Test getting MD parameters via an associated EVP_PKEY_CTX
   3714      1.1  christos      */
   3715      1.1  christos     mdctx = EVP_MD_CTX_new();
   3716      1.1  christos     if (!TEST_ptr(mdctx)
   3717      1.1  christos         || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
   3718  1.1.1.2  christos             pkey, NULL)))
   3719      1.1  christos         goto err;
   3720      1.1  christos 
   3721      1.1  christos     /*
   3722      1.1  christos      * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
   3723      1.1  christos      * able to obtain the digest's settable parameters from the provider.
   3724      1.1  christos      */
   3725      1.1  christos     params = EVP_MD_CTX_settable_params(mdctx);
   3726      1.1  christos     if (!TEST_ptr(params)
   3727  1.1.1.2  christos         || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
   3728  1.1.1.2  christos         /* The final key should be NULL */
   3729  1.1.1.2  christos         || !TEST_ptr_null(params[1].key))
   3730      1.1  christos         goto err;
   3731      1.1  christos 
   3732      1.1  christos     param = ourparams;
   3733      1.1  christos     memset(ssl3ms, 0, sizeof(ssl3ms));
   3734      1.1  christos     *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
   3735  1.1.1.2  christos         ssl3ms, sizeof(ssl3ms));
   3736      1.1  christos     *param++ = OSSL_PARAM_construct_end();
   3737      1.1  christos 
   3738      1.1  christos     if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
   3739      1.1  christos         goto err;
   3740      1.1  christos 
   3741      1.1  christos     ret = 1;
   3742      1.1  christos 
   3743  1.1.1.2  christos err:
   3744      1.1  christos     EVP_MD_CTX_free(mdctx);
   3745      1.1  christos     EVP_PKEY_CTX_free(ctx);
   3746      1.1  christos 
   3747      1.1  christos     return ret;
   3748      1.1  christos }
   3749      1.1  christos 
   3750      1.1  christos #ifndef OPENSSL_NO_DSA
   3751      1.1  christos static int test_DSA_get_set_params(void)
   3752      1.1  christos {
   3753      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   3754      1.1  christos     OSSL_PARAM *params = NULL;
   3755      1.1  christos     BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
   3756      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   3757      1.1  christos     EVP_PKEY *pkey = NULL;
   3758      1.1  christos     int ret = 0;
   3759      1.1  christos 
   3760      1.1  christos     /*
   3761      1.1  christos      * Setup the parameters for our DSA object. For our purposes they don't
   3762      1.1  christos      * have to actually be *valid* parameters. We just need to set something.
   3763      1.1  christos      */
   3764      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
   3765      1.1  christos         || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
   3766      1.1  christos         || !TEST_ptr(p = BN_new())
   3767      1.1  christos         || !TEST_ptr(q = BN_new())
   3768      1.1  christos         || !TEST_ptr(g = BN_new())
   3769      1.1  christos         || !TEST_ptr(pub = BN_new())
   3770      1.1  christos         || !TEST_ptr(priv = BN_new()))
   3771      1.1  christos         goto err;
   3772      1.1  christos     if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
   3773      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
   3774      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
   3775      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
   3776  1.1.1.2  christos             pub))
   3777      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
   3778  1.1.1.2  christos             priv)))
   3779      1.1  christos         goto err;
   3780      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
   3781      1.1  christos         goto err;
   3782      1.1  christos 
   3783      1.1  christos     if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
   3784      1.1  christos         || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
   3785  1.1.1.2  christos                             params),
   3786  1.1.1.2  christos             0))
   3787      1.1  christos         goto err;
   3788      1.1  christos 
   3789      1.1  christos     if (!TEST_ptr(pkey))
   3790      1.1  christos         goto err;
   3791      1.1  christos 
   3792      1.1  christos     ret = test_EVP_PKEY_CTX_get_set_params(pkey);
   3793      1.1  christos 
   3794  1.1.1.2  christos err:
   3795      1.1  christos     EVP_PKEY_free(pkey);
   3796      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3797      1.1  christos     OSSL_PARAM_free(params);
   3798      1.1  christos     OSSL_PARAM_BLD_free(bld);
   3799      1.1  christos     BN_free(p);
   3800      1.1  christos     BN_free(q);
   3801      1.1  christos     BN_free(g);
   3802      1.1  christos     BN_free(pub);
   3803      1.1  christos     BN_free(priv);
   3804      1.1  christos 
   3805      1.1  christos     return ret;
   3806      1.1  christos }
   3807      1.1  christos 
   3808      1.1  christos /*
   3809      1.1  christos  * Test combinations of private, public, missing and private + public key
   3810      1.1  christos  * params to ensure they are all accepted
   3811      1.1  christos  */
   3812      1.1  christos static int test_DSA_priv_pub(void)
   3813      1.1  christos {
   3814      1.1  christos     return test_EVP_PKEY_ffc_priv_pub("DSA");
   3815      1.1  christos }
   3816      1.1  christos 
   3817      1.1  christos #endif /* !OPENSSL_NO_DSA */
   3818      1.1  christos 
   3819      1.1  christos static int test_RSA_get_set_params(void)
   3820      1.1  christos {
   3821      1.1  christos     OSSL_PARAM_BLD *bld = NULL;
   3822      1.1  christos     OSSL_PARAM *params = NULL;
   3823      1.1  christos     BIGNUM *n = NULL, *e = NULL, *d = NULL;
   3824      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   3825      1.1  christos     EVP_PKEY *pkey = NULL;
   3826      1.1  christos     int ret = 0;
   3827      1.1  christos 
   3828      1.1  christos     /*
   3829      1.1  christos      * Setup the parameters for our RSA object. For our purposes they don't
   3830      1.1  christos      * have to actually be *valid* parameters. We just need to set something.
   3831      1.1  christos      */
   3832      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
   3833      1.1  christos         || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
   3834      1.1  christos         || !TEST_ptr(n = BN_new())
   3835      1.1  christos         || !TEST_ptr(e = BN_new())
   3836      1.1  christos         || !TEST_ptr(d = BN_new()))
   3837      1.1  christos         goto err;
   3838      1.1  christos     if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
   3839      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
   3840      1.1  christos         || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
   3841      1.1  christos         goto err;
   3842      1.1  christos     if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
   3843      1.1  christos         goto err;
   3844      1.1  christos 
   3845      1.1  christos     if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
   3846      1.1  christos         || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
   3847  1.1.1.2  christos                             params),
   3848  1.1.1.2  christos             0))
   3849      1.1  christos         goto err;
   3850      1.1  christos 
   3851      1.1  christos     if (!TEST_ptr(pkey))
   3852      1.1  christos         goto err;
   3853      1.1  christos 
   3854      1.1  christos     ret = test_EVP_PKEY_CTX_get_set_params(pkey);
   3855      1.1  christos 
   3856  1.1.1.2  christos err:
   3857      1.1  christos     EVP_PKEY_free(pkey);
   3858      1.1  christos     EVP_PKEY_CTX_free(pctx);
   3859      1.1  christos     OSSL_PARAM_free(params);
   3860      1.1  christos     OSSL_PARAM_BLD_free(bld);
   3861      1.1  christos     BN_free(n);
   3862      1.1  christos     BN_free(e);
   3863      1.1  christos     BN_free(d);
   3864      1.1  christos 
   3865      1.1  christos     return ret;
   3866      1.1  christos }
   3867      1.1  christos 
   3868      1.1  christos static int test_RSA_OAEP_set_get_params(void)
   3869      1.1  christos {
   3870      1.1  christos     int ret = 0;
   3871      1.1  christos     EVP_PKEY *key = NULL;
   3872      1.1  christos     EVP_PKEY_CTX *key_ctx = NULL;
   3873      1.1  christos 
   3874      1.1  christos     if (nullprov != NULL)
   3875      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   3876      1.1  christos 
   3877      1.1  christos     if (!TEST_ptr(key = load_example_rsa_key())
   3878      1.1  christos         || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(0, key, 0)))
   3879      1.1  christos         goto err;
   3880      1.1  christos 
   3881      1.1  christos     {
   3882      1.1  christos         int padding = RSA_PKCS1_OAEP_PADDING;
   3883      1.1  christos         OSSL_PARAM params[4];
   3884      1.1  christos 
   3885      1.1  christos         params[0] = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PAD_MODE, &padding);
   3886      1.1  christos         params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
   3887  1.1.1.2  christos             OSSL_DIGEST_NAME_SHA2_256, 0);
   3888      1.1  christos         params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
   3889  1.1.1.2  christos             OSSL_DIGEST_NAME_SHA1, 0);
   3890      1.1  christos         params[3] = OSSL_PARAM_construct_end();
   3891      1.1  christos 
   3892  1.1.1.2  christos         if (!TEST_int_gt(EVP_PKEY_encrypt_init_ex(key_ctx, params), 0))
   3893      1.1  christos             goto err;
   3894      1.1  christos     }
   3895      1.1  christos     {
   3896      1.1  christos         OSSL_PARAM params[3];
   3897      1.1  christos         char oaepmd[30] = { '\0' };
   3898      1.1  christos         char mgf1md[30] = { '\0' };
   3899      1.1  christos 
   3900      1.1  christos         params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
   3901  1.1.1.2  christos             oaepmd, sizeof(oaepmd));
   3902      1.1  christos         params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
   3903  1.1.1.2  christos             mgf1md, sizeof(mgf1md));
   3904      1.1  christos         params[2] = OSSL_PARAM_construct_end();
   3905      1.1  christos 
   3906      1.1  christos         if (!TEST_true(EVP_PKEY_CTX_get_params(key_ctx, params)))
   3907      1.1  christos             goto err;
   3908      1.1  christos 
   3909      1.1  christos         if (!TEST_str_eq(oaepmd, OSSL_DIGEST_NAME_SHA2_256)
   3910      1.1  christos             || !TEST_str_eq(mgf1md, OSSL_DIGEST_NAME_SHA1))
   3911      1.1  christos             goto err;
   3912      1.1  christos     }
   3913      1.1  christos 
   3914      1.1  christos     ret = 1;
   3915      1.1  christos 
   3916  1.1.1.2  christos err:
   3917      1.1  christos     EVP_PKEY_free(key);
   3918      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
   3919      1.1  christos 
   3920      1.1  christos     return ret;
   3921      1.1  christos }
   3922      1.1  christos 
   3923      1.1  christos /* https://github.com/openssl/openssl/issues/21288 */
   3924      1.1  christos static int test_RSA_OAEP_set_null_label(void)
   3925      1.1  christos {
   3926      1.1  christos     int ret = 0;
   3927      1.1  christos     EVP_PKEY *key = NULL;
   3928      1.1  christos     EVP_PKEY_CTX *key_ctx = NULL;
   3929      1.1  christos 
   3930      1.1  christos     if (!TEST_ptr(key = load_example_rsa_key())
   3931      1.1  christos         || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(testctx, key, NULL))
   3932      1.1  christos         || !TEST_true(EVP_PKEY_encrypt_init(key_ctx)))
   3933      1.1  christos         goto err;
   3934      1.1  christos 
   3935      1.1  christos     if (!TEST_true(EVP_PKEY_CTX_set_rsa_padding(key_ctx, RSA_PKCS1_OAEP_PADDING)))
   3936      1.1  christos         goto err;
   3937      1.1  christos 
   3938      1.1  christos     if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, OPENSSL_strdup("foo"), 0)))
   3939      1.1  christos         goto err;
   3940      1.1  christos 
   3941      1.1  christos     if (!TEST_true(EVP_PKEY_CTX_set0_rsa_oaep_label(key_ctx, NULL, 0)))
   3942      1.1  christos         goto err;
   3943      1.1  christos 
   3944      1.1  christos     ret = 1;
   3945      1.1  christos 
   3946  1.1.1.2  christos err:
   3947      1.1  christos     EVP_PKEY_free(key);
   3948      1.1  christos     EVP_PKEY_CTX_free(key_ctx);
   3949      1.1  christos 
   3950      1.1  christos     return ret;
   3951      1.1  christos }
   3952      1.1  christos 
   3953  1.1.1.2  christos static int test_RSA_encrypt(void)
   3954  1.1.1.2  christos {
   3955  1.1.1.2  christos     int ret = 0;
   3956  1.1.1.2  christos     EVP_PKEY *pkey = NULL;
   3957  1.1.1.2  christos     EVP_PKEY_CTX *pctx = NULL;
   3958  1.1.1.2  christos     unsigned char *cbuf = NULL, *pbuf = NULL;
   3959  1.1.1.2  christos     size_t clen = 0, plen = 0;
   3960  1.1.1.2  christos 
   3961  1.1.1.2  christos     if (!TEST_ptr(pkey = load_example_rsa_key())
   3962  1.1.1.2  christos         || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx,
   3963  1.1.1.2  christos                          pkey, testpropq))
   3964  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_encrypt_init(pctx), 0)
   3965  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_encrypt(pctx, cbuf, &clen, kMsg, sizeof(kMsg)), 0)
   3966  1.1.1.2  christos         || !TEST_ptr(cbuf = OPENSSL_malloc(clen))
   3967  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_encrypt(pctx, cbuf, &clen, kMsg, sizeof(kMsg)), 0))
   3968  1.1.1.2  christos         goto done;
   3969  1.1.1.2  christos 
   3970  1.1.1.2  christos     /* Require failure when the output buffer is too small */
   3971  1.1.1.2  christos     plen = clen - 1;
   3972  1.1.1.2  christos     if (!TEST_int_le(EVP_PKEY_encrypt(pctx, cbuf, &plen, kMsg, sizeof(kMsg)), 0))
   3973  1.1.1.2  christos         goto done;
   3974  1.1.1.2  christos     /* flush error stack */
   3975  1.1.1.2  christos     TEST_openssl_errors();
   3976  1.1.1.2  christos 
   3977  1.1.1.2  christos     /* Check decryption of encrypted result */
   3978  1.1.1.2  christos     if (!TEST_int_gt(EVP_PKEY_decrypt_init(pctx), 0)
   3979  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_decrypt(pctx, pbuf, &plen, cbuf, clen), 0)
   3980  1.1.1.2  christos         || !TEST_ptr(pbuf = OPENSSL_malloc(plen))
   3981  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_decrypt(pctx, pbuf, &plen, cbuf, clen), 0)
   3982  1.1.1.2  christos         || !TEST_mem_eq(pbuf, plen, kMsg, sizeof(kMsg))
   3983  1.1.1.2  christos         || !TEST_int_gt(EVP_PKEY_encrypt_init(pctx), 0))
   3984  1.1.1.2  christos         goto done;
   3985  1.1.1.2  christos 
   3986  1.1.1.2  christos     ret = 1;
   3987  1.1.1.2  christos done:
   3988  1.1.1.2  christos     EVP_PKEY_CTX_free(pctx);
   3989  1.1.1.2  christos     EVP_PKEY_free(pkey);
   3990  1.1.1.2  christos     OPENSSL_free(cbuf);
   3991  1.1.1.2  christos     OPENSSL_free(pbuf);
   3992  1.1.1.2  christos     return ret;
   3993  1.1.1.2  christos }
   3994  1.1.1.2  christos 
   3995      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   3996      1.1  christos static int test_RSA_legacy(void)
   3997      1.1  christos {
   3998      1.1  christos     int ret = 0;
   3999      1.1  christos     BIGNUM *p = NULL;
   4000      1.1  christos     BIGNUM *q = NULL;
   4001      1.1  christos     BIGNUM *n = NULL;
   4002      1.1  christos     BIGNUM *e = NULL;
   4003      1.1  christos     BIGNUM *d = NULL;
   4004      1.1  christos     const EVP_MD *md = EVP_sha256();
   4005      1.1  christos     EVP_MD_CTX *ctx = NULL;
   4006      1.1  christos     EVP_PKEY *pkey = NULL;
   4007      1.1  christos     RSA *rsa = NULL;
   4008      1.1  christos 
   4009      1.1  christos     if (nullprov != NULL)
   4010      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   4011      1.1  christos 
   4012      1.1  christos     if (!TEST_ptr(p = BN_dup(BN_value_one()))
   4013      1.1  christos         || !TEST_ptr(q = BN_dup(BN_value_one()))
   4014      1.1  christos         || !TEST_ptr(n = BN_dup(BN_value_one()))
   4015      1.1  christos         || !TEST_ptr(e = BN_dup(BN_value_one()))
   4016      1.1  christos         || !TEST_ptr(d = BN_dup(BN_value_one())))
   4017      1.1  christos         goto err;
   4018      1.1  christos 
   4019      1.1  christos     if (!TEST_ptr(rsa = RSA_new())
   4020      1.1  christos         || !TEST_ptr(pkey = EVP_PKEY_new())
   4021      1.1  christos         || !TEST_ptr(ctx = EVP_MD_CTX_new()))
   4022      1.1  christos         goto err;
   4023      1.1  christos 
   4024      1.1  christos     if (!TEST_true(RSA_set0_factors(rsa, p, q)))
   4025      1.1  christos         goto err;
   4026      1.1  christos     p = NULL;
   4027      1.1  christos     q = NULL;
   4028      1.1  christos 
   4029      1.1  christos     if (!TEST_true(RSA_set0_key(rsa, n, e, d)))
   4030      1.1  christos         goto err;
   4031      1.1  christos     n = NULL;
   4032      1.1  christos     e = NULL;
   4033      1.1  christos     d = NULL;
   4034      1.1  christos 
   4035      1.1  christos     if (!TEST_true(EVP_PKEY_assign_RSA(pkey, rsa)))
   4036      1.1  christos         goto err;
   4037      1.1  christos 
   4038      1.1  christos     rsa = NULL;
   4039      1.1  christos 
   4040      1.1  christos     if (!TEST_true(EVP_DigestSignInit(ctx, NULL, md, NULL, pkey)))
   4041      1.1  christos         goto err;
   4042      1.1  christos 
   4043      1.1  christos     ret = 1;
   4044      1.1  christos 
   4045      1.1  christos err:
   4046      1.1  christos     RSA_free(rsa);
   4047      1.1  christos     EVP_MD_CTX_free(ctx);
   4048      1.1  christos     EVP_PKEY_free(pkey);
   4049      1.1  christos     BN_free(p);
   4050      1.1  christos     BN_free(q);
   4051      1.1  christos     BN_free(n);
   4052      1.1  christos     BN_free(e);
   4053      1.1  christos     BN_free(d);
   4054      1.1  christos 
   4055      1.1  christos     return ret;
   4056      1.1  christos }
   4057      1.1  christos #endif
   4058      1.1  christos 
   4059      1.1  christos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
   4060      1.1  christos static int test_decrypt_null_chunks(void)
   4061      1.1  christos {
   4062  1.1.1.2  christos     EVP_CIPHER_CTX *ctx = NULL;
   4063      1.1  christos     EVP_CIPHER *cipher = NULL;
   4064      1.1  christos     const unsigned char key[32] = {
   4065      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   4066      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   4067      1.1  christos         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
   4068      1.1  christos     };
   4069      1.1  christos     unsigned char iv[12] = {
   4070      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
   4071      1.1  christos     };
   4072      1.1  christos     unsigned char msg[] = "It was the best of times, it was the worst of times";
   4073      1.1  christos     unsigned char ciphertext[80];
   4074      1.1  christos     unsigned char plaintext[80];
   4075      1.1  christos     /* We initialise tmp to a non zero value on purpose */
   4076      1.1  christos     int ctlen, ptlen, tmp = 99;
   4077      1.1  christos     int ret = 0;
   4078      1.1  christos     const int enc_offset = 10, dec_offset = 20;
   4079      1.1  christos 
   4080      1.1  christos     if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
   4081  1.1.1.2  christos         || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   4082  1.1.1.2  christos         || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
   4083  1.1.1.2  christos             key, iv))
   4084  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
   4085  1.1.1.2  christos             enc_offset))
   4086  1.1.1.2  christos         /* Deliberate add a zero length update */
   4087  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
   4088  1.1.1.2  christos             0))
   4089  1.1.1.2  christos         || !TEST_int_eq(tmp, 0)
   4090  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
   4091  1.1.1.2  christos             msg + enc_offset,
   4092  1.1.1.2  christos             sizeof(msg) - enc_offset))
   4093  1.1.1.2  christos         || !TEST_int_eq(ctlen += tmp, sizeof(msg))
   4094  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
   4095  1.1.1.2  christos         || !TEST_int_eq(tmp, 0))
   4096      1.1  christos         goto err;
   4097      1.1  christos 
   4098      1.1  christos     /* Deliberately initialise tmp to a non zero value */
   4099      1.1  christos     tmp = 99;
   4100      1.1  christos     if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
   4101  1.1.1.2  christos         || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
   4102  1.1.1.2  christos             dec_offset))
   4103  1.1.1.2  christos         /*
   4104  1.1.1.2  christos          * Deliberately add a zero length update. We also deliberately do
   4105  1.1.1.2  christos          * this at a different offset than for encryption.
   4106  1.1.1.2  christos          */
   4107  1.1.1.2  christos         || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
   4108  1.1.1.2  christos             0))
   4109  1.1.1.2  christos         || !TEST_int_eq(tmp, 0)
   4110  1.1.1.2  christos         || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
   4111  1.1.1.2  christos             ciphertext + dec_offset,
   4112  1.1.1.2  christos             ctlen - dec_offset))
   4113  1.1.1.2  christos         || !TEST_int_eq(ptlen += tmp, sizeof(msg))
   4114  1.1.1.2  christos         || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
   4115  1.1.1.2  christos         || !TEST_int_eq(tmp, 0)
   4116  1.1.1.2  christos         || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
   4117      1.1  christos         goto err;
   4118      1.1  christos 
   4119      1.1  christos     ret = 1;
   4120  1.1.1.2  christos err:
   4121      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   4122      1.1  christos     EVP_CIPHER_free(cipher);
   4123      1.1  christos     return ret;
   4124      1.1  christos }
   4125      1.1  christos #endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
   4126      1.1  christos 
   4127      1.1  christos #ifndef OPENSSL_NO_DH
   4128      1.1  christos /*
   4129      1.1  christos  * Test combinations of private, public, missing and private + public key
   4130      1.1  christos  * params to ensure they are all accepted
   4131      1.1  christos  */
   4132      1.1  christos static int test_DH_priv_pub(void)
   4133      1.1  christos {
   4134      1.1  christos     return test_EVP_PKEY_ffc_priv_pub("DH");
   4135      1.1  christos }
   4136      1.1  christos 
   4137  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   4138      1.1  christos static int test_EVP_PKEY_set1_DH(void)
   4139      1.1  christos {
   4140      1.1  christos     DH *x942dh = NULL, *noqdh = NULL;
   4141      1.1  christos     EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
   4142      1.1  christos     int ret = 0;
   4143      1.1  christos     BIGNUM *p, *g = NULL;
   4144      1.1  christos     BIGNUM *pubkey = NULL;
   4145      1.1  christos     unsigned char pub[2048 / 8];
   4146      1.1  christos     size_t len = 0;
   4147      1.1  christos 
   4148      1.1  christos     if (!TEST_ptr(p = BN_new())
   4149  1.1.1.2  christos         || !TEST_ptr(g = BN_new())
   4150  1.1.1.2  christos         || !TEST_ptr(pubkey = BN_new())
   4151  1.1.1.2  christos         || !TEST_true(BN_set_word(p, 9999))
   4152  1.1.1.2  christos         || !TEST_true(BN_set_word(g, 2))
   4153  1.1.1.2  christos         || !TEST_true(BN_set_word(pubkey, 4321))
   4154  1.1.1.2  christos         || !TEST_ptr(noqdh = DH_new())
   4155  1.1.1.2  christos         || !TEST_true(DH_set0_pqg(noqdh, p, NULL, g))
   4156  1.1.1.2  christos         || !TEST_true(DH_set0_key(noqdh, pubkey, NULL))
   4157  1.1.1.2  christos         || !TEST_ptr(pubkey = BN_new())
   4158  1.1.1.2  christos         || !TEST_true(BN_set_word(pubkey, 4321)))
   4159      1.1  christos         goto err;
   4160      1.1  christos     p = g = NULL;
   4161      1.1  christos 
   4162      1.1  christos     x942dh = DH_get_2048_256();
   4163      1.1  christos     pkey1 = EVP_PKEY_new();
   4164      1.1  christos     pkey2 = EVP_PKEY_new();
   4165      1.1  christos     if (!TEST_ptr(x942dh)
   4166  1.1.1.2  christos         || !TEST_ptr(noqdh)
   4167  1.1.1.2  christos         || !TEST_ptr(pkey1)
   4168  1.1.1.2  christos         || !TEST_ptr(pkey2)
   4169  1.1.1.2  christos         || !TEST_true(DH_set0_key(x942dh, pubkey, NULL)))
   4170      1.1  christos         goto err;
   4171      1.1  christos     pubkey = NULL;
   4172      1.1  christos 
   4173      1.1  christos     if (!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
   4174  1.1.1.2  christos         || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX))
   4175      1.1  christos         goto err;
   4176      1.1  christos 
   4177      1.1  christos     if (!TEST_true(EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PUB_KEY,
   4178  1.1.1.2  christos             &pubkey))
   4179  1.1.1.2  christos         || !TEST_ptr(pubkey))
   4180      1.1  christos         goto err;
   4181      1.1  christos 
   4182      1.1  christos     if (!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
   4183  1.1.1.2  christos         || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH))
   4184      1.1  christos         goto err;
   4185      1.1  christos 
   4186      1.1  christos     if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey2,
   4187  1.1.1.2  christos             OSSL_PKEY_PARAM_PUB_KEY,
   4188  1.1.1.2  christos             pub, sizeof(pub), &len))
   4189  1.1.1.2  christos         || !TEST_size_t_ne(len, 0))
   4190      1.1  christos         goto err;
   4191      1.1  christos 
   4192      1.1  christos     ret = 1;
   4193  1.1.1.2  christos err:
   4194      1.1  christos     BN_free(p);
   4195      1.1  christos     BN_free(g);
   4196      1.1  christos     BN_free(pubkey);
   4197      1.1  christos     EVP_PKEY_free(pkey1);
   4198      1.1  christos     EVP_PKEY_free(pkey2);
   4199      1.1  christos     DH_free(x942dh);
   4200      1.1  christos     DH_free(noqdh);
   4201      1.1  christos 
   4202      1.1  christos     return ret;
   4203      1.1  christos }
   4204  1.1.1.2  christos #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
   4205      1.1  christos #endif /* !OPENSSL_NO_DH */
   4206      1.1  christos 
   4207      1.1  christos /*
   4208      1.1  christos  * We test what happens with an empty template.  For the sake of this test,
   4209      1.1  christos  * the template must be ignored, and we know that's the case for RSA keys
   4210      1.1  christos  * (this might arguably be a misfeature, but that's what we currently do,
   4211      1.1  christos  * even in provider code, since that's how the legacy RSA implementation
   4212      1.1  christos  * does things)
   4213      1.1  christos  */
   4214      1.1  christos static int test_keygen_with_empty_template(int n)
   4215      1.1  christos {
   4216      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   4217      1.1  christos     EVP_PKEY *pkey = NULL;
   4218      1.1  christos     EVP_PKEY *tkey = NULL;
   4219      1.1  christos     int ret = 0;
   4220      1.1  christos 
   4221      1.1  christos     if (nullprov != NULL)
   4222      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   4223      1.1  christos 
   4224      1.1  christos     switch (n) {
   4225      1.1  christos     case 0:
   4226      1.1  christos         /* We do test with no template at all as well */
   4227      1.1  christos         if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
   4228      1.1  christos             goto err;
   4229      1.1  christos         break;
   4230      1.1  christos     case 1:
   4231      1.1  christos         /* Here we create an empty RSA key that serves as our template */
   4232      1.1  christos         if (!TEST_ptr(tkey = EVP_PKEY_new())
   4233      1.1  christos             || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
   4234      1.1  christos             || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
   4235      1.1  christos             goto err;
   4236      1.1  christos         break;
   4237      1.1  christos     }
   4238      1.1  christos 
   4239      1.1  christos     if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
   4240      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
   4241      1.1  christos         goto err;
   4242      1.1  christos 
   4243      1.1  christos     ret = 1;
   4244  1.1.1.2  christos err:
   4245      1.1  christos     EVP_PKEY_CTX_free(ctx);
   4246      1.1  christos     EVP_PKEY_free(pkey);
   4247      1.1  christos     EVP_PKEY_free(tkey);
   4248      1.1  christos     return ret;
   4249      1.1  christos }
   4250      1.1  christos 
   4251      1.1  christos /*
   4252      1.1  christos  * Test that we fail if we attempt to use an algorithm that is not available
   4253      1.1  christos  * in the current library context (unless we are using an algorithm that
   4254      1.1  christos  * should be made available via legacy codepaths).
   4255      1.1  christos  *
   4256      1.1  christos  * 0:   RSA
   4257      1.1  christos  * 1:   SM2
   4258      1.1  christos  */
   4259      1.1  christos static int test_pkey_ctx_fail_without_provider(int tst)
   4260      1.1  christos {
   4261      1.1  christos     OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
   4262      1.1  christos     OSSL_PROVIDER *tmpnullprov = NULL;
   4263      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   4264      1.1  christos     const char *keytype = NULL;
   4265      1.1  christos     int expect_null = 0;
   4266      1.1  christos     int ret = 0;
   4267      1.1  christos 
   4268      1.1  christos     if (!TEST_ptr(tmpctx))
   4269      1.1  christos         goto err;
   4270      1.1  christos 
   4271      1.1  christos     tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
   4272      1.1  christos     if (!TEST_ptr(tmpnullprov))
   4273      1.1  christos         goto err;
   4274      1.1  christos 
   4275      1.1  christos     /*
   4276      1.1  christos      * We check for certain algos in the null provider.
   4277      1.1  christos      * If an algo is expected to have a provider keymgmt, constructing an
   4278      1.1  christos      * EVP_PKEY_CTX is expected to fail (return NULL).
   4279      1.1  christos      * Otherwise, if it's expected to have legacy support, constructing an
   4280      1.1  christos      * EVP_PKEY_CTX is expected to succeed (return non-NULL).
   4281      1.1  christos      */
   4282      1.1  christos     switch (tst) {
   4283      1.1  christos     case 0:
   4284      1.1  christos         keytype = "RSA";
   4285      1.1  christos         expect_null = 1;
   4286      1.1  christos         break;
   4287      1.1  christos     case 1:
   4288      1.1  christos         keytype = "SM2";
   4289      1.1  christos         expect_null = 1;
   4290      1.1  christos #ifdef OPENSSL_NO_EC
   4291      1.1  christos         TEST_info("EC disable, skipping SM2 check...");
   4292      1.1  christos         goto end;
   4293      1.1  christos #endif
   4294      1.1  christos #ifdef OPENSSL_NO_SM2
   4295      1.1  christos         TEST_info("SM2 disable, skipping SM2 check...");
   4296      1.1  christos         goto end;
   4297      1.1  christos #endif
   4298      1.1  christos         break;
   4299      1.1  christos     default:
   4300      1.1  christos         TEST_error("No test for case %d", tst);
   4301      1.1  christos         goto err;
   4302      1.1  christos     }
   4303      1.1  christos 
   4304      1.1  christos     pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
   4305      1.1  christos     if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
   4306      1.1  christos         goto err;
   4307      1.1  christos 
   4308      1.1  christos #if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
   4309  1.1.1.2  christos end:
   4310      1.1  christos #endif
   4311      1.1  christos     ret = 1;
   4312      1.1  christos 
   4313  1.1.1.2  christos err:
   4314      1.1  christos     EVP_PKEY_CTX_free(pctx);
   4315      1.1  christos     OSSL_PROVIDER_unload(tmpnullprov);
   4316      1.1  christos     OSSL_LIB_CTX_free(tmpctx);
   4317      1.1  christos     return ret;
   4318      1.1  christos }
   4319      1.1  christos 
   4320      1.1  christos static int test_rand_agglomeration(void)
   4321      1.1  christos {
   4322      1.1  christos     EVP_RAND *rand;
   4323      1.1  christos     EVP_RAND_CTX *ctx;
   4324      1.1  christos     OSSL_PARAM params[3], *p = params;
   4325      1.1  christos     int res;
   4326      1.1  christos     unsigned int step = 7;
   4327      1.1  christos     static unsigned char seed[] = "It does not matter how slowly you go "
   4328      1.1  christos                                   "as long as you do not stop.";
   4329      1.1  christos     unsigned char out[sizeof(seed)];
   4330      1.1  christos 
   4331      1.1  christos     if (!TEST_int_ne(sizeof(seed) % step, 0)
   4332  1.1.1.2  christos         || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
   4333      1.1  christos         return 0;
   4334      1.1  christos     ctx = EVP_RAND_CTX_new(rand, NULL);
   4335      1.1  christos     EVP_RAND_free(rand);
   4336      1.1  christos     if (!TEST_ptr(ctx))
   4337      1.1  christos         return 0;
   4338      1.1  christos 
   4339      1.1  christos     memset(out, 0, sizeof(out));
   4340      1.1  christos     *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
   4341  1.1.1.2  christos         seed, sizeof(seed));
   4342      1.1  christos     *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
   4343      1.1  christos     *p = OSSL_PARAM_construct_end();
   4344      1.1  christos     res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
   4345  1.1.1.2  christos         && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
   4346  1.1.1.2  christos         && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
   4347      1.1  christos     EVP_RAND_CTX_free(ctx);
   4348      1.1  christos     return res;
   4349      1.1  christos }
   4350      1.1  christos 
   4351      1.1  christos /*
   4352      1.1  christos  * Test that we correctly return the original or "running" IV after
   4353      1.1  christos  * an encryption operation.
   4354      1.1  christos  * Run multiple times for some different relevant algorithms/modes.
   4355      1.1  christos  */
   4356      1.1  christos static int test_evp_iv_aes(int idx)
   4357      1.1  christos {
   4358      1.1  christos     int ret = 0;
   4359      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   4360      1.1  christos     unsigned char key[16] = {
   4361      1.1  christos         0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
   4362      1.1  christos         0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57
   4363      1.1  christos     };
   4364      1.1  christos     unsigned char init_iv[EVP_MAX_IV_LENGTH] = {
   4365      1.1  christos         0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
   4366      1.1  christos         0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
   4367      1.1  christos     };
   4368      1.1  christos     static const unsigned char msg[] = {
   4369      1.1  christos         1, 2, 3, 4, 5, 6, 7, 8,
   4370      1.1  christos         9, 10, 11, 12, 13, 14, 15, 16
   4371      1.1  christos     };
   4372      1.1  christos     unsigned char ciphertext[32], oiv[16], iv[16];
   4373      1.1  christos     unsigned char *ref_iv;
   4374      1.1  christos     unsigned char cbc_state[16] = {
   4375      1.1  christos         0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
   4376      1.1  christos         0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e
   4377      1.1  christos     };
   4378      1.1  christos 
   4379      1.1  christos     unsigned char ofb_state[16] = {
   4380      1.1  christos         0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
   4381      1.1  christos         0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd
   4382      1.1  christos     };
   4383      1.1  christos     unsigned char cfb_state[16] = {
   4384      1.1  christos         0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
   4385      1.1  christos         0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed
   4386      1.1  christos     };
   4387      1.1  christos     unsigned char gcm_state[12] = {
   4388      1.1  christos         0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
   4389      1.1  christos         0x98, 0x82, 0x5a, 0x55, 0x91, 0x81
   4390      1.1  christos     };
   4391      1.1  christos     unsigned char ccm_state[7] = { 0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98 };
   4392      1.1  christos #ifndef OPENSSL_NO_OCB
   4393      1.1  christos     unsigned char ocb_state[12] = {
   4394      1.1  christos         0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
   4395      1.1  christos         0x98, 0x82, 0x5a, 0x55, 0x91, 0x81
   4396      1.1  christos     };
   4397      1.1  christos #endif
   4398      1.1  christos     int len = sizeof(ciphertext);
   4399      1.1  christos     size_t ivlen, ref_len;
   4400      1.1  christos     const EVP_CIPHER *type = NULL;
   4401      1.1  christos     int iv_reset = 0;
   4402      1.1  christos 
   4403      1.1  christos     if (nullprov != NULL && idx < 6)
   4404      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   4405      1.1  christos 
   4406      1.1  christos     switch (idx) {
   4407      1.1  christos     case 0:
   4408      1.1  christos         type = EVP_aes_128_cbc();
   4409      1.1  christos         /* FALLTHROUGH */
   4410      1.1  christos     case 6:
   4411  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
   4412      1.1  christos         ref_iv = cbc_state;
   4413      1.1  christos         ref_len = sizeof(cbc_state);
   4414      1.1  christos         iv_reset = 1;
   4415      1.1  christos         break;
   4416      1.1  christos     case 1:
   4417      1.1  christos         type = EVP_aes_128_ofb();
   4418      1.1  christos         /* FALLTHROUGH */
   4419      1.1  christos     case 7:
   4420  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
   4421      1.1  christos         ref_iv = ofb_state;
   4422      1.1  christos         ref_len = sizeof(ofb_state);
   4423      1.1  christos         iv_reset = 1;
   4424      1.1  christos         break;
   4425      1.1  christos     case 2:
   4426      1.1  christos         type = EVP_aes_128_cfb();
   4427      1.1  christos         /* FALLTHROUGH */
   4428      1.1  christos     case 8:
   4429  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
   4430      1.1  christos         ref_iv = cfb_state;
   4431      1.1  christos         ref_len = sizeof(cfb_state);
   4432      1.1  christos         iv_reset = 1;
   4433      1.1  christos         break;
   4434      1.1  christos     case 3:
   4435      1.1  christos         type = EVP_aes_128_gcm();
   4436      1.1  christos         /* FALLTHROUGH */
   4437      1.1  christos     case 9:
   4438  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
   4439      1.1  christos         ref_iv = gcm_state;
   4440      1.1  christos         ref_len = sizeof(gcm_state);
   4441      1.1  christos         break;
   4442      1.1  christos     case 4:
   4443      1.1  christos         type = EVP_aes_128_ccm();
   4444      1.1  christos         /* FALLTHROUGH */
   4445      1.1  christos     case 10:
   4446  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
   4447      1.1  christos         ref_iv = ccm_state;
   4448      1.1  christos         ref_len = sizeof(ccm_state);
   4449      1.1  christos         break;
   4450      1.1  christos #ifdef OPENSSL_NO_OCB
   4451      1.1  christos     case 5:
   4452      1.1  christos     case 11:
   4453      1.1  christos         return 1;
   4454      1.1  christos #else
   4455      1.1  christos     case 5:
   4456      1.1  christos         type = EVP_aes_128_ocb();
   4457      1.1  christos         /* FALLTHROUGH */
   4458      1.1  christos     case 11:
   4459  1.1.1.2  christos         type = (type != NULL) ? type : EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
   4460      1.1  christos         ref_iv = ocb_state;
   4461      1.1  christos         ref_len = sizeof(ocb_state);
   4462      1.1  christos         break;
   4463      1.1  christos #endif
   4464      1.1  christos     default:
   4465      1.1  christos         return 0;
   4466      1.1  christos     }
   4467      1.1  christos 
   4468      1.1  christos     if (!TEST_ptr(type)
   4469  1.1.1.2  christos         || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
   4470  1.1.1.2  christos         || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
   4471  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
   4472  1.1.1.2  christos             (int)sizeof(msg)))
   4473  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
   4474  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
   4475  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
   4476      1.1  christos         goto err;
   4477      1.1  christos     ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
   4478      1.1  christos 
   4479      1.1  christos     if (!TEST_int_gt(ivlen, 0))
   4480      1.1  christos         goto err;
   4481      1.1  christos 
   4482      1.1  christos     if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
   4483  1.1.1.2  christos         || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
   4484      1.1  christos         goto err;
   4485      1.1  christos 
   4486      1.1  christos     /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
   4487      1.1  christos     if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
   4488      1.1  christos         || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
   4489      1.1  christos         goto err;
   4490      1.1  christos     if (iv_reset) {
   4491      1.1  christos         if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
   4492      1.1  christos             goto err;
   4493      1.1  christos     } else {
   4494      1.1  christos         if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
   4495      1.1  christos             goto err;
   4496      1.1  christos     }
   4497      1.1  christos 
   4498      1.1  christos     ret = 1;
   4499      1.1  christos err:
   4500      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   4501      1.1  christos     if (idx >= 6)
   4502      1.1  christos         EVP_CIPHER_free((EVP_CIPHER *)type);
   4503      1.1  christos     return ret;
   4504      1.1  christos }
   4505      1.1  christos 
   4506      1.1  christos #ifndef OPENSSL_NO_DES
   4507      1.1  christos static int test_evp_iv_des(int idx)
   4508      1.1  christos {
   4509      1.1  christos     int ret = 0;
   4510      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   4511      1.1  christos     static const unsigned char key[24] = {
   4512      1.1  christos         0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
   4513      1.1  christos         0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
   4514      1.1  christos         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
   4515      1.1  christos     };
   4516      1.1  christos     static const unsigned char init_iv[8] = {
   4517      1.1  christos         0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
   4518      1.1  christos     };
   4519      1.1  christos     static const unsigned char msg[] = {
   4520      1.1  christos         1, 2, 3, 4, 5, 6, 7, 8,
   4521      1.1  christos         9, 10, 11, 12, 13, 14, 15, 16
   4522      1.1  christos     };
   4523      1.1  christos     unsigned char ciphertext[32], oiv[8], iv[8];
   4524      1.1  christos     unsigned const char *ref_iv;
   4525      1.1  christos     static const unsigned char cbc_state_des[8] = {
   4526      1.1  christos         0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a
   4527      1.1  christos     };
   4528      1.1  christos     static const unsigned char cbc_state_3des[8] = {
   4529      1.1  christos         0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9
   4530      1.1  christos     };
   4531      1.1  christos     static const unsigned char ofb_state_des[8] = {
   4532      1.1  christos         0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c
   4533      1.1  christos     };
   4534      1.1  christos     static const unsigned char ofb_state_3des[8] = {
   4535      1.1  christos         0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e
   4536      1.1  christos     };
   4537      1.1  christos     static const unsigned char cfb_state_des[8] = {
   4538      1.1  christos         0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73
   4539      1.1  christos     };
   4540      1.1  christos     static const unsigned char cfb_state_3des[8] = {
   4541      1.1  christos         0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7
   4542      1.1  christos     };
   4543      1.1  christos     int len = sizeof(ciphertext);
   4544      1.1  christos     size_t ivlen, ref_len;
   4545      1.1  christos     EVP_CIPHER *type = NULL;
   4546      1.1  christos 
   4547      1.1  christos     if (lgcyprov == NULL && idx < 3)
   4548      1.1  christos         return TEST_skip("Test requires legacy provider to be loaded");
   4549      1.1  christos 
   4550      1.1  christos     switch (idx) {
   4551      1.1  christos     case 0:
   4552      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq);
   4553      1.1  christos         ref_iv = cbc_state_des;
   4554      1.1  christos         ref_len = sizeof(cbc_state_des);
   4555      1.1  christos         break;
   4556      1.1  christos     case 1:
   4557      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq);
   4558      1.1  christos         ref_iv = ofb_state_des;
   4559      1.1  christos         ref_len = sizeof(ofb_state_des);
   4560      1.1  christos         break;
   4561      1.1  christos     case 2:
   4562      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq);
   4563      1.1  christos         ref_iv = cfb_state_des;
   4564      1.1  christos         ref_len = sizeof(cfb_state_des);
   4565      1.1  christos         break;
   4566      1.1  christos     case 3:
   4567      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq);
   4568      1.1  christos         ref_iv = cbc_state_3des;
   4569      1.1  christos         ref_len = sizeof(cbc_state_3des);
   4570      1.1  christos         break;
   4571      1.1  christos     case 4:
   4572      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq);
   4573      1.1  christos         ref_iv = ofb_state_3des;
   4574      1.1  christos         ref_len = sizeof(ofb_state_3des);
   4575      1.1  christos         break;
   4576      1.1  christos     case 5:
   4577      1.1  christos         type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq);
   4578      1.1  christos         ref_iv = cfb_state_3des;
   4579      1.1  christos         ref_len = sizeof(cfb_state_3des);
   4580      1.1  christos         break;
   4581      1.1  christos     default:
   4582      1.1  christos         return 0;
   4583      1.1  christos     }
   4584      1.1  christos 
   4585      1.1  christos     if (!TEST_ptr(type)
   4586  1.1.1.2  christos         || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
   4587  1.1.1.2  christos         || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
   4588  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
   4589  1.1.1.2  christos             (int)sizeof(msg)))
   4590  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
   4591  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
   4592  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
   4593      1.1  christos         goto err;
   4594      1.1  christos     ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
   4595      1.1  christos 
   4596      1.1  christos     if (!TEST_int_gt(ivlen, 0))
   4597      1.1  christos         goto err;
   4598      1.1  christos 
   4599      1.1  christos     if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
   4600  1.1.1.2  christos         || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
   4601      1.1  christos         goto err;
   4602      1.1  christos 
   4603      1.1  christos     if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
   4604      1.1  christos         || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
   4605      1.1  christos         goto err;
   4606      1.1  christos     if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
   4607      1.1  christos         goto err;
   4608      1.1  christos 
   4609      1.1  christos     ret = 1;
   4610      1.1  christos err:
   4611      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   4612      1.1  christos     EVP_CIPHER_free(type);
   4613      1.1  christos     return ret;
   4614      1.1  christos }
   4615      1.1  christos #endif
   4616      1.1  christos 
   4617      1.1  christos #ifndef OPENSSL_NO_BF
   4618      1.1  christos static int test_evp_bf_default_keylen(int idx)
   4619      1.1  christos {
   4620      1.1  christos     int ret = 0;
   4621      1.1  christos     static const char *algos[4] = {
   4622      1.1  christos         "bf-ecb", "bf-cbc", "bf-cfb", "bf-ofb"
   4623      1.1  christos     };
   4624      1.1  christos     int ivlen[4] = { 0, 8, 8, 8 };
   4625      1.1  christos     EVP_CIPHER *cipher = NULL;
   4626      1.1  christos 
   4627      1.1  christos     if (lgcyprov == NULL)
   4628      1.1  christos         return TEST_skip("Test requires legacy provider to be loaded");
   4629      1.1  christos 
   4630      1.1  christos     if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, algos[idx], testpropq))
   4631  1.1.1.2  christos         || !TEST_int_eq(EVP_CIPHER_get_key_length(cipher), 16)
   4632  1.1.1.2  christos         || !TEST_int_eq(EVP_CIPHER_get_iv_length(cipher), ivlen[idx]))
   4633      1.1  christos         goto err;
   4634      1.1  christos 
   4635      1.1  christos     ret = 1;
   4636      1.1  christos err:
   4637      1.1  christos     EVP_CIPHER_free(cipher);
   4638      1.1  christos     return ret;
   4639      1.1  christos }
   4640      1.1  christos #endif
   4641      1.1  christos 
   4642      1.1  christos #ifndef OPENSSL_NO_EC
   4643      1.1  christos static int ecpub_nids[] = {
   4644      1.1  christos     NID_brainpoolP256r1, NID_X9_62_prime256v1,
   4645      1.1  christos     NID_secp384r1, NID_secp521r1,
   4646  1.1.1.2  christos #ifndef OPENSSL_NO_EC2M
   4647      1.1  christos     NID_sect233k1, NID_sect233r1, NID_sect283r1,
   4648      1.1  christos     NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
   4649  1.1.1.2  christos #endif
   4650      1.1  christos     NID_brainpoolP384r1, NID_brainpoolP512r1
   4651      1.1  christos };
   4652      1.1  christos 
   4653      1.1  christos static int test_ecpub(int idx)
   4654      1.1  christos {
   4655      1.1  christos     int ret = 0, len, savelen;
   4656      1.1  christos     int nid;
   4657      1.1  christos     unsigned char buf[1024];
   4658      1.1  christos     unsigned char *p;
   4659      1.1  christos     EVP_PKEY *pkey = NULL;
   4660      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   4661  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   4662      1.1  christos     const unsigned char *q;
   4663      1.1  christos     EVP_PKEY *pkey2 = NULL;
   4664      1.1  christos     EC_KEY *ec = NULL;
   4665  1.1.1.2  christos #endif
   4666      1.1  christos 
   4667      1.1  christos     if (nullprov != NULL)
   4668      1.1  christos         return TEST_skip("Test does not support a non-default library context");
   4669      1.1  christos 
   4670      1.1  christos     nid = ecpub_nids[idx];
   4671      1.1  christos 
   4672      1.1  christos     ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
   4673      1.1  christos     if (!TEST_ptr(ctx)
   4674      1.1  christos         || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
   4675      1.1  christos         || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0)
   4676      1.1  christos         || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
   4677      1.1  christos         goto done;
   4678      1.1  christos     len = i2d_PublicKey(pkey, NULL);
   4679      1.1  christos     savelen = len;
   4680      1.1  christos     if (!TEST_int_ge(len, 1)
   4681      1.1  christos         || !TEST_int_lt(len, 1024))
   4682      1.1  christos         goto done;
   4683      1.1  christos     p = buf;
   4684      1.1  christos     len = i2d_PublicKey(pkey, &p);
   4685      1.1  christos     if (!TEST_int_ge(len, 1)
   4686  1.1.1.2  christos         || !TEST_int_eq(len, savelen))
   4687      1.1  christos         goto done;
   4688      1.1  christos 
   4689  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   4690      1.1  christos     /* Now try to decode the just-created DER. */
   4691      1.1  christos     q = buf;
   4692      1.1  christos     if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
   4693  1.1.1.2  christos         || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
   4694  1.1.1.2  christos         || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
   4695      1.1  christos         goto done;
   4696      1.1  christos     /* EC_KEY ownership transferred */
   4697      1.1  christos     ec = NULL;
   4698      1.1  christos     if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
   4699      1.1  christos         goto done;
   4700      1.1  christos     /* The keys should match. */
   4701      1.1  christos     if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
   4702      1.1  christos         goto done;
   4703  1.1.1.2  christos #endif
   4704      1.1  christos 
   4705      1.1  christos     ret = 1;
   4706      1.1  christos 
   4707  1.1.1.2  christos done:
   4708      1.1  christos     EVP_PKEY_CTX_free(ctx);
   4709      1.1  christos     EVP_PKEY_free(pkey);
   4710  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   4711      1.1  christos     EVP_PKEY_free(pkey2);
   4712      1.1  christos     EC_KEY_free(ec);
   4713  1.1.1.2  christos #endif
   4714      1.1  christos     return ret;
   4715      1.1  christos }
   4716      1.1  christos #endif
   4717      1.1  christos 
   4718      1.1  christos static int test_EVP_rsa_pss_with_keygen_bits(void)
   4719      1.1  christos {
   4720      1.1  christos     int ret = 0;
   4721      1.1  christos     EVP_PKEY_CTX *ctx = NULL;
   4722      1.1  christos     EVP_PKEY *pkey = NULL;
   4723      1.1  christos     EVP_MD *md;
   4724      1.1  christos 
   4725      1.1  christos     md = EVP_MD_fetch(testctx, "sha256", testpropq);
   4726      1.1  christos     ret = TEST_ptr(md)
   4727      1.1  christos         && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq)))
   4728      1.1  christos         && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
   4729      1.1  christos         && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
   4730      1.1  christos         && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0)
   4731      1.1  christos         && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
   4732      1.1  christos 
   4733      1.1  christos     EVP_MD_free(md);
   4734      1.1  christos     EVP_PKEY_free(pkey);
   4735      1.1  christos     EVP_PKEY_CTX_free(ctx);
   4736      1.1  christos     return ret;
   4737      1.1  christos }
   4738      1.1  christos 
   4739      1.1  christos static int test_EVP_rsa_pss_set_saltlen(void)
   4740      1.1  christos {
   4741      1.1  christos     int ret = 0;
   4742      1.1  christos     EVP_PKEY *pkey = NULL;
   4743      1.1  christos     EVP_PKEY_CTX *pkey_ctx = NULL;
   4744      1.1  christos     EVP_MD *sha256 = NULL;
   4745      1.1  christos     EVP_MD_CTX *sha256_ctx = NULL;
   4746      1.1  christos     int saltlen = 9999; /* buggy EVP_PKEY_CTX_get_rsa_pss_saltlen() didn't update this */
   4747      1.1  christos     const int test_value = 32;
   4748      1.1  christos 
   4749      1.1  christos     ret = TEST_ptr(pkey = load_example_rsa_key())
   4750      1.1  christos         && TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", NULL))
   4751      1.1  christos         && TEST_ptr(sha256_ctx = EVP_MD_CTX_new())
   4752      1.1  christos         && TEST_true(EVP_DigestSignInit(sha256_ctx, &pkey_ctx, sha256, NULL, pkey))
   4753      1.1  christos         && TEST_true(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING))
   4754      1.1  christos         && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, test_value), 0)
   4755      1.1  christos         && TEST_int_gt(EVP_PKEY_CTX_get_rsa_pss_saltlen(pkey_ctx, &saltlen), 0)
   4756      1.1  christos         && TEST_int_eq(saltlen, test_value);
   4757      1.1  christos 
   4758      1.1  christos     EVP_MD_CTX_free(sha256_ctx);
   4759      1.1  christos     EVP_PKEY_free(pkey);
   4760      1.1  christos     EVP_MD_free(sha256);
   4761      1.1  christos 
   4762      1.1  christos     return ret;
   4763      1.1  christos }
   4764      1.1  christos 
   4765      1.1  christos static int test_EVP_rsa_invalid_key(void)
   4766      1.1  christos {
   4767      1.1  christos     int ret = 0;
   4768      1.1  christos     EVP_PKEY *pkey = NULL;
   4769      1.1  christos 
   4770      1.1  christos     pkey = load_example_key("RSA", kInvalidRSAKeyDER, sizeof(kInvalidRSAKeyDER));
   4771      1.1  christos     /* we expect to fail to load bogus key */
   4772      1.1  christos     ret = !TEST_ptr(pkey);
   4773      1.1  christos     EVP_PKEY_free(pkey);
   4774      1.1  christos 
   4775      1.1  christos     return ret;
   4776      1.1  christos }
   4777      1.1  christos 
   4778      1.1  christos static int success = 1;
   4779      1.1  christos static void md_names(const char *name, void *vctx)
   4780      1.1  christos {
   4781      1.1  christos     OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
   4782      1.1  christos     /* Force a namemap update */
   4783      1.1  christos     EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
   4784      1.1  christos 
   4785      1.1  christos     if (!TEST_ptr(aes128))
   4786      1.1  christos         success = 0;
   4787      1.1  christos 
   4788      1.1  christos     EVP_CIPHER_free(aes128);
   4789      1.1  christos }
   4790      1.1  christos 
   4791      1.1  christos /*
   4792      1.1  christos  * Test that changing the namemap in a user callback works in a names_do_all
   4793      1.1  christos  * function.
   4794      1.1  christos  */
   4795      1.1  christos static int test_names_do_all(void)
   4796      1.1  christos {
   4797      1.1  christos     /* We use a custom libctx so that we know the state of the namemap */
   4798      1.1  christos     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
   4799      1.1  christos     EVP_MD *sha256 = NULL;
   4800      1.1  christos     int testresult = 0;
   4801      1.1  christos 
   4802      1.1  christos     if (!TEST_ptr(ctx))
   4803      1.1  christos         goto err;
   4804      1.1  christos 
   4805      1.1  christos     sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
   4806      1.1  christos     if (!TEST_ptr(sha256))
   4807      1.1  christos         goto err;
   4808      1.1  christos 
   4809      1.1  christos     /*
   4810      1.1  christos      * We loop through all the names for a given digest. This should still work
   4811      1.1  christos      * even if the namemap changes part way through.
   4812      1.1  christos      */
   4813      1.1  christos     if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
   4814      1.1  christos         goto err;
   4815      1.1  christos 
   4816      1.1  christos     if (!TEST_true(success))
   4817      1.1  christos         goto err;
   4818      1.1  christos 
   4819      1.1  christos     testresult = 1;
   4820  1.1.1.2  christos err:
   4821      1.1  christos     EVP_MD_free(sha256);
   4822      1.1  christos     OSSL_LIB_CTX_free(ctx);
   4823      1.1  christos     return testresult;
   4824      1.1  christos }
   4825      1.1  christos 
   4826      1.1  christos typedef struct {
   4827      1.1  christos     const char *cipher;
   4828      1.1  christos     const unsigned char *key;
   4829      1.1  christos     const unsigned char *iv;
   4830      1.1  christos     const unsigned char *input;
   4831      1.1  christos     const unsigned char *expected;
   4832      1.1  christos     const unsigned char *tag;
   4833      1.1  christos     size_t ivlen; /* 0 if we do not need to set a specific IV len */
   4834      1.1  christos     size_t inlen;
   4835      1.1  christos     size_t expectedlen;
   4836      1.1  christos     size_t taglen;
   4837      1.1  christos     int keyfirst;
   4838      1.1  christos     int initenc;
   4839      1.1  christos     int finalenc;
   4840      1.1  christos } EVP_INIT_TEST_st;
   4841      1.1  christos 
   4842      1.1  christos static const EVP_INIT_TEST_st evp_init_tests[] = {
   4843  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
   4844      1.1  christos         cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
   4845  1.1.1.2  christos         0, 1, 0, 1 },
   4846  1.1.1.2  christos     { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
   4847      1.1  christos         gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
   4848      1.1  christos         sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
   4849  1.1.1.2  christos         sizeof(gcmDefaultTag), 1, 0, 1 },
   4850  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
   4851      1.1  christos         cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
   4852  1.1.1.2  christos         0, 0, 0, 1 },
   4853  1.1.1.2  christos     { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
   4854      1.1  christos         gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
   4855      1.1  christos         sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
   4856  1.1.1.2  christos         sizeof(gcmDefaultTag), 0, 0, 1 },
   4857  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
   4858      1.1  christos         cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
   4859  1.1.1.2  christos         0, 1, 1, 0 },
   4860  1.1.1.2  christos     { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
   4861      1.1  christos         gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
   4862      1.1  christos         sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
   4863  1.1.1.2  christos         sizeof(gcmDefaultTag), 1, 1, 0 },
   4864  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
   4865      1.1  christos         cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
   4866  1.1.1.2  christos         0, 0, 1, 0 },
   4867  1.1.1.2  christos     { "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
   4868      1.1  christos         gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
   4869      1.1  christos         sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
   4870  1.1.1.2  christos         sizeof(gcmDefaultTag), 0, 1, 0 }
   4871      1.1  christos };
   4872      1.1  christos 
   4873      1.1  christos /* use same key, iv and plaintext for cfb and ofb */
   4874      1.1  christos static const EVP_INIT_TEST_st evp_reinit_tests[] = {
   4875  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
   4876      1.1  christos         cfbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
   4877  1.1.1.2  christos         sizeof(cfbCiphertext_partial), 0, 0, 1, 0 },
   4878  1.1.1.2  christos     { "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext_partial,
   4879      1.1  christos         cfbPlaintext_partial, NULL, 0, sizeof(cfbCiphertext_partial),
   4880  1.1.1.2  christos         sizeof(cfbPlaintext_partial), 0, 0, 0, 0 },
   4881  1.1.1.2  christos     { "aes-128-ofb", kCFBDefaultKey, iCFBIV, cfbPlaintext_partial,
   4882      1.1  christos         ofbCiphertext_partial, NULL, 0, sizeof(cfbPlaintext_partial),
   4883  1.1.1.2  christos         sizeof(ofbCiphertext_partial), 0, 0, 1, 0 },
   4884  1.1.1.2  christos     { "aes-128-ofb", kCFBDefaultKey, iCFBIV, ofbCiphertext_partial,
   4885      1.1  christos         cfbPlaintext_partial, NULL, 0, sizeof(ofbCiphertext_partial),
   4886  1.1.1.2  christos         sizeof(cfbPlaintext_partial), 0, 0, 0, 0 },
   4887      1.1  christos };
   4888      1.1  christos 
   4889      1.1  christos static int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t)
   4890      1.1  christos {
   4891      1.1  christos     int res = 0;
   4892      1.1  christos 
   4893      1.1  christos     if (t->ivlen != 0) {
   4894      1.1  christos         if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL), 0))
   4895      1.1  christos             goto err;
   4896      1.1  christos     }
   4897      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1)))
   4898      1.1  christos         goto err;
   4899      1.1  christos     res = 1;
   4900  1.1.1.2  christos err:
   4901      1.1  christos     return res;
   4902      1.1  christos }
   4903      1.1  christos 
   4904      1.1  christos /*
   4905      1.1  christos  * Test step-wise cipher initialization via EVP_CipherInit_ex where the
   4906      1.1  christos  * arguments are given one at a time and a final adjustment to the enc
   4907      1.1  christos  * parameter sets the correct operation.
   4908      1.1  christos  */
   4909      1.1  christos static int test_evp_init_seq(int idx)
   4910      1.1  christos {
   4911      1.1  christos     int outlen1, outlen2;
   4912      1.1  christos     int testresult = 0;
   4913      1.1  christos     unsigned char outbuf[1024];
   4914      1.1  christos     unsigned char tag[16];
   4915      1.1  christos     const EVP_INIT_TEST_st *t = &evp_init_tests[idx];
   4916      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   4917      1.1  christos     EVP_CIPHER *type = NULL;
   4918      1.1  christos     size_t taglen = sizeof(tag);
   4919      1.1  christos     char *errmsg = NULL;
   4920      1.1  christos 
   4921      1.1  christos     ctx = EVP_CIPHER_CTX_new();
   4922      1.1  christos     if (ctx == NULL) {
   4923      1.1  christos         errmsg = "CTX_ALLOC";
   4924      1.1  christos         goto err;
   4925      1.1  christos     }
   4926      1.1  christos     if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))) {
   4927      1.1  christos         errmsg = "CIPHER_FETCH";
   4928      1.1  christos         goto err;
   4929      1.1  christos     }
   4930      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) {
   4931      1.1  christos         errmsg = "EMPTY_ENC_INIT";
   4932      1.1  christos         goto err;
   4933      1.1  christos     }
   4934      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
   4935      1.1  christos         errmsg = "PADDING";
   4936      1.1  christos         goto err;
   4937      1.1  christos     }
   4938      1.1  christos     if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
   4939      1.1  christos         errmsg = "KEY_INIT (before iv)";
   4940      1.1  christos         goto err;
   4941      1.1  christos     }
   4942      1.1  christos     if (!evp_init_seq_set_iv(ctx, t)) {
   4943      1.1  christos         errmsg = "IV_INIT";
   4944      1.1  christos         goto err;
   4945      1.1  christos     }
   4946  1.1.1.2  christos     if (t->keyfirst == 0 && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
   4947      1.1  christos         errmsg = "KEY_INIT (after iv)";
   4948      1.1  christos         goto err;
   4949      1.1  christos     }
   4950      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) {
   4951      1.1  christos         errmsg = "FINAL_ENC_INIT";
   4952      1.1  christos         goto err;
   4953      1.1  christos     }
   4954      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
   4955      1.1  christos         errmsg = "CIPHER_UPDATE";
   4956      1.1  christos         goto err;
   4957      1.1  christos     }
   4958      1.1  christos     if (t->finalenc == 0 && t->tag != NULL) {
   4959      1.1  christos         /* Set expected tag */
   4960      1.1  christos         if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
   4961  1.1.1.2  christos                              t->taglen, (void *)t->tag),
   4962  1.1.1.2  christos                 0)) {
   4963      1.1  christos             errmsg = "SET_TAG";
   4964      1.1  christos             goto err;
   4965      1.1  christos         }
   4966      1.1  christos     }
   4967      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   4968      1.1  christos         errmsg = "CIPHER_FINAL";
   4969      1.1  christos         goto err;
   4970      1.1  christos     }
   4971      1.1  christos     if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
   4972      1.1  christos         errmsg = "WRONG_RESULT";
   4973      1.1  christos         goto err;
   4974      1.1  christos     }
   4975      1.1  christos     if (t->finalenc != 0 && t->tag != NULL) {
   4976      1.1  christos         if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
   4977      1.1  christos             errmsg = "GET_TAG";
   4978      1.1  christos             goto err;
   4979      1.1  christos         }
   4980      1.1  christos         if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) {
   4981      1.1  christos             errmsg = "TAG_ERROR";
   4982      1.1  christos             goto err;
   4983      1.1  christos         }
   4984      1.1  christos     }
   4985      1.1  christos     testresult = 1;
   4986  1.1.1.2  christos err:
   4987      1.1  christos     if (errmsg != NULL)
   4988      1.1  christos         TEST_info("evp_init_test %d: %s", idx, errmsg);
   4989      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   4990      1.1  christos     EVP_CIPHER_free(type);
   4991      1.1  christos     return testresult;
   4992      1.1  christos }
   4993      1.1  christos 
   4994      1.1  christos /*
   4995      1.1  christos  * Test re-initialization of cipher context without changing key or iv.
   4996      1.1  christos  * The result of both iteration should be the same.
   4997      1.1  christos  */
   4998      1.1  christos static int test_evp_reinit_seq(int idx)
   4999      1.1  christos {
   5000      1.1  christos     int outlen1, outlen2, outlen_final;
   5001      1.1  christos     int testresult = 0;
   5002      1.1  christos     unsigned char outbuf1[1024];
   5003      1.1  christos     unsigned char outbuf2[1024];
   5004      1.1  christos     const EVP_INIT_TEST_st *t = &evp_reinit_tests[idx];
   5005      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5006      1.1  christos     EVP_CIPHER *type = NULL;
   5007      1.1  christos 
   5008      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   5009  1.1.1.2  christos         || !TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))
   5010  1.1.1.2  christos         /* setup cipher context */
   5011  1.1.1.2  christos         || !TEST_true(EVP_CipherInit_ex2(ctx, type, t->key, t->iv, t->initenc, NULL))
   5012  1.1.1.2  christos         /* first iteration */
   5013  1.1.1.2  christos         || !TEST_true(EVP_CipherUpdate(ctx, outbuf1, &outlen1, t->input, t->inlen))
   5014  1.1.1.2  christos         || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf1, &outlen_final))
   5015  1.1.1.2  christos         /* check test results iteration 1 */
   5016  1.1.1.2  christos         || !TEST_mem_eq(t->expected, t->expectedlen, outbuf1, outlen1 + outlen_final)
   5017  1.1.1.2  christos         /* now re-init the context (same cipher, key and iv) */
   5018  1.1.1.2  christos         || !TEST_true(EVP_CipherInit_ex2(ctx, NULL, NULL, NULL, -1, NULL))
   5019  1.1.1.2  christos         /* second iteration */
   5020  1.1.1.2  christos         || !TEST_true(EVP_CipherUpdate(ctx, outbuf2, &outlen2, t->input, t->inlen))
   5021  1.1.1.2  christos         || !TEST_true(EVP_CipherFinal_ex(ctx, outbuf2, &outlen_final))
   5022  1.1.1.2  christos         /* check test results iteration 2 */
   5023  1.1.1.2  christos         || !TEST_mem_eq(t->expected, t->expectedlen, outbuf2, outlen2 + outlen_final))
   5024      1.1  christos         goto err;
   5025      1.1  christos     testresult = 1;
   5026  1.1.1.2  christos err:
   5027      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5028      1.1  christos     EVP_CIPHER_free(type);
   5029      1.1  christos     return testresult;
   5030      1.1  christos }
   5031      1.1  christos 
   5032      1.1  christos typedef struct {
   5033      1.1  christos     const unsigned char *input;
   5034      1.1  christos     const unsigned char *expected;
   5035      1.1  christos     size_t inlen;
   5036      1.1  christos     size_t expectedlen;
   5037      1.1  christos     int enc;
   5038      1.1  christos } EVP_RESET_TEST_st;
   5039      1.1  christos 
   5040      1.1  christos static const EVP_RESET_TEST_st evp_reset_tests[] = {
   5041  1.1.1.2  christos     { cfbPlaintext, cfbCiphertext,
   5042  1.1.1.2  christos         sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1 },
   5043  1.1.1.2  christos     { cfbCiphertext, cfbPlaintext,
   5044  1.1.1.2  christos         sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0 }
   5045      1.1  christos };
   5046      1.1  christos 
   5047      1.1  christos /*
   5048      1.1  christos  * Test a reset of a cipher via EVP_CipherInit_ex after the cipher has already
   5049      1.1  christos  * been used.
   5050      1.1  christos  */
   5051      1.1  christos static int test_evp_reset(int idx)
   5052      1.1  christos {
   5053      1.1  christos     const EVP_RESET_TEST_st *t = &evp_reset_tests[idx];
   5054      1.1  christos     int outlen1, outlen2;
   5055      1.1  christos     int testresult = 0;
   5056      1.1  christos     unsigned char outbuf[1024];
   5057      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5058      1.1  christos     EVP_CIPHER *type = NULL;
   5059      1.1  christos     char *errmsg = NULL;
   5060      1.1  christos 
   5061      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
   5062      1.1  christos         errmsg = "CTX_ALLOC";
   5063      1.1  christos         goto err;
   5064      1.1  christos     }
   5065      1.1  christos     if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq))) {
   5066      1.1  christos         errmsg = "CIPHER_FETCH";
   5067      1.1  christos         goto err;
   5068      1.1  christos     }
   5069      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
   5070      1.1  christos         errmsg = "CIPHER_INIT";
   5071      1.1  christos         goto err;
   5072      1.1  christos     }
   5073      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
   5074      1.1  christos         errmsg = "PADDING";
   5075      1.1  christos         goto err;
   5076      1.1  christos     }
   5077      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
   5078      1.1  christos         errmsg = "CIPHER_UPDATE";
   5079      1.1  christos         goto err;
   5080      1.1  christos     }
   5081      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   5082      1.1  christos         errmsg = "CIPHER_FINAL";
   5083      1.1  christos         goto err;
   5084      1.1  christos     }
   5085      1.1  christos     if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
   5086      1.1  christos         errmsg = "WRONG_RESULT";
   5087      1.1  christos         goto err;
   5088      1.1  christos     }
   5089      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) {
   5090      1.1  christos         errmsg = "CIPHER_REINIT";
   5091      1.1  christos         goto err;
   5092      1.1  christos     }
   5093      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
   5094      1.1  christos         errmsg = "CIPHER_UPDATE (reinit)";
   5095      1.1  christos         goto err;
   5096      1.1  christos     }
   5097      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   5098      1.1  christos         errmsg = "CIPHER_FINAL (reinit)";
   5099      1.1  christos         goto err;
   5100      1.1  christos     }
   5101      1.1  christos     if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
   5102      1.1  christos         errmsg = "WRONG_RESULT (reinit)";
   5103      1.1  christos         goto err;
   5104      1.1  christos     }
   5105      1.1  christos     testresult = 1;
   5106  1.1.1.2  christos err:
   5107      1.1  christos     if (errmsg != NULL)
   5108      1.1  christos         TEST_info("test_evp_reset %d: %s", idx, errmsg);
   5109      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5110      1.1  christos     EVP_CIPHER_free(type);
   5111      1.1  christos     return testresult;
   5112      1.1  christos }
   5113      1.1  christos 
   5114      1.1  christos typedef struct {
   5115      1.1  christos     const char *cipher;
   5116      1.1  christos     int enc;
   5117      1.1  christos } EVP_UPDATED_IV_TEST_st;
   5118      1.1  christos 
   5119      1.1  christos static const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = {
   5120  1.1.1.2  christos     { "aes-128-cfb", 1 },
   5121  1.1.1.2  christos     { "aes-128-cfb", 0 },
   5122  1.1.1.2  christos     { "aes-128-cfb1", 1 },
   5123  1.1.1.2  christos     { "aes-128-cfb1", 0 },
   5124  1.1.1.2  christos     { "aes-128-cfb8", 1 },
   5125  1.1.1.2  christos     { "aes-128-cfb8", 0 },
   5126  1.1.1.2  christos     { "aes-128-ofb", 1 },
   5127  1.1.1.2  christos     { "aes-128-ofb", 0 },
   5128  1.1.1.2  christos     { "aes-128-ctr", 1 },
   5129  1.1.1.2  christos     { "aes-128-ctr", 0 },
   5130  1.1.1.2  christos     { "aes-128-cbc", 1 },
   5131  1.1.1.2  christos     { "aes-128-cbc", 0 }
   5132      1.1  christos };
   5133      1.1  christos 
   5134      1.1  christos /*
   5135      1.1  christos  * Test that the IV in the context is updated during a crypto operation for CFB
   5136      1.1  christos  * and OFB.
   5137      1.1  christos  */
   5138      1.1  christos static int test_evp_updated_iv(int idx)
   5139      1.1  christos {
   5140      1.1  christos     const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx];
   5141      1.1  christos     int outlen1, outlen2;
   5142      1.1  christos     int testresult = 0;
   5143      1.1  christos     unsigned char outbuf[1024];
   5144      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5145      1.1  christos     EVP_CIPHER *type = NULL;
   5146      1.1  christos     unsigned char updated_iv[EVP_MAX_IV_LENGTH];
   5147      1.1  christos     int iv_len;
   5148      1.1  christos     char *errmsg = NULL;
   5149      1.1  christos 
   5150      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
   5151      1.1  christos         errmsg = "CTX_ALLOC";
   5152      1.1  christos         goto err;
   5153      1.1  christos     }
   5154      1.1  christos     if ((type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) == NULL) {
   5155      1.1  christos         TEST_info("cipher %s not supported, skipping", t->cipher);
   5156      1.1  christos         goto ok;
   5157      1.1  christos     }
   5158      1.1  christos 
   5159      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
   5160      1.1  christos         errmsg = "CIPHER_INIT";
   5161      1.1  christos         goto err;
   5162      1.1  christos     }
   5163      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
   5164      1.1  christos         errmsg = "PADDING";
   5165      1.1  christos         goto err;
   5166      1.1  christos     }
   5167      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) {
   5168      1.1  christos         errmsg = "CIPHER_UPDATE";
   5169      1.1  christos         goto err;
   5170      1.1  christos     }
   5171      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, updated_iv, sizeof(updated_iv)))) {
   5172      1.1  christos         errmsg = "CIPHER_CTX_GET_UPDATED_IV";
   5173      1.1  christos         goto err;
   5174      1.1  christos     }
   5175      1.1  christos     iv_len = EVP_CIPHER_CTX_get_iv_length(ctx);
   5176  1.1.1.2  christos     if (!TEST_int_ge(iv_len, 0)) {
   5177      1.1  christos         errmsg = "CIPHER_CTX_GET_IV_LEN";
   5178      1.1  christos         goto err;
   5179      1.1  christos     }
   5180      1.1  christos     if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) {
   5181      1.1  christos         errmsg = "IV_NOT_UPDATED";
   5182      1.1  christos         goto err;
   5183      1.1  christos     }
   5184      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   5185      1.1  christos         errmsg = "CIPHER_FINAL";
   5186      1.1  christos         goto err;
   5187      1.1  christos     }
   5188  1.1.1.2  christos ok:
   5189      1.1  christos     testresult = 1;
   5190  1.1.1.2  christos err:
   5191      1.1  christos     if (errmsg != NULL)
   5192      1.1  christos         TEST_info("test_evp_updated_iv %d: %s", idx, errmsg);
   5193      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5194      1.1  christos     EVP_CIPHER_free(type);
   5195      1.1  christos     return testresult;
   5196      1.1  christos }
   5197      1.1  christos 
   5198      1.1  christos typedef struct {
   5199      1.1  christos     const unsigned char *iv1;
   5200      1.1  christos     const unsigned char *iv2;
   5201      1.1  christos     const unsigned char *expected1;
   5202      1.1  christos     const unsigned char *expected2;
   5203      1.1  christos     const unsigned char *tag1;
   5204      1.1  christos     const unsigned char *tag2;
   5205      1.1  christos     size_t ivlen1;
   5206      1.1  christos     size_t ivlen2;
   5207      1.1  christos     size_t expectedlen1;
   5208      1.1  christos     size_t expectedlen2;
   5209      1.1  christos } TEST_GCM_IV_REINIT_st;
   5210      1.1  christos 
   5211      1.1  christos static const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = {
   5212  1.1.1.2  christos     { iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2,
   5213      1.1  christos         gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2),
   5214  1.1.1.2  christos         sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2) },
   5215  1.1.1.2  christos     { iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1,
   5216      1.1  christos         gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1),
   5217  1.1.1.2  christos         sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1) }
   5218      1.1  christos };
   5219      1.1  christos 
   5220      1.1  christos static int test_gcm_reinit(int idx)
   5221      1.1  christos {
   5222      1.1  christos     int outlen1, outlen2, outlen3;
   5223      1.1  christos     int testresult = 0;
   5224      1.1  christos     unsigned char outbuf[1024];
   5225      1.1  christos     unsigned char tag[16];
   5226      1.1  christos     const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx];
   5227      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5228      1.1  christos     EVP_CIPHER *type = NULL;
   5229      1.1  christos     size_t taglen = sizeof(tag);
   5230      1.1  christos     char *errmsg = NULL;
   5231      1.1  christos 
   5232      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
   5233      1.1  christos         errmsg = "CTX_ALLOC";
   5234      1.1  christos         goto err;
   5235      1.1  christos     }
   5236      1.1  christos     if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-256-gcm", testpropq))) {
   5237      1.1  christos         errmsg = "CIPHER_FETCH";
   5238      1.1  christos         goto err;
   5239      1.1  christos     }
   5240      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) {
   5241      1.1  christos         errmsg = "ENC_INIT";
   5242      1.1  christos         goto err;
   5243      1.1  christos     }
   5244      1.1  christos     if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL), 0)) {
   5245      1.1  christos         errmsg = "SET_IVLEN1";
   5246      1.1  christos         goto err;
   5247      1.1  christos     }
   5248      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) {
   5249      1.1  christos         errmsg = "SET_IV1";
   5250      1.1  christos         goto err;
   5251      1.1  christos     }
   5252      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
   5253      1.1  christos         errmsg = "AAD1";
   5254      1.1  christos         goto err;
   5255      1.1  christos     }
   5256      1.1  christos     EVP_CIPHER_CTX_set_padding(ctx, 0);
   5257      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
   5258  1.1.1.2  christos             sizeof(gcmResetPlaintext)))) {
   5259      1.1  christos         errmsg = "CIPHER_UPDATE1";
   5260      1.1  christos         goto err;
   5261      1.1  christos     }
   5262      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   5263      1.1  christos         errmsg = "CIPHER_FINAL1";
   5264      1.1  christos         goto err;
   5265      1.1  christos     }
   5266      1.1  christos     if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) {
   5267      1.1  christos         errmsg = "WRONG_RESULT1";
   5268      1.1  christos         goto err;
   5269      1.1  christos     }
   5270      1.1  christos     if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
   5271      1.1  christos         errmsg = "GET_TAG1";
   5272      1.1  christos         goto err;
   5273      1.1  christos     }
   5274      1.1  christos     if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) {
   5275      1.1  christos         errmsg = "TAG_ERROR1";
   5276      1.1  christos         goto err;
   5277      1.1  christos     }
   5278      1.1  christos     /* Now reinit */
   5279      1.1  christos     if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL), 0)) {
   5280      1.1  christos         errmsg = "SET_IVLEN2";
   5281      1.1  christos         goto err;
   5282      1.1  christos     }
   5283      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) {
   5284      1.1  christos         errmsg = "SET_IV2";
   5285      1.1  christos         goto err;
   5286      1.1  christos     }
   5287      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
   5288      1.1  christos         errmsg = "AAD2";
   5289      1.1  christos         goto err;
   5290      1.1  christos     }
   5291      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
   5292  1.1.1.2  christos             sizeof(gcmResetPlaintext)))) {
   5293      1.1  christos         errmsg = "CIPHER_UPDATE2";
   5294      1.1  christos         goto err;
   5295      1.1  christos     }
   5296      1.1  christos     if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
   5297      1.1  christos         errmsg = "CIPHER_FINAL2";
   5298      1.1  christos         goto err;
   5299      1.1  christos     }
   5300      1.1  christos     if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) {
   5301      1.1  christos         errmsg = "WRONG_RESULT2";
   5302      1.1  christos         goto err;
   5303      1.1  christos     }
   5304      1.1  christos     if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
   5305      1.1  christos         errmsg = "GET_TAG2";
   5306      1.1  christos         goto err;
   5307      1.1  christos     }
   5308      1.1  christos     if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) {
   5309      1.1  christos         errmsg = "TAG_ERROR2";
   5310      1.1  christos         goto err;
   5311      1.1  christos     }
   5312      1.1  christos     testresult = 1;
   5313  1.1.1.2  christos err:
   5314      1.1  christos     if (errmsg != NULL)
   5315      1.1  christos         TEST_info("evp_init_test %d: %s", idx, errmsg);
   5316      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5317      1.1  christos     EVP_CIPHER_free(type);
   5318      1.1  christos     return testresult;
   5319      1.1  christos }
   5320      1.1  christos 
   5321      1.1  christos static const char *ivlen_change_ciphers[] = {
   5322      1.1  christos     "AES-256-GCM",
   5323      1.1  christos #ifndef OPENSSL_NO_OCB
   5324      1.1  christos     "AES-256-OCB",
   5325      1.1  christos #endif
   5326      1.1  christos     "AES-256-CCM"
   5327      1.1  christos };
   5328      1.1  christos 
   5329      1.1  christos /* Negative test for ivlen change after iv being set */
   5330      1.1  christos static int test_ivlen_change(int idx)
   5331      1.1  christos {
   5332      1.1  christos     int outlen;
   5333      1.1  christos     int res = 0;
   5334      1.1  christos     unsigned char outbuf[1024];
   5335      1.1  christos     static const unsigned char iv[] = {
   5336  1.1.1.2  christos         0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
   5337  1.1.1.2  christos         0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
   5338      1.1  christos     };
   5339      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5340      1.1  christos     EVP_CIPHER *ciph = NULL;
   5341      1.1  christos     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
   5342      1.1  christos     size_t ivlen = 13; /* non-default IV length */
   5343      1.1  christos 
   5344      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
   5345      1.1  christos         goto err;
   5346      1.1  christos 
   5347      1.1  christos     if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, ivlen_change_ciphers[idx],
   5348  1.1.1.2  christos                       testpropq)))
   5349      1.1  christos         goto err;
   5350      1.1  christos 
   5351      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, kGCMDefaultKey, iv, 1)))
   5352      1.1  christos         goto err;
   5353      1.1  christos 
   5354      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
   5355  1.1.1.2  christos             sizeof(gcmDefaultPlaintext))))
   5356      1.1  christos         goto err;
   5357      1.1  christos 
   5358      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
   5359  1.1.1.2  christos         &ivlen);
   5360      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
   5361      1.1  christos         goto err;
   5362      1.1  christos 
   5363      1.1  christos     ERR_set_mark();
   5364      1.1  christos     if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
   5365  1.1.1.2  christos             sizeof(gcmDefaultPlaintext)))) {
   5366      1.1  christos         ERR_clear_last_mark();
   5367      1.1  christos         goto err;
   5368      1.1  christos     }
   5369      1.1  christos     ERR_pop_to_mark();
   5370      1.1  christos 
   5371      1.1  christos     res = 1;
   5372  1.1.1.2  christos err:
   5373      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5374      1.1  christos     EVP_CIPHER_free(ciph);
   5375      1.1  christos     return res;
   5376      1.1  christos }
   5377      1.1  christos 
   5378      1.1  christos static const char *keylen_change_ciphers[] = {
   5379      1.1  christos #ifndef OPENSSL_NO_BF
   5380      1.1  christos     "BF-ECB",
   5381      1.1  christos #endif
   5382      1.1  christos #ifndef OPENSSL_NO_CAST
   5383      1.1  christos     "CAST5-ECB",
   5384      1.1  christos #endif
   5385      1.1  christos #ifndef OPENSSL_NO_RC2
   5386      1.1  christos     "RC2-ECB",
   5387      1.1  christos #endif
   5388      1.1  christos #ifndef OPENSSL_NO_RC4
   5389      1.1  christos     "RC4",
   5390      1.1  christos #endif
   5391      1.1  christos #ifndef OPENSSL_NO_RC5
   5392      1.1  christos     "RC5-ECB",
   5393      1.1  christos #endif
   5394      1.1  christos     NULL
   5395      1.1  christos };
   5396      1.1  christos 
   5397      1.1  christos /* Negative test for keylen change after key was set */
   5398      1.1  christos static int test_keylen_change(int idx)
   5399      1.1  christos {
   5400      1.1  christos     int outlen;
   5401      1.1  christos     int res = 0;
   5402      1.1  christos     unsigned char outbuf[1024];
   5403      1.1  christos     static const unsigned char key[] = {
   5404  1.1.1.2  christos         0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
   5405  1.1.1.2  christos         0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34
   5406      1.1  christos     };
   5407      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   5408      1.1  christos     EVP_CIPHER *ciph = NULL;
   5409      1.1  christos     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
   5410      1.1  christos     size_t keylen = 12; /* non-default key length */
   5411      1.1  christos 
   5412      1.1  christos     if (lgcyprov == NULL)
   5413      1.1  christos         return TEST_skip("Test requires legacy provider to be loaded");
   5414      1.1  christos 
   5415      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
   5416      1.1  christos         goto err;
   5417      1.1  christos 
   5418      1.1  christos     if (!TEST_ptr(ciph = EVP_CIPHER_fetch(testctx, keylen_change_ciphers[idx],
   5419  1.1.1.2  christos                       testpropq)))
   5420      1.1  christos         goto err;
   5421      1.1  christos 
   5422      1.1  christos     if (!TEST_true(EVP_CipherInit_ex(ctx, ciph, NULL, key, NULL, 1)))
   5423      1.1  christos         goto err;
   5424      1.1  christos 
   5425      1.1  christos     if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
   5426  1.1.1.2  christos             sizeof(gcmDefaultPlaintext))))
   5427      1.1  christos         goto err;
   5428      1.1  christos 
   5429      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
   5430  1.1.1.2  christos         &keylen);
   5431      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params)))
   5432      1.1  christos         goto err;
   5433      1.1  christos 
   5434      1.1  christos     ERR_set_mark();
   5435      1.1  christos     if (!TEST_false(EVP_CipherUpdate(ctx, outbuf, &outlen, gcmDefaultPlaintext,
   5436  1.1.1.2  christos             sizeof(gcmDefaultPlaintext)))) {
   5437      1.1  christos         ERR_clear_last_mark();
   5438      1.1  christos         goto err;
   5439      1.1  christos     }
   5440      1.1  christos     ERR_pop_to_mark();
   5441      1.1  christos 
   5442      1.1  christos     res = 1;
   5443  1.1.1.2  christos err:
   5444      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   5445      1.1  christos     EVP_CIPHER_free(ciph);
   5446      1.1  christos     return res;
   5447      1.1  christos }
   5448      1.1  christos 
   5449      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   5450  1.1.1.2  christos static EVP_PKEY_METHOD *custom_pmeth = NULL;
   5451      1.1  christos static const EVP_PKEY_METHOD *orig_pmeth = NULL;
   5452      1.1  christos 
   5453  1.1.1.2  christos #define EVP_PKEY_CTRL_MY_COMMAND 9999
   5454      1.1  christos 
   5455      1.1  christos static int custom_pmeth_init(EVP_PKEY_CTX *ctx)
   5456      1.1  christos {
   5457      1.1  christos     int (*pinit)(EVP_PKEY_CTX *ctx);
   5458      1.1  christos 
   5459      1.1  christos     EVP_PKEY_meth_get_init(orig_pmeth, &pinit);
   5460      1.1  christos     return pinit(ctx);
   5461      1.1  christos }
   5462      1.1  christos 
   5463      1.1  christos static void custom_pmeth_cleanup(EVP_PKEY_CTX *ctx)
   5464      1.1  christos {
   5465      1.1  christos     void (*pcleanup)(EVP_PKEY_CTX *ctx);
   5466      1.1  christos 
   5467      1.1  christos     EVP_PKEY_meth_get_cleanup(orig_pmeth, &pcleanup);
   5468      1.1  christos     pcleanup(ctx);
   5469      1.1  christos }
   5470      1.1  christos 
   5471      1.1  christos static int custom_pmeth_sign(EVP_PKEY_CTX *ctx, unsigned char *out,
   5472  1.1.1.2  christos     size_t *outlen, const unsigned char *in,
   5473  1.1.1.2  christos     size_t inlen)
   5474      1.1  christos {
   5475      1.1  christos     int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
   5476  1.1.1.2  christos         const unsigned char *tbs, size_t tbslen);
   5477      1.1  christos 
   5478      1.1  christos     EVP_PKEY_meth_get_sign(orig_pmeth, NULL, &psign);
   5479      1.1  christos     return psign(ctx, out, outlen, in, inlen);
   5480      1.1  christos }
   5481      1.1  christos 
   5482      1.1  christos static int custom_pmeth_digestsign(EVP_MD_CTX *ctx, unsigned char *sig,
   5483  1.1.1.2  christos     size_t *siglen, const unsigned char *tbs,
   5484  1.1.1.2  christos     size_t tbslen)
   5485      1.1  christos {
   5486      1.1  christos     int (*pdigestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
   5487  1.1.1.2  christos         const unsigned char *tbs, size_t tbslen);
   5488      1.1  christos 
   5489      1.1  christos     EVP_PKEY_meth_get_digestsign(orig_pmeth, &pdigestsign);
   5490      1.1  christos     return pdigestsign(ctx, sig, siglen, tbs, tbslen);
   5491      1.1  christos }
   5492      1.1  christos 
   5493      1.1  christos static int custom_pmeth_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
   5494  1.1.1.2  christos     size_t *keylen)
   5495      1.1  christos {
   5496      1.1  christos     int (*pderive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
   5497      1.1  christos 
   5498      1.1  christos     EVP_PKEY_meth_get_derive(orig_pmeth, NULL, &pderive);
   5499      1.1  christos     return pderive(ctx, key, keylen);
   5500      1.1  christos }
   5501      1.1  christos 
   5502      1.1  christos static int custom_pmeth_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
   5503      1.1  christos {
   5504      1.1  christos     int (*pcopy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
   5505      1.1  christos 
   5506      1.1  christos     EVP_PKEY_meth_get_copy(orig_pmeth, &pcopy);
   5507      1.1  christos     return pcopy(dst, src);
   5508      1.1  christos }
   5509      1.1  christos 
   5510      1.1  christos static int ctrl_called;
   5511      1.1  christos 
   5512      1.1  christos static int custom_pmeth_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
   5513      1.1  christos {
   5514      1.1  christos     int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
   5515      1.1  christos 
   5516      1.1  christos     EVP_PKEY_meth_get_ctrl(orig_pmeth, &pctrl, NULL);
   5517      1.1  christos 
   5518      1.1  christos     if (type == EVP_PKEY_CTRL_MY_COMMAND) {
   5519      1.1  christos         ctrl_called = 1;
   5520      1.1  christos         return 1;
   5521      1.1  christos     }
   5522      1.1  christos 
   5523      1.1  christos     return pctrl(ctx, type, p1, p2);
   5524      1.1  christos }
   5525      1.1  christos 
   5526      1.1  christos static int test_custom_pmeth(int idx)
   5527      1.1  christos {
   5528      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   5529      1.1  christos     EVP_MD_CTX *ctx = NULL;
   5530      1.1  christos     EVP_PKEY *pkey = NULL;
   5531      1.1  christos     int id, orig_id, orig_flags;
   5532      1.1  christos     int testresult = 0;
   5533      1.1  christos     size_t reslen;
   5534      1.1  christos     unsigned char *res = NULL;
   5535      1.1  christos     unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o' };
   5536      1.1  christos     const EVP_MD *md = EVP_sha256();
   5537      1.1  christos     int doderive = 0;
   5538      1.1  christos 
   5539      1.1  christos     ctrl_called = 0;
   5540      1.1  christos 
   5541      1.1  christos     /* We call deprecated APIs so this test doesn't support a custom libctx */
   5542      1.1  christos     if (testctx != NULL)
   5543      1.1  christos         return 1;
   5544      1.1  christos 
   5545      1.1  christos     switch (idx) {
   5546      1.1  christos     case 0:
   5547      1.1  christos     case 6:
   5548      1.1  christos         id = EVP_PKEY_RSA;
   5549      1.1  christos         pkey = load_example_rsa_key();
   5550      1.1  christos         break;
   5551      1.1  christos     case 1:
   5552      1.1  christos     case 7:
   5553  1.1.1.2  christos #ifndef OPENSSL_NO_DSA
   5554      1.1  christos         id = EVP_PKEY_DSA;
   5555      1.1  christos         pkey = load_example_dsa_key();
   5556      1.1  christos         break;
   5557  1.1.1.2  christos #else
   5558      1.1  christos         return 1;
   5559  1.1.1.2  christos #endif
   5560      1.1  christos     case 2:
   5561      1.1  christos     case 8:
   5562  1.1.1.2  christos #ifndef OPENSSL_NO_EC
   5563      1.1  christos         id = EVP_PKEY_EC;
   5564      1.1  christos         pkey = load_example_ec_key();
   5565      1.1  christos         break;
   5566  1.1.1.2  christos #else
   5567      1.1  christos         return 1;
   5568  1.1.1.2  christos #endif
   5569      1.1  christos     case 3:
   5570      1.1  christos     case 9:
   5571  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
   5572      1.1  christos         id = EVP_PKEY_ED25519;
   5573      1.1  christos         md = NULL;
   5574      1.1  christos         pkey = load_example_ed25519_key();
   5575      1.1  christos         break;
   5576  1.1.1.2  christos #else
   5577      1.1  christos         return 1;
   5578  1.1.1.2  christos #endif
   5579      1.1  christos     case 4:
   5580      1.1  christos     case 10:
   5581  1.1.1.2  christos #ifndef OPENSSL_NO_DH
   5582      1.1  christos         id = EVP_PKEY_DH;
   5583      1.1  christos         doderive = 1;
   5584      1.1  christos         pkey = load_example_dh_key();
   5585      1.1  christos         break;
   5586  1.1.1.2  christos #else
   5587      1.1  christos         return 1;
   5588  1.1.1.2  christos #endif
   5589      1.1  christos     case 5:
   5590      1.1  christos     case 11:
   5591  1.1.1.2  christos #ifndef OPENSSL_NO_ECX
   5592      1.1  christos         id = EVP_PKEY_X25519;
   5593      1.1  christos         doderive = 1;
   5594      1.1  christos         pkey = load_example_x25519_key();
   5595      1.1  christos         break;
   5596  1.1.1.2  christos #else
   5597      1.1  christos         return 1;
   5598  1.1.1.2  christos #endif
   5599      1.1  christos     default:
   5600      1.1  christos         TEST_error("Should not happen");
   5601      1.1  christos         goto err;
   5602      1.1  christos     }
   5603      1.1  christos 
   5604      1.1  christos     if (!TEST_ptr(pkey))
   5605      1.1  christos         goto err;
   5606      1.1  christos 
   5607      1.1  christos     if (idx < 6) {
   5608      1.1  christos         if (!TEST_true(evp_pkey_is_provided(pkey)))
   5609      1.1  christos             goto err;
   5610      1.1  christos     } else {
   5611      1.1  christos         EVP_PKEY *tmp = pkey;
   5612      1.1  christos 
   5613      1.1  christos         /* Convert to a legacy key */
   5614      1.1  christos         pkey = EVP_PKEY_new();
   5615      1.1  christos         if (!TEST_ptr(pkey)) {
   5616      1.1  christos             pkey = tmp;
   5617      1.1  christos             goto err;
   5618      1.1  christos         }
   5619      1.1  christos         if (!TEST_true(evp_pkey_copy_downgraded(&pkey, tmp))) {
   5620      1.1  christos             EVP_PKEY_free(tmp);
   5621      1.1  christos             goto err;
   5622      1.1  christos         }
   5623      1.1  christos         EVP_PKEY_free(tmp);
   5624      1.1  christos         if (!TEST_true(evp_pkey_is_legacy(pkey)))
   5625      1.1  christos             goto err;
   5626      1.1  christos     }
   5627      1.1  christos 
   5628      1.1  christos     if (!TEST_ptr(orig_pmeth = EVP_PKEY_meth_find(id))
   5629  1.1.1.2  christos         || !TEST_ptr(pkey))
   5630      1.1  christos         goto err;
   5631      1.1  christos 
   5632      1.1  christos     EVP_PKEY_meth_get0_info(&orig_id, &orig_flags, orig_pmeth);
   5633      1.1  christos     if (!TEST_int_eq(orig_id, id)
   5634  1.1.1.2  christos         || !TEST_ptr(custom_pmeth = EVP_PKEY_meth_new(id, orig_flags)))
   5635      1.1  christos         goto err;
   5636      1.1  christos 
   5637      1.1  christos     if (id == EVP_PKEY_ED25519) {
   5638      1.1  christos         EVP_PKEY_meth_set_digestsign(custom_pmeth, custom_pmeth_digestsign);
   5639  1.1.1.2  christos     }
   5640  1.1.1.2  christos     if (id == EVP_PKEY_DH || id == EVP_PKEY_X25519) {
   5641      1.1  christos         EVP_PKEY_meth_set_derive(custom_pmeth, NULL, custom_pmeth_derive);
   5642      1.1  christos     } else {
   5643      1.1  christos         EVP_PKEY_meth_set_sign(custom_pmeth, NULL, custom_pmeth_sign);
   5644      1.1  christos     }
   5645      1.1  christos     if (id != EVP_PKEY_ED25519 && id != EVP_PKEY_X25519) {
   5646      1.1  christos         EVP_PKEY_meth_set_init(custom_pmeth, custom_pmeth_init);
   5647      1.1  christos         EVP_PKEY_meth_set_cleanup(custom_pmeth, custom_pmeth_cleanup);
   5648      1.1  christos         EVP_PKEY_meth_set_copy(custom_pmeth, custom_pmeth_copy);
   5649      1.1  christos     }
   5650      1.1  christos     EVP_PKEY_meth_set_ctrl(custom_pmeth, custom_pmeth_ctrl, NULL);
   5651      1.1  christos     if (!TEST_true(EVP_PKEY_meth_add0(custom_pmeth)))
   5652      1.1  christos         goto err;
   5653      1.1  christos 
   5654      1.1  christos     if (doderive) {
   5655      1.1  christos         pctx = EVP_PKEY_CTX_new(pkey, NULL);
   5656      1.1  christos         if (!TEST_ptr(pctx)
   5657  1.1.1.2  christos             || !TEST_int_eq(EVP_PKEY_derive_init(pctx), 1)
   5658  1.1.1.2  christos             || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
   5659  1.1.1.2  christos                                 EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
   5660  1.1.1.2  christos                 1)
   5661  1.1.1.2  christos             || !TEST_int_eq(ctrl_called, 1)
   5662  1.1.1.2  christos             || !TEST_int_ge(EVP_PKEY_derive_set_peer(pctx, pkey), 1)
   5663  1.1.1.2  christos             || !TEST_int_ge(EVP_PKEY_derive(pctx, NULL, &reslen), 1)
   5664  1.1.1.2  christos             || !TEST_ptr(res = OPENSSL_malloc(reslen))
   5665  1.1.1.2  christos             || !TEST_int_ge(EVP_PKEY_derive(pctx, res, &reslen), 1))
   5666      1.1  christos             goto err;
   5667      1.1  christos     } else {
   5668      1.1  christos         ctx = EVP_MD_CTX_new();
   5669      1.1  christos         reslen = EVP_PKEY_size(pkey);
   5670      1.1  christos         res = OPENSSL_malloc(reslen);
   5671      1.1  christos         if (!TEST_ptr(ctx)
   5672  1.1.1.2  christos             || !TEST_ptr(res)
   5673  1.1.1.2  christos             || !TEST_true(EVP_DigestSignInit(ctx, &pctx, md, NULL, pkey))
   5674  1.1.1.2  christos             || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
   5675  1.1.1.2  christos                                 EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
   5676  1.1.1.2  christos                 1)
   5677  1.1.1.2  christos             || !TEST_int_eq(ctrl_called, 1))
   5678      1.1  christos             goto err;
   5679      1.1  christos 
   5680      1.1  christos         if (id == EVP_PKEY_ED25519) {
   5681      1.1  christos             if (!TEST_true(EVP_DigestSign(ctx, res, &reslen, msg, sizeof(msg))))
   5682      1.1  christos                 goto err;
   5683      1.1  christos         } else {
   5684      1.1  christos             if (!TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
   5685  1.1.1.2  christos                 || !TEST_true(EVP_DigestSignFinal(ctx, res, &reslen)))
   5686      1.1  christos                 goto err;
   5687      1.1  christos         }
   5688      1.1  christos     }
   5689      1.1  christos 
   5690      1.1  christos     testresult = 1;
   5691  1.1.1.2  christos err:
   5692      1.1  christos     OPENSSL_free(res);
   5693      1.1  christos     EVP_MD_CTX_free(ctx);
   5694      1.1  christos     if (doderive)
   5695      1.1  christos         EVP_PKEY_CTX_free(pctx);
   5696      1.1  christos     EVP_PKEY_free(pkey);
   5697      1.1  christos     EVP_PKEY_meth_remove(custom_pmeth);
   5698      1.1  christos     EVP_PKEY_meth_free(custom_pmeth);
   5699      1.1  christos     custom_pmeth = NULL;
   5700      1.1  christos     return testresult;
   5701      1.1  christos }
   5702      1.1  christos 
   5703      1.1  christos static int test_evp_md_cipher_meth(void)
   5704      1.1  christos {
   5705      1.1  christos     EVP_MD *md = EVP_MD_meth_dup(EVP_sha256());
   5706      1.1  christos     EVP_CIPHER *ciph = EVP_CIPHER_meth_dup(EVP_aes_128_cbc());
   5707      1.1  christos     int testresult = 0;
   5708      1.1  christos 
   5709      1.1  christos     if (!TEST_ptr(md) || !TEST_ptr(ciph))
   5710      1.1  christos         goto err;
   5711      1.1  christos 
   5712      1.1  christos     testresult = 1;
   5713      1.1  christos 
   5714  1.1.1.2  christos err:
   5715      1.1  christos     EVP_MD_meth_free(md);
   5716      1.1  christos     EVP_CIPHER_meth_free(ciph);
   5717      1.1  christos 
   5718      1.1  christos     return testresult;
   5719      1.1  christos }
   5720      1.1  christos 
   5721      1.1  christos typedef struct {
   5722  1.1.1.2  christos     int data;
   5723      1.1  christos } custom_dgst_ctx;
   5724      1.1  christos 
   5725      1.1  christos static int custom_md_init_called = 0;
   5726      1.1  christos static int custom_md_cleanup_called = 0;
   5727      1.1  christos 
   5728      1.1  christos static int custom_md_init(EVP_MD_CTX *ctx)
   5729      1.1  christos {
   5730      1.1  christos     custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
   5731      1.1  christos 
   5732      1.1  christos     if (p == NULL)
   5733      1.1  christos         return 0;
   5734      1.1  christos 
   5735      1.1  christos     custom_md_init_called++;
   5736      1.1  christos     return 1;
   5737      1.1  christos }
   5738      1.1  christos 
   5739      1.1  christos static int custom_md_cleanup(EVP_MD_CTX *ctx)
   5740      1.1  christos {
   5741      1.1  christos     custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
   5742      1.1  christos 
   5743      1.1  christos     if (p == NULL)
   5744      1.1  christos         /* Nothing to do */
   5745      1.1  christos         return 1;
   5746      1.1  christos 
   5747      1.1  christos     custom_md_cleanup_called++;
   5748      1.1  christos     return 1;
   5749      1.1  christos }
   5750      1.1  christos 
   5751      1.1  christos static int test_custom_md_meth(void)
   5752      1.1  christos {
   5753      1.1  christos     ASN1_OBJECT *o = NULL;
   5754      1.1  christos     EVP_MD_CTX *mdctx = NULL;
   5755      1.1  christos     EVP_MD *tmp = NULL;
   5756      1.1  christos     char mess[] = "Test Message\n";
   5757      1.1  christos     unsigned char md_value[EVP_MAX_MD_SIZE];
   5758      1.1  christos     unsigned int md_len;
   5759      1.1  christos     int testresult = 0;
   5760      1.1  christos     int nid;
   5761      1.1  christos 
   5762      1.1  christos     /*
   5763      1.1  christos      * We are testing deprecated functions. We don't support a non-default
   5764      1.1  christos      * library context in this test.
   5765      1.1  christos      */
   5766      1.1  christos     if (testctx != NULL)
   5767      1.1  christos         return TEST_skip("Non-default libctx");
   5768      1.1  christos 
   5769      1.1  christos     custom_md_init_called = custom_md_cleanup_called = 0;
   5770      1.1  christos 
   5771      1.1  christos     nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md");
   5772      1.1  christos     if (!TEST_int_ne(nid, NID_undef))
   5773      1.1  christos         goto err;
   5774      1.1  christos     tmp = EVP_MD_meth_new(nid, NID_undef);
   5775      1.1  christos     if (!TEST_ptr(tmp))
   5776      1.1  christos         goto err;
   5777      1.1  christos 
   5778      1.1  christos     if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init))
   5779  1.1.1.2  christos         || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup))
   5780  1.1.1.2  christos         || !TEST_true(EVP_MD_meth_set_app_datasize(tmp,
   5781  1.1.1.2  christos             sizeof(custom_dgst_ctx))))
   5782      1.1  christos         goto err;
   5783      1.1  christos 
   5784      1.1  christos     mdctx = EVP_MD_CTX_new();
   5785      1.1  christos     if (!TEST_ptr(mdctx)
   5786  1.1.1.2  christos         /*
   5787  1.1.1.2  christos          * Initing our custom md and then initing another md should
   5788  1.1.1.2  christos          * result in the init and cleanup functions of the custom md
   5789  1.1.1.2  christos          * being called.
   5790  1.1.1.2  christos          */
   5791  1.1.1.2  christos         || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
   5792  1.1.1.2  christos         || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
   5793  1.1.1.2  christos         || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess)))
   5794  1.1.1.2  christos         || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len))
   5795  1.1.1.2  christos         || !TEST_int_eq(custom_md_init_called, 1)
   5796  1.1.1.2  christos         || !TEST_int_eq(custom_md_cleanup_called, 1))
   5797      1.1  christos         goto err;
   5798      1.1  christos 
   5799      1.1  christos     if (!TEST_int_eq(OBJ_create("1.3.6.1.4.1.16604.998866.1",
   5800  1.1.1.2  christos                          "custom-md", "custom-md"),
   5801  1.1.1.2  christos             NID_undef)
   5802  1.1.1.2  christos         || !TEST_int_eq(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OBJ)
   5803  1.1.1.2  christos         || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), OBJ_R_OID_EXISTS))
   5804      1.1  christos         goto err;
   5805      1.1  christos 
   5806  1.1.1.2  christos     o = ASN1_OBJECT_create(nid, (unsigned char *)"\53\6\1\4\1\201\201\134\274\373\122\1", 12,
   5807  1.1.1.2  christos         "custom-md", "custom-md");
   5808      1.1  christos     if (!TEST_int_eq(OBJ_add_object(o), nid))
   5809      1.1  christos         goto err;
   5810      1.1  christos 
   5811      1.1  christos     testresult = 1;
   5812  1.1.1.2  christos err:
   5813      1.1  christos     ASN1_OBJECT_free(o);
   5814      1.1  christos     EVP_MD_CTX_free(mdctx);
   5815      1.1  christos     EVP_MD_meth_free(tmp);
   5816      1.1  christos     return testresult;
   5817      1.1  christos }
   5818      1.1  christos 
   5819      1.1  christos typedef struct {
   5820  1.1.1.2  christos     int data;
   5821      1.1  christos } custom_ciph_ctx;
   5822      1.1  christos 
   5823      1.1  christos static int custom_ciph_init_called = 0;
   5824      1.1  christos static int custom_ciph_cleanup_called = 0;
   5825      1.1  christos 
   5826      1.1  christos static int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
   5827  1.1.1.2  christos     const unsigned char *iv, int enc)
   5828      1.1  christos {
   5829      1.1  christos     custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
   5830      1.1  christos 
   5831      1.1  christos     if (p == NULL)
   5832      1.1  christos         return 0;
   5833      1.1  christos 
   5834      1.1  christos     custom_ciph_init_called++;
   5835      1.1  christos     return 1;
   5836      1.1  christos }
   5837      1.1  christos 
   5838      1.1  christos static int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx)
   5839      1.1  christos {
   5840      1.1  christos     custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
   5841      1.1  christos 
   5842      1.1  christos     if (p == NULL)
   5843      1.1  christos         /* Nothing to do */
   5844      1.1  christos         return 1;
   5845      1.1  christos 
   5846      1.1  christos     custom_ciph_cleanup_called++;
   5847      1.1  christos     return 1;
   5848      1.1  christos }
   5849      1.1  christos 
   5850      1.1  christos static int test_custom_ciph_meth(void)
   5851      1.1  christos {
   5852      1.1  christos     EVP_CIPHER_CTX *ciphctx = NULL;
   5853      1.1  christos     EVP_CIPHER *tmp = NULL;
   5854      1.1  christos     int testresult = 0;
   5855      1.1  christos     int nid;
   5856      1.1  christos 
   5857      1.1  christos     /*
   5858      1.1  christos      * We are testing deprecated functions. We don't support a non-default
   5859      1.1  christos      * library context in this test.
   5860      1.1  christos      */
   5861      1.1  christos     if (testctx != NULL)
   5862      1.1  christos         return TEST_skip("Non-default libctx");
   5863      1.1  christos 
   5864      1.1  christos     custom_ciph_init_called = custom_ciph_cleanup_called = 0;
   5865      1.1  christos 
   5866      1.1  christos     nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph");
   5867      1.1  christos     if (!TEST_int_ne(nid, NID_undef))
   5868      1.1  christos         goto err;
   5869      1.1  christos     tmp = EVP_CIPHER_meth_new(nid, 16, 16);
   5870      1.1  christos     if (!TEST_ptr(tmp))
   5871      1.1  christos         goto err;
   5872      1.1  christos 
   5873      1.1  christos     if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init))
   5874  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT))
   5875  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup))
   5876  1.1.1.2  christos         || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp,
   5877  1.1.1.2  christos             sizeof(custom_ciph_ctx))))
   5878      1.1  christos         goto err;
   5879      1.1  christos 
   5880      1.1  christos     ciphctx = EVP_CIPHER_CTX_new();
   5881      1.1  christos     if (!TEST_ptr(ciphctx)
   5882  1.1.1.2  christos         /*
   5883  1.1.1.2  christos          * Initing our custom cipher and then initing another cipher
   5884  1.1.1.2  christos          * should result in the init and cleanup functions of the custom
   5885  1.1.1.2  christos          * cipher being called.
   5886  1.1.1.2  christos          */
   5887  1.1.1.2  christos         || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1))
   5888  1.1.1.2  christos         || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL,
   5889  1.1.1.2  christos             NULL, NULL, 1))
   5890  1.1.1.2  christos         || !TEST_int_eq(custom_ciph_init_called, 1)
   5891  1.1.1.2  christos         || !TEST_int_eq(custom_ciph_cleanup_called, 1))
   5892      1.1  christos         goto err;
   5893      1.1  christos 
   5894      1.1  christos     testresult = 1;
   5895  1.1.1.2  christos err:
   5896      1.1  christos     EVP_CIPHER_CTX_free(ciphctx);
   5897      1.1  christos     EVP_CIPHER_meth_free(tmp);
   5898      1.1  christos     return testresult;
   5899      1.1  christos }
   5900      1.1  christos 
   5901  1.1.1.2  christos #ifndef OPENSSL_NO_DYNAMIC_ENGINE
   5902      1.1  christos /* Test we can create a signature keys with an associated ENGINE */
   5903      1.1  christos static int test_signatures_with_engine(int tst)
   5904      1.1  christos {
   5905      1.1  christos     ENGINE *e;
   5906      1.1  christos     const char *engine_id = "dasync";
   5907      1.1  christos     EVP_PKEY *pkey = NULL;
   5908      1.1  christos     const unsigned char badcmackey[] = { 0x00, 0x01 };
   5909      1.1  christos     const unsigned char cmackey[] = {
   5910      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   5911      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f
   5912      1.1  christos     };
   5913      1.1  christos     const unsigned char ed25519key[] = {
   5914      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   5915      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
   5916      1.1  christos         0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
   5917      1.1  christos     };
   5918      1.1  christos     const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
   5919      1.1  christos     int testresult = 0;
   5920      1.1  christos     EVP_MD_CTX *ctx = NULL;
   5921      1.1  christos     unsigned char *mac = NULL;
   5922      1.1  christos     size_t maclen = 0;
   5923      1.1  christos     int ret;
   5924      1.1  christos 
   5925  1.1.1.2  christos #ifdef OPENSSL_NO_CMAC
   5926      1.1  christos     /* Skip CMAC tests in a no-cmac build */
   5927      1.1  christos     if (tst <= 1)
   5928      1.1  christos         return 1;
   5929  1.1.1.2  christos #endif
   5930  1.1.1.2  christos #ifdef OPENSSL_NO_ECX
   5931      1.1  christos     /* Skip ECX tests in a no-ecx build */
   5932      1.1  christos     if (tst == 2)
   5933      1.1  christos         return 1;
   5934  1.1.1.2  christos #endif
   5935      1.1  christos 
   5936      1.1  christos     if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
   5937      1.1  christos         return 0;
   5938      1.1  christos 
   5939      1.1  christos     if (!TEST_true(ENGINE_init(e))) {
   5940      1.1  christos         ENGINE_free(e);
   5941      1.1  christos         return 0;
   5942      1.1  christos     }
   5943      1.1  christos 
   5944      1.1  christos     switch (tst) {
   5945      1.1  christos     case 0:
   5946      1.1  christos         pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey),
   5947  1.1.1.2  christos             EVP_aes_128_cbc());
   5948      1.1  christos         break;
   5949      1.1  christos     case 1:
   5950      1.1  christos         pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey),
   5951  1.1.1.2  christos             EVP_aes_128_cbc());
   5952      1.1  christos         break;
   5953      1.1  christos     case 2:
   5954      1.1  christos         pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key,
   5955  1.1.1.2  christos             sizeof(ed25519key));
   5956      1.1  christos         break;
   5957      1.1  christos     default:
   5958      1.1  christos         TEST_error("Invalid test case");
   5959      1.1  christos         goto err;
   5960      1.1  christos     }
   5961      1.1  christos     if (!TEST_ptr(pkey))
   5962      1.1  christos         goto err;
   5963      1.1  christos 
   5964      1.1  christos     if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
   5965      1.1  christos         goto err;
   5966      1.1  christos 
   5967      1.1  christos     ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL,
   5968  1.1.1.2  christos         pkey);
   5969      1.1  christos     if (tst == 0) {
   5970      1.1  christos         if (!TEST_true(ret))
   5971      1.1  christos             goto err;
   5972      1.1  christos 
   5973      1.1  christos         if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg)))
   5974  1.1.1.2  christos             || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen)))
   5975      1.1  christos             goto err;
   5976      1.1  christos 
   5977      1.1  christos         if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
   5978      1.1  christos             goto err;
   5979      1.1  christos 
   5980      1.1  christos         if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen)))
   5981      1.1  christos             goto err;
   5982      1.1  christos     } else {
   5983      1.1  christos         /* We used a bad key. We expect a failure here */
   5984      1.1  christos         if (!TEST_false(ret))
   5985      1.1  christos             goto err;
   5986      1.1  christos     }
   5987      1.1  christos 
   5988      1.1  christos     testresult = 1;
   5989  1.1.1.2  christos err:
   5990      1.1  christos     EVP_MD_CTX_free(ctx);
   5991      1.1  christos     OPENSSL_free(mac);
   5992      1.1  christos     EVP_PKEY_free(pkey);
   5993      1.1  christos     ENGINE_finish(e);
   5994      1.1  christos     ENGINE_free(e);
   5995      1.1  christos 
   5996      1.1  christos     return testresult;
   5997      1.1  christos }
   5998      1.1  christos 
   5999      1.1  christos static int test_cipher_with_engine(void)
   6000      1.1  christos {
   6001      1.1  christos     ENGINE *e;
   6002      1.1  christos     const char *engine_id = "dasync";
   6003      1.1  christos     const unsigned char keyiv[] = {
   6004      1.1  christos         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
   6005      1.1  christos         0x0c, 0x0d, 0x0e, 0x0f
   6006      1.1  christos     };
   6007      1.1  christos     const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
   6008      1.1  christos     int testresult = 0;
   6009      1.1  christos     EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL;
   6010      1.1  christos     unsigned char buf[AES_BLOCK_SIZE];
   6011      1.1  christos     int len = 0;
   6012      1.1  christos 
   6013      1.1  christos     if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
   6014      1.1  christos         return 0;
   6015      1.1  christos 
   6016      1.1  christos     if (!TEST_true(ENGINE_init(e))) {
   6017      1.1  christos         ENGINE_free(e);
   6018      1.1  christos         return 0;
   6019      1.1  christos     }
   6020      1.1  christos 
   6021      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   6022  1.1.1.2  christos         || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new()))
   6023      1.1  christos         goto err;
   6024      1.1  christos 
   6025      1.1  christos     if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv)))
   6026      1.1  christos         goto err;
   6027      1.1  christos 
   6028      1.1  christos     /* Copy the ctx, and complete the operation with the new ctx */
   6029      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx)))
   6030      1.1  christos         goto err;
   6031      1.1  christos 
   6032      1.1  christos     if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg)))
   6033  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len)))
   6034      1.1  christos         goto err;
   6035      1.1  christos 
   6036      1.1  christos     testresult = 1;
   6037  1.1.1.2  christos err:
   6038      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6039      1.1  christos     EVP_CIPHER_CTX_free(ctx2);
   6040      1.1  christos     ENGINE_finish(e);
   6041      1.1  christos     ENGINE_free(e);
   6042      1.1  christos 
   6043      1.1  christos     return testresult;
   6044      1.1  christos }
   6045  1.1.1.2  christos #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
   6046      1.1  christos #endif /* OPENSSL_NO_DEPRECATED_3_0 */
   6047      1.1  christos 
   6048      1.1  christos #ifndef OPENSSL_NO_ECX
   6049      1.1  christos static int ecxnids[] = {
   6050      1.1  christos     NID_X25519,
   6051      1.1  christos     NID_X448,
   6052      1.1  christos     NID_ED25519,
   6053      1.1  christos     NID_ED448
   6054      1.1  christos };
   6055      1.1  christos 
   6056      1.1  christos /* Test that creating ECX keys with a short private key fails as expected */
   6057      1.1  christos static int test_ecx_short_keys(int tst)
   6058      1.1  christos {
   6059      1.1  christos     unsigned char ecxkeydata = 1;
   6060      1.1  christos     EVP_PKEY *pkey;
   6061      1.1  christos 
   6062      1.1  christos     pkey = EVP_PKEY_new_raw_private_key_ex(testctx, OBJ_nid2sn(ecxnids[tst]),
   6063  1.1.1.2  christos         NULL, &ecxkeydata, 1);
   6064      1.1  christos     if (!TEST_ptr_null(pkey)) {
   6065      1.1  christos         EVP_PKEY_free(pkey);
   6066      1.1  christos         return 0;
   6067      1.1  christos     }
   6068      1.1  christos 
   6069      1.1  christos     return 1;
   6070      1.1  christos }
   6071      1.1  christos #endif
   6072      1.1  christos 
   6073      1.1  christos typedef enum OPTION_choice {
   6074      1.1  christos     OPT_ERR = -1,
   6075      1.1  christos     OPT_EOF = 0,
   6076      1.1  christos     OPT_CONTEXT,
   6077      1.1  christos     OPT_CONFIG_FILE,
   6078      1.1  christos     OPT_TEST_ENUM
   6079      1.1  christos } OPTION_CHOICE;
   6080      1.1  christos 
   6081      1.1  christos const OPTIONS *test_get_options(void)
   6082      1.1  christos {
   6083      1.1  christos     static const OPTIONS options[] = {
   6084      1.1  christos         OPT_TEST_OPTIONS_DEFAULT_USAGE,
   6085      1.1  christos         { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
   6086      1.1  christos         { "config", OPT_CONFIG_FILE, '<',
   6087  1.1.1.2  christos             "The configuration file to use for the libctx" },
   6088      1.1  christos         { NULL }
   6089      1.1  christos     };
   6090      1.1  christos     return options;
   6091      1.1  christos }
   6092      1.1  christos 
   6093      1.1  christos #ifndef OPENSSL_NO_ECX
   6094      1.1  christos /* Test that trying to sign with a public key errors out gracefully */
   6095      1.1  christos static int test_ecx_not_private_key(int tst)
   6096      1.1  christos {
   6097      1.1  christos     EVP_PKEY *pkey = NULL;
   6098      1.1  christos 
   6099      1.1  christos     const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
   6100      1.1  christos     int testresult = 0;
   6101      1.1  christos     EVP_MD_CTX *ctx = NULL;
   6102      1.1  christos     unsigned char *mac = NULL;
   6103      1.1  christos     size_t maclen = 0;
   6104      1.1  christos     const uint8_t *pubkey;
   6105      1.1  christos     size_t pubkeylen;
   6106      1.1  christos 
   6107      1.1  christos     switch (keys[tst].type) {
   6108      1.1  christos     case NID_X25519:
   6109      1.1  christos     case NID_X448:
   6110      1.1  christos     case NID_undef:
   6111      1.1  christos         return TEST_skip("signing not supported for X25519/X448/ML-KEM");
   6112      1.1  christos     }
   6113      1.1  christos 
   6114      1.1  christos     /* Check if this algorithm supports public keys */
   6115      1.1  christos     if (keys[tst].pub == NULL)
   6116      1.1  christos         return TEST_skip("no public key present");
   6117      1.1  christos 
   6118      1.1  christos     pubkey = keys[tst].pub;
   6119      1.1  christos     pubkeylen = keys[tst].publen;
   6120      1.1  christos 
   6121      1.1  christos     pkey = EVP_PKEY_new_raw_public_key_ex(testctx, OBJ_nid2sn(keys[tst].type),
   6122  1.1.1.2  christos         NULL, pubkey, pubkeylen);
   6123      1.1  christos     if (!TEST_ptr(pkey))
   6124      1.1  christos         goto err;
   6125      1.1  christos 
   6126      1.1  christos     if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
   6127      1.1  christos         goto err;
   6128      1.1  christos 
   6129      1.1  christos     if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1)
   6130      1.1  christos         goto check_err;
   6131      1.1  christos 
   6132      1.1  christos     if (EVP_DigestSign(ctx, NULL, &maclen, msg, sizeof(msg)) != 1)
   6133      1.1  christos         goto check_err;
   6134      1.1  christos 
   6135      1.1  christos     if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
   6136      1.1  christos         goto err;
   6137      1.1  christos 
   6138      1.1  christos     if (!TEST_int_eq(EVP_DigestSign(ctx, mac, &maclen, msg, sizeof(msg)), 0))
   6139      1.1  christos         goto err;
   6140      1.1  christos 
   6141  1.1.1.2  christos check_err:
   6142      1.1  christos     /*
   6143      1.1  christos      * Currently only EVP_DigestSign will throw PROV_R_NOT_A_PRIVATE_KEY,
   6144      1.1  christos      * but we relax the check to allow error also thrown by
   6145      1.1  christos      * EVP_DigestSignInit and EVP_DigestSign.
   6146      1.1  christos      */
   6147      1.1  christos     if (ERR_GET_REASON(ERR_peek_error()) == PROV_R_NOT_A_PRIVATE_KEY) {
   6148      1.1  christos         testresult = 1;
   6149      1.1  christos         ERR_clear_error();
   6150      1.1  christos     }
   6151      1.1  christos 
   6152  1.1.1.2  christos err:
   6153      1.1  christos     EVP_MD_CTX_free(ctx);
   6154      1.1  christos     OPENSSL_free(mac);
   6155      1.1  christos     EVP_PKEY_free(pkey);
   6156      1.1  christos 
   6157      1.1  christos     return testresult;
   6158      1.1  christos }
   6159      1.1  christos #endif /* OPENSSL_NO_ECX */
   6160      1.1  christos 
   6161      1.1  christos static int test_sign_continuation(void)
   6162      1.1  christos {
   6163      1.1  christos     OSSL_PROVIDER *fake_rsa = NULL;
   6164      1.1  christos     int testresult = 0;
   6165      1.1  christos     EVP_PKEY *pkey = NULL;
   6166      1.1  christos     EVP_PKEY_CTX *pctx = NULL;
   6167      1.1  christos     EVP_MD_CTX *mctx = NULL;
   6168      1.1  christos     const char sigbuf[] = "To Be Signed";
   6169      1.1  christos     unsigned char signature[256];
   6170      1.1  christos     size_t siglen = 256;
   6171      1.1  christos     static int nodupnum = 1;
   6172      1.1  christos     static const OSSL_PARAM nodup_params[] = {
   6173      1.1  christos         OSSL_PARAM_int("NO_DUP", &nodupnum),
   6174      1.1  christos         OSSL_PARAM_END
   6175      1.1  christos     };
   6176      1.1  christos 
   6177      1.1  christos     if (!TEST_ptr(fake_rsa = fake_rsa_start(testctx)))
   6178      1.1  christos         return 0;
   6179      1.1  christos 
   6180      1.1  christos     /* Construct a pkey using precise propq to use our provider */
   6181      1.1  christos     if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA",
   6182  1.1.1.2  christos                       "provider=fake-rsa"))
   6183      1.1  christos         || !TEST_true(EVP_PKEY_fromdata_init(pctx))
   6184      1.1  christos         || !TEST_true(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR, NULL))
   6185      1.1  christos         || !TEST_ptr(pkey))
   6186      1.1  christos         goto end;
   6187      1.1  christos 
   6188      1.1  christos     /* First test it continues (classic behavior) */
   6189      1.1  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
   6190      1.1  christos         || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
   6191  1.1.1.2  christos             NULL, pkey, NULL))
   6192      1.1  christos         || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
   6193      1.1  christos         || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
   6194      1.1  christos         || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
   6195      1.1  christos         || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen)))
   6196      1.1  christos         goto end;
   6197      1.1  christos 
   6198      1.1  christos     EVP_MD_CTX_free(mctx);
   6199      1.1  christos 
   6200      1.1  christos     /* try again but failing the continuation */
   6201      1.1  christos     if (!TEST_ptr(mctx = EVP_MD_CTX_new())
   6202      1.1  christos         || !TEST_true(EVP_DigestSignInit_ex(mctx, NULL, NULL, testctx,
   6203  1.1.1.2  christos             NULL, pkey, nodup_params))
   6204      1.1  christos         || !TEST_true(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
   6205      1.1  christos         || !TEST_true(EVP_DigestSignFinal(mctx, signature, &siglen))
   6206      1.1  christos         || !TEST_false(EVP_DigestSignUpdate(mctx, sigbuf, sizeof(sigbuf)))
   6207      1.1  christos         || !TEST_false(EVP_DigestSignFinal(mctx, signature, &siglen)))
   6208      1.1  christos         goto end;
   6209      1.1  christos 
   6210      1.1  christos     testresult = 1;
   6211      1.1  christos 
   6212      1.1  christos end:
   6213      1.1  christos     EVP_MD_CTX_free(mctx);
   6214      1.1  christos     EVP_PKEY_free(pkey);
   6215      1.1  christos     EVP_PKEY_CTX_free(pctx);
   6216      1.1  christos     fake_rsa_finish(fake_rsa);
   6217      1.1  christos     return testresult;
   6218      1.1  christos }
   6219      1.1  christos 
   6220      1.1  christos static int aes_gcm_encrypt(const unsigned char *gcm_key, size_t gcm_key_s,
   6221  1.1.1.2  christos     const unsigned char *gcm_iv, size_t gcm_ivlen,
   6222  1.1.1.2  christos     const unsigned char *gcm_pt, size_t gcm_pt_s,
   6223  1.1.1.2  christos     const unsigned char *gcm_aad, size_t gcm_aad_s,
   6224  1.1.1.2  christos     const unsigned char *gcm_ct, size_t gcm_ct_s,
   6225  1.1.1.2  christos     const unsigned char *gcm_tag, size_t gcm_tag_s)
   6226      1.1  christos {
   6227      1.1  christos     int ret = 0;
   6228      1.1  christos     EVP_CIPHER_CTX *ctx;
   6229      1.1  christos     EVP_CIPHER *cipher = NULL;
   6230      1.1  christos     int outlen, tmplen;
   6231      1.1  christos     unsigned char outbuf[1024];
   6232      1.1  christos     unsigned char outtag[16];
   6233      1.1  christos     OSSL_PARAM params[2] = {
   6234      1.1  christos         OSSL_PARAM_END, OSSL_PARAM_END
   6235      1.1  christos     };
   6236      1.1  christos 
   6237      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   6238  1.1.1.2  christos         || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")))
   6239      1.1  christos         goto err;
   6240      1.1  christos 
   6241      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
   6242  1.1.1.2  christos         &gcm_ivlen);
   6243      1.1  christos 
   6244      1.1  christos     if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
   6245  1.1.1.2  christos         || (gcm_aad != NULL
   6246  1.1.1.2  christos             && !TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen,
   6247  1.1.1.2  christos                 gcm_aad, gcm_aad_s)))
   6248  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
   6249  1.1.1.2  christos             gcm_pt, gcm_pt_s))
   6250  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
   6251      1.1  christos         goto err;
   6252      1.1  christos 
   6253      1.1  christos     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
   6254  1.1.1.2  christos         outtag, sizeof(outtag));
   6255      1.1  christos 
   6256      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params))
   6257  1.1.1.2  christos         || !TEST_mem_eq(outbuf, outlen, gcm_ct, gcm_ct_s)
   6258  1.1.1.2  christos         || !TEST_mem_eq(outtag, gcm_tag_s, gcm_tag, gcm_tag_s))
   6259      1.1  christos         goto err;
   6260      1.1  christos 
   6261      1.1  christos     ret = 1;
   6262      1.1  christos err:
   6263      1.1  christos     EVP_CIPHER_free(cipher);
   6264      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6265      1.1  christos 
   6266      1.1  christos     return ret;
   6267      1.1  christos }
   6268      1.1  christos 
   6269      1.1  christos static int aes_gcm_decrypt(const unsigned char *gcm_key, size_t gcm_key_s,
   6270  1.1.1.2  christos     const unsigned char *gcm_iv, size_t gcm_ivlen,
   6271  1.1.1.2  christos     const unsigned char *gcm_pt, size_t gcm_pt_s,
   6272  1.1.1.2  christos     const unsigned char *gcm_aad, size_t gcm_aad_s,
   6273  1.1.1.2  christos     const unsigned char *gcm_ct, size_t gcm_ct_s,
   6274  1.1.1.2  christos     const unsigned char *gcm_tag, size_t gcm_tag_s)
   6275      1.1  christos {
   6276      1.1  christos     int ret = 0;
   6277      1.1  christos     EVP_CIPHER_CTX *ctx;
   6278      1.1  christos     EVP_CIPHER *cipher = NULL;
   6279      1.1  christos     int outlen;
   6280      1.1  christos     unsigned char outbuf[1024];
   6281      1.1  christos     OSSL_PARAM params[2] = {
   6282      1.1  christos         OSSL_PARAM_END, OSSL_PARAM_END
   6283      1.1  christos     };
   6284      1.1  christos 
   6285      1.1  christos     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
   6286      1.1  christos         goto err;
   6287      1.1  christos 
   6288      1.1  christos     if ((cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM", "")) == NULL)
   6289      1.1  christos         goto err;
   6290      1.1  christos 
   6291      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN,
   6292  1.1.1.2  christos         &gcm_ivlen);
   6293      1.1  christos 
   6294      1.1  christos     if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, gcm_key, gcm_iv, params))
   6295  1.1.1.2  christos         || (gcm_aad != NULL
   6296  1.1.1.2  christos             && !TEST_true(EVP_DecryptUpdate(ctx, NULL, &outlen,
   6297  1.1.1.2  christos                 gcm_aad, gcm_aad_s)))
   6298  1.1.1.2  christos         || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
   6299  1.1.1.2  christos             gcm_ct, gcm_ct_s))
   6300  1.1.1.2  christos         || !TEST_mem_eq(outbuf, outlen, gcm_pt, gcm_pt_s))
   6301      1.1  christos         goto err;
   6302      1.1  christos 
   6303      1.1  christos     params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
   6304  1.1.1.2  christos         (void *)gcm_tag, gcm_tag_s);
   6305      1.1  christos 
   6306      1.1  christos     if (!TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))
   6307  1.1.1.2  christos         || !TEST_true(EVP_DecryptFinal_ex(ctx, outbuf, &outlen)))
   6308      1.1  christos         goto err;
   6309      1.1  christos 
   6310      1.1  christos     ret = 1;
   6311      1.1  christos err:
   6312      1.1  christos     EVP_CIPHER_free(cipher);
   6313      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6314      1.1  christos 
   6315      1.1  christos     return ret;
   6316      1.1  christos }
   6317      1.1  christos 
   6318      1.1  christos static int test_aes_gcm_ivlen_change_cve_2023_5363(void)
   6319      1.1  christos {
   6320      1.1  christos     /* AES-GCM test data obtained from NIST public test vectors */
   6321      1.1  christos     static const unsigned char gcm_key[] = {
   6322      1.1  christos         0xd0, 0xc2, 0x67, 0xc1, 0x9f, 0x30, 0xd8, 0x0b, 0x89, 0x14, 0xbb, 0xbf,
   6323      1.1  christos         0xb7, 0x2f, 0x73, 0xb8, 0xd3, 0xcd, 0x5f, 0x6a, 0x78, 0x70, 0x15, 0x84,
   6324  1.1.1.2  christos         0x8a, 0x7b, 0x30, 0xe3, 0x8f, 0x16, 0xf1, 0x8b
   6325      1.1  christos     };
   6326      1.1  christos     static const unsigned char gcm_iv[] = {
   6327      1.1  christos         0xb6, 0xdc, 0xda, 0x95, 0xac, 0x99, 0x77, 0x76, 0x25, 0xae, 0x87, 0xf8,
   6328      1.1  christos         0xa3, 0xa9, 0xdd, 0x64, 0xd7, 0x9b, 0xbd, 0x5f, 0x4a, 0x0e, 0x54, 0xca,
   6329      1.1  christos         0x1a, 0x9f, 0xa2, 0xe3, 0xf4, 0x5f, 0x5f, 0xc2, 0xce, 0xa7, 0xb6, 0x14,
   6330      1.1  christos         0x12, 0x6f, 0xf0, 0xaf, 0xfd, 0x3e, 0x17, 0x35, 0x6e, 0xa0, 0x16, 0x09,
   6331      1.1  christos         0xdd, 0xa1, 0x3f, 0xd8, 0xdd, 0xf3, 0xdf, 0x4f, 0xcb, 0x18, 0x49, 0xb8,
   6332      1.1  christos         0xb3, 0x69, 0x2c, 0x5d, 0x4f, 0xad, 0x30, 0x91, 0x08, 0xbc, 0xbe, 0x24,
   6333      1.1  christos         0x01, 0x0f, 0xbe, 0x9c, 0xfb, 0x4f, 0x5d, 0x19, 0x7f, 0x4c, 0x53, 0xb0,
   6334      1.1  christos         0x95, 0x90, 0xac, 0x7b, 0x1f, 0x7b, 0xa0, 0x99, 0xe1, 0xf3, 0x48, 0x54,
   6335      1.1  christos         0xd0, 0xfc, 0xa9, 0xcc, 0x91, 0xf8, 0x1f, 0x9b, 0x6c, 0x9a, 0xe0, 0xdc,
   6336      1.1  christos         0x63, 0xea, 0x7d, 0x2a, 0x4a, 0x7d, 0xa5, 0xed, 0x68, 0x57, 0x27, 0x6b,
   6337  1.1.1.2  christos         0x68, 0xe0, 0xf2, 0xb8, 0x51, 0x50, 0x8d, 0x3d
   6338      1.1  christos     };
   6339      1.1  christos     static const unsigned char gcm_pt[] = {
   6340      1.1  christos         0xb8, 0xb6, 0x88, 0x36, 0x44, 0xe2, 0x34, 0xdf, 0x24, 0x32, 0x91, 0x07,
   6341  1.1.1.2  christos         0x4f, 0xe3, 0x6f, 0x81
   6342      1.1  christos     };
   6343      1.1  christos     static const unsigned char gcm_ct[] = {
   6344      1.1  christos         0xff, 0x4f, 0xb3, 0xf3, 0xf9, 0xa2, 0x51, 0xd4, 0x82, 0xc2, 0xbe, 0xf3,
   6345  1.1.1.2  christos         0xe2, 0xd0, 0xec, 0xed
   6346      1.1  christos     };
   6347      1.1  christos     static const unsigned char gcm_tag[] = {
   6348      1.1  christos         0xbd, 0x06, 0x38, 0x09, 0xf7, 0xe1, 0xc4, 0x72, 0x0e, 0xf2, 0xea, 0x63,
   6349  1.1.1.2  christos         0xdb, 0x99, 0x6c, 0x21
   6350      1.1  christos     };
   6351      1.1  christos 
   6352      1.1  christos     return aes_gcm_encrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
   6353  1.1.1.2  christos                gcm_pt, sizeof(gcm_pt), NULL, 0,
   6354  1.1.1.2  christos                gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag))
   6355      1.1  christos         && aes_gcm_decrypt(gcm_key, sizeof(gcm_key), gcm_iv, sizeof(gcm_iv),
   6356  1.1.1.2  christos             gcm_pt, sizeof(gcm_pt), NULL, 0,
   6357  1.1.1.2  christos             gcm_ct, sizeof(gcm_ct), gcm_tag, sizeof(gcm_tag));
   6358      1.1  christos }
   6359      1.1  christos 
   6360      1.1  christos #ifndef OPENSSL_NO_RC4
   6361      1.1  christos static int rc4_encrypt(const unsigned char *rc4_key, size_t rc4_key_s,
   6362  1.1.1.2  christos     const unsigned char *rc4_pt, size_t rc4_pt_s,
   6363  1.1.1.2  christos     const unsigned char *rc4_ct, size_t rc4_ct_s)
   6364      1.1  christos {
   6365      1.1  christos     int ret = 0;
   6366      1.1  christos     EVP_CIPHER_CTX *ctx;
   6367      1.1  christos     EVP_CIPHER *cipher = NULL;
   6368      1.1  christos     int outlen, tmplen;
   6369      1.1  christos     unsigned char outbuf[1024];
   6370      1.1  christos     OSSL_PARAM params[2] = {
   6371      1.1  christos         OSSL_PARAM_END, OSSL_PARAM_END
   6372      1.1  christos     };
   6373      1.1  christos 
   6374      1.1  christos     if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
   6375  1.1.1.2  christos         || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "RC4", "")))
   6376      1.1  christos         goto err;
   6377      1.1  christos 
   6378      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
   6379  1.1.1.2  christos         &rc4_key_s);
   6380      1.1  christos 
   6381      1.1  christos     if (!TEST_true(EVP_EncryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
   6382  1.1.1.2  christos         || !TEST_true(EVP_EncryptUpdate(ctx, outbuf, &outlen,
   6383  1.1.1.2  christos             rc4_pt, rc4_pt_s))
   6384  1.1.1.2  christos         || !TEST_true(EVP_EncryptFinal_ex(ctx, outbuf, &tmplen)))
   6385      1.1  christos         goto err;
   6386      1.1  christos 
   6387      1.1  christos     if (!TEST_mem_eq(outbuf, outlen, rc4_ct, rc4_ct_s))
   6388      1.1  christos         goto err;
   6389      1.1  christos 
   6390      1.1  christos     ret = 1;
   6391      1.1  christos err:
   6392      1.1  christos     EVP_CIPHER_free(cipher);
   6393      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6394      1.1  christos 
   6395      1.1  christos     return ret;
   6396      1.1  christos }
   6397      1.1  christos 
   6398      1.1  christos static int rc4_decrypt(const unsigned char *rc4_key, size_t rc4_key_s,
   6399  1.1.1.2  christos     const unsigned char *rc4_pt, size_t rc4_pt_s,
   6400  1.1.1.2  christos     const unsigned char *rc4_ct, size_t rc4_ct_s)
   6401      1.1  christos {
   6402      1.1  christos     int ret = 0;
   6403      1.1  christos     EVP_CIPHER_CTX *ctx;
   6404      1.1  christos     EVP_CIPHER *cipher = NULL;
   6405      1.1  christos     int outlen;
   6406      1.1  christos     unsigned char outbuf[1024];
   6407      1.1  christos     OSSL_PARAM params[2] = {
   6408      1.1  christos         OSSL_PARAM_END, OSSL_PARAM_END
   6409      1.1  christos     };
   6410      1.1  christos 
   6411      1.1  christos     if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
   6412      1.1  christos         goto err;
   6413      1.1  christos 
   6414      1.1  christos     if ((cipher = EVP_CIPHER_fetch(testctx, "RC4", "")) == NULL)
   6415      1.1  christos         goto err;
   6416      1.1  christos 
   6417      1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN,
   6418  1.1.1.2  christos         &rc4_key_s);
   6419      1.1  christos 
   6420      1.1  christos     if (!TEST_true(EVP_DecryptInit_ex2(ctx, cipher, rc4_key, NULL, params))
   6421  1.1.1.2  christos         || !TEST_true(EVP_DecryptUpdate(ctx, outbuf, &outlen,
   6422  1.1.1.2  christos             rc4_ct, rc4_ct_s))
   6423  1.1.1.2  christos         || !TEST_mem_eq(outbuf, outlen, rc4_pt, rc4_pt_s))
   6424      1.1  christos         goto err;
   6425      1.1  christos 
   6426      1.1  christos     ret = 1;
   6427      1.1  christos err:
   6428      1.1  christos     EVP_CIPHER_free(cipher);
   6429      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6430      1.1  christos 
   6431      1.1  christos     return ret;
   6432      1.1  christos }
   6433      1.1  christos 
   6434      1.1  christos static int test_aes_rc4_keylen_change_cve_2023_5363(void)
   6435      1.1  christos {
   6436      1.1  christos     /* RC4 test data obtained from RFC 6229 */
   6437      1.1  christos     static const struct {
   6438      1.1  christos         unsigned char key[5];
   6439      1.1  christos         unsigned char padding[11];
   6440      1.1  christos     } rc4_key = {
   6441  1.1.1.2  christos         { /* Five bytes of key material */
   6442  1.1.1.2  christos             0x83, 0x32, 0x22, 0x77, 0x2a },
   6443  1.1.1.2  christos         { /* Random padding to 16 bytes */
   6444  1.1.1.2  christos             0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0xaa, 0x32, 0x91 }
   6445      1.1  christos     };
   6446      1.1  christos     static const unsigned char rc4_pt[] = {
   6447      1.1  christos         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   6448      1.1  christos         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   6449      1.1  christos     };
   6450      1.1  christos     static const unsigned char rc4_ct[] = {
   6451      1.1  christos         0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a,
   6452      1.1  christos         0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda
   6453      1.1  christos     };
   6454      1.1  christos 
   6455      1.1  christos     if (lgcyprov == NULL)
   6456      1.1  christos         return TEST_skip("Test requires legacy provider to be loaded");
   6457      1.1  christos 
   6458      1.1  christos     return rc4_encrypt(rc4_key.key, sizeof(rc4_key.key),
   6459  1.1.1.2  christos                rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct))
   6460      1.1  christos         && rc4_decrypt(rc4_key.key, sizeof(rc4_key.key),
   6461  1.1.1.2  christos             rc4_pt, sizeof(rc4_pt), rc4_ct, sizeof(rc4_ct));
   6462      1.1  christos }
   6463      1.1  christos #endif
   6464      1.1  christos 
   6465      1.1  christos static int test_invalid_ctx_for_digest(void)
   6466      1.1  christos {
   6467      1.1  christos     int ret;
   6468      1.1  christos     EVP_MD_CTX *mdctx;
   6469      1.1  christos 
   6470      1.1  christos     mdctx = EVP_MD_CTX_new();
   6471      1.1  christos     if (!TEST_ptr(mdctx))
   6472      1.1  christos         return 0;
   6473      1.1  christos 
   6474      1.1  christos     if (!TEST_int_eq(EVP_DigestUpdate(mdctx, "test", sizeof("test") - 1), 0))
   6475      1.1  christos         ret = 0;
   6476      1.1  christos     else
   6477      1.1  christos         ret = 1;
   6478      1.1  christos 
   6479      1.1  christos     EVP_MD_CTX_free(mdctx);
   6480      1.1  christos 
   6481      1.1  christos     return ret;
   6482      1.1  christos }
   6483      1.1  christos 
   6484      1.1  christos static int test_evp_cipher_pipeline(void)
   6485      1.1  christos {
   6486      1.1  christos     OSSL_PROVIDER *fake_pipeline = NULL;
   6487      1.1  christos     int testresult = 0;
   6488      1.1  christos     EVP_CIPHER *cipher = NULL;
   6489      1.1  christos     EVP_CIPHER *pipeline_cipher = NULL;
   6490      1.1  christos     EVP_CIPHER_CTX *ctx = NULL;
   6491      1.1  christos     unsigned char key[32];
   6492      1.1  christos     size_t keylen = 32;
   6493      1.1  christos     size_t ivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN + EVP_GCM_TLS_FIXED_IV_LEN;
   6494      1.1  christos     size_t taglen = EVP_GCM_TLS_TAG_LEN;
   6495      1.1  christos     unsigned char *iv_array[EVP_MAX_PIPES], *tag_array[EVP_MAX_PIPES];
   6496      1.1  christos     unsigned char *plaintext_array[EVP_MAX_PIPES];
   6497      1.1  christos     unsigned char *ciphertext_array_p[EVP_MAX_PIPES];
   6498      1.1  christos     void **aead_tags = (void **)&tag_array;
   6499      1.1  christos     unsigned char *temp[EVP_MAX_PIPES];
   6500      1.1  christos     size_t outsize_array[EVP_MAX_PIPES], outlen_array[EVP_MAX_PIPES];
   6501      1.1  christos     size_t ciphertextlen_array[EVP_MAX_PIPES];
   6502      1.1  christos     size_t inlen_array[EVP_MAX_PIPES];
   6503      1.1  christos     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
   6504      1.1  christos     unsigned char *ciphertext, *exp_plaintext, *tag;
   6505      1.1  christos     size_t numpipes, plaintextlen, i;
   6506      1.1  christos 
   6507      1.1  christos     if (!TEST_ptr(fake_pipeline = fake_pipeline_start(testctx)))
   6508      1.1  christos         return 0;
   6509      1.1  christos     if (!TEST_ptr(pipeline_cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM",
   6510  1.1.1.2  christos                       "provider=fake-pipeline"))
   6511      1.1  christos         || !TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "AES-256-GCM",
   6512  1.1.1.2  christos                          "provider!=fake-pipeline"))
   6513      1.1  christos         || !TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
   6514      1.1  christos         goto end;
   6515      1.1  christos     memset(key, 0x01, sizeof(key));
   6516      1.1  christos 
   6517      1.1  christos     /* Negative tests */
   6518      1.1  christos     if (!TEST_false(EVP_CIPHER_can_pipeline(cipher, 1)))
   6519      1.1  christos         goto end;
   6520      1.1  christos     if (!TEST_false(EVP_CIPHER_can_pipeline(EVP_aes_256_gcm(), 1)))
   6521      1.1  christos         goto end;
   6522      1.1  christos     if (!TEST_false(EVP_CipherPipelineEncryptInit(ctx, pipeline_cipher,
   6523  1.1.1.2  christos             key, keylen,
   6524  1.1.1.2  christos             EVP_MAX_PIPES + 1, NULL, 0)))
   6525      1.1  christos         goto end;
   6526      1.1  christos 
   6527      1.1  christos     /* Positive tests */
   6528      1.1  christos     for (numpipes = 1; numpipes <= EVP_MAX_PIPES; numpipes++) {
   6529      1.1  christos         for (plaintextlen = 1; plaintextlen <= 256; plaintextlen++) {
   6530      1.1  christos             size_t ciphertextlen = 0;
   6531      1.1  christos             int outlen = 0;
   6532      1.1  christos 
   6533      1.1  christos             /* Cleanup to be able to error out */
   6534      1.1  christos             memset(iv_array, 0, sizeof(iv_array));
   6535      1.1  christos             memset(plaintext_array, 0, sizeof(plaintext_array));
   6536      1.1  christos             memset(ciphertext_array_p, 0, sizeof(ciphertext_array_p));
   6537      1.1  christos             memset(tag_array, 0, sizeof(tag_array));
   6538      1.1  christos             ciphertext = NULL;
   6539      1.1  christos             exp_plaintext = NULL;
   6540      1.1  christos             tag = NULL;
   6541      1.1  christos 
   6542      1.1  christos             /* Allocate fresh buffers with exact size to catch buffer overwrites */
   6543      1.1  christos             for (i = 0; i < numpipes; i++) {
   6544      1.1  christos                 if (!TEST_ptr(iv_array[i] = OPENSSL_malloc(ivlen))
   6545      1.1  christos                     || !TEST_ptr(plaintext_array[i] = OPENSSL_malloc(plaintextlen))
   6546  1.1.1.2  christos                     || !TEST_ptr(ciphertext_array_p[i] = OPENSSL_malloc(plaintextlen + EVP_MAX_BLOCK_LENGTH))
   6547      1.1  christos                     || !TEST_ptr(tag_array[i] = OPENSSL_malloc(taglen)))
   6548      1.1  christos                     goto err;
   6549      1.1  christos 
   6550      1.1  christos                 memset(iv_array[i], i + 33, ivlen);
   6551      1.1  christos                 memset(plaintext_array[i], i + 1, plaintextlen);
   6552      1.1  christos                 inlen_array[i] = plaintextlen;
   6553      1.1  christos                 outlen_array[i] = 0;
   6554      1.1  christos                 ciphertextlen_array[i] = 0;
   6555      1.1  christos                 outsize_array[i] = plaintextlen + EVP_MAX_BLOCK_LENGTH;
   6556      1.1  christos             }
   6557  1.1.1.2  christos             if (!TEST_ptr(ciphertext = OPENSSL_malloc(plaintextlen + EVP_MAX_BLOCK_LENGTH))
   6558      1.1  christos                 || !TEST_ptr(tag = OPENSSL_malloc(taglen))
   6559      1.1  christos                 || !TEST_ptr(exp_plaintext = OPENSSL_malloc(plaintextlen)))
   6560      1.1  christos                 goto err;
   6561      1.1  christos 
   6562      1.1  christos             /* Encrypt using pipeline API */
   6563      1.1  christos             if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
   6564      1.1  christos                 || !TEST_true(EVP_CIPHER_can_pipeline(pipeline_cipher, 1))
   6565      1.1  christos                 || !TEST_true(EVP_CipherPipelineEncryptInit(ctx, pipeline_cipher,
   6566  1.1.1.2  christos                     key, keylen, numpipes,
   6567  1.1.1.2  christos                     (const unsigned char **)iv_array,
   6568  1.1.1.2  christos                     ivlen))
   6569      1.1  christos                 /* reuse plaintext for AAD as it won't affect test */
   6570      1.1  christos                 || !TEST_true(EVP_CipherPipelineUpdate(ctx, NULL, outlen_array, NULL,
   6571  1.1.1.2  christos                     (const unsigned char **)plaintext_array,
   6572  1.1.1.2  christos                     inlen_array))
   6573      1.1  christos                 || !TEST_true(EVP_CipherPipelineUpdate(ctx, ciphertext_array_p,
   6574  1.1.1.2  christos                     outlen_array, outsize_array,
   6575  1.1.1.2  christos                     (const unsigned char **)plaintext_array,
   6576  1.1.1.2  christos                     inlen_array)))
   6577      1.1  christos                 goto err;
   6578      1.1  christos 
   6579      1.1  christos             for (i = 0; i < numpipes; i++) {
   6580      1.1  christos                 ciphertextlen_array[i] = outlen_array[i];
   6581      1.1  christos                 temp[i] = ciphertext_array_p[i] + ciphertextlen_array[i];
   6582      1.1  christos                 outsize_array[i] = outsize_array[i] - ciphertextlen_array[i];
   6583      1.1  christos             }
   6584      1.1  christos 
   6585      1.1  christos             if (!TEST_true(EVP_CipherPipelineFinal(ctx, temp, outlen_array, outsize_array)))
   6586      1.1  christos                 goto err;
   6587      1.1  christos 
   6588      1.1  christos             for (i = 0; i < numpipes; i++)
   6589      1.1  christos                 ciphertextlen_array[i] += outlen_array[i];
   6590      1.1  christos 
   6591      1.1  christos             params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG,
   6592  1.1.1.2  christos                 (void **)&aead_tags, taglen);
   6593      1.1  christos             if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params)))
   6594      1.1  christos                 goto err;
   6595      1.1  christos 
   6596      1.1  christos             /* Encrypt using non-pipeline API and compare */
   6597      1.1  christos             if (!TEST_true(EVP_CIPHER_CTX_reset(ctx)))
   6598      1.1  christos                 goto err;
   6599      1.1  christos 
   6600      1.1  christos             for (i = 0; i < numpipes; i++) {
   6601      1.1  christos                 if (!TEST_true(EVP_EncryptInit(ctx, cipher, key, iv_array[i]))
   6602      1.1  christos                     || !TEST_true(EVP_EncryptUpdate(ctx, NULL, &outlen,
   6603  1.1.1.2  christos                         plaintext_array[i],
   6604  1.1.1.2  christos                         plaintextlen))
   6605      1.1  christos                     || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &outlen,
   6606  1.1.1.2  christos                         plaintext_array[i],
   6607  1.1.1.2  christos                         plaintextlen)))
   6608      1.1  christos                     goto err;
   6609      1.1  christos                 ciphertextlen = outlen;
   6610      1.1  christos 
   6611      1.1  christos                 if (!TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext + outlen, &outlen)))
   6612      1.1  christos                     goto err;
   6613      1.1  christos                 ciphertextlen += outlen;
   6614      1.1  christos 
   6615      1.1  christos                 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
   6616  1.1.1.2  christos                     (void *)tag, taglen);
   6617      1.1  christos                 if (!TEST_true(EVP_CIPHER_CTX_get_params(ctx, params)))
   6618      1.1  christos                     goto err;
   6619      1.1  christos 
   6620      1.1  christos                 if (!TEST_mem_eq(ciphertext_array_p[i], ciphertextlen_array[i],
   6621  1.1.1.2  christos                         ciphertext, ciphertextlen)
   6622      1.1  christos                     || !TEST_mem_eq(tag_array[i], taglen, tag, taglen))
   6623      1.1  christos                     goto err;
   6624      1.1  christos             }
   6625      1.1  christos 
   6626      1.1  christos             for (i = 0; i < numpipes; i++)
   6627      1.1  christos                 outsize_array[i] = plaintextlen;
   6628      1.1  christos 
   6629      1.1  christos             /* Decrypt using pipeline API and compare */
   6630      1.1  christos             params[0] = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_PIPELINE_AEAD_TAG,
   6631  1.1.1.2  christos                 (void **)&aead_tags, taglen);
   6632      1.1  christos             if (!TEST_true(EVP_CIPHER_CTX_reset(ctx))
   6633      1.1  christos                 || !TEST_true(EVP_CIPHER_can_pipeline(pipeline_cipher, 0))
   6634      1.1  christos                 || !TEST_true(EVP_CipherPipelineDecryptInit(ctx, pipeline_cipher,
   6635  1.1.1.2  christos                     key, keylen, numpipes,
   6636  1.1.1.2  christos                     (const unsigned char **)iv_array,
   6637  1.1.1.2  christos                     ivlen))
   6638      1.1  christos                 || !TEST_true(EVP_CIPHER_CTX_set_params(ctx, params))
   6639      1.1  christos                 || !TEST_true(EVP_CipherPipelineUpdate(ctx, NULL, outlen_array, NULL,
   6640  1.1.1.2  christos                     (const unsigned char **)plaintext_array,
   6641  1.1.1.2  christos                     inlen_array))
   6642      1.1  christos                 || !TEST_true(EVP_CipherPipelineUpdate(ctx, plaintext_array,
   6643  1.1.1.2  christos                     outlen_array, outsize_array,
   6644  1.1.1.2  christos                     (const unsigned char **)ciphertext_array_p,
   6645  1.1.1.2  christos                     ciphertextlen_array)))
   6646      1.1  christos                 goto err;
   6647      1.1  christos 
   6648      1.1  christos             for (i = 0; i < numpipes; i++) {
   6649      1.1  christos                 temp[i] = plaintext_array[i] + outlen_array[i];
   6650      1.1  christos                 outsize_array[i] = outsize_array[i] - outlen_array[i];
   6651      1.1  christos             }
   6652      1.1  christos 
   6653      1.1  christos             if (!TEST_true(EVP_CipherPipelineFinal(ctx, temp, outlen_array, outsize_array)))
   6654      1.1  christos                 goto err;
   6655      1.1  christos 
   6656      1.1  christos             for (i = 0; i < numpipes; i++) {
   6657      1.1  christos                 memset(exp_plaintext, i + 1, plaintextlen);
   6658      1.1  christos                 if (!TEST_mem_eq(plaintext_array[i], plaintextlen,
   6659  1.1.1.2  christos                         exp_plaintext, plaintextlen))
   6660      1.1  christos                     goto err;
   6661      1.1  christos             }
   6662      1.1  christos 
   6663      1.1  christos             for (i = 0; i < numpipes; i++) {
   6664      1.1  christos                 OPENSSL_free(iv_array[i]);
   6665      1.1  christos                 OPENSSL_free(plaintext_array[i]);
   6666      1.1  christos                 OPENSSL_free(ciphertext_array_p[i]);
   6667      1.1  christos                 OPENSSL_free(tag_array[i]);
   6668      1.1  christos             }
   6669      1.1  christos             OPENSSL_free(exp_plaintext);
   6670      1.1  christos             OPENSSL_free(ciphertext);
   6671      1.1  christos             OPENSSL_free(tag);
   6672      1.1  christos         }
   6673      1.1  christos     }
   6674      1.1  christos 
   6675      1.1  christos     testresult = 1;
   6676      1.1  christos     goto end;
   6677      1.1  christos 
   6678      1.1  christos err:
   6679      1.1  christos     for (i = 0; i < numpipes; i++) {
   6680      1.1  christos         OPENSSL_free(iv_array[i]);
   6681      1.1  christos         OPENSSL_free(plaintext_array[i]);
   6682      1.1  christos         OPENSSL_free(ciphertext_array_p[i]);
   6683      1.1  christos         OPENSSL_free(tag_array[i]);
   6684      1.1  christos     }
   6685      1.1  christos     OPENSSL_free(exp_plaintext);
   6686      1.1  christos     OPENSSL_free(ciphertext);
   6687      1.1  christos     OPENSSL_free(tag);
   6688      1.1  christos end:
   6689      1.1  christos     EVP_CIPHER_CTX_free(ctx);
   6690      1.1  christos     EVP_CIPHER_free(cipher);
   6691      1.1  christos     EVP_CIPHER_free(pipeline_cipher);
   6692      1.1  christos     fake_pipeline_finish(fake_pipeline);
   6693      1.1  christos     return testresult;
   6694      1.1  christos }
   6695      1.1  christos 
   6696      1.1  christos int setup_tests(void)
   6697      1.1  christos {
   6698      1.1  christos     char *config_file = NULL;
   6699      1.1  christos     OPTION_CHOICE o;
   6700      1.1  christos 
   6701      1.1  christos     while ((o = opt_next()) != OPT_EOF) {
   6702      1.1  christos         switch (o) {
   6703      1.1  christos         case OPT_CONTEXT:
   6704      1.1  christos             /* Set up an alternate library context */
   6705      1.1  christos             testctx = OSSL_LIB_CTX_new();
   6706      1.1  christos             if (!TEST_ptr(testctx))
   6707      1.1  christos                 return 0;
   6708      1.1  christos #ifdef STATIC_LEGACY
   6709      1.1  christos             /*
   6710      1.1  christos              * This test is always statically linked against libcrypto. We must not
   6711      1.1  christos              * attempt to load legacy.so that might be dynamically linked against
   6712      1.1  christos              * libcrypto. Instead we use a built-in version of the legacy provider.
   6713      1.1  christos              */
   6714      1.1  christos             if (!OSSL_PROVIDER_add_builtin(testctx, "legacy", ossl_legacy_provider_init))
   6715      1.1  christos                 return 0;
   6716      1.1  christos #endif
   6717      1.1  christos             /* Swap the libctx to test non-default context only */
   6718      1.1  christos             nullprov = OSSL_PROVIDER_load(NULL, "null");
   6719      1.1  christos             deflprov = OSSL_PROVIDER_load(testctx, "default");
   6720      1.1  christos #ifndef OPENSSL_SYS_TANDEM
   6721      1.1  christos             lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
   6722      1.1  christos #endif
   6723      1.1  christos             break;
   6724      1.1  christos         case OPT_CONFIG_FILE:
   6725      1.1  christos             config_file = opt_arg();
   6726      1.1  christos             if (!test_get_libctx(&testctx, &nullprov, config_file, NULL, NULL))
   6727      1.1  christos                 return 0;
   6728      1.1  christos             break;
   6729      1.1  christos         case OPT_TEST_CASES:
   6730      1.1  christos             break;
   6731      1.1  christos         default:
   6732      1.1  christos             return 0;
   6733      1.1  christos         }
   6734      1.1  christos     }
   6735      1.1  christos 
   6736      1.1  christos     if (config_file != NULL) {
   6737      1.1  christos         ADD_TEST(test_EVP_set_config_properties);
   6738      1.1  christos         return 1;
   6739      1.1  christos     }
   6740      1.1  christos 
   6741      1.1  christos     ADD_TEST(test_EVP_set_default_properties);
   6742      1.1  christos     ADD_ALL_TESTS(test_EVP_DigestSignInit, 30);
   6743      1.1  christos     ADD_TEST(test_EVP_DigestVerifyInit);
   6744      1.1  christos #ifndef OPENSSL_NO_EC
   6745      1.1  christos     ADD_TEST(test_ecdsa_digestsign_keccak);
   6746      1.1  christos #endif
   6747      1.1  christos #ifndef OPENSSL_NO_SIPHASH
   6748      1.1  christos     ADD_TEST(test_siphash_digestsign);
   6749      1.1  christos #endif
   6750      1.1  christos     ADD_TEST(test_EVP_Digest);
   6751      1.1  christos     ADD_TEST(test_EVP_md_null);
   6752      1.1  christos     ADD_ALL_TESTS(test_EVP_PKEY_sign, 3);
   6753      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6754      1.1  christos     ADD_ALL_TESTS(test_EVP_PKEY_sign_with_app_method, 2);
   6755      1.1  christos #endif
   6756      1.1  christos     ADD_ALL_TESTS(test_EVP_Enveloped, 2);
   6757      1.1  christos     ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
   6758      1.1  christos     ADD_TEST(test_privatekey_to_pkcs8);
   6759      1.1  christos     ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag);
   6760      1.1  christos     ADD_ALL_TESTS(test_EVP_PKCS82PKEY_v2, OSSL_NELEM(keydata_v2));
   6761      1.1  christos #ifndef OPENSSL_NO_EC
   6762      1.1  christos     ADD_TEST(test_EVP_PKCS82PKEY);
   6763      1.1  christos #endif
   6764      1.1  christos #ifndef OPENSSL_NO_EC
   6765      1.1  christos     ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
   6766      1.1  christos #endif
   6767      1.1  christos #if !defined(OPENSSL_NO_SM2)
   6768      1.1  christos     ADD_TEST(test_EVP_SM2);
   6769      1.1  christos     ADD_TEST(test_EVP_SM2_verify);
   6770      1.1  christos #endif
   6771      1.1  christos     ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
   6772      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6773      1.1  christos     custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
   6774      1.1  christos     if (!TEST_ptr(custom_pmeth))
   6775      1.1  christos         return 0;
   6776      1.1  christos     EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
   6777      1.1  christos     EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
   6778      1.1  christos     EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
   6779      1.1  christos     if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
   6780      1.1  christos         return 0;
   6781      1.1  christos #endif
   6782      1.1  christos     ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
   6783      1.1  christos #ifndef OPENSSL_NO_CMAC
   6784      1.1  christos     ADD_TEST(test_CMAC_keygen);
   6785      1.1  christos #endif
   6786      1.1  christos     ADD_TEST(test_HKDF);
   6787      1.1  christos     ADD_TEST(test_emptyikm_HKDF);
   6788      1.1  christos     ADD_TEST(test_empty_salt_info_HKDF);
   6789      1.1  christos #ifndef OPENSSL_NO_EC
   6790      1.1  christos     ADD_TEST(test_X509_PUBKEY_inplace);
   6791      1.1  christos     ADD_TEST(test_X509_PUBKEY_dup);
   6792      1.1  christos     ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
   6793  1.1.1.2  christos         OSSL_NELEM(ec_der_pub_keys));
   6794      1.1  christos #endif
   6795      1.1  christos #ifndef OPENSSL_NO_DSA
   6796      1.1  christos     ADD_TEST(test_DSA_get_set_params);
   6797      1.1  christos     ADD_TEST(test_DSA_priv_pub);
   6798      1.1  christos #endif
   6799      1.1  christos     ADD_TEST(test_RSA_get_set_params);
   6800      1.1  christos     ADD_TEST(test_RSA_OAEP_set_get_params);
   6801      1.1  christos     ADD_TEST(test_RSA_OAEP_set_null_label);
   6802  1.1.1.2  christos     ADD_TEST(test_RSA_encrypt);
   6803      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6804      1.1  christos     ADD_TEST(test_RSA_legacy);
   6805      1.1  christos #endif
   6806      1.1  christos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
   6807      1.1  christos     ADD_TEST(test_decrypt_null_chunks);
   6808      1.1  christos #endif
   6809      1.1  christos #ifndef OPENSSL_NO_DH
   6810      1.1  christos     ADD_TEST(test_DH_priv_pub);
   6811  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6812      1.1  christos     ADD_TEST(test_EVP_PKEY_set1_DH);
   6813  1.1.1.2  christos #endif
   6814      1.1  christos #endif
   6815      1.1  christos #ifndef OPENSSL_NO_EC
   6816      1.1  christos     ADD_TEST(test_EC_priv_pub);
   6817      1.1  christos     ADD_TEST(test_evp_get_ec_pub);
   6818  1.1.1.2  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6819      1.1  christos     ADD_TEST(test_EC_priv_only_legacy);
   6820      1.1  christos     ADD_TEST(test_evp_get_ec_pub_legacy);
   6821  1.1.1.2  christos #endif
   6822      1.1  christos #endif
   6823      1.1  christos     ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
   6824      1.1  christos     ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
   6825      1.1  christos 
   6826      1.1  christos     ADD_TEST(test_rand_agglomeration);
   6827      1.1  christos     ADD_ALL_TESTS(test_evp_iv_aes, 12);
   6828      1.1  christos #ifndef OPENSSL_NO_DES
   6829      1.1  christos     ADD_ALL_TESTS(test_evp_iv_des, 6);
   6830      1.1  christos #endif
   6831      1.1  christos #ifndef OPENSSL_NO_BF
   6832      1.1  christos     ADD_ALL_TESTS(test_evp_bf_default_keylen, 4);
   6833      1.1  christos #endif
   6834      1.1  christos     ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
   6835      1.1  christos     ADD_TEST(test_EVP_rsa_pss_set_saltlen);
   6836      1.1  christos     ADD_TEST(test_EVP_rsa_invalid_key);
   6837      1.1  christos #ifndef OPENSSL_NO_EC
   6838      1.1  christos     ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
   6839      1.1  christos #endif
   6840      1.1  christos 
   6841      1.1  christos     ADD_TEST(test_names_do_all);
   6842      1.1  christos 
   6843      1.1  christos     ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests));
   6844      1.1  christos     ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
   6845      1.1  christos     ADD_ALL_TESTS(test_evp_reinit_seq, OSSL_NELEM(evp_reinit_tests));
   6846      1.1  christos     ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
   6847      1.1  christos     ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
   6848      1.1  christos     ADD_ALL_TESTS(test_ivlen_change, OSSL_NELEM(ivlen_change_ciphers));
   6849      1.1  christos     if (OSSL_NELEM(keylen_change_ciphers) - 1 > 0)
   6850      1.1  christos         ADD_ALL_TESTS(test_keylen_change, OSSL_NELEM(keylen_change_ciphers) - 1);
   6851      1.1  christos 
   6852      1.1  christos #ifndef OPENSSL_NO_DEPRECATED_3_0
   6853      1.1  christos     ADD_ALL_TESTS(test_custom_pmeth, 12);
   6854      1.1  christos     ADD_TEST(test_evp_md_cipher_meth);
   6855      1.1  christos     ADD_TEST(test_custom_md_meth);
   6856      1.1  christos     ADD_TEST(test_custom_ciph_meth);
   6857      1.1  christos 
   6858  1.1.1.2  christos #ifndef OPENSSL_NO_DYNAMIC_ENGINE
   6859      1.1  christos     /* Tests only support the default libctx */
   6860      1.1  christos     if (testctx == NULL) {
   6861  1.1.1.2  christos #ifndef OPENSSL_NO_EC
   6862      1.1  christos         ADD_ALL_TESTS(test_signatures_with_engine, 3);
   6863  1.1.1.2  christos #else
   6864      1.1  christos         ADD_ALL_TESTS(test_signatures_with_engine, 2);
   6865  1.1.1.2  christos #endif
   6866      1.1  christos         ADD_TEST(test_cipher_with_engine);
   6867      1.1  christos     }
   6868  1.1.1.2  christos #endif
   6869      1.1  christos #endif
   6870      1.1  christos 
   6871      1.1  christos #ifndef OPENSSL_NO_ECX
   6872      1.1  christos     ADD_ALL_TESTS(test_ecx_short_keys, OSSL_NELEM(ecxnids));
   6873      1.1  christos     ADD_ALL_TESTS(test_ecx_not_private_key, OSSL_NELEM(keys));
   6874      1.1  christos #endif
   6875      1.1  christos 
   6876      1.1  christos     ADD_TEST(test_sign_continuation);
   6877      1.1  christos 
   6878      1.1  christos     /* Test cases for CVE-2023-5363 */
   6879      1.1  christos     ADD_TEST(test_aes_gcm_ivlen_change_cve_2023_5363);
   6880      1.1  christos #ifndef OPENSSL_NO_RC4
   6881      1.1  christos     ADD_TEST(test_aes_rc4_keylen_change_cve_2023_5363);
   6882      1.1  christos #endif
   6883      1.1  christos 
   6884      1.1  christos     ADD_TEST(test_invalid_ctx_for_digest);
   6885      1.1  christos 
   6886      1.1  christos     ADD_TEST(test_evp_cipher_pipeline);
   6887      1.1  christos 
   6888      1.1  christos     return 1;
   6889      1.1  christos }
   6890      1.1  christos 
   6891      1.1  christos void cleanup_tests(void)
   6892      1.1  christos {
   6893      1.1  christos     OSSL_PROVIDER_unload(nullprov);
   6894      1.1  christos     OSSL_PROVIDER_unload(deflprov);
   6895      1.1  christos #ifndef OPENSSL_SYS_TANDEM
   6896      1.1  christos     OSSL_PROVIDER_unload(lgcyprov);
   6897      1.1  christos #endif
   6898      1.1  christos     OSSL_LIB_CTX_free(testctx);
   6899      1.1  christos }
   6900