Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private protected int DecryptKeyWrapPaddedCore<TState>(
ReadOnlySpan<byte> source,
Span<byte> destination,
TState state,
KeyWrapEcbTransform<TState> decryptEcb)
Func<TState, ReadOnlySpan<byte>, Span<byte>, int> decryptEcb)
{
ulong iv;

Expand Down Expand Up @@ -429,17 +429,11 @@ protected virtual unsafe void EncryptKeyWrapPaddedCore(ReadOnlySpan<byte> source
this,
static (instance, source, destination) => instance.EncryptEcb(source, destination, PaddingMode.None));
}

private protected delegate int KeyWrapEcbTransform<TState>(
TState state,
ReadOnlySpan<byte> source,
Span<byte> destination);

private protected void EncryptKeyWrapPaddedCore<TState>(
ReadOnlySpan<byte> source,
Span<byte> destination,
TState state,
KeyWrapEcbTransform<TState> encryptEcb)
Func<TState, ReadOnlySpan<byte>, Span<byte>, int> encryptEcb)
{
Debug.Assert(destination.Length == GetKeyWrapPaddedLength(source.Length));

Expand Down Expand Up @@ -491,7 +485,7 @@ private void Rfc3394Wrap<TState>(
ReadOnlySpan<byte> source,
Span<byte> destination,
TState state,
KeyWrapEcbTransform<TState> encryptEcb)
Func<TState, ReadOnlySpan<byte>, Span<byte>, int> encryptEcb)
{
Debug.Assert(source.Length % 8 == 0);
Debug.Assert(source.Length >= 16);
Expand Down Expand Up @@ -530,7 +524,7 @@ private ulong Rfc3394Unwrap<TState>(
ReadOnlySpan<byte> source,
Span<byte> destination,
TState state,
KeyWrapEcbTransform<TState> decryptEcb)
Func<TState, ReadOnlySpan<byte>, Span<byte>, int> decryptEcb)
{
Span<byte> B = stackalloc byte[16];
Span<byte> A = B.Slice(0, 8);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Internal.Cryptography;
using Internal.NativeCrypto;

Expand Down Expand Up @@ -44,5 +45,53 @@ private static BasicSymmetricCipherLiteBCrypt CreateLiteCipher(
iv,
encrypting);
}

protected override void EncryptKeyWrapPaddedCore(ReadOnlySpan<byte> source, Span<byte> destination)
{
Debug.Assert(destination.Length == GetKeyWrapPaddedLength(source.Length));

ILiteSymmetricCipher cipher = GetKey().UseKey(
BlockSize / BitsPerByte,
static (blockSizeBytes, key) => CreateLiteCipher(
CipherMode.ECB,
key,
iv: default,
blockSize: blockSizeBytes,
paddingSize: blockSizeBytes,
feedbackSize: 0,
encrypting: true));

using (cipher)
{
EncryptKeyWrapPaddedCore(
source,
destination,
cipher,
static (cipher, source, destination) => cipher.Transform(source, destination));
}
}

protected override int DecryptKeyWrapPaddedCore(ReadOnlySpan<byte> source, Span<byte> destination)
{
ILiteSymmetricCipher cipher = GetKey().UseKey(
BlockSize / BitsPerByte,
static (blockSizeBytes, key) => CreateLiteCipher(
CipherMode.ECB,
key,
iv: default,
blockSize: blockSizeBytes,
paddingSize: blockSizeBytes,
feedbackSize: 0,
encrypting: false));

using (cipher)
{
return DecryptKeyWrapPaddedCore(
source,
destination,
cipher,
static (cipher, source, destination) => cipher.Transform(source, destination));
}
}
}
}
Loading