ot::Crypto::Ecdsa::P256 class
Implements ECDSA key-generation, signing, and verification for NIST P-256 curve using SHA-256 hash. NIST P-256 curve is also known as 256-bit Random ECP Group (RFC 5114 - 2.6), or secp256r1 (RFC 4492 - Appendix A).
Syntax
class P256
{
public:
static constexpr uint16_t kFieldBitLength = 256;
static constexpr uint8_t kMpiSize = kFieldBitLength / 8;
class PublicKey;
class KeyPair;
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
class KeyPairAsRef;
#endif
OT_TOOL_PACKED_BEGIN
class Signature : public otPlatCryptoEcdsaSignature
{
friend class KeyPair;
friend class PublicKey;
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
friend class KeyPairAsRef;
#endif
public:
static constexpr uint8_t kSize = OT_CRYPTO_ECDSA_SIGNATURE_SIZE;
const uint8_t *GetBytes(void) const { return m8; }
} OT_TOOL_PACKED_END;
class KeyPair : public otPlatCryptoEcdsaKeyPair
{
public:
static constexpr uint8_t kMaxDerSize = OT_CRYPTO_ECDSA_MAX_DER_SIZE;
KeyPair(void) { mDerLength = 0; }
Error Generate(void) { return otPlatCryptoEcdsaGenerateKey(this); }
Error GetPublicKey(PublicKey &aPublicKey) const { return otPlatCryptoEcdsaGetPublicKey(this, &aPublicKey); }
const uint8_t *GetDerBytes(void) const { return mDerBytes; }
uint8_t GetDerLength(void) const { return mDerLength; }
uint8_t *GetDerBytes(void) { return mDerBytes; }
void SetDerLength(uint8_t aDerLength) { mDerLength = aDerLength; }
Error Sign(const Sha256::Hash &aHash, Signature &aSignature) const
{
return otPlatCryptoEcdsaSign(this, &aHash, &aSignature);
}
};
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
class KeyPairAsRef
{
public:
explicit KeyPairAsRef(otCryptoKeyRef aKeyRef = 0) { mKeyRef = aKeyRef; }
Error Generate(void) const { return otPlatCryptoEcdsaGenerateAndImportKey(mKeyRef); }
Error ImportKeyPair(const KeyPair &aKeyPair)
{
return Crypto::Storage::ImportKey(mKeyRef, Storage::kKeyTypeEcdsa, Storage::kKeyAlgorithmEcdsa,
(Storage::kUsageSignHash | Storage::kUsageVerifyHash),
Storage::kTypePersistent, aKeyPair.GetDerBytes(),
aKeyPair.GetDerLength());
}
Error GetPublicKey(PublicKey &aPublicKey) const
{
return otPlatCryptoEcdsaExportPublicKey(mKeyRef, &aPublicKey);
}
Error Sign(const Sha256::Hash &aHash, Signature &aSignature) const
{
return otPlatCryptoEcdsaSignUsingKeyRef(mKeyRef, &aHash, &aSignature);
}
otCryptoKeyRef GetKeyRef(void) const { return mKeyRef; }
void SetKeyRef(otCryptoKeyRef aKeyRef) { mKeyRef = aKeyRef; }
private:
otCryptoKeyRef mKeyRef;
};
#endif
OT_TOOL_PACKED_BEGIN
class PublicKey : public otPlatCryptoEcdsaPublicKey, public Equatable<PublicKey>
{
friend class KeyPair;
#if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
friend class KeyPairAsRef;
#endif
public:
static constexpr uint8_t kSize = OT_CRYPTO_ECDSA_PUBLIC_KEY_SIZE;
const uint8_t *GetBytes(void) const { return m8; }
Error Verify(const Sha256::Hash &aHash, const Signature &aSignature) const
{
return otPlatCryptoEcdsaVerify(this, &aHash, &aSignature);
}
} OT_TOOL_PACKED_END;
};
Fields
Max bytes in binary representation of an MPI (multi-precision int).
![]()
class P256