Beispiel #1
0
        public IMac GetMacInstance(MacModes mode)
        {
            switch (mode)
            {
            case MacModes.CMAC_AES128:
                return(_cmacFactory.GetCmacInstance(CmacTypes.AES128));

            case MacModes.CMAC_AES192:
                return(_cmacFactory.GetCmacInstance(CmacTypes.AES192));

            case MacModes.CMAC_AES256:
                return(_cmacFactory.GetCmacInstance(CmacTypes.AES256));

            case MacModes.CMAC_TDES:
                return(_cmacFactory.GetCmacInstance(CmacTypes.TDES));

            case MacModes.HMAC_SHA1:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160)));

            case MacModes.HMAC_SHA224:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d224)));

            case MacModes.HMAC_SHA256:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256)));

            case MacModes.HMAC_SHA384:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d384)));

            case MacModes.HMAC_SHA512:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512)));

            case MacModes.HMAC_SHA_d512t224:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t224)));

            case MacModes.HMAC_SHA_d512t256:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t256)));

            case MacModes.HMAC_SHA3_224:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d224)));

            case MacModes.HMAC_SHA3_256:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d256)));

            case MacModes.HMAC_SHA3_384:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d384)));

            case MacModes.HMAC_SHA3_512:
                return(_hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d512)));

            default:
                throw new ArgumentException("MAC Mode not supported");
            }
        }
Beispiel #2
0
        public INoKeyConfirmation GetInstance(INoKeyConfirmationParameters parameters)
        {
            switch (parameters.KeyAgreementMacType)
            {
            case KeyAgreementMacType.AesCcm:
                return(new NoKeyConfirmationAesCcm(_macDataCreator, parameters, new CcmBlockCipher(new AesEngine(), new ModeBlockCipherFactory(), new AES_CCMInternals())));

            case KeyAgreementMacType.CmacAes:
                return(new NoKeyConfirmationCmac(_macDataCreator, parameters, _cmacFactory.GetCmacInstance(CmacTypes.AES128)));    // doesn't matter as long as aea

            case KeyAgreementMacType.HmacSha1:
            case KeyAgreementMacType.HmacSha2D224:
            case KeyAgreementMacType.HmacSha2D256:
            case KeyAgreementMacType.HmacSha2D384:
            case KeyAgreementMacType.HmacSha2D512:
            case KeyAgreementMacType.HmacSha2D512_T224:
            case KeyAgreementMacType.HmacSha2D512_T256:
            case KeyAgreementMacType.HmacSha3D224:
            case KeyAgreementMacType.HmacSha3D256:
            case KeyAgreementMacType.HmacSha3D384:
            case KeyAgreementMacType.HmacSha3D512:
                ModeValues  modeValue  = ModeValues.SHA2;
                DigestSizes digestSize = DigestSizes.NONE;
                EnumMapping.GetHashFunctionOptions(parameters.KeyAgreementMacType, ref modeValue, ref digestSize);
                return(new NoKeyConfirmationHmac(_macDataCreator, parameters, _hmacFactory.GetHmacInstance(new HashFunction(modeValue, digestSize))));

            default:
                throw new ArgumentException($"{GetType().Name}, {nameof(parameters.KeyAgreementMacType)}");
            }
        }
        public IKeyConfirmation GetInstance(IKeyConfirmationParameters parameters)
        {
            switch (parameters.MacType)
            {
            case KeyAgreementMacType.AesCcm:
                ConfirmKeyLengthAesCcm(parameters.KeyLength);
                return(new KeyConfirmationAesCcm(
                           _macDataCreator,
                           parameters,
                           new CcmBlockCipher(new AesEngine(), new ModeBlockCipherFactory(), new AES_CCMInternals())));

            case KeyAgreementMacType.CmacAes:
                var cmacEnum = MapCmacEnum(parameters.KeyLength);

                return(new KeyConfirmationCmac(
                           _macDataCreator, parameters, _cmacFactory.GetCmacInstance(cmacEnum)));

            case KeyAgreementMacType.HmacSha1:
            case KeyAgreementMacType.HmacSha2D224:
            case KeyAgreementMacType.HmacSha2D256:
            case KeyAgreementMacType.HmacSha2D384:
            case KeyAgreementMacType.HmacSha2D512:
            case KeyAgreementMacType.HmacSha2D512_T224:
            case KeyAgreementMacType.HmacSha2D512_T256:
            case KeyAgreementMacType.HmacSha3D224:
            case KeyAgreementMacType.HmacSha3D256:
            case KeyAgreementMacType.HmacSha3D384:
            case KeyAgreementMacType.HmacSha3D512:
                ModeValues  modeValue  = ModeValues.SHA2;
                DigestSizes digestSize = DigestSizes.NONE;
                EnumMapping.GetHashFunctionOptions(parameters.MacType, ref modeValue, ref digestSize);

                return(new KeyConfirmationHmac(
                           _macDataCreator, parameters, _hmacFactory.GetHmacInstance(new HashFunction(modeValue, digestSize))));

            case KeyAgreementMacType.Kmac_128:
                return(new KeyConfirmationKmac(_macDataCreator, parameters, _kmacFactory, 256));

            case KeyAgreementMacType.Kmac_256:
                return(new KeyConfirmationKmac(_macDataCreator, parameters, _kmacFactory, 512));

            default:
                throw new ArgumentException($"{GetType().Name}, {nameof(parameters.MacType)}");
            }
        }
Beispiel #4
0
        public KdfResult Kdf(KdfParameterTwoStep param, BitString fixedInfo)
        {
            IMac     randomnessExtractionMac = null;
            MacModes keyExpansionMacMode     = param.MacMode;

            switch (param.MacMode)
            {
            case MacModes.CMAC_AES128:
                randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES128);
                break;

            case MacModes.CMAC_AES192:
                randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES192);
                keyExpansionMacMode     = MacModes.CMAC_AES128;
                break;

            case MacModes.CMAC_AES256:
                randomnessExtractionMac = _cmacFactory.GetCmacInstance(CmacTypes.AES256);
                keyExpansionMacMode     = MacModes.CMAC_AES128;
                break;

            case MacModes.HMAC_SHA1:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA1, DigestSizes.d160));
                break;

            case MacModes.HMAC_SHA224:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d224));
                break;

            case MacModes.HMAC_SHA256:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d256));
                break;

            case MacModes.HMAC_SHA384:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d384));
                break;

            case MacModes.HMAC_SHA512:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512));
                break;

            case MacModes.HMAC_SHA_d512t224:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t224));
                break;

            case MacModes.HMAC_SHA_d512t256:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA2, DigestSizes.d512t256));
                break;

            case MacModes.HMAC_SHA3_224:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d224));
                break;

            case MacModes.HMAC_SHA3_256:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d256));
                break;

            case MacModes.HMAC_SHA3_384:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d384));
                break;

            case MacModes.HMAC_SHA3_512:
                randomnessExtractionMac =
                    _hmacFactory.GetHmacInstance(new HashFunction(ModeValues.SHA3, DigestSizes.d512));
                break;

            default:
                throw new ArgumentException($"Invalid {nameof(MacModes)} provided to KdfVisitor.");
            }

            // Randomness extraction (step one)
            var randomnessExtraction = randomnessExtractionMac.Generate(param.Salt, param.Z);

            // Key Expansion (step two)
            var kdf = _kdfTwoStepFactory.GetKdfInstance(
                param.KdfMode,
                keyExpansionMacMode,
                param.CounterLocation,
                param.CounterLen);

            return(new KdfResult(kdf.DeriveKey(randomnessExtraction.Mac, fixedInfo, param.L, param.Iv, 0).DerivedKey));
        }
        public void ShouldMacCorrectly(int keyLength, int macLength, BitString dkm, BitString nonce, BitString expectedMacData, BitString expectedMac)
        {
            NoKeyConfirmationParameters p =
                new NoKeyConfirmationParameters(KeyAgreementMacType.CmacAes, macLength, dkm, nonce);

            _subject = new NoKeyConfirmationCmac(new NoKeyConfirmationMacDataCreator(), p, _algoFactory.GetCmacInstance(CmacTypes.AES128));
            var result = _subject.ComputeMac();

            Assert.That(result.Success, nameof(result.Success));
            Assert.AreEqual(expectedMacData.ToHex(), result.MacData.ToHex(), nameof(result.MacData));
            Assert.AreEqual(expectedMac.ToHex(), result.Mac.ToHex(), nameof(result.Mac));
        }