1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2021-2025 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ====================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/async.h>
#include <openssl/objects.h>
#include <openssl/evp.h>
#include <openssl/engine.h>
#include "tests.h"
#include "../qat_utils.h"
#define NUM_OF_ECX_OPERATIONS 2
#define MAX_ECX_SIZE 128
/******************************************************************************
* function:
* test_ecx_curve (int nid,
* const char *text,
* int count,
* int print_output,
* int verify,
* BIO *out)
*
* @param nid [IN] - curve object identifier
* @param text [IN] - curve names
* @param count [IN] - number of iterations
* @param print_output [IN] - print hex output flag
* @param verify [IN] - verify flag
* @param out [IN] - for printf debug messages
*
* description:
* perform ECX generate key & derive key.
*
******************************************************************************/
static int test_ecx_curve(int nid,
const char *text,
int count,
int print_output,
int verify,
BIO *out)
{
EVP_PKEY_CTX *kctx = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY_CTX *comparison_ctx = NULL;
EVP_PKEY *key_A = NULL;
EVP_PKEY *key_B = NULL;
size_t outlen = 0;
size_t comparison_outlen = 0;
unsigned char *outbuf = NULL, *comparison_outbuf = NULL;
char buf[12];
int i = 0, ret = 0;
kctx = EVP_PKEY_CTX_new_id(nid, NULL); /* keygen ctx from NID */
if (kctx == NULL) {
WARN("# FAIL ECX - Initial mallocs failed\n");
ret = -1;
goto err;
}
if (print_output) {
BIO_puts(out, "\nTesting ECX key generation with ");
BIO_puts(out, text);
BIO_puts(out, "\n");
}
#ifdef QAT_OPENSSL_PROVIDER
if ((key_A = EVP_PKEY_new()) <= 0 ||
(key_B = EVP_PKEY_new()) <= 0) {
fprintf(stderr, "EVP_PKEY_new failed. \n");
ret = -1;
goto err;
}
#endif
if (EVP_PKEY_keygen_init(kctx) <= 0) {
WARN("# FAIL ECX - keygen_init failed\n");
ret = -1;
goto err;
}
if (EVP_PKEY_keygen(kctx, &key_A) <= 0) {
WARN("# FAIL ECX - keygen key_A failed\n");
ret = -1;
goto err;
}
if (EVP_PKEY_keygen(kctx, &key_B) <= 0) {
WARN("# FAIL ECX - keygen key_B failed\n");
ret = -1;
goto err;
}
for (i = 0; i < count; i++) {
/* Positive scenario */
if (print_output)
BIO_puts(out,"Positive test \n");
if ((ctx = EVP_PKEY_CTX_new(key_A, NULL)) == NULL ||
EVP_PKEY_derive_init(ctx) <= 0 ||
EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 ||
EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
outlen == 0 ||
outlen > MAX_ECX_SIZE) {
WARN("# FAIL ECX - ECX derive failed.\n");
ret = -1;
goto err;
}
outbuf = (unsigned char *)OPENSSL_zalloc(MAX_ECX_SIZE);
if (outbuf == NULL) {
WARN("#FAIL ECX - memory allocation failed.\n");
ret = -1;
goto err;
}
/*
* Here we multiply the private key of key_A with the public key of
* key_B, the peerkey.
*/
if ((EVP_PKEY_derive(ctx, outbuf, &outlen)) <= 0) {
WARN("# FAIL ECX - ECX derive failed.\n");
ret = -1;
goto err;
}
if (print_output) {
BIO_puts(out," derived key = ");
for (i = 0; i < outlen; i++) {
sprintf(buf, "%02X ", outbuf[i]);
BIO_puts(out, buf);
}
BIO_puts(out, "\n");
}
if (verify) {
if ((comparison_ctx = EVP_PKEY_CTX_new(key_B, NULL)) == NULL ||
EVP_PKEY_derive_init(comparison_ctx) <= 0 ||
EVP_PKEY_derive_set_peer(comparison_ctx, key_A) <= 0 ||
EVP_PKEY_derive(comparison_ctx, NULL, &comparison_outlen) <= 0 || /* determine max length */
comparison_outlen == 0 ||
comparison_outlen > MAX_ECX_SIZE) {
WARN("# FAIL ECX - ECX derive failed.\n");
ret = -1;
goto err;
}
comparison_outbuf = (unsigned char *)OPENSSL_zalloc(MAX_ECX_SIZE);
if (comparison_outbuf == NULL) {
WARN("#FAIL ECX - memory allocation failed.\n");
ret = -1;
goto err;
}
/*
* Here we multiply the private key of key_B with the public key of
* key_A, the peerkey.
*/
if ((EVP_PKEY_derive(comparison_ctx, comparison_outbuf,
&comparison_outlen)) <= 0) {
WARN("# FAIL ECX - ECX derive failed.\n");
ret = -1;
goto err;
}
if (print_output) {
BIO_puts(out," comparison derived key = ");
for (i = 0; i < comparison_outlen; i++) {
sprintf(buf, "%02X ", comparison_outbuf[i]);
BIO_puts(out, buf);
}
BIO_puts(out, "\n");
}
if ((comparison_outlen != outlen) ||
(memcmp(outbuf, comparison_outbuf, outlen) != 0)) {
WARN("#FAIL ECX - buffer comparison failed.\n");
ret = -1;
}
}
if (ctx != NULL) {
EVP_PKEY_CTX_free(ctx);
ctx = NULL;
}
if (comparison_ctx != NULL) {
EVP_PKEY_CTX_free(comparison_ctx);
comparison_ctx = NULL;
}
if (outbuf != NULL) {
OPENSSL_free(outbuf);
outbuf = NULL;
}
if (comparison_outbuf != NULL) {
OPENSSL_free(comparison_outbuf);
comparison_outbuf = NULL;
}
}
if (ret == 0)
INFO("# PASS verify for ECDH%s.\n", text);
else
INFO("# FAIL verify for ECDH%s.\n", text);
err:
if (ret != 0)
ERR_print_errors_fp(stderr);
EVP_PKEY_free(key_A);
EVP_PKEY_free(key_B);
if (kctx != NULL) {
EVP_PKEY_CTX_free(kctx);
kctx = NULL;
}
if (ctx != NULL) {
EVP_PKEY_CTX_free(ctx);
ctx = NULL;
}
if (comparison_ctx != NULL) {
EVP_PKEY_CTX_free(comparison_ctx);
comparison_ctx = NULL;
}
if (outbuf != NULL) {
OPENSSL_free(outbuf);
outbuf = NULL;
}
if (comparison_outbuf != NULL) {
OPENSSL_free(comparison_outbuf);
comparison_outbuf = NULL;
}
return ret;
}
/******************************************************************************
* function:
* run_ecx (void *args)
*
* @param args [IN] - the test parameters
*
* description:
* specify a test case
*
******************************************************************************/
static int run_ecx(void *args)
{
TEST_PARAMS *local_args = (TEST_PARAMS *)args;
int count = *(local_args->count);
int print_output = local_args->print_output;
int verify = local_args->verify;
BIO *out = NULL;
int ret = 1;
out = BIO_new(BIO_s_file());
if (out == NULL) {
WARN("# FAIL: Unable to create BIO\n");
exit(1);
}
BIO_set_fp(out, stdout, BIO_NOCLOSE);
if (local_args->curve == X_CURVE_448)
ret = test_ecx_curve(EVP_PKEY_X448, "X448", count, print_output, verify, out);
else
ret = test_ecx_curve(EVP_PKEY_X25519, "X25519", count, print_output, verify, out);
if (ret < 0)
ERR_print_errors_fp(stderr);
else
ret = 1;
BIO_free(out);
return ret;
}
/******************************************************************************
* function:
* tests_run_ecx (TEST_PARAMS *args)
*
* @param args [IN] - the test parameters
*
* description:
* specify a test case
*
******************************************************************************/
void tests_run_ecx(TEST_PARAMS *args)
{
if (!args->enable_async)
run_ecx(args);
else
start_async_job(args, run_ecx);
}
|