Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (ReadOnly)Span for buffer related operations #192

Open
manison opened this issue Aug 20, 2021 · 1 comment
Open

Support (ReadOnly)Span for buffer related operations #192

manison opened this issue Aug 20, 2021 · 1 comment

Comments

@manison
Copy link

manison commented Aug 20, 2021

Today all buffer related operations in both the high level and the low level APIs use arrays. When one must deal with external buffer slices this becomes inefficient and error prone since temporary arrays need to be allocated and content must be copied back and forth.

For example consider implementing the ICryptoTransform.TransformBlock method:

public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset);

If the inputOffset parameter is non-zero, temporary array must be allocated and inputCount bytes must be copied from it before it can be passed to C_EncryptUpdate method. If a new overload would be added to Pkcs11Library supporting the Span

public CKR C_EncryptUpdate(NativeULong session, ReadOnlySpan<byte> part, Span<byte> encryptedPart, ref NativeULong encryptedPartLen);

the unnecessary allocation could be eliminated and the call would be simplified to

public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
  ...
  _lib.C_EncryptUpdate(_session,
    new ReadOnlySpan<byte>(inputBuffer, inputOffset, inputCount),
    new Span<byte>(outputBuffer, outputOffset, outputBuffer.Length - outputOffset),
    ref len);
  ...
}

The downside of this is the Span could be only supported for net45 and netstandard2.0 builds through the System.Memory package.

@jariq
Copy link
Member

jariq commented Aug 21, 2021

Yes, Span utilization would be a great addition but as you already mentioned it would limit supported platforms. Maybe it will land in Pkcs11Interop 6 along with #160 and #161. We'll see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants