1 #ifndef CRYPTOPP_ELGAMAL_H
2 #define CRYPTOPP_ELGAMAL_H
7 NAMESPACE_BEGIN(CryptoPP)
14 void Derive(
const DL_GroupParameters<Integer> &groupParams, byte *derivedKey,
size_t derivedLength,
const Integer &agreedElement,
const Integer &ephemeralPublicKey,
const NameValuePairs &derivationParams)
const
16 agreedElement.
Encode(derivedKey, derivedLength);
19 size_t GetSymmetricKeyLength(
size_t plainTextLength)
const
21 return GetGroupParameters().GetModulus().ByteCount();
24 size_t GetSymmetricCiphertextLength(
size_t plainTextLength)
const
26 unsigned int len = GetGroupParameters().GetModulus().ByteCount();
27 if (plainTextLength <= GetMaxSymmetricPlaintextLength(len))
33 size_t GetMaxSymmetricPlaintextLength(
size_t cipherTextLength)
const
35 unsigned int len = GetGroupParameters().GetModulus().ByteCount();
36 if (cipherTextLength == len)
37 return STDMIN(255U, len-3);
44 const Integer &p = GetGroupParameters().GetModulus();
49 memcpy(block+modulusLen-2-plainTextLength, plainText, plainTextLength);
50 block[modulusLen-2] = (byte)plainTextLength;
52 a_times_b_mod_c(Integer(key, modulusLen), Integer(block, modulusLen-1), p).
Encode(cipherText, modulusLen);
55 DecodingResult SymmetricDecrypt(
const byte *key,
const byte *cipherText,
size_t cipherTextLength, byte *plainText,
const NameValuePairs ¶meters)
const
57 const Integer &p = GetGroupParameters().GetModulus();
60 if (cipherTextLength != modulusLen)
63 Integer m = a_times_b_mod_c(Integer(cipherText, modulusLen), Integer(key, modulusLen).InverseMod(p), p);
66 unsigned int plainTextLength = plainText[0];
67 if (plainTextLength > GetMaxSymmetricPlaintextLength(modulusLen))
70 m.
Encode(plainText, plainTextLength);
77 template <
class BASE,
class SCHEME_OPTIONS,
class KEY>
81 size_t FixedMaxPlaintextLength()
const {
return this->MaxPlaintextLength(FixedCiphertextLength());}
82 size_t FixedCiphertextLength()
const {
return this->CiphertextLength(0);}
87 {
return Decrypt(rng, cipherText, FixedCiphertextLength(), plainText);}
107 static const char * StaticAlgorithmName() {
return "ElgamalEnc/Crypto++Padding";}
109 typedef SchemeOptions::GroupParameters GroupParameters;