Esempio n. 1
0
        private static void ExtractCore(
            SecureMemoryHandle inputKeyingMaterial,
            ReadOnlySpan <byte> salt,
            Span <byte> pseudorandomKey)
        {
            bool addedRef = false;

            try
            {
                inputKeyingMaterial.DangerousAddRef(ref addedRef);

                ExtractCore(inputKeyingMaterial.DangerousGetSpan(), salt, pseudorandomKey);
            }
            finally
            {
                if (addedRef)
                {
                    inputKeyingMaterial.DangerousRelease();
                }
            }
        }
Esempio n. 2
0
        private protected virtual void DeriveBytesCore(
            SecureMemoryHandle inputKeyingMaterial,
            ReadOnlySpan <byte> salt,
            ReadOnlySpan <byte> info,
            Span <byte> bytes)
        {
            bool addedRef = false;

            try
            {
                inputKeyingMaterial.DangerousAddRef(ref addedRef);

                DeriveBytesCore(inputKeyingMaterial.DangerousGetSpan(), salt, info, bytes);
            }
            finally
            {
                if (addedRef)
                {
                    inputKeyingMaterial.DangerousRelease();
                }
            }
        }
Esempio n. 3
0
        internal virtual void DeriveKeyCore(
            SecureMemoryHandle inputKeyingMaterial,
            ReadOnlySpan <byte> salt,
            ReadOnlySpan <byte> info,
            SecureMemoryHandle keyHandle)
        {
            bool addedRef = false;

            try
            {
                keyHandle.DangerousAddRef(ref addedRef);

                DeriveBytesCore(inputKeyingMaterial, salt, info, keyHandle.DangerousGetSpan());
            }
            finally
            {
                if (addedRef)
                {
                    keyHandle.DangerousRelease();
                }
            }
        }
Esempio n. 4
0
            public static SecureMemoryHandle CreateFrom(
                ReadOnlySpan <byte> source)
            {
                SecureMemoryHandle handle = Create(source.Length);

                bool mustCallRelease = false;

                try
                {
                    handle.DangerousAddRef(ref mustCallRelease);
                    source.CopyTo(handle.DangerousGetSpan());
                }
                finally
                {
                    if (mustCallRelease)
                    {
                        handle.DangerousRelease();
                    }
                }

                return(handle);
            }
Esempio n. 5
0
        internal override void CreateKey(
            SecureMemoryHandle keyHandle,
            out byte[] publicKeyBytes)
        {
            Span <byte> seed;

            try
            {
                unsafe
                {
                    byte *pointer = stackalloc byte[crypto_sign_ed25519_SEEDBYTES];
                    seed = new Span <byte>(pointer, crypto_sign_ed25519_SEEDBYTES);
                }

                bool addedRef = false;
                try
                {
                    keyHandle.DangerousAddRef(ref addedRef);
                    keyHandle.DangerousGetSpan().Slice(0, crypto_sign_ed25519_SEEDBYTES).CopyTo(seed);
                }
                finally
                {
                    if (addedRef)
                    {
                        keyHandle.DangerousRelease();
                    }
                }

                publicKeyBytes = new byte[crypto_sign_ed25519_PUBLICKEYBYTES];
                crypto_sign_ed25519_seed_keypair(publicKeyBytes, keyHandle, ref seed.DangerousGetPinnableReference());
            }
            finally
            {
                sodium_memzero(ref seed.DangerousGetPinnableReference(), (UIntPtr)seed.Length);
            }
        }