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;
PK_FinalTemplate< ElGamalObjectImpl< DL_EncryptorBase< Integer >, SchemeOptions, SchemeOptions::PublicKey > > Encryptor
implements PK_Encryptor interface
DH key agreement algorithm.
GF(p) group parameters that default to same primes.
interface for key derivation algorithms used in DL cryptosystems
interface for random number generators
ElGamal encryption scheme with non-standard padding.
used to return decoding results
multiple precision integer and basic arithmetics
virtual void GenerateBlock(byte *output, size_t size)
generate random array of bytes
void Encode(byte *output, size_t outputLen, Signedness=UNSIGNED) const
encode in big-endian format
interface for symmetric encryption algorithms used in DL cryptosystems
PK_FinalTemplate< ElGamalObjectImpl< DL_DecryptorBase< Integer >, SchemeOptions, SchemeOptions::PrivateKey > > Decryptor
implements PK_Decryptor interface
unsigned int ByteCount() const
number of significant bytes = ceiling(BitCount()/8)
interface for retrieving values given their names
A template implementing constructors for public key algorithm classes.