Skip to content

Commit

Permalink
misc: edits to StoreDataCommand and ChannelEncryption
Browse files Browse the repository at this point in the history
ChannelEncyrption: use Span<byte> instead of byte[]
StoreDataCommand: change order of fields
  • Loading branch information
DennisDyallo committed Nov 27, 2024
1 parent d77bc6a commit 75a40ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ namespace Yubico.YubiKey.Scp.Commands
/// </remarks>
internal class StoreDataCommand : IYubiKeyCommand<StoreDataCommandResponse>
{
public YubiKeyApplication Application => YubiKeyApplication.SecurityDomain;
private const byte GpStoreDataIns = 0xE2;
private readonly ReadOnlyMemory<byte> _data;

public YubiKeyApplication Application => YubiKeyApplication.SecurityDomain;

/// <summary>
/// Initializes a new instance of the <see cref="StoreDataCommand"/> class, with the given data to be stored.
/// </summary>
Expand Down
37 changes: 21 additions & 16 deletions Yubico.YubiKey/src/Yubico/YubiKey/Scp/Helpers/ChannelEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static class ChannelEncryption
/// <summary>
/// Encrypts the provided data using AES CBC mode with the given key and encryption counter.
/// </summary>
/// <param name="dataToEncrypt">The data to be encrypted.</param>
/// <param name="plainText">The data to be encrypted.</param>
/// <param name="encryptionKey">The AES key to use for encryption.</param>
/// <param name="encryptionCounter">
/// A counter used to generate the initialization vector (IV) for encryption.
Expand All @@ -32,28 +32,31 @@ internal static class ChannelEncryption
/// A <see cref="Memory{T}"/> containing the encrypted data.
/// </returns>
public static ReadOnlyMemory<byte> EncryptData(
ReadOnlySpan<byte> dataToEncrypt,
ReadOnlySpan<byte> plainText,
ReadOnlySpan<byte> encryptionKey,
int encryptionCounter)
{
// NB: Could skip this if the payload is empty (rather than sending a 16-byte encrypted '0x800000...' payload
byte[] countBytes = new byte[sizeof(int)];
Span<byte> countBytes = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(countBytes, encryptionCounter);

byte[] ivInput = new byte[16];
countBytes.CopyTo(ivInput, 16 - countBytes.Length); // copy to rightmost part of block
var iv = AesUtilities.BlockCipher(encryptionKey, ivInput);
Span<byte> ivInput = stackalloc byte[16];
int offset = 16 - countBytes.Length;
countBytes.CopyTo(ivInput[offset..]);

var paddedPayload = Padding.PadToBlockSize(dataToEncrypt);
var encryptedData = AesUtilities.AesCbcEncrypt(encryptionKey, iv.Span, paddedPayload.Span);
var iv = AesUtilities.BlockCipher(encryptionKey, ivInput);
var paddedPlaintext = Padding.PadToBlockSize(plainText);
var encryptedData = AesUtilities.AesCbcEncrypt(
encryptionKey,
iv.Span,
paddedPlaintext.Span);

return encryptedData;
}

/// <summary>
/// Decrypts the provided data using AES CBC mode with the given key and encryption counter.
/// </summary>
/// <param name="dataToDecrypt">The encrypted data to be decrypted.</param>
/// <param name="encryptedData">The encrypted data to be decrypted.</param>
/// <param name="key">The AES key to use for decryption.</param>
/// <param name="encryptionCounter">
/// A counter used to generate the initialization vector (IV) for decryption.
Expand All @@ -62,21 +65,23 @@ public static ReadOnlyMemory<byte> EncryptData(
/// A <see cref="Memory{T}"/> containing the decrypted data with padding removed.
/// </returns>
public static ReadOnlyMemory<byte> DecryptData(
ReadOnlySpan<byte> dataToDecrypt,
ReadOnlySpan<byte> encryptedData,
ReadOnlySpan<byte> key,
int encryptionCounter)
{
byte[] countBytes = new byte[sizeof(int)];
Span<byte> countBytes = stackalloc byte[sizeof(int)];
BinaryPrimitives.WriteInt32BigEndian(countBytes, encryptionCounter);

byte[] ivInput = new byte[16];
countBytes.CopyTo(ivInput, 16 - countBytes.Length); // copy to rightmost part of block
Span<byte> ivInput = stackalloc byte[16];
int offset = 16 - countBytes.Length;
ivInput[0] = 0x80; // to mark as RMAC calculation
countBytes.CopyTo(ivInput[offset..]);

var iv = AesUtilities.BlockCipher(key, ivInput);
var decryptedData = AesUtilities.AesCbcDecrypt(key, iv.Span, dataToDecrypt);
var decryptedData = AesUtilities.AesCbcDecrypt(key, iv.Span, encryptedData);

return Padding.RemovePadding(decryptedData.Span);
var plainText = Padding.RemovePadding(decryptedData.Span);
return plainText;
}
}
}

0 comments on commit 75a40ae

Please sign in to comment.