Exemplo n.º 1
0
        internal SharedSecret(
            SecureMemoryHandle sharedSecretHandle)
        {
            Debug.Assert(sharedSecretHandle != null);

            sharedSecretHandle.MakeReadOnly();

            _handle = sharedSecretHandle;
        }
Exemplo n.º 2
0
        internal Key(
            Algorithm algorithm,
            KeyFlags flags,
            SecureMemoryHandle keyHandle,
            byte[] publicKeyBytes)
        {
            Debug.Assert(algorithm != null);
            Debug.Assert(keyHandle != null);

            keyHandle.MakeReadOnly();

            _algorithm = algorithm;
            _flags     = flags;
            _handle    = keyHandle;
            _publicKey = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null;
        }
Exemplo n.º 3
0
Arquivo: Key.cs Projeto: cwharris/nsec
        internal Key(
            Algorithm algorithm,
            KeyExportPolicies exportPolicy,
            SecureMemoryHandle keyHandle,
            byte[] publicKeyBytes)
        {
            Debug.Assert(algorithm != null);
            Debug.Assert(keyHandle != null);

            keyHandle.MakeReadOnly();

            _algorithm    = algorithm;
            _exportPolicy = exportPolicy;
            _handle       = keyHandle;
            _publicKey    = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null;
        }
Exemplo n.º 4
0
Arquivo: Key.cs Projeto: 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;
        }
Exemplo n.º 5
0
Arquivo: Key.cs Projeto: 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;
        }
Exemplo 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;
        }