From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by mx.groups.io with SMTP id smtpd.web12.12776.1656515283539439382 for ; Wed, 29 Jun 2022 08:08:03 -0700 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: arm.com, ip: 217.140.110.172, mailfrom: pierre.gondois@arm.com) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 689861764; Wed, 29 Jun 2022 08:08:03 -0700 (PDT) Received: from pierre123.arm.com (unknown [10.57.42.208]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 5197D3F93E; Wed, 29 Jun 2022 08:08:01 -0700 (PDT) From: "PierreGondois" To: devel@edk2.groups.io Cc: Sami Mujawar , Leif Lindholm , Ard Biesheuvel , Rebecca Cran , Michael D Kinney , Liming Gao , Edward Pickup Subject: [PATCH v1 5/7] MdePkg/AesLib: Definition for AES library class interface Date: Wed, 29 Jun 2022 17:07:11 +0200 Message-Id: <20220629150713.2600465-6-Pierre.Gondois@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220629150713.2600465-1-Pierre.Gondois@arm.com> References: <20220629150713.2600465-1-Pierre.Gondois@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pierre Gondois BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3970 The FIPS PUB 197: "Advanced Encryption Standard (AES)" details the AES algorithm. Add a library to allow different architecture specific implementations. Signed-off-by: Pierre Gondois --- MdePkg/Include/Library/AesLib.h | 104 ++++++++++++++++++++++++++++++++ MdePkg/MdePkg.dec | 4 ++ 2 files changed, 108 insertions(+) create mode 100644 MdePkg/Include/Library/AesLib.h diff --git a/MdePkg/Include/Library/AesLib.h b/MdePkg/Include/Library/Aes= Lib.h new file mode 100644 index 000000000000..bc3408bb249b --- /dev/null +++ b/MdePkg/Include/Library/AesLib.h @@ -0,0 +1,104 @@ +/** @file + AES library. + + Copyright (c) 2022, Arm Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + @par Reference(s): + - FIPS 197 November 26, 2001: + Specification for the ADVANCED ENCRYPTION STANDARD (AES) +**/ + +#ifndef AES_LIB_H_ +#define AES_LIB_H_ + +/// Key size in bytes. +#define AES_KEY_SIZE_128 16 +#define AES_KEY_SIZE_192 24 +#define AES_KEY_SIZE_256 32 +#define AES_BLOCK_SIZE 16 + +/* + The Key Expansion generates a total of Nb (Nr + 1) words with: + - Nb =3D 4: + Number of columns (32-bit words) comprising the State + - Nr =3D 10, 12, or 14: + Number of rounds. + */ +#define AES_MAX_KEYLENGTH_U32 (4 * (14 + 1)) + +/** A context holding information to for AES encryption/decryption. + */ +typedef struct { + /// Expanded encryption key. + UINT32 ExpEncKey[AES_MAX_KEYLENGTH_U32]; + /// Expanded decryption key. + UINT32 ExpDecKey[AES_MAX_KEYLENGTH_U32]; + /// Key size, in bytes. + /// Must be one of 16|24|32. + UINT32 KeySize; +} AES_CTX; + +/** Encrypt an AES block. + + Buffers are little-endian. Overlapping is not checked. + + @param [in] AesCtx AES context. + AesCtx is initialized with AesInitCtx (). + @param [in] InBlock Input Block. The block to cipher. + @param [out] OutBlock Output Block. The ciphered block. + + @retval RETURN_SUCCESS Success. + @retval RETURN_INVALID_PARAMETER Invalid parameter. + @retval RETURN_UNSUPPORTED Unsupported. +**/ +RETURN_STATUS +EFIAPI +AesEncrypt ( + IN AES_CTX *AesCtx, + IN UINT8 CONST *InBlock, + OUT UINT8 *OutBlock + ); + +/** Decrypt an AES block. + + Buffers are little-endian. Overlapping is not checked. + + @param [in] AesCtx AES context. + AesCtx is initialized with AesInitCtx (). + @param [in] InBlock Input Block. The block to de-cipher. + @param [out] OutBlock Output Block. The de-ciphered block. + + @retval RETURN_SUCCESS Success. + @retval RETURN_INVALID_PARAMETER Invalid parameter. + @retval RETURN_UNSUPPORTED Unsupported. +**/ +RETURN_STATUS +EFIAPI +AesDecrypt ( + IN AES_CTX *AesCtx, + IN UINT8 CONST *InBlock, + OUT UINT8 *OutBlock + ); + +/** Initialize an AES_CTX structure. + + @param [in] Key AES key. Buffer of KeySize bytes. + The buffer is little endian. + @param [in] KeySize Size of the key. Must be one of 128|192|25= 6. + @param [in, out] AesCtx AES context to initialize. + + @retval RETURN_SUCCESS Success. + @retval RETURN_INVALID_PARAMETER Invalid parameter. + @retval RETURN_UNSUPPORTED Unsupported. +**/ +RETURN_STATUS +EFIAPI +AesInitCtx ( + IN UINT8 *Key, + IN UINT32 KeySize, + IN OUT AES_CTX *AesCtx + ); + +#endif // AES_LIB_H_ diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index 7ff26e22f915..078ae9323ba6 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -280,6 +280,10 @@ [LibraryClasses] # TrngLib|Include/Library/TrngLib.h =20 + ## @libraryclass Provides AES encryption/decryption services. + # + AesLib|Include/Library/AesLib.h + [LibraryClasses.IA32, LibraryClasses.X64, LibraryClasses.AARCH64] ## @libraryclass Provides services to generate random number. # --=20 2.25.1