xtrcrypt.cpp
// xtrcrypt.cpp - originally written and placed in the public domain by Wei Dai
#include "pch.h"
#include "asn.h"
#include "integer.h"
#include "xtrcrypt.h"
#include "nbtheory.h"
#include "modarith.h"
#include "argnames.h"
NAMESPACE_BEGIN(CryptoPP)
XTR_DH::XTR_DH(const Integer &p, const Integer &q, const GFP2Element &g)
: m_p(p), m_q(q), m_g(g)
{
}
XTR_DH::XTR_DH(RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
{
XTR_FindPrimesAndGenerator(rng, m_p, m_q, m_g, pbits, qbits);
}
XTR_DH::XTR_DH(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
m_p.BERDecode(seq);
m_q.BERDecode(seq);
m_g.c1.BERDecode(seq);
m_g.c2.BERDecode(seq);
seq.MessageEnd();
}
void XTR_DH::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
m_p.DEREncode(seq);
m_q.DEREncode(seq);
m_g.c1.DEREncode(seq);
m_g.c2.DEREncode(seq);
seq.MessageEnd();
}
bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = true;
pass = pass && m_p > Integer::One() && m_p.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd();
CRYPTOPP_ASSERT(pass);
GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
CRYPTOPP_ASSERT(pass);
pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
CRYPTOPP_ASSERT(pass);
if (level >= 1)
{
pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
CRYPTOPP_ASSERT(pass);
}
if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
CRYPTOPP_ASSERT(pass);
pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
CRYPTOPP_ASSERT(pass);
}
return pass;
}
bool XTR_DH::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupOrder)
CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupGenerator)
;
}
void XTR_DH::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupGenerator)
;
}
void XTR_DH::GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
{
Integer x(rng, Integer::Zero(), m_q-1);
x.Encode(privateKey, PrivateKeyLength());
}
void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
{
CRYPTOPP_UNUSED(rng);
Integer x(privateKey, PrivateKeyLength());
GFP2Element y = XTR_Exponentiate(m_g, x, m_p);
y.Encode(publicKey, PublicKeyLength());
}
bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
{
GFP2Element w(otherPublicKey, PublicKeyLength());
if (validateOtherPublicKey)
{
GFP2_ONB<ModularArithmetic> gfp2(m_p);
GFP2Element three = gfp2.ConvertIn(3);
if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
return false;
if (XTR_Exponentiate(w, m_q, m_p) != three)
return false;
}
Integer s(privateKey, PrivateKeyLength());
GFP2Element z = XTR_Exponentiate(w, s, m_p);
z.Encode(agreedValue, AgreedValueLength());
return true;
}
NAMESPACE_END
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
asn.h
Classes and functions for working with ANS.1 objects.
BERSequenceDecoder
BER Sequence Decoder.
Definition: asn.h:525
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1652
DERSequenceEncoder
DER Sequence Encoder.
Definition: asn.h:557
GFP2_ONB
GF(p^2), optimal normal basis.
Definition: xtr.h:47
GFP2Element
an element of GF(p^2)
Definition: xtr.h:17
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Integer::Squared
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:631
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Integer::One
static const Integer & One()
Integer representing 1.
Integer::Zero
static const Integer & Zero()
Integer representing 0.
Integer::IsNegative
bool IsNegative() const
Determines if the Integer is negative.
Definition: integer.h:339
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:354
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:322
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1435
XTR_DH::Agree
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Derive agreed value.
Definition: xtrcrypt.cpp:104
XTR_DH::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: xtrcrypt.cpp:44
XTR_DH::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: xtrcrypt.cpp:72
XTR_DH::AgreedValueLength
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition: xtrcrypt.h:30
XTR_DH::PublicKeyLength
unsigned int PublicKeyLength() const
Provides the size of the public key.
Definition: xtrcrypt.h:32
XTR_DH::PrivateKeyLength
unsigned int PrivateKeyLength() const
Provides the size of the private key.
Definition: xtrcrypt.h:31
XTR_DH::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: xtrcrypt.cpp:81
XTR_DH::GeneratePrivateKey
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate private key in this domain.
Definition: xtrcrypt.cpp:90
XTR_DH::GeneratePublicKey
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: xtrcrypt.cpp:96
integer.h
Multiple precision integer with arithmetic operations.
modarith.h
Class file for performing modular arithmetic.
CryptoPP
Crypto++ library namespace.
Name::SubgroupGenerator
const char * SubgroupGenerator()
Integer, ECP::Point, or EC2N::Point.
Definition: argnames.h:39
Name::Modulus
const char * Modulus()
Integer.
Definition: argnames.h:33
Name::SubgroupOrder
const char * SubgroupOrder()
Integer.
Definition: argnames.h:37
nbtheory.h
Classes and functions for number theoretic operations.
VerifyPrime
CRYPTOPP_DLL bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
pch.h
Precompiled header file.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
XTR_FindPrimesAndGenerator
void XTR_FindPrimesAndGenerator(RandomNumberGenerator &rng, Integer &p, Integer &q, GFP2Element &g, unsigned int pbits, unsigned int qbits)
Creates primes p,q and generator g for XTR.
Definition: xtr.cpp:24
xtrcrypt.h
XTR public key system.