public KdfMultiExpansionResult Kdf(KdfMultiExpansionParameterTwoStep param)
        {
            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);

            var kdfResults = new List <KdfResult>();

            foreach (var iterationParameter in param.IterationParameters)
            {
                kdfResults.Add(kdf.DeriveKey(randomnessExtraction.Mac, iterationParameter.FixedInfo, iterationParameter.L, param.Iv, 0));
            }

            return(new KdfMultiExpansionResult(kdfResults));
        }
示例#2
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");
            }
        }