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 f2d9555
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public static ReadOnlyMemory<byte> EncryptData(
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 iv = AesUtilities.BlockCipher(encryptionKey, ivInput);
var paddedPayload = Padding.PadToBlockSize(dataToEncrypt);
var encryptedData = AesUtilities.AesCbcEncrypt(encryptionKey, iv.Span, paddedPayload.Span);

Expand Down

0 comments on commit f2d9555

Please sign in to comment.