Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
Refactor AES128-CTR
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Mar 14, 2017
1 parent 61ae37e commit 0dac6ca
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 207 deletions.
314 changes: 146 additions & 168 deletions libdevcrypto/AES.cpp

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions libdevcrypto/AES.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,24 @@

#pragma once

#include "Common.h"
#include <libdevcore/FixedHash.h>

namespace dev
{

void AES128_CTR_process_buffer(uint8_t* output, const uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv);
/// Encrypts payload with specified IV/ctr using AES128-CTR.
bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);

/// Decrypts payload with specified IV/ctr using AES128-CTR.
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);

/// Encrypts payload with specified IV/ctr using AES128-CTR.
inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }

/// Decrypts payload with specified IV/ctr using AES128-CTR.
inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }

bytes aesDecrypt(bytesConstRef _cipher, std::string const& _password, unsigned _rounds = 2000, bytesConstRef _salt = bytesConstRef());

Expand Down
20 changes: 0 additions & 20 deletions libdevcrypto/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,6 @@ std::pair<bytes, h128> dev::encryptSymNoAuth(SecureFixedHash<16> const& _k, byte
return make_pair(encryptSymNoAuth(_k, iv, _plain), iv);
}

bytes dev::encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain)
{
if (_k.size() != 16)
return {};

bytes ret(_plain.size());
AES128_CTR_process_buffer(ret.data(), _plain.data(), _plain.size(), _k.data(), _iv.data());
return ret;
}

bytesSec dev::decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher)
{
if (_k.size() != 16)
return {};

bytesSec ret(_cipher.size());
AES128_CTR_process_buffer(ret.writable().data(), _cipher.data(), _cipher.size(), _k.data(), _iv.data());
return ret;
}

Public dev::recover(Signature const& _sig, h256 const& _message)
{
int v = _sig[64];
Expand Down
18 changes: 2 additions & 16 deletions libdevcrypto/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,12 @@ bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytesConstRef
/// Encrypts payload with random IV/ctr using AES128-CTR.
std::pair<bytes, h128> encryptSymNoAuth(SecureFixedHash<16> const& _k, bytesConstRef _plain);

/// Encrypts payload with specified IV/ctr using AES128-CTR.
bytes encryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _plain);

/// Decrypts payload with specified IV/ctr using AES128-CTR.
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const& _iv, bytesConstRef _cipher);

/// Encrypts payload with specified IV/ctr using AES128-CTR.
inline bytes encryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }
inline bytes encryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _plain) { return encryptAES128CTR(_k.ref(), _iv, _plain); }

/// Decrypts payload with specified IV/ctr using AES128-CTR.
inline bytesSec decryptSymNoAuth(SecureFixedHash<16> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }
inline bytesSec decryptSymNoAuth(SecureFixedHash<32> const& _k, h128 const& _iv, bytesConstRef _cipher) { return decryptAES128CTR(_k.ref(), _iv, _cipher); }

/// Recovers Public key from signed message hash.
Public recover(Signature const& _sig, h256 const& _hash);

/// Returns siganture of message hash.
Signature sign(Secret const& _k, h256 const& _hash);

/// Verify signature.
bool verify(Public const& _k, Signature const& _s, h256 const& _hash);

Expand Down
1 change: 1 addition & 0 deletions libdevcrypto/CryptoPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <libdevcore/Assertions.h>
#include <libdevcore/SHA3.h>
#include "ECDHE.h"
#include "AES.h"

static_assert(CRYPTOPP_VERSION == 570, "Wrong Crypto++ version");

Expand Down
2 changes: 2 additions & 0 deletions libdevcrypto/SecretStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <libdevcore/FileSystem.h>
#include <json_spirit/JsonSpiritHeaders.h>
#include <libdevcrypto/Exceptions.h>
#include "AES.h"

using namespace std;
using namespace dev;
namespace js = json_spirit;
Expand Down
3 changes: 2 additions & 1 deletion libethcore/KeyManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

#include "KeyManager.h"
#include <thread>
#include <mutex>
#include <boost/filesystem.hpp>
#include <json_spirit/JsonSpiritHeaders.h>
#include <libdevcore/Log.h>
#include <libdevcore/Guards.h>
#include <libdevcore/RLP.h>
#include <libdevcore/SHA3.h>
#include <libdevcrypto/AES.h>

using namespace std;
using namespace dev;
using namespace eth;
Expand Down
1 change: 1 addition & 0 deletions test/libdevcrypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <libdevcrypto/ECDHE.h>
#include <libdevcrypto/Hash.h>
#include <libdevcrypto/CryptoPP.h>
#include <libdevcrypto/AES.h>
#include <test/libtesteth/TestHelper.h>

using namespace std;
Expand Down

0 comments on commit 0dac6ca

Please sign in to comment.