Crypto++ 8.9 Free C++ class library of cryptographic schemes

dh2.cpp
1 // dh2.cpp - originally written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4 #include "cryptlib.h"
5 #include "misc.h"
6 #include "dh2.h"
7
8 NAMESPACE_BEGIN(CryptoPP)
9
10 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
11 struct NullCryptoParameters : public CryptoParameters
12 {
13 void AssignFrom(const NameValuePairs &source) {
14 CRYPTOPP_UNUSED(source);
15 }
16 bool Validate(RandomNumberGenerator &rng, unsigned int level) const {
17 CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(level);
18 return false;
19 }
20 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const {
21 CRYPTOPP_UNUSED(name); CRYPTOPP_UNUSED(valueType); CRYPTOPP_UNUSED(pValue);
22 return false;
23 }
24 };
25
26 struct NullSimpleKeyAgreementDomain : public TwoBases<NullCryptoParameters, SimpleKeyAgreementDomain>
27 {
28 CryptoParameters & AccessCryptoParameters() {
29 return *this;
30 }
31 unsigned int AgreedValueLength() const {
32 return 1;
33 }
34 unsigned int PrivateKeyLength() const {
35 return 1;
36 }
37 unsigned int PublicKeyLength() const {
38 return 1;
39 }
40 void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const {
41 CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey);
42 }
43 void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const {
44 CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey); CRYPTOPP_UNUSED(publicKey);
45 }
46 bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const {
47 CRYPTOPP_UNUSED(agreedValue); CRYPTOPP_UNUSED(privateKey);
48 CRYPTOPP_UNUSED(otherPublicKey); CRYPTOPP_UNUSED(validateOtherPublicKey);
49 return false;
50 }
51 };
52
53 void DH2_TestInstantiations()
54 {
55 NullSimpleKeyAgreementDomain dom;
56 DH2 dh(dom);
57 }
58 #endif
59
60 bool DH2::Agree(byte *agreedValue,
61 const byte *staticSecretKey, const byte *ephemeralSecretKey,
62 const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
63 bool validateStaticOtherPublicKey) const
64 {
65 return d1.Agree(agreedValue, staticSecretKey, staticOtherPublicKey, validateStaticOtherPublicKey)
66 && d2.Agree(agreedValue+d1.AgreedValueLength(), ephemeralSecretKey, ephemeralOtherPublicKey, true);
67 }
68
69 NAMESPACE_END
CryptoMaterial::AssignFrom
virtual void AssignFrom(const NameValuePairs &source)=0
Assign values to this object.
CryptoMaterial::Validate
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
CryptoParameters
Interface for crypto parameters.
Definition: cryptlib.h:2551
DH2
Unified Diffie-Hellman in GF(p)
Definition: dh2.h:21
DH2::Agree
bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const
Derive agreed value.
Definition: dh2.cpp:60
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
NameValuePairs::GetVoidValue
virtual CRYPTOPP_DLL bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1440
SimpleKeyAgreementDomain::Agree
virtual bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const =0
Derive agreed value.
SimpleKeyAgreementDomain::AgreedValueLength
virtual unsigned int AgreedValueLength() const =0
Provides the size of the agreed value.
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
dh2.h
Classes for Unified Diffie-Hellman key exchange.
misc.h
Utility functions for the Crypto++ library.
CryptoPP
Crypto++ library namespace.
pch.h
Precompiled header file.