public bool TryImportText( ReadOnlySpan <byte> blob, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte>?owner, out PublicKeyBytes publicKeyBytes) { Span <byte> temp = stackalloc byte[_blobSize]; try { if (!Armor.TryDecodeFromUtf8(blob, s_beginLabel, s_endLabel, temp, out int written) || written != _blobSize) { memory = default; owner = default; publicKeyBytes = default; return(false); } return(TryImport(temp, memoryPool, out memory, out owner, out publicKeyBytes)); } finally { CryptographicOperations.ZeroMemory(temp); } }
protected unsafe override void Deserialize( ReadOnlySpan <byte> span, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte> owner, out PublicKeyBytes publicKeyBytes) { if (Unsafe.SizeOf <PublicKeyBytes>() != crypto_sign_ed25519_PUBLICKEYBYTES) { throw Error.InvalidOperation_InternalError(); } Debug.Assert(span.Length == crypto_sign_ed25519_SEEDBYTES); owner = memoryPool.Rent(crypto_sign_ed25519_SECRETKEYBYTES); memory = owner.Memory.Slice(0, crypto_sign_ed25519_SECRETKEYBYTES); fixed(PublicKeyBytes *pk = &publicKeyBytes) fixed(byte *sk = owner.Memory.Span) fixed(byte *seed_ = span) { int error = crypto_sign_ed25519_seed_keypair(pk, sk, seed_); Debug.Assert(error == 0); } }
protected override unsafe void Deserialize( ReadOnlySpan <byte> span, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte> owner, out PublicKeyBytes publicKeyBytes) { if (Unsafe.SizeOf <PublicKeyBytes>() != crypto_scalarmult_curve25519_SCALARBYTES) { throw Error.InvalidOperation_InternalError(); } Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); owner = memoryPool.Rent(crypto_scalarmult_curve25519_SCALARBYTES); memory = owner.Memory.Slice(0, crypto_scalarmult_curve25519_SCALARBYTES); span.CopyTo(owner.Memory.Span); fixed(PublicKeyBytes *q = &publicKeyBytes) fixed(byte *n = owner.Memory.Span) { int error = crypto_scalarmult_curve25519_base(q, n); Debug.Assert(error == 0); Debug.Assert((((byte *)q)[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); } }
protected override void Deserialize( ReadOnlySpan <byte> span, out PublicKeyBytes publicKeyBytes) { Debug.Assert(span.Length == crypto_sign_ed25519_PUBLICKEYBYTES); Debug.Assert(Unsafe.SizeOf <PublicKeyBytes>() == crypto_sign_ed25519_PUBLICKEYBYTES); Unsafe.CopyBlockUnaligned(ref Unsafe.As <PublicKeyBytes, byte>(ref publicKeyBytes), ref Unsafe.AsRef(in span.GetPinnableReference()), crypto_sign_ed25519_PUBLICKEYBYTES); }
protected override void Deserialize( ReadOnlySpan <byte> span, out PublicKeyBytes publicKeyBytes) { Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); Debug.Assert(Unsafe.SizeOf <PublicKeyBytes>() == crypto_scalarmult_curve25519_SCALARBYTES); Unsafe.CopyBlockUnaligned(ref Unsafe.As <PublicKeyBytes, byte>(ref publicKeyBytes), ref Unsafe.AsRef(in span.GetPinnableReference()), crypto_scalarmult_curve25519_SCALARBYTES); Unsafe.Add(ref Unsafe.As <PublicKeyBytes, byte>(ref publicKeyBytes), crypto_scalarmult_curve25519_SCALARBYTES - 1) &= 0x7F; }
protected override void Deserialize( ReadOnlySpan <byte> span, out PublicKeyBytes publicKeyBytes) { if (Unsafe.SizeOf <PublicKeyBytes>() != crypto_sign_ed25519_PUBLICKEYBYTES) { throw Error.InvalidOperation_InternalError(); } Debug.Assert(span.Length == crypto_sign_ed25519_PUBLICKEYBYTES); Unsafe.CopyBlockUnaligned(ref Unsafe.As <PublicKeyBytes, byte>(ref publicKeyBytes), ref Unsafe.AsRef(in span.GetPinnableReference()), crypto_sign_ed25519_PUBLICKEYBYTES); }
public bool TryImport( ReadOnlySpan <byte> blob, out SecureMemoryHandle?keyHandle, out PublicKeyBytes publicKeyBytes) { if (blob.Length != _blobSize || !blob.StartsWith(_blobHeader)) { keyHandle = default; publicKeyBytes = default; return(false); } Deserialize(blob.Slice(_blobHeader.Length), out keyHandle, out publicKeyBytes); return(true); }
public bool TryImport( ReadOnlySpan <byte> blob, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte>?owner, out PublicKeyBytes publicKeyBytes) { if (blob.Length != _blobSize || !blob.StartsWith(_blobHeader)) { memory = default; owner = default; publicKeyBytes = default; return(false); } Deserialize(blob.Slice(_blobHeader.Length), memoryPool, out memory, out owner, out publicKeyBytes); return(true); }
protected override void Deserialize( ReadOnlySpan <byte> span, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte> owner, out PublicKeyBytes publicKeyBytes) { Debug.Assert(span.Length == crypto_sign_ed25519_SEEDBYTES); Debug.Assert(Unsafe.SizeOf <PublicKeyBytes>() == crypto_sign_ed25519_PUBLICKEYBYTES); owner = memoryPool.Rent(crypto_sign_ed25519_SECRETKEYBYTES); memory = owner.Memory.Slice(0, crypto_sign_ed25519_SECRETKEYBYTES); int error = crypto_sign_ed25519_seed_keypair( out publicKeyBytes, out owner.Memory.Span.GetPinnableReference(), in span.GetPinnableReference()); Debug.Assert(error == 0); }
protected override void Deserialize( ReadOnlySpan <byte> span, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte> owner, out PublicKeyBytes publicKeyBytes) { Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); Debug.Assert(Unsafe.SizeOf <PublicKeyBytes>() == crypto_scalarmult_curve25519_SCALARBYTES); owner = memoryPool.Rent(crypto_scalarmult_curve25519_SCALARBYTES); memory = owner.Memory.Slice(0, crypto_scalarmult_curve25519_SCALARBYTES); span.CopyTo(owner.Memory.Span); int error = crypto_scalarmult_curve25519_base( out publicKeyBytes, in owner.Memory.Span.GetPinnableReference()); Debug.Assert(error == 0); Debug.Assert((Unsafe.Add(ref Unsafe.As <PublicKeyBytes, byte>(ref publicKeyBytes), crypto_scalarmult_curve25519_SCALARBYTES - 1) & 0x80) == 0); }
protected override unsafe void Deserialize( ReadOnlySpan <byte> span, out SecureMemoryHandle?keyHandle, out PublicKeyBytes publicKeyBytes) { if (Unsafe.SizeOf <PublicKeyBytes>() != crypto_scalarmult_curve25519_SCALARBYTES) { throw Error.InvalidOperation_InternalError(); } Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); keyHandle = SecureMemoryHandle.CreateFrom(span); fixed(PublicKeyBytes *q = &publicKeyBytes) { int error = crypto_scalarmult_curve25519_base(q, keyHandle); Debug.Assert(error == 0); Debug.Assert((((byte *)q)[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); } }
protected unsafe override void Deserialize( ReadOnlySpan <byte> span, out SecureMemoryHandle?keyHandle, out PublicKeyBytes publicKeyBytes) { if (Unsafe.SizeOf <PublicKeyBytes>() != crypto_sign_ed25519_PUBLICKEYBYTES) { throw Error.InvalidOperation_InternalError(); } Debug.Assert(span.Length == crypto_sign_ed25519_SEEDBYTES); keyHandle = SecureMemoryHandle.Create(crypto_sign_ed25519_SECRETKEYBYTES); fixed(PublicKeyBytes *pk = &publicKeyBytes) fixed(byte *seed_ = span) { int error = crypto_sign_ed25519_seed_keypair(pk, keyHandle, seed_); Debug.Assert(error == 0); } }
public bool TryImportText( ReadOnlySpan <byte> blob, out SecureMemoryHandle?keyHandle, out PublicKeyBytes publicKeyBytes) { Span <byte> temp = stackalloc byte[_blobSize]; try { if (!Armor.TryDecodeFromUtf8(blob, s_beginLabel, s_endLabel, temp, out int written) || written != _blobSize) { keyHandle = default; publicKeyBytes = default; return(false); } return(TryImport(temp, out keyHandle, out publicKeyBytes)); } finally { CryptographicOperations.ZeroMemory(temp); } }
protected abstract void Deserialize( ReadOnlySpan <byte> span, MemoryPool <byte> memoryPool, out ReadOnlyMemory <byte> memory, out IMemoryOwner <byte> owner, out PublicKeyBytes publicKeyBytes);
internal static extern int crypto_sign_ed25519_seed_keypair( out PublicKeyBytes pk, out byte sk, in byte seed);
internal static extern int crypto_scalarmult_curve25519_base( out PublicKeyBytes q, in byte n);
protected abstract void Deserialize( ReadOnlySpan <byte> span, out SecureMemoryHandle?keyHandle, out PublicKeyBytes publicKeyBytes);