internal override bool TryImportKey( ReadOnlySpan <byte> blob, KeyBlobFormat format, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { if (format != KeyBlobFormat.RawSymmetricKey) { throw Error.Argument_FormatNotSupported(nameof(format), format.ToString()); } if (blob.Length < MinKeySize) { keyHandle = null; publicKeyBytes = null; return(false); } if (blob.Length > SHA256MessageBlockSize) { publicKeyBytes = null; SecureMemoryHandle.Alloc(crypto_hash_sha256_BYTES, out keyHandle); crypto_hash_sha256_init(out crypto_hash_sha256_state state); crypto_hash_sha256_update(ref state, ref blob.DangerousGetPinnableReference(), (ulong)blob.Length); crypto_hash_sha256_final(ref state, keyHandle); } else { publicKeyBytes = null; SecureMemoryHandle.Alloc(blob.Length, out keyHandle); keyHandle.Import(blob); } return(true); }
public static SharedSecret Import( ReadOnlySpan <byte> sharedSecret) { if (sharedSecret.Length > 128) { throw Error.Argument_SharedSecretLength(nameof(sharedSecret), 128.ToString()); } Sodium.Initialize(); SecureMemoryHandle sharedSecretHandle = null; bool success = false; try { SecureMemoryHandle.Import(sharedSecret, out sharedSecretHandle); success = true; } finally { if (!success && sharedSecretHandle != null) { sharedSecretHandle.Dispose(); } } return(new SharedSecret(sharedSecretHandle)); }
public static void Import( ReadOnlySpan <byte> span, out SecureMemoryHandle handle) { Alloc(span.Length, out handle); handle.Import(span); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { publicKeyBytes = null; SecureMemoryHandle.Import(seed, out keyHandle); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(seed.Length == crypto_aead_norx3241_KEYBYTES); publicKeyBytes = null; SecureMemoryHandle.Import(seed, out keyHandle); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(seed.Length == crypto_secretbox_xsalsa20poly1305_KEYBYTES); publicKeyBytes = null; SecureMemoryHandle.Import(seed, out keyHandle); }
protected virtual void Deserialize( ReadOnlySpan <byte> span, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(span.Length == _keySize); publicKeyBytes = null; SecureMemoryHandle.Alloc(span.Length, out keyHandle); keyHandle.Import(span); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(seed.Length == crypto_aead_chacha20poly1305_ietf_KEYBYTES); publicKeyBytes = null; SecureMemoryHandle.Alloc(seed.Length, out keyHandle); keyHandle.Import(seed); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(seed.Length >= crypto_generichash_blake2b_KEYBYTES_MIN); Debug.Assert(seed.Length <= crypto_generichash_blake2b_KEYBYTES_MAX); publicKeyBytes = null; SecureMemoryHandle.Import(seed, out keyHandle); }
protected override void Deserialize( ReadOnlySpan <byte> span, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(span.Length == crypto_scalarmult_curve25519_SCALARBYTES); publicKeyBytes = new byte[crypto_scalarmult_curve25519_SCALARBYTES]; SecureMemoryHandle.Alloc(span.Length, out keyHandle); keyHandle.Import(span); crypto_scalarmult_curve25519_base(publicKeyBytes, keyHandle); }
internal override void CreateKey( ReadOnlySpan <byte> seed, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { Debug.Assert(seed.Length == crypto_scalarmult_curve25519_SCALARBYTES); publicKeyBytes = new byte[crypto_scalarmult_curve25519_SCALARBYTES]; SecureMemoryHandle.Import(seed, out keyHandle); crypto_scalarmult_curve25519_base(publicKeyBytes, keyHandle); Debug.Assert((publicKeyBytes[crypto_scalarmult_curve25519_SCALARBYTES - 1] & 0x80) == 0); }
public bool TryImport( ReadOnlySpan <byte> blob, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { if (blob.Length < _minKeySize || blob.Length > _maxKeySize) { keyHandle = null; publicKeyBytes = null; return(false); } publicKeyBytes = null; SecureMemoryHandle.Import(blob, out keyHandle); return(true); }
public bool TryImport( ReadOnlySpan <byte> blob, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { int keySize = blob.Length - (_blobHeader.Length + sizeof(uint)); if (keySize < _minKeySize || keySize > _maxKeySize || !blob.Slice(0, _blobHeader.Length).SequenceEqual(_blobHeader) || blob.Slice(_blobHeader.Length).ReadLittleEndian() != (uint)keySize) { keyHandle = null; publicKeyBytes = null; return(false); } publicKeyBytes = null; SecureMemoryHandle.Alloc(keySize, out keyHandle); keyHandle.Import(blob.Slice(_blobHeader.Length + sizeof(uint))); return(true); }
public bool TryImport( ReadOnlySpan <byte> blob, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { int start = _blobHeader.Length + sizeof(uint); int length = blob.Length - start; if (length < _minKeySize || length > _maxKeySize || !blob.Slice(0, _blobHeader.Length).SequenceEqual(_blobHeader) || BinaryPrimitives.ReadUInt32LittleEndian(blob.Slice(_blobHeader.Length, sizeof(uint))) != (uint)length) { keyHandle = null; publicKeyBytes = null; return(false); } publicKeyBytes = null; SecureMemoryHandle.Import(blob.Slice(start, length), out keyHandle); return(true); }
internal override bool TryImportKey( ReadOnlySpan <byte> blob, KeyBlobFormat format, out SecureMemoryHandle keyHandle, out byte[] publicKeyBytes) { if (format != KeyBlobFormat.RawSymmetricKey) { throw Error.Argument_FormatNotSupported(nameof(format), format.ToString()); } if (blob.Length < MinKeySize || blob.Length > MaxKeySize) { keyHandle = null; publicKeyBytes = null; return(false); } publicKeyBytes = null; SecureMemoryHandle.Alloc(blob.Length, out keyHandle); keyHandle.Import(blob); return(true); }