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