/// <summary>
        /// Updates the specified <see cref="UpdateNonceType"/> with the specified nonce.
        /// </summary>
        /// <param name="nonce">Nonce to use for the update.</param>
        /// <param name="nonceType">Nonce type to update.</param>
        /// <exception cref="ArgumentNullException"><paramref name="nonce"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="nonce"/> length is not 24.</exception>
        public void UpdateNonce(byte[] nonce, UpdateNonceType nonceType)
        {
            if (_cryptoState == CryptoState.SecoundKey)
            {
                throw new InvalidOperationException("Cannot update nonce after updated with shared key 'k'.");
            }
            if (nonce == null)
            {
                throw new ArgumentNullException(nameof(nonce));
            }
            if (nonce.Length != 24)
            {
                throw new ArgumentOutOfRangeException(nameof(nonce), "nonce must be 24 bytes in length.");
            }

            switch (nonceType)
            {
            case UpdateNonceType.Blake:
                if (_cryptoState == CryptoState.InitialKey)
                {
                    if (Direction == MessageDirection.Client)     // order of keys is important. we're the server
                    {
                        _blake2bNonce = GenerateBlake2BNonce(nonce, _sharedKey, _keyPair.PublicKey);
                    }
                    else     // we're the client
                    {
                        _blake2bNonce = GenerateBlake2BNonce(nonce, _keyPair.PublicKey, _sharedKey);
                    }

                    _cryptoState = CryptoState.BlakeNonce;     // use blake nonce
                }
                break;

            case UpdateNonceType.Decrypt:
                _decryptNonce = nonce;
                break;

            case UpdateNonceType.Encrypt:
                _encryptNonce = nonce;
                break;

            default:
                throw new ArgumentException("Unexpected NonceType: " + nonceType, "nonceType");
            }
        }
示例#2
0
        /// <summary>
        /// Updates the specified <see cref="UpdateNonceType"/> with the specified nonce.
        /// </summary>
        /// <param name="nonce">Nonce to use for the update.</param>
        /// <param name="nonceType">Nonce type to update.</param>
        /// <exception cref="ArgumentNullException"><paramref name="nonce"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="nonce"/> length is not 24.</exception>
        public void UpdateNonce(byte[] nonce, UpdateNonceType nonceType)
        {
            if (_cryptoState == CryptoState.SecoundKey) // can only be updated twice
                throw new InvalidOperationException("Cannot update nonce after updated with shared key 'k'.");
            if (nonce == null)
                throw new ArgumentNullException("nonce");
            if (nonce.Length != CoCKeyPair.NonceLength)
                throw new ArgumentOutOfRangeException("nonce", "nonce must be 24 bytes in length.");

            switch (nonceType)
            {
                case UpdateNonceType.Blake:
                    if (_cryptoState == CryptoState.InitialKey)
                    {
                        if (Direction == MessageDirection.Client) // order of keys is important. we're the server
                            _blake2bNonce = GenerateBlake2BNonce(nonce, _sharedKey, _keyPair.PublicKey);
                        else // we're the client
                            _blake2bNonce = GenerateBlake2BNonce(nonce, _keyPair.PublicKey, _sharedKey);

                        _cryptoState = CryptoState.BlakeNonce; // use blake nonce
                    }
                    break;

                case UpdateNonceType.Decrypt:
                    _decryptNonce = nonce;
                    break;

                case UpdateNonceType.Encrypt:
                    _encryptNonce = nonce;
                    break;

                default:
                    throw new ArgumentException("Unexpected NonceType: " + nonceType, "nonceType");
            }
        }