Example #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));
        }
Example #2
0
File: Key.cs Project: 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;
        }
Example #3
0
File: Key.cs Project: 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;
        }
Example #4
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));
        }