3 #include <openssl/bio.h>
4 #include <openssl/dh.h>
5 #include <openssl/sha.h>
13 #define MIN_COIN_LENGTH 16
14 #define BLINDING_LENGTH 8
15 #define DIGEST_LENGTH SHA_DIGEST_LENGTH
24 extern const char _NL[];
29 void DumpNumber(BIO *out,
const char *szTitle,
const BIGNUM *bn,
30 const char *szTrailer=
_NL);
31 void DumpNumber(
const char *szTitle,
const BIGNUM *bn,
32 const char *szTrailer=
_NL);
33 void HexDump(BIO *out,
const char *szTitle,
const unsigned char *acBuf,
35 void HexDump(
const char *szTitle,
const unsigned char *acBuf,
int nLength);
36 BIGNUM *
ReadNumber(BIO *in,
const char *szTitle);
53 const BIGNUM *
p()
const
58 {
return m_pDH->pub_key; }
72 {
return BN_num_bytes(
p()); }
79 static void cb(
int p,
int,
void *arg);
84 m_pDH=DH_generate_parameters(nPrimeLength*8,DH_GENERATOR_5,cb,NULL);
86 assert(BN_num_bytes(
m_pDH->p) == nPrimeLength);
87 int nRet=DH_generate_key(
m_pDH);
97 { BN_CTX_free(m_ctx); }
109 {
return m_pDH->priv_key; }
132 void Set(
const BIGNUM *bnCoinID)
145 assert(BN_num_bytes(
m_bnCoinID) == nCoinLength);
158 unsigned char *xplusd=
159 static_cast<unsigned char *
>(alloca(bank.
PrimeLength()));
162 memset(xplusd,
'\0',nCoinLength);
165 for(
int n=0 ; n < nDigestIterations ; ++n)
172 BN_bin2bn(xplusd,nCoinLength+nDigestIterations*DIGEST_LENGTH,bnNumber);
187 BIGNUM *m_bnCoinSignature;
190 { m_bnCoinSignature=NULL; }
193 m_bnCoinSignature=NULL;
196 Coin(BIGNUM *bnCoinID,BIGNUM *bnCoinSignature)
198 { m_bnCoinSignature=BN_dup(bnCoinSignature); }
200 { BN_free(m_bnCoinSignature); }
203 BN_free(m_bnCoinSignature);
204 m_bnCoinSignature=NULL;
207 void Set(BIGNUM *bnCoinID,BIGNUM *bnCoinSignature)
210 m_bnCoinSignature=BN_dup(bnCoinSignature);
214 {
Set(ucoin.
ID(),bnCoinSignature); }
221 {
return m_bnCoinSignature; }
251 BIGNUM *m_bnBlindingFactor;
278 if(BN_cmp(y,bank.
p()) < 0)
283 m_bnBlindingFactor=BN_new();
299 BN_free(m_bnBlindingFactor);
306 BN_mod_exp(z,bank.
pub_key(),m_bnBlindingFactor,bank.
p(),m_ctx);
307 BN_mod_inverse(z,z,bank.
p(),m_ctx);
308 BN_mod_mul(z,bnSignedCoin,z,bank.
p(),m_ctx);
312 BIGNUM *bnSignedCoinRequest)
314 BIGNUM *bnCoinSignature=
Unblind(bnSignedCoinRequest,bank);
316 pCoin->
Set(m_coin,bnCoinSignature);
PublicCoinRequest(BIO *bio)
CoinRequest(PublicBank &bank)
void DumpNumber(BIO *out, const char *szTitle, const BIGNUM *bn, const char *szTrailer=_NL)
void Set(BIGNUM *bnCoinID, BIGNUM *bnCoinSignature)
BIGNUM * ReadNumber(BIO *in, const char *szTitle)
boolean GenerateCoinNumber(BIGNUM *bnNumber, const PublicBank &bank)
const BIGNUM * priv_key()
void Random(int nCoinLength)
void HexDump(BIO *out, const char *szTitle, const unsigned char *acBuf, int nLength)
Coin(BIGNUM *bnCoinID, BIGNUM *bnCoinSignature)
void SetMonitor(BIO *out)
void ProcessResponse(Coin *pCoin, PublicBank &bank, BIGNUM *bnSignedCoinRequest)
void Set(UnsignedCoin &ucoin, BIGNUM *bnCoinSignature)
BIGNUM * Unblind(BIGNUM *bnSignedCoin, PublicBank &bank)
BIGNUM * SignRequest(PublicCoinRequest &req)
void Set(const BIGNUM *bnCoinID)
boolean Verify(Coin &coin)
UnsignedCoin(BIGNUM *bnCoinID)