Skip to content

Commit ca5812c

Browse files
committed
refactor: Enhance encryption handling by defining constants for AES block length and HKDF salt length, and update decryption methods to use these constants.
1 parent cdd800d commit ca5812c

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Fido2/AuthenticatorInfo.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ public class AuthenticatorInfo
8080
/// </summary>
8181
public const int DefaultMinimumPinLength = 4;
8282

83+
/// <summary>
84+
/// The length of the IV and ciphertext blocks used in AES-128-CBC encryption for encrypted fields.
85+
/// </summary>
86+
private const int AesBlockLength = 16;
87+
88+
/// <summary>
89+
/// The length of the salt used in HKDF key derivation for encrypted fields.
90+
/// </summary>
91+
private const int HkdfSaltLength = 32;
92+
93+
/// <summary>
94+
/// The HKDF info string used to derive the decryption key for the encrypted identifier.
95+
/// </summary>
96+
private static ReadOnlySpan<byte> HkdfInfoEncIdentifier => "encIdentifier"u8;
97+
98+
/// <summary>
99+
/// The HKDF info string used to derive the decryption key for the encrypted credential store state.
100+
/// </summary>
101+
private static ReadOnlySpan<byte> HkdfInfoEncCredStoreState => "encCredStoreState"u8;
102+
83103
/// <summary>
84104
/// The string in the <see cref="Versions"/> property that indicates
85105
/// FIDO U2F.
@@ -598,7 +618,7 @@ List<PinUvAuthProtocol> ParsePinUvAuthProtocols(CborMap<int> cborMap)
598618
/// The decrypted identifier as a read-only memory block of bytes, or null if the encrypted identifier is not set.
599619
/// </returns>
600620
public ReadOnlyMemory<byte>? GetIdentifier(ReadOnlyMemory<byte> persistentUvAuthToken) =>
601-
DecryptEncryptedField(EncIdentifier, persistentUvAuthToken, "encIdentifier"u8);
621+
DecryptEncryptedField(EncIdentifier, persistentUvAuthToken, HkdfInfoEncIdentifier);
602622

603623
/// <summary>
604624
/// Retrieves the credential store state derived from the encrypted credential store state, using the provided persistent UV authentication token.
@@ -610,7 +630,7 @@ List<PinUvAuthProtocol> ParsePinUvAuthProtocols(CborMap<int> cborMap)
610630
/// The decrypted credential store state as a read-only memory block of bytes, or null if the encrypted credential store state is not set.
611631
/// </returns>
612632
public ReadOnlyMemory<byte>? GetCredStoreState(ReadOnlyMemory<byte> persistentPinUvAuthToken) =>
613-
DecryptEncryptedField(EncCredStoreState, persistentPinUvAuthToken, "encCredStoreState"u8);
633+
DecryptEncryptedField(EncCredStoreState, persistentPinUvAuthToken, HkdfInfoEncCredStoreState);
614634

615635
/// <summary>
616636
/// Decrypts an encrypted field using the provided persistent PIN/UV authentication token.
@@ -650,13 +670,13 @@ List<PinUvAuthProtocol> ParsePinUvAuthProtocols(CborMap<int> cborMap)
650670
return null;
651671
}
652672

653-
Span<byte> iv = stackalloc byte[16];
654-
Span<byte> ct = stackalloc byte[16];
655-
Span<byte> salt = stackalloc byte[32];
656-
encryptedData.Value.Span[..16].CopyTo(iv);
657-
encryptedData.Value.Span[16..].CopyTo(ct);
673+
Span<byte> iv = stackalloc byte[AesBlockLength];
674+
Span<byte> ct = stackalloc byte[AesBlockLength];
675+
Span<byte> salt = stackalloc byte[HkdfSaltLength];
676+
encryptedData.Value.Span[..AesBlockLength].CopyTo(iv);
677+
encryptedData.Value.Span[AesBlockLength..].CopyTo(ct);
658678

659-
var key = HkdfUtilities.DeriveKey(persistentPinUvAuthToken.Span, salt, hkdfInfo, 16);
679+
var key = HkdfUtilities.DeriveKey(persistentPinUvAuthToken.Span, salt, hkdfInfo, AesBlockLength);
660680
var decrypted = AesUtilities.AesCbcDecrypt(key.Span, iv, ct);
661681
CryptographicOperations.ZeroMemory(key.Span);
662682

0 commit comments

Comments
 (0)