1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/* We need to use some deprecated APIs */
11e1051a39Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <stdio.h>
14e1051a39Sopenharmony_ci#include <stdlib.h>
15e1051a39Sopenharmony_ci#include <string.h>
16e1051a39Sopenharmony_ci#include <openssl/bio.h>
17e1051a39Sopenharmony_ci#include <openssl/conf.h>
18e1051a39Sopenharmony_ci#include <openssl/crypto.h>
19e1051a39Sopenharmony_ci#include <openssl/err.h>
20e1051a39Sopenharmony_ci#include <openssl/evp.h>
21e1051a39Sopenharmony_ci#include <openssl/x509.h>
22e1051a39Sopenharmony_ci#include <openssl/pem.h>
23e1051a39Sopenharmony_ci#include <openssl/kdf.h>
24e1051a39Sopenharmony_ci#include <openssl/provider.h>
25e1051a39Sopenharmony_ci#include <openssl/core_names.h>
26e1051a39Sopenharmony_ci#include <openssl/params.h>
27e1051a39Sopenharmony_ci#include <openssl/param_build.h>
28e1051a39Sopenharmony_ci#include <openssl/dsa.h>
29e1051a39Sopenharmony_ci#include <openssl/dh.h>
30e1051a39Sopenharmony_ci#include <openssl/aes.h>
31e1051a39Sopenharmony_ci#include <openssl/decoder.h>
32e1051a39Sopenharmony_ci#include <openssl/rsa.h>
33e1051a39Sopenharmony_ci#include <openssl/engine.h>
34e1051a39Sopenharmony_ci#include <openssl/proverr.h>
35e1051a39Sopenharmony_ci#include "testutil.h"
36e1051a39Sopenharmony_ci#include "internal/nelem.h"
37e1051a39Sopenharmony_ci#include "internal/sizes.h"
38e1051a39Sopenharmony_ci#include "crypto/evp.h"
39e1051a39Sopenharmony_ci
40e1051a39Sopenharmony_cistatic OSSL_LIB_CTX *testctx = NULL;
41e1051a39Sopenharmony_cistatic char *testpropq = NULL;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_cistatic OSSL_PROVIDER *nullprov = NULL;
44e1051a39Sopenharmony_cistatic OSSL_PROVIDER *deflprov = NULL;
45e1051a39Sopenharmony_cistatic OSSL_PROVIDER *lgcyprov = NULL;
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci/*
48e1051a39Sopenharmony_ci * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
49e1051a39Sopenharmony_ci * should never use this key anywhere but in an example.
50e1051a39Sopenharmony_ci */
51e1051a39Sopenharmony_cistatic const unsigned char kExampleRSAKeyDER[] = {
52e1051a39Sopenharmony_ci    0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
53e1051a39Sopenharmony_ci    0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
54e1051a39Sopenharmony_ci    0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
55e1051a39Sopenharmony_ci    0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
56e1051a39Sopenharmony_ci    0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
57e1051a39Sopenharmony_ci    0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
58e1051a39Sopenharmony_ci    0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
59e1051a39Sopenharmony_ci    0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
60e1051a39Sopenharmony_ci    0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
61e1051a39Sopenharmony_ci    0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
62e1051a39Sopenharmony_ci    0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
63e1051a39Sopenharmony_ci    0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
64e1051a39Sopenharmony_ci    0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
65e1051a39Sopenharmony_ci    0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
66e1051a39Sopenharmony_ci    0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
67e1051a39Sopenharmony_ci    0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
68e1051a39Sopenharmony_ci    0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
69e1051a39Sopenharmony_ci    0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
70e1051a39Sopenharmony_ci    0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
71e1051a39Sopenharmony_ci    0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
72e1051a39Sopenharmony_ci    0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
73e1051a39Sopenharmony_ci    0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
74e1051a39Sopenharmony_ci    0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
75e1051a39Sopenharmony_ci    0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
76e1051a39Sopenharmony_ci    0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
77e1051a39Sopenharmony_ci    0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
78e1051a39Sopenharmony_ci    0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
79e1051a39Sopenharmony_ci    0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
80e1051a39Sopenharmony_ci    0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
81e1051a39Sopenharmony_ci    0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
82e1051a39Sopenharmony_ci    0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
83e1051a39Sopenharmony_ci    0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
84e1051a39Sopenharmony_ci    0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
85e1051a39Sopenharmony_ci    0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
86e1051a39Sopenharmony_ci    0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
87e1051a39Sopenharmony_ci    0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
88e1051a39Sopenharmony_ci    0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
89e1051a39Sopenharmony_ci    0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
90e1051a39Sopenharmony_ci    0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
91e1051a39Sopenharmony_ci    0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
92e1051a39Sopenharmony_ci    0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
93e1051a39Sopenharmony_ci    0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
94e1051a39Sopenharmony_ci    0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
95e1051a39Sopenharmony_ci    0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
96e1051a39Sopenharmony_ci    0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
97e1051a39Sopenharmony_ci    0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
98e1051a39Sopenharmony_ci    0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
99e1051a39Sopenharmony_ci    0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
100e1051a39Sopenharmony_ci    0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
101e1051a39Sopenharmony_ci    0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
102e1051a39Sopenharmony_ci    0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
103e1051a39Sopenharmony_ci};
104e1051a39Sopenharmony_ci
105e1051a39Sopenharmony_ci/*
106e1051a39Sopenharmony_ci* kExampleDSAKeyDER is a DSA private key in ASN.1, DER format. Of course, you
107e1051a39Sopenharmony_ci * should never use this key anywhere but in an example.
108e1051a39Sopenharmony_ci */
109e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
110e1051a39Sopenharmony_cistatic const unsigned char kExampleDSAKeyDER[] = {
111e1051a39Sopenharmony_ci    0x30, 0x82, 0x01, 0xba, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0x9a,
112e1051a39Sopenharmony_ci    0x05, 0x6d, 0x33, 0xcd, 0x5d, 0x78, 0xa1, 0xbb, 0xcb, 0x7d, 0x5b, 0x8d,
113e1051a39Sopenharmony_ci    0xb4, 0xcc, 0xbf, 0x03, 0x99, 0x64, 0xde, 0x38, 0x78, 0x06, 0x15, 0x2f,
114e1051a39Sopenharmony_ci    0x86, 0x26, 0x77, 0xf3, 0xb1, 0x85, 0x00, 0xed, 0xfc, 0x28, 0x3a, 0x42,
115e1051a39Sopenharmony_ci    0x4d, 0xab, 0xab, 0xdf, 0xbc, 0x9c, 0x16, 0xd0, 0x22, 0x50, 0xd1, 0x38,
116e1051a39Sopenharmony_ci    0xdd, 0x3f, 0x64, 0x05, 0x9e, 0x68, 0x7a, 0x1e, 0xf1, 0x56, 0xbf, 0x1e,
117e1051a39Sopenharmony_ci    0x2c, 0xc5, 0x97, 0x2a, 0xfe, 0x7a, 0x22, 0xdc, 0x6c, 0x68, 0xb8, 0x2e,
118e1051a39Sopenharmony_ci    0x06, 0xdb, 0x41, 0xca, 0x98, 0xd8, 0x54, 0xc7, 0x64, 0x48, 0x24, 0x04,
119e1051a39Sopenharmony_ci    0x20, 0xbc, 0x59, 0xe3, 0x6b, 0xea, 0x7e, 0xfc, 0x7e, 0xc5, 0x4e, 0xd4,
120e1051a39Sopenharmony_ci    0xd8, 0x3a, 0xed, 0xcd, 0x5d, 0x99, 0xb8, 0x5c, 0xa2, 0x8b, 0xbb, 0x0b,
121e1051a39Sopenharmony_ci    0xac, 0xe6, 0x8e, 0x25, 0x56, 0x22, 0x3a, 0x2d, 0x3a, 0x56, 0x41, 0x14,
122e1051a39Sopenharmony_ci    0x1f, 0x1c, 0x8f, 0x53, 0x46, 0x13, 0x85, 0x02, 0x15, 0x00, 0x98, 0x7e,
123e1051a39Sopenharmony_ci    0x92, 0x81, 0x88, 0xc7, 0x3f, 0x70, 0x49, 0x54, 0xf6, 0x76, 0xb4, 0xa3,
124e1051a39Sopenharmony_ci    0x9e, 0x1d, 0x45, 0x98, 0x32, 0x7f, 0x02, 0x81, 0x80, 0x69, 0x4d, 0xef,
125e1051a39Sopenharmony_ci    0x55, 0xff, 0x4d, 0x59, 0x2c, 0x01, 0xfa, 0x6a, 0x38, 0xe0, 0x70, 0x9f,
126e1051a39Sopenharmony_ci    0x9e, 0x66, 0x8e, 0x3e, 0x8c, 0x52, 0x22, 0x9d, 0x15, 0x7e, 0x3c, 0xef,
127e1051a39Sopenharmony_ci    0x4c, 0x7a, 0x61, 0x26, 0xe0, 0x2b, 0x81, 0x3f, 0xeb, 0xaf, 0x35, 0x38,
128e1051a39Sopenharmony_ci    0x8d, 0xfe, 0xed, 0x46, 0xff, 0x5f, 0x03, 0x9b, 0x81, 0x92, 0xe7, 0x6f,
129e1051a39Sopenharmony_ci    0x76, 0x4f, 0x1d, 0xd9, 0xbb, 0x89, 0xc9, 0x3e, 0xd9, 0x0b, 0xf9, 0xf4,
130e1051a39Sopenharmony_ci    0x78, 0x11, 0x59, 0xc0, 0x1d, 0xcd, 0x0e, 0xa1, 0x6f, 0x15, 0xf1, 0x4d,
131e1051a39Sopenharmony_ci    0xc1, 0xc9, 0x22, 0xed, 0x8d, 0xad, 0x67, 0xc5, 0x4b, 0x95, 0x93, 0x86,
132e1051a39Sopenharmony_ci    0xa6, 0xaf, 0x8a, 0xee, 0x06, 0x89, 0x2f, 0x37, 0x7e, 0x64, 0xaa, 0xf6,
133e1051a39Sopenharmony_ci    0xe7, 0xb1, 0x5a, 0x0a, 0x93, 0x95, 0x5d, 0x3e, 0x53, 0x9a, 0xde, 0x8a,
134e1051a39Sopenharmony_ci    0xc2, 0x95, 0x45, 0x81, 0xbe, 0x5c, 0x2f, 0xc2, 0xb2, 0x92, 0x58, 0x19,
135e1051a39Sopenharmony_ci    0x72, 0x80, 0xe9, 0x79, 0xa1, 0x02, 0x81, 0x80, 0x07, 0xd7, 0x62, 0xff,
136e1051a39Sopenharmony_ci    0xdf, 0x1a, 0x3f, 0xed, 0x32, 0xd4, 0xd4, 0x88, 0x7b, 0x2c, 0x63, 0x7f,
137e1051a39Sopenharmony_ci    0x97, 0xdc, 0x44, 0xd4, 0x84, 0xa2, 0xdd, 0x17, 0x16, 0x85, 0x13, 0xe0,
138e1051a39Sopenharmony_ci    0xac, 0x51, 0x8d, 0x29, 0x1b, 0x75, 0x9a, 0xe4, 0xe3, 0x8a, 0x92, 0x69,
139e1051a39Sopenharmony_ci    0x09, 0x03, 0xc5, 0x68, 0xae, 0x5e, 0x94, 0xfe, 0xc9, 0x92, 0x6c, 0x07,
140e1051a39Sopenharmony_ci    0xb4, 0x1e, 0x64, 0x62, 0x87, 0xc6, 0xa4, 0xfd, 0x0d, 0x5f, 0xe5, 0xf9,
141e1051a39Sopenharmony_ci    0x1b, 0x4f, 0x85, 0x5f, 0xae, 0xf3, 0x11, 0xe5, 0x18, 0xd4, 0x4d, 0x79,
142e1051a39Sopenharmony_ci    0x9f, 0xc4, 0x79, 0x26, 0x04, 0x27, 0xf0, 0x0b, 0xee, 0x2b, 0x86, 0x9f,
143e1051a39Sopenharmony_ci    0x86, 0x61, 0xe6, 0x51, 0xce, 0x04, 0x9b, 0x5d, 0x6b, 0x34, 0x43, 0x8c,
144e1051a39Sopenharmony_ci    0x85, 0x3c, 0xf1, 0x51, 0x9b, 0x08, 0x23, 0x1b, 0xf5, 0x7e, 0x33, 0x12,
145e1051a39Sopenharmony_ci    0xea, 0xab, 0x1f, 0xb7, 0x2d, 0xe2, 0x5f, 0xe6, 0x97, 0x99, 0xb5, 0x45,
146e1051a39Sopenharmony_ci    0x16, 0x5b, 0xc3, 0x41, 0x02, 0x14, 0x61, 0xbf, 0x51, 0x60, 0xcf, 0xc8,
147e1051a39Sopenharmony_ci    0xf1, 0x8c, 0x82, 0x97, 0xf2, 0xf4, 0x19, 0xba, 0x2b, 0xf3, 0x16, 0xbe,
148e1051a39Sopenharmony_ci    0x40, 0x48
149e1051a39Sopenharmony_ci};
150e1051a39Sopenharmony_ci#endif
151e1051a39Sopenharmony_ci
152e1051a39Sopenharmony_ci/*
153e1051a39Sopenharmony_ci * kExampleBadRSAKeyDER is an RSA private key in ASN.1, DER format. The private
154e1051a39Sopenharmony_ci * components are not correct.
155e1051a39Sopenharmony_ci */
156e1051a39Sopenharmony_cistatic const unsigned char kExampleBadRSAKeyDER[] = {
157e1051a39Sopenharmony_ci    0x30, 0x82, 0x04, 0x27, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
158e1051a39Sopenharmony_ci    0xa6, 0x1a, 0x1e, 0x6e, 0x7b, 0xee, 0xc6, 0x89, 0x66, 0xe7, 0x93, 0xef,
159e1051a39Sopenharmony_ci    0x54, 0x12, 0x68, 0xea, 0xbf, 0x86, 0x2f, 0xdd, 0xd2, 0x79, 0xb8, 0xa9,
160e1051a39Sopenharmony_ci    0x6e, 0x03, 0xc2, 0xa3, 0xb9, 0xa3, 0xe1, 0x4b, 0x2a, 0xb3, 0xf8, 0xb4,
161e1051a39Sopenharmony_ci    0xcd, 0xea, 0xbe, 0x24, 0xa6, 0x57, 0x5b, 0x83, 0x1f, 0x0f, 0xf2, 0xd3,
162e1051a39Sopenharmony_ci    0xb7, 0xac, 0x7e, 0xd6, 0x8e, 0x6e, 0x1e, 0xbf, 0xb8, 0x73, 0x8c, 0x05,
163e1051a39Sopenharmony_ci    0x56, 0xe6, 0x35, 0x1f, 0xe9, 0x04, 0x0b, 0x09, 0x86, 0x7d, 0xf1, 0x26,
164e1051a39Sopenharmony_ci    0x08, 0x99, 0xad, 0x7b, 0xc8, 0x4d, 0x94, 0xb0, 0x0b, 0x8b, 0x38, 0xa0,
165e1051a39Sopenharmony_ci    0x5c, 0x62, 0xa0, 0xab, 0xd3, 0x8f, 0xd4, 0x09, 0x60, 0x72, 0x1e, 0x33,
166e1051a39Sopenharmony_ci    0x50, 0x80, 0x6e, 0x22, 0xa6, 0x77, 0x57, 0x6b, 0x9a, 0x33, 0x21, 0x66,
167e1051a39Sopenharmony_ci    0x87, 0x6e, 0x21, 0x7b, 0xc7, 0x24, 0x0e, 0xd8, 0x13, 0xdf, 0x83, 0xde,
168e1051a39Sopenharmony_ci    0xcd, 0x40, 0x58, 0x1d, 0x84, 0x86, 0xeb, 0xb8, 0x12, 0x4e, 0xd2, 0xfa,
169e1051a39Sopenharmony_ci    0x80, 0x1f, 0xe4, 0xe7, 0x96, 0x29, 0xb8, 0xcc, 0xce, 0x66, 0x6d, 0x53,
170e1051a39Sopenharmony_ci    0xca, 0xb9, 0x5a, 0xd7, 0xf6, 0x84, 0x6c, 0x2d, 0x9a, 0x1a, 0x14, 0x1c,
171e1051a39Sopenharmony_ci    0x4e, 0x93, 0x39, 0xba, 0x74, 0xed, 0xed, 0x87, 0x87, 0x5e, 0x48, 0x75,
172e1051a39Sopenharmony_ci    0x36, 0xf0, 0xbc, 0x34, 0xfb, 0x29, 0xf9, 0x9f, 0x96, 0x5b, 0x0b, 0xa7,
173e1051a39Sopenharmony_ci    0x54, 0x30, 0x51, 0x29, 0x18, 0x5b, 0x7d, 0xac, 0x0f, 0xd6, 0x5f, 0x7c,
174e1051a39Sopenharmony_ci    0xf8, 0x98, 0x8c, 0xd8, 0x86, 0x62, 0xb3, 0xdc, 0xff, 0x0f, 0xff, 0x7a,
175e1051a39Sopenharmony_ci    0xaf, 0x5c, 0x4c, 0x61, 0x49, 0x2e, 0xc8, 0x95, 0x86, 0xc4, 0x0e, 0x87,
176e1051a39Sopenharmony_ci    0xfc, 0x1d, 0xcf, 0x8b, 0x7c, 0x61, 0xf6, 0xd8, 0xd0, 0x69, 0xf6, 0xcd,
177e1051a39Sopenharmony_ci    0x8a, 0x8c, 0xf6, 0x62, 0xa2, 0x56, 0xa9, 0xe3, 0xd1, 0xcf, 0x4d, 0xa0,
178e1051a39Sopenharmony_ci    0xf6, 0x2d, 0x20, 0x0a, 0x04, 0xb7, 0xa2, 0xf7, 0xb5, 0x99, 0x47, 0x18,
179e1051a39Sopenharmony_ci    0x56, 0x85, 0x87, 0xc7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01,
180e1051a39Sopenharmony_ci    0x01, 0x00, 0x99, 0x41, 0x38, 0x1a, 0xd0, 0x96, 0x7a, 0xf0, 0x83, 0xd5,
181e1051a39Sopenharmony_ci    0xdf, 0x94, 0xce, 0x89, 0x3d, 0xec, 0x7a, 0x52, 0x21, 0x10, 0x16, 0x06,
182e1051a39Sopenharmony_ci    0xe0, 0xee, 0xd2, 0xe6, 0xfd, 0x4b, 0x7b, 0x19, 0x4d, 0xe1, 0xc0, 0xc0,
183e1051a39Sopenharmony_ci    0xd5, 0x14, 0x5d, 0x79, 0xdd, 0x7e, 0x8b, 0x4b, 0xc6, 0xcf, 0xb0, 0x75,
184e1051a39Sopenharmony_ci    0x52, 0xa3, 0x2d, 0xb1, 0x26, 0x46, 0x68, 0x9c, 0x0a, 0x1a, 0xf2, 0xe1,
185e1051a39Sopenharmony_ci    0x09, 0xac, 0x53, 0x85, 0x8c, 0x36, 0xa9, 0x14, 0x65, 0xea, 0xa0, 0x00,
186e1051a39Sopenharmony_ci    0xcb, 0xe3, 0x3f, 0xc4, 0x2b, 0x61, 0x2e, 0x6b, 0x06, 0x69, 0x77, 0xfd,
187e1051a39Sopenharmony_ci    0x38, 0x7e, 0x1d, 0x3f, 0x92, 0xe7, 0x77, 0x08, 0x19, 0xa7, 0x9d, 0x29,
188e1051a39Sopenharmony_ci    0x2d, 0xdc, 0x42, 0xc6, 0x7c, 0xd7, 0xd3, 0xa8, 0x01, 0x2c, 0xf2, 0xd5,
189e1051a39Sopenharmony_ci    0x82, 0x57, 0xcb, 0x55, 0x3d, 0xe7, 0xaa, 0xd2, 0x06, 0x30, 0x30, 0x05,
190e1051a39Sopenharmony_ci    0xe6, 0xf2, 0x47, 0x86, 0xba, 0xc6, 0x61, 0x64, 0xeb, 0x4f, 0x2a, 0x5e,
191e1051a39Sopenharmony_ci    0x07, 0x29, 0xe0, 0x96, 0xb2, 0x43, 0xff, 0x5f, 0x1a, 0x54, 0x16, 0xcf,
192e1051a39Sopenharmony_ci    0xb5, 0x56, 0x5c, 0xa0, 0x9b, 0x0c, 0xfd, 0xb3, 0xd2, 0xe3, 0x79, 0x1d,
193e1051a39Sopenharmony_ci    0x21, 0xe2, 0xd6, 0x13, 0xc4, 0x74, 0xa6, 0xf5, 0x8e, 0x8e, 0x81, 0xbb,
194e1051a39Sopenharmony_ci    0xb4, 0xad, 0x8a, 0xf0, 0x93, 0x0a, 0xd8, 0x0a, 0x42, 0x36, 0xbc, 0xe5,
195e1051a39Sopenharmony_ci    0x26, 0x2a, 0x0d, 0x5d, 0x57, 0x13, 0xc5, 0x4e, 0x2f, 0x12, 0x0e, 0xef,
196e1051a39Sopenharmony_ci    0xa7, 0x81, 0x1e, 0xc3, 0xa5, 0xdb, 0xc9, 0x24, 0xeb, 0x1a, 0xa1, 0xf9,
197e1051a39Sopenharmony_ci    0xf6, 0xa1, 0x78, 0x98, 0x93, 0x77, 0x42, 0x45, 0x03, 0xe2, 0xc9, 0xa2,
198e1051a39Sopenharmony_ci    0xfe, 0x2d, 0x77, 0xc8, 0xc6, 0xac, 0x9b, 0x98, 0x89, 0x6d, 0x9a, 0xe7,
199e1051a39Sopenharmony_ci    0x61, 0x63, 0xb7, 0xf2, 0xec, 0xd6, 0xb1, 0xa1, 0x6e, 0x0a, 0x1a, 0xff,
200e1051a39Sopenharmony_ci    0xfd, 0x43, 0x28, 0xc3, 0x0c, 0xdc, 0xf2, 0x47, 0x4f, 0x27, 0xaa, 0x99,
201e1051a39Sopenharmony_ci    0x04, 0x8e, 0xac, 0xe8, 0x7c, 0x01, 0x02, 0x04, 0x12, 0x34, 0x56, 0x78,
202e1051a39Sopenharmony_ci    0x02, 0x81, 0x81, 0x00, 0xca, 0x69, 0xe5, 0xbb, 0x3a, 0x90, 0x82, 0xcb,
203e1051a39Sopenharmony_ci    0x82, 0x50, 0x2f, 0x29, 0xe2, 0x76, 0x6a, 0x57, 0x55, 0x45, 0x4e, 0x35,
204e1051a39Sopenharmony_ci    0x18, 0x61, 0xe0, 0x12, 0x70, 0xc0, 0xab, 0xc7, 0x80, 0xa2, 0xd4, 0x46,
205e1051a39Sopenharmony_ci    0x34, 0x03, 0xa0, 0x19, 0x26, 0x23, 0x9e, 0xef, 0x1a, 0xcb, 0x75, 0xd6,
206e1051a39Sopenharmony_ci    0xba, 0x81, 0xf4, 0x7e, 0x52, 0xe5, 0x2a, 0xe8, 0xf1, 0x49, 0x6c, 0x0f,
207e1051a39Sopenharmony_ci    0x1a, 0xa0, 0xf9, 0xc6, 0xe7, 0xec, 0x60, 0xe4, 0xcb, 0x2a, 0xb5, 0x56,
208e1051a39Sopenharmony_ci    0xe9, 0x9c, 0xcd, 0x19, 0x75, 0x92, 0xb1, 0x66, 0xce, 0xc3, 0xd9, 0x3d,
209e1051a39Sopenharmony_ci    0x11, 0xcb, 0xc4, 0x09, 0xce, 0x1e, 0x30, 0xba, 0x2f, 0x60, 0x60, 0x55,
210e1051a39Sopenharmony_ci    0x8d, 0x02, 0xdc, 0x5d, 0xaf, 0xf7, 0x52, 0x31, 0x17, 0x07, 0x53, 0x20,
211e1051a39Sopenharmony_ci    0x33, 0xad, 0x8c, 0xd5, 0x2f, 0x5a, 0xd0, 0x57, 0xd7, 0xd1, 0x80, 0xd6,
212e1051a39Sopenharmony_ci    0x3a, 0x9b, 0x04, 0x4f, 0x35, 0xbf, 0xe7, 0xd5, 0xbc, 0x8f, 0xd4, 0x81,
213e1051a39Sopenharmony_ci    0x02, 0x81, 0x81, 0x00, 0xc0, 0x9f, 0xf8, 0xcd, 0xf7, 0x3f, 0x26, 0x8a,
214e1051a39Sopenharmony_ci    0x3d, 0x4d, 0x2b, 0x0c, 0x01, 0xd0, 0xa2, 0xb4, 0x18, 0xfe, 0xf7, 0x5e,
215e1051a39Sopenharmony_ci    0x2f, 0x06, 0x13, 0xcd, 0x63, 0xaa, 0x12, 0xa9, 0x24, 0x86, 0xe3, 0xf3,
216e1051a39Sopenharmony_ci    0x7b, 0xda, 0x1a, 0x3c, 0xb1, 0x38, 0x80, 0x80, 0xef, 0x64, 0x64, 0xa1,
217e1051a39Sopenharmony_ci    0x9b, 0xfe, 0x76, 0x63, 0x8e, 0x83, 0xd2, 0xd9, 0xb9, 0x86, 0xb0, 0xe6,
218e1051a39Sopenharmony_ci    0xa6, 0x0c, 0x7e, 0xa8, 0x84, 0x90, 0x98, 0x0c, 0x1e, 0xf3, 0x14, 0x77,
219e1051a39Sopenharmony_ci    0xe0, 0x5f, 0x81, 0x08, 0x11, 0x8f, 0xa6, 0x23, 0xc4, 0xba, 0xc0, 0x8a,
220e1051a39Sopenharmony_ci    0xe4, 0xc6, 0xe3, 0x5c, 0xbe, 0xc5, 0xec, 0x2c, 0xb9, 0xd8, 0x8c, 0x4d,
221e1051a39Sopenharmony_ci    0x1a, 0x9d, 0xe7, 0x7c, 0x85, 0x4c, 0x0d, 0x71, 0x4e, 0x72, 0x33, 0x1b,
222e1051a39Sopenharmony_ci    0xfe, 0xa9, 0x17, 0x72, 0x76, 0x56, 0x9d, 0x74, 0x7e, 0x52, 0x67, 0x9a,
223e1051a39Sopenharmony_ci    0x87, 0x9a, 0xdb, 0x30, 0xde, 0xe4, 0x49, 0x28, 0x3b, 0xd2, 0x67, 0xaf,
224e1051a39Sopenharmony_ci    0x02, 0x81, 0x81, 0x00, 0x89, 0x74, 0x9a, 0x8e, 0xa7, 0xb9, 0xa5, 0x28,
225e1051a39Sopenharmony_ci    0xc0, 0x68, 0xe5, 0x6e, 0x63, 0x1c, 0x99, 0x20, 0x8f, 0x86, 0x8e, 0x12,
226e1051a39Sopenharmony_ci    0x9e, 0x69, 0x30, 0xfa, 0x34, 0xd9, 0x92, 0x8d, 0xdb, 0x7c, 0x37, 0xfd,
227e1051a39Sopenharmony_ci    0x28, 0xab, 0x61, 0x98, 0x52, 0x7f, 0x14, 0x1a, 0x39, 0xae, 0xfb, 0x6a,
228e1051a39Sopenharmony_ci    0x03, 0xa3, 0xe6, 0xbd, 0xb6, 0x5b, 0x6b, 0xe5, 0x5e, 0x9d, 0xc6, 0xa5,
229e1051a39Sopenharmony_ci    0x07, 0x27, 0x54, 0x17, 0xd0, 0x3d, 0x84, 0x9b, 0x3a, 0xa0, 0xd9, 0x1e,
230e1051a39Sopenharmony_ci    0x99, 0x6c, 0x63, 0x17, 0xab, 0xf1, 0x1f, 0x49, 0xba, 0x95, 0xe3, 0x3b,
231e1051a39Sopenharmony_ci    0x86, 0x8f, 0x42, 0xa4, 0x89, 0xf5, 0x94, 0x8f, 0x8b, 0x46, 0xbe, 0x84,
232e1051a39Sopenharmony_ci    0xba, 0x4a, 0xbc, 0x0d, 0x5f, 0x46, 0xeb, 0xe8, 0xec, 0x43, 0x8c, 0x1e,
233e1051a39Sopenharmony_ci    0xad, 0x19, 0x69, 0x2f, 0x08, 0x86, 0x7a, 0x3f, 0x7d, 0x0f, 0x07, 0x97,
234e1051a39Sopenharmony_ci    0xf3, 0x9a, 0x7b, 0xb5, 0xb2, 0xc1, 0x8c, 0x95, 0x68, 0x04, 0xa0, 0x81,
235e1051a39Sopenharmony_ci    0x02, 0x81, 0x80, 0x4e, 0xbf, 0x7e, 0x1b, 0xcb, 0x13, 0x61, 0x75, 0x3b,
236e1051a39Sopenharmony_ci    0xdb, 0x59, 0x5f, 0xb1, 0xd4, 0xb8, 0xeb, 0x9e, 0x73, 0xb5, 0xe7, 0xf6,
237e1051a39Sopenharmony_ci    0x89, 0x3d, 0x1c, 0xda, 0xf0, 0x36, 0xff, 0x35, 0xbd, 0x1e, 0x0b, 0x74,
238e1051a39Sopenharmony_ci    0xe3, 0x9e, 0xf0, 0xf2, 0xf7, 0xd7, 0x82, 0xb7, 0x7b, 0x6a, 0x1b, 0x0e,
239e1051a39Sopenharmony_ci    0x30, 0x4a, 0x98, 0x0e, 0xb4, 0xf9, 0x81, 0x07, 0xe4, 0x75, 0x39, 0xe9,
240e1051a39Sopenharmony_ci    0x53, 0xca, 0xbb, 0x5c, 0xaa, 0x93, 0x07, 0x0e, 0xa8, 0x2f, 0xba, 0x98,
241e1051a39Sopenharmony_ci    0x49, 0x30, 0xa7, 0xcc, 0x1a, 0x3c, 0x68, 0x0c, 0xe1, 0xa4, 0xb1, 0x05,
242e1051a39Sopenharmony_ci    0xe6, 0xe0, 0x25, 0x78, 0x58, 0x14, 0x37, 0xf5, 0x1f, 0xe3, 0x22, 0xef,
243e1051a39Sopenharmony_ci    0xa8, 0x0e, 0x22, 0xa0, 0x94, 0x3a, 0xf6, 0xc9, 0x13, 0xe6, 0x06, 0xbf,
244e1051a39Sopenharmony_ci    0x7f, 0x99, 0xc6, 0xcc, 0xd8, 0xc6, 0xbe, 0xd9, 0x2e, 0x24, 0xc7, 0x69,
245e1051a39Sopenharmony_ci    0x8c, 0x95, 0xba, 0xf6, 0x04, 0xb3, 0x0a, 0xf4, 0xcb, 0xf0, 0xce,
246e1051a39Sopenharmony_ci};
247e1051a39Sopenharmony_ci
248e1051a39Sopenharmony_ci/*
249e1051a39Sopenharmony_ci * kExampleBad2RSAKeyDER is an RSA private key in ASN.1, DER format. All
250e1051a39Sopenharmony_ci * values are 0.
251e1051a39Sopenharmony_ci */
252e1051a39Sopenharmony_cistatic const unsigned char kExampleBad2RSAKeyDER[] = {
253e1051a39Sopenharmony_ci    0x30, 0x1b, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
254e1051a39Sopenharmony_ci    0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x02,
255e1051a39Sopenharmony_ci    0x01, 0x00, 0x02, 0x01, 0x00
256e1051a39Sopenharmony_ci};
257e1051a39Sopenharmony_ci
258e1051a39Sopenharmony_cistatic const unsigned char kMsg[] = { 1, 2, 3, 4 };
259e1051a39Sopenharmony_ci
260e1051a39Sopenharmony_cistatic const unsigned char kSignature[] = {
261e1051a39Sopenharmony_ci    0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
262e1051a39Sopenharmony_ci    0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
263e1051a39Sopenharmony_ci    0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
264e1051a39Sopenharmony_ci    0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
265e1051a39Sopenharmony_ci    0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
266e1051a39Sopenharmony_ci    0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
267e1051a39Sopenharmony_ci    0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
268e1051a39Sopenharmony_ci    0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
269e1051a39Sopenharmony_ci    0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
270e1051a39Sopenharmony_ci    0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
271e1051a39Sopenharmony_ci    0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
272e1051a39Sopenharmony_ci};
273e1051a39Sopenharmony_ci
274e1051a39Sopenharmony_ci/*
275e1051a39Sopenharmony_ci * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
276e1051a39Sopenharmony_ci * PrivateKeyInfo.
277e1051a39Sopenharmony_ci */
278e1051a39Sopenharmony_cistatic const unsigned char kExampleRSAKeyPKCS8[] = {
279e1051a39Sopenharmony_ci    0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
280e1051a39Sopenharmony_ci    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
281e1051a39Sopenharmony_ci    0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
282e1051a39Sopenharmony_ci    0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
283e1051a39Sopenharmony_ci    0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
284e1051a39Sopenharmony_ci    0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
285e1051a39Sopenharmony_ci    0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
286e1051a39Sopenharmony_ci    0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
287e1051a39Sopenharmony_ci    0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
288e1051a39Sopenharmony_ci    0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
289e1051a39Sopenharmony_ci    0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
290e1051a39Sopenharmony_ci    0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
291e1051a39Sopenharmony_ci    0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
292e1051a39Sopenharmony_ci    0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
293e1051a39Sopenharmony_ci    0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
294e1051a39Sopenharmony_ci    0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
295e1051a39Sopenharmony_ci    0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
296e1051a39Sopenharmony_ci    0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
297e1051a39Sopenharmony_ci    0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
298e1051a39Sopenharmony_ci    0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
299e1051a39Sopenharmony_ci    0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
300e1051a39Sopenharmony_ci    0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
301e1051a39Sopenharmony_ci    0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
302e1051a39Sopenharmony_ci    0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
303e1051a39Sopenharmony_ci    0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
304e1051a39Sopenharmony_ci    0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
305e1051a39Sopenharmony_ci    0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
306e1051a39Sopenharmony_ci    0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
307e1051a39Sopenharmony_ci    0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
308e1051a39Sopenharmony_ci    0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
309e1051a39Sopenharmony_ci    0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
310e1051a39Sopenharmony_ci    0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
311e1051a39Sopenharmony_ci    0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
312e1051a39Sopenharmony_ci    0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
313e1051a39Sopenharmony_ci    0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
314e1051a39Sopenharmony_ci    0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
315e1051a39Sopenharmony_ci    0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
316e1051a39Sopenharmony_ci    0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
317e1051a39Sopenharmony_ci    0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
318e1051a39Sopenharmony_ci    0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
319e1051a39Sopenharmony_ci    0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
320e1051a39Sopenharmony_ci    0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
321e1051a39Sopenharmony_ci    0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
322e1051a39Sopenharmony_ci    0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
323e1051a39Sopenharmony_ci    0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
324e1051a39Sopenharmony_ci    0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
325e1051a39Sopenharmony_ci    0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
326e1051a39Sopenharmony_ci    0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
327e1051a39Sopenharmony_ci    0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
328e1051a39Sopenharmony_ci    0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
329e1051a39Sopenharmony_ci    0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
330e1051a39Sopenharmony_ci    0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
331e1051a39Sopenharmony_ci    0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
332e1051a39Sopenharmony_ci};
333e1051a39Sopenharmony_ci
334e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
335e1051a39Sopenharmony_ci/*
336e1051a39Sopenharmony_ci * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
337e1051a39Sopenharmony_ci * structure.
338e1051a39Sopenharmony_ci */
339e1051a39Sopenharmony_cistatic const unsigned char kExampleECKeyDER[] = {
340e1051a39Sopenharmony_ci    0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
341e1051a39Sopenharmony_ci    0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
342e1051a39Sopenharmony_ci    0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
343e1051a39Sopenharmony_ci    0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
344e1051a39Sopenharmony_ci    0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
345e1051a39Sopenharmony_ci    0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
346e1051a39Sopenharmony_ci    0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
347e1051a39Sopenharmony_ci    0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
348e1051a39Sopenharmony_ci    0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
349e1051a39Sopenharmony_ci    0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
350e1051a39Sopenharmony_ci    0xc1,
351e1051a39Sopenharmony_ci};
352e1051a39Sopenharmony_ci
353e1051a39Sopenharmony_ci/*
354e1051a39Sopenharmony_ci * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
355e1051a39Sopenharmony_ci * structure. The private key is equal to the order and will fail to import
356e1051a39Sopenharmony_ci */
357e1051a39Sopenharmony_cistatic const unsigned char kExampleBadECKeyDER[] = {
358e1051a39Sopenharmony_ci    0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
359e1051a39Sopenharmony_ci    0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
360e1051a39Sopenharmony_ci    0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
361e1051a39Sopenharmony_ci    0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
362e1051a39Sopenharmony_ci    0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
363e1051a39Sopenharmony_ci    0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
364e1051a39Sopenharmony_ci    0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
365e1051a39Sopenharmony_ci    0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
366e1051a39Sopenharmony_ci    0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
367e1051a39Sopenharmony_ci};
368e1051a39Sopenharmony_ci
369e1051a39Sopenharmony_ci/* prime256v1 */
370e1051a39Sopenharmony_cistatic const unsigned char kExampleECPubKeyDER[] = {
371e1051a39Sopenharmony_ci    0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
372e1051a39Sopenharmony_ci    0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
373e1051a39Sopenharmony_ci    0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
374e1051a39Sopenharmony_ci    0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
375e1051a39Sopenharmony_ci    0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
376e1051a39Sopenharmony_ci    0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
377e1051a39Sopenharmony_ci    0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
378e1051a39Sopenharmony_ci    0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
379e1051a39Sopenharmony_ci};
380e1051a39Sopenharmony_ci
381e1051a39Sopenharmony_ci/*
382e1051a39Sopenharmony_ci * kExampleBadECPubKeyDER is a sample EC public key with a wrong OID
383e1051a39Sopenharmony_ci * 1.2.840.10045.2.2 instead of 1.2.840.10045.2.1 - EC Public Key
384e1051a39Sopenharmony_ci */
385e1051a39Sopenharmony_cistatic const unsigned char kExampleBadECPubKeyDER[] = {
386e1051a39Sopenharmony_ci    0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
387e1051a39Sopenharmony_ci    0x02, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
388e1051a39Sopenharmony_ci    0x42, 0x00, 0x04, 0xba, 0xeb, 0x83, 0xfb, 0x3b, 0xb2, 0xff, 0x30, 0x53,
389e1051a39Sopenharmony_ci    0xdb, 0xce, 0x32, 0xf2, 0xac, 0xae, 0x44, 0x0d, 0x3d, 0x13, 0x53, 0xb8,
390e1051a39Sopenharmony_ci    0xd1, 0x68, 0x55, 0xde, 0x44, 0x46, 0x05, 0xa6, 0xc9, 0xd2, 0x04, 0xb7,
391e1051a39Sopenharmony_ci    0xe3, 0xa2, 0x96, 0xc8, 0xb2, 0x5e, 0x22, 0x03, 0xd7, 0x03, 0x7a, 0x8b,
392e1051a39Sopenharmony_ci    0x13, 0x5c, 0x42, 0x49, 0xc2, 0xab, 0x86, 0xd6, 0xac, 0x6b, 0x93, 0x20,
393e1051a39Sopenharmony_ci    0x56, 0x6a, 0xc6, 0xc8, 0xa5, 0x0b, 0xe5
394e1051a39Sopenharmony_ci};
395e1051a39Sopenharmony_ci
396e1051a39Sopenharmony_cistatic const unsigned char pExampleECParamDER[] = {
397e1051a39Sopenharmony_ci    0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
398e1051a39Sopenharmony_ci};
399e1051a39Sopenharmony_ci
400e1051a39Sopenharmony_cistatic const unsigned char kExampleED25519KeyDER[] = {
401e1051a39Sopenharmony_ci    0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
402e1051a39Sopenharmony_ci    0x04, 0x22, 0x04, 0x20, 0xba, 0x7b, 0xba, 0x20, 0x1b, 0x02, 0x75, 0x3a,
403e1051a39Sopenharmony_ci    0xe8, 0x88, 0xfe, 0x00, 0xcd, 0x8b, 0xc6, 0xf4, 0x5c, 0x47, 0x09, 0x46,
404e1051a39Sopenharmony_ci    0x66, 0xe4, 0x72, 0x85, 0x25, 0x26, 0x5e, 0x12, 0x33, 0x48, 0xf6, 0x50
405e1051a39Sopenharmony_ci};
406e1051a39Sopenharmony_ci
407e1051a39Sopenharmony_cistatic const unsigned char kExampleED25519PubKeyDER[] = {
408e1051a39Sopenharmony_ci    0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
409e1051a39Sopenharmony_ci    0xf5, 0xc5, 0xeb, 0x52, 0x3e, 0x7d, 0x07, 0x86, 0xb2, 0x55, 0x07, 0x45,
410e1051a39Sopenharmony_ci    0xef, 0x5b, 0x7c, 0x20, 0xe8, 0x66, 0x28, 0x30, 0x3c, 0x8a, 0x82, 0x40,
411e1051a39Sopenharmony_ci    0x97, 0xa3, 0x08, 0xdc, 0x65, 0x80, 0x39, 0x29
412e1051a39Sopenharmony_ci};
413e1051a39Sopenharmony_ci
414e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
415e1051a39Sopenharmony_cistatic const unsigned char kExampleX25519KeyDER[] = {
416e1051a39Sopenharmony_ci    0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
417e1051a39Sopenharmony_ci    0x04, 0x22, 0x04, 0x20, 0xa0, 0x24, 0x3a, 0x31, 0x24, 0xc3, 0x3f, 0xf6,
418e1051a39Sopenharmony_ci    0x7b, 0x96, 0x0b, 0xd4, 0x8f, 0xd1, 0xee, 0x67, 0xf2, 0x9b, 0x88, 0xac,
419e1051a39Sopenharmony_ci    0x50, 0xce, 0x97, 0x36, 0xdd, 0xaf, 0x25, 0xf6, 0x10, 0x34, 0x96, 0x6e
420e1051a39Sopenharmony_ci};
421e1051a39Sopenharmony_ci# endif
422e1051a39Sopenharmony_ci#endif
423e1051a39Sopenharmony_ci
424e1051a39Sopenharmony_ci/* kExampleDHKeyDER is a DH private key in ASN.1, DER format. */
425e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
426e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
427e1051a39Sopenharmony_cistatic const unsigned char kExampleDHKeyDER[] = {
428e1051a39Sopenharmony_ci    0x30, 0x82, 0x01, 0x21, 0x02, 0x01, 0x00, 0x30, 0x81, 0x95, 0x06, 0x09,
429e1051a39Sopenharmony_ci    0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x03, 0x01, 0x30, 0x81, 0x87,
430e1051a39Sopenharmony_ci    0x02, 0x81, 0x81, 0x00, 0xf7, 0x52, 0xc2, 0x68, 0xcc, 0x66, 0xc4, 0x8d,
431e1051a39Sopenharmony_ci    0x03, 0x3f, 0xfa, 0x9c, 0x52, 0xd0, 0xd8, 0x33, 0xf2, 0xe1, 0xc9, 0x9e,
432e1051a39Sopenharmony_ci    0xb7, 0xe7, 0x6e, 0x90, 0x97, 0xeb, 0x92, 0x91, 0x6a, 0x9a, 0x85, 0x63,
433e1051a39Sopenharmony_ci    0x92, 0x79, 0xab, 0xb6, 0x3d, 0x23, 0x58, 0x5a, 0xe8, 0x45, 0x06, 0x81,
434e1051a39Sopenharmony_ci    0x97, 0x77, 0xe1, 0xcc, 0x34, 0x4e, 0xae, 0x36, 0x80, 0xf2, 0xc4, 0x7f,
435e1051a39Sopenharmony_ci    0x8a, 0x52, 0xb8, 0xdb, 0x58, 0xc8, 0x4b, 0x12, 0x4c, 0xf1, 0x4c, 0x53,
436e1051a39Sopenharmony_ci    0xc1, 0x89, 0x39, 0x8d, 0xb6, 0x06, 0xd8, 0xea, 0x7f, 0x2d, 0x36, 0x53,
437e1051a39Sopenharmony_ci    0x96, 0x29, 0xbe, 0xb6, 0x75, 0xfc, 0xe7, 0xf3, 0x36, 0xd6, 0xf4, 0x8f,
438e1051a39Sopenharmony_ci    0x16, 0xa6, 0xc7, 0xec, 0x7b, 0xce, 0x42, 0x8d, 0x48, 0x2e, 0xb7, 0x74,
439e1051a39Sopenharmony_ci    0x00, 0x11, 0x52, 0x61, 0xb4, 0x19, 0x35, 0xec, 0x5c, 0xe4, 0xbe, 0x34,
440e1051a39Sopenharmony_ci    0xc6, 0x59, 0x64, 0x5e, 0x42, 0x61, 0x70, 0x54, 0xf4, 0xe9, 0x6b, 0x53,
441e1051a39Sopenharmony_ci    0x02, 0x01, 0x02, 0x04, 0x81, 0x83, 0x02, 0x81, 0x80, 0x64, 0xc2, 0xe3,
442e1051a39Sopenharmony_ci    0x09, 0x69, 0x37, 0x3c, 0xd2, 0x4a, 0xba, 0xc3, 0x78, 0x6a, 0x9b, 0x8a,
443e1051a39Sopenharmony_ci    0x2a, 0xdb, 0xe7, 0xe6, 0xc0, 0xfa, 0x3a, 0xbe, 0x39, 0x67, 0xc0, 0xa9,
444e1051a39Sopenharmony_ci    0x2a, 0xf0, 0x0a, 0xc1, 0x53, 0x1c, 0xdb, 0xfa, 0x1a, 0x26, 0x98, 0xb0,
445e1051a39Sopenharmony_ci    0x8c, 0xc6, 0x06, 0x4a, 0xa2, 0x48, 0xd3, 0xa4, 0x3b, 0xbd, 0x05, 0x48,
446e1051a39Sopenharmony_ci    0xea, 0x59, 0xdb, 0x18, 0xa4, 0xca, 0x66, 0xd9, 0x5d, 0xb8, 0x95, 0xd1,
447e1051a39Sopenharmony_ci    0xeb, 0x97, 0x3d, 0x66, 0x97, 0x5c, 0x86, 0x8f, 0x7e, 0x90, 0xd3, 0x43,
448e1051a39Sopenharmony_ci    0xd1, 0xa2, 0x0d, 0xcb, 0xe7, 0xeb, 0x90, 0xea, 0x09, 0x40, 0xb1, 0x6f,
449e1051a39Sopenharmony_ci    0xf7, 0x4c, 0xf2, 0x41, 0x83, 0x1d, 0xd0, 0x76, 0xef, 0xaf, 0x55, 0x6f,
450e1051a39Sopenharmony_ci    0x5d, 0xa9, 0xa3, 0x55, 0x81, 0x2a, 0xd1, 0x5d, 0x9d, 0x22, 0x77, 0x97,
451e1051a39Sopenharmony_ci    0x83, 0xde, 0xad, 0xb6, 0x5d, 0x19, 0xc1, 0x53, 0xec, 0xfb, 0xaf, 0x06,
452e1051a39Sopenharmony_ci    0x2e, 0x87, 0x2a, 0x0b, 0x7a
453e1051a39Sopenharmony_ci};
454e1051a39Sopenharmony_ci# endif
455e1051a39Sopenharmony_ci#endif
456e1051a39Sopenharmony_ci
457e1051a39Sopenharmony_cistatic const unsigned char kCFBDefaultKey[] = {
458e1051a39Sopenharmony_ci    0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88,
459e1051a39Sopenharmony_ci    0x09, 0xCF, 0x4F, 0x3C
460e1051a39Sopenharmony_ci};
461e1051a39Sopenharmony_ci
462e1051a39Sopenharmony_cistatic const unsigned char kGCMDefaultKey[32] = { 0 };
463e1051a39Sopenharmony_ci
464e1051a39Sopenharmony_cistatic const unsigned char kGCMResetKey[] = {
465e1051a39Sopenharmony_ci    0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
466e1051a39Sopenharmony_ci    0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
467e1051a39Sopenharmony_ci    0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
468e1051a39Sopenharmony_ci};
469e1051a39Sopenharmony_ci
470e1051a39Sopenharmony_cistatic const unsigned char iCFBIV[] = {
471e1051a39Sopenharmony_ci    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
472e1051a39Sopenharmony_ci    0x0C, 0x0D, 0x0E, 0x0F
473e1051a39Sopenharmony_ci};
474e1051a39Sopenharmony_ci
475e1051a39Sopenharmony_cistatic const unsigned char iGCMDefaultIV[12] = { 0 };
476e1051a39Sopenharmony_ci
477e1051a39Sopenharmony_cistatic const unsigned char iGCMResetIV1[] = {
478e1051a39Sopenharmony_ci    0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad
479e1051a39Sopenharmony_ci};
480e1051a39Sopenharmony_ci
481e1051a39Sopenharmony_cistatic const unsigned char iGCMResetIV2[] = {
482e1051a39Sopenharmony_ci    0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88
483e1051a39Sopenharmony_ci};
484e1051a39Sopenharmony_ci
485e1051a39Sopenharmony_cistatic const unsigned char cfbPlaintext[] = {
486e1051a39Sopenharmony_ci    0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96, 0xE9, 0x3D, 0x7E, 0x11,
487e1051a39Sopenharmony_ci    0x73, 0x93, 0x17, 0x2A
488e1051a39Sopenharmony_ci};
489e1051a39Sopenharmony_ci
490e1051a39Sopenharmony_cistatic const unsigned char gcmDefaultPlaintext[16] = { 0 };
491e1051a39Sopenharmony_ci
492e1051a39Sopenharmony_cistatic const unsigned char gcmResetPlaintext[] = {
493e1051a39Sopenharmony_ci    0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
494e1051a39Sopenharmony_ci    0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
495e1051a39Sopenharmony_ci    0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
496e1051a39Sopenharmony_ci    0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
497e1051a39Sopenharmony_ci    0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39
498e1051a39Sopenharmony_ci};
499e1051a39Sopenharmony_ci
500e1051a39Sopenharmony_cistatic const unsigned char cfbCiphertext[] = {
501e1051a39Sopenharmony_ci    0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20, 0x33, 0x34, 0x49, 0xF8,
502e1051a39Sopenharmony_ci    0xE8, 0x3C, 0xFB, 0x4A
503e1051a39Sopenharmony_ci};
504e1051a39Sopenharmony_ci
505e1051a39Sopenharmony_cistatic const unsigned char gcmDefaultCiphertext[] = {
506e1051a39Sopenharmony_ci    0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e, 0x07, 0x4e, 0xc5, 0xd3,
507e1051a39Sopenharmony_ci    0xba, 0xf3, 0x9d, 0x18
508e1051a39Sopenharmony_ci};
509e1051a39Sopenharmony_ci
510e1051a39Sopenharmony_cistatic const unsigned char gcmResetCiphertext1[] = {
511e1051a39Sopenharmony_ci    0xc3, 0x76, 0x2d, 0xf1, 0xca, 0x78, 0x7d, 0x32, 0xae, 0x47, 0xc1, 0x3b,
512e1051a39Sopenharmony_ci    0xf1, 0x98, 0x44, 0xcb, 0xaf, 0x1a, 0xe1, 0x4d, 0x0b, 0x97, 0x6a, 0xfa,
513e1051a39Sopenharmony_ci    0xc5, 0x2f, 0xf7, 0xd7, 0x9b, 0xba, 0x9d, 0xe0, 0xfe, 0xb5, 0x82, 0xd3,
514e1051a39Sopenharmony_ci    0x39, 0x34, 0xa4, 0xf0, 0x95, 0x4c, 0xc2, 0x36, 0x3b, 0xc7, 0x3f, 0x78,
515e1051a39Sopenharmony_ci    0x62, 0xac, 0x43, 0x0e, 0x64, 0xab, 0xe4, 0x99, 0xf4, 0x7c, 0x9b, 0x1f
516e1051a39Sopenharmony_ci};
517e1051a39Sopenharmony_ci
518e1051a39Sopenharmony_cistatic const unsigned char gcmResetCiphertext2[] = {
519e1051a39Sopenharmony_ci    0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3,
520e1051a39Sopenharmony_ci    0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
521e1051a39Sopenharmony_ci    0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48,
522e1051a39Sopenharmony_ci    0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
523e1051a39Sopenharmony_ci    0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62
524e1051a39Sopenharmony_ci};
525e1051a39Sopenharmony_ci
526e1051a39Sopenharmony_cistatic const unsigned char gcmAAD[] = {
527e1051a39Sopenharmony_ci    0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, 0xfa, 0xce,
528e1051a39Sopenharmony_ci    0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2
529e1051a39Sopenharmony_ci};
530e1051a39Sopenharmony_ci
531e1051a39Sopenharmony_cistatic const unsigned char gcmDefaultTag[] = {
532e1051a39Sopenharmony_ci    0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99, 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5,
533e1051a39Sopenharmony_ci    0xd4, 0x8a, 0xb9, 0x19
534e1051a39Sopenharmony_ci};
535e1051a39Sopenharmony_ci
536e1051a39Sopenharmony_cistatic const unsigned char gcmResetTag1[] = {
537e1051a39Sopenharmony_ci    0x3a, 0x33, 0x7d, 0xbf, 0x46, 0xa7, 0x92, 0xc4, 0x5e, 0x45, 0x49, 0x13,
538e1051a39Sopenharmony_ci    0xfe, 0x2e, 0xa8, 0xf2
539e1051a39Sopenharmony_ci};
540e1051a39Sopenharmony_ci
541e1051a39Sopenharmony_cistatic const unsigned char gcmResetTag2[] = {
542e1051a39Sopenharmony_ci    0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53,
543e1051a39Sopenharmony_ci    0xbb, 0x2d, 0x55, 0x1b
544e1051a39Sopenharmony_ci};
545e1051a39Sopenharmony_ci
546e1051a39Sopenharmony_citypedef struct APK_DATA_st {
547e1051a39Sopenharmony_ci    const unsigned char *kder;
548e1051a39Sopenharmony_ci    size_t size;
549e1051a39Sopenharmony_ci    const char *keytype;
550e1051a39Sopenharmony_ci    int evptype;
551e1051a39Sopenharmony_ci    int check;
552e1051a39Sopenharmony_ci    int pub_check;
553e1051a39Sopenharmony_ci    int param_check;
554e1051a39Sopenharmony_ci    int type; /* 0 for private, 1 for public, 2 for params */
555e1051a39Sopenharmony_ci} APK_DATA;
556e1051a39Sopenharmony_ci
557e1051a39Sopenharmony_cistatic APK_DATA keydata[] = {
558e1051a39Sopenharmony_ci    {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA},
559e1051a39Sopenharmony_ci    {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), "RSA", EVP_PKEY_RSA},
560e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
561e1051a39Sopenharmony_ci    {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC}
562e1051a39Sopenharmony_ci#endif
563e1051a39Sopenharmony_ci};
564e1051a39Sopenharmony_ci
565e1051a39Sopenharmony_cistatic APK_DATA keycheckdata[] = {
566e1051a39Sopenharmony_ci    {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), "RSA", EVP_PKEY_RSA, 1, 1, 1,
567e1051a39Sopenharmony_ci     0},
568e1051a39Sopenharmony_ci    {kExampleBadRSAKeyDER, sizeof(kExampleBadRSAKeyDER), "RSA", EVP_PKEY_RSA,
569e1051a39Sopenharmony_ci     0, 1, 1, 0},
570e1051a39Sopenharmony_ci    {kExampleBad2RSAKeyDER, sizeof(kExampleBad2RSAKeyDER), "RSA", EVP_PKEY_RSA,
571e1051a39Sopenharmony_ci     0, 0, 1 /* Since there are no "params" in an RSA key this passes */, 0},
572e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
573e1051a39Sopenharmony_ci    {kExampleECKeyDER, sizeof(kExampleECKeyDER), "EC", EVP_PKEY_EC, 1, 1, 1, 0},
574e1051a39Sopenharmony_ci    /* group is also associated in our pub key */
575e1051a39Sopenharmony_ci    {kExampleECPubKeyDER, sizeof(kExampleECPubKeyDER), "EC", EVP_PKEY_EC, 0, 1,
576e1051a39Sopenharmony_ci     1, 1},
577e1051a39Sopenharmony_ci    {pExampleECParamDER, sizeof(pExampleECParamDER), "EC", EVP_PKEY_EC, 0, 0, 1,
578e1051a39Sopenharmony_ci     2},
579e1051a39Sopenharmony_ci    {kExampleED25519KeyDER, sizeof(kExampleED25519KeyDER), "ED25519",
580e1051a39Sopenharmony_ci     EVP_PKEY_ED25519, 1, 1, 1, 0},
581e1051a39Sopenharmony_ci    {kExampleED25519PubKeyDER, sizeof(kExampleED25519PubKeyDER), "ED25519",
582e1051a39Sopenharmony_ci     EVP_PKEY_ED25519, 0, 1, 1, 1},
583e1051a39Sopenharmony_ci#endif
584e1051a39Sopenharmony_ci};
585e1051a39Sopenharmony_ci
586e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_key(const char *keytype,
587e1051a39Sopenharmony_ci                                  const unsigned char *data, size_t data_len)
588e1051a39Sopenharmony_ci{
589e1051a39Sopenharmony_ci    const unsigned char **pdata = &data;
590e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
591e1051a39Sopenharmony_ci    OSSL_DECODER_CTX *dctx =
592e1051a39Sopenharmony_ci        OSSL_DECODER_CTX_new_for_pkey(&pkey, "DER", NULL, keytype, 0,
593e1051a39Sopenharmony_ci                                      testctx, testpropq);
594e1051a39Sopenharmony_ci
595e1051a39Sopenharmony_ci    /* |pkey| will be NULL on error */
596e1051a39Sopenharmony_ci    (void)OSSL_DECODER_from_data(dctx, pdata, &data_len);
597e1051a39Sopenharmony_ci    OSSL_DECODER_CTX_free(dctx);
598e1051a39Sopenharmony_ci    return pkey;
599e1051a39Sopenharmony_ci}
600e1051a39Sopenharmony_ci
601e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_rsa_key(void)
602e1051a39Sopenharmony_ci{
603e1051a39Sopenharmony_ci    return load_example_key("RSA", kExampleRSAKeyDER,
604e1051a39Sopenharmony_ci                            sizeof(kExampleRSAKeyDER));
605e1051a39Sopenharmony_ci}
606e1051a39Sopenharmony_ci
607e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
608e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_dsa_key(void)
609e1051a39Sopenharmony_ci{
610e1051a39Sopenharmony_ci    return load_example_key("DSA", kExampleDSAKeyDER,
611e1051a39Sopenharmony_ci                            sizeof(kExampleDSAKeyDER));
612e1051a39Sopenharmony_ci}
613e1051a39Sopenharmony_ci#endif
614e1051a39Sopenharmony_ci
615e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
616e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_ec_key(void)
617e1051a39Sopenharmony_ci{
618e1051a39Sopenharmony_ci    return load_example_key("EC", kExampleECKeyDER,
619e1051a39Sopenharmony_ci                            sizeof(kExampleECKeyDER));
620e1051a39Sopenharmony_ci}
621e1051a39Sopenharmony_ci#endif
622e1051a39Sopenharmony_ci
623e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
624e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
625e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_dh_key(void)
626e1051a39Sopenharmony_ci{
627e1051a39Sopenharmony_ci    return load_example_key("DH", kExampleDHKeyDER,
628e1051a39Sopenharmony_ci                            sizeof(kExampleDHKeyDER));
629e1051a39Sopenharmony_ci}
630e1051a39Sopenharmony_ci# endif
631e1051a39Sopenharmony_ci
632e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
633e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_ed25519_key(void)
634e1051a39Sopenharmony_ci{
635e1051a39Sopenharmony_ci    return load_example_key("ED25519", kExampleED25519KeyDER,
636e1051a39Sopenharmony_ci                            sizeof(kExampleED25519KeyDER));
637e1051a39Sopenharmony_ci}
638e1051a39Sopenharmony_ci
639e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_x25519_key(void)
640e1051a39Sopenharmony_ci{
641e1051a39Sopenharmony_ci    return load_example_key("X25519", kExampleX25519KeyDER,
642e1051a39Sopenharmony_ci                            sizeof(kExampleX25519KeyDER));
643e1051a39Sopenharmony_ci}
644e1051a39Sopenharmony_ci# endif
645e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DEPRECATED_3_0 */
646e1051a39Sopenharmony_ci
647e1051a39Sopenharmony_cistatic EVP_PKEY *load_example_hmac_key(void)
648e1051a39Sopenharmony_ci{
649e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
650e1051a39Sopenharmony_ci    unsigned char key[] = {
651e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
652e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
653e1051a39Sopenharmony_ci        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
654e1051a39Sopenharmony_ci    };
655e1051a39Sopenharmony_ci
656e1051a39Sopenharmony_ci    pkey = EVP_PKEY_new_raw_private_key_ex(testctx, "HMAC",
657e1051a39Sopenharmony_ci                                           NULL, key, sizeof(key));
658e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
659e1051a39Sopenharmony_ci        return NULL;
660e1051a39Sopenharmony_ci
661e1051a39Sopenharmony_ci    return pkey;
662e1051a39Sopenharmony_ci}
663e1051a39Sopenharmony_ci
664e1051a39Sopenharmony_cistatic int test_EVP_set_default_properties(void)
665e1051a39Sopenharmony_ci{
666e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx;
667e1051a39Sopenharmony_ci    EVP_MD *md = NULL;
668e1051a39Sopenharmony_ci    int res = 0;
669e1051a39Sopenharmony_ci
670e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
671e1051a39Sopenharmony_ci            || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
672e1051a39Sopenharmony_ci        goto err;
673e1051a39Sopenharmony_ci    EVP_MD_free(md);
674e1051a39Sopenharmony_ci    md = NULL;
675e1051a39Sopenharmony_ci
676e1051a39Sopenharmony_ci    if (!TEST_true(EVP_set_default_properties(ctx, "provider=fizzbang"))
677e1051a39Sopenharmony_ci            || !TEST_ptr_null(md = EVP_MD_fetch(ctx, "sha256", NULL))
678e1051a39Sopenharmony_ci            || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", "-provider")))
679e1051a39Sopenharmony_ci        goto err;
680e1051a39Sopenharmony_ci    EVP_MD_free(md);
681e1051a39Sopenharmony_ci    md = NULL;
682e1051a39Sopenharmony_ci
683e1051a39Sopenharmony_ci    if (!TEST_true(EVP_set_default_properties(ctx, NULL))
684e1051a39Sopenharmony_ci            || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
685e1051a39Sopenharmony_ci        goto err;
686e1051a39Sopenharmony_ci    res = 1;
687e1051a39Sopenharmony_cierr:
688e1051a39Sopenharmony_ci    EVP_MD_free(md);
689e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(ctx);
690e1051a39Sopenharmony_ci    return res;
691e1051a39Sopenharmony_ci}
692e1051a39Sopenharmony_ci
693e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
694e1051a39Sopenharmony_cistatic EVP_PKEY *make_key_fromdata(char *keytype, OSSL_PARAM *params)
695e1051a39Sopenharmony_ci{
696e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
697e1051a39Sopenharmony_ci    EVP_PKEY *tmp_pkey = NULL, *pkey = NULL;
698e1051a39Sopenharmony_ci
699e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, keytype, testpropq)))
700e1051a39Sopenharmony_ci        goto err;
701e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
702e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &tmp_pkey, EVP_PKEY_KEYPAIR,
703e1051a39Sopenharmony_ci                                          params), 0))
704e1051a39Sopenharmony_ci        goto err;
705e1051a39Sopenharmony_ci
706e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp_pkey))
707e1051a39Sopenharmony_ci        goto err;
708e1051a39Sopenharmony_ci
709e1051a39Sopenharmony_ci    pkey = tmp_pkey;
710e1051a39Sopenharmony_ci    tmp_pkey = NULL;
711e1051a39Sopenharmony_ci err:
712e1051a39Sopenharmony_ci    EVP_PKEY_free(tmp_pkey);
713e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
714e1051a39Sopenharmony_ci    return pkey;
715e1051a39Sopenharmony_ci}
716e1051a39Sopenharmony_ci
717e1051a39Sopenharmony_cistatic int test_selection(EVP_PKEY *pkey, int selection)
718e1051a39Sopenharmony_ci{
719e1051a39Sopenharmony_ci    int testresult = 0;
720e1051a39Sopenharmony_ci    int ret;
721e1051a39Sopenharmony_ci    BIO *bio = BIO_new(BIO_s_mem());
722e1051a39Sopenharmony_ci
723e1051a39Sopenharmony_ci    ret = PEM_write_bio_PUBKEY(bio, pkey);
724e1051a39Sopenharmony_ci    if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
725e1051a39Sopenharmony_ci        if (!TEST_true(ret))
726e1051a39Sopenharmony_ci            goto err;
727e1051a39Sopenharmony_ci    } else {
728e1051a39Sopenharmony_ci        if (!TEST_false(ret))
729e1051a39Sopenharmony_ci            goto err;
730e1051a39Sopenharmony_ci    }
731e1051a39Sopenharmony_ci    ret = PEM_write_bio_PrivateKey_ex(bio, pkey, NULL, NULL, 0, NULL, NULL,
732e1051a39Sopenharmony_ci                                      testctx, NULL);
733e1051a39Sopenharmony_ci    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
734e1051a39Sopenharmony_ci        if (!TEST_true(ret))
735e1051a39Sopenharmony_ci            goto err;
736e1051a39Sopenharmony_ci    } else {
737e1051a39Sopenharmony_ci        if (!TEST_false(ret))
738e1051a39Sopenharmony_ci            goto err;
739e1051a39Sopenharmony_ci    }
740e1051a39Sopenharmony_ci
741e1051a39Sopenharmony_ci    testresult = 1;
742e1051a39Sopenharmony_ci err:
743e1051a39Sopenharmony_ci    BIO_free(bio);
744e1051a39Sopenharmony_ci
745e1051a39Sopenharmony_ci    return testresult;
746e1051a39Sopenharmony_ci}
747e1051a39Sopenharmony_ci#endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA || !OPENSSL_NO_EC */
748e1051a39Sopenharmony_ci
749e1051a39Sopenharmony_ci/*
750e1051a39Sopenharmony_ci * Test combinations of private, public, missing and private + public key
751e1051a39Sopenharmony_ci * params to ensure they are all accepted
752e1051a39Sopenharmony_ci */
753e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA)
754e1051a39Sopenharmony_cistatic int test_EVP_PKEY_ffc_priv_pub(char *keytype)
755e1051a39Sopenharmony_ci{
756e1051a39Sopenharmony_ci    OSSL_PARAM_BLD *bld = NULL;
757e1051a39Sopenharmony_ci    OSSL_PARAM *params = NULL;
758e1051a39Sopenharmony_ci    EVP_PKEY *just_params = NULL;
759e1051a39Sopenharmony_ci    EVP_PKEY *params_and_priv = NULL;
760e1051a39Sopenharmony_ci    EVP_PKEY *params_and_pub = NULL;
761e1051a39Sopenharmony_ci    EVP_PKEY *params_and_keypair = NULL;
762e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
763e1051a39Sopenharmony_ci    int ret = 0;
764e1051a39Sopenharmony_ci
765e1051a39Sopenharmony_ci    /*
766e1051a39Sopenharmony_ci     * Setup the parameters for our pkey object. For our purposes they don't
767e1051a39Sopenharmony_ci     * have to actually be *valid* parameters. We just need to set something.
768e1051a39Sopenharmony_ci     */
769e1051a39Sopenharmony_ci    if (!TEST_ptr(p = BN_new())
770e1051a39Sopenharmony_ci        || !TEST_ptr(q = BN_new())
771e1051a39Sopenharmony_ci        || !TEST_ptr(g = BN_new())
772e1051a39Sopenharmony_ci        || !TEST_ptr(pub = BN_new())
773e1051a39Sopenharmony_ci        || !TEST_ptr(priv = BN_new()))
774e1051a39Sopenharmony_ci        goto err;
775e1051a39Sopenharmony_ci
776e1051a39Sopenharmony_ci    /* Test !priv and !pub */
777e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
778e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
779e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
780e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)))
781e1051a39Sopenharmony_ci        goto err;
782e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
783e1051a39Sopenharmony_ci        || !TEST_ptr(just_params = make_key_fromdata(keytype, params)))
784e1051a39Sopenharmony_ci        goto err;
785e1051a39Sopenharmony_ci
786e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
787e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
788e1051a39Sopenharmony_ci    params = NULL;
789e1051a39Sopenharmony_ci    bld = NULL;
790e1051a39Sopenharmony_ci
791e1051a39Sopenharmony_ci    if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
792e1051a39Sopenharmony_ci        || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
793e1051a39Sopenharmony_ci        goto err;
794e1051a39Sopenharmony_ci
795e1051a39Sopenharmony_ci    /* Test priv and !pub */
796e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
797e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
798e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
799e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
800e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
801e1051a39Sopenharmony_ci                                             priv)))
802e1051a39Sopenharmony_ci        goto err;
803e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
804e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_priv = make_key_fromdata(keytype, params)))
805e1051a39Sopenharmony_ci        goto err;
806e1051a39Sopenharmony_ci
807e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
808e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
809e1051a39Sopenharmony_ci    params = NULL;
810e1051a39Sopenharmony_ci    bld = NULL;
811e1051a39Sopenharmony_ci
812e1051a39Sopenharmony_ci    if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)
813e1051a39Sopenharmony_ci        || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
814e1051a39Sopenharmony_ci        goto err;
815e1051a39Sopenharmony_ci
816e1051a39Sopenharmony_ci    /* Test !priv and pub */
817e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
818e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
819e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
820e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
821e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
822e1051a39Sopenharmony_ci                                             pub)))
823e1051a39Sopenharmony_ci        goto err;
824e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
825e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_pub = make_key_fromdata(keytype, params)))
826e1051a39Sopenharmony_ci        goto err;
827e1051a39Sopenharmony_ci
828e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
829e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
830e1051a39Sopenharmony_ci    params = NULL;
831e1051a39Sopenharmony_ci    bld = NULL;
832e1051a39Sopenharmony_ci
833e1051a39Sopenharmony_ci    if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
834e1051a39Sopenharmony_ci        || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
835e1051a39Sopenharmony_ci        goto err;
836e1051a39Sopenharmony_ci
837e1051a39Sopenharmony_ci    /* Test priv and pub */
838e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
839e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
840e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
841e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
842e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
843e1051a39Sopenharmony_ci                                             pub))
844e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
845e1051a39Sopenharmony_ci                                             priv)))
846e1051a39Sopenharmony_ci        goto err;
847e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
848e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_keypair = make_key_fromdata(keytype, params)))
849e1051a39Sopenharmony_ci        goto err;
850e1051a39Sopenharmony_ci
851e1051a39Sopenharmony_ci    if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
852e1051a39Sopenharmony_ci        goto err;
853e1051a39Sopenharmony_ci
854e1051a39Sopenharmony_ci    ret = 1;
855e1051a39Sopenharmony_ci err:
856e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
857e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
858e1051a39Sopenharmony_ci    EVP_PKEY_free(just_params);
859e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_priv);
860e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_pub);
861e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_keypair);
862e1051a39Sopenharmony_ci    BN_free(p);
863e1051a39Sopenharmony_ci    BN_free(q);
864e1051a39Sopenharmony_ci    BN_free(g);
865e1051a39Sopenharmony_ci    BN_free(pub);
866e1051a39Sopenharmony_ci    BN_free(priv);
867e1051a39Sopenharmony_ci
868e1051a39Sopenharmony_ci    return ret;
869e1051a39Sopenharmony_ci}
870e1051a39Sopenharmony_ci#endif /* !OPENSSL_NO_DH || !OPENSSL_NO_DSA */
871e1051a39Sopenharmony_ci
872e1051a39Sopenharmony_ci/*
873e1051a39Sopenharmony_ci * Test combinations of private, public, missing and private + public key
874e1051a39Sopenharmony_ci * params to ensure they are all accepted for EC keys
875e1051a39Sopenharmony_ci */
876e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
877e1051a39Sopenharmony_cistatic unsigned char ec_priv[] = {
878e1051a39Sopenharmony_ci    0xe9, 0x25, 0xf7, 0x66, 0x58, 0xa4, 0xdd, 0x99, 0x61, 0xe7, 0xe8, 0x23,
879e1051a39Sopenharmony_ci    0x85, 0xc2, 0xe8, 0x33, 0x27, 0xc5, 0x5c, 0xeb, 0xdb, 0x43, 0x9f, 0xd5,
880e1051a39Sopenharmony_ci    0xf2, 0x5a, 0x75, 0x55, 0xd0, 0x2e, 0x6d, 0x16
881e1051a39Sopenharmony_ci};
882e1051a39Sopenharmony_cistatic unsigned char ec_pub[] = {
883e1051a39Sopenharmony_ci    0x04, 0xad, 0x11, 0x90, 0x77, 0x4b, 0x46, 0xee, 0x72, 0x51, 0x15, 0x97,
884e1051a39Sopenharmony_ci    0x4a, 0x6a, 0xa7, 0xaf, 0x59, 0xfa, 0x4b, 0xf2, 0x41, 0xc8, 0x3a, 0x81,
885e1051a39Sopenharmony_ci    0x23, 0xb6, 0x90, 0x04, 0x6c, 0x67, 0x66, 0xd0, 0xdc, 0xf2, 0x15, 0x1d,
886e1051a39Sopenharmony_ci    0x41, 0x61, 0xb7, 0x95, 0x85, 0x38, 0x5a, 0x84, 0x56, 0xe8, 0xb3, 0x0e,
887e1051a39Sopenharmony_ci    0xf5, 0xc6, 0x5d, 0xa4, 0x54, 0x26, 0xb0, 0xf7, 0xa5, 0x4a, 0x33, 0xf1,
888e1051a39Sopenharmony_ci    0x08, 0x09, 0xb8, 0xdb, 0x03
889e1051a39Sopenharmony_ci};
890e1051a39Sopenharmony_ci
891e1051a39Sopenharmony_cistatic int test_EC_priv_pub(void)
892e1051a39Sopenharmony_ci{
893e1051a39Sopenharmony_ci    OSSL_PARAM_BLD *bld = NULL;
894e1051a39Sopenharmony_ci    OSSL_PARAM *params = NULL;
895e1051a39Sopenharmony_ci    EVP_PKEY *just_params = NULL;
896e1051a39Sopenharmony_ci    EVP_PKEY *params_and_priv = NULL;
897e1051a39Sopenharmony_ci    EVP_PKEY *params_and_pub = NULL;
898e1051a39Sopenharmony_ci    EVP_PKEY *params_and_keypair = NULL;
899e1051a39Sopenharmony_ci    BIGNUM *priv = NULL;
900e1051a39Sopenharmony_ci    int ret = 0;
901e1051a39Sopenharmony_ci    unsigned char *encoded = NULL;
902e1051a39Sopenharmony_ci    size_t len = 0;
903e1051a39Sopenharmony_ci    unsigned char buffer[128];
904e1051a39Sopenharmony_ci
905e1051a39Sopenharmony_ci    /*
906e1051a39Sopenharmony_ci     * Setup the parameters for our pkey object. For our purposes they don't
907e1051a39Sopenharmony_ci     * have to actually be *valid* parameters. We just need to set something.
908e1051a39Sopenharmony_ci     */
909e1051a39Sopenharmony_ci    if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
910e1051a39Sopenharmony_ci        goto err;
911e1051a39Sopenharmony_ci
912e1051a39Sopenharmony_ci    /* Test !priv and !pub */
913e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
914e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
915e1051a39Sopenharmony_ci                                                      OSSL_PKEY_PARAM_GROUP_NAME,
916e1051a39Sopenharmony_ci                                                      "P-256", 0)))
917e1051a39Sopenharmony_ci        goto err;
918e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
919e1051a39Sopenharmony_ci        || !TEST_ptr(just_params = make_key_fromdata("EC", params)))
920e1051a39Sopenharmony_ci        goto err;
921e1051a39Sopenharmony_ci
922e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
923e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
924e1051a39Sopenharmony_ci    params = NULL;
925e1051a39Sopenharmony_ci    bld = NULL;
926e1051a39Sopenharmony_ci
927e1051a39Sopenharmony_ci    if (!test_selection(just_params, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
928e1051a39Sopenharmony_ci        || test_selection(just_params, OSSL_KEYMGMT_SELECT_KEYPAIR))
929e1051a39Sopenharmony_ci        goto err;
930e1051a39Sopenharmony_ci
931e1051a39Sopenharmony_ci    /* Test priv and !pub */
932e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
933e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
934e1051a39Sopenharmony_ci                                                      OSSL_PKEY_PARAM_GROUP_NAME,
935e1051a39Sopenharmony_ci                                                      "P-256", 0))
936e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
937e1051a39Sopenharmony_ci                                             priv)))
938e1051a39Sopenharmony_ci        goto err;
939e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
940e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_priv = make_key_fromdata("EC", params)))
941e1051a39Sopenharmony_ci        goto err;
942e1051a39Sopenharmony_ci
943e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
944e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
945e1051a39Sopenharmony_ci    params = NULL;
946e1051a39Sopenharmony_ci    bld = NULL;
947e1051a39Sopenharmony_ci
948e1051a39Sopenharmony_ci    /*
949e1051a39Sopenharmony_ci     * We indicate only parameters here, in spite of having built a key that
950e1051a39Sopenharmony_ci     * has a private part, because the PEM_write_bio_PrivateKey_ex call is
951e1051a39Sopenharmony_ci     * expected to fail because it does not support exporting a private EC
952e1051a39Sopenharmony_ci     * key without a corresponding public key
953e1051a39Sopenharmony_ci     */
954e1051a39Sopenharmony_ci    if (!test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
955e1051a39Sopenharmony_ci        || test_selection(params_and_priv, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
956e1051a39Sopenharmony_ci        goto err;
957e1051a39Sopenharmony_ci
958e1051a39Sopenharmony_ci    /* Test !priv and pub */
959e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
960e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
961e1051a39Sopenharmony_ci                                                      OSSL_PKEY_PARAM_GROUP_NAME,
962e1051a39Sopenharmony_ci                                                      "P-256", 0))
963e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
964e1051a39Sopenharmony_ci                                                       OSSL_PKEY_PARAM_PUB_KEY,
965e1051a39Sopenharmony_ci                                                       ec_pub, sizeof(ec_pub))))
966e1051a39Sopenharmony_ci        goto err;
967e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
968e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_pub = make_key_fromdata("EC", params)))
969e1051a39Sopenharmony_ci        goto err;
970e1051a39Sopenharmony_ci
971e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
972e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
973e1051a39Sopenharmony_ci    params = NULL;
974e1051a39Sopenharmony_ci    bld = NULL;
975e1051a39Sopenharmony_ci
976e1051a39Sopenharmony_ci    if (!test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
977e1051a39Sopenharmony_ci        || test_selection(params_and_pub, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
978e1051a39Sopenharmony_ci        goto err;
979e1051a39Sopenharmony_ci
980e1051a39Sopenharmony_ci    /* Test priv and pub */
981e1051a39Sopenharmony_ci    if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
982e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
983e1051a39Sopenharmony_ci                                                      OSSL_PKEY_PARAM_GROUP_NAME,
984e1051a39Sopenharmony_ci                                                      "P-256", 0))
985e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
986e1051a39Sopenharmony_ci                                                       OSSL_PKEY_PARAM_PUB_KEY,
987e1051a39Sopenharmony_ci                                                       ec_pub, sizeof(ec_pub)))
988e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
989e1051a39Sopenharmony_ci                                             priv)))
990e1051a39Sopenharmony_ci        goto err;
991e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
992e1051a39Sopenharmony_ci        || !TEST_ptr(params_and_keypair = make_key_fromdata("EC", params)))
993e1051a39Sopenharmony_ci        goto err;
994e1051a39Sopenharmony_ci
995e1051a39Sopenharmony_ci    if (!test_selection(params_and_keypair, EVP_PKEY_KEYPAIR))
996e1051a39Sopenharmony_ci        goto err;
997e1051a39Sopenharmony_ci
998e1051a39Sopenharmony_ci    /* Try key equality */
999e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_parameters_eq(just_params, just_params), 0)
1000e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_pub),
1001e1051a39Sopenharmony_ci                        0)
1002e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_priv),
1003e1051a39Sopenharmony_ci                        0)
1004e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_parameters_eq(just_params, params_and_keypair),
1005e1051a39Sopenharmony_ci                        0)
1006e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_eq(params_and_pub, params_and_pub), 0)
1007e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_eq(params_and_priv, params_and_priv), 0)
1008e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_pub), 0)
1009e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_eq(params_and_keypair, params_and_priv), 0))
1010e1051a39Sopenharmony_ci        goto err;
1011e1051a39Sopenharmony_ci
1012e1051a39Sopenharmony_ci    /* Positive and negative testcase for EVP_PKEY_get1_encoded_public_key */
1013e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_get1_encoded_public_key(params_and_pub, &encoded), 0))
1014e1051a39Sopenharmony_ci        goto err;
1015e1051a39Sopenharmony_ci    OPENSSL_free(encoded);
1016e1051a39Sopenharmony_ci    encoded = NULL;
1017e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_get1_encoded_public_key(just_params, &encoded), 0)) {
1018e1051a39Sopenharmony_ci        OPENSSL_free(encoded);
1019e1051a39Sopenharmony_ci        encoded = NULL;
1020e1051a39Sopenharmony_ci        goto err;
1021e1051a39Sopenharmony_ci    }
1022e1051a39Sopenharmony_ci
1023e1051a39Sopenharmony_ci    /* Positive and negative testcase for EVP_PKEY_get_octet_string_param */
1024e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
1025e1051a39Sopenharmony_ci                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1026e1051a39Sopenharmony_ci                                                     buffer, sizeof(buffer), &len), 1)
1027e1051a39Sopenharmony_ci        || !TEST_int_eq(len, 65))
1028e1051a39Sopenharmony_ci        goto err;
1029e1051a39Sopenharmony_ci
1030e1051a39Sopenharmony_ci    len = 0;
1031e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
1032e1051a39Sopenharmony_ci                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1033e1051a39Sopenharmony_ci                                                     NULL, 0, &len), 1)
1034e1051a39Sopenharmony_ci        || !TEST_int_eq(len, 65))
1035e1051a39Sopenharmony_ci        goto err;
1036e1051a39Sopenharmony_ci
1037e1051a39Sopenharmony_ci    /* too-short buffer len*/
1038e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
1039e1051a39Sopenharmony_ci                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1040e1051a39Sopenharmony_ci                                                     buffer, 10, &len), 0))
1041e1051a39Sopenharmony_ci        goto err;
1042e1051a39Sopenharmony_ci
1043e1051a39Sopenharmony_ci    ret = 1;
1044e1051a39Sopenharmony_ci err:
1045e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
1046e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
1047e1051a39Sopenharmony_ci    EVP_PKEY_free(just_params);
1048e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_priv);
1049e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_pub);
1050e1051a39Sopenharmony_ci    EVP_PKEY_free(params_and_keypair);
1051e1051a39Sopenharmony_ci    BN_free(priv);
1052e1051a39Sopenharmony_ci
1053e1051a39Sopenharmony_ci    return ret;
1054e1051a39Sopenharmony_ci}
1055e1051a39Sopenharmony_ci
1056e1051a39Sopenharmony_ci/* Test that using a legacy EC key with only a private key in it works */
1057e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
1058e1051a39Sopenharmony_cistatic int test_EC_priv_only_legacy(void)
1059e1051a39Sopenharmony_ci{
1060e1051a39Sopenharmony_ci    BIGNUM *priv = NULL;
1061e1051a39Sopenharmony_ci    int ret = 0;
1062e1051a39Sopenharmony_ci    EC_KEY *eckey = NULL;
1063e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL, *dup_pk = NULL;
1064e1051a39Sopenharmony_ci    EVP_MD_CTX *ctx = NULL;
1065e1051a39Sopenharmony_ci
1066e1051a39Sopenharmony_ci    /* Create the low level EC_KEY */
1067e1051a39Sopenharmony_ci    if (!TEST_ptr(priv = BN_bin2bn(ec_priv, sizeof(ec_priv), NULL)))
1068e1051a39Sopenharmony_ci        goto err;
1069e1051a39Sopenharmony_ci
1070e1051a39Sopenharmony_ci    eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1071e1051a39Sopenharmony_ci    if (!TEST_ptr(eckey))
1072e1051a39Sopenharmony_ci        goto err;
1073e1051a39Sopenharmony_ci
1074e1051a39Sopenharmony_ci    if (!TEST_true(EC_KEY_set_private_key(eckey, priv)))
1075e1051a39Sopenharmony_ci        goto err;
1076e1051a39Sopenharmony_ci
1077e1051a39Sopenharmony_ci    pkey = EVP_PKEY_new();
1078e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
1079e1051a39Sopenharmony_ci        goto err;
1080e1051a39Sopenharmony_ci
1081e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey)))
1082e1051a39Sopenharmony_ci        goto err;
1083e1051a39Sopenharmony_ci    eckey = NULL;
1084e1051a39Sopenharmony_ci
1085e1051a39Sopenharmony_ci    while (dup_pk == NULL) {
1086e1051a39Sopenharmony_ci        ret = 0;
1087e1051a39Sopenharmony_ci        ctx = EVP_MD_CTX_new();
1088e1051a39Sopenharmony_ci        if (!TEST_ptr(ctx))
1089e1051a39Sopenharmony_ci            goto err;
1090e1051a39Sopenharmony_ci
1091e1051a39Sopenharmony_ci        /*
1092e1051a39Sopenharmony_ci         * The EVP_DigestSignInit function should create the key on the
1093e1051a39Sopenharmony_ci         * provider side which is sufficient for this test.
1094e1051a39Sopenharmony_ci         */
1095e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignInit_ex(ctx, NULL, NULL, testctx,
1096e1051a39Sopenharmony_ci                                             testpropq, pkey, NULL)))
1097e1051a39Sopenharmony_ci            goto err;
1098e1051a39Sopenharmony_ci        EVP_MD_CTX_free(ctx);
1099e1051a39Sopenharmony_ci        ctx = NULL;
1100e1051a39Sopenharmony_ci
1101e1051a39Sopenharmony_ci        if (!TEST_ptr(dup_pk = EVP_PKEY_dup(pkey)))
1102e1051a39Sopenharmony_ci            goto err;
1103e1051a39Sopenharmony_ci        /* EVP_PKEY_eq() returns -2 with missing public keys */
1104e1051a39Sopenharmony_ci        ret = TEST_int_eq(EVP_PKEY_eq(pkey, dup_pk), -2);
1105e1051a39Sopenharmony_ci        EVP_PKEY_free(pkey);
1106e1051a39Sopenharmony_ci        pkey = dup_pk;
1107e1051a39Sopenharmony_ci        if (!ret)
1108e1051a39Sopenharmony_ci            goto err;
1109e1051a39Sopenharmony_ci    }
1110e1051a39Sopenharmony_ci
1111e1051a39Sopenharmony_ci err:
1112e1051a39Sopenharmony_ci    EVP_MD_CTX_free(ctx);
1113e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1114e1051a39Sopenharmony_ci    EC_KEY_free(eckey);
1115e1051a39Sopenharmony_ci    BN_free(priv);
1116e1051a39Sopenharmony_ci
1117e1051a39Sopenharmony_ci    return ret;
1118e1051a39Sopenharmony_ci}
1119e1051a39Sopenharmony_ci# endif /* OPENSSL_NO_DEPRECATED_3_0 */
1120e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_EC */
1121e1051a39Sopenharmony_ci
1122e1051a39Sopenharmony_cistatic int test_EVP_PKEY_sign(int tst)
1123e1051a39Sopenharmony_ci{
1124e1051a39Sopenharmony_ci    int ret = 0;
1125e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1126e1051a39Sopenharmony_ci    unsigned char *sig = NULL;
1127e1051a39Sopenharmony_ci    size_t sig_len = 0, shortsig_len = 1;
1128e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
1129e1051a39Sopenharmony_ci    unsigned char tbs[] = {
1130e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
1131e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
1132e1051a39Sopenharmony_ci    };
1133e1051a39Sopenharmony_ci
1134e1051a39Sopenharmony_ci    if (tst == 0 ) {
1135e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_rsa_key()))
1136e1051a39Sopenharmony_ci                goto out;
1137e1051a39Sopenharmony_ci    } else if (tst == 1) {
1138e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
1139e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_dsa_key()))
1140e1051a39Sopenharmony_ci                goto out;
1141e1051a39Sopenharmony_ci#else
1142e1051a39Sopenharmony_ci        ret = 1;
1143e1051a39Sopenharmony_ci        goto out;
1144e1051a39Sopenharmony_ci#endif
1145e1051a39Sopenharmony_ci    } else {
1146e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
1147e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_ec_key()))
1148e1051a39Sopenharmony_ci                goto out;
1149e1051a39Sopenharmony_ci#else
1150e1051a39Sopenharmony_ci        ret = 1;
1151e1051a39Sopenharmony_ci        goto out;
1152e1051a39Sopenharmony_ci#endif
1153e1051a39Sopenharmony_ci    }
1154e1051a39Sopenharmony_ci
1155e1051a39Sopenharmony_ci    ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, NULL);
1156e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx)
1157e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0)
1158e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_sign(ctx, NULL, &sig_len, tbs,
1159e1051a39Sopenharmony_ci                                          sizeof(tbs)), 0))
1160e1051a39Sopenharmony_ci        goto out;
1161e1051a39Sopenharmony_ci    sig = OPENSSL_malloc(sig_len);
1162e1051a39Sopenharmony_ci    if (!TEST_ptr(sig)
1163e1051a39Sopenharmony_ci            /* Test sending a signature buffer that is too short is rejected */
1164e1051a39Sopenharmony_ci            || !TEST_int_le(EVP_PKEY_sign(ctx, sig, &shortsig_len, tbs,
1165e1051a39Sopenharmony_ci                                          sizeof(tbs)), 0)
1166e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_sign(ctx, sig, &sig_len, tbs, sizeof(tbs)),
1167e1051a39Sopenharmony_ci                            0)
1168e1051a39Sopenharmony_ci            /* Test the signature round-trips */
1169e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_verify_init(ctx), 0)
1170e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_verify(ctx, sig, sig_len, tbs, sizeof(tbs)),
1171e1051a39Sopenharmony_ci                            0))
1172e1051a39Sopenharmony_ci        goto out;
1173e1051a39Sopenharmony_ci
1174e1051a39Sopenharmony_ci    ret = 1;
1175e1051a39Sopenharmony_ci out:
1176e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
1177e1051a39Sopenharmony_ci    OPENSSL_free(sig);
1178e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1179e1051a39Sopenharmony_ci    return ret;
1180e1051a39Sopenharmony_ci}
1181e1051a39Sopenharmony_ci
1182e1051a39Sopenharmony_ci/*
1183e1051a39Sopenharmony_ci * n = 0 => test using legacy cipher
1184e1051a39Sopenharmony_ci * n = 1 => test using fetched cipher
1185e1051a39Sopenharmony_ci */
1186e1051a39Sopenharmony_cistatic int test_EVP_Enveloped(int n)
1187e1051a39Sopenharmony_ci{
1188e1051a39Sopenharmony_ci    int ret = 0;
1189e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
1190e1051a39Sopenharmony_ci    EVP_PKEY *keypair = NULL;
1191e1051a39Sopenharmony_ci    unsigned char *kek = NULL;
1192e1051a39Sopenharmony_ci    unsigned char iv[EVP_MAX_IV_LENGTH];
1193e1051a39Sopenharmony_ci    static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
1194e1051a39Sopenharmony_ci    int len, kek_len, ciphertext_len, plaintext_len;
1195e1051a39Sopenharmony_ci    unsigned char ciphertext[32], plaintext[16];
1196e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
1197e1051a39Sopenharmony_ci
1198e1051a39Sopenharmony_ci    if (nullprov != NULL)
1199e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
1200e1051a39Sopenharmony_ci
1201e1051a39Sopenharmony_ci    if (n == 0)
1202e1051a39Sopenharmony_ci        type = (EVP_CIPHER *)EVP_aes_256_cbc();
1203e1051a39Sopenharmony_ci    else if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "AES-256-CBC",
1204e1051a39Sopenharmony_ci                                               testpropq)))
1205e1051a39Sopenharmony_ci        goto err;
1206e1051a39Sopenharmony_ci
1207e1051a39Sopenharmony_ci    if (!TEST_ptr(keypair = load_example_rsa_key())
1208e1051a39Sopenharmony_ci            || !TEST_ptr(kek = OPENSSL_zalloc(EVP_PKEY_get_size(keypair)))
1209e1051a39Sopenharmony_ci            || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
1210e1051a39Sopenharmony_ci            || !TEST_true(EVP_SealInit(ctx, type, &kek, &kek_len, iv,
1211e1051a39Sopenharmony_ci                                       &keypair, 1))
1212e1051a39Sopenharmony_ci            || !TEST_true(EVP_SealUpdate(ctx, ciphertext, &ciphertext_len,
1213e1051a39Sopenharmony_ci                                         msg, sizeof(msg)))
1214e1051a39Sopenharmony_ci            || !TEST_true(EVP_SealFinal(ctx, ciphertext + ciphertext_len,
1215e1051a39Sopenharmony_ci                                        &len)))
1216e1051a39Sopenharmony_ci        goto err;
1217e1051a39Sopenharmony_ci
1218e1051a39Sopenharmony_ci    ciphertext_len += len;
1219e1051a39Sopenharmony_ci
1220e1051a39Sopenharmony_ci    if (!TEST_true(EVP_OpenInit(ctx, type, kek, kek_len, iv, keypair))
1221e1051a39Sopenharmony_ci            || !TEST_true(EVP_OpenUpdate(ctx, plaintext, &plaintext_len,
1222e1051a39Sopenharmony_ci                                         ciphertext, ciphertext_len))
1223e1051a39Sopenharmony_ci            || !TEST_true(EVP_OpenFinal(ctx, plaintext + plaintext_len, &len)))
1224e1051a39Sopenharmony_ci        goto err;
1225e1051a39Sopenharmony_ci
1226e1051a39Sopenharmony_ci    plaintext_len += len;
1227e1051a39Sopenharmony_ci    if (!TEST_mem_eq(msg, sizeof(msg), plaintext, plaintext_len))
1228e1051a39Sopenharmony_ci        goto err;
1229e1051a39Sopenharmony_ci
1230e1051a39Sopenharmony_ci    ret = 1;
1231e1051a39Sopenharmony_cierr:
1232e1051a39Sopenharmony_ci    if (n != 0)
1233e1051a39Sopenharmony_ci        EVP_CIPHER_free(type);
1234e1051a39Sopenharmony_ci    OPENSSL_free(kek);
1235e1051a39Sopenharmony_ci    EVP_PKEY_free(keypair);
1236e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
1237e1051a39Sopenharmony_ci    return ret;
1238e1051a39Sopenharmony_ci}
1239e1051a39Sopenharmony_ci
1240e1051a39Sopenharmony_ci/*
1241e1051a39Sopenharmony_ci * Test 0: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, RSA)
1242e1051a39Sopenharmony_ci * Test 1: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, DSA)
1243e1051a39Sopenharmony_ci * Test 2: Standard calls to EVP_DigestSignInit/Update/Final (Implicit fetch digest, HMAC)
1244e1051a39Sopenharmony_ci * Test 3: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, RSA)
1245e1051a39Sopenharmony_ci * Test 4: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch digest, DSA)
1246e1051a39Sopenharmony_ci * Test 5: Standard calls to EVP_DigestSignInit/Update/Final (Explicit fetch diegst, HMAC)
1247e1051a39Sopenharmony_ci * Test 6: Use an MD BIO to do the Update calls instead (RSA)
1248e1051a39Sopenharmony_ci * Test 7: Use an MD BIO to do the Update calls instead (DSA)
1249e1051a39Sopenharmony_ci * Test 8: Use an MD BIO to do the Update calls instead (HMAC)
1250e1051a39Sopenharmony_ci * Test 9: Use EVP_DigestSign (Implicit fetch digest, RSA, short sig)
1251e1051a39Sopenharmony_ci * Test 10: Use EVP_DigestSign (Implicit fetch digest, DSA, short sig)
1252e1051a39Sopenharmony_ci * Test 11: Use EVP_DigestSign (Implicit fetch digest, HMAC, short sig)
1253e1051a39Sopenharmony_ci * Test 12: Use EVP_DigestSign (Implicit fetch digest, RSA)
1254e1051a39Sopenharmony_ci * Test 13: Use EVP_DigestSign (Implicit fetch digest, DSA)
1255e1051a39Sopenharmony_ci * Test 14: Use EVP_DigestSign (Implicit fetch digest, HMAC)
1256e1051a39Sopenharmony_ci * Test 15-29: Same as above with reinitialization
1257e1051a39Sopenharmony_ci */
1258e1051a39Sopenharmony_cistatic int test_EVP_DigestSignInit(int tst)
1259e1051a39Sopenharmony_ci{
1260e1051a39Sopenharmony_ci    int ret = 0;
1261e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1262e1051a39Sopenharmony_ci    unsigned char *sig = NULL, *sig2 = NULL;
1263e1051a39Sopenharmony_ci    size_t sig_len = 0, sig2_len = 0, shortsig_len = 1;
1264e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx = NULL, *md_ctx_verify = NULL;
1265e1051a39Sopenharmony_ci    EVP_MD_CTX *a_md_ctx = NULL, *a_md_ctx_verify = NULL;
1266e1051a39Sopenharmony_ci    BIO *mdbio = NULL, *membio = NULL;
1267e1051a39Sopenharmony_ci    size_t written;
1268e1051a39Sopenharmony_ci    const EVP_MD *md;
1269e1051a39Sopenharmony_ci    EVP_MD *mdexp = NULL;
1270e1051a39Sopenharmony_ci    int reinit = 0;
1271e1051a39Sopenharmony_ci
1272e1051a39Sopenharmony_ci    if (nullprov != NULL)
1273e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
1274e1051a39Sopenharmony_ci
1275e1051a39Sopenharmony_ci    if (tst >= 15) {
1276e1051a39Sopenharmony_ci        reinit = 1;
1277e1051a39Sopenharmony_ci        tst -= 15;
1278e1051a39Sopenharmony_ci    }
1279e1051a39Sopenharmony_ci
1280e1051a39Sopenharmony_ci    if (tst >= 6 && tst <= 8) {
1281e1051a39Sopenharmony_ci        membio = BIO_new(BIO_s_mem());
1282e1051a39Sopenharmony_ci        mdbio = BIO_new(BIO_f_md());
1283e1051a39Sopenharmony_ci        if (!TEST_ptr(membio) || !TEST_ptr(mdbio))
1284e1051a39Sopenharmony_ci            goto out;
1285e1051a39Sopenharmony_ci        BIO_push(mdbio, membio);
1286e1051a39Sopenharmony_ci        if (!TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx), 0))
1287e1051a39Sopenharmony_ci            goto out;
1288e1051a39Sopenharmony_ci    } else {
1289e1051a39Sopenharmony_ci        if (!TEST_ptr(a_md_ctx = md_ctx = EVP_MD_CTX_new())
1290e1051a39Sopenharmony_ci                || !TEST_ptr(a_md_ctx_verify = md_ctx_verify = EVP_MD_CTX_new()))
1291e1051a39Sopenharmony_ci            goto out;
1292e1051a39Sopenharmony_ci    }
1293e1051a39Sopenharmony_ci
1294e1051a39Sopenharmony_ci    if (tst % 3 == 0) {
1295e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_rsa_key()))
1296e1051a39Sopenharmony_ci                goto out;
1297e1051a39Sopenharmony_ci    } else if (tst % 3 == 1) {
1298e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
1299e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_dsa_key()))
1300e1051a39Sopenharmony_ci                goto out;
1301e1051a39Sopenharmony_ci#else
1302e1051a39Sopenharmony_ci        ret = 1;
1303e1051a39Sopenharmony_ci        goto out;
1304e1051a39Sopenharmony_ci#endif
1305e1051a39Sopenharmony_ci    } else {
1306e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey = load_example_hmac_key()))
1307e1051a39Sopenharmony_ci                goto out;
1308e1051a39Sopenharmony_ci    }
1309e1051a39Sopenharmony_ci
1310e1051a39Sopenharmony_ci    if (tst >= 3 && tst <= 5)
1311e1051a39Sopenharmony_ci        md = mdexp = EVP_MD_fetch(NULL, "SHA256", NULL);
1312e1051a39Sopenharmony_ci    else
1313e1051a39Sopenharmony_ci        md = EVP_sha256();
1314e1051a39Sopenharmony_ci
1315e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, md, NULL, pkey)))
1316e1051a39Sopenharmony_ci        goto out;
1317e1051a39Sopenharmony_ci
1318e1051a39Sopenharmony_ci    if (reinit && !TEST_true(EVP_DigestSignInit(md_ctx, NULL, NULL, NULL, NULL)))
1319e1051a39Sopenharmony_ci        goto out;
1320e1051a39Sopenharmony_ci
1321e1051a39Sopenharmony_ci    if (tst >= 6 && tst <= 8) {
1322e1051a39Sopenharmony_ci        if (!BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written))
1323e1051a39Sopenharmony_ci            goto out;
1324e1051a39Sopenharmony_ci    } else if (tst < 6) {
1325e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
1326e1051a39Sopenharmony_ci            goto out;
1327e1051a39Sopenharmony_ci    }
1328e1051a39Sopenharmony_ci
1329e1051a39Sopenharmony_ci    if (tst >= 9) {
1330e1051a39Sopenharmony_ci        /* Determine the size of the signature. */
1331e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSign(md_ctx, NULL, &sig_len, kMsg,
1332e1051a39Sopenharmony_ci                                      sizeof(kMsg)))
1333e1051a39Sopenharmony_ci                || !TEST_ptr(sig = OPENSSL_malloc(sig_len)))
1334e1051a39Sopenharmony_ci            goto out;
1335e1051a39Sopenharmony_ci        if (tst <= 11) {
1336e1051a39Sopenharmony_ci            /* Test that supply a short sig buffer fails */
1337e1051a39Sopenharmony_ci            if (!TEST_false(EVP_DigestSign(md_ctx, sig, &shortsig_len, kMsg,
1338e1051a39Sopenharmony_ci                                           sizeof(kMsg))))
1339e1051a39Sopenharmony_ci                goto out;
1340e1051a39Sopenharmony_ci            /*
1341e1051a39Sopenharmony_ci             * We end here because once EVP_DigestSign() has failed you should
1342e1051a39Sopenharmony_ci             * not call it again without re-initing the ctx
1343e1051a39Sopenharmony_ci             */
1344e1051a39Sopenharmony_ci            ret = 1;
1345e1051a39Sopenharmony_ci            goto out;
1346e1051a39Sopenharmony_ci        }
1347e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSign(md_ctx, sig, &sig_len, kMsg,
1348e1051a39Sopenharmony_ci                                      sizeof(kMsg))))
1349e1051a39Sopenharmony_ci            goto out;
1350e1051a39Sopenharmony_ci    } else {
1351e1051a39Sopenharmony_ci        /* Determine the size of the signature. */
1352e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len))
1353e1051a39Sopenharmony_ci                || !TEST_ptr(sig = OPENSSL_malloc(sig_len))
1354e1051a39Sopenharmony_ci                /*
1355e1051a39Sopenharmony_ci                    * Trying to create a signature with a deliberately short
1356e1051a39Sopenharmony_ci                    * buffer should fail.
1357e1051a39Sopenharmony_ci                    */
1358e1051a39Sopenharmony_ci                || !TEST_false(EVP_DigestSignFinal(md_ctx, sig, &shortsig_len))
1359e1051a39Sopenharmony_ci                || !TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
1360e1051a39Sopenharmony_ci            goto out;
1361e1051a39Sopenharmony_ci    }
1362e1051a39Sopenharmony_ci
1363e1051a39Sopenharmony_ci    /*
1364e1051a39Sopenharmony_ci     * Ensure that the signature round-trips (Verification isn't supported for
1365e1051a39Sopenharmony_ci     * HMAC via EVP_DigestVerify*)
1366e1051a39Sopenharmony_ci     */
1367e1051a39Sopenharmony_ci    if (tst % 3 != 2) {
1368e1051a39Sopenharmony_ci        if (tst >= 6 && tst <= 8) {
1369e1051a39Sopenharmony_ci            if (!TEST_int_gt(BIO_reset(mdbio), 0)
1370e1051a39Sopenharmony_ci                || !TEST_int_gt(BIO_get_md_ctx(mdbio, &md_ctx_verify), 0))
1371e1051a39Sopenharmony_ci                goto out;
1372e1051a39Sopenharmony_ci        }
1373e1051a39Sopenharmony_ci
1374e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, md,
1375e1051a39Sopenharmony_ci                                            NULL, pkey)))
1376e1051a39Sopenharmony_ci            goto out;
1377e1051a39Sopenharmony_ci
1378e1051a39Sopenharmony_ci        if (tst >= 6 && tst <= 8) {
1379e1051a39Sopenharmony_ci            if (!TEST_true(BIO_write_ex(mdbio, kMsg, sizeof(kMsg), &written)))
1380e1051a39Sopenharmony_ci                goto out;
1381e1051a39Sopenharmony_ci        } else {
1382e1051a39Sopenharmony_ci            if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg,
1383e1051a39Sopenharmony_ci                                                  sizeof(kMsg))))
1384e1051a39Sopenharmony_ci                goto out;
1385e1051a39Sopenharmony_ci        }
1386e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
1387e1051a39Sopenharmony_ci            goto out;
1388e1051a39Sopenharmony_ci
1389e1051a39Sopenharmony_ci        /* Multiple calls to EVP_DigestVerifyFinal should work */
1390e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
1391e1051a39Sopenharmony_ci            goto out;
1392e1051a39Sopenharmony_ci    } else {
1393e1051a39Sopenharmony_ci        /*
1394e1051a39Sopenharmony_ci         * For HMAC a doubled call to DigestSignFinal should produce the same
1395e1051a39Sopenharmony_ci         * value as finalization should not happen.
1396e1051a39Sopenharmony_ci         */
1397e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig2_len))
1398e1051a39Sopenharmony_ci            || !TEST_ptr(sig2 = OPENSSL_malloc(sig2_len))
1399e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestSignFinal(md_ctx, sig2, &sig2_len)))
1400e1051a39Sopenharmony_ci            goto out;
1401e1051a39Sopenharmony_ci
1402e1051a39Sopenharmony_ci        if (!TEST_mem_eq(sig, sig_len, sig2, sig2_len))
1403e1051a39Sopenharmony_ci            goto out;
1404e1051a39Sopenharmony_ci    }
1405e1051a39Sopenharmony_ci
1406e1051a39Sopenharmony_ci    ret = 1;
1407e1051a39Sopenharmony_ci
1408e1051a39Sopenharmony_ci out:
1409e1051a39Sopenharmony_ci    BIO_free(membio);
1410e1051a39Sopenharmony_ci    BIO_free(mdbio);
1411e1051a39Sopenharmony_ci    EVP_MD_CTX_free(a_md_ctx);
1412e1051a39Sopenharmony_ci    EVP_MD_CTX_free(a_md_ctx_verify);
1413e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1414e1051a39Sopenharmony_ci    OPENSSL_free(sig);
1415e1051a39Sopenharmony_ci    OPENSSL_free(sig2);
1416e1051a39Sopenharmony_ci    EVP_MD_free(mdexp);
1417e1051a39Sopenharmony_ci
1418e1051a39Sopenharmony_ci    return ret;
1419e1051a39Sopenharmony_ci}
1420e1051a39Sopenharmony_ci
1421e1051a39Sopenharmony_cistatic int test_EVP_DigestVerifyInit(void)
1422e1051a39Sopenharmony_ci{
1423e1051a39Sopenharmony_ci    int ret = 0;
1424e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1425e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx = NULL;
1426e1051a39Sopenharmony_ci
1427e1051a39Sopenharmony_ci    if (nullprov != NULL)
1428e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
1429e1051a39Sopenharmony_ci
1430e1051a39Sopenharmony_ci    if (!TEST_ptr(md_ctx = EVP_MD_CTX_new())
1431e1051a39Sopenharmony_ci            || !TEST_ptr(pkey = load_example_rsa_key()))
1432e1051a39Sopenharmony_ci        goto out;
1433e1051a39Sopenharmony_ci
1434e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, EVP_sha256(), NULL, pkey))
1435e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
1436e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
1437e1051a39Sopenharmony_ci                                                 sizeof(kSignature)), 0))
1438e1051a39Sopenharmony_ci        goto out;
1439e1051a39Sopenharmony_ci
1440e1051a39Sopenharmony_ci    /* test with reinitialization */
1441e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyInit(md_ctx, NULL, NULL, NULL, NULL))
1442e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestVerifyUpdate(md_ctx, kMsg, sizeof(kMsg)))
1443e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_DigestVerifyFinal(md_ctx, kSignature,
1444e1051a39Sopenharmony_ci                                                 sizeof(kSignature)), 0))
1445e1051a39Sopenharmony_ci        goto out;
1446e1051a39Sopenharmony_ci    ret = 1;
1447e1051a39Sopenharmony_ci
1448e1051a39Sopenharmony_ci out:
1449e1051a39Sopenharmony_ci    EVP_MD_CTX_free(md_ctx);
1450e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1451e1051a39Sopenharmony_ci    return ret;
1452e1051a39Sopenharmony_ci}
1453e1051a39Sopenharmony_ci
1454e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SIPHASH
1455e1051a39Sopenharmony_ci/* test SIPHASH MAC via EVP_PKEY with non-default parameters and reinit */
1456e1051a39Sopenharmony_cistatic int test_siphash_digestsign(void)
1457e1051a39Sopenharmony_ci{
1458e1051a39Sopenharmony_ci    unsigned char key[16];
1459e1051a39Sopenharmony_ci    unsigned char buf[8], digest[8];
1460e1051a39Sopenharmony_ci    unsigned char expected[8] = {
1461e1051a39Sopenharmony_ci        0x6d, 0x3e, 0x54, 0xc2, 0x2f, 0xf1, 0xfe, 0xe2
1462e1051a39Sopenharmony_ci    };
1463e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1464e1051a39Sopenharmony_ci    EVP_MD_CTX *mdctx = NULL;
1465e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
1466e1051a39Sopenharmony_ci    int ret = 0;
1467e1051a39Sopenharmony_ci    size_t len = 8;
1468e1051a39Sopenharmony_ci
1469e1051a39Sopenharmony_ci    if (nullprov != NULL)
1470e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
1471e1051a39Sopenharmony_ci
1472e1051a39Sopenharmony_ci    memset(buf, 0, 8);
1473e1051a39Sopenharmony_ci    memset(key, 1, 16);
1474e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_SIPHASH, NULL,
1475e1051a39Sopenharmony_ci                                                      key, 16)))
1476e1051a39Sopenharmony_ci        goto out;
1477e1051a39Sopenharmony_ci
1478e1051a39Sopenharmony_ci    if (!TEST_ptr(mdctx = EVP_MD_CTX_create()))
1479e1051a39Sopenharmony_ci        goto out;
1480e1051a39Sopenharmony_ci
1481e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignInit(mdctx, &ctx, NULL, NULL, pkey)))
1482e1051a39Sopenharmony_ci        goto out;
1483e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_SIGNCTX,
1484e1051a39Sopenharmony_ci                                       EVP_PKEY_CTRL_SET_DIGEST_SIZE,
1485e1051a39Sopenharmony_ci                                       8, NULL), 1))
1486e1051a39Sopenharmony_ci        goto out;
1487e1051a39Sopenharmony_ci    /* reinitialize */
1488e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignInit(mdctx, NULL, NULL, NULL, NULL)))
1489e1051a39Sopenharmony_ci        goto out;
1490e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignUpdate(mdctx, buf, 8)))
1491e1051a39Sopenharmony_ci        goto out;
1492e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignFinal(mdctx, digest, &len)))
1493e1051a39Sopenharmony_ci        goto out;
1494e1051a39Sopenharmony_ci    if (!TEST_mem_eq(digest, len, expected, sizeof(expected)))
1495e1051a39Sopenharmony_ci        goto out;
1496e1051a39Sopenharmony_ci
1497e1051a39Sopenharmony_ci    ret = 1;
1498e1051a39Sopenharmony_ci out:
1499e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1500e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mdctx);
1501e1051a39Sopenharmony_ci    return ret;
1502e1051a39Sopenharmony_ci}
1503e1051a39Sopenharmony_ci#endif
1504e1051a39Sopenharmony_ci
1505e1051a39Sopenharmony_ci/*
1506e1051a39Sopenharmony_ci * Test corner cases of EVP_DigestInit/Update/Final API call behavior.
1507e1051a39Sopenharmony_ci */
1508e1051a39Sopenharmony_cistatic int test_EVP_Digest(void)
1509e1051a39Sopenharmony_ci{
1510e1051a39Sopenharmony_ci    int ret = 0;
1511e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx = NULL;
1512e1051a39Sopenharmony_ci    unsigned char md[EVP_MAX_MD_SIZE];
1513e1051a39Sopenharmony_ci    EVP_MD *sha256 = NULL;
1514e1051a39Sopenharmony_ci    EVP_MD *shake256 = NULL;
1515e1051a39Sopenharmony_ci
1516e1051a39Sopenharmony_ci    if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
1517e1051a39Sopenharmony_ci        goto out;
1518e1051a39Sopenharmony_ci
1519e1051a39Sopenharmony_ci    if (!TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", testpropq))
1520e1051a39Sopenharmony_ci            || !TEST_ptr(shake256 = EVP_MD_fetch(testctx, "shake256", testpropq)))
1521e1051a39Sopenharmony_ci        goto out;
1522e1051a39Sopenharmony_ci
1523e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
1524e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
1525e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestFinal(md_ctx, md, NULL))
1526e1051a39Sopenharmony_ci            /* EVP_DigestFinal resets the EVP_MD_CTX. */
1527e1051a39Sopenharmony_ci            || !TEST_ptr_eq(EVP_MD_CTX_get0_md(md_ctx), NULL))
1528e1051a39Sopenharmony_ci        goto out;
1529e1051a39Sopenharmony_ci
1530e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestInit_ex(md_ctx, sha256, NULL))
1531e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
1532e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestFinal_ex(md_ctx, md, NULL))
1533e1051a39Sopenharmony_ci            /* EVP_DigestFinal_ex does not reset the EVP_MD_CTX. */
1534e1051a39Sopenharmony_ci            || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
1535e1051a39Sopenharmony_ci            /*
1536e1051a39Sopenharmony_ci             * EVP_DigestInit_ex with NULL type should work on
1537e1051a39Sopenharmony_ci             * pre-initialized context.
1538e1051a39Sopenharmony_ci             */
1539e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
1540e1051a39Sopenharmony_ci        goto out;
1541e1051a39Sopenharmony_ci
1542e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestInit_ex(md_ctx, shake256, NULL))
1543e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestUpdate(md_ctx, kMsg, sizeof(kMsg)))
1544e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestFinalXOF(md_ctx, md, sizeof(md)))
1545e1051a39Sopenharmony_ci            /* EVP_DigestFinalXOF does not reset the EVP_MD_CTX. */
1546e1051a39Sopenharmony_ci            || !TEST_ptr(EVP_MD_CTX_get0_md(md_ctx))
1547e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestInit_ex(md_ctx, NULL, NULL)))
1548e1051a39Sopenharmony_ci        goto out;
1549e1051a39Sopenharmony_ci    ret = 1;
1550e1051a39Sopenharmony_ci
1551e1051a39Sopenharmony_ci out:
1552e1051a39Sopenharmony_ci    EVP_MD_CTX_free(md_ctx);
1553e1051a39Sopenharmony_ci    EVP_MD_free(sha256);
1554e1051a39Sopenharmony_ci    EVP_MD_free(shake256);
1555e1051a39Sopenharmony_ci    return ret;
1556e1051a39Sopenharmony_ci}
1557e1051a39Sopenharmony_ci
1558e1051a39Sopenharmony_cistatic int test_EVP_md_null(void)
1559e1051a39Sopenharmony_ci{
1560e1051a39Sopenharmony_ci    int ret = 0;
1561e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx = NULL;
1562e1051a39Sopenharmony_ci    const EVP_MD *md_null = EVP_md_null();
1563e1051a39Sopenharmony_ci    unsigned char md_value[EVP_MAX_MD_SIZE];
1564e1051a39Sopenharmony_ci    unsigned int md_len = sizeof(md_value);
1565e1051a39Sopenharmony_ci
1566e1051a39Sopenharmony_ci    if (nullprov != NULL)
1567e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
1568e1051a39Sopenharmony_ci
1569e1051a39Sopenharmony_ci    if (!TEST_ptr(md_null)
1570e1051a39Sopenharmony_ci        || !TEST_ptr(md_ctx = EVP_MD_CTX_new()))
1571e1051a39Sopenharmony_ci        goto out;
1572e1051a39Sopenharmony_ci
1573e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestInit_ex(md_ctx, md_null, NULL))
1574e1051a39Sopenharmony_ci        || !TEST_true(EVP_DigestUpdate(md_ctx, "test", 4))
1575e1051a39Sopenharmony_ci        || !TEST_true(EVP_DigestFinal_ex(md_ctx, md_value, &md_len)))
1576e1051a39Sopenharmony_ci        goto out;
1577e1051a39Sopenharmony_ci
1578e1051a39Sopenharmony_ci    if (!TEST_uint_eq(md_len, 0))
1579e1051a39Sopenharmony_ci        goto out;
1580e1051a39Sopenharmony_ci
1581e1051a39Sopenharmony_ci    ret = 1;
1582e1051a39Sopenharmony_ci out:
1583e1051a39Sopenharmony_ci    EVP_MD_CTX_free(md_ctx);
1584e1051a39Sopenharmony_ci    return ret;
1585e1051a39Sopenharmony_ci}
1586e1051a39Sopenharmony_ci
1587e1051a39Sopenharmony_cistatic int test_d2i_AutoPrivateKey(int i)
1588e1051a39Sopenharmony_ci{
1589e1051a39Sopenharmony_ci    int ret = 0;
1590e1051a39Sopenharmony_ci    const unsigned char *p;
1591e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1592e1051a39Sopenharmony_ci    const APK_DATA *ak = &keydata[i];
1593e1051a39Sopenharmony_ci    const unsigned char *input = ak->kder;
1594e1051a39Sopenharmony_ci    size_t input_len = ak->size;
1595e1051a39Sopenharmony_ci    int expected_id = ak->evptype;
1596e1051a39Sopenharmony_ci
1597e1051a39Sopenharmony_ci    p = input;
1598e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len))
1599e1051a39Sopenharmony_ci            || !TEST_ptr_eq(p, input + input_len)
1600e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
1601e1051a39Sopenharmony_ci        goto done;
1602e1051a39Sopenharmony_ci
1603e1051a39Sopenharmony_ci    ret = 1;
1604e1051a39Sopenharmony_ci
1605e1051a39Sopenharmony_ci done:
1606e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1607e1051a39Sopenharmony_ci    return ret;
1608e1051a39Sopenharmony_ci}
1609e1051a39Sopenharmony_ci
1610e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
1611e1051a39Sopenharmony_ci
1612e1051a39Sopenharmony_cistatic const unsigned char ec_public_sect163k1_validxy[] = {
1613e1051a39Sopenharmony_ci    0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
1614e1051a39Sopenharmony_ci    0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
1615e1051a39Sopenharmony_ci    0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
1616e1051a39Sopenharmony_ci    0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x02, 0xd1, 0x7b,
1617e1051a39Sopenharmony_ci    0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
1618e1051a39Sopenharmony_ci    0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
1619e1051a39Sopenharmony_ci};
1620e1051a39Sopenharmony_ci
1621e1051a39Sopenharmony_cistatic const unsigned char ec_public_sect163k1_badx[] = {
1622e1051a39Sopenharmony_ci    0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
1623e1051a39Sopenharmony_ci    0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
1624e1051a39Sopenharmony_ci    0x0a, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
1625e1051a39Sopenharmony_ci    0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0xb0, 0x02, 0xd1, 0x7b,
1626e1051a39Sopenharmony_ci    0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
1627e1051a39Sopenharmony_ci    0x6a, 0xd8, 0x17, 0x65, 0x41, 0x2f
1628e1051a39Sopenharmony_ci};
1629e1051a39Sopenharmony_ci
1630e1051a39Sopenharmony_cistatic const unsigned char ec_public_sect163k1_bady[] = {
1631e1051a39Sopenharmony_ci    0x30, 0x40, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
1632e1051a39Sopenharmony_ci    0x01, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x01, 0x03, 0x2c, 0x00, 0x04,
1633e1051a39Sopenharmony_ci    0x02, 0x84, 0x58, 0xa6, 0xd4, 0xa0, 0x35, 0x2b, 0xae, 0xf0, 0xc0, 0x69,
1634e1051a39Sopenharmony_ci    0x05, 0xcf, 0x2a, 0x50, 0x33, 0xf9, 0xe3, 0x92, 0x79, 0x0a, 0xd1, 0x7b,
1635e1051a39Sopenharmony_ci    0x9f, 0x22, 0x00, 0xf0, 0x3b, 0x0e, 0x5d, 0x2e, 0xb7, 0x23, 0x24, 0xf3,
1636e1051a39Sopenharmony_ci    0x6a, 0xd8, 0x17, 0x65, 0x41, 0xe6
1637e1051a39Sopenharmony_ci};
1638e1051a39Sopenharmony_ci
1639e1051a39Sopenharmony_cistatic struct ec_der_pub_keys_st {
1640e1051a39Sopenharmony_ci    const unsigned char *der;
1641e1051a39Sopenharmony_ci    size_t len;
1642e1051a39Sopenharmony_ci    int valid;
1643e1051a39Sopenharmony_ci} ec_der_pub_keys[] = {
1644e1051a39Sopenharmony_ci    { ec_public_sect163k1_validxy, sizeof(ec_public_sect163k1_validxy), 1 },
1645e1051a39Sopenharmony_ci    { ec_public_sect163k1_badx, sizeof(ec_public_sect163k1_badx), 0 },
1646e1051a39Sopenharmony_ci    { ec_public_sect163k1_bady, sizeof(ec_public_sect163k1_bady), 0 },
1647e1051a39Sopenharmony_ci};
1648e1051a39Sopenharmony_ci
1649e1051a39Sopenharmony_ci/*
1650e1051a39Sopenharmony_ci * Tests the range of the decoded EC char2 public point.
1651e1051a39Sopenharmony_ci * See ec_GF2m_simple_oct2point().
1652e1051a39Sopenharmony_ci */
1653e1051a39Sopenharmony_cistatic int test_invalide_ec_char2_pub_range_decode(int id)
1654e1051a39Sopenharmony_ci{
1655e1051a39Sopenharmony_ci    int ret = 0;
1656e1051a39Sopenharmony_ci    EVP_PKEY *pkey;
1657e1051a39Sopenharmony_ci
1658e1051a39Sopenharmony_ci    pkey = load_example_key("EC", ec_der_pub_keys[id].der,
1659e1051a39Sopenharmony_ci                            ec_der_pub_keys[id].len);
1660e1051a39Sopenharmony_ci
1661e1051a39Sopenharmony_ci    ret = (ec_der_pub_keys[id].valid && TEST_ptr(pkey))
1662e1051a39Sopenharmony_ci          || TEST_ptr_null(pkey);
1663e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1664e1051a39Sopenharmony_ci    return ret;
1665e1051a39Sopenharmony_ci}
1666e1051a39Sopenharmony_ci
1667e1051a39Sopenharmony_ci/* Tests loading a bad key in PKCS8 format */
1668e1051a39Sopenharmony_cistatic int test_EVP_PKCS82PKEY(void)
1669e1051a39Sopenharmony_ci{
1670e1051a39Sopenharmony_ci    int ret = 0;
1671e1051a39Sopenharmony_ci    const unsigned char *derp = kExampleBadECKeyDER;
1672e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
1673e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1674e1051a39Sopenharmony_ci
1675e1051a39Sopenharmony_ci    if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp,
1676e1051a39Sopenharmony_ci                                              sizeof(kExampleBadECKeyDER))))
1677e1051a39Sopenharmony_ci        goto done;
1678e1051a39Sopenharmony_ci
1679e1051a39Sopenharmony_ci    if (!TEST_ptr_eq(derp,
1680e1051a39Sopenharmony_ci                     kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)))
1681e1051a39Sopenharmony_ci        goto done;
1682e1051a39Sopenharmony_ci
1683e1051a39Sopenharmony_ci    if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf)))
1684e1051a39Sopenharmony_ci        goto done;
1685e1051a39Sopenharmony_ci
1686e1051a39Sopenharmony_ci    ret = 1;
1687e1051a39Sopenharmony_ci
1688e1051a39Sopenharmony_ci done:
1689e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO_free(p8inf);
1690e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1691e1051a39Sopenharmony_ci
1692e1051a39Sopenharmony_ci    return ret;
1693e1051a39Sopenharmony_ci}
1694e1051a39Sopenharmony_ci
1695e1051a39Sopenharmony_ci#endif
1696e1051a39Sopenharmony_cistatic int test_EVP_PKCS82PKEY_wrong_tag(void)
1697e1051a39Sopenharmony_ci{
1698e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1699e1051a39Sopenharmony_ci    EVP_PKEY *pkey2 = NULL;
1700e1051a39Sopenharmony_ci    BIO *membio = NULL;
1701e1051a39Sopenharmony_ci    char *membuf = NULL;
1702e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
1703e1051a39Sopenharmony_ci    int ok = 0;
1704e1051a39Sopenharmony_ci
1705e1051a39Sopenharmony_ci    if (testctx != NULL)
1706e1051a39Sopenharmony_ci        /* test not supported with non-default context */
1707e1051a39Sopenharmony_ci        return 1;
1708e1051a39Sopenharmony_ci
1709e1051a39Sopenharmony_ci    if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
1710e1051a39Sopenharmony_ci        || !TEST_ptr(pkey = load_example_rsa_key())
1711e1051a39Sopenharmony_ci        || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
1712e1051a39Sopenharmony_ci                                                NULL, 0, NULL, NULL),
1713e1051a39Sopenharmony_ci                        0)
1714e1051a39Sopenharmony_ci        || !TEST_int_gt(BIO_get_mem_data(membio, &membuf), 0)
1715e1051a39Sopenharmony_ci        || !TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(membio, NULL))
1716e1051a39Sopenharmony_ci        || !TEST_ptr(pkey2 = EVP_PKCS82PKEY(p8inf))
1717e1051a39Sopenharmony_ci        || !TEST_int_eq(ERR_peek_last_error(), 0)) {
1718e1051a39Sopenharmony_ci        goto done;
1719e1051a39Sopenharmony_ci    }
1720e1051a39Sopenharmony_ci
1721e1051a39Sopenharmony_ci    ok = 1;
1722e1051a39Sopenharmony_ci done:
1723e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1724e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey2);
1725e1051a39Sopenharmony_ci    PKCS8_PRIV_KEY_INFO_free(p8inf);
1726e1051a39Sopenharmony_ci    BIO_free_all(membio);
1727e1051a39Sopenharmony_ci    return ok;
1728e1051a39Sopenharmony_ci}
1729e1051a39Sopenharmony_ci
1730e1051a39Sopenharmony_ci/* This uses kExampleRSAKeyDER and kExampleRSAKeyPKCS8 to verify encoding */
1731e1051a39Sopenharmony_cistatic int test_privatekey_to_pkcs8(void)
1732e1051a39Sopenharmony_ci{
1733e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1734e1051a39Sopenharmony_ci    BIO *membio = NULL;
1735e1051a39Sopenharmony_ci    char *membuf = NULL;
1736e1051a39Sopenharmony_ci    long membuf_len = 0;
1737e1051a39Sopenharmony_ci    int ok = 0;
1738e1051a39Sopenharmony_ci
1739e1051a39Sopenharmony_ci    if (!TEST_ptr(membio = BIO_new(BIO_s_mem()))
1740e1051a39Sopenharmony_ci        || !TEST_ptr(pkey = load_example_rsa_key())
1741e1051a39Sopenharmony_ci        || !TEST_int_gt(i2d_PKCS8PrivateKey_bio(membio, pkey, NULL,
1742e1051a39Sopenharmony_ci                                                NULL, 0, NULL, NULL),
1743e1051a39Sopenharmony_ci                        0)
1744e1051a39Sopenharmony_ci        || !TEST_int_gt(membuf_len = BIO_get_mem_data(membio, &membuf), 0)
1745e1051a39Sopenharmony_ci        || !TEST_ptr(membuf)
1746e1051a39Sopenharmony_ci        || !TEST_mem_eq(membuf, (size_t)membuf_len,
1747e1051a39Sopenharmony_ci                        kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8))
1748e1051a39Sopenharmony_ci        /*
1749e1051a39Sopenharmony_ci         * We try to write PEM as well, just to see that it doesn't err, but
1750e1051a39Sopenharmony_ci         * assume that the result is correct.
1751e1051a39Sopenharmony_ci         */
1752e1051a39Sopenharmony_ci        || !TEST_int_gt(PEM_write_bio_PKCS8PrivateKey(membio, pkey, NULL,
1753e1051a39Sopenharmony_ci                                                      NULL, 0, NULL, NULL),
1754e1051a39Sopenharmony_ci                        0))
1755e1051a39Sopenharmony_ci        goto done;
1756e1051a39Sopenharmony_ci
1757e1051a39Sopenharmony_ci    ok = 1;
1758e1051a39Sopenharmony_ci done:
1759e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1760e1051a39Sopenharmony_ci    BIO_free_all(membio);
1761e1051a39Sopenharmony_ci    return ok;
1762e1051a39Sopenharmony_ci}
1763e1051a39Sopenharmony_ci
1764e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
1765e1051a39Sopenharmony_cistatic const struct {
1766e1051a39Sopenharmony_ci    int encoding;
1767e1051a39Sopenharmony_ci    const char *encoding_name;
1768e1051a39Sopenharmony_ci} ec_encodings[] = {
1769e1051a39Sopenharmony_ci    { OPENSSL_EC_EXPLICIT_CURVE, OSSL_PKEY_EC_ENCODING_EXPLICIT },
1770e1051a39Sopenharmony_ci    { OPENSSL_EC_NAMED_CURVE,    OSSL_PKEY_EC_ENCODING_GROUP }
1771e1051a39Sopenharmony_ci};
1772e1051a39Sopenharmony_ci
1773e1051a39Sopenharmony_cistatic int ec_export_get_encoding_cb(const OSSL_PARAM params[], void *arg)
1774e1051a39Sopenharmony_ci{
1775e1051a39Sopenharmony_ci    const OSSL_PARAM *p;
1776e1051a39Sopenharmony_ci    const char *enc_name = NULL;
1777e1051a39Sopenharmony_ci    int *enc = arg;
1778e1051a39Sopenharmony_ci    size_t i;
1779e1051a39Sopenharmony_ci
1780e1051a39Sopenharmony_ci    *enc = -1;
1781e1051a39Sopenharmony_ci
1782e1051a39Sopenharmony_ci    if (!TEST_ptr(p = OSSL_PARAM_locate_const(params,
1783e1051a39Sopenharmony_ci                                              OSSL_PKEY_PARAM_EC_ENCODING))
1784e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_get_utf8_string_ptr(p, &enc_name)))
1785e1051a39Sopenharmony_ci        return 0;
1786e1051a39Sopenharmony_ci
1787e1051a39Sopenharmony_ci    for (i = 0; i < OSSL_NELEM(ec_encodings); i++) {
1788e1051a39Sopenharmony_ci        if (OPENSSL_strcasecmp(enc_name, ec_encodings[i].encoding_name) == 0) {
1789e1051a39Sopenharmony_ci            *enc = ec_encodings[i].encoding;
1790e1051a39Sopenharmony_ci            break;
1791e1051a39Sopenharmony_ci        }
1792e1051a39Sopenharmony_ci    }
1793e1051a39Sopenharmony_ci
1794e1051a39Sopenharmony_ci    return (*enc != -1);
1795e1051a39Sopenharmony_ci}
1796e1051a39Sopenharmony_ci
1797e1051a39Sopenharmony_cistatic int test_EC_keygen_with_enc(int idx)
1798e1051a39Sopenharmony_ci{
1799e1051a39Sopenharmony_ci    EVP_PKEY *params = NULL, *key = NULL;
1800e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL, *kctx = NULL;
1801e1051a39Sopenharmony_ci    int enc;
1802e1051a39Sopenharmony_ci    int ret = 0;
1803e1051a39Sopenharmony_ci
1804e1051a39Sopenharmony_ci    enc = ec_encodings[idx].encoding;
1805e1051a39Sopenharmony_ci
1806e1051a39Sopenharmony_ci    /* Create key parameters */
1807e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "EC", NULL))
1808e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_paramgen_init(pctx), 0)
1809e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_CTX_set_group_name(pctx, "P-256"), 0)
1810e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_CTX_set_ec_param_enc(pctx, enc), 0)
1811e1051a39Sopenharmony_ci        || !TEST_true(EVP_PKEY_paramgen(pctx, &params))
1812e1051a39Sopenharmony_ci        || !TEST_ptr(params))
1813e1051a39Sopenharmony_ci        goto done;
1814e1051a39Sopenharmony_ci
1815e1051a39Sopenharmony_ci    /* Create key */
1816e1051a39Sopenharmony_ci    if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx, params, NULL))
1817e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
1818e1051a39Sopenharmony_ci        || !TEST_true(EVP_PKEY_keygen(kctx, &key))
1819e1051a39Sopenharmony_ci        || !TEST_ptr(key))
1820e1051a39Sopenharmony_ci        goto done;
1821e1051a39Sopenharmony_ci
1822e1051a39Sopenharmony_ci    /* Check that the encoding got all the way into the key */
1823e1051a39Sopenharmony_ci    if (!TEST_true(evp_keymgmt_util_export(key, OSSL_KEYMGMT_SELECT_ALL,
1824e1051a39Sopenharmony_ci                                           ec_export_get_encoding_cb, &enc))
1825e1051a39Sopenharmony_ci        || !TEST_int_eq(enc, ec_encodings[idx].encoding))
1826e1051a39Sopenharmony_ci        goto done;
1827e1051a39Sopenharmony_ci
1828e1051a39Sopenharmony_ci    ret = 1;
1829e1051a39Sopenharmony_ci done:
1830e1051a39Sopenharmony_ci    EVP_PKEY_free(key);
1831e1051a39Sopenharmony_ci    EVP_PKEY_free(params);
1832e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(kctx);
1833e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
1834e1051a39Sopenharmony_ci    return ret;
1835e1051a39Sopenharmony_ci}
1836e1051a39Sopenharmony_ci#endif
1837e1051a39Sopenharmony_ci
1838e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_SM2)
1839e1051a39Sopenharmony_ci
1840e1051a39Sopenharmony_cistatic int test_EVP_SM2_verify(void)
1841e1051a39Sopenharmony_ci{
1842e1051a39Sopenharmony_ci    const char *pubkey =
1843e1051a39Sopenharmony_ci        "-----BEGIN PUBLIC KEY-----\n"
1844e1051a39Sopenharmony_ci        "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEp1KLWq1ZE2jmoAnnBJE1LBGxVr18\n"
1845e1051a39Sopenharmony_ci        "YvvqECWCpXfAQ9qUJ+UmthnUPf0iM3SaXKHe6PlLIDyNlWMWb9RUh/yU3g==\n"
1846e1051a39Sopenharmony_ci        "-----END PUBLIC KEY-----\n";
1847e1051a39Sopenharmony_ci
1848e1051a39Sopenharmony_ci    const char *msg = "message digest";
1849e1051a39Sopenharmony_ci    const char *id = "ALICE123@YAHOO.COM";
1850e1051a39Sopenharmony_ci
1851e1051a39Sopenharmony_ci    const uint8_t signature[] = {
1852e1051a39Sopenharmony_ci        0x30, 0x44, 0x02, 0x20, 0x5b, 0xdb, 0xab, 0x81, 0x4f, 0xbb,
1853e1051a39Sopenharmony_ci        0x8b, 0x69, 0xb1, 0x05, 0x9c, 0x99, 0x3b, 0xb2, 0x45, 0x06,
1854e1051a39Sopenharmony_ci        0x4a, 0x30, 0x15, 0x59, 0x84, 0xcd, 0xee, 0x30, 0x60, 0x36,
1855e1051a39Sopenharmony_ci        0x57, 0x87, 0xef, 0x5c, 0xd0, 0xbe, 0x02, 0x20, 0x43, 0x8d,
1856e1051a39Sopenharmony_ci        0x1f, 0xc7, 0x77, 0x72, 0x39, 0xbb, 0x72, 0xe1, 0xfd, 0x07,
1857e1051a39Sopenharmony_ci        0x58, 0xd5, 0x82, 0xc8, 0x2d, 0xba, 0x3b, 0x2c, 0x46, 0x24,
1858e1051a39Sopenharmony_ci        0xe3, 0x50, 0xff, 0x04, 0xc7, 0xa0, 0x71, 0x9f, 0xa4, 0x70
1859e1051a39Sopenharmony_ci    };
1860e1051a39Sopenharmony_ci
1861e1051a39Sopenharmony_ci    int rc = 0;
1862e1051a39Sopenharmony_ci    BIO *bio = NULL;
1863e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1864e1051a39Sopenharmony_ci    EVP_MD_CTX *mctx = NULL;
1865e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
1866e1051a39Sopenharmony_ci    EVP_MD *sm3 = NULL;
1867e1051a39Sopenharmony_ci
1868e1051a39Sopenharmony_ci    bio = BIO_new_mem_buf(pubkey, strlen(pubkey));
1869e1051a39Sopenharmony_ci    if (!TEST_true(bio != NULL))
1870e1051a39Sopenharmony_ci        goto done;
1871e1051a39Sopenharmony_ci
1872e1051a39Sopenharmony_ci    pkey = PEM_read_bio_PUBKEY_ex(bio, NULL, NULL, NULL, testctx, testpropq);
1873e1051a39Sopenharmony_ci    if (!TEST_true(pkey != NULL))
1874e1051a39Sopenharmony_ci        goto done;
1875e1051a39Sopenharmony_ci
1876e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_is_a(pkey, "SM2")))
1877e1051a39Sopenharmony_ci        goto done;
1878e1051a39Sopenharmony_ci
1879e1051a39Sopenharmony_ci    if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
1880e1051a39Sopenharmony_ci        goto done;
1881e1051a39Sopenharmony_ci
1882e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
1883e1051a39Sopenharmony_ci        goto done;
1884e1051a39Sopenharmony_ci
1885e1051a39Sopenharmony_ci    EVP_MD_CTX_set_pkey_ctx(mctx, pctx);
1886e1051a39Sopenharmony_ci
1887e1051a39Sopenharmony_ci    if (!TEST_ptr(sm3 = EVP_MD_fetch(testctx, "sm3", testpropq)))
1888e1051a39Sopenharmony_ci        goto done;
1889e1051a39Sopenharmony_ci
1890e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyInit(mctx, NULL, sm3, NULL, pkey)))
1891e1051a39Sopenharmony_ci        goto done;
1892e1051a39Sopenharmony_ci
1893e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(pctx, id, strlen(id)), 0))
1894e1051a39Sopenharmony_ci        goto done;
1895e1051a39Sopenharmony_ci
1896e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyUpdate(mctx, msg, strlen(msg))))
1897e1051a39Sopenharmony_ci        goto done;
1898e1051a39Sopenharmony_ci
1899e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_DigestVerifyFinal(mctx, signature, sizeof(signature)), 0))
1900e1051a39Sopenharmony_ci        goto done;
1901e1051a39Sopenharmony_ci    rc = 1;
1902e1051a39Sopenharmony_ci
1903e1051a39Sopenharmony_ci done:
1904e1051a39Sopenharmony_ci    BIO_free(bio);
1905e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
1906e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
1907e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mctx);
1908e1051a39Sopenharmony_ci    EVP_MD_free(sm3);
1909e1051a39Sopenharmony_ci    return rc;
1910e1051a39Sopenharmony_ci}
1911e1051a39Sopenharmony_ci
1912e1051a39Sopenharmony_cistatic int test_EVP_SM2(void)
1913e1051a39Sopenharmony_ci{
1914e1051a39Sopenharmony_ci    int ret = 0;
1915e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
1916e1051a39Sopenharmony_ci    EVP_PKEY *pkeyparams = NULL;
1917e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
1918e1051a39Sopenharmony_ci    EVP_PKEY_CTX *kctx = NULL;
1919e1051a39Sopenharmony_ci    EVP_PKEY_CTX *sctx = NULL;
1920e1051a39Sopenharmony_ci    size_t sig_len = 0;
1921e1051a39Sopenharmony_ci    unsigned char *sig = NULL;
1922e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx = NULL;
1923e1051a39Sopenharmony_ci    EVP_MD_CTX *md_ctx_verify = NULL;
1924e1051a39Sopenharmony_ci    EVP_PKEY_CTX *cctx = NULL;
1925e1051a39Sopenharmony_ci    EVP_MD *check_md = NULL;
1926e1051a39Sopenharmony_ci
1927e1051a39Sopenharmony_ci    uint8_t ciphertext[128];
1928e1051a39Sopenharmony_ci    size_t ctext_len = sizeof(ciphertext);
1929e1051a39Sopenharmony_ci
1930e1051a39Sopenharmony_ci    uint8_t plaintext[8];
1931e1051a39Sopenharmony_ci    size_t ptext_len = sizeof(plaintext);
1932e1051a39Sopenharmony_ci
1933e1051a39Sopenharmony_ci    uint8_t sm2_id[] = {1, 2, 3, 4, 'l', 'e', 't', 't', 'e', 'r'};
1934e1051a39Sopenharmony_ci
1935e1051a39Sopenharmony_ci    OSSL_PARAM sparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
1936e1051a39Sopenharmony_ci    OSSL_PARAM gparams[2] = {OSSL_PARAM_END, OSSL_PARAM_END};
1937e1051a39Sopenharmony_ci    int i;
1938e1051a39Sopenharmony_ci    char mdname[OSSL_MAX_NAME_SIZE];
1939e1051a39Sopenharmony_ci
1940e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx,
1941e1051a39Sopenharmony_ci                                                    "SM2", testpropq)))
1942e1051a39Sopenharmony_ci        goto done;
1943e1051a39Sopenharmony_ci
1944e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_paramgen_init(pctx) == 1))
1945e1051a39Sopenharmony_ci        goto done;
1946e1051a39Sopenharmony_ci
1947e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2), 0))
1948e1051a39Sopenharmony_ci        goto done;
1949e1051a39Sopenharmony_ci
1950e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_paramgen(pctx, &pkeyparams)))
1951e1051a39Sopenharmony_ci        goto done;
1952e1051a39Sopenharmony_ci
1953e1051a39Sopenharmony_ci    if (!TEST_ptr(kctx = EVP_PKEY_CTX_new_from_pkey(testctx,
1954e1051a39Sopenharmony_ci                                                    pkeyparams, testpropq)))
1955e1051a39Sopenharmony_ci        goto done;
1956e1051a39Sopenharmony_ci
1957e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0))
1958e1051a39Sopenharmony_ci        goto done;
1959e1051a39Sopenharmony_ci
1960e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_keygen(kctx, &pkey)))
1961e1051a39Sopenharmony_ci        goto done;
1962e1051a39Sopenharmony_ci
1963e1051a39Sopenharmony_ci    if (!TEST_ptr(md_ctx = EVP_MD_CTX_new()))
1964e1051a39Sopenharmony_ci        goto done;
1965e1051a39Sopenharmony_ci
1966e1051a39Sopenharmony_ci    if (!TEST_ptr(md_ctx_verify = EVP_MD_CTX_new()))
1967e1051a39Sopenharmony_ci        goto done;
1968e1051a39Sopenharmony_ci
1969e1051a39Sopenharmony_ci    if (!TEST_ptr(sctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
1970e1051a39Sopenharmony_ci        goto done;
1971e1051a39Sopenharmony_ci
1972e1051a39Sopenharmony_ci    EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);
1973e1051a39Sopenharmony_ci    EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);
1974e1051a39Sopenharmony_ci
1975e1051a39Sopenharmony_ci    if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, "sm3", testpropq)))
1976e1051a39Sopenharmony_ci        goto done;
1977e1051a39Sopenharmony_ci
1978e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignInit(md_ctx, NULL, check_md, NULL, pkey)))
1979e1051a39Sopenharmony_ci        goto done;
1980e1051a39Sopenharmony_ci
1981e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
1982e1051a39Sopenharmony_ci        goto done;
1983e1051a39Sopenharmony_ci
1984e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignUpdate(md_ctx, kMsg, sizeof(kMsg))))
1985e1051a39Sopenharmony_ci        goto done;
1986e1051a39Sopenharmony_ci
1987e1051a39Sopenharmony_ci    /* Determine the size of the signature. */
1988e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignFinal(md_ctx, NULL, &sig_len)))
1989e1051a39Sopenharmony_ci        goto done;
1990e1051a39Sopenharmony_ci
1991e1051a39Sopenharmony_ci    if (!TEST_ptr(sig = OPENSSL_malloc(sig_len)))
1992e1051a39Sopenharmony_ci        goto done;
1993e1051a39Sopenharmony_ci
1994e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestSignFinal(md_ctx, sig, &sig_len)))
1995e1051a39Sopenharmony_ci        goto done;
1996e1051a39Sopenharmony_ci
1997e1051a39Sopenharmony_ci    /* Ensure that the signature round-trips. */
1998e1051a39Sopenharmony_ci
1999e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
2000e1051a39Sopenharmony_ci                                        pkey)))
2001e1051a39Sopenharmony_ci        goto done;
2002e1051a39Sopenharmony_ci
2003e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, sm2_id, sizeof(sm2_id)), 0))
2004e1051a39Sopenharmony_ci        goto done;
2005e1051a39Sopenharmony_ci
2006e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
2007e1051a39Sopenharmony_ci        goto done;
2008e1051a39Sopenharmony_ci
2009e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
2010e1051a39Sopenharmony_ci        goto done;
2011e1051a39Sopenharmony_ci
2012e1051a39Sopenharmony_ci    /*
2013e1051a39Sopenharmony_ci     * Try verify again with non-matching 0 length id but ensure that it can
2014e1051a39Sopenharmony_ci     * be set on the context and overrides the previous value.
2015e1051a39Sopenharmony_ci     */
2016e1051a39Sopenharmony_ci
2017e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyInit(md_ctx_verify, NULL, check_md, NULL,
2018e1051a39Sopenharmony_ci                                        pkey)))
2019e1051a39Sopenharmony_ci        goto done;
2020e1051a39Sopenharmony_ci
2021e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set1_id(sctx, NULL, 0), 0))
2022e1051a39Sopenharmony_ci        goto done;
2023e1051a39Sopenharmony_ci
2024e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DigestVerifyUpdate(md_ctx_verify, kMsg, sizeof(kMsg))))
2025e1051a39Sopenharmony_ci        goto done;
2026e1051a39Sopenharmony_ci
2027e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len), 0))
2028e1051a39Sopenharmony_ci        goto done;
2029e1051a39Sopenharmony_ci
2030e1051a39Sopenharmony_ci    /* now check encryption/decryption */
2031e1051a39Sopenharmony_ci
2032e1051a39Sopenharmony_ci    gparams[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
2033e1051a39Sopenharmony_ci                                                  mdname, sizeof(mdname));
2034e1051a39Sopenharmony_ci    for (i = 0; i < 2; i++) {
2035e1051a39Sopenharmony_ci        const char *mdnames[] = {
2036e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SM3
2037e1051a39Sopenharmony_ci            "SM3",
2038e1051a39Sopenharmony_ci#else
2039e1051a39Sopenharmony_ci            NULL,
2040e1051a39Sopenharmony_ci#endif
2041e1051a39Sopenharmony_ci            "SHA2-256" };
2042e1051a39Sopenharmony_ci        EVP_PKEY_CTX_free(cctx);
2043e1051a39Sopenharmony_ci
2044e1051a39Sopenharmony_ci        if (mdnames[i] == NULL)
2045e1051a39Sopenharmony_ci            continue;
2046e1051a39Sopenharmony_ci
2047e1051a39Sopenharmony_ci        sparams[0] =
2048e1051a39Sopenharmony_ci            OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_DIGEST,
2049e1051a39Sopenharmony_ci                                             (char *)mdnames[i], 0);
2050e1051a39Sopenharmony_ci
2051e1051a39Sopenharmony_ci        if (!TEST_ptr(cctx = EVP_PKEY_CTX_new_from_pkey(testctx,
2052e1051a39Sopenharmony_ci                                                        pkey, testpropq)))
2053e1051a39Sopenharmony_ci            goto done;
2054e1051a39Sopenharmony_ci
2055e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_encrypt_init(cctx)))
2056e1051a39Sopenharmony_ci            goto done;
2057e1051a39Sopenharmony_ci
2058e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
2059e1051a39Sopenharmony_ci            goto done;
2060e1051a39Sopenharmony_ci
2061e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_encrypt(cctx, ciphertext, &ctext_len, kMsg,
2062e1051a39Sopenharmony_ci                                        sizeof(kMsg))))
2063e1051a39Sopenharmony_ci            goto done;
2064e1051a39Sopenharmony_ci
2065e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_PKEY_decrypt_init(cctx), 0))
2066e1051a39Sopenharmony_ci            goto done;
2067e1051a39Sopenharmony_ci
2068e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_CTX_set_params(cctx, sparams)))
2069e1051a39Sopenharmony_ci            goto done;
2070e1051a39Sopenharmony_ci
2071e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_PKEY_decrypt(cctx, plaintext, &ptext_len, ciphertext,
2072e1051a39Sopenharmony_ci                                        ctext_len), 0))
2073e1051a39Sopenharmony_ci            goto done;
2074e1051a39Sopenharmony_ci
2075e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_CTX_get_params(cctx, gparams)))
2076e1051a39Sopenharmony_ci            goto done;
2077e1051a39Sopenharmony_ci
2078e1051a39Sopenharmony_ci        /*
2079e1051a39Sopenharmony_ci         * Test we're still using the digest we think we are.
2080e1051a39Sopenharmony_ci         * Because of aliases, the easiest is to fetch the digest and
2081e1051a39Sopenharmony_ci         * check the name with EVP_MD_is_a().
2082e1051a39Sopenharmony_ci         */
2083e1051a39Sopenharmony_ci        EVP_MD_free(check_md);
2084e1051a39Sopenharmony_ci        if (!TEST_ptr(check_md = EVP_MD_fetch(testctx, mdname, testpropq)))
2085e1051a39Sopenharmony_ci            goto done;
2086e1051a39Sopenharmony_ci        if (!TEST_true(EVP_MD_is_a(check_md, mdnames[i]))) {
2087e1051a39Sopenharmony_ci            TEST_info("Fetched md %s isn't %s", mdname, mdnames[i]);
2088e1051a39Sopenharmony_ci            goto done;
2089e1051a39Sopenharmony_ci        }
2090e1051a39Sopenharmony_ci
2091e1051a39Sopenharmony_ci        if (!TEST_true(ptext_len == sizeof(kMsg)))
2092e1051a39Sopenharmony_ci            goto done;
2093e1051a39Sopenharmony_ci
2094e1051a39Sopenharmony_ci        if (!TEST_true(memcmp(plaintext, kMsg, sizeof(kMsg)) == 0))
2095e1051a39Sopenharmony_ci            goto done;
2096e1051a39Sopenharmony_ci    }
2097e1051a39Sopenharmony_ci
2098e1051a39Sopenharmony_ci    ret = 1;
2099e1051a39Sopenharmony_cidone:
2100e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
2101e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(kctx);
2102e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(sctx);
2103e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(cctx);
2104e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2105e1051a39Sopenharmony_ci    EVP_PKEY_free(pkeyparams);
2106e1051a39Sopenharmony_ci    EVP_MD_CTX_free(md_ctx);
2107e1051a39Sopenharmony_ci    EVP_MD_CTX_free(md_ctx_verify);
2108e1051a39Sopenharmony_ci    EVP_MD_free(check_md);
2109e1051a39Sopenharmony_ci    OPENSSL_free(sig);
2110e1051a39Sopenharmony_ci    return ret;
2111e1051a39Sopenharmony_ci}
2112e1051a39Sopenharmony_ci
2113e1051a39Sopenharmony_ci#endif
2114e1051a39Sopenharmony_ci
2115e1051a39Sopenharmony_cistatic struct keys_st {
2116e1051a39Sopenharmony_ci    int type;
2117e1051a39Sopenharmony_ci    char *priv;
2118e1051a39Sopenharmony_ci    char *pub;
2119e1051a39Sopenharmony_ci} keys[] = {
2120e1051a39Sopenharmony_ci    {
2121e1051a39Sopenharmony_ci        EVP_PKEY_HMAC, "0123456789", NULL
2122e1051a39Sopenharmony_ci    },
2123e1051a39Sopenharmony_ci    {
2124e1051a39Sopenharmony_ci        EVP_PKEY_HMAC, "", NULL
2125e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_POLY1305
2126e1051a39Sopenharmony_ci    }, {
2127e1051a39Sopenharmony_ci        EVP_PKEY_POLY1305, "01234567890123456789012345678901", NULL
2128e1051a39Sopenharmony_ci#endif
2129e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SIPHASH
2130e1051a39Sopenharmony_ci    }, {
2131e1051a39Sopenharmony_ci        EVP_PKEY_SIPHASH, "0123456789012345", NULL
2132e1051a39Sopenharmony_ci#endif
2133e1051a39Sopenharmony_ci    },
2134e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
2135e1051a39Sopenharmony_ci    {
2136e1051a39Sopenharmony_ci        EVP_PKEY_X25519, "01234567890123456789012345678901",
2137e1051a39Sopenharmony_ci        "abcdefghijklmnopqrstuvwxyzabcdef"
2138e1051a39Sopenharmony_ci    }, {
2139e1051a39Sopenharmony_ci        EVP_PKEY_ED25519, "01234567890123456789012345678901",
2140e1051a39Sopenharmony_ci        "abcdefghijklmnopqrstuvwxyzabcdef"
2141e1051a39Sopenharmony_ci    }, {
2142e1051a39Sopenharmony_ci        EVP_PKEY_X448,
2143e1051a39Sopenharmony_ci        "01234567890123456789012345678901234567890123456789012345",
2144e1051a39Sopenharmony_ci        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd"
2145e1051a39Sopenharmony_ci    }, {
2146e1051a39Sopenharmony_ci        EVP_PKEY_ED448,
2147e1051a39Sopenharmony_ci        "012345678901234567890123456789012345678901234567890123456",
2148e1051a39Sopenharmony_ci        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcde"
2149e1051a39Sopenharmony_ci    }
2150e1051a39Sopenharmony_ci#endif
2151e1051a39Sopenharmony_ci};
2152e1051a39Sopenharmony_ci
2153e1051a39Sopenharmony_cistatic int test_set_get_raw_keys_int(int tst, int pub, int uselibctx)
2154e1051a39Sopenharmony_ci{
2155e1051a39Sopenharmony_ci    int ret = 0;
2156e1051a39Sopenharmony_ci    unsigned char buf[80];
2157e1051a39Sopenharmony_ci    unsigned char *in;
2158e1051a39Sopenharmony_ci    size_t inlen, len = 0, shortlen = 1;
2159e1051a39Sopenharmony_ci    EVP_PKEY *pkey;
2160e1051a39Sopenharmony_ci
2161e1051a39Sopenharmony_ci    /* Check if this algorithm supports public keys */
2162e1051a39Sopenharmony_ci    if (pub && keys[tst].pub == NULL)
2163e1051a39Sopenharmony_ci        return 1;
2164e1051a39Sopenharmony_ci
2165e1051a39Sopenharmony_ci    memset(buf, 0, sizeof(buf));
2166e1051a39Sopenharmony_ci
2167e1051a39Sopenharmony_ci    if (pub) {
2168e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
2169e1051a39Sopenharmony_ci        inlen = strlen(keys[tst].pub);
2170e1051a39Sopenharmony_ci        in = (unsigned char *)keys[tst].pub;
2171e1051a39Sopenharmony_ci        if (uselibctx) {
2172e1051a39Sopenharmony_ci            pkey = EVP_PKEY_new_raw_public_key_ex(
2173e1051a39Sopenharmony_ci                        testctx,
2174e1051a39Sopenharmony_ci                        OBJ_nid2sn(keys[tst].type),
2175e1051a39Sopenharmony_ci                        NULL,
2176e1051a39Sopenharmony_ci                        in,
2177e1051a39Sopenharmony_ci                        inlen);
2178e1051a39Sopenharmony_ci        } else {
2179e1051a39Sopenharmony_ci            pkey = EVP_PKEY_new_raw_public_key(keys[tst].type,
2180e1051a39Sopenharmony_ci                                               NULL,
2181e1051a39Sopenharmony_ci                                               in,
2182e1051a39Sopenharmony_ci                                               inlen);
2183e1051a39Sopenharmony_ci        }
2184e1051a39Sopenharmony_ci#else
2185e1051a39Sopenharmony_ci        return 1;
2186e1051a39Sopenharmony_ci#endif
2187e1051a39Sopenharmony_ci    } else {
2188e1051a39Sopenharmony_ci        inlen = strlen(keys[tst].priv);
2189e1051a39Sopenharmony_ci        in = (unsigned char *)keys[tst].priv;
2190e1051a39Sopenharmony_ci        if (uselibctx) {
2191e1051a39Sopenharmony_ci            pkey = EVP_PKEY_new_raw_private_key_ex(
2192e1051a39Sopenharmony_ci                        testctx, OBJ_nid2sn(keys[tst].type),
2193e1051a39Sopenharmony_ci                        NULL,
2194e1051a39Sopenharmony_ci                        in,
2195e1051a39Sopenharmony_ci                        inlen);
2196e1051a39Sopenharmony_ci        } else {
2197e1051a39Sopenharmony_ci            pkey = EVP_PKEY_new_raw_private_key(keys[tst].type,
2198e1051a39Sopenharmony_ci                                                NULL,
2199e1051a39Sopenharmony_ci                                                in,
2200e1051a39Sopenharmony_ci                                                inlen);
2201e1051a39Sopenharmony_ci        }
2202e1051a39Sopenharmony_ci    }
2203e1051a39Sopenharmony_ci
2204e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey)
2205e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey), 1)
2206e1051a39Sopenharmony_ci            || (!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, NULL, &len)))
2207e1051a39Sopenharmony_ci            || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, NULL, &len)))
2208e1051a39Sopenharmony_ci            || !TEST_true(len == inlen))
2209e1051a39Sopenharmony_ci        goto done;
2210e1051a39Sopenharmony_ci    if (tst != 1) {
2211e1051a39Sopenharmony_ci        /*
2212e1051a39Sopenharmony_ci         * Test that supplying a buffer that is too small fails. Doesn't apply
2213e1051a39Sopenharmony_ci         * to HMAC with a zero length key
2214e1051a39Sopenharmony_ci         */
2215e1051a39Sopenharmony_ci        if ((!pub && !TEST_false(EVP_PKEY_get_raw_private_key(pkey, buf,
2216e1051a39Sopenharmony_ci                                                                 &shortlen)))
2217e1051a39Sopenharmony_ci                || (pub && !TEST_false(EVP_PKEY_get_raw_public_key(pkey, buf,
2218e1051a39Sopenharmony_ci                                                                   &shortlen))))
2219e1051a39Sopenharmony_ci            goto done;
2220e1051a39Sopenharmony_ci    }
2221e1051a39Sopenharmony_ci    if ((!pub && !TEST_true(EVP_PKEY_get_raw_private_key(pkey, buf, &len)))
2222e1051a39Sopenharmony_ci            || (pub && !TEST_true(EVP_PKEY_get_raw_public_key(pkey, buf, &len)))
2223e1051a39Sopenharmony_ci            || !TEST_mem_eq(in, inlen, buf, len))
2224e1051a39Sopenharmony_ci        goto done;
2225e1051a39Sopenharmony_ci
2226e1051a39Sopenharmony_ci    ret = 1;
2227e1051a39Sopenharmony_ci done:
2228e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2229e1051a39Sopenharmony_ci    return ret;
2230e1051a39Sopenharmony_ci}
2231e1051a39Sopenharmony_ci
2232e1051a39Sopenharmony_cistatic int test_set_get_raw_keys(int tst)
2233e1051a39Sopenharmony_ci{
2234e1051a39Sopenharmony_ci    return (nullprov != NULL || test_set_get_raw_keys_int(tst, 0, 0))
2235e1051a39Sopenharmony_ci           && test_set_get_raw_keys_int(tst, 0, 1)
2236e1051a39Sopenharmony_ci           && (nullprov != NULL || test_set_get_raw_keys_int(tst, 1, 0))
2237e1051a39Sopenharmony_ci           && test_set_get_raw_keys_int(tst, 1, 1);
2238e1051a39Sopenharmony_ci}
2239e1051a39Sopenharmony_ci
2240e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
2241e1051a39Sopenharmony_cistatic int pkey_custom_check(EVP_PKEY *pkey)
2242e1051a39Sopenharmony_ci{
2243e1051a39Sopenharmony_ci    return 0xbeef;
2244e1051a39Sopenharmony_ci}
2245e1051a39Sopenharmony_ci
2246e1051a39Sopenharmony_cistatic int pkey_custom_pub_check(EVP_PKEY *pkey)
2247e1051a39Sopenharmony_ci{
2248e1051a39Sopenharmony_ci    return 0xbeef;
2249e1051a39Sopenharmony_ci}
2250e1051a39Sopenharmony_ci
2251e1051a39Sopenharmony_cistatic int pkey_custom_param_check(EVP_PKEY *pkey)
2252e1051a39Sopenharmony_ci{
2253e1051a39Sopenharmony_ci    return 0xbeef;
2254e1051a39Sopenharmony_ci}
2255e1051a39Sopenharmony_ci
2256e1051a39Sopenharmony_cistatic EVP_PKEY_METHOD *custom_pmeth;
2257e1051a39Sopenharmony_ci#endif
2258e1051a39Sopenharmony_ci
2259e1051a39Sopenharmony_cistatic int test_EVP_PKEY_check(int i)
2260e1051a39Sopenharmony_ci{
2261e1051a39Sopenharmony_ci    int ret = 0;
2262e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
2263e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
2264e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
2265e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx2 = NULL;
2266e1051a39Sopenharmony_ci#endif
2267e1051a39Sopenharmony_ci    const APK_DATA *ak = &keycheckdata[i];
2268e1051a39Sopenharmony_ci    const unsigned char *input = ak->kder;
2269e1051a39Sopenharmony_ci    size_t input_len = ak->size;
2270e1051a39Sopenharmony_ci    int expected_id = ak->evptype;
2271e1051a39Sopenharmony_ci    int expected_check = ak->check;
2272e1051a39Sopenharmony_ci    int expected_pub_check = ak->pub_check;
2273e1051a39Sopenharmony_ci    int expected_param_check = ak->param_check;
2274e1051a39Sopenharmony_ci    int type = ak->type;
2275e1051a39Sopenharmony_ci
2276e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey = load_example_key(ak->keytype, input, input_len)))
2277e1051a39Sopenharmony_ci        goto done;
2278e1051a39Sopenharmony_ci    if (type == 0
2279e1051a39Sopenharmony_ci        && !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
2280e1051a39Sopenharmony_ci        goto done;
2281e1051a39Sopenharmony_ci
2282e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq)))
2283e1051a39Sopenharmony_ci        goto done;
2284e1051a39Sopenharmony_ci
2285e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check))
2286e1051a39Sopenharmony_ci        goto done;
2287e1051a39Sopenharmony_ci
2288e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check))
2289e1051a39Sopenharmony_ci        goto done;
2290e1051a39Sopenharmony_ci
2291e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check))
2292e1051a39Sopenharmony_ci        goto done;
2293e1051a39Sopenharmony_ci
2294e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
2295e1051a39Sopenharmony_ci    ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL);
2296e1051a39Sopenharmony_ci    /* assign the pkey directly, as an internal test */
2297e1051a39Sopenharmony_ci    EVP_PKEY_up_ref(pkey);
2298e1051a39Sopenharmony_ci    ctx2->pkey = pkey;
2299e1051a39Sopenharmony_ci
2300e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef))
2301e1051a39Sopenharmony_ci        goto done;
2302e1051a39Sopenharmony_ci
2303e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef))
2304e1051a39Sopenharmony_ci        goto done;
2305e1051a39Sopenharmony_ci
2306e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef))
2307e1051a39Sopenharmony_ci        goto done;
2308e1051a39Sopenharmony_ci#endif
2309e1051a39Sopenharmony_ci
2310e1051a39Sopenharmony_ci    ret = 1;
2311e1051a39Sopenharmony_ci
2312e1051a39Sopenharmony_ci done:
2313e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
2314e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
2315e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx2);
2316e1051a39Sopenharmony_ci#endif
2317e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2318e1051a39Sopenharmony_ci    return ret;
2319e1051a39Sopenharmony_ci}
2320e1051a39Sopenharmony_ci
2321e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_CMAC
2322e1051a39Sopenharmony_cistatic int get_cmac_val(EVP_PKEY *pkey, unsigned char *mac)
2323e1051a39Sopenharmony_ci{
2324e1051a39Sopenharmony_ci    EVP_MD_CTX *mdctx = EVP_MD_CTX_new();
2325e1051a39Sopenharmony_ci    const char msg[] = "Hello World";
2326e1051a39Sopenharmony_ci    size_t maclen = AES_BLOCK_SIZE;
2327e1051a39Sopenharmony_ci    int ret = 1;
2328e1051a39Sopenharmony_ci
2329e1051a39Sopenharmony_ci    if (!TEST_ptr(mdctx)
2330e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, NULL, testctx,
2331e1051a39Sopenharmony_ci                                                testpropq, pkey, NULL))
2332e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestSignUpdate(mdctx, msg, sizeof(msg)))
2333e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestSignFinal(mdctx, mac, &maclen))
2334e1051a39Sopenharmony_ci            || !TEST_size_t_eq(maclen, AES_BLOCK_SIZE))
2335e1051a39Sopenharmony_ci        ret = 0;
2336e1051a39Sopenharmony_ci
2337e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mdctx);
2338e1051a39Sopenharmony_ci
2339e1051a39Sopenharmony_ci    return ret;
2340e1051a39Sopenharmony_ci}
2341e1051a39Sopenharmony_cistatic int test_CMAC_keygen(void)
2342e1051a39Sopenharmony_ci{
2343e1051a39Sopenharmony_ci    static unsigned char key[] = {
2344e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
2345e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
2346e1051a39Sopenharmony_ci        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
2347e1051a39Sopenharmony_ci    };
2348e1051a39Sopenharmony_ci    EVP_PKEY_CTX *kctx = NULL;
2349e1051a39Sopenharmony_ci    int ret = 0;
2350e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
2351e1051a39Sopenharmony_ci    unsigned char mac[AES_BLOCK_SIZE];
2352e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_DEPRECATED_3_0)
2353e1051a39Sopenharmony_ci    unsigned char mac2[AES_BLOCK_SIZE];
2354e1051a39Sopenharmony_ci# endif
2355e1051a39Sopenharmony_ci
2356e1051a39Sopenharmony_ci    if (nullprov != NULL)
2357e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
2358e1051a39Sopenharmony_ci
2359e1051a39Sopenharmony_ci    /*
2360e1051a39Sopenharmony_ci     * This is a legacy method for CMACs, but should still work.
2361e1051a39Sopenharmony_ci     * This verifies that it works without an ENGINE.
2362e1051a39Sopenharmony_ci     */
2363e1051a39Sopenharmony_ci    kctx = EVP_PKEY_CTX_new_id(EVP_PKEY_CMAC, NULL);
2364e1051a39Sopenharmony_ci
2365e1051a39Sopenharmony_ci    /* Test a CMAC key created using the "generated" method */
2366e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_keygen_init(kctx), 0)
2367e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
2368e1051a39Sopenharmony_ci                                            EVP_PKEY_CTRL_CIPHER,
2369e1051a39Sopenharmony_ci                                            0, (void *)EVP_aes_256_ecb()), 0)
2370e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_ctrl(kctx, -1, EVP_PKEY_OP_KEYGEN,
2371e1051a39Sopenharmony_ci                                            EVP_PKEY_CTRL_SET_MAC_KEY,
2372e1051a39Sopenharmony_ci                                            sizeof(key), (void *)key), 0)
2373e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_keygen(kctx, &pkey), 0)
2374e1051a39Sopenharmony_ci            || !TEST_ptr(pkey)
2375e1051a39Sopenharmony_ci            || !TEST_true(get_cmac_val(pkey, mac)))
2376e1051a39Sopenharmony_ci        goto done;
2377e1051a39Sopenharmony_ci
2378e1051a39Sopenharmony_ci# if !defined(OPENSSL_NO_DEPRECATED_3_0)
2379e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2380e1051a39Sopenharmony_ci
2381e1051a39Sopenharmony_ci    /*
2382e1051a39Sopenharmony_ci     * Test a CMAC key using the direct method, and compare with the mac
2383e1051a39Sopenharmony_ci     * created above.
2384e1051a39Sopenharmony_ci     */
2385e1051a39Sopenharmony_ci    pkey = EVP_PKEY_new_CMAC_key(NULL, key, sizeof(key), EVP_aes_256_ecb());
2386e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey)
2387e1051a39Sopenharmony_ci            || !TEST_true(get_cmac_val(pkey, mac2))
2388e1051a39Sopenharmony_ci            || !TEST_mem_eq(mac, sizeof(mac), mac2, sizeof(mac2)))
2389e1051a39Sopenharmony_ci        goto done;
2390e1051a39Sopenharmony_ci# endif
2391e1051a39Sopenharmony_ci
2392e1051a39Sopenharmony_ci    ret = 1;
2393e1051a39Sopenharmony_ci
2394e1051a39Sopenharmony_ci done:
2395e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2396e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(kctx);
2397e1051a39Sopenharmony_ci    return ret;
2398e1051a39Sopenharmony_ci}
2399e1051a39Sopenharmony_ci#endif
2400e1051a39Sopenharmony_ci
2401e1051a39Sopenharmony_cistatic int test_HKDF(void)
2402e1051a39Sopenharmony_ci{
2403e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx;
2404e1051a39Sopenharmony_ci    unsigned char out[20];
2405e1051a39Sopenharmony_ci    size_t outlen;
2406e1051a39Sopenharmony_ci    int i, ret = 0;
2407e1051a39Sopenharmony_ci    unsigned char salt[] = "0123456789";
2408e1051a39Sopenharmony_ci    unsigned char key[] = "012345678901234567890123456789";
2409e1051a39Sopenharmony_ci    unsigned char info[] = "infostring";
2410e1051a39Sopenharmony_ci    const unsigned char expected[] = {
2411e1051a39Sopenharmony_ci        0xe5, 0x07, 0x70, 0x7f, 0xc6, 0x78, 0xd6, 0x54, 0x32, 0x5f, 0x7e, 0xc5,
2412e1051a39Sopenharmony_ci        0x7b, 0x59, 0x3e, 0xd8, 0x03, 0x6b, 0xed, 0xca
2413e1051a39Sopenharmony_ci    };
2414e1051a39Sopenharmony_ci    size_t expectedlen = sizeof(expected);
2415e1051a39Sopenharmony_ci
2416e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
2417e1051a39Sopenharmony_ci        goto done;
2418e1051a39Sopenharmony_ci
2419e1051a39Sopenharmony_ci    /* We do this twice to test reuse of the EVP_PKEY_CTX */
2420e1051a39Sopenharmony_ci    for (i = 0; i < 2; i++) {
2421e1051a39Sopenharmony_ci        outlen = sizeof(out);
2422e1051a39Sopenharmony_ci        memset(out, 0, outlen);
2423e1051a39Sopenharmony_ci
2424e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
2425e1051a39Sopenharmony_ci                || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
2426e1051a39Sopenharmony_ci                || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
2427e1051a39Sopenharmony_ci                                                            sizeof(salt) - 1), 0)
2428e1051a39Sopenharmony_ci                || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
2429e1051a39Sopenharmony_ci                                                           sizeof(key) - 1), 0)
2430e1051a39Sopenharmony_ci                || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
2431e1051a39Sopenharmony_ci                                                            sizeof(info) - 1), 0)
2432e1051a39Sopenharmony_ci                || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
2433e1051a39Sopenharmony_ci                || !TEST_mem_eq(out, outlen, expected, expectedlen))
2434e1051a39Sopenharmony_ci            goto done;
2435e1051a39Sopenharmony_ci    }
2436e1051a39Sopenharmony_ci
2437e1051a39Sopenharmony_ci    ret = 1;
2438e1051a39Sopenharmony_ci
2439e1051a39Sopenharmony_ci done:
2440e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
2441e1051a39Sopenharmony_ci
2442e1051a39Sopenharmony_ci    return ret;
2443e1051a39Sopenharmony_ci}
2444e1051a39Sopenharmony_ci
2445e1051a39Sopenharmony_cistatic int test_emptyikm_HKDF(void)
2446e1051a39Sopenharmony_ci{
2447e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx;
2448e1051a39Sopenharmony_ci    unsigned char out[20];
2449e1051a39Sopenharmony_ci    size_t outlen;
2450e1051a39Sopenharmony_ci    int ret = 0;
2451e1051a39Sopenharmony_ci    unsigned char salt[] = "9876543210";
2452e1051a39Sopenharmony_ci    unsigned char key[] = "";
2453e1051a39Sopenharmony_ci    unsigned char info[] = "stringinfo";
2454e1051a39Sopenharmony_ci    const unsigned char expected[] = {
2455e1051a39Sopenharmony_ci        0x68, 0x81, 0xa5, 0x3e, 0x5b, 0x9c, 0x7b, 0x6f, 0x2e, 0xec, 0xc8, 0x47,
2456e1051a39Sopenharmony_ci        0x7c, 0xfa, 0x47, 0x35, 0x66, 0x82, 0x15, 0x30
2457e1051a39Sopenharmony_ci    };
2458e1051a39Sopenharmony_ci    size_t expectedlen = sizeof(expected);
2459e1051a39Sopenharmony_ci
2460e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
2461e1051a39Sopenharmony_ci        goto done;
2462e1051a39Sopenharmony_ci
2463e1051a39Sopenharmony_ci    outlen = sizeof(out);
2464e1051a39Sopenharmony_ci    memset(out, 0, outlen);
2465e1051a39Sopenharmony_ci
2466e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
2467e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
2468e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
2469e1051a39Sopenharmony_ci                                                        sizeof(salt) - 1), 0)
2470e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
2471e1051a39Sopenharmony_ci                                                       sizeof(key) - 1), 0)
2472e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
2473e1051a39Sopenharmony_ci                                                        sizeof(info) - 1), 0)
2474e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
2475e1051a39Sopenharmony_ci            || !TEST_mem_eq(out, outlen, expected, expectedlen))
2476e1051a39Sopenharmony_ci        goto done;
2477e1051a39Sopenharmony_ci
2478e1051a39Sopenharmony_ci    ret = 1;
2479e1051a39Sopenharmony_ci
2480e1051a39Sopenharmony_ci done:
2481e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
2482e1051a39Sopenharmony_ci
2483e1051a39Sopenharmony_ci    return ret;
2484e1051a39Sopenharmony_ci}
2485e1051a39Sopenharmony_ci
2486e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
2487e1051a39Sopenharmony_cistatic int test_X509_PUBKEY_inplace(void)
2488e1051a39Sopenharmony_ci{
2489e1051a39Sopenharmony_ci    int ret = 0;
2490e1051a39Sopenharmony_ci    X509_PUBKEY *xp = X509_PUBKEY_new_ex(testctx, testpropq);
2491e1051a39Sopenharmony_ci    const unsigned char *p = kExampleECPubKeyDER;
2492e1051a39Sopenharmony_ci    size_t input_len = sizeof(kExampleECPubKeyDER);
2493e1051a39Sopenharmony_ci
2494e1051a39Sopenharmony_ci    if (!TEST_ptr(xp))
2495e1051a39Sopenharmony_ci        goto done;
2496e1051a39Sopenharmony_ci    if (!TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len)))
2497e1051a39Sopenharmony_ci        goto done;
2498e1051a39Sopenharmony_ci
2499e1051a39Sopenharmony_ci    if (!TEST_ptr(X509_PUBKEY_get0(xp)))
2500e1051a39Sopenharmony_ci        goto done;
2501e1051a39Sopenharmony_ci
2502e1051a39Sopenharmony_ci    p = kExampleBadECPubKeyDER;
2503e1051a39Sopenharmony_ci    input_len = sizeof(kExampleBadECPubKeyDER);
2504e1051a39Sopenharmony_ci
2505e1051a39Sopenharmony_ci    if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len)))
2506e1051a39Sopenharmony_ci        goto done;
2507e1051a39Sopenharmony_ci
2508e1051a39Sopenharmony_ci    if (!TEST_true(X509_PUBKEY_get0(xp) == NULL))
2509e1051a39Sopenharmony_ci        goto done;
2510e1051a39Sopenharmony_ci
2511e1051a39Sopenharmony_ci    ret = 1;
2512e1051a39Sopenharmony_ci
2513e1051a39Sopenharmony_ci done:
2514e1051a39Sopenharmony_ci    X509_PUBKEY_free(xp);
2515e1051a39Sopenharmony_ci    return ret;
2516e1051a39Sopenharmony_ci}
2517e1051a39Sopenharmony_ci
2518e1051a39Sopenharmony_cistatic int test_X509_PUBKEY_dup(void)
2519e1051a39Sopenharmony_ci{
2520e1051a39Sopenharmony_ci    int ret = 0;
2521e1051a39Sopenharmony_ci    X509_PUBKEY *xp = NULL, *xq = NULL;
2522e1051a39Sopenharmony_ci    const unsigned char *p = kExampleECPubKeyDER;
2523e1051a39Sopenharmony_ci    size_t input_len = sizeof(kExampleECPubKeyDER);
2524e1051a39Sopenharmony_ci
2525e1051a39Sopenharmony_ci    xp = X509_PUBKEY_new_ex(testctx, testpropq);
2526e1051a39Sopenharmony_ci    if (!TEST_ptr(xp)
2527e1051a39Sopenharmony_ci            || !TEST_ptr(d2i_X509_PUBKEY(&xp, &p, input_len))
2528e1051a39Sopenharmony_ci            || !TEST_ptr(xq = X509_PUBKEY_dup(xp))
2529e1051a39Sopenharmony_ci            || !TEST_ptr_ne(xp, xq))
2530e1051a39Sopenharmony_ci        goto done;
2531e1051a39Sopenharmony_ci
2532e1051a39Sopenharmony_ci    if (!TEST_ptr(X509_PUBKEY_get0(xq))
2533e1051a39Sopenharmony_ci            || !TEST_ptr(X509_PUBKEY_get0(xp))
2534e1051a39Sopenharmony_ci            || !TEST_ptr_ne(X509_PUBKEY_get0(xq), X509_PUBKEY_get0(xp)))
2535e1051a39Sopenharmony_ci        goto done;
2536e1051a39Sopenharmony_ci
2537e1051a39Sopenharmony_ci    X509_PUBKEY_free(xq);
2538e1051a39Sopenharmony_ci    xq = NULL;
2539e1051a39Sopenharmony_ci    p = kExampleBadECPubKeyDER;
2540e1051a39Sopenharmony_ci    input_len = sizeof(kExampleBadECPubKeyDER);
2541e1051a39Sopenharmony_ci
2542e1051a39Sopenharmony_ci    if (!TEST_ptr(xp = d2i_X509_PUBKEY(&xp, &p, input_len))
2543e1051a39Sopenharmony_ci            || !TEST_ptr(xq = X509_PUBKEY_dup(xp)))
2544e1051a39Sopenharmony_ci        goto done;
2545e1051a39Sopenharmony_ci
2546e1051a39Sopenharmony_ci    X509_PUBKEY_free(xp);
2547e1051a39Sopenharmony_ci    xp = NULL;
2548e1051a39Sopenharmony_ci    if (!TEST_true(X509_PUBKEY_get0(xq) == NULL))
2549e1051a39Sopenharmony_ci        goto done;
2550e1051a39Sopenharmony_ci
2551e1051a39Sopenharmony_ci    ret = 1;
2552e1051a39Sopenharmony_ci
2553e1051a39Sopenharmony_ci done:
2554e1051a39Sopenharmony_ci    X509_PUBKEY_free(xp);
2555e1051a39Sopenharmony_ci    X509_PUBKEY_free(xq);
2556e1051a39Sopenharmony_ci    return ret;
2557e1051a39Sopenharmony_ci}
2558e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_EC */
2559e1051a39Sopenharmony_ci
2560e1051a39Sopenharmony_ci/* Test getting and setting parameters on an EVP_PKEY_CTX */
2561e1051a39Sopenharmony_cistatic int test_EVP_PKEY_CTX_get_set_params(EVP_PKEY *pkey)
2562e1051a39Sopenharmony_ci{
2563e1051a39Sopenharmony_ci    EVP_MD_CTX *mdctx = NULL;
2564e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
2565e1051a39Sopenharmony_ci    const OSSL_PARAM *params;
2566e1051a39Sopenharmony_ci    OSSL_PARAM ourparams[2], *param = ourparams, *param_md;
2567e1051a39Sopenharmony_ci    int ret = 0;
2568e1051a39Sopenharmony_ci    const EVP_MD *md;
2569e1051a39Sopenharmony_ci    char mdname[OSSL_MAX_NAME_SIZE];
2570e1051a39Sopenharmony_ci    char ssl3ms[48];
2571e1051a39Sopenharmony_ci
2572e1051a39Sopenharmony_ci    /* Initialise a sign operation */
2573e1051a39Sopenharmony_ci    ctx = EVP_PKEY_CTX_new_from_pkey(testctx, pkey, testpropq);
2574e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx)
2575e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_sign_init(ctx), 0))
2576e1051a39Sopenharmony_ci        goto err;
2577e1051a39Sopenharmony_ci
2578e1051a39Sopenharmony_ci    /*
2579e1051a39Sopenharmony_ci     * We should be able to query the parameters now.
2580e1051a39Sopenharmony_ci     */
2581e1051a39Sopenharmony_ci    params = EVP_PKEY_CTX_settable_params(ctx);
2582e1051a39Sopenharmony_ci    if (!TEST_ptr(params)
2583e1051a39Sopenharmony_ci        || !TEST_ptr(OSSL_PARAM_locate_const(params,
2584e1051a39Sopenharmony_ci                                             OSSL_SIGNATURE_PARAM_DIGEST)))
2585e1051a39Sopenharmony_ci        goto err;
2586e1051a39Sopenharmony_ci
2587e1051a39Sopenharmony_ci    params = EVP_PKEY_CTX_gettable_params(ctx);
2588e1051a39Sopenharmony_ci    if (!TEST_ptr(params)
2589e1051a39Sopenharmony_ci        || !TEST_ptr(OSSL_PARAM_locate_const(params,
2590e1051a39Sopenharmony_ci                                             OSSL_SIGNATURE_PARAM_ALGORITHM_ID))
2591e1051a39Sopenharmony_ci        || !TEST_ptr(OSSL_PARAM_locate_const(params,
2592e1051a39Sopenharmony_ci                                             OSSL_SIGNATURE_PARAM_DIGEST)))
2593e1051a39Sopenharmony_ci        goto err;
2594e1051a39Sopenharmony_ci
2595e1051a39Sopenharmony_ci    /*
2596e1051a39Sopenharmony_ci     * Test getting and setting params via EVP_PKEY_CTX_set_params() and
2597e1051a39Sopenharmony_ci     * EVP_PKEY_CTX_get_params()
2598e1051a39Sopenharmony_ci     */
2599e1051a39Sopenharmony_ci    strcpy(mdname, "SHA512");
2600e1051a39Sopenharmony_ci    param_md = param;
2601e1051a39Sopenharmony_ci    *param++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
2602e1051a39Sopenharmony_ci                                                mdname, 0);
2603e1051a39Sopenharmony_ci    *param++ = OSSL_PARAM_construct_end();
2604e1051a39Sopenharmony_ci
2605e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_CTX_set_params(ctx, ourparams)))
2606e1051a39Sopenharmony_ci        goto err;
2607e1051a39Sopenharmony_ci
2608e1051a39Sopenharmony_ci    mdname[0] = '\0';
2609e1051a39Sopenharmony_ci    *param_md = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
2610e1051a39Sopenharmony_ci                                                 mdname, sizeof(mdname));
2611e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_CTX_get_params(ctx, ourparams))
2612e1051a39Sopenharmony_ci            || !TEST_str_eq(mdname, "SHA512"))
2613e1051a39Sopenharmony_ci        goto err;
2614e1051a39Sopenharmony_ci
2615e1051a39Sopenharmony_ci    /*
2616e1051a39Sopenharmony_ci     * Test the TEST_PKEY_CTX_set_signature_md() and
2617e1051a39Sopenharmony_ci     * TEST_PKEY_CTX_get_signature_md() functions
2618e1051a39Sopenharmony_ci     */
2619e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_CTX_set_signature_md(ctx, EVP_sha256()), 0)
2620e1051a39Sopenharmony_ci            || !TEST_int_gt(EVP_PKEY_CTX_get_signature_md(ctx, &md), 0)
2621e1051a39Sopenharmony_ci            || !TEST_ptr_eq(md, EVP_sha256()))
2622e1051a39Sopenharmony_ci        goto err;
2623e1051a39Sopenharmony_ci
2624e1051a39Sopenharmony_ci    /*
2625e1051a39Sopenharmony_ci     * Test getting MD parameters via an associated EVP_PKEY_CTX
2626e1051a39Sopenharmony_ci     */
2627e1051a39Sopenharmony_ci    mdctx = EVP_MD_CTX_new();
2628e1051a39Sopenharmony_ci    if (!TEST_ptr(mdctx)
2629e1051a39Sopenharmony_ci        || !TEST_true(EVP_DigestSignInit_ex(mdctx, NULL, "SHA1", testctx, testpropq,
2630e1051a39Sopenharmony_ci                                            pkey, NULL)))
2631e1051a39Sopenharmony_ci        goto err;
2632e1051a39Sopenharmony_ci
2633e1051a39Sopenharmony_ci    /*
2634e1051a39Sopenharmony_ci     * We now have an EVP_MD_CTX with an EVP_PKEY_CTX inside it. We should be
2635e1051a39Sopenharmony_ci     * able to obtain the digest's settable parameters from the provider.
2636e1051a39Sopenharmony_ci     */
2637e1051a39Sopenharmony_ci    params = EVP_MD_CTX_settable_params(mdctx);
2638e1051a39Sopenharmony_ci    if (!TEST_ptr(params)
2639e1051a39Sopenharmony_ci            || !TEST_int_eq(strcmp(params[0].key, OSSL_DIGEST_PARAM_SSL3_MS), 0)
2640e1051a39Sopenharmony_ci               /* The final key should be NULL */
2641e1051a39Sopenharmony_ci            || !TEST_ptr_null(params[1].key))
2642e1051a39Sopenharmony_ci        goto err;
2643e1051a39Sopenharmony_ci
2644e1051a39Sopenharmony_ci    param = ourparams;
2645e1051a39Sopenharmony_ci    memset(ssl3ms, 0, sizeof(ssl3ms));
2646e1051a39Sopenharmony_ci    *param++ = OSSL_PARAM_construct_octet_string(OSSL_DIGEST_PARAM_SSL3_MS,
2647e1051a39Sopenharmony_ci                                                 ssl3ms, sizeof(ssl3ms));
2648e1051a39Sopenharmony_ci    *param++ = OSSL_PARAM_construct_end();
2649e1051a39Sopenharmony_ci
2650e1051a39Sopenharmony_ci    if (!TEST_true(EVP_MD_CTX_set_params(mdctx, ourparams)))
2651e1051a39Sopenharmony_ci        goto err;
2652e1051a39Sopenharmony_ci
2653e1051a39Sopenharmony_ci    ret = 1;
2654e1051a39Sopenharmony_ci
2655e1051a39Sopenharmony_ci err:
2656e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mdctx);
2657e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
2658e1051a39Sopenharmony_ci
2659e1051a39Sopenharmony_ci    return ret;
2660e1051a39Sopenharmony_ci}
2661e1051a39Sopenharmony_ci
2662e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
2663e1051a39Sopenharmony_cistatic int test_DSA_get_set_params(void)
2664e1051a39Sopenharmony_ci{
2665e1051a39Sopenharmony_ci    OSSL_PARAM_BLD *bld = NULL;
2666e1051a39Sopenharmony_ci    OSSL_PARAM *params = NULL;
2667e1051a39Sopenharmony_ci    BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
2668e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
2669e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
2670e1051a39Sopenharmony_ci    int ret = 0;
2671e1051a39Sopenharmony_ci
2672e1051a39Sopenharmony_ci    /*
2673e1051a39Sopenharmony_ci     * Setup the parameters for our DSA object. For our purposes they don't
2674e1051a39Sopenharmony_ci     * have to actually be *valid* parameters. We just need to set something.
2675e1051a39Sopenharmony_ci     */
2676e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "DSA", NULL))
2677e1051a39Sopenharmony_ci        || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
2678e1051a39Sopenharmony_ci        || !TEST_ptr(p = BN_new())
2679e1051a39Sopenharmony_ci        || !TEST_ptr(q = BN_new())
2680e1051a39Sopenharmony_ci        || !TEST_ptr(g = BN_new())
2681e1051a39Sopenharmony_ci        || !TEST_ptr(pub = BN_new())
2682e1051a39Sopenharmony_ci        || !TEST_ptr(priv = BN_new()))
2683e1051a39Sopenharmony_ci        goto err;
2684e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p))
2685e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q))
2686e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
2687e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
2688e1051a39Sopenharmony_ci                                             pub))
2689e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
2690e1051a39Sopenharmony_ci                                             priv)))
2691e1051a39Sopenharmony_ci        goto err;
2692e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
2693e1051a39Sopenharmony_ci        goto err;
2694e1051a39Sopenharmony_ci
2695e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
2696e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
2697e1051a39Sopenharmony_ci                                          params), 0))
2698e1051a39Sopenharmony_ci        goto err;
2699e1051a39Sopenharmony_ci
2700e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
2701e1051a39Sopenharmony_ci        goto err;
2702e1051a39Sopenharmony_ci
2703e1051a39Sopenharmony_ci    ret = test_EVP_PKEY_CTX_get_set_params(pkey);
2704e1051a39Sopenharmony_ci
2705e1051a39Sopenharmony_ci err:
2706e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2707e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
2708e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
2709e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
2710e1051a39Sopenharmony_ci    BN_free(p);
2711e1051a39Sopenharmony_ci    BN_free(q);
2712e1051a39Sopenharmony_ci    BN_free(g);
2713e1051a39Sopenharmony_ci    BN_free(pub);
2714e1051a39Sopenharmony_ci    BN_free(priv);
2715e1051a39Sopenharmony_ci
2716e1051a39Sopenharmony_ci    return ret;
2717e1051a39Sopenharmony_ci}
2718e1051a39Sopenharmony_ci
2719e1051a39Sopenharmony_ci/*
2720e1051a39Sopenharmony_ci * Test combinations of private, public, missing and private + public key
2721e1051a39Sopenharmony_ci * params to ensure they are all accepted
2722e1051a39Sopenharmony_ci */
2723e1051a39Sopenharmony_cistatic int test_DSA_priv_pub(void)
2724e1051a39Sopenharmony_ci{
2725e1051a39Sopenharmony_ci    return test_EVP_PKEY_ffc_priv_pub("DSA");
2726e1051a39Sopenharmony_ci}
2727e1051a39Sopenharmony_ci
2728e1051a39Sopenharmony_ci#endif /* !OPENSSL_NO_DSA */
2729e1051a39Sopenharmony_ci
2730e1051a39Sopenharmony_cistatic int test_RSA_get_set_params(void)
2731e1051a39Sopenharmony_ci{
2732e1051a39Sopenharmony_ci    OSSL_PARAM_BLD *bld = NULL;
2733e1051a39Sopenharmony_ci    OSSL_PARAM *params = NULL;
2734e1051a39Sopenharmony_ci    BIGNUM *n = NULL, *e = NULL, *d = NULL;
2735e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
2736e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
2737e1051a39Sopenharmony_ci    int ret = 0;
2738e1051a39Sopenharmony_ci
2739e1051a39Sopenharmony_ci    /*
2740e1051a39Sopenharmony_ci     * Setup the parameters for our RSA object. For our purposes they don't
2741e1051a39Sopenharmony_ci     * have to actually be *valid* parameters. We just need to set something.
2742e1051a39Sopenharmony_ci     */
2743e1051a39Sopenharmony_ci    if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA", NULL))
2744e1051a39Sopenharmony_ci        || !TEST_ptr(bld = OSSL_PARAM_BLD_new())
2745e1051a39Sopenharmony_ci        || !TEST_ptr(n = BN_new())
2746e1051a39Sopenharmony_ci        || !TEST_ptr(e = BN_new())
2747e1051a39Sopenharmony_ci        || !TEST_ptr(d = BN_new()))
2748e1051a39Sopenharmony_ci        goto err;
2749e1051a39Sopenharmony_ci    if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n))
2750e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e))
2751e1051a39Sopenharmony_ci        || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)))
2752e1051a39Sopenharmony_ci        goto err;
2753e1051a39Sopenharmony_ci    if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld)))
2754e1051a39Sopenharmony_ci        goto err;
2755e1051a39Sopenharmony_ci
2756e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
2757e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkey, EVP_PKEY_KEYPAIR,
2758e1051a39Sopenharmony_ci                                          params), 0))
2759e1051a39Sopenharmony_ci        goto err;
2760e1051a39Sopenharmony_ci
2761e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
2762e1051a39Sopenharmony_ci        goto err;
2763e1051a39Sopenharmony_ci
2764e1051a39Sopenharmony_ci    ret = test_EVP_PKEY_CTX_get_set_params(pkey);
2765e1051a39Sopenharmony_ci
2766e1051a39Sopenharmony_ci err:
2767e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
2768e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
2769e1051a39Sopenharmony_ci    OSSL_PARAM_free(params);
2770e1051a39Sopenharmony_ci    OSSL_PARAM_BLD_free(bld);
2771e1051a39Sopenharmony_ci    BN_free(n);
2772e1051a39Sopenharmony_ci    BN_free(e);
2773e1051a39Sopenharmony_ci    BN_free(d);
2774e1051a39Sopenharmony_ci
2775e1051a39Sopenharmony_ci    return ret;
2776e1051a39Sopenharmony_ci}
2777e1051a39Sopenharmony_ci
2778e1051a39Sopenharmony_cistatic int test_RSA_OAEP_set_get_params(void)
2779e1051a39Sopenharmony_ci{
2780e1051a39Sopenharmony_ci    int ret = 0;
2781e1051a39Sopenharmony_ci    EVP_PKEY *key = NULL;
2782e1051a39Sopenharmony_ci    EVP_PKEY_CTX *key_ctx = NULL;
2783e1051a39Sopenharmony_ci
2784e1051a39Sopenharmony_ci    if (nullprov != NULL)
2785e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
2786e1051a39Sopenharmony_ci
2787e1051a39Sopenharmony_ci    if (!TEST_ptr(key = load_example_rsa_key())
2788e1051a39Sopenharmony_ci        || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(0, key, 0)))
2789e1051a39Sopenharmony_ci        goto err;
2790e1051a39Sopenharmony_ci
2791e1051a39Sopenharmony_ci    {
2792e1051a39Sopenharmony_ci        int padding = RSA_PKCS1_OAEP_PADDING;
2793e1051a39Sopenharmony_ci        OSSL_PARAM params[4];
2794e1051a39Sopenharmony_ci
2795e1051a39Sopenharmony_ci        params[0] = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PAD_MODE, &padding);
2796e1051a39Sopenharmony_ci        params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
2797e1051a39Sopenharmony_ci                                                     OSSL_DIGEST_NAME_SHA2_256, 0);
2798e1051a39Sopenharmony_ci        params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
2799e1051a39Sopenharmony_ci                                                     OSSL_DIGEST_NAME_SHA1, 0);
2800e1051a39Sopenharmony_ci        params[3] = OSSL_PARAM_construct_end();
2801e1051a39Sopenharmony_ci
2802e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_PKEY_encrypt_init_ex(key_ctx, params),0))
2803e1051a39Sopenharmony_ci            goto err;
2804e1051a39Sopenharmony_ci    }
2805e1051a39Sopenharmony_ci    {
2806e1051a39Sopenharmony_ci        OSSL_PARAM params[3];
2807e1051a39Sopenharmony_ci        char oaepmd[30] = { '\0' };
2808e1051a39Sopenharmony_ci        char mgf1md[30] = { '\0' };
2809e1051a39Sopenharmony_ci
2810e1051a39Sopenharmony_ci        params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
2811e1051a39Sopenharmony_ci                                                     oaepmd, sizeof(oaepmd));
2812e1051a39Sopenharmony_ci        params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
2813e1051a39Sopenharmony_ci                                                     mgf1md, sizeof(mgf1md));
2814e1051a39Sopenharmony_ci        params[2] = OSSL_PARAM_construct_end();
2815e1051a39Sopenharmony_ci
2816e1051a39Sopenharmony_ci        if (!TEST_true(EVP_PKEY_CTX_get_params(key_ctx, params)))
2817e1051a39Sopenharmony_ci            goto err;
2818e1051a39Sopenharmony_ci
2819e1051a39Sopenharmony_ci        if (!TEST_str_eq(oaepmd, OSSL_DIGEST_NAME_SHA2_256)
2820e1051a39Sopenharmony_ci            || !TEST_str_eq(mgf1md, OSSL_DIGEST_NAME_SHA1))
2821e1051a39Sopenharmony_ci            goto err;
2822e1051a39Sopenharmony_ci    }
2823e1051a39Sopenharmony_ci
2824e1051a39Sopenharmony_ci    ret = 1;
2825e1051a39Sopenharmony_ci
2826e1051a39Sopenharmony_ci err:
2827e1051a39Sopenharmony_ci    EVP_PKEY_free(key);
2828e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(key_ctx);
2829e1051a39Sopenharmony_ci
2830e1051a39Sopenharmony_ci    return ret;
2831e1051a39Sopenharmony_ci}
2832e1051a39Sopenharmony_ci
2833e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
2834e1051a39Sopenharmony_cistatic int test_decrypt_null_chunks(void)
2835e1051a39Sopenharmony_ci{
2836e1051a39Sopenharmony_ci    EVP_CIPHER_CTX* ctx = NULL;
2837e1051a39Sopenharmony_ci    EVP_CIPHER *cipher = NULL;
2838e1051a39Sopenharmony_ci    const unsigned char key[32] = {
2839e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
2840e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
2841e1051a39Sopenharmony_ci        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1
2842e1051a39Sopenharmony_ci    };
2843e1051a39Sopenharmony_ci    unsigned char iv[12] = {
2844e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
2845e1051a39Sopenharmony_ci    };
2846e1051a39Sopenharmony_ci    unsigned char msg[] = "It was the best of times, it was the worst of times";
2847e1051a39Sopenharmony_ci    unsigned char ciphertext[80];
2848e1051a39Sopenharmony_ci    unsigned char plaintext[80];
2849e1051a39Sopenharmony_ci    /* We initialise tmp to a non zero value on purpose */
2850e1051a39Sopenharmony_ci    int ctlen, ptlen, tmp = 99;
2851e1051a39Sopenharmony_ci    int ret = 0;
2852e1051a39Sopenharmony_ci    const int enc_offset = 10, dec_offset = 20;
2853e1051a39Sopenharmony_ci
2854e1051a39Sopenharmony_ci    if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, "ChaCha20-Poly1305", testpropq))
2855e1051a39Sopenharmony_ci            || !TEST_ptr(ctx = EVP_CIPHER_CTX_new())
2856e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptInit_ex(ctx, cipher, NULL,
2857e1051a39Sopenharmony_ci                                             key, iv))
2858e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &ctlen, msg,
2859e1051a39Sopenharmony_ci                                            enc_offset))
2860e1051a39Sopenharmony_ci            /* Deliberate add a zero length update */
2861e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp, NULL,
2862e1051a39Sopenharmony_ci                                            0))
2863e1051a39Sopenharmony_ci            || !TEST_int_eq(tmp, 0)
2864e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext + ctlen, &tmp,
2865e1051a39Sopenharmony_ci                                            msg + enc_offset,
2866e1051a39Sopenharmony_ci                                            sizeof(msg) - enc_offset))
2867e1051a39Sopenharmony_ci            || !TEST_int_eq(ctlen += tmp, sizeof(msg))
2868e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptFinal(ctx, ciphertext + ctlen, &tmp))
2869e1051a39Sopenharmony_ci            || !TEST_int_eq(tmp, 0))
2870e1051a39Sopenharmony_ci        goto err;
2871e1051a39Sopenharmony_ci
2872e1051a39Sopenharmony_ci    /* Deliberately initialise tmp to a non zero value */
2873e1051a39Sopenharmony_ci    tmp = 99;
2874e1051a39Sopenharmony_ci    if (!TEST_true(EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv))
2875e1051a39Sopenharmony_ci            || !TEST_true(EVP_DecryptUpdate(ctx, plaintext, &ptlen, ciphertext,
2876e1051a39Sopenharmony_ci                                            dec_offset))
2877e1051a39Sopenharmony_ci            /*
2878e1051a39Sopenharmony_ci             * Deliberately add a zero length update. We also deliberately do
2879e1051a39Sopenharmony_ci             * this at a different offset than for encryption.
2880e1051a39Sopenharmony_ci             */
2881e1051a39Sopenharmony_ci            || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp, NULL,
2882e1051a39Sopenharmony_ci                                            0))
2883e1051a39Sopenharmony_ci            || !TEST_int_eq(tmp, 0)
2884e1051a39Sopenharmony_ci            || !TEST_true(EVP_DecryptUpdate(ctx, plaintext + ptlen, &tmp,
2885e1051a39Sopenharmony_ci                                            ciphertext + dec_offset,
2886e1051a39Sopenharmony_ci                                            ctlen - dec_offset))
2887e1051a39Sopenharmony_ci            || !TEST_int_eq(ptlen += tmp, sizeof(msg))
2888e1051a39Sopenharmony_ci            || !TEST_true(EVP_DecryptFinal(ctx, plaintext + ptlen, &tmp))
2889e1051a39Sopenharmony_ci            || !TEST_int_eq(tmp, 0)
2890e1051a39Sopenharmony_ci            || !TEST_mem_eq(msg, sizeof(msg), plaintext, ptlen))
2891e1051a39Sopenharmony_ci        goto err;
2892e1051a39Sopenharmony_ci
2893e1051a39Sopenharmony_ci    ret = 1;
2894e1051a39Sopenharmony_ci err:
2895e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
2896e1051a39Sopenharmony_ci    EVP_CIPHER_free(cipher);
2897e1051a39Sopenharmony_ci    return ret;
2898e1051a39Sopenharmony_ci}
2899e1051a39Sopenharmony_ci#endif /* !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305) */
2900e1051a39Sopenharmony_ci
2901e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH
2902e1051a39Sopenharmony_ci/*
2903e1051a39Sopenharmony_ci * Test combinations of private, public, missing and private + public key
2904e1051a39Sopenharmony_ci * params to ensure they are all accepted
2905e1051a39Sopenharmony_ci */
2906e1051a39Sopenharmony_cistatic int test_DH_priv_pub(void)
2907e1051a39Sopenharmony_ci{
2908e1051a39Sopenharmony_ci    return test_EVP_PKEY_ffc_priv_pub("DH");
2909e1051a39Sopenharmony_ci}
2910e1051a39Sopenharmony_ci
2911e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
2912e1051a39Sopenharmony_cistatic int test_EVP_PKEY_set1_DH(void)
2913e1051a39Sopenharmony_ci{
2914e1051a39Sopenharmony_ci    DH *x942dh = NULL, *noqdh = NULL;
2915e1051a39Sopenharmony_ci    EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
2916e1051a39Sopenharmony_ci    int ret = 0;
2917e1051a39Sopenharmony_ci    BIGNUM *p, *g = NULL;
2918e1051a39Sopenharmony_ci    BIGNUM *pubkey = NULL;
2919e1051a39Sopenharmony_ci    unsigned char pub[2048 / 8];
2920e1051a39Sopenharmony_ci    size_t len = 0;
2921e1051a39Sopenharmony_ci
2922e1051a39Sopenharmony_ci    if (!TEST_ptr(p = BN_new())
2923e1051a39Sopenharmony_ci            || !TEST_ptr(g = BN_new())
2924e1051a39Sopenharmony_ci            || !TEST_ptr(pubkey = BN_new())
2925e1051a39Sopenharmony_ci            || !TEST_true(BN_set_word(p, 9999))
2926e1051a39Sopenharmony_ci            || !TEST_true(BN_set_word(g, 2))
2927e1051a39Sopenharmony_ci            || !TEST_true(BN_set_word(pubkey, 4321))
2928e1051a39Sopenharmony_ci            || !TEST_ptr(noqdh = DH_new())
2929e1051a39Sopenharmony_ci            || !TEST_true(DH_set0_pqg(noqdh, p, NULL, g))
2930e1051a39Sopenharmony_ci            || !TEST_true(DH_set0_key(noqdh, pubkey, NULL))
2931e1051a39Sopenharmony_ci            || !TEST_ptr(pubkey = BN_new())
2932e1051a39Sopenharmony_ci            || !TEST_true(BN_set_word(pubkey, 4321)))
2933e1051a39Sopenharmony_ci        goto err;
2934e1051a39Sopenharmony_ci    p = g = NULL;
2935e1051a39Sopenharmony_ci
2936e1051a39Sopenharmony_ci    x942dh = DH_get_2048_256();
2937e1051a39Sopenharmony_ci    pkey1 = EVP_PKEY_new();
2938e1051a39Sopenharmony_ci    pkey2 = EVP_PKEY_new();
2939e1051a39Sopenharmony_ci    if (!TEST_ptr(x942dh)
2940e1051a39Sopenharmony_ci            || !TEST_ptr(noqdh)
2941e1051a39Sopenharmony_ci            || !TEST_ptr(pkey1)
2942e1051a39Sopenharmony_ci            || !TEST_ptr(pkey2)
2943e1051a39Sopenharmony_ci            || !TEST_true(DH_set0_key(x942dh, pubkey, NULL)))
2944e1051a39Sopenharmony_ci        goto err;
2945e1051a39Sopenharmony_ci    pubkey = NULL;
2946e1051a39Sopenharmony_ci
2947e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_set1_DH(pkey1, x942dh))
2948e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_PKEY_get_id(pkey1), EVP_PKEY_DHX))
2949e1051a39Sopenharmony_ci        goto err;
2950e1051a39Sopenharmony_ci
2951e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_get_bn_param(pkey1, OSSL_PKEY_PARAM_PUB_KEY,
2952e1051a39Sopenharmony_ci                                         &pubkey))
2953e1051a39Sopenharmony_ci            || !TEST_ptr(pubkey))
2954e1051a39Sopenharmony_ci        goto err;
2955e1051a39Sopenharmony_ci
2956e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_set1_DH(pkey2, noqdh))
2957e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_PKEY_get_id(pkey2), EVP_PKEY_DH))
2958e1051a39Sopenharmony_ci        goto err;
2959e1051a39Sopenharmony_ci
2960e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey2,
2961e1051a39Sopenharmony_ci                                                   OSSL_PKEY_PARAM_PUB_KEY,
2962e1051a39Sopenharmony_ci                                                   pub, sizeof(pub), &len))
2963e1051a39Sopenharmony_ci            || !TEST_size_t_ne(len, 0))
2964e1051a39Sopenharmony_ci        goto err;
2965e1051a39Sopenharmony_ci
2966e1051a39Sopenharmony_ci    ret = 1;
2967e1051a39Sopenharmony_ci err:
2968e1051a39Sopenharmony_ci    BN_free(p);
2969e1051a39Sopenharmony_ci    BN_free(g);
2970e1051a39Sopenharmony_ci    BN_free(pubkey);
2971e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey1);
2972e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey2);
2973e1051a39Sopenharmony_ci    DH_free(x942dh);
2974e1051a39Sopenharmony_ci    DH_free(noqdh);
2975e1051a39Sopenharmony_ci
2976e1051a39Sopenharmony_ci    return ret;
2977e1051a39Sopenharmony_ci}
2978e1051a39Sopenharmony_ci# endif /* !OPENSSL_NO_DEPRECATED_3_0 */
2979e1051a39Sopenharmony_ci#endif /* !OPENSSL_NO_DH */
2980e1051a39Sopenharmony_ci
2981e1051a39Sopenharmony_ci/*
2982e1051a39Sopenharmony_ci * We test what happens with an empty template.  For the sake of this test,
2983e1051a39Sopenharmony_ci * the template must be ignored, and we know that's the case for RSA keys
2984e1051a39Sopenharmony_ci * (this might arguably be a misfeature, but that's what we currently do,
2985e1051a39Sopenharmony_ci * even in provider code, since that's how the legacy RSA implementation
2986e1051a39Sopenharmony_ci * does things)
2987e1051a39Sopenharmony_ci */
2988e1051a39Sopenharmony_cistatic int test_keygen_with_empty_template(int n)
2989e1051a39Sopenharmony_ci{
2990e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
2991e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
2992e1051a39Sopenharmony_ci    EVP_PKEY *tkey = NULL;
2993e1051a39Sopenharmony_ci    int ret = 0;
2994e1051a39Sopenharmony_ci
2995e1051a39Sopenharmony_ci    if (nullprov != NULL)
2996e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
2997e1051a39Sopenharmony_ci
2998e1051a39Sopenharmony_ci    switch (n) {
2999e1051a39Sopenharmony_ci    case 0:
3000e1051a39Sopenharmony_ci        /* We do test with no template at all as well */
3001e1051a39Sopenharmony_ci        if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL)))
3002e1051a39Sopenharmony_ci            goto err;
3003e1051a39Sopenharmony_ci        break;
3004e1051a39Sopenharmony_ci    case 1:
3005e1051a39Sopenharmony_ci        /* Here we create an empty RSA key that serves as our template */
3006e1051a39Sopenharmony_ci        if (!TEST_ptr(tkey = EVP_PKEY_new())
3007e1051a39Sopenharmony_ci            || !TEST_true(EVP_PKEY_set_type(tkey, EVP_PKEY_RSA))
3008e1051a39Sopenharmony_ci            || !TEST_ptr(ctx = EVP_PKEY_CTX_new(tkey, NULL)))
3009e1051a39Sopenharmony_ci            goto err;
3010e1051a39Sopenharmony_ci        break;
3011e1051a39Sopenharmony_ci    }
3012e1051a39Sopenharmony_ci
3013e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
3014e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_keygen(ctx, &pkey), 0))
3015e1051a39Sopenharmony_ci        goto err;
3016e1051a39Sopenharmony_ci
3017e1051a39Sopenharmony_ci    ret = 1;
3018e1051a39Sopenharmony_ci err:
3019e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
3020e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
3021e1051a39Sopenharmony_ci    EVP_PKEY_free(tkey);
3022e1051a39Sopenharmony_ci    return ret;
3023e1051a39Sopenharmony_ci}
3024e1051a39Sopenharmony_ci
3025e1051a39Sopenharmony_ci/*
3026e1051a39Sopenharmony_ci * Test that we fail if we attempt to use an algorithm that is not available
3027e1051a39Sopenharmony_ci * in the current library context (unless we are using an algorithm that
3028e1051a39Sopenharmony_ci * should be made available via legacy codepaths).
3029e1051a39Sopenharmony_ci *
3030e1051a39Sopenharmony_ci * 0:   RSA
3031e1051a39Sopenharmony_ci * 1:   SM2
3032e1051a39Sopenharmony_ci */
3033e1051a39Sopenharmony_cistatic int test_pkey_ctx_fail_without_provider(int tst)
3034e1051a39Sopenharmony_ci{
3035e1051a39Sopenharmony_ci    OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
3036e1051a39Sopenharmony_ci    OSSL_PROVIDER *tmpnullprov = NULL;
3037e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
3038e1051a39Sopenharmony_ci    const char *keytype = NULL;
3039e1051a39Sopenharmony_ci    int expect_null = 0;
3040e1051a39Sopenharmony_ci    int ret = 0;
3041e1051a39Sopenharmony_ci
3042e1051a39Sopenharmony_ci    if (!TEST_ptr(tmpctx))
3043e1051a39Sopenharmony_ci        goto err;
3044e1051a39Sopenharmony_ci
3045e1051a39Sopenharmony_ci    tmpnullprov = OSSL_PROVIDER_load(tmpctx, "null");
3046e1051a39Sopenharmony_ci    if (!TEST_ptr(tmpnullprov))
3047e1051a39Sopenharmony_ci        goto err;
3048e1051a39Sopenharmony_ci
3049e1051a39Sopenharmony_ci    /*
3050e1051a39Sopenharmony_ci     * We check for certain algos in the null provider.
3051e1051a39Sopenharmony_ci     * If an algo is expected to have a provider keymgmt, contructing an
3052e1051a39Sopenharmony_ci     * EVP_PKEY_CTX is expected to fail (return NULL).
3053e1051a39Sopenharmony_ci     * Otherwise, if it's expected to have legacy support, contructing an
3054e1051a39Sopenharmony_ci     * EVP_PKEY_CTX is expected to succeed (return non-NULL).
3055e1051a39Sopenharmony_ci     */
3056e1051a39Sopenharmony_ci    switch (tst) {
3057e1051a39Sopenharmony_ci    case 0:
3058e1051a39Sopenharmony_ci        keytype = "RSA";
3059e1051a39Sopenharmony_ci        expect_null = 1;
3060e1051a39Sopenharmony_ci        break;
3061e1051a39Sopenharmony_ci    case 1:
3062e1051a39Sopenharmony_ci        keytype = "SM2";
3063e1051a39Sopenharmony_ci        expect_null = 1;
3064e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_EC
3065e1051a39Sopenharmony_ci        TEST_info("EC disable, skipping SM2 check...");
3066e1051a39Sopenharmony_ci        goto end;
3067e1051a39Sopenharmony_ci#endif
3068e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_SM2
3069e1051a39Sopenharmony_ci        TEST_info("SM2 disable, skipping SM2 check...");
3070e1051a39Sopenharmony_ci        goto end;
3071e1051a39Sopenharmony_ci#endif
3072e1051a39Sopenharmony_ci        break;
3073e1051a39Sopenharmony_ci    default:
3074e1051a39Sopenharmony_ci        TEST_error("No test for case %d", tst);
3075e1051a39Sopenharmony_ci        goto err;
3076e1051a39Sopenharmony_ci    }
3077e1051a39Sopenharmony_ci
3078e1051a39Sopenharmony_ci    pctx = EVP_PKEY_CTX_new_from_name(tmpctx, keytype, "");
3079e1051a39Sopenharmony_ci    if (expect_null ? !TEST_ptr_null(pctx) : !TEST_ptr(pctx))
3080e1051a39Sopenharmony_ci        goto err;
3081e1051a39Sopenharmony_ci
3082e1051a39Sopenharmony_ci#if defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SM2)
3083e1051a39Sopenharmony_ci end:
3084e1051a39Sopenharmony_ci#endif
3085e1051a39Sopenharmony_ci    ret = 1;
3086e1051a39Sopenharmony_ci
3087e1051a39Sopenharmony_ci err:
3088e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(pctx);
3089e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(tmpnullprov);
3090e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(tmpctx);
3091e1051a39Sopenharmony_ci    return ret;
3092e1051a39Sopenharmony_ci}
3093e1051a39Sopenharmony_ci
3094e1051a39Sopenharmony_cistatic int test_rand_agglomeration(void)
3095e1051a39Sopenharmony_ci{
3096e1051a39Sopenharmony_ci    EVP_RAND *rand;
3097e1051a39Sopenharmony_ci    EVP_RAND_CTX *ctx;
3098e1051a39Sopenharmony_ci    OSSL_PARAM params[3], *p = params;
3099e1051a39Sopenharmony_ci    int res;
3100e1051a39Sopenharmony_ci    unsigned int step = 7;
3101e1051a39Sopenharmony_ci    static unsigned char seed[] = "It does not matter how slowly you go "
3102e1051a39Sopenharmony_ci                                  "as long as you do not stop.";
3103e1051a39Sopenharmony_ci    unsigned char out[sizeof(seed)];
3104e1051a39Sopenharmony_ci
3105e1051a39Sopenharmony_ci    if (!TEST_int_ne(sizeof(seed) % step, 0)
3106e1051a39Sopenharmony_ci            || !TEST_ptr(rand = EVP_RAND_fetch(testctx, "TEST-RAND", testpropq)))
3107e1051a39Sopenharmony_ci        return 0;
3108e1051a39Sopenharmony_ci    ctx = EVP_RAND_CTX_new(rand, NULL);
3109e1051a39Sopenharmony_ci    EVP_RAND_free(rand);
3110e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx))
3111e1051a39Sopenharmony_ci        return 0;
3112e1051a39Sopenharmony_ci
3113e1051a39Sopenharmony_ci    memset(out, 0, sizeof(out));
3114e1051a39Sopenharmony_ci    *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
3115e1051a39Sopenharmony_ci                                             seed, sizeof(seed));
3116e1051a39Sopenharmony_ci    *p++ = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_MAX_REQUEST, &step);
3117e1051a39Sopenharmony_ci    *p = OSSL_PARAM_construct_end();
3118e1051a39Sopenharmony_ci    res = TEST_true(EVP_RAND_CTX_set_params(ctx, params))
3119e1051a39Sopenharmony_ci          && TEST_true(EVP_RAND_generate(ctx, out, sizeof(out), 0, 1, NULL, 0))
3120e1051a39Sopenharmony_ci          && TEST_mem_eq(seed, sizeof(seed), out, sizeof(out));
3121e1051a39Sopenharmony_ci    EVP_RAND_CTX_free(ctx);
3122e1051a39Sopenharmony_ci    return res;
3123e1051a39Sopenharmony_ci}
3124e1051a39Sopenharmony_ci
3125e1051a39Sopenharmony_ci/*
3126e1051a39Sopenharmony_ci * Test that we correctly return the original or "running" IV after
3127e1051a39Sopenharmony_ci * an encryption operation.
3128e1051a39Sopenharmony_ci * Run multiple times for some different relevant algorithms/modes.
3129e1051a39Sopenharmony_ci */
3130e1051a39Sopenharmony_cistatic int test_evp_iv_aes(int idx)
3131e1051a39Sopenharmony_ci{
3132e1051a39Sopenharmony_ci    int ret = 0;
3133e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3134e1051a39Sopenharmony_ci    unsigned char key[16] = {0x4c, 0x43, 0xdb, 0xdd, 0x42, 0x73, 0x47, 0xd1,
3135e1051a39Sopenharmony_ci                             0xe5, 0x62, 0x7d, 0xcd, 0x4d, 0x76, 0x4d, 0x57};
3136e1051a39Sopenharmony_ci    unsigned char init_iv[EVP_MAX_IV_LENGTH] =
3137e1051a39Sopenharmony_ci        {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98, 0x82,
3138e1051a39Sopenharmony_ci         0x5a, 0x55, 0x91, 0x81, 0x42, 0xa8, 0x89, 0x34};
3139e1051a39Sopenharmony_ci    static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
3140e1051a39Sopenharmony_ci                                         9, 10, 11, 12, 13, 14, 15, 16 };
3141e1051a39Sopenharmony_ci    unsigned char ciphertext[32], oiv[16], iv[16];
3142e1051a39Sopenharmony_ci    unsigned char *ref_iv;
3143e1051a39Sopenharmony_ci    unsigned char cbc_state[16] = {0x10, 0x2f, 0x05, 0xcc, 0xc2, 0x55, 0x72, 0xb9,
3144e1051a39Sopenharmony_ci                                   0x88, 0xe6, 0x4a, 0x17, 0x10, 0x74, 0x22, 0x5e};
3145e1051a39Sopenharmony_ci
3146e1051a39Sopenharmony_ci    unsigned char ofb_state[16] = {0x76, 0xe6, 0x66, 0x61, 0xd0, 0x8a, 0xe4, 0x64,
3147e1051a39Sopenharmony_ci                                   0xdd, 0x66, 0xbf, 0x00, 0xf0, 0xe3, 0x6f, 0xfd};
3148e1051a39Sopenharmony_ci    unsigned char cfb_state[16] = {0x77, 0xe4, 0x65, 0x65, 0xd5, 0x8c, 0xe3, 0x6c,
3149e1051a39Sopenharmony_ci                                   0xd4, 0x6c, 0xb4, 0x0c, 0xfd, 0xed, 0x60, 0xed};
3150e1051a39Sopenharmony_ci    unsigned char gcm_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
3151e1051a39Sopenharmony_ci                                   0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
3152e1051a39Sopenharmony_ci    unsigned char ccm_state[7] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b, 0x98};
3153e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_OCB
3154e1051a39Sopenharmony_ci    unsigned char ocb_state[12] = {0x57, 0x71, 0x7d, 0xad, 0xdb, 0x9b,
3155e1051a39Sopenharmony_ci                                   0x98, 0x82, 0x5a, 0x55, 0x91, 0x81};
3156e1051a39Sopenharmony_ci#endif
3157e1051a39Sopenharmony_ci    int len = sizeof(ciphertext);
3158e1051a39Sopenharmony_ci    size_t ivlen, ref_len;
3159e1051a39Sopenharmony_ci    const EVP_CIPHER *type = NULL;
3160e1051a39Sopenharmony_ci    int iv_reset = 0;
3161e1051a39Sopenharmony_ci
3162e1051a39Sopenharmony_ci    if (nullprov != NULL && idx < 6)
3163e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
3164e1051a39Sopenharmony_ci
3165e1051a39Sopenharmony_ci    switch(idx) {
3166e1051a39Sopenharmony_ci    case 0:
3167e1051a39Sopenharmony_ci        type = EVP_aes_128_cbc();
3168e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3169e1051a39Sopenharmony_ci    case 6:
3170e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3171e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-cbc", testpropq);
3172e1051a39Sopenharmony_ci        ref_iv = cbc_state;
3173e1051a39Sopenharmony_ci        ref_len = sizeof(cbc_state);
3174e1051a39Sopenharmony_ci        iv_reset = 1;
3175e1051a39Sopenharmony_ci        break;
3176e1051a39Sopenharmony_ci    case 1:
3177e1051a39Sopenharmony_ci        type = EVP_aes_128_ofb();
3178e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3179e1051a39Sopenharmony_ci    case 7:
3180e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3181e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-ofb", testpropq);
3182e1051a39Sopenharmony_ci        ref_iv = ofb_state;
3183e1051a39Sopenharmony_ci        ref_len = sizeof(ofb_state);
3184e1051a39Sopenharmony_ci        iv_reset = 1;
3185e1051a39Sopenharmony_ci        break;
3186e1051a39Sopenharmony_ci    case 2:
3187e1051a39Sopenharmony_ci        type = EVP_aes_128_cfb();
3188e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3189e1051a39Sopenharmony_ci    case 8:
3190e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3191e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq);
3192e1051a39Sopenharmony_ci        ref_iv = cfb_state;
3193e1051a39Sopenharmony_ci        ref_len = sizeof(cfb_state);
3194e1051a39Sopenharmony_ci        iv_reset = 1;
3195e1051a39Sopenharmony_ci        break;
3196e1051a39Sopenharmony_ci    case 3:
3197e1051a39Sopenharmony_ci        type = EVP_aes_128_gcm();
3198e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3199e1051a39Sopenharmony_ci    case 9:
3200e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3201e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-gcm", testpropq);
3202e1051a39Sopenharmony_ci        ref_iv = gcm_state;
3203e1051a39Sopenharmony_ci        ref_len = sizeof(gcm_state);
3204e1051a39Sopenharmony_ci        break;
3205e1051a39Sopenharmony_ci    case 4:
3206e1051a39Sopenharmony_ci        type = EVP_aes_128_ccm();
3207e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3208e1051a39Sopenharmony_ci    case 10:
3209e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3210e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-ccm", testpropq);
3211e1051a39Sopenharmony_ci        ref_iv = ccm_state;
3212e1051a39Sopenharmony_ci        ref_len = sizeof(ccm_state);
3213e1051a39Sopenharmony_ci        break;
3214e1051a39Sopenharmony_ci#ifdef OPENSSL_NO_OCB
3215e1051a39Sopenharmony_ci    case 5:
3216e1051a39Sopenharmony_ci    case 11:
3217e1051a39Sopenharmony_ci        return 1;
3218e1051a39Sopenharmony_ci#else
3219e1051a39Sopenharmony_ci    case 5:
3220e1051a39Sopenharmony_ci        type = EVP_aes_128_ocb();
3221e1051a39Sopenharmony_ci        /* FALLTHROUGH */
3222e1051a39Sopenharmony_ci    case 11:
3223e1051a39Sopenharmony_ci        type = (type != NULL) ? type :
3224e1051a39Sopenharmony_ci                                EVP_CIPHER_fetch(testctx, "aes-128-ocb", testpropq);
3225e1051a39Sopenharmony_ci        ref_iv = ocb_state;
3226e1051a39Sopenharmony_ci        ref_len = sizeof(ocb_state);
3227e1051a39Sopenharmony_ci        break;
3228e1051a39Sopenharmony_ci#endif
3229e1051a39Sopenharmony_ci    default:
3230e1051a39Sopenharmony_ci        return 0;
3231e1051a39Sopenharmony_ci    }
3232e1051a39Sopenharmony_ci
3233e1051a39Sopenharmony_ci    if (!TEST_ptr(type)
3234e1051a39Sopenharmony_ci            || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
3235e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
3236e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
3237e1051a39Sopenharmony_ci                          (int)sizeof(msg)))
3238e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
3239e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
3240e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
3241e1051a39Sopenharmony_ci        goto err;
3242e1051a39Sopenharmony_ci    ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
3243e1051a39Sopenharmony_ci    if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
3244e1051a39Sopenharmony_ci            || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
3245e1051a39Sopenharmony_ci        goto err;
3246e1051a39Sopenharmony_ci
3247e1051a39Sopenharmony_ci    /* CBC, OFB, and CFB modes: the updated iv must be reset after reinit */
3248e1051a39Sopenharmony_ci    if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
3249e1051a39Sopenharmony_ci        || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
3250e1051a39Sopenharmony_ci        goto err;
3251e1051a39Sopenharmony_ci    if (iv_reset) {
3252e1051a39Sopenharmony_ci        if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
3253e1051a39Sopenharmony_ci            goto err;
3254e1051a39Sopenharmony_ci    } else {
3255e1051a39Sopenharmony_ci        if (!TEST_mem_eq(ref_iv, ivlen, iv, ivlen))
3256e1051a39Sopenharmony_ci            goto err;
3257e1051a39Sopenharmony_ci    }
3258e1051a39Sopenharmony_ci
3259e1051a39Sopenharmony_ci    ret = 1;
3260e1051a39Sopenharmony_cierr:
3261e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
3262e1051a39Sopenharmony_ci    if (idx >= 6)
3263e1051a39Sopenharmony_ci        EVP_CIPHER_free((EVP_CIPHER *)type);
3264e1051a39Sopenharmony_ci    return ret;
3265e1051a39Sopenharmony_ci}
3266e1051a39Sopenharmony_ci
3267e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES
3268e1051a39Sopenharmony_cistatic int test_evp_iv_des(int idx)
3269e1051a39Sopenharmony_ci{
3270e1051a39Sopenharmony_ci    int ret = 0;
3271e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3272e1051a39Sopenharmony_ci    static const unsigned char key[24] = {
3273e1051a39Sopenharmony_ci        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
3274e1051a39Sopenharmony_ci        0xf1, 0xe0, 0xd3, 0xc2, 0xb5, 0xa4, 0x97, 0x86,
3275e1051a39Sopenharmony_ci        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
3276e1051a39Sopenharmony_ci    };
3277e1051a39Sopenharmony_ci    static const unsigned char init_iv[8] = {
3278e1051a39Sopenharmony_ci        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
3279e1051a39Sopenharmony_ci    };
3280e1051a39Sopenharmony_ci    static const unsigned char msg[] = { 1, 2, 3, 4, 5, 6, 7, 8,
3281e1051a39Sopenharmony_ci                                         9, 10, 11, 12, 13, 14, 15, 16 };
3282e1051a39Sopenharmony_ci    unsigned char ciphertext[32], oiv[8], iv[8];
3283e1051a39Sopenharmony_ci    unsigned const char *ref_iv;
3284e1051a39Sopenharmony_ci    static const unsigned char cbc_state_des[8] = {
3285e1051a39Sopenharmony_ci        0x4f, 0xa3, 0x85, 0xcd, 0x8b, 0xf3, 0x06, 0x2a
3286e1051a39Sopenharmony_ci    };
3287e1051a39Sopenharmony_ci    static const unsigned char cbc_state_3des[8] = {
3288e1051a39Sopenharmony_ci        0x35, 0x27, 0x7d, 0x65, 0x6c, 0xfb, 0x50, 0xd9
3289e1051a39Sopenharmony_ci    };
3290e1051a39Sopenharmony_ci    static const unsigned char ofb_state_des[8] = {
3291e1051a39Sopenharmony_ci        0xa7, 0x0d, 0x1d, 0x45, 0xf9, 0x96, 0x3f, 0x2c
3292e1051a39Sopenharmony_ci    };
3293e1051a39Sopenharmony_ci    static const unsigned char ofb_state_3des[8] = {
3294e1051a39Sopenharmony_ci        0xab, 0x16, 0x24, 0xbb, 0x5b, 0xac, 0xed, 0x5e
3295e1051a39Sopenharmony_ci    };
3296e1051a39Sopenharmony_ci    static const unsigned char cfb_state_des[8] = {
3297e1051a39Sopenharmony_ci        0x91, 0xeb, 0x6d, 0x29, 0x4b, 0x08, 0xbd, 0x73
3298e1051a39Sopenharmony_ci    };
3299e1051a39Sopenharmony_ci    static const unsigned char cfb_state_3des[8] = {
3300e1051a39Sopenharmony_ci        0x34, 0xdd, 0xfb, 0x47, 0x33, 0x1c, 0x61, 0xf7
3301e1051a39Sopenharmony_ci    };
3302e1051a39Sopenharmony_ci    int len = sizeof(ciphertext);
3303e1051a39Sopenharmony_ci    size_t ivlen, ref_len;
3304e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
3305e1051a39Sopenharmony_ci
3306e1051a39Sopenharmony_ci    if (lgcyprov == NULL && idx < 3)
3307e1051a39Sopenharmony_ci        return TEST_skip("Test requires legacy provider to be loaded");
3308e1051a39Sopenharmony_ci
3309e1051a39Sopenharmony_ci    switch(idx) {
3310e1051a39Sopenharmony_ci    case 0:
3311e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-cbc", testpropq);
3312e1051a39Sopenharmony_ci        ref_iv = cbc_state_des;
3313e1051a39Sopenharmony_ci        ref_len = sizeof(cbc_state_des);
3314e1051a39Sopenharmony_ci        break;
3315e1051a39Sopenharmony_ci    case 1:
3316e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-ofb", testpropq);
3317e1051a39Sopenharmony_ci        ref_iv = ofb_state_des;
3318e1051a39Sopenharmony_ci        ref_len = sizeof(ofb_state_des);
3319e1051a39Sopenharmony_ci        break;
3320e1051a39Sopenharmony_ci    case 2:
3321e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-cfb", testpropq);
3322e1051a39Sopenharmony_ci        ref_iv = cfb_state_des;
3323e1051a39Sopenharmony_ci        ref_len = sizeof(cfb_state_des);
3324e1051a39Sopenharmony_ci        break;
3325e1051a39Sopenharmony_ci    case 3:
3326e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-ede3-cbc", testpropq);
3327e1051a39Sopenharmony_ci        ref_iv = cbc_state_3des;
3328e1051a39Sopenharmony_ci        ref_len = sizeof(cbc_state_3des);
3329e1051a39Sopenharmony_ci        break;
3330e1051a39Sopenharmony_ci    case 4:
3331e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-ede3-ofb", testpropq);
3332e1051a39Sopenharmony_ci        ref_iv = ofb_state_3des;
3333e1051a39Sopenharmony_ci        ref_len = sizeof(ofb_state_3des);
3334e1051a39Sopenharmony_ci        break;
3335e1051a39Sopenharmony_ci    case 5:
3336e1051a39Sopenharmony_ci        type = EVP_CIPHER_fetch(testctx, "des-ede3-cfb", testpropq);
3337e1051a39Sopenharmony_ci        ref_iv = cfb_state_3des;
3338e1051a39Sopenharmony_ci        ref_len = sizeof(cfb_state_3des);
3339e1051a39Sopenharmony_ci        break;
3340e1051a39Sopenharmony_ci    default:
3341e1051a39Sopenharmony_ci        return 0;
3342e1051a39Sopenharmony_ci    }
3343e1051a39Sopenharmony_ci
3344e1051a39Sopenharmony_ci    if (!TEST_ptr(type)
3345e1051a39Sopenharmony_ci            || !TEST_ptr((ctx = EVP_CIPHER_CTX_new()))
3346e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptInit_ex(ctx, type, NULL, key, init_iv))
3347e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptUpdate(ctx, ciphertext, &len, msg,
3348e1051a39Sopenharmony_ci                          (int)sizeof(msg)))
3349e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_CTX_get_original_iv(ctx, oiv, sizeof(oiv)))
3350e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv)))
3351e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptFinal_ex(ctx, ciphertext, &len)))
3352e1051a39Sopenharmony_ci        goto err;
3353e1051a39Sopenharmony_ci    ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
3354e1051a39Sopenharmony_ci    if (!TEST_mem_eq(init_iv, ivlen, oiv, ivlen)
3355e1051a39Sopenharmony_ci            || !TEST_mem_eq(ref_iv, ref_len, iv, ivlen))
3356e1051a39Sopenharmony_ci        goto err;
3357e1051a39Sopenharmony_ci
3358e1051a39Sopenharmony_ci    if (!TEST_true(EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL))
3359e1051a39Sopenharmony_ci        || !TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, iv, sizeof(iv))))
3360e1051a39Sopenharmony_ci        goto err;
3361e1051a39Sopenharmony_ci    if (!TEST_mem_eq(init_iv, ivlen, iv, ivlen))
3362e1051a39Sopenharmony_ci        goto err;
3363e1051a39Sopenharmony_ci
3364e1051a39Sopenharmony_ci    ret = 1;
3365e1051a39Sopenharmony_cierr:
3366e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
3367e1051a39Sopenharmony_ci    EVP_CIPHER_free(type);
3368e1051a39Sopenharmony_ci    return ret;
3369e1051a39Sopenharmony_ci}
3370e1051a39Sopenharmony_ci#endif
3371e1051a39Sopenharmony_ci
3372e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_BF
3373e1051a39Sopenharmony_cistatic int test_evp_bf_default_keylen(int idx)
3374e1051a39Sopenharmony_ci{
3375e1051a39Sopenharmony_ci    int ret = 0;
3376e1051a39Sopenharmony_ci    static const char *algos[4] = {
3377e1051a39Sopenharmony_ci        "bf-ecb", "bf-cbc", "bf-cfb", "bf-ofb"
3378e1051a39Sopenharmony_ci    };
3379e1051a39Sopenharmony_ci    int ivlen[4] = { 0, 8, 8, 8 };
3380e1051a39Sopenharmony_ci    EVP_CIPHER *cipher = NULL;
3381e1051a39Sopenharmony_ci
3382e1051a39Sopenharmony_ci    if (lgcyprov == NULL)
3383e1051a39Sopenharmony_ci        return TEST_skip("Test requires legacy provider to be loaded");
3384e1051a39Sopenharmony_ci
3385e1051a39Sopenharmony_ci    if (!TEST_ptr(cipher = EVP_CIPHER_fetch(testctx, algos[idx], testpropq))
3386e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_CIPHER_get_key_length(cipher), 16)
3387e1051a39Sopenharmony_ci            || !TEST_int_eq(EVP_CIPHER_get_iv_length(cipher), ivlen[idx]))
3388e1051a39Sopenharmony_ci        goto err;
3389e1051a39Sopenharmony_ci
3390e1051a39Sopenharmony_ci    ret = 1;
3391e1051a39Sopenharmony_cierr:
3392e1051a39Sopenharmony_ci    EVP_CIPHER_free(cipher);
3393e1051a39Sopenharmony_ci    return ret;
3394e1051a39Sopenharmony_ci}
3395e1051a39Sopenharmony_ci#endif
3396e1051a39Sopenharmony_ci
3397e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
3398e1051a39Sopenharmony_cistatic int ecpub_nids[] = {
3399e1051a39Sopenharmony_ci    NID_brainpoolP256r1, NID_X9_62_prime256v1,
3400e1051a39Sopenharmony_ci    NID_secp384r1, NID_secp521r1,
3401e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC2M
3402e1051a39Sopenharmony_ci    NID_sect233k1, NID_sect233r1, NID_sect283r1,
3403e1051a39Sopenharmony_ci    NID_sect409k1, NID_sect409r1, NID_sect571k1, NID_sect571r1,
3404e1051a39Sopenharmony_ci# endif
3405e1051a39Sopenharmony_ci    NID_brainpoolP384r1, NID_brainpoolP512r1
3406e1051a39Sopenharmony_ci};
3407e1051a39Sopenharmony_ci
3408e1051a39Sopenharmony_cistatic int test_ecpub(int idx)
3409e1051a39Sopenharmony_ci{
3410e1051a39Sopenharmony_ci    int ret = 0, len, savelen;
3411e1051a39Sopenharmony_ci    int nid;
3412e1051a39Sopenharmony_ci    unsigned char buf[1024];
3413e1051a39Sopenharmony_ci    unsigned char *p;
3414e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
3415e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
3416e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
3417e1051a39Sopenharmony_ci    const unsigned char *q;
3418e1051a39Sopenharmony_ci    EVP_PKEY *pkey2 = NULL;
3419e1051a39Sopenharmony_ci    EC_KEY *ec = NULL;
3420e1051a39Sopenharmony_ci# endif
3421e1051a39Sopenharmony_ci
3422e1051a39Sopenharmony_ci    if (nullprov != NULL)
3423e1051a39Sopenharmony_ci        return TEST_skip("Test does not support a non-default library context");
3424e1051a39Sopenharmony_ci
3425e1051a39Sopenharmony_ci    nid = ecpub_nids[idx];
3426e1051a39Sopenharmony_ci
3427e1051a39Sopenharmony_ci    ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
3428e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx)
3429e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
3430e1051a39Sopenharmony_ci        || !TEST_int_gt(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid), 0)
3431e1051a39Sopenharmony_ci        || !TEST_true(EVP_PKEY_keygen(ctx, &pkey)))
3432e1051a39Sopenharmony_ci        goto done;
3433e1051a39Sopenharmony_ci    len = i2d_PublicKey(pkey, NULL);
3434e1051a39Sopenharmony_ci    savelen = len;
3435e1051a39Sopenharmony_ci    if (!TEST_int_ge(len, 1)
3436e1051a39Sopenharmony_ci        || !TEST_int_lt(len, 1024))
3437e1051a39Sopenharmony_ci        goto done;
3438e1051a39Sopenharmony_ci    p = buf;
3439e1051a39Sopenharmony_ci    len = i2d_PublicKey(pkey, &p);
3440e1051a39Sopenharmony_ci    if (!TEST_int_ge(len, 1)
3441e1051a39Sopenharmony_ci            || !TEST_int_eq(len, savelen))
3442e1051a39Sopenharmony_ci        goto done;
3443e1051a39Sopenharmony_ci
3444e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
3445e1051a39Sopenharmony_ci    /* Now try to decode the just-created DER. */
3446e1051a39Sopenharmony_ci    q = buf;
3447e1051a39Sopenharmony_ci    if (!TEST_ptr((pkey2 = EVP_PKEY_new()))
3448e1051a39Sopenharmony_ci            || !TEST_ptr((ec = EC_KEY_new_by_curve_name(nid)))
3449e1051a39Sopenharmony_ci            || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey2, ec)))
3450e1051a39Sopenharmony_ci        goto done;
3451e1051a39Sopenharmony_ci    /* EC_KEY ownership transferred */
3452e1051a39Sopenharmony_ci    ec = NULL;
3453e1051a39Sopenharmony_ci    if (!TEST_ptr(d2i_PublicKey(EVP_PKEY_EC, &pkey2, &q, savelen)))
3454e1051a39Sopenharmony_ci        goto done;
3455e1051a39Sopenharmony_ci    /* The keys should match. */
3456e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
3457e1051a39Sopenharmony_ci        goto done;
3458e1051a39Sopenharmony_ci# endif
3459e1051a39Sopenharmony_ci
3460e1051a39Sopenharmony_ci    ret = 1;
3461e1051a39Sopenharmony_ci
3462e1051a39Sopenharmony_ci done:
3463e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
3464e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
3465e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
3466e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey2);
3467e1051a39Sopenharmony_ci    EC_KEY_free(ec);
3468e1051a39Sopenharmony_ci# endif
3469e1051a39Sopenharmony_ci    return ret;
3470e1051a39Sopenharmony_ci}
3471e1051a39Sopenharmony_ci#endif
3472e1051a39Sopenharmony_ci
3473e1051a39Sopenharmony_cistatic int test_EVP_rsa_pss_with_keygen_bits(void)
3474e1051a39Sopenharmony_ci{
3475e1051a39Sopenharmony_ci    int ret = 0;
3476e1051a39Sopenharmony_ci    EVP_PKEY_CTX *ctx = NULL;
3477e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
3478e1051a39Sopenharmony_ci    EVP_MD *md;
3479e1051a39Sopenharmony_ci
3480e1051a39Sopenharmony_ci    md = EVP_MD_fetch(testctx, "sha256", testpropq);
3481e1051a39Sopenharmony_ci    ret = TEST_ptr(md)
3482e1051a39Sopenharmony_ci        && TEST_ptr((ctx = EVP_PKEY_CTX_new_from_name(testctx, "RSA-PSS", testpropq)))
3483e1051a39Sopenharmony_ci        && TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0)
3484e1051a39Sopenharmony_ci        && TEST_int_gt(EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 512), 0)
3485e1051a39Sopenharmony_ci        && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx, md), 0)
3486e1051a39Sopenharmony_ci        && TEST_true(EVP_PKEY_keygen(ctx, &pkey));
3487e1051a39Sopenharmony_ci
3488e1051a39Sopenharmony_ci    EVP_MD_free(md);
3489e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
3490e1051a39Sopenharmony_ci    EVP_PKEY_CTX_free(ctx);
3491e1051a39Sopenharmony_ci    return ret;
3492e1051a39Sopenharmony_ci}
3493e1051a39Sopenharmony_ci
3494e1051a39Sopenharmony_cistatic int test_EVP_rsa_pss_set_saltlen(void)
3495e1051a39Sopenharmony_ci{
3496e1051a39Sopenharmony_ci    int ret = 0;
3497e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
3498e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pkey_ctx = NULL;
3499e1051a39Sopenharmony_ci    EVP_MD *sha256 = NULL;
3500e1051a39Sopenharmony_ci    EVP_MD_CTX *sha256_ctx = NULL;
3501e1051a39Sopenharmony_ci    int saltlen = 9999; /* buggy EVP_PKEY_CTX_get_rsa_pss_saltlen() didn't update this */
3502e1051a39Sopenharmony_ci    const int test_value = 32;
3503e1051a39Sopenharmony_ci
3504e1051a39Sopenharmony_ci    ret = TEST_ptr(pkey = load_example_rsa_key())
3505e1051a39Sopenharmony_ci        && TEST_ptr(sha256 = EVP_MD_fetch(testctx, "sha256", NULL))
3506e1051a39Sopenharmony_ci        && TEST_ptr(sha256_ctx = EVP_MD_CTX_new())
3507e1051a39Sopenharmony_ci        && TEST_true(EVP_DigestSignInit(sha256_ctx, &pkey_ctx, sha256, NULL, pkey))
3508e1051a39Sopenharmony_ci        && TEST_true(EVP_PKEY_CTX_set_rsa_padding(pkey_ctx, RSA_PKCS1_PSS_PADDING))
3509e1051a39Sopenharmony_ci        && TEST_int_gt(EVP_PKEY_CTX_set_rsa_pss_saltlen(pkey_ctx, test_value), 0)
3510e1051a39Sopenharmony_ci        && TEST_int_gt(EVP_PKEY_CTX_get_rsa_pss_saltlen(pkey_ctx, &saltlen), 0)
3511e1051a39Sopenharmony_ci        && TEST_int_eq(saltlen, test_value);
3512e1051a39Sopenharmony_ci
3513e1051a39Sopenharmony_ci    EVP_MD_CTX_free(sha256_ctx);
3514e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
3515e1051a39Sopenharmony_ci    EVP_MD_free(sha256);
3516e1051a39Sopenharmony_ci
3517e1051a39Sopenharmony_ci    return ret;
3518e1051a39Sopenharmony_ci}
3519e1051a39Sopenharmony_ci
3520e1051a39Sopenharmony_cistatic int success = 1;
3521e1051a39Sopenharmony_cistatic void md_names(const char *name, void *vctx)
3522e1051a39Sopenharmony_ci{
3523e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = (OSSL_LIB_CTX *)vctx;
3524e1051a39Sopenharmony_ci    /* Force a namemap update */
3525e1051a39Sopenharmony_ci    EVP_CIPHER *aes128 = EVP_CIPHER_fetch(ctx, "AES-128-CBC", NULL);
3526e1051a39Sopenharmony_ci
3527e1051a39Sopenharmony_ci    if (!TEST_ptr(aes128))
3528e1051a39Sopenharmony_ci        success = 0;
3529e1051a39Sopenharmony_ci
3530e1051a39Sopenharmony_ci    EVP_CIPHER_free(aes128);
3531e1051a39Sopenharmony_ci}
3532e1051a39Sopenharmony_ci
3533e1051a39Sopenharmony_ci/*
3534e1051a39Sopenharmony_ci * Test that changing the namemap in a user callback works in a names_do_all
3535e1051a39Sopenharmony_ci * function.
3536e1051a39Sopenharmony_ci */
3537e1051a39Sopenharmony_cistatic int test_names_do_all(void)
3538e1051a39Sopenharmony_ci{
3539e1051a39Sopenharmony_ci    /* We use a custom libctx so that we know the state of the namemap */
3540e1051a39Sopenharmony_ci    OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
3541e1051a39Sopenharmony_ci    EVP_MD *sha256 = NULL;
3542e1051a39Sopenharmony_ci    int testresult = 0;
3543e1051a39Sopenharmony_ci
3544e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx))
3545e1051a39Sopenharmony_ci        goto err;
3546e1051a39Sopenharmony_ci
3547e1051a39Sopenharmony_ci    sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
3548e1051a39Sopenharmony_ci    if (!TEST_ptr(sha256))
3549e1051a39Sopenharmony_ci        goto err;
3550e1051a39Sopenharmony_ci
3551e1051a39Sopenharmony_ci    /*
3552e1051a39Sopenharmony_ci     * We loop through all the names for a given digest. This should still work
3553e1051a39Sopenharmony_ci     * even if the namemap changes part way through.
3554e1051a39Sopenharmony_ci     */
3555e1051a39Sopenharmony_ci    if (!TEST_true(EVP_MD_names_do_all(sha256, md_names, ctx)))
3556e1051a39Sopenharmony_ci        goto err;
3557e1051a39Sopenharmony_ci
3558e1051a39Sopenharmony_ci    if (!TEST_true(success))
3559e1051a39Sopenharmony_ci        goto err;
3560e1051a39Sopenharmony_ci
3561e1051a39Sopenharmony_ci    testresult = 1;
3562e1051a39Sopenharmony_ci err:
3563e1051a39Sopenharmony_ci    EVP_MD_free(sha256);
3564e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(ctx);
3565e1051a39Sopenharmony_ci    return testresult;
3566e1051a39Sopenharmony_ci}
3567e1051a39Sopenharmony_ci
3568e1051a39Sopenharmony_citypedef struct {
3569e1051a39Sopenharmony_ci    const char *cipher;
3570e1051a39Sopenharmony_ci    const unsigned char *key;
3571e1051a39Sopenharmony_ci    const unsigned char *iv;
3572e1051a39Sopenharmony_ci    const unsigned char *input;
3573e1051a39Sopenharmony_ci    const unsigned char *expected;
3574e1051a39Sopenharmony_ci    const unsigned char *tag;
3575e1051a39Sopenharmony_ci    size_t ivlen; /* 0 if we do not need to set a specific IV len */
3576e1051a39Sopenharmony_ci    size_t inlen;
3577e1051a39Sopenharmony_ci    size_t expectedlen;
3578e1051a39Sopenharmony_ci    size_t taglen;
3579e1051a39Sopenharmony_ci    int keyfirst;
3580e1051a39Sopenharmony_ci    int initenc;
3581e1051a39Sopenharmony_ci    int finalenc;
3582e1051a39Sopenharmony_ci} EVP_INIT_TEST_st;
3583e1051a39Sopenharmony_ci
3584e1051a39Sopenharmony_cistatic const EVP_INIT_TEST_st evp_init_tests[] = {
3585e1051a39Sopenharmony_ci    {
3586e1051a39Sopenharmony_ci        "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
3587e1051a39Sopenharmony_ci        cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
3588e1051a39Sopenharmony_ci        0, 1, 0, 1
3589e1051a39Sopenharmony_ci    },
3590e1051a39Sopenharmony_ci    {
3591e1051a39Sopenharmony_ci        "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
3592e1051a39Sopenharmony_ci        gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
3593e1051a39Sopenharmony_ci        sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
3594e1051a39Sopenharmony_ci        sizeof(gcmDefaultTag), 1, 0, 1
3595e1051a39Sopenharmony_ci    },
3596e1051a39Sopenharmony_ci    {
3597e1051a39Sopenharmony_ci        "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbPlaintext,
3598e1051a39Sopenharmony_ci        cfbCiphertext, NULL, 0, sizeof(cfbPlaintext), sizeof(cfbCiphertext),
3599e1051a39Sopenharmony_ci        0, 0, 0, 1
3600e1051a39Sopenharmony_ci    },
3601e1051a39Sopenharmony_ci    {
3602e1051a39Sopenharmony_ci        "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultPlaintext,
3603e1051a39Sopenharmony_ci        gcmDefaultCiphertext, gcmDefaultTag, sizeof(iGCMDefaultIV),
3604e1051a39Sopenharmony_ci        sizeof(gcmDefaultPlaintext), sizeof(gcmDefaultCiphertext),
3605e1051a39Sopenharmony_ci        sizeof(gcmDefaultTag), 0, 0, 1
3606e1051a39Sopenharmony_ci    },
3607e1051a39Sopenharmony_ci    {
3608e1051a39Sopenharmony_ci        "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
3609e1051a39Sopenharmony_ci        cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
3610e1051a39Sopenharmony_ci        0, 1, 1, 0
3611e1051a39Sopenharmony_ci    },
3612e1051a39Sopenharmony_ci    {
3613e1051a39Sopenharmony_ci        "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
3614e1051a39Sopenharmony_ci        gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
3615e1051a39Sopenharmony_ci        sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
3616e1051a39Sopenharmony_ci        sizeof(gcmDefaultTag), 1, 1, 0
3617e1051a39Sopenharmony_ci    },
3618e1051a39Sopenharmony_ci    {
3619e1051a39Sopenharmony_ci        "aes-128-cfb", kCFBDefaultKey, iCFBIV, cfbCiphertext,
3620e1051a39Sopenharmony_ci        cfbPlaintext, NULL, 0, sizeof(cfbCiphertext), sizeof(cfbPlaintext),
3621e1051a39Sopenharmony_ci        0, 0, 1, 0
3622e1051a39Sopenharmony_ci    },
3623e1051a39Sopenharmony_ci    {
3624e1051a39Sopenharmony_ci        "aes-256-gcm", kGCMDefaultKey, iGCMDefaultIV, gcmDefaultCiphertext,
3625e1051a39Sopenharmony_ci        gcmDefaultPlaintext, gcmDefaultTag, sizeof(iGCMDefaultIV),
3626e1051a39Sopenharmony_ci        sizeof(gcmDefaultCiphertext), sizeof(gcmDefaultPlaintext),
3627e1051a39Sopenharmony_ci        sizeof(gcmDefaultTag), 0, 1, 0
3628e1051a39Sopenharmony_ci    }
3629e1051a39Sopenharmony_ci};
3630e1051a39Sopenharmony_ci
3631e1051a39Sopenharmony_cistatic int evp_init_seq_set_iv(EVP_CIPHER_CTX *ctx, const EVP_INIT_TEST_st *t)
3632e1051a39Sopenharmony_ci{
3633e1051a39Sopenharmony_ci    int res = 0;
3634e1051a39Sopenharmony_ci
3635e1051a39Sopenharmony_ci    if (t->ivlen != 0) {
3636e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen, NULL), 0))
3637e1051a39Sopenharmony_ci            goto err;
3638e1051a39Sopenharmony_ci    }
3639e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv, -1)))
3640e1051a39Sopenharmony_ci        goto err;
3641e1051a39Sopenharmony_ci    res = 1;
3642e1051a39Sopenharmony_ci err:
3643e1051a39Sopenharmony_ci    return res;
3644e1051a39Sopenharmony_ci}
3645e1051a39Sopenharmony_ci
3646e1051a39Sopenharmony_ci/*
3647e1051a39Sopenharmony_ci * Test step-wise cipher initialization via EVP_CipherInit_ex where the
3648e1051a39Sopenharmony_ci * arguments are given one at a time and a final adjustment to the enc
3649e1051a39Sopenharmony_ci * parameter sets the correct operation.
3650e1051a39Sopenharmony_ci */
3651e1051a39Sopenharmony_cistatic int test_evp_init_seq(int idx)
3652e1051a39Sopenharmony_ci{
3653e1051a39Sopenharmony_ci    int outlen1, outlen2;
3654e1051a39Sopenharmony_ci    int testresult = 0;
3655e1051a39Sopenharmony_ci    unsigned char outbuf[1024];
3656e1051a39Sopenharmony_ci    unsigned char tag[16];
3657e1051a39Sopenharmony_ci    const EVP_INIT_TEST_st *t = &evp_init_tests[idx];
3658e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3659e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
3660e1051a39Sopenharmony_ci    size_t taglen = sizeof(tag);
3661e1051a39Sopenharmony_ci    char *errmsg = NULL;
3662e1051a39Sopenharmony_ci
3663e1051a39Sopenharmony_ci    ctx = EVP_CIPHER_CTX_new();
3664e1051a39Sopenharmony_ci    if (ctx == NULL) {
3665e1051a39Sopenharmony_ci        errmsg = "CTX_ALLOC";
3666e1051a39Sopenharmony_ci        goto err;
3667e1051a39Sopenharmony_ci    }
3668e1051a39Sopenharmony_ci    if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq))) {
3669e1051a39Sopenharmony_ci        errmsg = "CIPHER_FETCH";
3670e1051a39Sopenharmony_ci        goto err;
3671e1051a39Sopenharmony_ci    }
3672e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, t->initenc))) {
3673e1051a39Sopenharmony_ci        errmsg = "EMPTY_ENC_INIT";
3674e1051a39Sopenharmony_ci        goto err;
3675e1051a39Sopenharmony_ci    }
3676e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
3677e1051a39Sopenharmony_ci        errmsg = "PADDING";
3678e1051a39Sopenharmony_ci        goto err;
3679e1051a39Sopenharmony_ci    }
3680e1051a39Sopenharmony_ci    if (t->keyfirst && !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
3681e1051a39Sopenharmony_ci        errmsg = "KEY_INIT (before iv)";
3682e1051a39Sopenharmony_ci        goto err;
3683e1051a39Sopenharmony_ci    }
3684e1051a39Sopenharmony_ci    if (!evp_init_seq_set_iv(ctx, t)) {
3685e1051a39Sopenharmony_ci        errmsg = "IV_INIT";
3686e1051a39Sopenharmony_ci        goto err;
3687e1051a39Sopenharmony_ci    }
3688e1051a39Sopenharmony_ci    if (t->keyfirst == 0 &&  !TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, t->key, NULL, -1))) {
3689e1051a39Sopenharmony_ci        errmsg = "KEY_INIT (after iv)";
3690e1051a39Sopenharmony_ci        goto err;
3691e1051a39Sopenharmony_ci    }
3692e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, t->finalenc))) {
3693e1051a39Sopenharmony_ci        errmsg = "FINAL_ENC_INIT";
3694e1051a39Sopenharmony_ci        goto err;
3695e1051a39Sopenharmony_ci    }
3696e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
3697e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE";
3698e1051a39Sopenharmony_ci        goto err;
3699e1051a39Sopenharmony_ci    }
3700e1051a39Sopenharmony_ci    if (t->finalenc == 0 && t->tag != NULL) {
3701e1051a39Sopenharmony_ci        /* Set expected tag */
3702e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
3703e1051a39Sopenharmony_ci                                           t->taglen, (void *)t->tag), 0)) {
3704e1051a39Sopenharmony_ci            errmsg = "SET_TAG";
3705e1051a39Sopenharmony_ci            goto err;
3706e1051a39Sopenharmony_ci        }
3707e1051a39Sopenharmony_ci    }
3708e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
3709e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL";
3710e1051a39Sopenharmony_ci        goto err;
3711e1051a39Sopenharmony_ci    }
3712e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
3713e1051a39Sopenharmony_ci        errmsg = "WRONG_RESULT";
3714e1051a39Sopenharmony_ci        goto err;
3715e1051a39Sopenharmony_ci    }
3716e1051a39Sopenharmony_ci    if (t->finalenc != 0 && t->tag != NULL) {
3717e1051a39Sopenharmony_ci        if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
3718e1051a39Sopenharmony_ci            errmsg = "GET_TAG";
3719e1051a39Sopenharmony_ci            goto err;
3720e1051a39Sopenharmony_ci        }
3721e1051a39Sopenharmony_ci        if (!TEST_mem_eq(t->tag, t->taglen, tag, taglen)) {
3722e1051a39Sopenharmony_ci            errmsg = "TAG_ERROR";
3723e1051a39Sopenharmony_ci            goto err;
3724e1051a39Sopenharmony_ci        }
3725e1051a39Sopenharmony_ci    }
3726e1051a39Sopenharmony_ci    testresult = 1;
3727e1051a39Sopenharmony_ci err:
3728e1051a39Sopenharmony_ci    if (errmsg != NULL)
3729e1051a39Sopenharmony_ci        TEST_info("evp_init_test %d: %s", idx, errmsg);
3730e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
3731e1051a39Sopenharmony_ci    EVP_CIPHER_free(type);
3732e1051a39Sopenharmony_ci    return testresult;
3733e1051a39Sopenharmony_ci}
3734e1051a39Sopenharmony_ci
3735e1051a39Sopenharmony_citypedef struct {
3736e1051a39Sopenharmony_ci    const unsigned char *input;
3737e1051a39Sopenharmony_ci    const unsigned char *expected;
3738e1051a39Sopenharmony_ci    size_t inlen;
3739e1051a39Sopenharmony_ci    size_t expectedlen;
3740e1051a39Sopenharmony_ci    int enc;
3741e1051a39Sopenharmony_ci} EVP_RESET_TEST_st;
3742e1051a39Sopenharmony_ci
3743e1051a39Sopenharmony_cistatic const EVP_RESET_TEST_st evp_reset_tests[] = {
3744e1051a39Sopenharmony_ci    {
3745e1051a39Sopenharmony_ci        cfbPlaintext, cfbCiphertext,
3746e1051a39Sopenharmony_ci        sizeof(cfbPlaintext), sizeof(cfbCiphertext), 1
3747e1051a39Sopenharmony_ci    },
3748e1051a39Sopenharmony_ci    {
3749e1051a39Sopenharmony_ci        cfbCiphertext, cfbPlaintext,
3750e1051a39Sopenharmony_ci        sizeof(cfbCiphertext), sizeof(cfbPlaintext), 0
3751e1051a39Sopenharmony_ci    }
3752e1051a39Sopenharmony_ci};
3753e1051a39Sopenharmony_ci
3754e1051a39Sopenharmony_ci/*
3755e1051a39Sopenharmony_ci * Test a reset of a cipher via EVP_CipherInit_ex after the cipher has already
3756e1051a39Sopenharmony_ci * been used.
3757e1051a39Sopenharmony_ci */
3758e1051a39Sopenharmony_cistatic int test_evp_reset(int idx)
3759e1051a39Sopenharmony_ci{
3760e1051a39Sopenharmony_ci    const EVP_RESET_TEST_st *t = &evp_reset_tests[idx];
3761e1051a39Sopenharmony_ci    int outlen1, outlen2;
3762e1051a39Sopenharmony_ci    int testresult = 0;
3763e1051a39Sopenharmony_ci    unsigned char outbuf[1024];
3764e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3765e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
3766e1051a39Sopenharmony_ci    char *errmsg = NULL;
3767e1051a39Sopenharmony_ci
3768e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
3769e1051a39Sopenharmony_ci        errmsg = "CTX_ALLOC";
3770e1051a39Sopenharmony_ci        goto err;
3771e1051a39Sopenharmony_ci    }
3772e1051a39Sopenharmony_ci    if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-128-cfb", testpropq))) {
3773e1051a39Sopenharmony_ci        errmsg = "CIPHER_FETCH";
3774e1051a39Sopenharmony_ci        goto err;
3775e1051a39Sopenharmony_ci    }
3776e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
3777e1051a39Sopenharmony_ci        errmsg = "CIPHER_INIT";
3778e1051a39Sopenharmony_ci        goto err;
3779e1051a39Sopenharmony_ci    }
3780e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
3781e1051a39Sopenharmony_ci        errmsg = "PADDING";
3782e1051a39Sopenharmony_ci        goto err;
3783e1051a39Sopenharmony_ci    }
3784e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
3785e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE";
3786e1051a39Sopenharmony_ci        goto err;
3787e1051a39Sopenharmony_ci    }
3788e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
3789e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL";
3790e1051a39Sopenharmony_ci        goto err;
3791e1051a39Sopenharmony_ci    }
3792e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
3793e1051a39Sopenharmony_ci        errmsg = "WRONG_RESULT";
3794e1051a39Sopenharmony_ci        goto err;
3795e1051a39Sopenharmony_ci    }
3796e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1))) {
3797e1051a39Sopenharmony_ci        errmsg = "CIPHER_REINIT";
3798e1051a39Sopenharmony_ci        goto err;
3799e1051a39Sopenharmony_ci    }
3800e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, t->input, t->inlen))) {
3801e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE (reinit)";
3802e1051a39Sopenharmony_ci        goto err;
3803e1051a39Sopenharmony_ci    }
3804e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
3805e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL (reinit)";
3806e1051a39Sopenharmony_ci        goto err;
3807e1051a39Sopenharmony_ci    }
3808e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->expected, t->expectedlen, outbuf, outlen1 + outlen2)) {
3809e1051a39Sopenharmony_ci        errmsg = "WRONG_RESULT (reinit)";
3810e1051a39Sopenharmony_ci        goto err;
3811e1051a39Sopenharmony_ci    }
3812e1051a39Sopenharmony_ci    testresult = 1;
3813e1051a39Sopenharmony_ci err:
3814e1051a39Sopenharmony_ci    if (errmsg != NULL)
3815e1051a39Sopenharmony_ci        TEST_info("test_evp_reset %d: %s", idx, errmsg);
3816e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
3817e1051a39Sopenharmony_ci    EVP_CIPHER_free(type);
3818e1051a39Sopenharmony_ci    return testresult;
3819e1051a39Sopenharmony_ci}
3820e1051a39Sopenharmony_ci
3821e1051a39Sopenharmony_citypedef struct {
3822e1051a39Sopenharmony_ci    const char *cipher;
3823e1051a39Sopenharmony_ci    int enc;
3824e1051a39Sopenharmony_ci} EVP_UPDATED_IV_TEST_st;
3825e1051a39Sopenharmony_ci
3826e1051a39Sopenharmony_cistatic const EVP_UPDATED_IV_TEST_st evp_updated_iv_tests[] = {
3827e1051a39Sopenharmony_ci    {
3828e1051a39Sopenharmony_ci        "aes-128-cfb", 1
3829e1051a39Sopenharmony_ci    },
3830e1051a39Sopenharmony_ci    {
3831e1051a39Sopenharmony_ci        "aes-128-cfb", 0
3832e1051a39Sopenharmony_ci    },
3833e1051a39Sopenharmony_ci    {
3834e1051a39Sopenharmony_ci        "aes-128-cfb1", 1
3835e1051a39Sopenharmony_ci    },
3836e1051a39Sopenharmony_ci    {
3837e1051a39Sopenharmony_ci        "aes-128-cfb1", 0
3838e1051a39Sopenharmony_ci    },
3839e1051a39Sopenharmony_ci    {
3840e1051a39Sopenharmony_ci        "aes-128-cfb8", 1
3841e1051a39Sopenharmony_ci    },
3842e1051a39Sopenharmony_ci    {
3843e1051a39Sopenharmony_ci        "aes-128-cfb8", 0
3844e1051a39Sopenharmony_ci    },
3845e1051a39Sopenharmony_ci    {
3846e1051a39Sopenharmony_ci        "aes-128-ofb", 1
3847e1051a39Sopenharmony_ci    },
3848e1051a39Sopenharmony_ci    {
3849e1051a39Sopenharmony_ci        "aes-128-ofb", 0
3850e1051a39Sopenharmony_ci    },
3851e1051a39Sopenharmony_ci    {
3852e1051a39Sopenharmony_ci        "aes-128-ctr", 1
3853e1051a39Sopenharmony_ci    },
3854e1051a39Sopenharmony_ci    {
3855e1051a39Sopenharmony_ci        "aes-128-ctr", 0
3856e1051a39Sopenharmony_ci    },
3857e1051a39Sopenharmony_ci    {
3858e1051a39Sopenharmony_ci        "aes-128-cbc", 1
3859e1051a39Sopenharmony_ci    },
3860e1051a39Sopenharmony_ci    {
3861e1051a39Sopenharmony_ci        "aes-128-cbc", 0
3862e1051a39Sopenharmony_ci    }
3863e1051a39Sopenharmony_ci};
3864e1051a39Sopenharmony_ci
3865e1051a39Sopenharmony_ci/*
3866e1051a39Sopenharmony_ci * Test that the IV in the context is updated during a crypto operation for CFB
3867e1051a39Sopenharmony_ci * and OFB.
3868e1051a39Sopenharmony_ci */
3869e1051a39Sopenharmony_cistatic int test_evp_updated_iv(int idx)
3870e1051a39Sopenharmony_ci{
3871e1051a39Sopenharmony_ci    const EVP_UPDATED_IV_TEST_st *t = &evp_updated_iv_tests[idx];
3872e1051a39Sopenharmony_ci    int outlen1, outlen2;
3873e1051a39Sopenharmony_ci    int testresult = 0;
3874e1051a39Sopenharmony_ci    unsigned char outbuf[1024];
3875e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3876e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
3877e1051a39Sopenharmony_ci    unsigned char updated_iv[EVP_MAX_IV_LENGTH];
3878e1051a39Sopenharmony_ci    int iv_len;
3879e1051a39Sopenharmony_ci    char *errmsg = NULL;
3880e1051a39Sopenharmony_ci
3881e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
3882e1051a39Sopenharmony_ci        errmsg = "CTX_ALLOC";
3883e1051a39Sopenharmony_ci        goto err;
3884e1051a39Sopenharmony_ci    }
3885e1051a39Sopenharmony_ci    if ((type = EVP_CIPHER_fetch(testctx, t->cipher, testpropq)) == NULL) {
3886e1051a39Sopenharmony_ci        TEST_info("cipher %s not supported, skipping", t->cipher);
3887e1051a39Sopenharmony_ci        goto ok;
3888e1051a39Sopenharmony_ci    }
3889e1051a39Sopenharmony_ci
3890e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, kCFBDefaultKey, iCFBIV, t->enc))) {
3891e1051a39Sopenharmony_ci        errmsg = "CIPHER_INIT";
3892e1051a39Sopenharmony_ci        goto err;
3893e1051a39Sopenharmony_ci    }
3894e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_CTX_set_padding(ctx, 0))) {
3895e1051a39Sopenharmony_ci        errmsg = "PADDING";
3896e1051a39Sopenharmony_ci        goto err;
3897e1051a39Sopenharmony_ci    }
3898e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, cfbPlaintext, sizeof(cfbPlaintext)))) {
3899e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE";
3900e1051a39Sopenharmony_ci        goto err;
3901e1051a39Sopenharmony_ci    }
3902e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_CTX_get_updated_iv(ctx, updated_iv, sizeof(updated_iv)))) {
3903e1051a39Sopenharmony_ci        errmsg = "CIPHER_CTX_GET_UPDATED_IV";
3904e1051a39Sopenharmony_ci        goto err;
3905e1051a39Sopenharmony_ci    }
3906e1051a39Sopenharmony_ci    if (!TEST_true(iv_len = EVP_CIPHER_CTX_get_iv_length(ctx))) {
3907e1051a39Sopenharmony_ci        errmsg = "CIPHER_CTX_GET_IV_LEN";
3908e1051a39Sopenharmony_ci        goto err;
3909e1051a39Sopenharmony_ci    }
3910e1051a39Sopenharmony_ci    if (!TEST_mem_ne(iCFBIV, sizeof(iCFBIV), updated_iv, iv_len)) {
3911e1051a39Sopenharmony_ci        errmsg = "IV_NOT_UPDATED";
3912e1051a39Sopenharmony_ci        goto err;
3913e1051a39Sopenharmony_ci    }
3914e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
3915e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL";
3916e1051a39Sopenharmony_ci        goto err;
3917e1051a39Sopenharmony_ci    }
3918e1051a39Sopenharmony_ci ok:
3919e1051a39Sopenharmony_ci    testresult = 1;
3920e1051a39Sopenharmony_ci err:
3921e1051a39Sopenharmony_ci    if (errmsg != NULL)
3922e1051a39Sopenharmony_ci        TEST_info("test_evp_updated_iv %d: %s", idx, errmsg);
3923e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
3924e1051a39Sopenharmony_ci    EVP_CIPHER_free(type);
3925e1051a39Sopenharmony_ci    return testresult;
3926e1051a39Sopenharmony_ci}
3927e1051a39Sopenharmony_ci
3928e1051a39Sopenharmony_citypedef struct {
3929e1051a39Sopenharmony_ci    const unsigned char *iv1;
3930e1051a39Sopenharmony_ci    const unsigned char *iv2;
3931e1051a39Sopenharmony_ci    const unsigned char *expected1;
3932e1051a39Sopenharmony_ci    const unsigned char *expected2;
3933e1051a39Sopenharmony_ci    const unsigned char *tag1;
3934e1051a39Sopenharmony_ci    const unsigned char *tag2;
3935e1051a39Sopenharmony_ci    size_t ivlen1;
3936e1051a39Sopenharmony_ci    size_t ivlen2;
3937e1051a39Sopenharmony_ci    size_t expectedlen1;
3938e1051a39Sopenharmony_ci    size_t expectedlen2;
3939e1051a39Sopenharmony_ci} TEST_GCM_IV_REINIT_st;
3940e1051a39Sopenharmony_ci
3941e1051a39Sopenharmony_cistatic const TEST_GCM_IV_REINIT_st gcm_reinit_tests[] = {
3942e1051a39Sopenharmony_ci    {
3943e1051a39Sopenharmony_ci        iGCMResetIV1, iGCMResetIV2, gcmResetCiphertext1, gcmResetCiphertext2,
3944e1051a39Sopenharmony_ci        gcmResetTag1, gcmResetTag2, sizeof(iGCMResetIV1), sizeof(iGCMResetIV2),
3945e1051a39Sopenharmony_ci        sizeof(gcmResetCiphertext1), sizeof(gcmResetCiphertext2)
3946e1051a39Sopenharmony_ci    },
3947e1051a39Sopenharmony_ci    {
3948e1051a39Sopenharmony_ci        iGCMResetIV2, iGCMResetIV1, gcmResetCiphertext2, gcmResetCiphertext1,
3949e1051a39Sopenharmony_ci        gcmResetTag2, gcmResetTag1, sizeof(iGCMResetIV2), sizeof(iGCMResetIV1),
3950e1051a39Sopenharmony_ci        sizeof(gcmResetCiphertext2), sizeof(gcmResetCiphertext1)
3951e1051a39Sopenharmony_ci    }
3952e1051a39Sopenharmony_ci};
3953e1051a39Sopenharmony_ci
3954e1051a39Sopenharmony_cistatic int test_gcm_reinit(int idx)
3955e1051a39Sopenharmony_ci{
3956e1051a39Sopenharmony_ci    int outlen1, outlen2, outlen3;
3957e1051a39Sopenharmony_ci    int testresult = 0;
3958e1051a39Sopenharmony_ci    unsigned char outbuf[1024];
3959e1051a39Sopenharmony_ci    unsigned char tag[16];
3960e1051a39Sopenharmony_ci    const TEST_GCM_IV_REINIT_st *t = &gcm_reinit_tests[idx];
3961e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL;
3962e1051a39Sopenharmony_ci    EVP_CIPHER *type = NULL;
3963e1051a39Sopenharmony_ci    size_t taglen = sizeof(tag);
3964e1051a39Sopenharmony_ci    char *errmsg = NULL;
3965e1051a39Sopenharmony_ci
3966e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) {
3967e1051a39Sopenharmony_ci        errmsg = "CTX_ALLOC";
3968e1051a39Sopenharmony_ci        goto err;
3969e1051a39Sopenharmony_ci    }
3970e1051a39Sopenharmony_ci    if (!TEST_ptr(type = EVP_CIPHER_fetch(testctx, "aes-256-gcm", testpropq))) {
3971e1051a39Sopenharmony_ci        errmsg = "CIPHER_FETCH";
3972e1051a39Sopenharmony_ci        goto err;
3973e1051a39Sopenharmony_ci    }
3974e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, type, NULL, NULL, NULL, 1))) {
3975e1051a39Sopenharmony_ci        errmsg = "ENC_INIT";
3976e1051a39Sopenharmony_ci        goto err;
3977e1051a39Sopenharmony_ci    }
3978e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen1, NULL), 0)) {
3979e1051a39Sopenharmony_ci        errmsg = "SET_IVLEN1";
3980e1051a39Sopenharmony_ci        goto err;
3981e1051a39Sopenharmony_ci    }
3982e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, kGCMResetKey, t->iv1, 1))) {
3983e1051a39Sopenharmony_ci        errmsg = "SET_IV1";
3984e1051a39Sopenharmony_ci        goto err;
3985e1051a39Sopenharmony_ci    }
3986e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
3987e1051a39Sopenharmony_ci        errmsg = "AAD1";
3988e1051a39Sopenharmony_ci        goto err;
3989e1051a39Sopenharmony_ci    }
3990e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_set_padding(ctx, 0);
3991e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
3992e1051a39Sopenharmony_ci                                    sizeof(gcmResetPlaintext)))) {
3993e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE1";
3994e1051a39Sopenharmony_ci        goto err;
3995e1051a39Sopenharmony_ci    }
3996e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
3997e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL1";
3998e1051a39Sopenharmony_ci        goto err;
3999e1051a39Sopenharmony_ci    }
4000e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->expected1, t->expectedlen1, outbuf, outlen1 + outlen2)) {
4001e1051a39Sopenharmony_ci        errmsg = "WRONG_RESULT1";
4002e1051a39Sopenharmony_ci        goto err;
4003e1051a39Sopenharmony_ci    }
4004e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
4005e1051a39Sopenharmony_ci        errmsg = "GET_TAG1";
4006e1051a39Sopenharmony_ci        goto err;
4007e1051a39Sopenharmony_ci    }
4008e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->tag1, taglen, tag, taglen)) {
4009e1051a39Sopenharmony_ci        errmsg = "TAG_ERROR1";
4010e1051a39Sopenharmony_ci        goto err;
4011e1051a39Sopenharmony_ci    }
4012e1051a39Sopenharmony_ci    /* Now reinit */
4013e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, t->ivlen2, NULL), 0)) {
4014e1051a39Sopenharmony_ci        errmsg = "SET_IVLEN2";
4015e1051a39Sopenharmony_ci        goto err;
4016e1051a39Sopenharmony_ci    }
4017e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherInit_ex(ctx, NULL, NULL, NULL, t->iv2, -1))) {
4018e1051a39Sopenharmony_ci        errmsg = "SET_IV2";
4019e1051a39Sopenharmony_ci        goto err;
4020e1051a39Sopenharmony_ci    }
4021e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, NULL, &outlen3, gcmAAD, sizeof(gcmAAD)))) {
4022e1051a39Sopenharmony_ci        errmsg = "AAD2";
4023e1051a39Sopenharmony_ci        goto err;
4024e1051a39Sopenharmony_ci    }
4025e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherUpdate(ctx, outbuf, &outlen1, gcmResetPlaintext,
4026e1051a39Sopenharmony_ci                                    sizeof(gcmResetPlaintext)))) {
4027e1051a39Sopenharmony_ci        errmsg = "CIPHER_UPDATE2";
4028e1051a39Sopenharmony_ci        goto err;
4029e1051a39Sopenharmony_ci    }
4030e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CipherFinal_ex(ctx, outbuf + outlen1, &outlen2))) {
4031e1051a39Sopenharmony_ci        errmsg = "CIPHER_FINAL2";
4032e1051a39Sopenharmony_ci        goto err;
4033e1051a39Sopenharmony_ci    }
4034e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->expected2, t->expectedlen2, outbuf, outlen1 + outlen2)) {
4035e1051a39Sopenharmony_ci        errmsg = "WRONG_RESULT2";
4036e1051a39Sopenharmony_ci        goto err;
4037e1051a39Sopenharmony_ci    }
4038e1051a39Sopenharmony_ci    if (!TEST_int_gt(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen, tag), 0)) {
4039e1051a39Sopenharmony_ci        errmsg = "GET_TAG2";
4040e1051a39Sopenharmony_ci        goto err;
4041e1051a39Sopenharmony_ci    }
4042e1051a39Sopenharmony_ci    if (!TEST_mem_eq(t->tag2, taglen, tag, taglen)) {
4043e1051a39Sopenharmony_ci        errmsg = "TAG_ERROR2";
4044e1051a39Sopenharmony_ci        goto err;
4045e1051a39Sopenharmony_ci    }
4046e1051a39Sopenharmony_ci    testresult = 1;
4047e1051a39Sopenharmony_ci err:
4048e1051a39Sopenharmony_ci    if (errmsg != NULL)
4049e1051a39Sopenharmony_ci        TEST_info("evp_init_test %d: %s", idx, errmsg);
4050e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
4051e1051a39Sopenharmony_ci    EVP_CIPHER_free(type);
4052e1051a39Sopenharmony_ci    return testresult;
4053e1051a39Sopenharmony_ci}
4054e1051a39Sopenharmony_ci
4055e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
4056e1051a39Sopenharmony_cistatic EVP_PKEY_METHOD *custom_pmeth =  NULL;
4057e1051a39Sopenharmony_cistatic const EVP_PKEY_METHOD *orig_pmeth = NULL;
4058e1051a39Sopenharmony_ci
4059e1051a39Sopenharmony_ci# define EVP_PKEY_CTRL_MY_COMMAND 9999
4060e1051a39Sopenharmony_ci
4061e1051a39Sopenharmony_cistatic int custom_pmeth_init(EVP_PKEY_CTX *ctx)
4062e1051a39Sopenharmony_ci{
4063e1051a39Sopenharmony_ci    int (*pinit)(EVP_PKEY_CTX *ctx);
4064e1051a39Sopenharmony_ci
4065e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_init(orig_pmeth, &pinit);
4066e1051a39Sopenharmony_ci    return pinit(ctx);
4067e1051a39Sopenharmony_ci}
4068e1051a39Sopenharmony_ci
4069e1051a39Sopenharmony_cistatic void custom_pmeth_cleanup(EVP_PKEY_CTX *ctx)
4070e1051a39Sopenharmony_ci{
4071e1051a39Sopenharmony_ci    void (*pcleanup)(EVP_PKEY_CTX *ctx);
4072e1051a39Sopenharmony_ci
4073e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_cleanup(orig_pmeth, &pcleanup);
4074e1051a39Sopenharmony_ci    pcleanup(ctx);
4075e1051a39Sopenharmony_ci}
4076e1051a39Sopenharmony_ci
4077e1051a39Sopenharmony_cistatic int custom_pmeth_sign(EVP_PKEY_CTX *ctx, unsigned char *out,
4078e1051a39Sopenharmony_ci                             size_t *outlen, const unsigned char *in,
4079e1051a39Sopenharmony_ci                             size_t inlen)
4080e1051a39Sopenharmony_ci{
4081e1051a39Sopenharmony_ci    int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
4082e1051a39Sopenharmony_ci                 const unsigned char *tbs, size_t tbslen);
4083e1051a39Sopenharmony_ci
4084e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_sign(orig_pmeth, NULL, &psign);
4085e1051a39Sopenharmony_ci    return psign(ctx, out, outlen, in, inlen);
4086e1051a39Sopenharmony_ci}
4087e1051a39Sopenharmony_ci
4088e1051a39Sopenharmony_cistatic int custom_pmeth_digestsign(EVP_MD_CTX *ctx, unsigned char *sig,
4089e1051a39Sopenharmony_ci                                   size_t *siglen, const unsigned char *tbs,
4090e1051a39Sopenharmony_ci                                   size_t tbslen)
4091e1051a39Sopenharmony_ci{
4092e1051a39Sopenharmony_ci    int (*pdigestsign)(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
4093e1051a39Sopenharmony_ci                       const unsigned char *tbs, size_t tbslen);
4094e1051a39Sopenharmony_ci
4095e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_digestsign(orig_pmeth, &pdigestsign);
4096e1051a39Sopenharmony_ci    return pdigestsign(ctx, sig, siglen, tbs, tbslen);
4097e1051a39Sopenharmony_ci}
4098e1051a39Sopenharmony_ci
4099e1051a39Sopenharmony_cistatic int custom_pmeth_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
4100e1051a39Sopenharmony_ci                               size_t *keylen)
4101e1051a39Sopenharmony_ci{
4102e1051a39Sopenharmony_ci    int (*pderive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
4103e1051a39Sopenharmony_ci
4104e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_derive(orig_pmeth, NULL, &pderive);
4105e1051a39Sopenharmony_ci    return pderive(ctx, key, keylen);
4106e1051a39Sopenharmony_ci}
4107e1051a39Sopenharmony_ci
4108e1051a39Sopenharmony_cistatic int custom_pmeth_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
4109e1051a39Sopenharmony_ci{
4110e1051a39Sopenharmony_ci    int (*pcopy)(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src);
4111e1051a39Sopenharmony_ci
4112e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_copy(orig_pmeth, &pcopy);
4113e1051a39Sopenharmony_ci    return pcopy(dst, src);
4114e1051a39Sopenharmony_ci}
4115e1051a39Sopenharmony_ci
4116e1051a39Sopenharmony_cistatic int ctrl_called;
4117e1051a39Sopenharmony_ci
4118e1051a39Sopenharmony_cistatic int custom_pmeth_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
4119e1051a39Sopenharmony_ci{
4120e1051a39Sopenharmony_ci    int (*pctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2);
4121e1051a39Sopenharmony_ci
4122e1051a39Sopenharmony_ci    EVP_PKEY_meth_get_ctrl(orig_pmeth, &pctrl, NULL);
4123e1051a39Sopenharmony_ci
4124e1051a39Sopenharmony_ci    if (type == EVP_PKEY_CTRL_MY_COMMAND) {
4125e1051a39Sopenharmony_ci        ctrl_called = 1;
4126e1051a39Sopenharmony_ci        return 1;
4127e1051a39Sopenharmony_ci    }
4128e1051a39Sopenharmony_ci
4129e1051a39Sopenharmony_ci    return pctrl(ctx, type, p1, p2);
4130e1051a39Sopenharmony_ci}
4131e1051a39Sopenharmony_ci
4132e1051a39Sopenharmony_cistatic int test_custom_pmeth(int idx)
4133e1051a39Sopenharmony_ci{
4134e1051a39Sopenharmony_ci    EVP_PKEY_CTX *pctx = NULL;
4135e1051a39Sopenharmony_ci    EVP_MD_CTX *ctx = NULL;
4136e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
4137e1051a39Sopenharmony_ci    int id, orig_id, orig_flags;
4138e1051a39Sopenharmony_ci    int testresult = 0;
4139e1051a39Sopenharmony_ci    size_t reslen;
4140e1051a39Sopenharmony_ci    unsigned char *res = NULL;
4141e1051a39Sopenharmony_ci    unsigned char msg[] = { 'H', 'e', 'l', 'l', 'o' };
4142e1051a39Sopenharmony_ci    const EVP_MD *md = EVP_sha256();
4143e1051a39Sopenharmony_ci    int doderive = 0;
4144e1051a39Sopenharmony_ci
4145e1051a39Sopenharmony_ci    ctrl_called = 0;
4146e1051a39Sopenharmony_ci
4147e1051a39Sopenharmony_ci    /* We call deprecated APIs so this test doesn't support a custom libctx */
4148e1051a39Sopenharmony_ci    if (testctx != NULL)
4149e1051a39Sopenharmony_ci        return 1;
4150e1051a39Sopenharmony_ci
4151e1051a39Sopenharmony_ci    switch(idx) {
4152e1051a39Sopenharmony_ci    case 0:
4153e1051a39Sopenharmony_ci    case 6:
4154e1051a39Sopenharmony_ci        id = EVP_PKEY_RSA;
4155e1051a39Sopenharmony_ci        pkey = load_example_rsa_key();
4156e1051a39Sopenharmony_ci        break;
4157e1051a39Sopenharmony_ci    case 1:
4158e1051a39Sopenharmony_ci    case 7:
4159e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DSA
4160e1051a39Sopenharmony_ci        id = EVP_PKEY_DSA;
4161e1051a39Sopenharmony_ci        pkey = load_example_dsa_key();
4162e1051a39Sopenharmony_ci        break;
4163e1051a39Sopenharmony_ci# else
4164e1051a39Sopenharmony_ci        return 1;
4165e1051a39Sopenharmony_ci# endif
4166e1051a39Sopenharmony_ci    case 2:
4167e1051a39Sopenharmony_ci    case 8:
4168e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
4169e1051a39Sopenharmony_ci        id = EVP_PKEY_EC;
4170e1051a39Sopenharmony_ci        pkey = load_example_ec_key();
4171e1051a39Sopenharmony_ci        break;
4172e1051a39Sopenharmony_ci# else
4173e1051a39Sopenharmony_ci        return 1;
4174e1051a39Sopenharmony_ci# endif
4175e1051a39Sopenharmony_ci    case 3:
4176e1051a39Sopenharmony_ci    case 9:
4177e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
4178e1051a39Sopenharmony_ci        id = EVP_PKEY_ED25519;
4179e1051a39Sopenharmony_ci        md = NULL;
4180e1051a39Sopenharmony_ci        pkey = load_example_ed25519_key();
4181e1051a39Sopenharmony_ci        break;
4182e1051a39Sopenharmony_ci# else
4183e1051a39Sopenharmony_ci        return 1;
4184e1051a39Sopenharmony_ci# endif
4185e1051a39Sopenharmony_ci    case 4:
4186e1051a39Sopenharmony_ci    case 10:
4187e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DH
4188e1051a39Sopenharmony_ci        id = EVP_PKEY_DH;
4189e1051a39Sopenharmony_ci        doderive = 1;
4190e1051a39Sopenharmony_ci        pkey = load_example_dh_key();
4191e1051a39Sopenharmony_ci        break;
4192e1051a39Sopenharmony_ci# else
4193e1051a39Sopenharmony_ci        return 1;
4194e1051a39Sopenharmony_ci# endif
4195e1051a39Sopenharmony_ci    case 5:
4196e1051a39Sopenharmony_ci    case 11:
4197e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_EC
4198e1051a39Sopenharmony_ci        id = EVP_PKEY_X25519;
4199e1051a39Sopenharmony_ci        doderive = 1;
4200e1051a39Sopenharmony_ci        pkey = load_example_x25519_key();
4201e1051a39Sopenharmony_ci        break;
4202e1051a39Sopenharmony_ci# else
4203e1051a39Sopenharmony_ci        return 1;
4204e1051a39Sopenharmony_ci# endif
4205e1051a39Sopenharmony_ci    default:
4206e1051a39Sopenharmony_ci        TEST_error("Should not happen");
4207e1051a39Sopenharmony_ci        goto err;
4208e1051a39Sopenharmony_ci    }
4209e1051a39Sopenharmony_ci
4210e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
4211e1051a39Sopenharmony_ci        goto err;
4212e1051a39Sopenharmony_ci
4213e1051a39Sopenharmony_ci    if (idx < 6) {
4214e1051a39Sopenharmony_ci        if (!TEST_true(evp_pkey_is_provided(pkey)))
4215e1051a39Sopenharmony_ci            goto err;
4216e1051a39Sopenharmony_ci    } else {
4217e1051a39Sopenharmony_ci        EVP_PKEY *tmp = pkey;
4218e1051a39Sopenharmony_ci
4219e1051a39Sopenharmony_ci        /* Convert to a legacy key */
4220e1051a39Sopenharmony_ci        pkey = EVP_PKEY_new();
4221e1051a39Sopenharmony_ci        if (!TEST_ptr(pkey)) {
4222e1051a39Sopenharmony_ci            pkey = tmp;
4223e1051a39Sopenharmony_ci            goto err;
4224e1051a39Sopenharmony_ci        }
4225e1051a39Sopenharmony_ci        if (!TEST_true(evp_pkey_copy_downgraded(&pkey, tmp))) {
4226e1051a39Sopenharmony_ci            EVP_PKEY_free(tmp);
4227e1051a39Sopenharmony_ci            goto err;
4228e1051a39Sopenharmony_ci        }
4229e1051a39Sopenharmony_ci        EVP_PKEY_free(tmp);
4230e1051a39Sopenharmony_ci        if (!TEST_true(evp_pkey_is_legacy(pkey)))
4231e1051a39Sopenharmony_ci            goto err;
4232e1051a39Sopenharmony_ci    }
4233e1051a39Sopenharmony_ci
4234e1051a39Sopenharmony_ci    if (!TEST_ptr(orig_pmeth = EVP_PKEY_meth_find(id))
4235e1051a39Sopenharmony_ci            || !TEST_ptr(pkey))
4236e1051a39Sopenharmony_ci        goto err;
4237e1051a39Sopenharmony_ci
4238e1051a39Sopenharmony_ci    EVP_PKEY_meth_get0_info(&orig_id, &orig_flags, orig_pmeth);
4239e1051a39Sopenharmony_ci    if (!TEST_int_eq(orig_id, id)
4240e1051a39Sopenharmony_ci            || !TEST_ptr(custom_pmeth = EVP_PKEY_meth_new(id, orig_flags)))
4241e1051a39Sopenharmony_ci        goto err;
4242e1051a39Sopenharmony_ci
4243e1051a39Sopenharmony_ci    if (id == EVP_PKEY_ED25519) {
4244e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_digestsign(custom_pmeth, custom_pmeth_digestsign);
4245e1051a39Sopenharmony_ci    } if (id == EVP_PKEY_DH || id == EVP_PKEY_X25519) {
4246e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_derive(custom_pmeth, NULL, custom_pmeth_derive);
4247e1051a39Sopenharmony_ci    } else {
4248e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_sign(custom_pmeth, NULL, custom_pmeth_sign);
4249e1051a39Sopenharmony_ci    }
4250e1051a39Sopenharmony_ci    if (id != EVP_PKEY_ED25519 && id != EVP_PKEY_X25519) {
4251e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_init(custom_pmeth, custom_pmeth_init);
4252e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_cleanup(custom_pmeth, custom_pmeth_cleanup);
4253e1051a39Sopenharmony_ci        EVP_PKEY_meth_set_copy(custom_pmeth, custom_pmeth_copy);
4254e1051a39Sopenharmony_ci    }
4255e1051a39Sopenharmony_ci    EVP_PKEY_meth_set_ctrl(custom_pmeth, custom_pmeth_ctrl, NULL);
4256e1051a39Sopenharmony_ci    if (!TEST_true(EVP_PKEY_meth_add0(custom_pmeth)))
4257e1051a39Sopenharmony_ci        goto err;
4258e1051a39Sopenharmony_ci
4259e1051a39Sopenharmony_ci    if (doderive) {
4260e1051a39Sopenharmony_ci        pctx = EVP_PKEY_CTX_new(pkey, NULL);
4261e1051a39Sopenharmony_ci        if (!TEST_ptr(pctx)
4262e1051a39Sopenharmony_ci                || !TEST_int_eq(EVP_PKEY_derive_init(pctx), 1)
4263e1051a39Sopenharmony_ci                || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
4264e1051a39Sopenharmony_ci                                                EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
4265e1051a39Sopenharmony_ci                                1)
4266e1051a39Sopenharmony_ci                || !TEST_int_eq(ctrl_called, 1)
4267e1051a39Sopenharmony_ci                || !TEST_int_ge(EVP_PKEY_derive_set_peer(pctx, pkey), 1)
4268e1051a39Sopenharmony_ci                || !TEST_int_ge(EVP_PKEY_derive(pctx, NULL, &reslen), 1)
4269e1051a39Sopenharmony_ci                || !TEST_ptr(res = OPENSSL_malloc(reslen))
4270e1051a39Sopenharmony_ci                || !TEST_int_ge(EVP_PKEY_derive(pctx, res, &reslen), 1))
4271e1051a39Sopenharmony_ci            goto err;
4272e1051a39Sopenharmony_ci    } else {
4273e1051a39Sopenharmony_ci        ctx = EVP_MD_CTX_new();
4274e1051a39Sopenharmony_ci        reslen = EVP_PKEY_size(pkey);
4275e1051a39Sopenharmony_ci        res = OPENSSL_malloc(reslen);
4276e1051a39Sopenharmony_ci        if (!TEST_ptr(ctx)
4277e1051a39Sopenharmony_ci                || !TEST_ptr(res)
4278e1051a39Sopenharmony_ci                || !TEST_true(EVP_DigestSignInit(ctx, &pctx, md, NULL, pkey))
4279e1051a39Sopenharmony_ci                || !TEST_int_ge(EVP_PKEY_CTX_ctrl(pctx, -1, -1,
4280e1051a39Sopenharmony_ci                                                EVP_PKEY_CTRL_MY_COMMAND, 0, NULL),
4281e1051a39Sopenharmony_ci                                1)
4282e1051a39Sopenharmony_ci                || !TEST_int_eq(ctrl_called, 1))
4283e1051a39Sopenharmony_ci            goto err;
4284e1051a39Sopenharmony_ci
4285e1051a39Sopenharmony_ci        if (id == EVP_PKEY_ED25519) {
4286e1051a39Sopenharmony_ci            if (!TEST_true(EVP_DigestSign(ctx, res, &reslen, msg, sizeof(msg))))
4287e1051a39Sopenharmony_ci                goto err;
4288e1051a39Sopenharmony_ci        } else {
4289e1051a39Sopenharmony_ci            if (!TEST_true(EVP_DigestUpdate(ctx, msg, sizeof(msg)))
4290e1051a39Sopenharmony_ci                    || !TEST_true(EVP_DigestSignFinal(ctx, res, &reslen)))
4291e1051a39Sopenharmony_ci                goto err;
4292e1051a39Sopenharmony_ci        }
4293e1051a39Sopenharmony_ci    }
4294e1051a39Sopenharmony_ci
4295e1051a39Sopenharmony_ci    testresult = 1;
4296e1051a39Sopenharmony_ci err:
4297e1051a39Sopenharmony_ci    OPENSSL_free(res);
4298e1051a39Sopenharmony_ci    EVP_MD_CTX_free(ctx);
4299e1051a39Sopenharmony_ci    if (doderive)
4300e1051a39Sopenharmony_ci        EVP_PKEY_CTX_free(pctx);
4301e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
4302e1051a39Sopenharmony_ci    EVP_PKEY_meth_remove(custom_pmeth);
4303e1051a39Sopenharmony_ci    EVP_PKEY_meth_free(custom_pmeth);
4304e1051a39Sopenharmony_ci    custom_pmeth = NULL;
4305e1051a39Sopenharmony_ci    return testresult;
4306e1051a39Sopenharmony_ci}
4307e1051a39Sopenharmony_ci
4308e1051a39Sopenharmony_cistatic int test_evp_md_cipher_meth(void)
4309e1051a39Sopenharmony_ci{
4310e1051a39Sopenharmony_ci    EVP_MD *md = EVP_MD_meth_dup(EVP_sha256());
4311e1051a39Sopenharmony_ci    EVP_CIPHER *ciph = EVP_CIPHER_meth_dup(EVP_aes_128_cbc());
4312e1051a39Sopenharmony_ci    int testresult = 0;
4313e1051a39Sopenharmony_ci
4314e1051a39Sopenharmony_ci    if (!TEST_ptr(md) || !TEST_ptr(ciph))
4315e1051a39Sopenharmony_ci        goto err;
4316e1051a39Sopenharmony_ci
4317e1051a39Sopenharmony_ci    testresult = 1;
4318e1051a39Sopenharmony_ci
4319e1051a39Sopenharmony_ci err:
4320e1051a39Sopenharmony_ci    EVP_MD_meth_free(md);
4321e1051a39Sopenharmony_ci    EVP_CIPHER_meth_free(ciph);
4322e1051a39Sopenharmony_ci
4323e1051a39Sopenharmony_ci    return testresult;
4324e1051a39Sopenharmony_ci}
4325e1051a39Sopenharmony_ci
4326e1051a39Sopenharmony_citypedef struct {
4327e1051a39Sopenharmony_ci        int data;
4328e1051a39Sopenharmony_ci} custom_dgst_ctx;
4329e1051a39Sopenharmony_ci
4330e1051a39Sopenharmony_cistatic int custom_md_init_called = 0;
4331e1051a39Sopenharmony_cistatic int custom_md_cleanup_called = 0;
4332e1051a39Sopenharmony_ci
4333e1051a39Sopenharmony_cistatic int custom_md_init(EVP_MD_CTX *ctx)
4334e1051a39Sopenharmony_ci{
4335e1051a39Sopenharmony_ci    custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
4336e1051a39Sopenharmony_ci
4337e1051a39Sopenharmony_ci    if (p == NULL)
4338e1051a39Sopenharmony_ci        return 0;
4339e1051a39Sopenharmony_ci
4340e1051a39Sopenharmony_ci    custom_md_init_called++;
4341e1051a39Sopenharmony_ci    return 1;
4342e1051a39Sopenharmony_ci}
4343e1051a39Sopenharmony_ci
4344e1051a39Sopenharmony_cistatic int custom_md_cleanup(EVP_MD_CTX *ctx)
4345e1051a39Sopenharmony_ci{
4346e1051a39Sopenharmony_ci    custom_dgst_ctx *p = EVP_MD_CTX_md_data(ctx);
4347e1051a39Sopenharmony_ci
4348e1051a39Sopenharmony_ci    if (p == NULL)
4349e1051a39Sopenharmony_ci        /* Nothing to do */
4350e1051a39Sopenharmony_ci        return 1;
4351e1051a39Sopenharmony_ci
4352e1051a39Sopenharmony_ci    custom_md_cleanup_called++;
4353e1051a39Sopenharmony_ci    return 1;
4354e1051a39Sopenharmony_ci}
4355e1051a39Sopenharmony_ci
4356e1051a39Sopenharmony_cistatic int test_custom_md_meth(void)
4357e1051a39Sopenharmony_ci{
4358e1051a39Sopenharmony_ci    EVP_MD_CTX *mdctx = NULL;
4359e1051a39Sopenharmony_ci    EVP_MD *tmp = NULL;
4360e1051a39Sopenharmony_ci    char mess[] = "Test Message\n";
4361e1051a39Sopenharmony_ci    unsigned char md_value[EVP_MAX_MD_SIZE];
4362e1051a39Sopenharmony_ci    unsigned int md_len;
4363e1051a39Sopenharmony_ci    int testresult = 0;
4364e1051a39Sopenharmony_ci    int nid;
4365e1051a39Sopenharmony_ci
4366e1051a39Sopenharmony_ci    /*
4367e1051a39Sopenharmony_ci     * We are testing deprecated functions. We don't support a non-default
4368e1051a39Sopenharmony_ci     * library context in this test.
4369e1051a39Sopenharmony_ci     */
4370e1051a39Sopenharmony_ci    if (testctx != NULL)
4371e1051a39Sopenharmony_ci        return TEST_skip("Non-default libctx");
4372e1051a39Sopenharmony_ci
4373e1051a39Sopenharmony_ci    custom_md_init_called = custom_md_cleanup_called = 0;
4374e1051a39Sopenharmony_ci
4375e1051a39Sopenharmony_ci    nid = OBJ_create("1.3.6.1.4.1.16604.998866.1", "custom-md", "custom-md");
4376e1051a39Sopenharmony_ci    if (!TEST_int_ne(nid, NID_undef))
4377e1051a39Sopenharmony_ci        goto err;
4378e1051a39Sopenharmony_ci    tmp = EVP_MD_meth_new(nid, NID_undef);
4379e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp))
4380e1051a39Sopenharmony_ci        goto err;
4381e1051a39Sopenharmony_ci
4382e1051a39Sopenharmony_ci    if (!TEST_true(EVP_MD_meth_set_init(tmp, custom_md_init))
4383e1051a39Sopenharmony_ci            || !TEST_true(EVP_MD_meth_set_cleanup(tmp, custom_md_cleanup))
4384e1051a39Sopenharmony_ci            || !TEST_true(EVP_MD_meth_set_app_datasize(tmp,
4385e1051a39Sopenharmony_ci                                                       sizeof(custom_dgst_ctx))))
4386e1051a39Sopenharmony_ci        goto err;
4387e1051a39Sopenharmony_ci
4388e1051a39Sopenharmony_ci    mdctx = EVP_MD_CTX_new();
4389e1051a39Sopenharmony_ci    if (!TEST_ptr(mdctx)
4390e1051a39Sopenharmony_ci               /*
4391e1051a39Sopenharmony_ci                * Initing our custom md and then initing another md should
4392e1051a39Sopenharmony_ci                * result in the init and cleanup functions of the custom md
4393e1051a39Sopenharmony_ci                * being called.
4394e1051a39Sopenharmony_ci                */
4395e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestInit_ex(mdctx, tmp, NULL))
4396e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL))
4397e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestUpdate(mdctx, mess, strlen(mess)))
4398e1051a39Sopenharmony_ci            || !TEST_true(EVP_DigestFinal_ex(mdctx, md_value, &md_len))
4399e1051a39Sopenharmony_ci            || !TEST_int_eq(custom_md_init_called, 1)
4400e1051a39Sopenharmony_ci            || !TEST_int_eq(custom_md_cleanup_called, 1))
4401e1051a39Sopenharmony_ci        goto err;
4402e1051a39Sopenharmony_ci
4403e1051a39Sopenharmony_ci    testresult = 1;
4404e1051a39Sopenharmony_ci err:
4405e1051a39Sopenharmony_ci    EVP_MD_CTX_free(mdctx);
4406e1051a39Sopenharmony_ci    EVP_MD_meth_free(tmp);
4407e1051a39Sopenharmony_ci    return testresult;
4408e1051a39Sopenharmony_ci}
4409e1051a39Sopenharmony_ci
4410e1051a39Sopenharmony_citypedef struct {
4411e1051a39Sopenharmony_ci        int data;
4412e1051a39Sopenharmony_ci} custom_ciph_ctx;
4413e1051a39Sopenharmony_ci
4414e1051a39Sopenharmony_cistatic int custom_ciph_init_called = 0;
4415e1051a39Sopenharmony_cistatic int custom_ciph_cleanup_called = 0;
4416e1051a39Sopenharmony_ci
4417e1051a39Sopenharmony_cistatic int custom_ciph_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
4418e1051a39Sopenharmony_ci                            const unsigned char *iv, int enc)
4419e1051a39Sopenharmony_ci{
4420e1051a39Sopenharmony_ci    custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
4421e1051a39Sopenharmony_ci
4422e1051a39Sopenharmony_ci    if (p == NULL)
4423e1051a39Sopenharmony_ci        return 0;
4424e1051a39Sopenharmony_ci
4425e1051a39Sopenharmony_ci    custom_ciph_init_called++;
4426e1051a39Sopenharmony_ci    return 1;
4427e1051a39Sopenharmony_ci}
4428e1051a39Sopenharmony_ci
4429e1051a39Sopenharmony_cistatic int custom_ciph_cleanup(EVP_CIPHER_CTX *ctx)
4430e1051a39Sopenharmony_ci{
4431e1051a39Sopenharmony_ci    custom_ciph_ctx *p = EVP_CIPHER_CTX_get_cipher_data(ctx);
4432e1051a39Sopenharmony_ci
4433e1051a39Sopenharmony_ci    if (p == NULL)
4434e1051a39Sopenharmony_ci        /* Nothing to do */
4435e1051a39Sopenharmony_ci        return 1;
4436e1051a39Sopenharmony_ci
4437e1051a39Sopenharmony_ci    custom_ciph_cleanup_called++;
4438e1051a39Sopenharmony_ci    return 1;
4439e1051a39Sopenharmony_ci}
4440e1051a39Sopenharmony_ci
4441e1051a39Sopenharmony_cistatic int test_custom_ciph_meth(void)
4442e1051a39Sopenharmony_ci{
4443e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ciphctx = NULL;
4444e1051a39Sopenharmony_ci    EVP_CIPHER *tmp = NULL;
4445e1051a39Sopenharmony_ci    int testresult = 0;
4446e1051a39Sopenharmony_ci    int nid;
4447e1051a39Sopenharmony_ci
4448e1051a39Sopenharmony_ci    /*
4449e1051a39Sopenharmony_ci     * We are testing deprecated functions. We don't support a non-default
4450e1051a39Sopenharmony_ci     * library context in this test.
4451e1051a39Sopenharmony_ci     */
4452e1051a39Sopenharmony_ci    if (testctx != NULL)
4453e1051a39Sopenharmony_ci        return TEST_skip("Non-default libctx");
4454e1051a39Sopenharmony_ci
4455e1051a39Sopenharmony_ci    custom_ciph_init_called = custom_ciph_cleanup_called = 0;
4456e1051a39Sopenharmony_ci
4457e1051a39Sopenharmony_ci    nid = OBJ_create("1.3.6.1.4.1.16604.998866.2", "custom-ciph", "custom-ciph");
4458e1051a39Sopenharmony_ci    if (!TEST_int_ne(nid, NID_undef))
4459e1051a39Sopenharmony_ci        goto err;
4460e1051a39Sopenharmony_ci    tmp = EVP_CIPHER_meth_new(nid, 16, 16);
4461e1051a39Sopenharmony_ci    if (!TEST_ptr(tmp))
4462e1051a39Sopenharmony_ci        goto err;
4463e1051a39Sopenharmony_ci
4464e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_meth_set_init(tmp, custom_ciph_init))
4465e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_meth_set_flags(tmp, EVP_CIPH_ALWAYS_CALL_INIT))
4466e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_meth_set_cleanup(tmp, custom_ciph_cleanup))
4467e1051a39Sopenharmony_ci            || !TEST_true(EVP_CIPHER_meth_set_impl_ctx_size(tmp,
4468e1051a39Sopenharmony_ci                                                            sizeof(custom_ciph_ctx))))
4469e1051a39Sopenharmony_ci        goto err;
4470e1051a39Sopenharmony_ci
4471e1051a39Sopenharmony_ci    ciphctx = EVP_CIPHER_CTX_new();
4472e1051a39Sopenharmony_ci    if (!TEST_ptr(ciphctx)
4473e1051a39Sopenharmony_ci            /*
4474e1051a39Sopenharmony_ci             * Initing our custom cipher and then initing another cipher
4475e1051a39Sopenharmony_ci             * should result in the init and cleanup functions of the custom
4476e1051a39Sopenharmony_ci             * cipher being called.
4477e1051a39Sopenharmony_ci             */
4478e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherInit_ex(ciphctx, tmp, NULL, NULL, NULL, 1))
4479e1051a39Sopenharmony_ci            || !TEST_true(EVP_CipherInit_ex(ciphctx, EVP_aes_128_cbc(), NULL,
4480e1051a39Sopenharmony_ci                                            NULL, NULL, 1))
4481e1051a39Sopenharmony_ci            || !TEST_int_eq(custom_ciph_init_called, 1)
4482e1051a39Sopenharmony_ci            || !TEST_int_eq(custom_ciph_cleanup_called, 1))
4483e1051a39Sopenharmony_ci        goto err;
4484e1051a39Sopenharmony_ci
4485e1051a39Sopenharmony_ci    testresult = 1;
4486e1051a39Sopenharmony_ci err:
4487e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ciphctx);
4488e1051a39Sopenharmony_ci    EVP_CIPHER_meth_free(tmp);
4489e1051a39Sopenharmony_ci    return testresult;
4490e1051a39Sopenharmony_ci}
4491e1051a39Sopenharmony_ci
4492e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DYNAMIC_ENGINE
4493e1051a39Sopenharmony_ci/* Test we can create a signature keys with an associated ENGINE */
4494e1051a39Sopenharmony_cistatic int test_signatures_with_engine(int tst)
4495e1051a39Sopenharmony_ci{
4496e1051a39Sopenharmony_ci    ENGINE *e;
4497e1051a39Sopenharmony_ci    const char *engine_id = "dasync";
4498e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
4499e1051a39Sopenharmony_ci    const unsigned char badcmackey[] = { 0x00, 0x01 };
4500e1051a39Sopenharmony_ci    const unsigned char cmackey[] = {
4501e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
4502e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f
4503e1051a39Sopenharmony_ci    };
4504e1051a39Sopenharmony_ci    const unsigned char ed25519key[] = {
4505e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
4506e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
4507e1051a39Sopenharmony_ci        0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
4508e1051a39Sopenharmony_ci    };
4509e1051a39Sopenharmony_ci    const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
4510e1051a39Sopenharmony_ci    int testresult = 0;
4511e1051a39Sopenharmony_ci    EVP_MD_CTX *ctx = NULL;
4512e1051a39Sopenharmony_ci    unsigned char *mac = NULL;
4513e1051a39Sopenharmony_ci    size_t maclen = 0;
4514e1051a39Sopenharmony_ci    int ret;
4515e1051a39Sopenharmony_ci
4516e1051a39Sopenharmony_ci#  ifdef OPENSSL_NO_CMAC
4517e1051a39Sopenharmony_ci    /* Skip CMAC tests in a no-cmac build */
4518e1051a39Sopenharmony_ci    if (tst <= 1)
4519e1051a39Sopenharmony_ci        return 1;
4520e1051a39Sopenharmony_ci#  endif
4521e1051a39Sopenharmony_ci
4522e1051a39Sopenharmony_ci    if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
4523e1051a39Sopenharmony_ci        return 0;
4524e1051a39Sopenharmony_ci
4525e1051a39Sopenharmony_ci    if (!TEST_true(ENGINE_init(e))) {
4526e1051a39Sopenharmony_ci        ENGINE_free(e);
4527e1051a39Sopenharmony_ci        return 0;
4528e1051a39Sopenharmony_ci    }
4529e1051a39Sopenharmony_ci
4530e1051a39Sopenharmony_ci    switch (tst) {
4531e1051a39Sopenharmony_ci    case 0:
4532e1051a39Sopenharmony_ci        pkey = EVP_PKEY_new_CMAC_key(e, cmackey, sizeof(cmackey),
4533e1051a39Sopenharmony_ci                                     EVP_aes_128_cbc());
4534e1051a39Sopenharmony_ci        break;
4535e1051a39Sopenharmony_ci    case 1:
4536e1051a39Sopenharmony_ci        pkey = EVP_PKEY_new_CMAC_key(e, badcmackey, sizeof(badcmackey),
4537e1051a39Sopenharmony_ci                                     EVP_aes_128_cbc());
4538e1051a39Sopenharmony_ci        break;
4539e1051a39Sopenharmony_ci    case 2:
4540e1051a39Sopenharmony_ci        pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, e, ed25519key,
4541e1051a39Sopenharmony_ci                                            sizeof(ed25519key));
4542e1051a39Sopenharmony_ci        break;
4543e1051a39Sopenharmony_ci    default:
4544e1051a39Sopenharmony_ci        TEST_error("Invalid test case");
4545e1051a39Sopenharmony_ci        goto err;
4546e1051a39Sopenharmony_ci    }
4547e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
4548e1051a39Sopenharmony_ci        goto err;
4549e1051a39Sopenharmony_ci
4550e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
4551e1051a39Sopenharmony_ci        goto err;
4552e1051a39Sopenharmony_ci
4553e1051a39Sopenharmony_ci    ret = EVP_DigestSignInit(ctx, NULL, tst == 2 ? NULL : EVP_sha256(), NULL,
4554e1051a39Sopenharmony_ci                             pkey);
4555e1051a39Sopenharmony_ci    if (tst == 0) {
4556e1051a39Sopenharmony_ci        if (!TEST_true(ret))
4557e1051a39Sopenharmony_ci            goto err;
4558e1051a39Sopenharmony_ci
4559e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignUpdate(ctx, msg, sizeof(msg)))
4560e1051a39Sopenharmony_ci                || !TEST_true(EVP_DigestSignFinal(ctx, NULL, &maclen)))
4561e1051a39Sopenharmony_ci            goto err;
4562e1051a39Sopenharmony_ci
4563e1051a39Sopenharmony_ci        if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
4564e1051a39Sopenharmony_ci            goto err;
4565e1051a39Sopenharmony_ci
4566e1051a39Sopenharmony_ci        if (!TEST_true(EVP_DigestSignFinal(ctx, mac, &maclen)))
4567e1051a39Sopenharmony_ci            goto err;
4568e1051a39Sopenharmony_ci    } else {
4569e1051a39Sopenharmony_ci        /* We used a bad key. We expect a failure here */
4570e1051a39Sopenharmony_ci        if (!TEST_false(ret))
4571e1051a39Sopenharmony_ci            goto err;
4572e1051a39Sopenharmony_ci    }
4573e1051a39Sopenharmony_ci
4574e1051a39Sopenharmony_ci    testresult = 1;
4575e1051a39Sopenharmony_ci err:
4576e1051a39Sopenharmony_ci    EVP_MD_CTX_free(ctx);
4577e1051a39Sopenharmony_ci    OPENSSL_free(mac);
4578e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
4579e1051a39Sopenharmony_ci    ENGINE_finish(e);
4580e1051a39Sopenharmony_ci    ENGINE_free(e);
4581e1051a39Sopenharmony_ci
4582e1051a39Sopenharmony_ci    return testresult;
4583e1051a39Sopenharmony_ci}
4584e1051a39Sopenharmony_ci
4585e1051a39Sopenharmony_cistatic int test_cipher_with_engine(void)
4586e1051a39Sopenharmony_ci{
4587e1051a39Sopenharmony_ci    ENGINE *e;
4588e1051a39Sopenharmony_ci    const char *engine_id = "dasync";
4589e1051a39Sopenharmony_ci    const unsigned char keyiv[] = {
4590e1051a39Sopenharmony_ci        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
4591e1051a39Sopenharmony_ci        0x0c, 0x0d, 0x0e, 0x0f
4592e1051a39Sopenharmony_ci    };
4593e1051a39Sopenharmony_ci    const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
4594e1051a39Sopenharmony_ci    int testresult = 0;
4595e1051a39Sopenharmony_ci    EVP_CIPHER_CTX *ctx = NULL, *ctx2 = NULL;
4596e1051a39Sopenharmony_ci    unsigned char buf[AES_BLOCK_SIZE];
4597e1051a39Sopenharmony_ci    int len = 0;
4598e1051a39Sopenharmony_ci
4599e1051a39Sopenharmony_ci    if (!TEST_ptr(e = ENGINE_by_id(engine_id)))
4600e1051a39Sopenharmony_ci        return 0;
4601e1051a39Sopenharmony_ci
4602e1051a39Sopenharmony_ci    if (!TEST_true(ENGINE_init(e))) {
4603e1051a39Sopenharmony_ci        ENGINE_free(e);
4604e1051a39Sopenharmony_ci        return 0;
4605e1051a39Sopenharmony_ci    }
4606e1051a39Sopenharmony_ci
4607e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
4608e1051a39Sopenharmony_ci            || !TEST_ptr(ctx2 = EVP_CIPHER_CTX_new()))
4609e1051a39Sopenharmony_ci        goto err;
4610e1051a39Sopenharmony_ci
4611e1051a39Sopenharmony_ci    if (!TEST_true(EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), e, keyiv, keyiv)))
4612e1051a39Sopenharmony_ci        goto err;
4613e1051a39Sopenharmony_ci
4614e1051a39Sopenharmony_ci    /* Copy the ctx, and complete the operation with the new ctx */
4615e1051a39Sopenharmony_ci    if (!TEST_true(EVP_CIPHER_CTX_copy(ctx2, ctx)))
4616e1051a39Sopenharmony_ci        goto err;
4617e1051a39Sopenharmony_ci
4618e1051a39Sopenharmony_ci    if (!TEST_true(EVP_EncryptUpdate(ctx2, buf, &len, msg, sizeof(msg)))
4619e1051a39Sopenharmony_ci            || !TEST_true(EVP_EncryptFinal_ex(ctx2, buf + len, &len)))
4620e1051a39Sopenharmony_ci        goto err;
4621e1051a39Sopenharmony_ci
4622e1051a39Sopenharmony_ci    testresult = 1;
4623e1051a39Sopenharmony_ci err:
4624e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx);
4625e1051a39Sopenharmony_ci    EVP_CIPHER_CTX_free(ctx2);
4626e1051a39Sopenharmony_ci    ENGINE_finish(e);
4627e1051a39Sopenharmony_ci    ENGINE_free(e);
4628e1051a39Sopenharmony_ci
4629e1051a39Sopenharmony_ci    return testresult;
4630e1051a39Sopenharmony_ci}
4631e1051a39Sopenharmony_ci# endif /* OPENSSL_NO_DYNAMIC_ENGINE */
4632e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_DEPRECATED_3_0 */
4633e1051a39Sopenharmony_ci
4634e1051a39Sopenharmony_cistatic int ecxnids[] = {
4635e1051a39Sopenharmony_ci    NID_X25519,
4636e1051a39Sopenharmony_ci    NID_X448,
4637e1051a39Sopenharmony_ci    NID_ED25519,
4638e1051a39Sopenharmony_ci    NID_ED448
4639e1051a39Sopenharmony_ci};
4640e1051a39Sopenharmony_ci
4641e1051a39Sopenharmony_ci/* Test that creating ECX keys with a short private key fails as expected */
4642e1051a39Sopenharmony_cistatic int test_ecx_short_keys(int tst)
4643e1051a39Sopenharmony_ci{
4644e1051a39Sopenharmony_ci    unsigned char ecxkeydata = 1;
4645e1051a39Sopenharmony_ci    EVP_PKEY *pkey;
4646e1051a39Sopenharmony_ci
4647e1051a39Sopenharmony_ci
4648e1051a39Sopenharmony_ci    pkey = EVP_PKEY_new_raw_private_key_ex(testctx, OBJ_nid2sn(ecxnids[tst]),
4649e1051a39Sopenharmony_ci                                           NULL, &ecxkeydata, 1);
4650e1051a39Sopenharmony_ci    if (!TEST_ptr_null(pkey)) {
4651e1051a39Sopenharmony_ci        EVP_PKEY_free(pkey);
4652e1051a39Sopenharmony_ci        return 0;
4653e1051a39Sopenharmony_ci    }
4654e1051a39Sopenharmony_ci
4655e1051a39Sopenharmony_ci    return 1;
4656e1051a39Sopenharmony_ci}
4657e1051a39Sopenharmony_ci
4658e1051a39Sopenharmony_citypedef enum OPTION_choice {
4659e1051a39Sopenharmony_ci    OPT_ERR = -1,
4660e1051a39Sopenharmony_ci    OPT_EOF = 0,
4661e1051a39Sopenharmony_ci    OPT_CONTEXT,
4662e1051a39Sopenharmony_ci    OPT_TEST_ENUM
4663e1051a39Sopenharmony_ci} OPTION_CHOICE;
4664e1051a39Sopenharmony_ci
4665e1051a39Sopenharmony_ciconst OPTIONS *test_get_options(void)
4666e1051a39Sopenharmony_ci{
4667e1051a39Sopenharmony_ci    static const OPTIONS options[] = {
4668e1051a39Sopenharmony_ci        OPT_TEST_OPTIONS_DEFAULT_USAGE,
4669e1051a39Sopenharmony_ci        { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
4670e1051a39Sopenharmony_ci        { NULL }
4671e1051a39Sopenharmony_ci    };
4672e1051a39Sopenharmony_ci    return options;
4673e1051a39Sopenharmony_ci}
4674e1051a39Sopenharmony_ci
4675e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4676e1051a39Sopenharmony_ci/* Test that trying to sign with a public key errors out gracefully */
4677e1051a39Sopenharmony_cistatic int test_ecx_not_private_key(int tst)
4678e1051a39Sopenharmony_ci{
4679e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
4680e1051a39Sopenharmony_ci
4681e1051a39Sopenharmony_ci    const unsigned char msg[] = { 0x00, 0x01, 0x02, 0x03 };
4682e1051a39Sopenharmony_ci    int testresult = 0;
4683e1051a39Sopenharmony_ci    EVP_MD_CTX *ctx = NULL;
4684e1051a39Sopenharmony_ci    unsigned char *mac = NULL;
4685e1051a39Sopenharmony_ci    size_t maclen = 0;
4686e1051a39Sopenharmony_ci    unsigned char *pubkey;
4687e1051a39Sopenharmony_ci    size_t pubkeylen;
4688e1051a39Sopenharmony_ci
4689e1051a39Sopenharmony_ci    switch (keys[tst].type) {
4690e1051a39Sopenharmony_ci    case NID_X25519:
4691e1051a39Sopenharmony_ci    case NID_X448:
4692e1051a39Sopenharmony_ci        return TEST_skip("signing not supported for X25519/X448");
4693e1051a39Sopenharmony_ci    }
4694e1051a39Sopenharmony_ci
4695e1051a39Sopenharmony_ci    /* Check if this algorithm supports public keys */
4696e1051a39Sopenharmony_ci    if (keys[tst].pub == NULL)
4697e1051a39Sopenharmony_ci        return TEST_skip("no public key present");
4698e1051a39Sopenharmony_ci
4699e1051a39Sopenharmony_ci    pubkey = (unsigned char *)keys[tst].pub;
4700e1051a39Sopenharmony_ci    pubkeylen = strlen(keys[tst].pub);
4701e1051a39Sopenharmony_ci
4702e1051a39Sopenharmony_ci    pkey = EVP_PKEY_new_raw_public_key_ex(testctx, OBJ_nid2sn(keys[tst].type),
4703e1051a39Sopenharmony_ci                                          NULL, pubkey, pubkeylen);
4704e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey))
4705e1051a39Sopenharmony_ci        goto err;
4706e1051a39Sopenharmony_ci
4707e1051a39Sopenharmony_ci    if (!TEST_ptr(ctx = EVP_MD_CTX_new()))
4708e1051a39Sopenharmony_ci        goto err;
4709e1051a39Sopenharmony_ci
4710e1051a39Sopenharmony_ci    if (EVP_DigestSignInit(ctx, NULL, NULL, NULL, pkey) != 1)
4711e1051a39Sopenharmony_ci        goto check_err;
4712e1051a39Sopenharmony_ci
4713e1051a39Sopenharmony_ci    if (EVP_DigestSign(ctx, NULL, &maclen, msg, sizeof(msg)) != 1)
4714e1051a39Sopenharmony_ci        goto check_err;
4715e1051a39Sopenharmony_ci
4716e1051a39Sopenharmony_ci    if (!TEST_ptr(mac = OPENSSL_malloc(maclen)))
4717e1051a39Sopenharmony_ci        goto err;
4718e1051a39Sopenharmony_ci
4719e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_DigestSign(ctx, mac, &maclen, msg, sizeof(msg)), 0))
4720e1051a39Sopenharmony_ci        goto err;
4721e1051a39Sopenharmony_ci
4722e1051a39Sopenharmony_ci check_err:
4723e1051a39Sopenharmony_ci    /*
4724e1051a39Sopenharmony_ci     * Currently only EVP_DigestSign will throw PROV_R_NOT_A_PRIVATE_KEY,
4725e1051a39Sopenharmony_ci     * but we relax the check to allow error also thrown by
4726e1051a39Sopenharmony_ci     * EVP_DigestSignInit and EVP_DigestSign.
4727e1051a39Sopenharmony_ci     */
4728e1051a39Sopenharmony_ci    if (ERR_GET_REASON(ERR_peek_error()) == PROV_R_NOT_A_PRIVATE_KEY) {
4729e1051a39Sopenharmony_ci        testresult = 1;
4730e1051a39Sopenharmony_ci        ERR_clear_error();
4731e1051a39Sopenharmony_ci    }
4732e1051a39Sopenharmony_ci
4733e1051a39Sopenharmony_ci err:
4734e1051a39Sopenharmony_ci    EVP_MD_CTX_free(ctx);
4735e1051a39Sopenharmony_ci    OPENSSL_free(mac);
4736e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
4737e1051a39Sopenharmony_ci
4738e1051a39Sopenharmony_ci    return testresult;
4739e1051a39Sopenharmony_ci}
4740e1051a39Sopenharmony_ci#endif /* OPENSSL_NO_EC */
4741e1051a39Sopenharmony_ci
4742e1051a39Sopenharmony_ciint setup_tests(void)
4743e1051a39Sopenharmony_ci{
4744e1051a39Sopenharmony_ci    OPTION_CHOICE o;
4745e1051a39Sopenharmony_ci
4746e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
4747e1051a39Sopenharmony_ci        switch (o) {
4748e1051a39Sopenharmony_ci        case OPT_CONTEXT:
4749e1051a39Sopenharmony_ci            /* Set up an alternate library context */
4750e1051a39Sopenharmony_ci            testctx = OSSL_LIB_CTX_new();
4751e1051a39Sopenharmony_ci            if (!TEST_ptr(testctx))
4752e1051a39Sopenharmony_ci                return 0;
4753e1051a39Sopenharmony_ci            /* Swap the libctx to test non-default context only */
4754e1051a39Sopenharmony_ci            nullprov = OSSL_PROVIDER_load(NULL, "null");
4755e1051a39Sopenharmony_ci            deflprov = OSSL_PROVIDER_load(testctx, "default");
4756e1051a39Sopenharmony_ci            lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
4757e1051a39Sopenharmony_ci            break;
4758e1051a39Sopenharmony_ci        case OPT_TEST_CASES:
4759e1051a39Sopenharmony_ci            break;
4760e1051a39Sopenharmony_ci        default:
4761e1051a39Sopenharmony_ci            return 0;
4762e1051a39Sopenharmony_ci        }
4763e1051a39Sopenharmony_ci    }
4764e1051a39Sopenharmony_ci
4765e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_set_default_properties);
4766e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_EVP_DigestSignInit, 30);
4767e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_DigestVerifyInit);
4768e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_SIPHASH
4769e1051a39Sopenharmony_ci    ADD_TEST(test_siphash_digestsign);
4770e1051a39Sopenharmony_ci#endif
4771e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_Digest);
4772e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_md_null);
4773e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_EVP_PKEY_sign, 3);
4774e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_EVP_Enveloped, 2);
4775e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_d2i_AutoPrivateKey, OSSL_NELEM(keydata));
4776e1051a39Sopenharmony_ci    ADD_TEST(test_privatekey_to_pkcs8);
4777e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_PKCS82PKEY_wrong_tag);
4778e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4779e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_PKCS82PKEY);
4780e1051a39Sopenharmony_ci#endif
4781e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4782e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_EC_keygen_with_enc, OSSL_NELEM(ec_encodings));
4783e1051a39Sopenharmony_ci#endif
4784e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_SM2)
4785e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_SM2);
4786e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_SM2_verify);
4787e1051a39Sopenharmony_ci#endif
4788e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_set_get_raw_keys, OSSL_NELEM(keys));
4789e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
4790e1051a39Sopenharmony_ci    custom_pmeth = EVP_PKEY_meth_new(0xdefaced, 0);
4791e1051a39Sopenharmony_ci    if (!TEST_ptr(custom_pmeth))
4792e1051a39Sopenharmony_ci        return 0;
4793e1051a39Sopenharmony_ci    EVP_PKEY_meth_set_check(custom_pmeth, pkey_custom_check);
4794e1051a39Sopenharmony_ci    EVP_PKEY_meth_set_public_check(custom_pmeth, pkey_custom_pub_check);
4795e1051a39Sopenharmony_ci    EVP_PKEY_meth_set_param_check(custom_pmeth, pkey_custom_param_check);
4796e1051a39Sopenharmony_ci    if (!TEST_int_eq(EVP_PKEY_meth_add0(custom_pmeth), 1))
4797e1051a39Sopenharmony_ci        return 0;
4798e1051a39Sopenharmony_ci#endif
4799e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_EVP_PKEY_check, OSSL_NELEM(keycheckdata));
4800e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_CMAC
4801e1051a39Sopenharmony_ci    ADD_TEST(test_CMAC_keygen);
4802e1051a39Sopenharmony_ci#endif
4803e1051a39Sopenharmony_ci    ADD_TEST(test_HKDF);
4804e1051a39Sopenharmony_ci    ADD_TEST(test_emptyikm_HKDF);
4805e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4806e1051a39Sopenharmony_ci    ADD_TEST(test_X509_PUBKEY_inplace);
4807e1051a39Sopenharmony_ci    ADD_TEST(test_X509_PUBKEY_dup);
4808e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_invalide_ec_char2_pub_range_decode,
4809e1051a39Sopenharmony_ci                  OSSL_NELEM(ec_der_pub_keys));
4810e1051a39Sopenharmony_ci#endif
4811e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DSA
4812e1051a39Sopenharmony_ci    ADD_TEST(test_DSA_get_set_params);
4813e1051a39Sopenharmony_ci    ADD_TEST(test_DSA_priv_pub);
4814e1051a39Sopenharmony_ci#endif
4815e1051a39Sopenharmony_ci    ADD_TEST(test_RSA_get_set_params);
4816e1051a39Sopenharmony_ci    ADD_TEST(test_RSA_OAEP_set_get_params);
4817e1051a39Sopenharmony_ci#if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
4818e1051a39Sopenharmony_ci    ADD_TEST(test_decrypt_null_chunks);
4819e1051a39Sopenharmony_ci#endif
4820e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DH
4821e1051a39Sopenharmony_ci    ADD_TEST(test_DH_priv_pub);
4822e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
4823e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_PKEY_set1_DH);
4824e1051a39Sopenharmony_ci# endif
4825e1051a39Sopenharmony_ci#endif
4826e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4827e1051a39Sopenharmony_ci    ADD_TEST(test_EC_priv_pub);
4828e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DEPRECATED_3_0
4829e1051a39Sopenharmony_ci    ADD_TEST(test_EC_priv_only_legacy);
4830e1051a39Sopenharmony_ci# endif
4831e1051a39Sopenharmony_ci#endif
4832e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_keygen_with_empty_template, 2);
4833e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_pkey_ctx_fail_without_provider, 2);
4834e1051a39Sopenharmony_ci
4835e1051a39Sopenharmony_ci    ADD_TEST(test_rand_agglomeration);
4836e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_iv_aes, 12);
4837e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DES
4838e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_iv_des, 6);
4839e1051a39Sopenharmony_ci#endif
4840e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_BF
4841e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_bf_default_keylen, 4);
4842e1051a39Sopenharmony_ci#endif
4843e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_rsa_pss_with_keygen_bits);
4844e1051a39Sopenharmony_ci    ADD_TEST(test_EVP_rsa_pss_set_saltlen);
4845e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4846e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ecpub, OSSL_NELEM(ecpub_nids));
4847e1051a39Sopenharmony_ci#endif
4848e1051a39Sopenharmony_ci
4849e1051a39Sopenharmony_ci    ADD_TEST(test_names_do_all);
4850e1051a39Sopenharmony_ci
4851e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_init_seq, OSSL_NELEM(evp_init_tests));
4852e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_reset, OSSL_NELEM(evp_reset_tests));
4853e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_gcm_reinit, OSSL_NELEM(gcm_reinit_tests));
4854e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_evp_updated_iv, OSSL_NELEM(evp_updated_iv_tests));
4855e1051a39Sopenharmony_ci
4856e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_DEPRECATED_3_0
4857e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_custom_pmeth, 12);
4858e1051a39Sopenharmony_ci    ADD_TEST(test_evp_md_cipher_meth);
4859e1051a39Sopenharmony_ci    ADD_TEST(test_custom_md_meth);
4860e1051a39Sopenharmony_ci    ADD_TEST(test_custom_ciph_meth);
4861e1051a39Sopenharmony_ci
4862e1051a39Sopenharmony_ci# ifndef OPENSSL_NO_DYNAMIC_ENGINE
4863e1051a39Sopenharmony_ci    /* Tests only support the default libctx */
4864e1051a39Sopenharmony_ci    if (testctx == NULL) {
4865e1051a39Sopenharmony_ci#  ifndef OPENSSL_NO_EC
4866e1051a39Sopenharmony_ci        ADD_ALL_TESTS(test_signatures_with_engine, 3);
4867e1051a39Sopenharmony_ci#  else
4868e1051a39Sopenharmony_ci        ADD_ALL_TESTS(test_signatures_with_engine, 2);
4869e1051a39Sopenharmony_ci#  endif
4870e1051a39Sopenharmony_ci        ADD_TEST(test_cipher_with_engine);
4871e1051a39Sopenharmony_ci    }
4872e1051a39Sopenharmony_ci# endif
4873e1051a39Sopenharmony_ci#endif
4874e1051a39Sopenharmony_ci
4875e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ecx_short_keys, OSSL_NELEM(ecxnids));
4876e1051a39Sopenharmony_ci
4877e1051a39Sopenharmony_ci#ifndef OPENSSL_NO_EC
4878e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_ecx_not_private_key, OSSL_NELEM(keys));
4879e1051a39Sopenharmony_ci#endif
4880e1051a39Sopenharmony_ci
4881e1051a39Sopenharmony_ci    return 1;
4882e1051a39Sopenharmony_ci}
4883e1051a39Sopenharmony_ci
4884e1051a39Sopenharmony_civoid cleanup_tests(void)
4885e1051a39Sopenharmony_ci{
4886e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(nullprov);
4887e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(deflprov);
4888e1051a39Sopenharmony_ci    OSSL_PROVIDER_unload(lgcyprov);
4889e1051a39Sopenharmony_ci    OSSL_LIB_CTX_free(testctx);
4890e1051a39Sopenharmony_ci}
4891