Add the ECIES definitions. I'm going to put them in their own file.

This commit is contained in:
JoelKatz
2012-04-04 15:06:06 -07:00
parent c2c94b8a8b
commit b7d72553ec
2 changed files with 9 additions and 35 deletions

View File

@@ -1,3 +1,4 @@
#include <openssl/ec.h> #include <openssl/ec.h>
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/ecdsa.h> #include <openssl/ecdsa.h>
@@ -286,38 +287,4 @@ EC_KEY* CKey::GeneratePrivateDeterministicKey(const NewcoinAddress& family, cons
return pkey; return pkey;
} }
static void* ecies_key_derivation(const void *input, size_t ilen, void *output, size_t *olen)
{ // This function must not be changed as it must be what ECDH_compute_key expects
if (*olen < SHA512_DIGEST_LENGTH)
return NULL;
*olen = SHA512_DIGEST_LENGTH;
return SHA512(static_cast<const unsigned char *>(input), ilen, static_cast<unsigned char *>(output));
}
std::vector<unsigned char> CKey::getECIESSecret(CKey& otherKey)
{ // Retrieve a secret generated from an EC key pair. At least one private key must be known.
if(!pkey || !otherKey.pkey)
throw std::runtime_error("missing key");
EC_KEY *pubkey, *privkey;
if(EC_KEY_get0_private_key(pkey))
{
privkey=pkey;
pubkey=otherKey.pkey;
}
else if(EC_KEY_get0_private_key(otherKey.pkey))
{
privkey=otherKey.pkey;
pubkey=pkey;
}
else throw std::runtime_error("no private key");
std::vector<unsigned char> ret(SHA512_DIGEST_LENGTH);
if (ECDH_compute_key(&(ret.front()), SHA512_DIGEST_LENGTH, EC_KEY_get0_public_key(pubkey),
privkey, ecies_key_derivation) != SHA512_DIGEST_LENGTH)
throw std::runtime_error("ecdh key failed");
return ret;
}
// vim:ts=4 // vim:ts=4

View File

@@ -274,8 +274,15 @@ public:
return true; return true;
} }
// Returns a 64-byte secret unique to these two keys. At least one private key must be known. // ECIES functions. These throw on failure
// returns a 64-byte secret unique to these two keys. At least one private key must be known.
std::vector<unsigned char> getECIESSecret(CKey& otherKey); std::vector<unsigned char> getECIESSecret(CKey& otherKey);
// encrypt/decrypt functions with integrity checking.
// Note that the other side must somehow know what keys to use
std::vector<unsigned char> encryptECIES(CKey& otherKey, const std::vector<unsigned char>& plaintext);
std::vector<unsigned char> decryptECIES(CKey& otherKey, const std::vector<unsigned char>& ciphertext);
}; };
#endif #endif