Exemplo n.º 1
0
        public void ShouldEncryptAndDecryptWithValidatedTag()
        {
            var testData = GetTestData();

            // Perform encryption operation and assert
            var encryptParam = new AeadModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                testData.Nonce,
                testData.Key,
                testData.Payload,
                testData.AssocData,
                128
                );
            var encryptionResult = _newSubject.ProcessPayload(encryptParam);

            Assert.IsTrue(encryptionResult.Success, $"{nameof(encryptionResult.Success)} Encrypt");
            Assert.AreEqual(testData.CipherText, encryptionResult.Result, nameof(testData.CipherText));

            // Validate the decryption operation / tag
            var decryptParam = new AeadModeBlockCipherParameters(
                BlockCipherDirections.Decrypt,
                testData.Nonce,
                testData.Key,
                encryptionResult.Result,
                testData.AssocData,
                128
                );
            var decryptionResult = _newSubject.ProcessPayload(decryptParam);

            Assert.IsTrue(decryptionResult.Success, $"{nameof(decryptionResult.Success)} Decrypt");
            Assert.AreEqual(testData.Payload, decryptionResult.Result);
        }
Exemplo n.º 2
0
        protected override BitString Mac(BitString macData)
        {
            var param = new AeadModeBlockCipherParameters(
                BlockCipherDirections.Encrypt,
                _keyConfirmationParameters.CcmNonce,
                _keyConfirmationParameters.DerivedKeyingMaterial,
                new BitString(0),
                macData,
                _keyConfirmationParameters.MacLength
                );

            var result = _ccm.ProcessPayload(param);

            if (!result.Success)
            {
                throw new Exception(result.ErrorMessage);
            }

            return(result.Result);
        }
Exemplo n.º 3
0
        public void AESGCM_XPNNewEngine()
        {
            BitString aad       = new BitString("56d1dc66b2ae1c5c972aa1c22025c74b");
            BitString plainText = new BitString("f15e9ceb86dd8309767c3f675eb5503c");
            BitString key       = new BitString("b9eac5c6 50daeab9 c15aec8d 362cfd1f ccef9d20 fe3c0e54 fd321554 8d203f0d");
            BitString iv        = new BitString("04527842 a98b3336 2a09067c");
            BitString salt      = new BitString("12345678 87654388 44888844");
            BitString newIV;

            newIV = iv.XOR(salt);

            var param         = new AeadModeBlockCipherParameters(BlockCipherDirections.Encrypt, newIV, key, plainText, aad, 128);
            var encryptResult = _subject.ProcessPayload(param);

            Assert.IsTrue(encryptResult.Success);
            Assert.AreEqual(encryptResult.Tag, new BitString("e47971b2 c83ed28a d66fb896 2478d01f"), nameof(encryptResult.Tag));

            var param2        = new AeadModeBlockCipherParameters(BlockCipherDirections.Decrypt, newIV, key, encryptResult.Result, aad, encryptResult.Tag);
            var decryptResult = _subject.ProcessPayload(param2);

            Assert.IsTrue(decryptResult.Success);
        }