From 2b2eac703dcc3f258274623edcfbab04da0a5a8a Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Sat, 17 Jul 2021 20:15:53 -0700 Subject: [PATCH] [crypto] replace `enum` constants with `constexpr` (#6845) --- src/core/crypto/aes_ccm.hpp | 11 ++++------- src/core/crypto/aes_ecb.hpp | 5 +---- src/core/crypto/ecdsa.hpp | 33 +++++++++++++-------------------- src/core/crypto/pbkdf2_cmac.hpp | 5 +---- src/core/crypto/sha256.hpp | 5 +---- 5 files changed, 20 insertions(+), 39 deletions(-) diff --git a/src/core/crypto/aes_ccm.hpp b/src/core/crypto/aes_ccm.hpp index 67744b379..ec11f4db4 100644 --- a/src/core/crypto/aes_ccm.hpp +++ b/src/core/crypto/aes_ccm.hpp @@ -59,18 +59,15 @@ namespace Crypto { class AesCcm { public: - enum - { - kMinTagLength = 4, ///< Minimum tag length (in bytes). - kMaxTagLength = AesEcb::kBlockSize, ///< Maximum tag length (in bytes). - kNonceSize = 13, ///< Size of IEEE 802.15.4 Nonce (in bytes). - }; + static constexpr uint8_t kMinTagLength = 4; ///< Minimum tag length (in bytes). + static constexpr uint8_t kMaxTagLength = AesEcb::kBlockSize; ///< Maximum tag length (in bytes). + static constexpr uint8_t kNonceSize = 13; ///< Size of IEEE 802.15.4 Nonce (in bytes). /** * This enumeration type represent the encryption vs decryption mode. * */ - enum Mode + enum Mode : uint8_t { kEncrypt, // Encryption mode. kDecrypt, // Decryption mode. diff --git a/src/core/crypto/aes_ecb.hpp b/src/core/crypto/aes_ecb.hpp index 085ccfa50..baed30e7c 100644 --- a/src/core/crypto/aes_ecb.hpp +++ b/src/core/crypto/aes_ecb.hpp @@ -55,10 +55,7 @@ namespace Crypto { class AesEcb { public: - enum - { - kBlockSize = 16, ///< AES-128 block size (bytes). - }; + static constexpr uint8_t kBlockSize = 16; ///< AES-128 block size (bytes). /** * Constructor to initialize the mbedtls_aes_context. diff --git a/src/core/crypto/ecdsa.hpp b/src/core/crypto/ecdsa.hpp index 6cf731311..7111363a4 100644 --- a/src/core/crypto/ecdsa.hpp +++ b/src/core/crypto/ecdsa.hpp @@ -64,15 +64,13 @@ namespace Ecdsa { class P256 { public: - enum : uint16_t - { - kFieldBitLength = 256, ///< Prime field bit length used by the P-256 curve. - }; + static constexpr uint16_t kFieldBitLength = 256; ///< Prime field bit length used by the P-256 curve. - enum : uint8_t - { - kMpiSize = kFieldBitLength / 8, ///< Max bytes in binary representation of an MPI (multi-precision int). - }; + /** + * Max bytes in binary representation of an MPI (multi-precision int). + * + */ + static constexpr uint8_t kMpiSize = kFieldBitLength / 8; class PublicKey; class KeyPair; @@ -91,10 +89,7 @@ public: friend class PublicKey; public: - enum : uint8_t - { - kSize = 2 * kMpiSize, ///< Size of the signature in bytes (two times the curve MPI size). - }; + static constexpr uint8_t kSize = 2 * kMpiSize; ///< Signature size in bytes (two times the curve MPI size). /** * This method returns the signature as a byte array. @@ -128,10 +123,11 @@ public: class KeyPair { public: - enum : uint8_t - { - kMaxDerSize = 125, ///< Max buffer size (in bytes) for representing the key-pair in DER format. - }; + /** + * Max buffer size (in bytes) for representing the key-pair in DER format. + * + */ + static constexpr uint8_t kMaxDerSize = 125; /** * This constructor initializes a `KeyPair` as empty (no key). @@ -237,10 +233,7 @@ public: friend class KeyPair; public: - enum - { - kSize = kMpiSize * 2, ///< Size of the public key in bytes. - }; + static constexpr uint8_t kSize = kMpiSize * 2; ///< Size of the public key in bytes. /** * This method gets the pointer to the buffer containing the public key (as an uncompressed curve point). diff --git a/src/core/crypto/pbkdf2_cmac.hpp b/src/core/crypto/pbkdf2_cmac.hpp index 7c8559c2b..d98e33d2a 100644 --- a/src/core/crypto/pbkdf2_cmac.hpp +++ b/src/core/crypto/pbkdf2_cmac.hpp @@ -50,10 +50,7 @@ namespace Pbkdf2 { * */ -enum : uint16_t -{ - kMaxSaltLength = 30, ///< Max SALT length: salt prefix (6) + extended panid (8) + network name (16) -}; +constexpr uint16_t kMaxSaltLength = 30; ///< Max SALT length: salt prefix (6) + extended panid (8) + network name (16) /** * This function performs PKCS#5 PBKDF2 using CMAC (AES-CMAC-PRF-128). diff --git a/src/core/crypto/sha256.hpp b/src/core/crypto/sha256.hpp index 627a5f53f..024fa452d 100644 --- a/src/core/crypto/sha256.hpp +++ b/src/core/crypto/sha256.hpp @@ -73,10 +73,7 @@ public: class Hash : public otCryptoSha256Hash, public Clearable, public Equatable { public: - enum : uint8_t - { - kSize = OT_CRYPTO_SHA256_HASH_SIZE, ///< SHA-256 hash size (bytes) - }; + static const uint8_t kSize = OT_CRYPTO_SHA256_HASH_SIZE; ///< SHA-256 hash size (bytes) /** * This method returns a pointer to a byte array containing the hash value.