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 222F0C352A1 for ; Wed, 7 Dec 2022 19:33:09 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AD2068363D; Wed, 7 Dec 2022 20:33:06 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1670441586; bh=GfF425F6FqfW7XSZ7B9fk949aZFTm5VcQYOAjtsUndM=; h=Date:Subject:To:Cc:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=jaVLo7eX1/8K9UwD8X0v1b0TlFrvZRFrswBo8fHhfTw3AtywNiPUC/UWadHsy2d1e 7mah4rlhisjJHc5j+0LVApNzey8lLjpCm5s6GcFu+ZI+QWwaoJ0WcLMfC3MjXoa0jM lM18bVgsJ9jBzxppkyToRYnTjKv+oclEa7xBP2NS5oBMA27F38NKyxhnHMbVqd4Vb4 QCpVuR99khbTwKRZ6PMzKC8Gdk4BWWLKGR0cGXpB4U/pfGFaXbr77MtbY/QoSS3vxa +dvuIp6ErV55kOjiLegjjMmV+KjX0S6L2wNwKiH9N0cDrUy0Wd9ToGmGiWFPk3cL47 D1ogMQQlO2KGA== Received: from [127.0.0.1] (p578adb1c.dip0.t-ipconnect.de [87.138.219.28]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: marex@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id 310A981A10; Wed, 7 Dec 2022 20:32:59 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1670441584; bh=GfF425F6FqfW7XSZ7B9fk949aZFTm5VcQYOAjtsUndM=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=WXo/IeFWODP5UudU3DPtTGVw2H4F8/dfSxJdWtGbSeJR3BcvQSz+of/P4mYBay19f mR0q7L+LgsSloKjcQ/t2SqjnnZcWVolEabXd6t5XSdeK1HhnZNwSaZGMWL7vJDABW8 28Jw2RA8YuCR/xnYSyD2KlYGtdVZw0UJq8MjFCGDNY0gj+cpg+NzFwS44KKQNxk/3r +qkMTvvnshAIkllIUxmvPdw4cBBZvbe6ZAo6cdqAqJTO+LmjlpnJ/IL5wK9FKhmLSo 8oz78yY3xs9rTjHGOhZuZ4s9vVZViBAxKMmzIJDiaY/fgWbgVmPHFIKNxovFCZscU2 oubU3YcqkE+Gg== Message-ID: <361145ae-c2fa-4f9e-cfcd-2357b204a8cf@denx.de> Date: Wed, 7 Dec 2022 20:32:58 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: Re: [PATCH v2 1/4] ARM: stm32: Fix ECDSA authentication with Dcache enabled Content-Language: en-US To: Patrick DELAUNAY , u-boot@lists.denx.de Cc: Alexandru Gagniuc , Patrice Chotard References: <20221206224929.33015-1-marex@denx.de> <2ce07612-9539-46f2-9540-55adcbdcabe1@foss.st.com> From: Marek Vasut In-Reply-To: <2ce07612-9539-46f2-9540-55adcbdcabe1@foss.st.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit 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.6 at phobos.denx.de X-Virus-Status: Clean On 12/7/22 11:08, Patrick DELAUNAY wrote: > Hi Marek, Hello Patrick, > Sorry for the delay. No worries. > I cross-check with ROM code team to understood this API limitation. Thank you! > On 12/6/22 23:49, Marek Vasut wrote: >> In case Dcache is enabled while the ECDSA authentication function is >> called via BootROM ROM API, the CRYP DMA might pick stale version of >> data from DRAM. Disable Dcache around the BootROM call to avoid this >> issue. >> >> Signed-off-by: Marek Vasut >> --- >> Cc: Alexandru Gagniuc >> Cc: Patrice Chotard >> Cc: Patrick Delaunay >> --- >> V2: - Initialize reenable_dcache variable >> --- >>   arch/arm/mach-stm32mp/ecdsa_romapi.c | 14 ++++++++++++++ >>   1 file changed, 14 insertions(+) >> >> diff --git a/arch/arm/mach-stm32mp/ecdsa_romapi.c >> b/arch/arm/mach-stm32mp/ecdsa_romapi.c >> index a2f63ff879f..082178ce83f 100644 >> --- a/arch/arm/mach-stm32mp/ecdsa_romapi.c >> +++ b/arch/arm/mach-stm32mp/ecdsa_romapi.c >> @@ -63,6 +63,7 @@ static int romapi_ecdsa_verify(struct udevice *dev, >>                      const void *hash, size_t hash_len, >>                      const void *signature, size_t sig_len) >>   { >> +    bool reenable_dcache = false; >>       struct ecdsa_rom_api rom; >>       uint8_t raw_key[64]; >>       uint32_t rom_ret; >> @@ -81,8 +82,21 @@ static int romapi_ecdsa_verify(struct udevice *dev, >>       memcpy(raw_key + 32, pubkey->y, 32); >>       stm32mp_rom_get_ecdsa_functions(&rom); >> + >> +    /* >> +     * Disable D-cache before calling into BootROM, else CRYP DMA >> +     * may fail to pick up the correct data. >> +     */ >> +    if (dcache_status()) { >> +        dcache_disable(); >> +        reenable_dcache = true; >> +    } >> + >>       rom_ret = rom.ecdsa_verify_signature(hash, raw_key, signature, >> algo); >> +    if (reenable_dcache) >> +        dcache_enable(); >> + >>       return rom_ret == ROM_API_SUCCESS ? 0 : -EPERM; >>   } > > > In fact, the ecdsa_verify_signature() don't use the HW (no DMA and no > use of CRYP IP ) Hmmm, what does the BootROM use CRYP for then ? It is necessary to have MP15xC/F for the authenticated boot to work, but it seems the only difference there is the presence of CRYP. Or is there some BootROM fuse too ? > It is only a SW library, integrated in ROM code and exported to avoid > the need > > to include the same library in FSBL = TF-A, with size limitation (SYSRAM). > > > This library don't need to deactivate the data cache, the only impact of > this deactivation it > > is to reduce the execution performance.... > > > After cross-check, I think the only problem today it the U-Boot MMU > configuration of STM32MP15x > > plaform: by default only the DDR is marked executable in U-Boot, all the > other region are > > defined as DEVICE memory/not executable (DCACHE_OFF in mmu_setup). > > > Deactivate the data cache only avoids the exception which occurs on jump > to NotExecutable region > > because in U-Boot "dcache OFF" imply  "MMU off"  (see cache_enable in > ./arch/arm/lib/cache-cp15.c) > > and with MMU deactivated the check on executable MMU tag is also > deactivated. > > > I think the next patch is enough: > > > #define STM32MP_ROM_BASE        U(0x00000000) > > > static int romapi_ecdsa_verify(struct udevice *dev, >                     const void *hash, size_t hash_len, >                     const void *signature, size_t sig_len) >  { >      struct ecdsa_rom_api rom; >      uint8_t raw_key[64]; >      uint32_t rom_ret; > @@ -81,8 +82,21 @@ static int romapi_ecdsa_verify(struct udevice *dev, >      memcpy(raw_key + 32, pubkey->y, 32); > >      stm32mp_rom_get_ecdsa_functions(&rom); > + > +    /* mark executable the exported ROM code function: */ > +    mmu_set_region_dcache_behaviour(STM32MP_ROM_BASE, MMU_SECTION_SIZE, > DCACHE_DEFAULT_OPTION); > + >      rom_ret = rom.ecdsa_verify_signature(hash, raw_key, signature, algo); > >      return rom_ret == ROM_API_SUCCESS ? 0 : -EPERM; >  } This indeed works, tested and sent V3. > Sorry again for the first review, not complete... Thank you for checking !