コード例 #1
0
        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);
        }
コード例 #2
0
ファイル: SharedSecret.cs プロジェクト: dustinsoftware/nsec
        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));
        }
コード例 #3
0
 public static void Import(
     ReadOnlySpan <byte> span,
     out SecureMemoryHandle handle)
 {
     Alloc(span.Length, out handle);
     handle.Import(span);
 }
コード例 #4
0
ファイル: HmacSha256.cs プロジェクト: dustinsoftware/nsec
 internal override void CreateKey(
     ReadOnlySpan <byte> seed,
     out SecureMemoryHandle keyHandle,
     out byte[] publicKeyBytes)
 {
     publicKeyBytes = null;
     SecureMemoryHandle.Import(seed, out keyHandle);
 }
コード例 #5
0
ファイル: Norx3241.cs プロジェクト: dustinsoftware/nsec
        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);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
ファイル: PrivateKeyFormatter.cs プロジェクト: judgie79/nsec
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
ファイル: Blake2bMac.cs プロジェクト: dustinsoftware/nsec
        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);
        }
コード例 #10
0
        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);
        }
コード例 #11
0
ファイル: X25519.cs プロジェクト: dustinsoftware/nsec
        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);
        }
コード例 #12
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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
ファイル: Blake2.cs プロジェクト: wandbond/nsec
        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);
        }