From nobody Mon Nov 23 04:26:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1793622267; cv=none; d=zohomail.com; s=zohoarc; b=ePnG+pfQM0HWXLs2P8IjK87H4QUT1Y1MXqGO5+h6T9es7c85YppXnCDkZP6MfTcHWkSZSouRbDIY0zFZoGJaEDNoWWFQpfxezftWxDzNerSeB5sEjp4OeuG5fpwCJci/+uFMIwUUlrZEvnNzOfZttglNmb4VAd4o40Ts4kvkTwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793622267; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=x1+LpcslGBEliiOMWoZSSQNfVGm/yD8sVMN11d+3YvA=; b=hUmEeaR7GE/ErEkqJ8xMossNBw9IOUY5iQy7vGOdIyiYJgUtoNsMZsHoJ+0sR3bNTzb6Bl5LzsP2od/RG1Vrx1pQeeu7WcW+x2W2cYrwflWWQjvYh9S2Nm3Bxb8DDwP7aZUxiRAJ8b/1NoOaUZ4R7wM4oLVTVzb3cer4AJDZ6Kk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793622267196200.5499379260856; Mon, 2 Nov 2026 04:24:27 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNr4r-00064b-Pv; Mon, 02 Nov 2026 07:23:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4q-000644-N5 for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:52 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4p-0001Ob-4e for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:52 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-641-p7PHSDIoMEadnvdvJxi3og-1; Mon, 02 Nov 2026 07:23:47 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D73CE1954117; Mon, 2 Nov 2026 12:23:46 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F01BC1800480; Mon, 2 Nov 2026 12:23:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793622229; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x1+LpcslGBEliiOMWoZSSQNfVGm/yD8sVMN11d+3YvA=; b=dYPZEgweSa4Bsgo959QFh9/KfO1bGQv4LlWVTC1vclu41BulKVb6lQym9CsmpymYlHNVF4 IJgfnVko2vesjO3hE8Tp8/14BNfKAweA12VtweJIAsIIclFQr/lnHaLH/mv0lrFJa86ogQ izSycGXiJ5okCjabJKpt+628mYyJZ7s= X-MC-Unique: p7PHSDIoMEadnvdvJxi3og-1 X-Mimecast-MFC-AGG-ID: p7PHSDIoMEadnvdvJxi3og_1793622227 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v2 1/4] crypto: set default initializer hmac/hash arrays to -1 Date: Mon, 2 Nov 2026 12:23:40 +0000 Message-ID: <20261102122343.3348956-2-berrange@redhat.com> In-Reply-To: <20261102122343.3348956-1-berrange@redhat.com> References: <20261102122343.3348956-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1793622270370154100 The default static array initializer of 0 is not suitable for the glib hash/hmac backends, as G_CHECKSUM_MD5 has a value of 0. Instead elements must be initialized to -1, but remembering todo that as new algorithms are defined is errorprone. The SM3 algorithm initializer is missing, leading to it being mapped to MD5. This could potentially be a security flaw through use of the wrong (much weaker) hashing algorithm, if a further bug elsewhere didn't mask the issue. Set a default array initializer so unlisted algorithms are always set to -1 instead of 0. Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/4786 Signed-off-by: Daniel P. Berrang=C3=A9 --- crypto/hash-glib.c | 3 +-- crypto/hmac-glib.c | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/crypto/hash-glib.c b/crypto/hash-glib.c index 809cef98aee..d915430f459 100644 --- a/crypto/hash-glib.c +++ b/crypto/hash-glib.c @@ -26,13 +26,12 @@ =20 =20 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALGO__MAX] =3D { + [0 ... QCRYPTO_HASH_ALGO__MAX - 1] =3D -1, [QCRYPTO_HASH_ALGO_MD5] =3D G_CHECKSUM_MD5, [QCRYPTO_HASH_ALGO_SHA1] =3D G_CHECKSUM_SHA1, - [QCRYPTO_HASH_ALGO_SHA224] =3D -1, [QCRYPTO_HASH_ALGO_SHA256] =3D G_CHECKSUM_SHA256, [QCRYPTO_HASH_ALGO_SHA384] =3D G_CHECKSUM_SHA384, [QCRYPTO_HASH_ALGO_SHA512] =3D G_CHECKSUM_SHA512, - [QCRYPTO_HASH_ALGO_RIPEMD160] =3D -1, }; =20 gboolean qcrypto_hash_supports(QCryptoHashAlgo alg) diff --git a/crypto/hmac-glib.c b/crypto/hmac-glib.c index 1f17769c1c7..4453e0d5f06 100644 --- a/crypto/hmac-glib.c +++ b/crypto/hmac-glib.c @@ -18,13 +18,11 @@ #include "hmacpriv.h" =20 static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] =3D { + [0 ... QCRYPTO_HASH_ALGO__MAX - 1] =3D -1, [QCRYPTO_HASH_ALGO_MD5] =3D G_CHECKSUM_MD5, [QCRYPTO_HASH_ALGO_SHA1] =3D G_CHECKSUM_SHA1, [QCRYPTO_HASH_ALGO_SHA256] =3D G_CHECKSUM_SHA256, [QCRYPTO_HASH_ALGO_SHA512] =3D G_CHECKSUM_SHA512, - [QCRYPTO_HASH_ALGO_SHA224] =3D -1, - [QCRYPTO_HASH_ALGO_SHA384] =3D -1, - [QCRYPTO_HASH_ALGO_RIPEMD160] =3D -1, }; =20 typedef struct QCryptoHmacGlib QCryptoHmacGlib; --=20 2.56.0 From nobody Mon Nov 23 04:26:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1793622285; cv=none; d=zohomail.com; s=zohoarc; b=LggBdLw+yyVe62mZg9QJOXSC0w4RdQYsA0nTUoSjQDXxLRbhZYvmX4lIndu83wy+t7rZuplsq014dit2rXf5q9fIPpY1LcG/TUUCu5HH+EF9crEecKb1DgRbASEv+RX1dn0/81HP9pSXEEb7lcSRGDL8bAvSQmsakiboWFKglL4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793622285; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V3RYicfA4Pw7ib/W99AxB190i0s6VxYIiej6qMSywHE=; b=jg06fFKR8DOkROp0Zbgu2GHuvbJcAuASBR863d5SDFIlBJ5QD1BDdmiqpsjiTRWZzYURiriXjJxndGqshz0HUQwV5/pwZv0OwKgmFCG6gonNi8tfIGcIy8wqA7eUGxovoOA5hBkaIJ32PpchE0TubkLwWMPsodz5IWC9zs9r5FA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793622284762515.7236325425387; Mon, 2 Nov 2026 04:24:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNr4s-000657-Pg; Mon, 02 Nov 2026 07:23:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4r-00064d-PH for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4p-0001Om-Gb for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:53 -0500 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-484-Pcrw7--GOQeubB4usvxoZA-1; Mon, 02 Nov 2026 07:23:49 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5A45E180AC7C; Mon, 2 Nov 2026 12:23:48 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6B6D41800480; Mon, 2 Nov 2026 12:23:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793622230; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V3RYicfA4Pw7ib/W99AxB190i0s6VxYIiej6qMSywHE=; b=JwlabwOyUu5BsH18vT51lgZdTJ7CeB/VZ2Zpl0l5IAhjtifmA/ayOou4vqJNVbje4+UzeW d2IGzn9oNhO8UUzYr+Es+mrOLt+W+r9WEx2X/IrHhqK9hP+Y4+IDPHORCDUTtACrQlTvhc IQK2remf8C8VNDsroAdP6otbMc4qQn0= X-MC-Unique: Pcrw7--GOQeubB4usvxoZA-1 X-Mimecast-MFC-AGG-ID: Pcrw7--GOQeubB4usvxoZA_1793622228 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v2 2/4] crypto: unconditionally report SM3 hash length Date: Mon, 2 Nov 2026 12:23:41 +0000 Message-ID: <20261102122343.3348956-3-berrange@redhat.com> In-Reply-To: <20261102122343.3348956-1-berrange@redhat.com> References: <20261102122343.3348956-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1793622287611158500 Callers may query the hash digest length before getting to the code path that performs hashing, which is where the lack of support for SM3 in the backend will be reported. Reporting a hash length of 0 is liable to cause divide by zero errors in callers. Fixes: https://gitlab.com/qemu-project/qemu/-/work_items/4786 Signed-off-by: Daniel P. Berrang=C3=A9 --- crypto/hash.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crypto/hash.c b/crypto/hash.c index 6ffb88bf541..42a0a8ec56f 100644 --- a/crypto/hash.c +++ b/crypto/hash.c @@ -33,9 +33,7 @@ static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALGO__MA= X] =3D { [QCRYPTO_HASH_ALGO_SHA384] =3D QCRYPTO_HASH_DIGEST_LEN_SHA384, [QCRYPTO_HASH_ALGO_SHA512] =3D QCRYPTO_HASH_DIGEST_LEN_SHA512, [QCRYPTO_HASH_ALGO_RIPEMD160] =3D QCRYPTO_HASH_DIGEST_LEN_RIPEMD160, -#ifdef CONFIG_CRYPTO_SM3 - [QCRYPTO_HASH_ALGO_SM3] =3D QCRYPTO_HASH_DIGEST_LEN_SM3, -#endif + [QCRYPTO_HASH_ALGO_SM3] =3D QCRYPTO_HASH_DIGEST_LEN_SM3, }; =20 size_t qcrypto_hash_digest_len(QCryptoHashAlgo alg) --=20 2.56.0 From nobody Mon Nov 23 04:26:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1793622297; cv=none; d=zohomail.com; s=zohoarc; b=URiwD8Imfl5BSflttXadgNHlLCXcjAdUbdRlTD8Q4kVgD5/o2bwtcMCH9dAUU7IPxby4DEIH3wTAGJWS4cNSbTPS4RYwYwk0h9FiMn+1KdHMD5hZ26K482cQY68ovkPm4j3qpe3QmKjDJG0O6oFJs2rgImlYslxIhesIYtMmoUE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793622297; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Uz8Hrgu0Sh2CH2H2mhPX4+rMsFO85y8ygqyZ7xV7v18=; b=LCEG+CddOKCIUnRPJ7GtZ1VbdAhgKtY12i3tkbaVZ15+4bnKgxre+69TbeK/i9zYv/BX9AlMHgYCmEN6rwzb/YwK4sa6wWc2lVnuGV5errmWX/Ym8EprIyOLEVvf5wQA8dx6zmxdodfkbe1mB1mMwPC7/d/XJorJbBcDOf/byew= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793622297358526.4244228080444; Mon, 2 Nov 2026 04:24:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNr4t-00065f-T3; Mon, 02 Nov 2026 07:23:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4r-00064j-Ux for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:53 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4q-0001Oq-Em for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:23:53 -0500 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-342-LYCG9YVTO22WlDvfVPP0DA-1; Mon, 02 Nov 2026 07:23:50 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 945221955BEE; Mon, 2 Nov 2026 12:23:49 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ACA2F1800480; Mon, 2 Nov 2026 12:23:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793622231; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Uz8Hrgu0Sh2CH2H2mhPX4+rMsFO85y8ygqyZ7xV7v18=; b=ZKbM6fsAgJO8YecqNzvzzsKGp32Gg0naXTmjGQ244VaCEYRhZCkoIWWqRAITHISczLCRuz U3SxAZKnEaNO794Kd06QwzBTp4ugSue5pI9jnfJ++XWLKTCqgyK7eUVzdZsZgFEFZPsZfc qrvx9K5JJBj47EZp1FgLMZiuwwVFUzc= X-MC-Unique: LYCG9YVTO22WlDvfVPP0DA-1 X-Mimecast-MFC-AGG-ID: LYCG9YVTO22WlDvfVPP0DA_1793622229 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v2 3/4] tests/unit: drop conditionals for SM3 Date: Mon, 2 Nov 2026 12:23:42 +0000 Message-ID: <20261102122343.3348956-4-berrange@redhat.com> In-Reply-To: <20261102122343.3348956-1-berrange@redhat.com> References: <20261102122343.3348956-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: 5 X-Spam_score: 0.5 X-Spam_bar: / X-Spam_report: (0.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1793622300773154100 There is no need to conditionalize test data for SM3, since the test explicitly checks whether each algorithm is supported. Signed-off-by: Daniel P. Berrang=C3=A9 --- tests/unit/test-crypto-hash.c | 10 ---------- tests/unit/test-crypto-hmac.c | 4 +--- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/tests/unit/test-crypto-hash.c b/tests/unit/test-crypto-hash.c index 8fee1593f94..1813e77b5a5 100644 --- a/tests/unit/test-crypto-hash.c +++ b/tests/unit/test-crypto-hash.c @@ -43,9 +43,7 @@ "63b54e4cb2d2032b393994aa263c0dbb" \ "e00a9f2fe9ef6037352232a1eec55ee7" #define OUTPUT_RIPEMD160 "f3d658fad3fdfb2b52c9369cf0d441249ddfa8a0" -#ifdef CONFIG_CRYPTO_SM3 #define OUTPUT_SM3 "d4a97db105b477b84c4f20ec9c31a6c814e2705a0b83a5a89748d7= 5f0ef456a1" -#endif =20 #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=3D=3D" #define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI=3D" @@ -58,9 +56,7 @@ "7sVe5w=3D=3D" #define OUTPUT_RIPEMD160_B64 "89ZY+tP9+ytSyTac8NRBJJ3fqKA=3D" =20 -#ifdef CONFIG_CRYPTO_SM3 #define OUTPUT_SM3_B64 "1Kl9sQW0d7hMTyDsnDGmyBTicFoLg6Wol0jXXw70VqE=3D" -#endif =20 static const char *expected_outputs[] =3D { [QCRYPTO_HASH_ALGO_MD5] =3D OUTPUT_MD5, @@ -70,9 +66,7 @@ static const char *expected_outputs[] =3D { [QCRYPTO_HASH_ALGO_SHA384] =3D OUTPUT_SHA384, [QCRYPTO_HASH_ALGO_SHA512] =3D OUTPUT_SHA512, [QCRYPTO_HASH_ALGO_RIPEMD160] =3D OUTPUT_RIPEMD160, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] =3D OUTPUT_SM3, -#endif }; static const char *expected_outputs_b64[] =3D { [QCRYPTO_HASH_ALGO_MD5] =3D OUTPUT_MD5_B64, @@ -82,9 +76,7 @@ static const char *expected_outputs_b64[] =3D { [QCRYPTO_HASH_ALGO_SHA384] =3D OUTPUT_SHA384_B64, [QCRYPTO_HASH_ALGO_SHA512] =3D OUTPUT_SHA512_B64, [QCRYPTO_HASH_ALGO_RIPEMD160] =3D OUTPUT_RIPEMD160_B64, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] =3D OUTPUT_SM3_B64, -#endif }; static const int expected_lens[] =3D { [QCRYPTO_HASH_ALGO_MD5] =3D 16, @@ -94,9 +86,7 @@ static const int expected_lens[] =3D { [QCRYPTO_HASH_ALGO_SHA384] =3D 48, [QCRYPTO_HASH_ALGO_SHA512] =3D 64, [QCRYPTO_HASH_ALGO_RIPEMD160] =3D 20, -#ifdef CONFIG_CRYPTO_SM3 [QCRYPTO_HASH_ALGO_SM3] =3D 32, -#endif }; =20 static const char hex[] =3D "0123456789abcdef"; diff --git a/tests/unit/test-crypto-hmac.c b/tests/unit/test-crypto-hmac.c index 20c60eb9d8b..f7158621908 100644 --- a/tests/unit/test-crypto-hmac.c +++ b/tests/unit/test-crypto-hmac.c @@ -76,14 +76,12 @@ static QCryptoHmacTestData test_data[] =3D { "94964ed4c1155b62b668c241d67279e5" "8a711676", }, -#ifdef CONFIG_CRYPTO_SM3 { .alg =3D QCRYPTO_HASH_ALGO_SM3, .hex_digest =3D "760e3799332bc913819b930085360ddb" - "c05529261313d5b15b75bab4fd7ae91e", + "c05529261313d5b15b75bab4fd7ae91e", }, -#endif }; =20 static const char hex[] =3D "0123456789abcdef"; --=20 2.56.0 From nobody Mon Nov 23 04:26:16 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1793622265; cv=none; d=zohomail.com; s=zohoarc; b=IMWUHStAdcBtP2YcdPI8WH3MEVPtpL4nxfnYF7kUEUvLsaXXY0i8j+h8McHZzXLUiuBBrJry0PD4o/OAOdygfdksUFhEnX3dwvdTYaQBHjotIWFFStaZ4a+cvgxYpBvy/yNj8L6ypTvVgFSBXwvxGy/OdtBTemMsCngtyrAN6YQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793622265; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=INvTgZSdQ7+HVJvkjanBtO4MoKUVwdhTiernyxLhUSE=; b=iuaj4bA6VhjqRz0bBfN+aRkRzseFAzfEGbBz+GJEvG0AMoEwtRmFKpunjeD1IvbxDuxzgkR4AxJ/vXwwntgwpFoT5bb2s3IyCxTx9kajikyc6KrksrUMU7hg5ELRhK8RVMfv5WFMBWO/I9h6vuOS3qXFkDzYhUaJu07TqtEqjbc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793622265877836.0390550619178; Mon, 2 Nov 2026 04:24:25 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNr51-00066k-Fm; Mon, 02 Nov 2026 07:24:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4y-00066M-9E for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:24:00 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNr4v-0001PQ-9K for qemu-devel@nongnu.org; Mon, 02 Nov 2026 07:24:00 -0500 Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-173-xP_KByenPB6dASTULsbe6Q-1; Mon, 02 Nov 2026 07:23:52 -0500 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 043581802578; Mon, 2 Nov 2026 12:23:51 +0000 (UTC) Received: from berrange.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E56671800480; Mon, 2 Nov 2026 12:23:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1793622235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=INvTgZSdQ7+HVJvkjanBtO4MoKUVwdhTiernyxLhUSE=; b=SePryHVuMknbSlpt54GInoKSPxtncWxd824PlVxlVA6mxMOOC2NxiedBbEdrkO6y+Leu5s 49Va1TKd8Z9zUaMejFsGSrMM9zFN5Qh7Chb8TGH2f0AmBFCdM+GZfl5z6AqeJRT5LmmtSN rOjp9XYU0SHegYGXHVPrqb0Mwfdf8DE= X-MC-Unique: xP_KByenPB6dASTULsbe6Q-1 X-Mimecast-MFC-AGG-ID: xP_KByenPB6dASTULsbe6Q_1793622231 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v2 4/4] tests/unit: register one test per hash/hmac algorithm Date: Mon, 2 Nov 2026 12:23:43 +0000 Message-ID: <20261102122343.3348956-5-berrange@redhat.com> In-Reply-To: <20261102122343.3348956-1-berrange@redhat.com> References: <20261102122343.3348956-1-berrange@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=berrange@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1793622267644158500 The hash/hmac tests iterate over all support algorithms testing each one in turn, but this is not visible from the test output. Register one test per algorithm so the test result directly reflect what algorithms were tested. This is useful information since different crypto backends support different subsets of algorithms. Signed-off-by: Daniel P. Berrang=C3=A9 --- tests/unit/test-crypto-hash.c | 315 ++++++++++++++++------------------ tests/unit/test-crypto-hmac.c | 268 ++++++++++++++--------------- 2 files changed, 271 insertions(+), 312 deletions(-) diff --git a/tests/unit/test-crypto-hash.c b/tests/unit/test-crypto-hash.c index 1813e77b5a5..fbdae46fd64 100644 --- a/tests/unit/test-crypto-hash.c +++ b/tests/unit/test-crypto-hash.c @@ -92,218 +92,191 @@ static const int expected_lens[] =3D { static const char hex[] =3D "0123456789abcdef"; =20 /* Test with dynamic allocation */ -static void test_hash_alloc(void) +static void test_hash_alloc(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - uint8_t *result =3D NULL; - size_t resultlen =3D 0; - int ret; - size_t j; - - if (!qcrypto_hash_supports(i)) { - continue; - } - - ret =3D qcrypto_hash_bytes(i, - INPUT_TEXT, - strlen(INPUT_TEXT), - &result, - &resultlen, - &error_fatal); - g_assert(ret =3D=3D 0); - g_assert(resultlen =3D=3D expected_lens[i]); + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + uint8_t *result =3D NULL; + size_t resultlen =3D 0; + int ret; + size_t j; + + ret =3D qcrypto_hash_bytes(alg, + INPUT_TEXT, + strlen(INPUT_TEXT), + &result, + &resultlen, + &error_fatal); + g_assert(ret =3D=3D 0); + g_assert(resultlen =3D=3D expected_lens[alg]); =20 - for (j =3D 0; j < resultlen; j++) { - g_assert(expected_outputs[i][j * 2] =3D=3D hex[(result[j] >> 4= ) & 0xf]); - g_assert(expected_outputs[i][j * 2 + 1] =3D=3D hex[result[j] &= 0xf]); - } - g_free(result); + for (j =3D 0; j < resultlen; j++) { + g_assert(expected_outputs[alg][j * 2] =3D=3D hex[(result[j] >> 4) = & 0xf]); + g_assert(expected_outputs[alg][j * 2 + 1] =3D=3D hex[result[j] & 0= xf]); } + g_free(result); } =20 /* Test with caller preallocating */ -static void test_hash_prealloc(void) +static void test_hash_prealloc(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - uint8_t *result, *origresult; - size_t resultlen; - int ret; - size_t j; - - if (!qcrypto_hash_supports(i)) { - continue; - } - - resultlen =3D expected_lens[i]; - origresult =3D result =3D g_new0(uint8_t, resultlen); - - ret =3D qcrypto_hash_bytes(i, - INPUT_TEXT, - strlen(INPUT_TEXT), - &result, - &resultlen, - &error_fatal); - g_assert(ret =3D=3D 0); - /* Validate that our pre-allocated pointer was not replaced */ - g_assert(result =3D=3D origresult); - g_assert(resultlen =3D=3D expected_lens[i]); - for (j =3D 0; j < resultlen; j++) { - g_assert(expected_outputs[i][j * 2] =3D=3D hex[(result[j] >> 4= ) & 0xf]); - g_assert(expected_outputs[i][j * 2 + 1] =3D=3D hex[result[j] &= 0xf]); - } - g_free(result); + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + uint8_t *result, *origresult; + size_t resultlen; + int ret; + size_t j; + + resultlen =3D expected_lens[alg]; + origresult =3D result =3D g_new0(uint8_t, resultlen); + + ret =3D qcrypto_hash_bytes(alg, + INPUT_TEXT, + strlen(INPUT_TEXT), + &result, + &resultlen, + &error_fatal); + g_assert(ret =3D=3D 0); + /* Validate that our pre-allocated pointer was not replaced */ + g_assert(result =3D=3D origresult); + g_assert(resultlen =3D=3D expected_lens[alg]); + for (j =3D 0; j < resultlen; j++) { + g_assert(expected_outputs[alg][j * 2] =3D=3D hex[(result[j] >> 4) = & 0xf]); + g_assert(expected_outputs[alg][j * 2 + 1] =3D=3D hex[result[j] & 0= xf]); } + g_free(result); } =20 =20 /* Test with dynamic allocation */ -static void test_hash_iov(void) +static void test_hash_iov(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - struct iovec iov[3] =3D { - { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT= _TEXT1) }, - { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT= _TEXT2) }, - { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT= _TEXT3) }, - }; - uint8_t *result =3D NULL; - size_t resultlen =3D 0; - int ret; - size_t j; - - if (!qcrypto_hash_supports(i)) { - continue; - } - - ret =3D qcrypto_hash_bytesv(i, - iov, 3, - &result, - &resultlen, - &error_fatal); - g_assert(ret =3D=3D 0); - g_assert(resultlen =3D=3D expected_lens[i]); - for (j =3D 0; j < resultlen; j++) { - g_assert(expected_outputs[i][j * 2] =3D=3D hex[(result[j] >> 4= ) & 0xf]); - g_assert(expected_outputs[i][j * 2 + 1] =3D=3D hex[result[j] &= 0xf]); - } - g_free(result); + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + struct iovec iov[3] =3D { + { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT_TEX= T1) }, + { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT_TEX= T2) }, + { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT_TEX= T3) }, + }; + uint8_t *result =3D NULL; + size_t resultlen =3D 0; + int ret; + size_t j; + + ret =3D qcrypto_hash_bytesv(alg, + iov, 3, + &result, + &resultlen, + &error_fatal); + g_assert(ret =3D=3D 0); + g_assert(resultlen =3D=3D expected_lens[alg]); + for (j =3D 0; j < resultlen; j++) { + g_assert(expected_outputs[alg][j * 2] =3D=3D hex[(result[j] >> 4) = & 0xf]); + g_assert(expected_outputs[alg][j * 2 + 1] =3D=3D hex[result[j] & 0= xf]); } + g_free(result); } =20 =20 /* Test with printable hashing */ -static void test_hash_digest(void) +static void test_hash_digest(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - int ret; - char *digest; - size_t digestsize; - - if (!qcrypto_hash_supports(i)) { - continue; - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + int ret; + char *digest; + size_t digestsize; =20 - digestsize =3D qcrypto_hash_digest_len(i); + digestsize =3D qcrypto_hash_digest_len(alg); =20 - g_assert_cmpint(digestsize * 2, =3D=3D, strlen(expected_outputs[i]= )); + g_assert_cmpint(digestsize * 2, =3D=3D, strlen(expected_outputs[alg])); =20 - ret =3D qcrypto_hash_digest(i, - INPUT_TEXT, - strlen(INPUT_TEXT), - &digest, - &error_fatal); - g_assert(ret =3D=3D 0); - g_assert_cmpstr(digest, =3D=3D, expected_outputs[i]); - g_free(digest); - } + ret =3D qcrypto_hash_digest(alg, + INPUT_TEXT, + strlen(INPUT_TEXT), + &digest, + &error_fatal); + g_assert(ret =3D=3D 0); + g_assert_cmpstr(digest, =3D=3D, expected_outputs[alg]); + g_free(digest); } =20 /* Test with base64 encoding */ -static void test_hash_base64(void) +static void test_hash_base64(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - int ret; - char *digest; - - if (!qcrypto_hash_supports(i)) { - continue; - } - - ret =3D qcrypto_hash_base64(i, - INPUT_TEXT, - strlen(INPUT_TEXT), - &digest, - &error_fatal); - g_assert(ret =3D=3D 0); - g_assert_cmpstr(digest, =3D=3D, expected_outputs_b64[i]); - g_free(digest); - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + int ret; + char *digest; + + ret =3D qcrypto_hash_base64(alg, + INPUT_TEXT, + strlen(INPUT_TEXT), + &digest, + &error_fatal); + g_assert(ret =3D=3D 0); + g_assert_cmpstr(digest, =3D=3D, expected_outputs_b64[alg]); + g_free(digest); } =20 -static void test_hash_accumulate(void) +static void test_hash_accumulate(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(expected_outputs) ; i++) { - g_autoptr(QCryptoHash) hash =3D NULL; - struct iovec iov[] =3D { - { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT= _TEXT1) }, - { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT= _TEXT2) }, - { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT= _TEXT3) }, - }; - g_autofree uint8_t *result =3D NULL; - size_t resultlen =3D 0; - int ret; - size_t j; - - if (!qcrypto_hash_supports(i)) { - continue; - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + g_autoptr(QCryptoHash) hash =3D NULL; + struct iovec iov[] =3D { + { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT_TEX= T1) }, + { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT_TEX= T2) }, + { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT_TEX= T3) }, + }; + g_autofree uint8_t *result =3D NULL; + size_t resultlen =3D 0; + int ret; + size_t j; + + hash =3D qcrypto_hash_new(alg, &error_fatal); + g_assert(hash !=3D NULL); + + /* Add each iovec to the hash context separately */ + for (j =3D 0; j < G_N_ELEMENTS(iov); j++) { + ret =3D qcrypto_hash_updatev(hash, + &iov[j], 1, + &error_fatal); =20 - hash =3D qcrypto_hash_new(i, &error_fatal); - g_assert(hash !=3D NULL); + g_assert(ret =3D=3D 0); + } =20 - /* Add each iovec to the hash context separately */ - for (j =3D 0; j < G_N_ELEMENTS(iov); j++) { - ret =3D qcrypto_hash_updatev(hash, - &iov[j], 1, + ret =3D qcrypto_hash_finalize_bytes(hash, &result, &resultlen, &error_fatal); =20 - g_assert(ret =3D=3D 0); - } - - ret =3D qcrypto_hash_finalize_bytes(hash, &result, &resultlen, - &error_fatal); - - g_assert(ret =3D=3D 0); - g_assert(resultlen =3D=3D expected_lens[i]); - for (j =3D 0; j < resultlen; j++) { - g_assert(expected_outputs[i][j * 2] =3D=3D hex[(result[j] >> 4= ) & 0xf]); - g_assert(expected_outputs[i][j * 2 + 1] =3D=3D hex[result[j] &= 0xf]); - } + g_assert(ret =3D=3D 0); + g_assert(resultlen =3D=3D expected_lens[alg]); + for (j =3D 0; j < resultlen; j++) { + g_assert(expected_outputs[alg][j * 2] =3D=3D hex[(result[j] >> 4) = & 0xf]); + g_assert(expected_outputs[alg][j * 2 + 1] =3D=3D hex[result[j] & 0= xf]); } } =20 int main(int argc, char **argv) { + size_t i; int ret =3D qcrypto_init(&error_fatal); g_assert(ret =3D=3D 0); =20 g_test_init(&argc, &argv, NULL); - g_test_add_func("/crypto/hash/iov", test_hash_iov); - g_test_add_func("/crypto/hash/alloc", test_hash_alloc); - g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc); - g_test_add_func("/crypto/hash/digest", test_hash_digest); - g_test_add_func("/crypto/hash/base64", test_hash_base64); - g_test_add_func("/crypto/hash/accumulate", test_hash_accumulate); + +#define ADD_TEST(alg, prefix, meth) \ + { \ + g_autofree char *name =3D g_strdup_printf( \ + "%s/%s", prefix, QCryptoHashAlgo_str(alg)); \ + g_test_add_data_func(name, GINT_TO_POINTER(alg), meth); \ + } + + for (i =3D 0; i < QCRYPTO_HASH_ALGO__MAX ; i++) { + if (!qcrypto_hash_supports(i)) { + continue; + } + + ADD_TEST(i, "/crypto/hash/iov", test_hash_iov); + ADD_TEST(i, "/crypto/hash/alloc", test_hash_alloc); + ADD_TEST(i, "/crypto/hash/prealloc", test_hash_prealloc); + ADD_TEST(i, "/crypto/hash/digest", test_hash_digest); + ADD_TEST(i, "/crypto/hash/base64", test_hash_base64); + ADD_TEST(i, "/crypto/hash/accumulate", test_hash_accumulate); + } return g_test_run(); } diff --git a/tests/unit/test-crypto-hmac.c b/tests/unit/test-crypto-hmac.c index f7158621908..9c2ee8c17f8 100644 --- a/tests/unit/test-crypto-hmac.c +++ b/tests/unit/test-crypto-hmac.c @@ -86,177 +86,163 @@ static QCryptoHmacTestData test_data[] =3D { =20 static const char hex[] =3D "0123456789abcdef"; =20 -static void test_hmac_alloc(void) +static void test_hmac_alloc(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(test_data); i++) { - QCryptoHmacTestData *data =3D &test_data[i]; - QCryptoHmac *hmac =3D NULL; - uint8_t *result =3D NULL; - size_t resultlen =3D 0; - const char *exp_output =3D NULL; - int ret; - size_t j; - - if (!qcrypto_hmac_supports(data->alg)) { - return; - } - - exp_output =3D data->hex_digest; - - hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, - strlen(KEY), &error_fatal); - g_assert(hmac !=3D NULL); - - ret =3D qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT, - strlen(INPUT_TEXT), &result, - &resultlen, &error_fatal); - g_assert(ret =3D=3D 0); - - for (j =3D 0; j < resultlen; j++) { - g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); - g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + QCryptoHmacTestData *data =3D &test_data[alg]; + QCryptoHmac *hmac =3D NULL; + uint8_t *result =3D NULL; + size_t resultlen =3D 0; + const char *exp_output =3D NULL; + int ret; + size_t j; + + exp_output =3D data->hex_digest; + + hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, + strlen(KEY), &error_fatal); + g_assert(hmac !=3D NULL); + + ret =3D qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT, + strlen(INPUT_TEXT), &result, + &resultlen, &error_fatal); + g_assert(ret =3D=3D 0); + + for (j =3D 0; j < resultlen; j++) { + g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); + g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); + } =20 - qcrypto_hmac_free(hmac); + qcrypto_hmac_free(hmac); =20 - g_free(result); - } + g_free(result); } =20 -static void test_hmac_prealloc(void) +static void test_hmac_prealloc(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(test_data); i++) { - QCryptoHmacTestData *data =3D &test_data[i]; - QCryptoHmac *hmac =3D NULL; - uint8_t *result =3D NULL, *origresult =3D NULL; - size_t resultlen =3D 0; - const char *exp_output =3D NULL; - int ret; - size_t j; - - if (!qcrypto_hmac_supports(data->alg)) { - return; - } - - exp_output =3D data->hex_digest; - - resultlen =3D strlen(exp_output) / 2; - origresult =3D result =3D g_new0(uint8_t, resultlen); - - hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, - strlen(KEY), &error_fatal); - g_assert(hmac !=3D NULL); - - ret =3D qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT, - strlen(INPUT_TEXT), &result, - &resultlen, &error_fatal); - g_assert(ret =3D=3D 0); - /* Validate that our pre-allocated pointer was not replaced */ - g_assert(result =3D=3D origresult); - - exp_output =3D data->hex_digest; - for (j =3D 0; j < resultlen; j++) { - g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); - g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + QCryptoHmacTestData *data =3D &test_data[alg]; + QCryptoHmac *hmac =3D NULL; + uint8_t *result =3D NULL, *origresult =3D NULL; + size_t resultlen =3D 0; + const char *exp_output =3D NULL; + int ret; + size_t j; + + exp_output =3D data->hex_digest; + + resultlen =3D strlen(exp_output) / 2; + origresult =3D result =3D g_new0(uint8_t, resultlen); + + hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, + strlen(KEY), &error_fatal); + g_assert(hmac !=3D NULL); + + ret =3D qcrypto_hmac_bytes(hmac, (const char *)INPUT_TEXT, + strlen(INPUT_TEXT), &result, + &resultlen, &error_fatal); + g_assert(ret =3D=3D 0); + /* Validate that our pre-allocated pointer was not replaced */ + g_assert(result =3D=3D origresult); + + exp_output =3D data->hex_digest; + for (j =3D 0; j < resultlen; j++) { + g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); + g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); + } =20 - qcrypto_hmac_free(hmac); + qcrypto_hmac_free(hmac); =20 - g_free(result); - } + g_free(result); } =20 -static void test_hmac_iov(void) +static void test_hmac_iov(const void *opaque) { - size_t i; - - for (i =3D 0; i < G_N_ELEMENTS(test_data); i++) { - QCryptoHmacTestData *data =3D &test_data[i]; - QCryptoHmac *hmac =3D NULL; - uint8_t *result =3D NULL; - size_t resultlen =3D 0; - const char *exp_output =3D NULL; - int ret; - size_t j; - struct iovec iov[3] =3D { - { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT= _TEXT1) }, - { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT= _TEXT2) }, - { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT= _TEXT3) }, - }; - - if (!qcrypto_hmac_supports(data->alg)) { - return; - } - - exp_output =3D data->hex_digest; - - hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, - strlen(KEY), &error_fatal); - g_assert(hmac !=3D NULL); - - ret =3D qcrypto_hmac_bytesv(hmac, iov, 3, &result, - &resultlen, &error_fatal); - g_assert(ret =3D=3D 0); - - for (j =3D 0; j < resultlen; j++) { - g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); - g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); - } + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + QCryptoHmacTestData *data =3D &test_data[alg]; + QCryptoHmac *hmac =3D NULL; + uint8_t *result =3D NULL; + size_t resultlen =3D 0; + const char *exp_output =3D NULL; + int ret; + size_t j; + struct iovec iov[3] =3D { + { .iov_base =3D (char *)INPUT_TEXT1, .iov_len =3D strlen(INPUT_TEX= T1) }, + { .iov_base =3D (char *)INPUT_TEXT2, .iov_len =3D strlen(INPUT_TEX= T2) }, + { .iov_base =3D (char *)INPUT_TEXT3, .iov_len =3D strlen(INPUT_TEX= T3) }, + }; + + exp_output =3D data->hex_digest; + + hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, + strlen(KEY), &error_fatal); + g_assert(hmac !=3D NULL); + + ret =3D qcrypto_hmac_bytesv(hmac, iov, 3, &result, + &resultlen, &error_fatal); + g_assert(ret =3D=3D 0); + + for (j =3D 0; j < resultlen; j++) { + g_assert(exp_output[j * 2] =3D=3D hex[(result[j] >> 4) & 0xf]); + g_assert(exp_output[j * 2 + 1] =3D=3D hex[result[j] & 0xf]); + } =20 - qcrypto_hmac_free(hmac); + qcrypto_hmac_free(hmac); =20 - g_free(result); - } + g_free(result); } =20 -static void test_hmac_digest(void) +static void test_hmac_digest(const void *opaque) { - size_t i; + QCryptoHashAlgo alg =3D GPOINTER_TO_INT(opaque); + QCryptoHmacTestData *data =3D &test_data[alg]; + QCryptoHmac *hmac =3D NULL; + uint8_t *result =3D NULL; + const char *exp_output =3D NULL; + int ret; =20 - for (i =3D 0; i < G_N_ELEMENTS(test_data); i++) { - QCryptoHmacTestData *data =3D &test_data[i]; - QCryptoHmac *hmac =3D NULL; - uint8_t *result =3D NULL; - const char *exp_output =3D NULL; - int ret; + exp_output =3D data->hex_digest; =20 - if (!qcrypto_hmac_supports(data->alg)) { - return; - } - - exp_output =3D data->hex_digest; - - hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, - strlen(KEY), &error_fatal); - g_assert(hmac !=3D NULL); + hmac =3D qcrypto_hmac_new(data->alg, (const uint8_t *)KEY, + strlen(KEY), &error_fatal); + g_assert(hmac !=3D NULL); =20 - ret =3D qcrypto_hmac_digest(hmac, (const char *)INPUT_TEXT, - strlen(INPUT_TEXT), (char **)&result, - &error_fatal); - g_assert(ret =3D=3D 0); + ret =3D qcrypto_hmac_digest(hmac, (const char *)INPUT_TEXT, + strlen(INPUT_TEXT), (char **)&result, + &error_fatal); + g_assert(ret =3D=3D 0); =20 - g_assert_cmpstr((const char *)result, =3D=3D, exp_output); + g_assert_cmpstr((const char *)result, =3D=3D, exp_output); =20 - qcrypto_hmac_free(hmac); + qcrypto_hmac_free(hmac); =20 - g_free(result); - } + g_free(result); } =20 int main(int argc, char **argv) { + size_t i; g_test_init(&argc, &argv, NULL); =20 g_assert(qcrypto_init(NULL) =3D=3D 0); =20 - g_test_add_func("/crypto/hmac/iov", test_hmac_iov); - g_test_add_func("/crypto/hmac/alloc", test_hmac_alloc); - g_test_add_func("/crypto/hmac/prealloc", test_hmac_prealloc); - g_test_add_func("/crypto/hmac/digest", test_hmac_digest); +#define ADD_TEST(alg, prefix, meth) \ + { \ + g_autofree char *name =3D g_strdup_printf( \ + "%s/%s", prefix, QCryptoHashAlgo_str(alg)); \ + g_test_add_data_func(name, GINT_TO_POINTER(alg), meth); \ + } + + for (i =3D 0; i < QCRYPTO_HASH_ALGO__MAX ; i++) { + if (!qcrypto_hmac_supports(i)) { + continue; + } + + ADD_TEST(i, "/crypto/hmac/iov", test_hmac_iov); + ADD_TEST(i, "/crypto/hmac/alloc", test_hmac_alloc); + ADD_TEST(i, "/crypto/hmac/prealloc", test_hmac_prealloc); + ADD_TEST(i, "/crypto/hmac/digest", test_hmac_digest); + } =20 return g_test_run(); } --=20 2.56.0