Fabcoin Core  0.16.2
P2P Digital Currency
elgamal.h
Go to the documentation of this file.
1 // elgamal.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_ELGAMAL_H
7 #define CRYPTOPP_ELGAMAL_H
8 
9 #include "cryptlib.h"
10 #include "modexppc.h"
11 #include "integer.h"
12 #include "gfpcrypt.h"
13 #include "pubkey.h"
14 #include "dsa.h"
15 #include "misc.h"
16 
18 
23  public DL_KeyDerivationAlgorithm<Integer>,
25 {
26 public:
27  virtual ~ElGamalBase() {}
28 
29  void Derive(const DL_GroupParameters<Integer> &groupParams, byte *derivedKey, size_t derivedLength, const Integer &agreedElement, const Integer &ephemeralPublicKey, const NameValuePairs &derivationParams) const
30  {
31  CRYPTOPP_UNUSED(groupParams), CRYPTOPP_UNUSED(ephemeralPublicKey), CRYPTOPP_UNUSED(derivationParams);
32  agreedElement.Encode(derivedKey, derivedLength);
33  }
34 
35  size_t GetSymmetricKeyLength(size_t plainTextLength) const
36  {
37  CRYPTOPP_UNUSED(plainTextLength);
38  return GetGroupParameters().GetModulus().ByteCount();
39  }
40 
41  size_t GetSymmetricCiphertextLength(size_t plainTextLength) const
42  {
43  unsigned int len = GetGroupParameters().GetModulus().ByteCount();
44  if (plainTextLength <= GetMaxSymmetricPlaintextLength(len))
45  return len;
46  else
47  return 0;
48  }
49 
50  size_t GetMaxSymmetricPlaintextLength(size_t cipherTextLength) const
51  {
52  unsigned int len = GetGroupParameters().GetModulus().ByteCount();
53  if (cipherTextLength == len)
54  return STDMIN(255U, len-3);
55  else
56  return 0;
57  }
58 
59  void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plainText, size_t plainTextLength, byte *cipherText, const NameValuePairs &parameters) const
60  {
61  CRYPTOPP_UNUSED(parameters);
62  const Integer &p = GetGroupParameters().GetModulus();
63  unsigned int modulusLen = p.ByteCount();
64 
65  SecByteBlock block(modulusLen-1);
66  rng.GenerateBlock(block, modulusLen-2-plainTextLength);
67  memcpy(block+modulusLen-2-plainTextLength, plainText, plainTextLength);
68  block[modulusLen-2] = (byte)plainTextLength;
69 
70  a_times_b_mod_c(Integer(key, modulusLen), Integer(block, modulusLen-1), p).Encode(cipherText, modulusLen);
71  }
72 
73  DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, size_t cipherTextLength, byte *plainText, const NameValuePairs &parameters) const
74  {
75  CRYPTOPP_UNUSED(parameters);
76  const Integer &p = GetGroupParameters().GetModulus();
77  unsigned int modulusLen = p.ByteCount();
78 
79  if (cipherTextLength != modulusLen)
80  return DecodingResult();
81 
82  Integer m = a_times_b_mod_c(Integer(cipherText, modulusLen), Integer(key, modulusLen).InverseMod(p), p);
83 
84  m.Encode(plainText, 1);
85  unsigned int plainTextLength = plainText[0];
86  if (plainTextLength > GetMaxSymmetricPlaintextLength(modulusLen))
87  return DecodingResult();
88  m >>= 8;
89  m.Encode(plainText, plainTextLength);
90  return DecodingResult(plainTextLength);
91  }
92 
93  virtual const DL_GroupParameters_GFP & GetGroupParameters() const =0;
94 };
95 
99 template <class BASE, class SCHEME_OPTIONS, class KEY>
100 class ElGamalObjectImpl : public DL_ObjectImplBase<BASE, SCHEME_OPTIONS, KEY>, public ElGamalBase
101 {
102 public:
103  virtual ~ElGamalObjectImpl() {}
104 
105  size_t FixedMaxPlaintextLength() const {return this->MaxPlaintextLength(FixedCiphertextLength());}
106  size_t FixedCiphertextLength() const {return this->CiphertextLength(0);}
107 
108  const DL_GroupParameters_GFP & GetGroupParameters() const {return this->GetKey().GetGroupParameters();}
109 
110  DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const
111  {return Decrypt(rng, cipherText, FixedCiphertextLength(), plainText);}
112 
113 protected:
117 };
118 
122 {
126 };
127 
131 struct ElGamal
132 {
134 
135  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ElgamalEnc/Crypto++Padding";}
136 
142 };
143 
146 
148 
149 #endif
PK_FinalTemplate< ElGamalObjectImpl< DL_EncryptorBase< Integer >, SchemeOptions, SchemeOptions::PublicKey > > Encryptor
implements PK_Encryptor interface
Definition: elgamal.h:139
Keys::PrivateKey PrivateKey
Definition: pubkey.h:1711
const KEY & GetKey() const
Definition: pubkey.h:1760
uint8_t byte
Definition: Common.h:57
Diffie-Hellman key agreement algorithm.
Definition: pubkey.h:1933
size_t GetSymmetricKeyLength(size_t plainTextLength) const
Definition: elgamal.h:35
Utility functions for the Crypto++ library.
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
GF(p) group parameters.
Definition: gfpcrypt.h:158
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3369
ElGamal key agreement and encryption schemes default implementation.
Definition: elgamal.h:100
Integer a_times_b_mod_c(const Integer &x, const Integer &y, const Integer &m)
Definition: integer.cpp:4354
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:326
DL_CryptoKeys_GFP::GroupParameters GroupParameters
Definition: elgamal.h:123
ElGamal::Encryptor ElGamalEncryptor
Definition: elgamal.h:144
DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, size_t cipherTextLength, byte *plainText, const NameValuePairs &parameters) const
Definition: elgamal.h:73
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
SchemeOptions::GroupParameters GroupParameters
Definition: elgamal.h:137
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:116
Abstract base classes that provide a uniform interface to this library.
ElGamal::Decryptor ElGamalDecryptor
Definition: elgamal.h:145
Interface for key derivation algorithms used in DL cryptosystems.
Definition: pubkey.h:1304
Interface for random number generators.
Definition: cryptlib.h:1188
void Derive(const DL_GroupParameters< Integer > &groupParams, byte *derivedKey, size_t derivedLength, const Integer &agreedElement, const Integer &ephemeralPublicKey, const NameValuePairs &derivationParams) const
Definition: elgamal.h:29
ElGamal key agreement and encryption schemes keys.
Definition: elgamal.h:121
Discrete Log (DL) crypto scheme options.
Definition: pubkey.h:1736
const DL_SymmetricEncryptionAlgorithm & GetSymmetricEncryptionAlgorithm() const
Definition: elgamal.h:116
ElGamal encryption scheme with non-standard padding.
Definition: elgamal.h:131
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: elgamal.h:135
size_t GetMaxSymmetricPlaintextLength(size_t cipherTextLength) const
Definition: elgamal.h:50
Returns a decoding results.
Definition: cryptlib.h:238
const DL_KeyAgreementAlgorithm< Integer > & GetKeyAgreementAlgorithm() const
Definition: elgamal.h:114
Discrete Log (DL) public key in GF(p) groups.
Definition: gfpcrypt.h:593
size_t FixedCiphertextLength() const
Definition: elgamal.h:106
const DL_KeyDerivationAlgorithm< Integer > & GetKeyDerivationAlgorithm() const
Definition: elgamal.h:115
SCHEME_OPTIONS SchemeOptions
Definition: pubkey.h:1751
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Keys::PublicKey PublicKey
Definition: pubkey.h:1712
Discrete Log (DL) base object implementation.
Definition: pubkey.h:1748
virtual ~ElGamalBase()
Definition: elgamal.h:27
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
Classes for the DSA signature algorithm.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:477
size_t GetSymmetricCiphertextLength(size_t plainTextLength) const
Definition: elgamal.h:41
#define CRYPTOPP_NO_VTABLE
Definition: config.h:369
size_t FixedMaxPlaintextLength() const
Definition: elgamal.h:105
DL_PrivateKey_GFP_OldFormat< DL_CryptoKeys_GFP::PrivateKey > PrivateKey
Definition: elgamal.h:124
DL_PublicKey_GFP_OldFormat< DL_CryptoKeys_GFP::PublicKey > PublicKey
Definition: elgamal.h:125
Discrete Log (DL) private key in GF(p) groups.
Definition: gfpcrypt.h:638
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
uint8_t byte
Definition: Common.h:10
Multiple precision integer with arithmetic operations.
virtual ~ElGamalObjectImpl()
Definition: elgamal.h:103
ElGamal key agreement and encryption schemes base class.
Definition: elgamal.h:22
DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const
Definition: elgamal.h:110
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plainText, size_t plainTextLength, byte *cipherText, const NameValuePairs &parameters) const
Definition: elgamal.h:59
Interface for symmetric encryption algorithms used in DL cryptosystems.
Definition: pubkey.h:1315
DL_CryptoSchemeOptions< ElGamal, ElGamalKeys, int, int, int > SchemeOptions
Definition: elgamal.h:133
PK_FinalTemplate< ElGamalObjectImpl< DL_DecryptorBase< Integer >, SchemeOptions, SchemeOptions::PrivateKey > > Decryptor
implements PK_Decryptor interface
Definition: elgamal.h:141
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
Definition: integer.cpp:3296
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:1989
const DL_GroupParameters_GFP & GetGroupParameters() const
Definition: elgamal.h:108