Crypto++  5.6.3
Free C++ class library of cryptographic schemes
xtrcrypt.cpp
1 // xtrcrypt.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 
5 #include "asn.h"
6 #include "integer.h"
7 #include "xtrcrypt.h"
8 #include "nbtheory.h"
9 #include "modarith.h"
10 #include "argnames.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 XTR_DH::XTR_DH(const Integer &p, const Integer &q, const GFP2Element &g)
15  : m_p(p), m_q(q), m_g(g)
16 {
17 }
18 
19 XTR_DH::XTR_DH(RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
20 {
21  XTR_FindPrimesAndGenerator(rng, m_p, m_q, m_g, pbits, qbits);
22 }
23 
24 XTR_DH::XTR_DH(BufferedTransformation &bt)
25 {
26  BERSequenceDecoder seq(bt);
27  m_p.BERDecode(seq);
28  m_q.BERDecode(seq);
29  m_g.c1.BERDecode(seq);
30  m_g.c2.BERDecode(seq);
31  seq.MessageEnd();
32 }
33 
34 void XTR_DH::DEREncode(BufferedTransformation &bt) const
35 {
36  DERSequenceEncoder seq(bt);
37  m_p.DEREncode(seq);
38  m_q.DEREncode(seq);
39  m_g.c1.DEREncode(seq);
40  m_g.c2.DEREncode(seq);
41  seq.MessageEnd();
42 }
43 
44 bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const
45 {
46  bool pass = true;
47  pass = pass && m_p > Integer::One() && m_p.IsOdd();
48  pass = pass && m_q > Integer::One() && m_q.IsOdd();
49  GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
50  pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
51  if (level >= 1)
52  pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
53  if (level >= 2)
54  {
55  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
56  pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
57  pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
58  }
59  return pass;
60 }
61 
62 bool XTR_DH::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
63 {
64  return GetValueHelper(this, name, valueType, pValue).Assignable()
65  CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
66  CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupOrder)
67  CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupGenerator)
68  ;
69 }
70 
72 {
73  AssignFromHelper(this, source)
74  CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
75  CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
76  CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupGenerator)
77  ;
78 }
79 
80 void XTR_DH::GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
81 {
82  Integer x(rng, Integer::Zero(), m_q-1);
83  x.Encode(privateKey, PrivateKeyLength());
84 }
85 
86 void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
87 {
88  CRYPTOPP_UNUSED(rng);
89  Integer x(privateKey, PrivateKeyLength());
90  GFP2Element y = XTR_Exponentiate(m_g, x, m_p);
91  y.Encode(publicKey, PublicKeyLength());
92 }
93 
94 bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
95 {
96  GFP2Element w(otherPublicKey, PublicKeyLength());
97  if (validateOtherPublicKey)
98  {
100  GFP2Element three = gfp2.ConvertIn(3);
101  if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
102  return false;
103  if (XTR_Exponentiate(w, m_q, m_p) != three)
104  return false;
105  }
106  Integer s(privateKey, PrivateKeyLength());
107  GFP2Element z = XTR_Exponentiate(w, s, m_p);
108  z.Encode(agreedValue, AgreedValueLength());
109  return true;
110 }
111 
112 NAMESPACE_END
unsigned int PublicKeyLength() const
Provides the size of the public key.
Definition: xtrcrypt.h:33
Standard names for retrieving values by name when working with NameValuePairs.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: xtrcrypt.cpp:62
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3338
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:333
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: xtrcrypt.cpp:71
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: xtrcrypt.cpp:44
Interface for random number generators.
Definition: cryptlib.h:1186
BER Sequence Decoder.
Definition: asn.h:294
Interface for buffered transformations.
Definition: cryptlib.h:1352
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3016
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Derive agreed value.
Definition: xtrcrypt.cpp:94
an element of GF(p^2)
Definition: xtr.h:17
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: xtrcrypt.cpp:86
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate private key in this domain.
Definition: xtrcrypt.cpp:80
Integer Squared() const
Definition: integer.h:482
XTR-DH with key validation.
Definition: xtrcrypt.h:16
bool IsNegative() const
Determines if the Integer is negative.
Definition: integer.h:318
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
Definition: nbtheory.cpp:249
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition: xtrcrypt.h:31
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
const char * SubgroupGenerator()
Integer, ECP::Point, or EC2N::Point.
Definition: argnames.h:38
Classes and functions for working with ANS.1 objects.
Classes and functions for number theoretic operations.
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3360
DER Sequence Encoder.
Definition: asn.h:304
"The XTR public key system" by Arjen K.
const char * Modulus()
Integer.
Definition: argnames.h:32
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:3011
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3367
Class file for performing modular arithmetic.
Crypto++ library namespace.
GF(p^2), optimal normal basis.
Definition: xtr.h:48
const char * SubgroupOrder()
Integer.
Definition: argnames.h:36
unsigned int PrivateKeyLength() const
Provides the size of the private key.
Definition: xtrcrypt.h:32
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:19
Interface for retrieving values given their names.
Definition: cryptlib.h:277