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);
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
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);
            }
        }
Пример #4
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);
        }
Пример #5
0
        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;
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #9
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_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);
        }
Пример #10
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);
        }
Пример #11
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);
            }
        }
Пример #12
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);
            }
        }
Пример #13
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);
            }
        }
Пример #14
0
 protected abstract void Deserialize(
     ReadOnlySpan <byte> span,
     MemoryPool <byte> memoryPool,
     out ReadOnlyMemory <byte> memory,
     out IMemoryOwner <byte> owner,
     out PublicKeyBytes publicKeyBytes);
Пример #15
0
 internal static extern int crypto_sign_ed25519_seed_keypair(
     out PublicKeyBytes pk,
     out byte sk,
     in byte seed);
Пример #16
0
 internal static extern int crypto_scalarmult_curve25519_base(
     out PublicKeyBytes q,
     in byte n);
Пример #17
0
 protected abstract void Deserialize(
     ReadOnlySpan <byte> span,
     out SecureMemoryHandle?keyHandle,
     out PublicKeyBytes publicKeyBytes);