From patchwork Fri Jul 17 07:16:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 235676 Delivered-To: patch@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp1457600ilg; Fri, 17 Jul 2020 00:17:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwaP4DmVGDnEbYf4iLJxGEoKHRBUmPGQPTzoSe6OCr/EKyFnrEnO7EJk/02VVdfMwmIqM08 X-Received: by 2002:a17:906:45a:: with SMTP id e26mr6355264eja.67.1594970229617; Fri, 17 Jul 2020 00:17:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594970229; cv=none; d=google.com; s=arc-20160816; b=i7wTdv1WNmbYYkvyVqhRctC67zfbm00l4vwlNAbOBcobEUIbyaA2Toqc+xx9qtPGRr XnsH1CEHDPCKvT26fifMv+qKEQrVj/umOTn6UBNOH3UuqPplgHN+gr8M3im9eZV5gp73 2tGR2SH3lcRflfp9GWbjwmg7kt4XJD6hxpQ2p1+fAnRgP8YUd2DZzhUTgvcnWFVKfY70 hRYyDDb79Kp4NtsD4wyJKmmL5kFfoawFkCBR2+isyeJxGSjDEIpMRtRs9OI4zGNsEXGv Jyphm815mgT5NXfJblwaMQw87huweBFaK8b0drcqii3/np9yGV1Bl9bJXtCjxvjk8VKu 41Sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=jyTmnoyp4eBJ9ueA2Yq5kQvlAa18P2UeNaZXgFIe+k8=; b=cK7mouZ7jly/Dx+1fTt8l8Ad+NqYSrHqi+mZSNaqq0xK5+rqAdaOHU6Vxz/vYOccqy wBdY2ifjk6tPLd+QRcEjHR18TBZrXngbiJcpG9nn+v1E/qTZwtKJlFaObMrxTzuuQIcw KlCexDI3A0jsFbX56eFi0iadegQW+x/UTVwYS//g+VO7dYwa0XHODbBAkBF7hBR27ynr h8EHbqRmFbpIdnKvHcP8/8DTqpHqBluMC7mjSJhEp1jqe/SnD5BwunrFpLG8XLDu+u6g zEALmoNd19hB8Aw7NspgR5+drGD83X+tdp+3RTJzn2EfkHPwmrUp+Hcksa6PA/22UktN AUSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rezP3sXS; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id w24si4474383ejk.712.2020.07.17.00.17.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:17:09 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rezP3sXS; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 89A7B81C35; Fri, 17 Jul 2020 09:17:05 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="rezP3sXS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3DFBB81C2D; Fri, 17 Jul 2020 09:17:02 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 526CE81C29 for ; Fri, 17 Jul 2020 09:16:57 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=takahiro.akashi@linaro.org Received: by mail-pj1-x1036.google.com with SMTP id a9so459982pjd.3 for ; Fri, 17 Jul 2020 00:16:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jyTmnoyp4eBJ9ueA2Yq5kQvlAa18P2UeNaZXgFIe+k8=; b=rezP3sXSBOIABgL9JT5PnJ0wAyBu/qLtoAVzq9NEbADe0bfa+KhVMX1U8MF3Kt6DXw OhmB3Te8PXUzRL6GK6HJdDOEson+wUSdj/ADyiY3+fOv7GoII5Ey5FlnS6s302CG+vI5 Y89AREX+FibgoMRSn8LghJrl4hathP5iJFoYd31E5ML+ZwGeDIrN8k5MApdSUIhVuwXY 1CW93NLuUES6rCBeNenSuS1yvfVznVWUI5JzNpTe9KZ3R8wWASk6bFA5TudtDk/6ekYl zCzwIHDMsNmnmyS/+WdT3ZALeMZwzg9kJVK+bhjgYVRMih4gFPUtyRS6F+VuAko7o6iM A1xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jyTmnoyp4eBJ9ueA2Yq5kQvlAa18P2UeNaZXgFIe+k8=; b=U0pFNieV+qYMYahBGNz7nYMlJW+/HvV5CsBqEispqoV3QQGw5iLsIpexW7vL18Zh1Q aw/0N343z9O9ccJMoOMaPMydiTOIyOVSMv5lwDgaMW50Xtyh6dQmTkP6uJXvs5Z8YDty gMx738LMN383Z7kXe6Z9bREbXSPRu0qG7ZSnRqHjPGQJYeYQRp5W8ODH43lyzeGofyU9 iui5x2H/1dXvJTlkGru93YhA4t6C+gboXFq7EkNmvMEE0mKDx5jXWU172aifOg29B1IV GGdjmyNVAE73z2dpuBah4xqDACLHhcFT/vr1wt29SUpIImMhe4JwGuQd79GNhYx0OUoN g0+Q== X-Gm-Message-State: AOAM531tNA74RQ+2nmC0QN3PpHKEHEAVl07DJy9DaaeXNfnGZ4ExZenl uqlR+f1o6a+IN92jUqoQWLGZzg== X-Received: by 2002:a17:902:d20c:: with SMTP id t12mr6631621ply.291.1594970215840; Fri, 17 Jul 2020 00:16:55 -0700 (PDT) Received: from localhost.localdomain (p6e424d9a.tkyea130.ap.so-net.ne.jp. [110.66.77.154]) by smtp.gmail.com with ESMTPSA id g12sm6749388pfb.190.2020.07.17.00.16.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2020 00:16:55 -0700 (PDT) From: AKASHI Takahiro To: xypron.glpk@gmx.de, agraf@csgraf.de Cc: sughosh.ganu@linaro.org, mail@patrick-wildt.de, u-boot@lists.denx.de, AKASHI Takahiro Subject: [PATCH v4 1/7] lib: crypto: add public_key_verify_signature() Date: Fri, 17 Jul 2020 16:16:24 +0900 Message-Id: <20200717071630.7363-2-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200717071630.7363-1-takahiro.akashi@linaro.org> References: <20200717071630.7363-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean This function will be called from x509_check_for_self_signed() and pkcs7_verify_one(), which will be imported from linux in a later patch. While it does exist in linux code and has a similar functionality of rsa_verify(), it calls further linux-specific interfaces inside. That could lead to more files being imported from linux. So simply re-implement it here instead of re-using the code. Signed-off-by: AKASHI Takahiro --- include/crypto/public_key.h | 2 +- lib/crypto/public_key.c | 70 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) -- 2.27.0 diff --git a/include/crypto/public_key.h b/include/crypto/public_key.h index 436a1ee1ee64..3ba90fcc3483 100644 --- a/include/crypto/public_key.h +++ b/include/crypto/public_key.h @@ -82,9 +82,9 @@ extern int decrypt_blob(struct kernel_pkey_params *, const void *, void *); extern int create_signature(struct kernel_pkey_params *, const void *, void *); extern int verify_signature(const struct key *, const struct public_key_signature *); +#endif /* __UBOOT__ */ int public_key_verify_signature(const struct public_key *pkey, const struct public_key_signature *sig); -#endif /* !__UBOOT__ */ #endif /* _LINUX_PUBLIC_KEY_H */ diff --git a/lib/crypto/public_key.c b/lib/crypto/public_key.c index e12ebbb3d0c5..71a0e0356beb 100644 --- a/lib/crypto/public_key.c +++ b/lib/crypto/public_key.c @@ -25,7 +25,10 @@ #include #endif #include -#ifndef __UBOOT__ +#ifdef __UBOOT__ +#include +#include +#else #include #endif @@ -80,6 +83,71 @@ void public_key_signature_free(struct public_key_signature *sig) } EXPORT_SYMBOL_GPL(public_key_signature_free); +/** + * public_key_verify_signature - Verify a signature using a public key. + * + * @pkey: Public key + * @sig: Signature + * + * Verify a signature, @sig, using a RSA public key, @pkey. + * + * Return: 0 - verified, non-zero error code - otherwise + */ +int public_key_verify_signature(const struct public_key *pkey, + const struct public_key_signature *sig) +{ + struct image_sign_info info; + struct image_region region; + int ret; + + pr_devel("==>%s()\n", __func__); + + if (!pkey || !sig) + return -EINVAL; + + if (pkey->key_is_private) + return -EINVAL; + + memset(&info, '\0', sizeof(info)); + info.padding = image_get_padding_algo("pkcs-1.5"); + /* + * Note: image_get_[checksum|crypto]_algo takes a string + * argument like "," + * TODO: support other hash algorithms + */ + if (strcmp(sig->pkey_algo, "rsa") || (sig->s_size * 8) != 2048) { + pr_warn("Encryption is not RSA2048: %s%d\n", + sig->pkey_algo, sig->s_size * 8); + return -ENOPKG; + } + if (!strcmp(sig->hash_algo, "sha1")) { + info.checksum = image_get_checksum_algo("sha1,rsa2048"); + info.name = "sha1,rsa2048"; + } else if (!strcmp(sig->hash_algo, "sha256")) { + info.checksum = image_get_checksum_algo("sha256,rsa2048"); + info.name = "sha256,rsa2048"; + } else { + pr_warn("unknown msg digest algo: %s\n", sig->hash_algo); + return -ENOPKG; + } + info.crypto = image_get_crypto_algo(info.name); + if (unlikely(IS_ERR(info.checksum) || IS_ERR(info.crypto))) + return -ENOPKG; + + info.key = pkey->key; + info.keylen = pkey->keylen; + + region.data = sig->digest; + region.size = sig->digest_size; + + if (rsa_verify_with_pkey(&info, sig->digest, sig->s, sig->s_size)) + ret = -EKEYREJECTED; + else + ret = 0; + + pr_devel("<==%s() = %d\n", __func__, ret); + return ret; +} #else /* * Destroy a public key algorithm key.