예제 #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");
            }
        }
예제 #2
0
        public void ShouldCorrectlyDeriveKey(MacModes mac, CounterLocations ctrLocation, int rLen, int outLen, string kI, string fixedInput, string kO)
        {
            var keyIn          = new BitString(kI);
            var fixedInputData = new BitString(fixedInput);
            var expectedKey    = new BitString(kO);

            var kdf = _factory.GetKdfInstance(KdfModes.Pipeline, mac, ctrLocation, rLen);

            var result = kdf.DeriveKey(keyIn, fixedInputData, outLen);

            Assert.IsTrue(result.Success, result.ErrorMessage);
            Assert.AreEqual(expectedKey, result.DerivedKey);
        }
예제 #3
0
        public static int GetMacOutputLength(MacModes macMode)
        {
            switch (macMode)
            {
            case MacModes.CMAC_AES128:
            case MacModes.CMAC_AES192:
            case MacModes.CMAC_AES256:
                return(128);

            case MacModes.CMAC_TDES:
                return(64);

            case MacModes.HMAC_SHA1:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA1, DigestSizes.d160).outputLen);

            case MacModes.HMAC_SHA224:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d224).outputLen);

            case MacModes.HMAC_SHA256:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d256).outputLen);

            case MacModes.HMAC_SHA384:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d384).outputLen);

            case MacModes.HMAC_SHA512:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d512).outputLen);

            case MacModes.HMAC_SHA_d512t224:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d512t224).outputLen);

            case MacModes.HMAC_SHA_d512t256:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA2, DigestSizes.d512t256).outputLen);

            case MacModes.HMAC_SHA3_224:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA3, DigestSizes.d224).outputLen);

            case MacModes.HMAC_SHA3_256:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA3, DigestSizes.d256).outputLen);

            case MacModes.HMAC_SHA3_384:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA3, DigestSizes.d384).outputLen);

            case MacModes.HMAC_SHA3_512:
                return(ShaAttributes.GetShaAttributes(ModeValues.SHA3, DigestSizes.d512).outputLen);
            }

            throw new ArgumentException(nameof(macMode));
        }
예제 #4
0
        public IKdf GetKdfInstance(KdfModes kdfMode, MacModes macMode, CounterLocations counterLocation, int counterLength = 0)
        {
            var mac = GetMacInstance(macMode);

            switch (kdfMode)
            {
            case KdfModes.Counter:
                return(new CounterKdf(mac, counterLocation, counterLength));

            case KdfModes.Feedback:
                return(new FeedbackKdf(mac, counterLocation, counterLength));

            case KdfModes.Pipeline:
                return(new PipelineKdf(mac, counterLocation, counterLength));

            default:
                throw new ArgumentException("KDF Mode not supported");
            }
        }
예제 #5
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));
        }
예제 #6
0
        public void ShouldReturnProperMacInstance(MacModes macType, Type expectedType)
        {
            var result = _subject.GetMacInstance(macType);

            Assert.IsInstanceOf(expectedType, result);
        }
예제 #7
0
        public void ShouldCorrectlyDeriveKeyNoTestNameMessingUpTestRunner(string label, MacModes mac, CounterLocations ctrLocation, int rLen, int outLen, string kI, string fixedInput, string kO, int breakLocation = 0)
        {
            var keyIn          = new BitString(kI);
            var fixedInputData = new BitString(fixedInput);
            var expectedKey    = new BitString(kO);

            var kdf = _factory.GetKdfInstance(KdfModes.Counter, mac, ctrLocation, rLen);

            var result = kdf.DeriveKey(keyIn, fixedInputData, outLen, breakLocation: breakLocation);

            Assert.IsTrue(result.Success, result.ErrorMessage);
            Assert.AreEqual(expectedKey, result.DerivedKey);
        }