void Redirector::ChannelInitialize(const std::string &channel, const NameValuePairs ¶meters, int propagation) { if (channel.empty()) { m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL); m_passSignal = parameters.GetValueWithDefault("PassSignal", true); } if (m_target && m_passSignal) m_target->ChannelInitialize(channel, parameters, propagation); }
void HexEncoder::IsolatedInitialize(const NameValuePairs ¶meters) { bool uppercase = parameters.GetValueWithDefault(Name::Uppercase(), true); m_filter->Initialize(CombinedNameValuePairs( parameters, MakeParameters(Name::EncodingLookupArray(), uppercase ? &s_vecUpper[0] : &s_vecLower[0], false)(Name::Log2Base(), 4, true))); }
void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { int modulusSize = 2048; alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize); ASSERT( modulusSize >= 16 ); m_e = alg.GetValueWithDefault("PublicExponent", Integer(17)); ASSERT( m_e >= 3 ); ASSERT( !m_e.IsEven() ); RSAPrimeSelector selector(m_e); const NameValuePairs &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize) ("PointerToPrimeSelector", selector.GetSelectorPointer()); m_p.GenerateRandom(rng, primeParam); m_q.GenerateRandom(rng, primeParam); m_d = EuclideanMultiplicativeInverse(m_e, LCM(m_p-1, m_q-1)); assert(m_d.IsPositive()); m_dp = m_d % (m_p-1); m_dq = m_d % (m_q-1); m_n = m_p * m_q; m_u = m_q.InverseMod(m_p); }
void BaseN_Encoder::IsolatedInitialize(const NameValuePairs ¶meters) { parameters.GetRequiredParameter("BaseN_Encoder", "EncodingLookupArray", m_alphabet); parameters.GetRequiredIntParameter("BaseN_Encoder", "Log2Base", m_bitsPerChar); if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8) throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive"); byte padding; bool pad; if (parameters.GetValue("PaddingByte", padding)) pad = parameters.GetValueWithDefault("Pad", true); else pad = false; m_padding = pad ? padding : -1; m_bytePos = m_bitPos = 0; int i = 8; while (i%m_bitsPerChar != 0) i += 8; m_outputBlockSize = i/m_bitsPerChar; m_outBuf.New(m_outputBlockSize); }
void DL_GroupParameters_DSA::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { Integer p, q, g; if (alg.GetValue("Modulus", p) && alg.GetValue("SubgroupGenerator", g)) { q = alg.GetValueWithDefault("SubgroupOrder", ComputeGroupOrder(p)/2); } else { int modulusSize = 1024; alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize); if (!DSA::IsValidPrimeLength(modulusSize)) throw InvalidArgument("DSA: not a valid prime length"); SecByteBlock seed(SHA::DIGESTSIZE); Integer h; int c; do { rng.GenerateBlock(seed, SHA::DIGESTSIZE); } while (!DSA::GeneratePrimes(seed, SHA::DIGESTSIZE*8, c, p, modulusSize, q)); do { h.Randomize(rng, 2, p-2); g = a_exp_b_mod_c(h, (p-1)/q, p); } while (g <= 1); } Initialize(p, q, g); }
void DL_GroupParameters_DSA::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { Integer p, q, g; if (alg.GetValue("Modulus", p) && alg.GetValue("SubgroupGenerator", g)) { q = alg.GetValueWithDefault("SubgroupOrder", ComputeGroupOrder(p)/2); Initialize(p, q, g); } else { int modulusSize = 1024, defaultSubgroupOrderSize; alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize); switch (modulusSize) { case 1024: defaultSubgroupOrderSize = 160; break; case 2048: defaultSubgroupOrderSize = 224; break; case 3072: defaultSubgroupOrderSize = 256; break; default: throw InvalidArgument("DSA: not a valid prime length"); } DL_GroupParameters_GFP::GenerateRandom(rng, CombinedNameValuePairs(alg, MakeParameters(Name::SubgroupOrderSize(), defaultSubgroupOrderSize, false))); } }
void Redirector::Initialize(const NameValuePairs ¶meters, int propagation) { m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL); m_behavior = parameters.GetIntValueWithDefault("RedirectionBehavior", PASS_EVERYTHING); if (m_target && GetPassSignals()) m_target->Initialize(parameters, propagation); }
void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize) { m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS); m_messageAccumulator.reset(m_verifier.NewVerificationAccumulator()); unsigned int size = m_verifier.SignatureLength(); m_verified = false; firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0; blockSize = 1; lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size; }
void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize) { m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS); m_hashModule.Restart(); unsigned int size = m_hashModule.DigestSize(); m_verified = false; firstSize = m_flags & HASH_AT_BEGIN ? size : 0; blockSize = 1; lastSize = m_flags & HASH_AT_BEGIN ? 0 : size; }
void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize) { m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS); int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1); m_digestSize = s < 0 ? m_hashModule.DigestSize() : s; m_verified = false; firstSize = m_flags & HASH_AT_BEGIN ? m_digestSize : 0; blockSize = 1; lastSize = m_flags & HASH_AT_BEGIN ? 0 : m_digestSize; }
void AuthenticatedDecryptionFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize) { word32 flags = parameters.GetValueWithDefault(Name::AuthenticatedDecryptionFilterFlags(), (word32)DEFAULT_FLAGS); m_hashVerifier.Initialize(CombinedNameValuePairs(parameters, MakeParameters(Name::HashVerificationFilterFlags(), flags))); m_streamFilter.Initialize(parameters); firstSize = m_hashVerifier.m_firstSize; blockSize = 1; lastSize = m_hashVerifier.m_lastSize; }
void Base64Encoder::IsolatedInitialize(const NameValuePairs ¶meters) { bool insertLineBreaks = parameters.GetValueWithDefault(Name::InsertLineBreaks(), true); int maxLineLength = parameters.GetIntValueWithDefault(Name::MaxLineLength(), 72); const char *lineBreak = insertLineBreaks ? "\n" : ""; m_filter->Initialize(CombinedNameValuePairs( parameters, MakeParameters(Name::EncodingLookupArray(), &s_vec[0], false) (Name::PaddingByte(), s_padding) (Name::GroupSize(), insertLineBreaks ? maxLineLength : 0) (Name::Separator(), ConstByteArrayParameter(lineBreak)) (Name::Terminator(), ConstByteArrayParameter(lineBreak)) (Name::Log2Base(), 6, true))); }
void StreamTransformationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize) { BlockPaddingScheme padding = parameters.GetValueWithDefault(Name::BlockPaddingScheme(), DEFAULT_PADDING); bool isBlockCipher = (m_cipher.MandatoryBlockSize() > 1 && m_cipher.MinLastBlockSize() == 0); if (padding == DEFAULT_PADDING) m_padding = isBlockCipher ? PKCS_PADDING : NO_PADDING; else m_padding = padding; if (!isBlockCipher && (m_padding == PKCS_PADDING || m_padding == ONE_AND_ZEROS_PADDING)) throw InvalidArgument("StreamTransformationFilter: PKCS_PADDING and ONE_AND_ZEROS_PADDING cannot be used with " + m_cipher.AlgorithmName()); firstSize = 0; blockSize = m_cipher.MandatoryBlockSize(); lastSize = LastBlockSize(m_cipher, m_padding); }
void FileSink::IsolatedInitialize(const NameValuePairs ¶meters) { const char *fileName; if (parameters.GetValue("OutputFileName", fileName)) { ios::openmode binary = parameters.GetValueWithDefault("OutputBinaryMode", true) ? ios::binary : ios::openmode(0); m_file.open(fileName, ios::out | ios::trunc | binary); if (!m_file) throw OpenErr(fileName); m_stream = &m_file; } else { m_stream = NULL; parameters.GetValue("OutputStreamPointer", m_stream); } }
void FileStore::StoreInitialize(const NameValuePairs ¶meters) { const char *fileName; if (parameters.GetValue("InputFileName", fileName)) { ios::openmode binary = parameters.GetValueWithDefault("InputBinaryMode", true) ? ios::binary : ios::openmode(0); m_file.open(fileName, ios::in | binary); if (!m_file) throw OpenErr(fileName); m_stream = &m_file; } else { m_stream = NULL; parameters.GetValue("InputStreamPointer", m_stream); } m_waiting = false; }
void FileSink::IsolatedInitialize(const NameValuePairs ¶meters) { m_file.reset(new std::ofstream); const char *fileName; if (parameters.GetValue(Name::OutputFileName(), fileName)) { ios::openmode binary = parameters.GetValueWithDefault(Name::OutputBinaryMode(), true) ? ios::binary : ios::openmode(0); m_file->open(fileName, ios::out | ios::trunc | binary); if (!*m_file) throw OpenErr(fileName); m_stream = m_file.get(); } else { m_stream = NULL; parameters.GetValue(Name::OutputStreamPointer(), m_stream); } }
void InvertibleESIGNFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶m) { int modulusSize = 1023*2; param.GetIntValue("ModulusSize", modulusSize) || param.GetIntValue("KeySize", modulusSize); if (modulusSize < 24) throw InvalidArgument("InvertibleESIGNFunction: specified modulus size is too small"); if (modulusSize % 3 != 0) throw InvalidArgument("InvertibleESIGNFunction: modulus size must be divisible by 3"); m_e = param.GetValueWithDefault("PublicExponent", Integer(32)); if (m_e < 8) throw InvalidArgument("InvertibleESIGNFunction: public exponents less than 8 may not be secure"); // VC70 workaround: putting these after primeParam causes overlapped stack allocation ConstByteArrayParameter seedParam; SecByteBlock seed; const Integer minP = Integer(204) << (modulusSize/3-8); const Integer maxP = Integer::Power2(modulusSize/3)-1; AlgorithmParameters primeParam = MakeParameters("Min", minP)("Max", maxP)("RandomNumberType", Integer::PRIME); if (param.GetValue("Seed", seedParam)) { seed.resize(seedParam.size() + 4); memcpy(seed + 4, seedParam.begin(), seedParam.size()); PutWord(false, BIG_ENDIAN_ORDER, seed, (word32)0); m_p.GenerateRandom(rng, CombinedNameValuePairs(primeParam, MakeParameters("Seed", ConstByteArrayParameter(seed)))); PutWord(false, BIG_ENDIAN_ORDER, seed, (word32)1); m_q.GenerateRandom(rng, CombinedNameValuePairs(primeParam, MakeParameters("Seed", ConstByteArrayParameter(seed)))); } else { m_p.GenerateRandom(rng, primeParam); m_q.GenerateRandom(rng, primeParam); } m_n = m_p * m_p * m_q; CRYPTOPP_ASSERT(m_n.BitCount() == (unsigned int)modulusSize); }
void FileStore::StoreInitialize(const NameValuePairs ¶meters) { m_file.reset(new std::ifstream); const char *fileName; if (parameters.GetValue(Name::InputFileName(), fileName)) { ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? ios::binary : ios::openmode(0); m_file->open(fileName, ios::in | binary); if (!*m_file) throw OpenErr(fileName); m_stream = m_file.get(); } else { m_stream = NULL; parameters.GetValue(Name::InputStreamPointer(), m_stream); } m_waiting = false; }
void DL_GroupParameters_EC<EC>::AssignFrom(const NameValuePairs &source) { OID oid; if (source.GetValue(Name::GroupOID(), oid)) Initialize(oid); else { EllipticCurve ec; Point G; Integer n; source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::Curve(), ec); source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::SubgroupGenerator(), G); source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::SubgroupOrder(), n); Integer k = source.GetValueWithDefault(Name::Cofactor(), Integer::Zero()); Initialize(ec, G, n, k); } }
void FileStore::StoreInitialize(const NameValuePairs ¶meters) { m_waiting = false; m_stream = NULL; m_file.release(); const char *fileName = NULL; #if defined(CRYPTOPP_UNIX_AVAILABLE) || _MSC_VER >= 1400 const wchar_t *fileNameWide = NULL; if (!parameters.GetValue(Name::InputFileNameWide(), fileNameWide)) #endif if (!parameters.GetValue(Name::InputFileName(), fileName)) { parameters.GetValue(Name::InputStreamPointer(), m_stream); return; } std::ios::openmode binary = parameters.GetValueWithDefault(Name::InputBinaryMode(), true) ? std::ios::binary : std::ios::openmode(0); m_file.reset(new std::ifstream); #ifdef CRYPTOPP_UNIX_AVAILABLE std::string narrowed; if (fileNameWide) fileName = (narrowed = StringNarrow(fileNameWide)).c_str(); #endif #if _MSC_VER >= 1400 if (fileNameWide) { m_file->open(fileNameWide, std::ios::in | binary); if (!*m_file) throw OpenErr(StringNarrow(fileNameWide, false)); } #endif if (fileName) { m_file->open(fileName, std::ios::in | binary); if (!*m_file) throw OpenErr(fileName); } m_stream = m_file.get(); }
void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { int modulusSize = 2048; alg.GetIntValue(Name::ModulusSize(), modulusSize) || alg.GetIntValue(Name::KeySize(), modulusSize); assert(modulusSize >= 16); if (modulusSize < 16) throw InvalidArgument("InvertibleRSAFunction: specified modulus size is too small"); m_e = alg.GetValueWithDefault(Name::PublicExponent(), Integer(17)); assert(m_e >= 3); assert(!m_e.IsEven()); if (m_e < 3 || m_e.IsEven()) throw InvalidArgument("InvertibleRSAFunction: invalid public exponent"); RSAPrimeSelector selector(m_e); AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize) (Name::PointerToPrimeSelector(), selector.GetSelectorPointer()); m_p.GenerateRandom(rng, primeParam); m_q.GenerateRandom(rng, primeParam); m_d = m_e.InverseMod(LCM(m_p-1, m_q-1)); assert(m_d.IsPositive()); m_dp = m_d % (m_p-1); m_dq = m_d % (m_q-1); m_n = m_p * m_q; m_u = m_q.InverseMod(m_p); if (FIPS_140_2_ComplianceEnabled()) { RSASS<PKCS1v15, SHA>::Signer signer(*this); RSASS<PKCS1v15, SHA>::Verifier verifier(signer); SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier); RSAES<OAEP<SHA> >::Decryptor decryptor(*this); RSAES<OAEP<SHA> >::Encryptor encryptor(decryptor); EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor); } }
void InvertibleLUCFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg) { int modulusSize = 2048; alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize); if (modulusSize < 16) throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small"); m_e = alg.GetValueWithDefault("PublicExponent", Integer(17)); if (m_e < 5 || m_e.IsEven()) throw InvalidArgument("InvertibleLUCFunction: invalid public exponent"); LUCPrimeSelector selector(m_e); AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize) ("PointerToPrimeSelector", selector.GetSelectorPointer()); m_p.GenerateRandom(rng, primeParam); m_q.GenerateRandom(rng, primeParam); m_n = m_p * m_q; m_u = m_q.InverseMod(m_p); }
void HashFilter::IsolatedInitialize(const NameValuePairs ¶meters) { m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false); m_hashModule.Restart(); }
void SecretSharing::IsolatedInitialize(const NameValuePairs ¶meters) { m_pad = parameters.GetValueWithDefault("AddPadding", true); m_ida.IsolatedInitialize(parameters); }
void SignerFilter::IsolatedInitialize(const NameValuePairs ¶meters) { m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false); m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng)); }
void SecretRecovery::IsolatedInitialize(const NameValuePairs ¶meters) { m_pad = parameters.GetValueWithDefault("RemovePadding", true); RawIDA::IsolatedInitialize(CombinedNameValuePairs(parameters, MakeParameters("OutputChannelID", (word32)0xffffffff))); }
void HashFilter::IsolatedInitialize(const NameValuePairs ¶meters) { m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false); int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1); m_digestSize = s < 0 ? m_hashModule.DigestSize() : s; }
void InformationDispersal::IsolatedInitialize(const NameValuePairs ¶meters) { m_nextChannel = 0; m_pad = parameters.GetValueWithDefault("AddPadding", true); m_ida.IsolatedInitialize(parameters); }
void InformationRecovery::IsolatedInitialize(const NameValuePairs ¶meters) { m_pad = parameters.GetValueWithDefault("RemovePadding", true); RawIDA::IsolatedInitialize(parameters); }