internal SharedSecret( SecureMemoryHandle sharedSecretHandle) { Debug.Assert(sharedSecretHandle != null); sharedSecretHandle.MakeReadOnly(); _handle = sharedSecretHandle; }
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; }
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; }
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; }
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; }
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; }