Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
 protected BitString PseudoRandomFunction(BitString key, BitString data)
 {
     return(Mac.Generate(key, data).Mac);
 }