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

xtrcrypt.cpp
1 // xtrcrypt.cpp - originally 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 CRYPTOPP_ASSERT(pass);
49 pass = pass && m_q > Integer::One() && m_q.IsOdd();
50 CRYPTOPP_ASSERT(pass);
51 GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
52 CRYPTOPP_ASSERT(pass);
53 pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
54 CRYPTOPP_ASSERT(pass);
55 if (level >= 1)
56 {
57 pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
58 CRYPTOPP_ASSERT(pass);
59 }
60 if (level >= 2)
61 {
62 pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
63 CRYPTOPP_ASSERT(pass);
64 pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
65 CRYPTOPP_ASSERT(pass);
66 pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
67 CRYPTOPP_ASSERT(pass);
68 }
69 return pass;
70 }
71
72 bool XTR_DH::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
73 {
74 return GetValueHelper(this, name, valueType, pValue).Assignable()
75 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
76 CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupOrder)
77 CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupGenerator)
78 ;
79 }
80
81 void XTR_DH::AssignFrom(const NameValuePairs &source)
82 {
83 AssignFromHelper(this, source)
84 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
85 CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
86 CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupGenerator)
87 ;
88 }
89
90 void XTR_DH::GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
91 {
92 Integer x(rng, Integer::Zero(), m_q-1);
93 x.Encode(privateKey, PrivateKeyLength());
94 }
95
96 void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
97 {
98 CRYPTOPP_UNUSED(rng);
99 Integer x(privateKey, PrivateKeyLength());
100 GFP2Element y = XTR_Exponentiate(m_g, x, m_p);
101 y.Encode(publicKey, PublicKeyLength());
102 }
103
104 bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
105 {
106 GFP2Element w(otherPublicKey, PublicKeyLength());
107 if (validateOtherPublicKey)
108 {
109 GFP2_ONB<ModularArithmetic> gfp2(m_p);
110 GFP2Element three = gfp2.ConvertIn(3);
111 if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
112 return false;
113 if (XTR_Exponentiate(w, m_q, m_p) != three)
114 return false;
115 }
116 Integer s(privateKey, PrivateKeyLength());
117 GFP2Element z = XTR_Exponentiate(w, s, m_p);
118 z.Encode(agreedValue, AgreedValueLength());
119 return true;
120 }
121
122 NAMESPACE_END
XTR_DH::Agree
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Derive agreed value.
Definition: xtrcrypt.cpp:104
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
XTR_DH::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: xtrcrypt.cpp:72
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3432
XTR_DH::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: xtrcrypt.cpp:81
Integer::IsNegative
bool IsNegative() const
Determines if the Integer is negative.
Definition: integer.h:336
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1383
XTR_DH::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: xtrcrypt.cpp:44
BERSequenceDecoder
BER Sequence Decoder.
Definition: asn.h:309
XTR_DH::AgreedValueLength
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition: xtrcrypt.h:30
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1598
Integer::One
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:4868
GFP2Element
an element of GF(p^2)
Definition: xtr.h:16
VerifyPrime
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
Definition: nbtheory.cpp:247
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
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
pch.h
Precompiled header file.
XTR_DH::GeneratePrivateKey
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate private key in this domain.
Definition: xtrcrypt.cpp:90
Name::SubgroupGenerator
const char * SubgroupGenerator()
Integer, ECP::Point, or EC2N::Point.
Definition: argnames.h:39
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:69
asn.h
Classes and functions for working with ANS.1 objects.
nbtheory.h
Classes and functions for number theoretic operations.
DERSequenceEncoder
DER Sequence Encoder.
Definition: asn.h:319
xtrcrypt.h
XTR public key system.
Integer::Squared
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:609
XTR_DH::PublicKeyLength
unsigned int PublicKeyLength() const
Provides the size of the public key.
Definition: xtrcrypt.h:32
Name::Modulus
const char * Modulus()
Integer.
Definition: argnames.h:33
XTR_DH::PrivateKeyLength
unsigned int PrivateKeyLength() const
Provides the size of the private key.
Definition: xtrcrypt.h:31
integer.h
Multiple precision integer with arithmetic operations.
Integer::Zero
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:4856
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3439
modarith.h
Class file for performing modular arithmetic.
CryptoPP
Crypto++ library namespace.
GFP2_ONB
GF(p^2), optimal normal basis.
Definition: xtr.h:46
Name::SubgroupOrder
const char * SubgroupOrder()
Integer.
Definition: argnames.h:37
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
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:351
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293