From 18fc5bd93c1e868c17e8cbbe0f5e80492ddb03da Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 4 Apr 2012 18:11:24 -0700 Subject: [PATCH] Finish encrypt operation. --- src/ECIES.cpp | 63 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/src/ECIES.cpp b/src/ECIES.cpp index 203d83ec04..ed6d5e5c96 100644 --- a/src/ECIES.cpp +++ b/src/ECIES.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -43,10 +44,9 @@ std::vector CKey::getECIESSecret(CKey& otherKey) return ret; } -// Our ciphertext is all encrypted. The encrypted data decodes as follows: +// Our ciphertext is all encrypted except the IV. The encrypted data decodes as follows: // 1) 256-bits of SHA-512 HMAC of original plaintext -// 2) 128-bit IV -// 3) Original plaintext +// 2) Original plaintext static uint256 makeHMAC(const std::vector& secret, const std::vector data) { @@ -80,8 +80,6 @@ static uint256 makeHMAC(const std::vector& secret, const std::vec return ret; } -#if 0 - std::vector CKey::encryptECIES(CKey& otherKey, const std::vector& plaintext) { std::vector secret=getECIESSecret(otherKey); @@ -89,26 +87,61 @@ std::vector CKey::encryptECIES(CKey& otherKey, const std::vector< uint256 hmac=makeHMAC(secret, plaintext); uint128 iv; - if(RAND_bytes((unsigned char *) iv.begin(), 128/8) != 1) + if(RAND_bytes(static_cast(iv.begin()), 128/8) != 1) throw std::runtime_error("insufficient entropy"); - ECP_CIPHER_CTX ctx; + EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); - if (EVP_EncryptInit_ex(&ctx, EVP_AES_128_cbc(), NULL, key, iv) != 1) + if (EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, + &(secret.front()), static_cast(iv.begin())) != 1) { EVP_CIPHER_CTX_cleanup(&ctx); throw std::runtime_error("init cipher ctx"); } - EVP_EncryptUpdate - EVP_EncryptUpdate - EVP_EncryptUpdate - - ECP_EncryptFinal_ex + std::vector out(plaintext.size() + (256/8) + (512/8) + 48, 0); + int len=0, bytesWritten; + + // output 256-bit IV + memcpy(&(out.front()), iv.begin(), 32); + len=32; + + // Encrypt/output 512-bit HMAC + bytesWritten=out.capacity()-len; + assert(bytesWritten>0); + if(EVP_EncryptUpdate(&ctx, &(out.front())+len, &bytesWritten, hmac.begin(), 64) < 0) + { + EVP_CIPHER_CTX_cleanup(&ctx); + throw std::runtime_error(""); + } + len+=bytesWritten; + + // encrypt/output plaintext + bytesWritten=out.capacity()-len; + assert(bytesWritten>0); + if(EVP_EncryptUpdate(&ctx, &(out.front())+len, &bytesWritten, &(plaintext.front()), plaintext.size()) < 0) + { + EVP_CIPHER_CTX_cleanup(&ctx); + throw std::runtime_error(""); + } + len+=bytesWritten; + + // finalize + bytesWritten=out.capacity()-len; + if(EVP_EncryptFinal_ex(&ctx, &(out.front())+len, &bytesWritten) < 0) + { + EVP_CIPHER_CTX_cleanup(&ctx); + throw std::runtime_error(""); + } + len+=bytesWritten; + + out.resize(len); + EVP_CIPHER_CTX_cleanup(&ctx); + return out; } -std::vector CKey::decryptECIES(CKey& otherKey, const std::Vector& ciphertext) +std::vector CKey::decryptECIES(CKey& otherKey, const std::vector& ciphertext) { std::vector secret=getECIESSecret(otherKey); @@ -122,6 +155,4 @@ std::vector CKey::decryptECIES(CKey& otherKey, const std::Vector< } -#endif - // vim:ts=4