|
|
@ -43,7 +43,7 @@ namespace crypto |
|
|
|
DSAVerifier (const uint8_t * signingKey) |
|
|
|
DSAVerifier (const uint8_t * signingKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_PublicKey = CreateDSA (); |
|
|
|
m_PublicKey = CreateDSA (); |
|
|
|
m_PublicKey->pub_key = BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL); |
|
|
|
DSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL), NULL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~DSAVerifier () |
|
|
|
~DSAVerifier () |
|
|
@ -58,8 +58,7 @@ namespace crypto |
|
|
|
SHA1 (buf, len, digest); |
|
|
|
SHA1 (buf, len, digest); |
|
|
|
// signature
|
|
|
|
// signature
|
|
|
|
DSA_SIG * sig = DSA_SIG_new(); |
|
|
|
DSA_SIG * sig = DSA_SIG_new(); |
|
|
|
sig->r = BN_bin2bn (signature, DSA_SIGNATURE_LENGTH/2, NULL); |
|
|
|
DSA_SIG_set0 (sig, BN_bin2bn (signature, DSA_SIGNATURE_LENGTH/2, NULL), BN_bin2bn (signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2, NULL)); |
|
|
|
sig->s = BN_bin2bn (signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2, NULL); |
|
|
|
|
|
|
|
// DSA verification
|
|
|
|
// DSA verification
|
|
|
|
int ret = DSA_do_verify (digest, 20, sig, m_PublicKey); |
|
|
|
int ret = DSA_do_verify (digest, 20, sig, m_PublicKey); |
|
|
|
DSA_SIG_free(sig); |
|
|
|
DSA_SIG_free(sig); |
|
|
@ -81,7 +80,7 @@ namespace crypto |
|
|
|
DSASigner (const uint8_t * signingPrivateKey) |
|
|
|
DSASigner (const uint8_t * signingPrivateKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_PrivateKey = CreateDSA (); |
|
|
|
m_PrivateKey = CreateDSA (); |
|
|
|
m_PrivateKey->priv_key = BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL); |
|
|
|
DSA_set0_key (m_PrivateKey, NULL, BN_bin2bn (signingPrivateKey, DSA_PRIVATE_KEY_LENGTH, NULL)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~DSASigner () |
|
|
|
~DSASigner () |
|
|
@ -94,8 +93,10 @@ namespace crypto |
|
|
|
uint8_t digest[20]; |
|
|
|
uint8_t digest[20]; |
|
|
|
SHA1 (buf, len, digest); |
|
|
|
SHA1 (buf, len, digest); |
|
|
|
DSA_SIG * sig = DSA_do_sign (digest, 20, m_PrivateKey); |
|
|
|
DSA_SIG * sig = DSA_do_sign (digest, 20, m_PrivateKey); |
|
|
|
bn2buf (sig->r, signature, DSA_SIGNATURE_LENGTH/2); |
|
|
|
const BIGNUM * r, * s; |
|
|
|
bn2buf (sig->s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2); |
|
|
|
DSA_SIG_get0 (sig, &r, &s); |
|
|
|
|
|
|
|
bn2buf (r, signature, DSA_SIGNATURE_LENGTH/2); |
|
|
|
|
|
|
|
bn2buf (s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2); |
|
|
|
DSA_SIG_free(sig); |
|
|
|
DSA_SIG_free(sig); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -108,10 +109,11 @@ namespace crypto |
|
|
|
{ |
|
|
|
{ |
|
|
|
DSA * dsa = CreateDSA (); |
|
|
|
DSA * dsa = CreateDSA (); |
|
|
|
DSA_generate_key (dsa); |
|
|
|
DSA_generate_key (dsa); |
|
|
|
bn2buf (dsa->priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH); |
|
|
|
const BIGNUM * pub_key, * priv_key; |
|
|
|
bn2buf (dsa->pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH); |
|
|
|
DSA_get0_key(dsa, &pub_key, &priv_key); |
|
|
|
|
|
|
|
bn2buf (priv_key, signingPrivateKey, DSA_PRIVATE_KEY_LENGTH); |
|
|
|
|
|
|
|
bn2buf (pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH); |
|
|
|
DSA_free (dsa); |
|
|
|
DSA_free (dsa); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct SHA256Hash |
|
|
|
struct SHA256Hash |
|
|
@ -270,8 +272,7 @@ namespace crypto |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_PublicKey = RSA_new (); |
|
|
|
m_PublicKey = RSA_new (); |
|
|
|
memset (m_PublicKey, 0, sizeof (RSA)); |
|
|
|
memset (m_PublicKey, 0, sizeof (RSA)); |
|
|
|
m_PublicKey->e = BN_dup (GetRSAE ()); |
|
|
|
RSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, keyLen, NULL) /* n */ , BN_dup (GetRSAE ()) /* d */, NULL); |
|
|
|
m_PublicKey->n = BN_bin2bn (signingKey, keyLen, NULL); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~RSAVerifier () |
|
|
|
~RSAVerifier () |
|
|
@ -304,9 +305,8 @@ namespace crypto |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_PrivateKey = RSA_new (); |
|
|
|
m_PrivateKey = RSA_new (); |
|
|
|
memset (m_PrivateKey, 0, sizeof (RSA)); |
|
|
|
memset (m_PrivateKey, 0, sizeof (RSA)); |
|
|
|
m_PrivateKey->e = BN_dup (GetRSAE ()); |
|
|
|
RSA_set0_key (m_PrivateKey, BN_bin2bn (signingPrivateKey, keyLen, NULL), /* n */ |
|
|
|
m_PrivateKey->n = BN_bin2bn (signingPrivateKey, keyLen, NULL); |
|
|
|
BN_dup (GetRSAE ()) /* e */, BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL) /* d */); |
|
|
|
m_PrivateKey->d = BN_bin2bn (signingPrivateKey + keyLen, keyLen, NULL); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
~RSASigner () |
|
|
|
~RSASigner () |
|
|
@ -332,9 +332,11 @@ namespace crypto |
|
|
|
RSA * rsa = RSA_new (); |
|
|
|
RSA * rsa = RSA_new (); |
|
|
|
BIGNUM * e = BN_dup (GetRSAE ()); // make it non-const
|
|
|
|
BIGNUM * e = BN_dup (GetRSAE ()); // make it non-const
|
|
|
|
RSA_generate_key_ex (rsa, publicKeyLen*8, e, NULL); |
|
|
|
RSA_generate_key_ex (rsa, publicKeyLen*8, e, NULL); |
|
|
|
bn2buf (rsa->n, signingPrivateKey, publicKeyLen); |
|
|
|
const BIGNUM * n, * d, * e1; |
|
|
|
bn2buf (rsa->d, signingPrivateKey + publicKeyLen, publicKeyLen); |
|
|
|
RSA_get0_key (rsa, &n, &e1, &d); |
|
|
|
bn2buf (rsa->n, signingPublicKey, publicKeyLen); |
|
|
|
bn2buf (n, signingPrivateKey, publicKeyLen); |
|
|
|
|
|
|
|
bn2buf (d, signingPrivateKey + publicKeyLen, publicKeyLen); |
|
|
|
|
|
|
|
bn2buf (n, signingPublicKey, publicKeyLen); |
|
|
|
BN_free (e); // this e is not assigned to rsa->e
|
|
|
|
BN_free (e); // this e is not assigned to rsa->e
|
|
|
|
RSA_free (rsa); |
|
|
|
RSA_free (rsa); |
|
|
|
} |
|
|
|
} |
|
|
|