pub fn key_type_matches_length(
key_type: KeyType,
length: Option<u32>,
) -> Result<(), Error>
Expand description
Ensures that a KeyType
is compatible with an optional key length
§Errors
Returns an Error::Key
if
key_type
is one ofKeyType::Curve25519
,KeyType::EcP224
,KeyType::EcP256
,KeyType::EcP384
orKeyType::EcP521
andlength
isSome
.key_type
isKeyType::Generic
orKeyType::Rsa
andlength
isNone
.key_type
isKeyType::Generic
andlength
is notSome
value of128
,192
or256
.key_type
isKeyType::Rsa
andlength
is notSome
value equal to or greater thanMIN_RSA_BIT_LENGTH
.
§Examples
use nethsm::{key_type_matches_length, KeyType};
key_type_matches_length(KeyType::Curve25519, None)?;
key_type_matches_length(KeyType::EcP224, None)?;
key_type_matches_length(KeyType::Rsa, Some(2048))?;
key_type_matches_length(KeyType::Generic, Some(256))?;
// this fails because elliptic curve keys have their length set intrinsically
assert!(key_type_matches_length(KeyType::Curve25519, Some(2048)).is_err());
// this fails because a bit length of 2048 is not defined for AES block ciphers
assert!(key_type_matches_length(KeyType::Generic, Some(2048)).is_err());
// this fails because a bit length of 1024 is unsafe to use for RSA keys
assert!(key_type_matches_length(KeyType::Rsa, Some(1024)).is_err());