Esempio n. 1
0
        public Key DeriveKey(
            SharedSecret sharedSecret,
            ReadOnlySpan <byte> salt,
            ReadOnlySpan <byte> info,
            Algorithm algorithm,
            KeyExportPolicies exportPolicy = KeyExportPolicies.None)
        {
            if (sharedSecret == null)
            {
                throw Error.ArgumentNull_SharedSecret(nameof(sharedSecret));
            }
            if (!_supportsSalt && !salt.IsEmpty)
            {
                throw Error.Argument_SaltNotSupported(nameof(salt));
            }
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int seedSize = algorithm.GetDefaultSeedSize();

            if (seedSize > MaxOutputSize)
            {
                throw Error.NotSupported_CreateKey();
            }
            Debug.Assert(seedSize <= 64);

            SecureMemoryHandle keyHandle = null;

            byte[] publicKeyBytes = null;
            bool   success        = false;

            try
            {
                Span <byte> seed = stackalloc byte[seedSize];
                try
                {
                    DeriveBytesCore(sharedSecret.Handle, salt, info, seed);
                    algorithm.CreateKey(seed, out keyHandle, out publicKeyBytes);
                    success = true;
                }
                finally
                {
                    sodium_memzero(ref seed.DangerousGetPinnableReference(), (UIntPtr)seed.Length);
                }
            }
            finally
            {
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            return(new Key(algorithm, exportPolicy, keyHandle, publicKeyBytes));
        }
Esempio n. 2
0
        public Key DeriveKey(
            SharedSecret sharedSecret,
            ReadOnlySpan <byte> salt,
            ReadOnlySpan <byte> info,
            Algorithm algorithm,
            KeyFlags flags = KeyFlags.None)
        {
            if (sharedSecret == null)
            {
                throw Error.ArgumentNull_SharedSecret(nameof(sharedSecret));
            }
            if (!_supportsSalt && !salt.IsEmpty)
            {
                throw Error.Argument_SaltNotSupported(nameof(salt));
            }
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int keySize = algorithm.GetDefaultKeySize();

            if (keySize > MaxOutputSize)
            {
                throw Error.NotSupported_CreateKey();
            }

            SecureMemoryHandle keyHandle = null;

            byte[] publicKeyBytes = null;
            bool   success        = false;

            try
            {
                SecureMemoryHandle.Alloc(keySize, out keyHandle);
                DeriveKeyCore(sharedSecret.Handle, salt, info, keyHandle);
                algorithm.CreateKey(keyHandle, out publicKeyBytes);
                success = true;
            }
            finally
            {
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            return(new Key(algorithm, flags, keyHandle, publicKeyBytes));
        }
Esempio n. 3
0
File: Key.cs Progetto: cwharris/nsec
        public Key(
            Algorithm algorithm,
            KeyExportPolicies exportPolicy = KeyExportPolicies.None)
        {
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int seedSize = algorithm.GetDefaultSeedSize();

            Debug.Assert(seedSize <= 64);

            SecureMemoryHandle keyHandle = null;

            byte[] publicKeyBytes = null;
            bool   success        = false;

            try
            {
                Span <byte> seed = stackalloc byte[seedSize];
                try
                {
                    RandomGenerator.Default.GenerateBytes(seed);
                    algorithm.CreateKey(seed, out keyHandle, out publicKeyBytes);
                    success = true;
                }
                finally
                {
                    sodium_memzero(ref seed.DangerousGetPinnableReference(), (UIntPtr)seed.Length);
                }
            }
            finally
            {
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            keyHandle.MakeReadOnly();

            _algorithm    = algorithm;
            _exportPolicy = exportPolicy;
            _handle       = keyHandle;
            _publicKey    = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null;
        }
Esempio n. 4
0
File: Key.cs Progetto: judgie79/nsec
        public Key(
            Algorithm algorithm,
            KeyFlags flags = KeyFlags.None)
        {
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int seedSize = algorithm.GetDefaultSeedSize();

            SecureMemoryHandle keyHandle = null;

            byte[]      publicKeyBytes = null;
            bool        success        = false;
            Span <byte> seed;

            try
            {
                unsafe
                {
                    Debug.Assert(seedSize <= 64);
                    byte *pointer = stackalloc byte[seedSize];
                    seed = new Span <byte>(pointer, seedSize);
                }

                SecureRandom.GenerateBytesCore(seed);
                algorithm.CreateKey(seed, out keyHandle, out publicKeyBytes);
                success = true;
            }
            finally
            {
                sodium_memzero(ref seed.DangerousGetPinnableReference(), (UIntPtr)seed.Length);
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            keyHandle.MakeReadOnly();

            _algorithm = algorithm;
            _flags     = flags;
            _handle    = keyHandle;
            _publicKey = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null;
        }
Esempio n. 5
0
        public Key GenerateKey(
            Algorithm algorithm,
            KeyExportPolicies exportPolicy = KeyExportPolicies.None)
        {
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int seedSize = algorithm.GetDefaultSeedSize();

            Debug.Assert(seedSize <= 64);

            SecureMemoryHandle keyHandle = null;

            byte[] publicKeyBytes = null;
            bool   success        = false;

            try
            {
                Span <byte> seed = stackalloc byte[seedSize];
                try
                {
                    GenerateBytesCore(seed);
                    algorithm.CreateKey(seed, out keyHandle, out publicKeyBytes);
                    success = true;
                }
                finally
                {
                    sodium_memzero(ref MemoryMarshal.GetReference(seed), (UIntPtr)seed.Length);
                }
            }
            finally
            {
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            return(new Key(algorithm, exportPolicy, keyHandle, publicKeyBytes));
        }
Esempio n. 6
0
        public Key(
            Algorithm algorithm,
            KeyFlags flags = KeyFlags.None)
        {
            if (algorithm == null)
            {
                throw Error.ArgumentNull_Algorithm(nameof(algorithm));
            }

            int keySize = algorithm.GetDefaultKeySize();

            SecureMemoryHandle keyHandle = null;

            byte[] publicKeyBytes = null;
            bool   success        = false;

            try
            {
                SecureMemoryHandle.Alloc(keySize, out keyHandle);
                SecureRandom.GenerateKeyCore(keyHandle);
                algorithm.CreateKey(keyHandle, out publicKeyBytes);
                success = true;
            }
            finally
            {
                if (!success && keyHandle != null)
                {
                    keyHandle.Dispose();
                }
            }

            keyHandle.MakeReadOnly();

            _algorithm = algorithm;
            _flags     = flags;
            _handle    = keyHandle;
            _publicKey = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null;
        }