From nobody Sat Nov 30 16:51:09 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y0wzf3ZfWz5fHxc; Sat, 30 Nov 2024 16:51:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y0wzd5LjSz4nSg; Sat, 30 Nov 2024 16:51:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732985469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Nzl93lLQrdXKSZjStxy9uBP4hxGjDEkqwbc/GSFccmc=; b=B4+bnoEN6JeTiRWBbfaYP3U1U9OxIC6hZj7xXCnT1Tqe83ruthKDED1i4G3El/PpF67OP5 YbwzricKS4fPUIzJaPwZ3dYPZ8le1jwaEUPasg7vVCd01J03ZrW9pvnX/OodaX6dy66xbn P8UePlZCD5aNvIl1gzxmiguKWcZjfY/8N8bvBgwQcqkr2vYQzzQDM25Xc9X3FWQtP3psiZ ZKYn6lAUMKWJ0GHn1fQ3JAjiUUuFA+tiMhI+Og5Q9x6QDRw6pjIrlvlvR1BehLh5IyV8UX +d155XIjnSC6Y5cYDIonx8pv2b67hUZPFr8NSmtES5Ge9koP8Y3A3QS+AXeA8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732985469; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Nzl93lLQrdXKSZjStxy9uBP4hxGjDEkqwbc/GSFccmc=; b=yRK3hdy1fkznlzZq8ob7xpQTuUdNCq5SIeAYjElUX+8vjsJ3NMPZuvI6dPBzlvuixjq57I 6P2dB5Z1btaeK0RF2Zp0FiztgoZROt/idMnzcWfWQWOj9zXEVhk1k7QMznfuvKggnuc4pX VH/62bESfOZ4iC0KsI+YyughJVqhI5U+GGzvYyY6HWAinpuXnFWZghJpvu1A2O7wQn8Aa3 hiXeEL7vqSsRXp5v0i/zgN8hjDkR4FQAdX9GoRow3ZKvFfv8yu3GnjJn9CqS5aXN3s2MYa yMycCslINN8cYTK+MciOr8nbfmMYelUZejGaiziS8lPNkyLQ3seriLWKwRxYzg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732985469; a=rsa-sha256; cv=none; b=xoCK0nK0jY7hvmUygGWPZ8k1aaormEartuI/U/yVMDDdwZ0lZllsPFuZcWacuD+hLALBzO /fFInQ+85BT7uA66b/f790HuXBbBD3qVHDwF5KdFCNbeMWj5TlxjsMGI4lqqBNaANf6qJC 2ImC/zOiLIgEid4t3nJI/o6t7BUmdzzuxbbvUOIzAvGlw8XGXqV0xt3/2/bJtuZh/Y3oRY n3HhlCD0FOhZTH8QrH3K5K++erBHwGzP65sH/NxcTyhD6EIKZ/9DhOmbLVFvwOIFMZK+u7 vqbeZLi9Q52OuR2FxeqZZouCICcOqN154FS5vu9f7OrRleiuST1ecicRtYCLQQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y0wzd4yHwzFxY; Sat, 30 Nov 2024 16:51:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AUGp9xS043397; Sat, 30 Nov 2024 16:51:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AUGp9Nd043394; Sat, 30 Nov 2024 16:51:09 GMT (envelope-from git) Date: Sat, 30 Nov 2024 16:51:09 GMT Message-Id: <202411301651.4AUGp9Nd043394@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: 8b664b8f0ec8 - stable/14 - cryptocheck: Don't treat OpenSSL errors as fatal List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 8b664b8f0ec89beff848115444ef09fde677862f Auto-Submitted: auto-generated The branch stable/14 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=8b664b8f0ec89beff848115444ef09fde677862f commit 8b664b8f0ec89beff848115444ef09fde677862f Author: John Baldwin AuthorDate: 2024-06-06 21:47:04 +0000 Commit: John Baldwin CommitDate: 2024-11-30 13:55:57 +0000 cryptocheck: Don't treat OpenSSL errors as fatal Abort the current test but keep running additional tests if OpenSSL reports an error during a test. This matches the behavior for other tests such as an error from OCF. Reviewed by: markj Sponsored by: AFRL, DARPA Differential Revision: https://reviews.freebsd.org/D45279 (cherry picked from commit b0e7358bf8b9791aaaf38807c841953946b88785) --- tools/tools/crypto/cryptocheck.c | 322 ++++++++++++++++++++++++++------------- 1 file changed, 220 insertions(+), 102 deletions(-) diff --git a/tools/tools/crypto/cryptocheck.c b/tools/tools/crypto/cryptocheck.c index ef3e225e94f6..6506671455ac 100644 --- a/tools/tools/crypto/cryptocheck.c +++ b/tools/tools/crypto/cryptocheck.c @@ -537,7 +537,7 @@ ocf_hash(const struct alg *alg, const char *buffer, size_t size, char *digest, return (true); } -static void +static bool openssl_hash(const struct alg *alg, const EVP_MD *md, const void *buffer, size_t size, void *digest_out, unsigned *digest_sz_out) { @@ -564,11 +564,12 @@ openssl_hash(const struct alg *alg, const EVP_MD *md, const void *buffer, goto err_out; EVP_MD_CTX_destroy(mdctx); - return; + return (true); err_out: - errx(1, "OpenSSL %s HASH failed%s: %s", alg->name, errs, + warnx("OpenSSL %s HASH failed%s: %s", alg->name, errs, ERR_error_string(ERR_get_error(), NULL)); + return (false); } static void @@ -590,7 +591,8 @@ run_hash_test(const struct alg *alg, size_t size) /* OpenSSL HASH. */ digest_len = sizeof(control_digest); - openssl_hash(alg, md, buffer, size, control_digest, &digest_len); + if (!openssl_hash(alg, md, buffer, size, control_digest, &digest_len)) + goto out; /* cryptodev HASH. */ if (!ocf_hash(alg, buffer, size, test_digest, &crid)) @@ -671,9 +673,11 @@ run_hmac_test(const struct alg *alg, size_t size) /* OpenSSL HMAC. */ digest_len = sizeof(control_digest); if (HMAC(md, key, key_len, (u_char *)buffer, size, - (u_char *)control_digest, &digest_len) == NULL) - errx(1, "OpenSSL %s (%zu) HMAC failed: %s", alg->name, + (u_char *)control_digest, &digest_len) == NULL) { + warnx("OpenSSL %s (%zu) HMAC failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto out; + } /* cryptodev HMAC. */ if (!ocf_hmac(alg, buffer, size, key, key_len, test_digest, &crid)) @@ -700,7 +704,7 @@ out: free(key); } -static void +static bool openssl_cipher(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, const char *iv, const char *input, char *output, size_t size, int enc) { @@ -708,27 +712,42 @@ openssl_cipher(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, int outl, total; ctx = EVP_CIPHER_CTX_new(); - if (ctx == NULL) - errx(1, "OpenSSL %s (%zu) ctx new failed: %s", alg->name, + if (ctx == NULL) { + warnx("OpenSSL %s (%zu) ctx new failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + return (false); + } if (EVP_CipherInit_ex(ctx, cipher, NULL, (const u_char *)key, - (const u_char *)iv, enc) != 1) - errx(1, "OpenSSL %s (%zu) ctx init failed: %s", alg->name, + (const u_char *)iv, enc) != 1) { + warnx("OpenSSL %s (%zu) ctx init failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } EVP_CIPHER_CTX_set_padding(ctx, 0); if (EVP_CipherUpdate(ctx, (u_char *)output, &outl, - (const u_char *)input, size) != 1) - errx(1, "OpenSSL %s (%zu) cipher update failed: %s", alg->name, + (const u_char *)input, size) != 1) { + warnx("OpenSSL %s (%zu) cipher update failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total = outl; - if (EVP_CipherFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) - errx(1, "OpenSSL %s (%zu) cipher final failed: %s", alg->name, + if (EVP_CipherFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) { + warnx("OpenSSL %s (%zu) cipher final failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total += outl; - if ((size_t)total != size) - errx(1, "OpenSSL %s (%zu) cipher size mismatch: %d", alg->name, + if ((size_t)total != size) { + warnx("OpenSSL %s (%zu) cipher size mismatch: %d", alg->name, size, total); + goto error; + } + EVP_CIPHER_CTX_free(ctx); + return (true); + +error: EVP_CIPHER_CTX_free(ctx); + return (false); } static bool @@ -808,22 +827,27 @@ run_cipher_test(const struct alg *alg, size_t size) ciphertext = malloc(size); /* OpenSSL cipher. */ - openssl_cipher(alg, cipher, key, iv, cleartext, ciphertext, size, 1); - if (size > 0 && memcmp(cleartext, ciphertext, size) == 0) + if (!openssl_cipher(alg, cipher, key, iv, cleartext, ciphertext, size, + 1)) + goto out_noocf; + if (size > 0 && memcmp(cleartext, ciphertext, size) == 0) { warnx("OpenSSL %s (%zu): cipher text unchanged", alg->name, size); - openssl_cipher(alg, cipher, key, iv, ciphertext, buffer, size, 0); + goto out_noocf; + } + if (!openssl_cipher(alg, cipher, key, iv, ciphertext, buffer, size, 0)) + goto out_noocf; if (memcmp(cleartext, buffer, size) != 0) { printf("OpenSSL %s (%zu): cipher mismatch:", alg->name, size); printf("original:\n"); hexdump(cleartext, size, NULL, 0); printf("decrypted:\n"); hexdump(buffer, size, NULL, 0); - exit(1); + goto out_noocf; } if (!ocf_init_cipher_session(alg, key, key_len, &ses)) - goto out; + goto out_noocf; /* OCF encrypt. */ if (!ocf_cipher(&ses, alg, iv, cleartext, buffer, size, COP_ENCRYPT)) @@ -855,6 +879,7 @@ run_cipher_test(const struct alg *alg, size_t size) out: ocf_destroy_session(&ses); +out_noocf: free(ciphertext); free(buffer); free(cleartext); @@ -970,21 +995,26 @@ run_eta_test(const struct alg *alg, size_t aad_len, size_t size) /* OpenSSL encrypt + HMAC. */ if (aad_len != 0) memcpy(ciphertext, cleartext, aad_len); - openssl_cipher(alg, cipher, cipher_key, iv, cleartext + aad_len, - ciphertext + aad_len, size, 1); + if (!openssl_cipher(alg, cipher, cipher_key, iv, cleartext + aad_len, + ciphertext + aad_len, size, 1)) + goto out_noocf; if (size > 0 && memcmp(cleartext + aad_len, ciphertext + aad_len, - size) == 0) + size) == 0) { warnx("OpenSSL %s (%zu, %zu): cipher text unchanged", alg->name, aad_len, size); + goto out_noocf; + } digest_len = sizeof(control_digest); if (HMAC(md, auth_key, auth_key_len, (u_char *)ciphertext, - aad_len + size, (u_char *)control_digest, &digest_len) == NULL) - errx(1, "OpenSSL %s (%zu, %zu) HMAC failed: %s", alg->name, + aad_len + size, (u_char *)control_digest, &digest_len) == NULL) { + warnx("OpenSSL %s (%zu, %zu) HMAC failed: %s", alg->name, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto out_noocf; + } if (!ocf_init_eta_session(alg, cipher_key, cipher_key_len, auth_key, auth_key_len, &ses)) - goto out; + goto out_noocf; /* OCF encrypt + HMAC. */ error = ocf_eta(&ses, iv, iv_len, aad_len != 0 ? cleartext : NULL, @@ -1060,6 +1090,7 @@ run_eta_test(const struct alg *alg, size_t aad_len, size_t size) out: ocf_destroy_session(&ses); +out_noocf: free(ciphertext); free(buffer); free(cleartext); @@ -1068,7 +1099,7 @@ out: free(cipher_key); } -static void +static bool openssl_gmac(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, const char *iv, const char *input, size_t size, char *tag) { @@ -1076,26 +1107,41 @@ openssl_gmac(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, int outl; ctx = EVP_CIPHER_CTX_new(); - if (ctx == NULL) - errx(1, "OpenSSL %s (%zu) ctx new failed: %s", alg->name, + if (ctx == NULL) { + warnx("OpenSSL %s (%zu) ctx new failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + return (false); + } if (EVP_EncryptInit_ex(ctx, cipher, NULL, (const u_char *)key, - (const u_char *)iv) != 1) - errx(1, "OpenSSL %s (%zu) ctx init failed: %s", alg->name, + (const u_char *)iv) != 1) { + warnx("OpenSSL %s (%zu) ctx init failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } EVP_CIPHER_CTX_set_padding(ctx, 0); if (EVP_EncryptUpdate(ctx, NULL, &outl, (const u_char *)input, - size) != 1) - errx(1, "OpenSSL %s (%zu) update failed: %s", + size) != 1) { + warnx("OpenSSL %s (%zu) update failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_EncryptFinal_ex(ctx, NULL, &outl) != 1) - errx(1, "OpenSSL %s (%zu) final failed: %s", alg->name, + goto error; + } + if (EVP_EncryptFinal_ex(ctx, NULL, &outl) != 1) { + warnx("OpenSSL %s (%zu) final failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, alg->tag_len, - tag) != 1) - errx(1, "OpenSSL %s (%zu) get tag failed: %s", alg->name, + tag) != 1) { + warnx("OpenSSL %s (%zu) get tag failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } EVP_CIPHER_CTX_free(ctx); + return (true); + +error: + EVP_CIPHER_CTX_free(ctx); + return (false); } static bool @@ -1154,7 +1200,8 @@ run_gmac_test(const struct alg *alg, size_t size) buffer = alloc_buffer(size); /* OpenSSL GMAC. */ - openssl_gmac(alg, cipher, key, iv, buffer, size, control_tag); + if (!openssl_gmac(alg, cipher, key, iv, buffer, size, control_tag)) + goto out; /* OCF GMAC. */ if (!ocf_mac(alg, buffer, size, key, key_len, iv, test_tag, &crid)) @@ -1178,7 +1225,7 @@ out: free(key); } -static void +static bool openssl_digest(const struct alg *alg, const char *key, u_int key_len, const char *input, size_t size, char *tag, u_int tag_len) { @@ -1187,25 +1234,42 @@ openssl_digest(const struct alg *alg, const char *key, u_int key_len, size_t len; pkey = EVP_PKEY_new_raw_private_key(alg->pkey, NULL, key, key_len); - if (pkey == NULL) - errx(1, "OpenSSL %s (%zu) pkey new failed: %s", alg->name, + if (pkey == NULL) { + warnx("OpenSSL %s (%zu) pkey new failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + return (false); + } mdctx = EVP_MD_CTX_new(); - if (mdctx == NULL) - errx(1, "OpenSSL %s (%zu) ctx new failed: %s", alg->name, + if (mdctx == NULL) { + warnx("OpenSSL %s (%zu) ctx new failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_DigestSignInit(mdctx, NULL, NULL, NULL, pkey) != 1) - errx(1, "OpenSSL %s (%zu) digest sign init failed: %s", + EVP_PKEY_free(pkey); + return (false); + } + if (EVP_DigestSignInit(mdctx, NULL, NULL, NULL, pkey) != 1) { + warnx("OpenSSL %s (%zu) digest sign init failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_DigestSignUpdate(mdctx, input, size) != 1) - errx(1, "OpenSSL %s (%zu) digest update failed: %s", alg->name, + goto error; + } + if (EVP_DigestSignUpdate(mdctx, input, size) != 1) { + warnx("OpenSSL %s (%zu) digest update failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } len = tag_len; - if (EVP_DigestSignFinal(mdctx, tag, &len) != 1) - errx(1, "OpenSSL %s (%zu) digest final failed: %s", alg->name, + if (EVP_DigestSignFinal(mdctx, tag, &len) != 1) { + warnx("OpenSSL %s (%zu) digest final failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } EVP_MD_CTX_free(mdctx); EVP_PKEY_free(pkey); + return (true); + +error: + EVP_MD_CTX_free(mdctx); + EVP_PKEY_free(pkey); + return (false); } static void @@ -1225,8 +1289,9 @@ run_digest_test(const struct alg *alg, size_t size) buffer = alloc_buffer(size); /* OpenSSL Poly1305. */ - openssl_digest(alg, key, key_len, buffer, size, control_tag, - sizeof(control_tag)); + if (!openssl_digest(alg, key, key_len, buffer, size, control_tag, + sizeof(control_tag))) + goto out; /* OCF Poly1305. */ if (!ocf_mac(alg, buffer, size, key, key_len, NULL, test_tag, &crid)) @@ -1249,7 +1314,7 @@ out: free(key); } -static void +static bool openssl_aead_encrypt(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, const char *iv, size_t iv_len, const char *aad, size_t aad_len, const char *input, char *output, size_t size, char *tag) @@ -1258,44 +1323,68 @@ openssl_aead_encrypt(const struct alg *alg, const EVP_CIPHER *cipher, int outl, total; ctx = EVP_CIPHER_CTX_new(); - if (ctx == NULL) - errx(1, "OpenSSL %s (%zu) ctx new failed: %s", alg->name, + if (ctx == NULL) { + warnx("OpenSSL %s (%zu) ctx new failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) - errx(1, "OpenSSL %s (%zu) ctx init failed: %s", alg->name, + return (false); + } + if (EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) { + warnx("OpenSSL %s (%zu) ctx init failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL) != 1) - errx(1, "OpenSSL %s (%zu) setting iv length failed: %s", alg->name, + goto error; + } + if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL) != + 1) { + warnx("OpenSSL %s (%zu) setting iv length failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } if (EVP_EncryptInit_ex(ctx, NULL, NULL, (const u_char *)key, - (const u_char *)iv) != 1) - errx(1, "OpenSSL %s (%zu) ctx init failed: %s", alg->name, + (const u_char *)iv) != 1) { + warnx("OpenSSL %s (%zu) ctx init failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } EVP_CIPHER_CTX_set_padding(ctx, 0); if (aad != NULL) { if (EVP_EncryptUpdate(ctx, NULL, &outl, (const u_char *)aad, - aad_len) != 1) - errx(1, "OpenSSL %s (%zu) aad update failed: %s", + aad_len) != 1) { + warnx("OpenSSL %s (%zu) aad update failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } } if (EVP_EncryptUpdate(ctx, (u_char *)output, &outl, - (const u_char *)input, size) != 1) - errx(1, "OpenSSL %s (%zu) encrypt update failed: %s", alg->name, + (const u_char *)input, size) != 1) { + warnx("OpenSSL %s (%zu) encrypt update failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total = outl; - if (EVP_EncryptFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) - errx(1, "OpenSSL %s (%zu) encrypt final failed: %s", alg->name, + if (EVP_EncryptFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) { + warnx("OpenSSL %s (%zu) encrypt final failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total += outl; - if ((size_t)total != size) - errx(1, "OpenSSL %s (%zu) encrypt size mismatch: %d", alg->name, + if ((size_t)total != size) { + warnx("OpenSSL %s (%zu) encrypt size mismatch: %d", alg->name, size, total); + goto error; + } if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, alg->tag_len, - tag) != 1) - errx(1, "OpenSSL %s (%zu) get tag failed: %s", alg->name, + tag) != 1) { + warnx("OpenSSL %s (%zu) get tag failed: %s", alg->name, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } + EVP_CIPHER_CTX_free(ctx); + return (true); + +error: EVP_CIPHER_CTX_free(ctx); + return (false); } #ifdef notused @@ -1343,7 +1432,7 @@ openssl_aead_decrypt(const struct alg *alg, const EVP_CIPHER *cipher, } #endif -static void +static bool openssl_ccm_encrypt(const struct alg *alg, const EVP_CIPHER *cipher, const char *key, const char *iv, size_t iv_len, const char *aad, size_t aad_len, const char *input, char *output, size_t size, char *tag) @@ -1352,63 +1441,88 @@ openssl_ccm_encrypt(const struct alg *alg, const EVP_CIPHER *cipher, int outl, total; ctx = EVP_CIPHER_CTX_new(); - if (ctx == NULL) - errx(1, "OpenSSL %s/%zu (%zu, %zu) ctx new failed: %s", + if (ctx == NULL) { + warnx("OpenSSL %s/%zu (%zu, %zu) ctx new failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) - errx(1, "OpenSSL %s/%zu (%zu, %zu) ctx init failed: %s", + return (false); + } + if (EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) ctx init failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL) != 1) - errx(1, - "OpenSSL %s/%zu (%zu, %zu) setting iv length failed: %s", + goto error; + } + if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, iv_len, NULL) != + 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) setting iv length failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, AES_CBC_MAC_HASH_LEN, NULL) != 1) - errx(1, - "OpenSSL %s/%zu (%zu, %zu) setting tag length failed: %s", + goto error; + } + if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, AES_CBC_MAC_HASH_LEN, + NULL) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) setting tag length failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } if (EVP_EncryptInit_ex(ctx, NULL, NULL, (const u_char *)key, - (const u_char *)iv) != 1) - errx(1, "OpenSSL %s/%zu (%zu, %zu) ctx init failed: %s", + (const u_char *)iv) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) ctx init failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); - if (EVP_EncryptUpdate(ctx, NULL, &outl, NULL, size) != 1) - errx(1, - "OpenSSL %s/%zu (%zu, %zu) unable to set data length: %s", + goto error; + } + if (EVP_EncryptUpdate(ctx, NULL, &outl, NULL, size) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) unable to set data length: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } if (aad != NULL) { if (EVP_EncryptUpdate(ctx, NULL, &outl, (const u_char *)aad, - aad_len) != 1) - errx(1, - "OpenSSL %s/%zu (%zu, %zu) aad update failed: %s", + aad_len) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) aad update failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } } if (EVP_EncryptUpdate(ctx, (u_char *)output, &outl, - (const u_char *)input, size) != 1) - errx(1, "OpenSSL %s/%zu (%zu, %zu) encrypt update failed: %s", + (const u_char *)input, size) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) encrypt update failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total = outl; - if (EVP_EncryptFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) - errx(1, "OpenSSL %s/%zu (%zu, %zu) encrypt final failed: %s", + if (EVP_EncryptFinal_ex(ctx, (u_char *)output + outl, &outl) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) encrypt final failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } total += outl; - if ((size_t)total != size) - errx(1, "OpenSSL %s/%zu (%zu, %zu) encrypt size mismatch: %d", + if ((size_t)total != size) { + warnx("OpenSSL %s/%zu (%zu, %zu) encrypt size mismatch: %d", alg->name, iv_len, aad_len, size, total); + goto error; + } if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, AES_CBC_MAC_HASH_LEN, - tag) != 1) - errx(1, "OpenSSL %s/%zu (%zu, %zu) get tag failed: %s", + tag) != 1) { + warnx("OpenSSL %s/%zu (%zu, %zu) get tag failed: %s", alg->name, iv_len, aad_len, size, ERR_error_string(ERR_get_error(), NULL)); + goto error; + } + EVP_CIPHER_CTX_free(ctx); + return (true); + +error: EVP_CIPHER_CTX_free(ctx); + return (false); } static bool @@ -1490,6 +1604,7 @@ run_aead_test(const struct alg *alg, size_t aad_len, size_t size, u_int key_len; int error; char control_tag[AEAD_MAX_TAG_LEN], test_tag[AEAD_MAX_TAG_LEN]; + bool ok; cipher = alg->evp_cipher(); if (size % EVP_CIPHER_block_size(cipher) != 0) { @@ -1526,14 +1641,16 @@ run_aead_test(const struct alg *alg, size_t aad_len, size_t size, /* OpenSSL encrypt */ if (EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE) - openssl_ccm_encrypt(alg, cipher, key, iv, iv_len, aad, + ok = openssl_ccm_encrypt(alg, cipher, key, iv, iv_len, aad, aad_len, cleartext, ciphertext, size, control_tag); else - openssl_aead_encrypt(alg, cipher, key, iv, iv_len, aad, + ok = openssl_aead_encrypt(alg, cipher, key, iv, iv_len, aad, aad_len, cleartext, ciphertext, size, control_tag); + if (!ok) + goto out_noocf; if (!ocf_init_aead_session(alg, key, key_len, iv_len, &ses)) - goto out; + goto out_noocf; /* OCF encrypt */ error = ocf_aead(&ses, iv, iv_len, aad, aad_len, cleartext, buffer, @@ -1602,6 +1719,7 @@ run_aead_test(const struct alg *alg, size_t aad_len, size_t size, out: ocf_destroy_session(&ses); +out_noocf: free(aad); free(ciphertext); free(buffer);