Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 734E4C433EF for ; Wed, 9 Mar 2022 13:57:42 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 40F57839A9; Wed, 9 Mar 2022 14:57:35 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com 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=gmail.com header.i=@gmail.com header.b="AfjTVGxq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B8F6783991; Wed, 9 Mar 2022 10:28:44 +0100 (CET) Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) (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 94DBD801B2 for ; Wed, 9 Mar 2022 10:28:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=giojahermann@gmail.com Received: by mail-wm1-x331.google.com with SMTP id r65so981032wma.2 for ; Wed, 09 Mar 2022 01:28:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PzFs0Y5Nl3fQY5r7Ae35VVLNTZth7a7nPi8/LFVFsSQ=; b=AfjTVGxqJIRfkikomECbyfoaLAYzWvszn4rSR0RjRexbHa6y/fhpQrPbeUYXt7+v/R ImWf1JpOJv9l1jRTgd18J3699Ncbp0ieGVFjBKG4/R/33zU1yVXs6f1I1YKumWhLEd+B gQmNZwn7OsA2qtsK490PHng7F3+sffRd1eUxOtzCsa6VmuwEWdtZM9KWOFJdjpKl574+ IfBDZZt2kGCbHiIs/NdoZdE25MIpLNgKfI99WwAdS7EXDnihUI1rRM3exmG6D8U9IPBX wWXxeDZBfmLnpviefRYaKYnDZjTCn6v1exJlDOdsvFrKcYtWDKS0oiZH+6Ol1gQaDPiP K5Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PzFs0Y5Nl3fQY5r7Ae35VVLNTZth7a7nPi8/LFVFsSQ=; b=zD+u32Ln+DzkQLyMn+L155cihayZWrtWLu33JAOlKt9T9ZSusDr+X6R28zlimrBoud 5/acQV5MhijzM2r1/f6WBmh8ALPSsnnJczdMfksnZQS1ANe/mGOmI8+Ap2WYKBv8PqM3 CHA62f2njQ9Cj8esgZDy5ISvR/I+uG5dcxWUjyj8H1aCgdg4D+iwh3Z4XDNwQDXJsW5Q 9Oh6FzwJJsMGiZ/Nlb5S5zTR8cwYdpi80EEl4qyi64zRX6DOFo4KJbjDMkUnbXGCp4wj SekcriKje0zrYUdkj7EPSGWZX1n5PqJpivLIkSWmp4ZCIkcwtSQ1wCJ+2iNX+AD5Chn8 79yg== X-Gm-Message-State: AOAM533aX8iPgI57jJUprCOGJ3zCUaKcvqvwC8rG4J81iLK7HfWvVghK dwwd+rNWcmI3y6GjQw7qb2YWpYJsMNhChw== X-Google-Smtp-Source: ABdhPJy3csC9nh6uMbUV3/g6ahjkggQaNjcdSemoZ7ssyuNK9jYO73RlY80VWU0K60iPD4w/xU+gCA== X-Received: by 2002:a7b:c841:0:b0:389:864c:e715 with SMTP id c1-20020a7bc841000000b00389864ce715mr2566318wml.72.1646818118171; Wed, 09 Mar 2022 01:28:38 -0800 (PST) Received: from localhost.localdomain ([185.228.230.0]) by smtp.gmail.com with ESMTPSA id v13-20020adfe28d000000b0020375f27a5asm1221771wri.4.2022.03.09.01.28.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Mar 2022 01:28:37 -0800 (PST) From: SESA644425 X-Google-Original-From: SESA644425 To: sjg@chromium.org Cc: gioja.hermann@non.se.com, mr.nuke.me@gmail.com, jamin_lin@aspeedtech.com, xypron.glpk@gmx.de, philippe.reynes@softathome.com, thomas.perrot@bootlin.com, u-boot@lists.denx.de, giojahermann@gmail.com Subject: [PATCH 2/3] lib: rsa: Leverage existing data buffer instead of systematic copy Date: Wed, 9 Mar 2022 01:27:16 -0800 Message-Id: <20220309092717.4203-3-gioja.hermann@non.se.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220309092717.4203-1-gioja.hermann@non.se.com> References: <20220309092717.4203-1-gioja.hermann@non.se.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Wed, 09 Mar 2022 14:57:21 +0100 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.103.5 at phobos.denx.de X-Virus-Status: Clean Prior to introduction of modifications in rsassa_pss functions related to padding verification, doing a pass to reduce memory consumption of function by replacing memory copies of parts of const buffer by pointers to the original buffer (masked_db and h are subparts of msg buffer which is declared const, salt is a subpart of db which is a working buffer, unmodified after being filled). New pointers scope is limited to the function where they are declared (not returned to caller by any mean), zeroing risk of memory fault related to the change. Signed-off-by: SESA644425 --- Despite checkpath.pl recommendation, it is not possible to use u8 instead of uint8_t. Proceeding with u8 breaks build with error: unknown type name u8 lib/rsa/rsa-verify.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/rsa/rsa-verify.c b/lib/rsa/rsa-verify.c index c2c7248f90..de71234cfb 100644 --- a/lib/rsa/rsa-verify.c +++ b/lib/rsa/rsa-verify.c @@ -208,6 +208,10 @@ out: * saltlen:-1 "set the salt length to the digest length" is currently * not supported. * + * msg is a concatenation of : masked_db + h + 0xbc + * Once unmasked, db is a concatenation of : [0x00]* + 0x01 + salt + * Length of 0-padding at begin of db depends on salt length. + * * @info: Specifies key and FIT information * @msg: byte array of message, len equal to msg_len * @msg_len: Message length @@ -218,27 +222,25 @@ int padding_pss_verify(struct image_sign_info *info, const uint8_t *msg, int msg_len, const uint8_t *hash, int hash_len) { - uint8_t *masked_db = NULL; - int masked_db_len = msg_len - hash_len - 1; - uint8_t *h = NULL, *hprime = NULL; - int h_len = hash_len; + const uint8_t *masked_db = NULL; uint8_t *db_mask = NULL; - int db_mask_len = masked_db_len; - uint8_t *db = NULL, *salt = NULL; - int db_len = masked_db_len, salt_len = msg_len - hash_len - 2; + uint8_t *db = NULL; + int db_len = msg_len - hash_len - 1; + const uint8_t *h = NULL; + uint8_t *hprime = NULL; + int h_len = hash_len; + uint8_t *salt = NULL; + int salt_len = msg_len - hash_len - 2; uint8_t pad_zero[8] = { 0 }; int ret, i, leftmost_bits = 1; uint8_t leftmost_mask; struct checksum_algo *checksum = info->checksum; /* first, allocate everything */ - masked_db = malloc(masked_db_len); - h = malloc(h_len); - db_mask = malloc(db_mask_len); + db_mask = malloc(db_len); db = malloc(db_len); - salt = malloc(salt_len); hprime = malloc(hash_len); - if (!masked_db || !h || !db_mask || !db || !salt || !hprime) { + if (!db_mask || !db || !hprime) { printf("%s: can't allocate some buffer\n", __func__); ret = -ENOMEM; goto out; @@ -252,8 +254,8 @@ int padding_pss_verify(struct image_sign_info *info, } /* step 5 */ - memcpy(masked_db, msg, masked_db_len); - memcpy(h, msg + masked_db_len, h_len); + masked_db = &msg[0]; + h = &msg[db_len]; /* step 6 */ leftmost_mask = (0xff >> (8 - leftmost_bits)) << (8 - leftmost_bits); @@ -265,7 +267,7 @@ int padding_pss_verify(struct image_sign_info *info, } /* step 7 */ - mask_generation_function1(checksum, h, h_len, db_mask, db_mask_len); + mask_generation_function1(checksum, h, h_len, db_mask, db_len); /* step 8 */ for (i = 0; i < db_len; i++) @@ -283,7 +285,7 @@ int padding_pss_verify(struct image_sign_info *info, } /* step 11 */ - memcpy(salt, &db[1], salt_len); + salt = &db[1]; /* step 12 & 13 */ compute_hash_prime(checksum, pad_zero, 8, @@ -295,11 +297,8 @@ int padding_pss_verify(struct image_sign_info *info, out: free(hprime); - free(salt); free(db); free(db_mask); - free(h); - free(masked_db); return ret; } -- 2.25.1