Skip to content

Commit

Permalink
feat(*): Add cpp for remaning aes-candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriHoffmann committed Apr 9, 2022
1 parent fd4c948 commit 5918afb
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 230 deletions.
118 changes: 66 additions & 52 deletions cpp/aes-candidates.cpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,106 @@
#include "aes-candidates.h"

namespace rncryptopp {
namespace aescandidates {

template <class CIPHER>
void encrypt(std::string *key, std::string *iv, std::string *data,
std::string *result, int encodingWith) {
template <template <class> class Mode, class T_BlockCipher>
void runEncrypt(std::string *key, std::string *iv, std::string *data,
std::string *result, int encodeTo) {

typename CIPHER::Encryption e;
typename Mode<T_BlockCipher>::Encryption e;
e.SetKeyWithIV(reinterpret_cast<const byte *>(key), key->size(),
reinterpret_cast<const byte *>(iv), iv->size());
std::string encrypted;
StringSource _(*data, true,
new StreamTransformationFilter(e, new StringSink(encrypted)));
encode(&encrypted, result, encodingWith);
encode(&encrypted, result, encodeTo);
}

template <class CIPHER>
void decrypt(std::string *key, std::string *iv, std::string *data,
std::string *result) {

typename CIPHER::Decryption d;
d.SetKeyWithIV(reinterpret_cast<const byte *>(key), key->size(),
reinterpret_cast<const byte *>(iv), iv->size());
StringSource s(*data, true,
new StreamTransformationFilter(d, new StringSink(*result)));
template <class T_BlockCipher>
void getModeAndRunEncrypt(jsi::Runtime &rt, std::string *key, std::string *iv,
std::string *data, std::string &mode,
std::string *result, int encodeTo) {
if (mode == "ecb")
runEncrypt<ECB_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "cbc")
runEncrypt<CBC_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "cbc_cts")
runEncrypt<CBC_CTS_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "cfb")
runEncrypt<CFB_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "ofb")
runEncrypt<OFB_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "ctr")
runEncrypt<CTR_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else if (mode == "xts")
runEncrypt<XTS_Mode, T_BlockCipher>(key, iv, data, result, encodeTo);
else
throwJSError(rt, "RNCryptopp: encrypt mode is not a valid mode value");
}

void aes_encrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result) {

template <class T_BlockCipher>
void encrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result) {
std::string data;
if (!binaryLikeValueToString(rt, args[0], &data, 0, 0))
throwJSError(rt, "RNCryptopp: aes_encrypt data is not a string");
throwJSError(rt, "RNCryptopp: encrypt data is not a string");

std::string key;
if (!binaryLikeValueToString(rt, args[1], &key, 1, 0))
throwJSError(rt,
"RNCryptopp: aes_encrypt iv is not a string or ArrayBuffer");
throwJSError(rt, "RNCryptopp: encrypt key is not a string or ArrayBuffer");

std::string iv;
if (!binaryLikeValueToString(rt, args[2], &iv, 1, 0))
throwJSError(rt,
"RNCryptopp: aes_encrypt iv is not a string or ArrayBuffer");
throwJSError(rt, "RNCryptopp: encrypt iv is not a string or ArrayBuffer");

std::string mode;
if (!stringValueToString(rt, args[3], &mode))
throwJSError(rt, "RNCryptopp: aes_encrypt mode is not a string");
throwJSError(rt, "RNCryptopp: encrypt mode is not a string");

// encode result with base64 by default
int encodingWith =
int encodeTo =
rncryptopp::getEncodingFromArgs(rt, args, argCount, 4, 2, false);

getModeAndRunEncrypt<T_BlockCipher>(rt, &key, &iv, &data, mode, result,
encodeTo);
}

template <template <class> class Mode, class T_BlockCipher>
void runDecrypt(std::string *key, std::string *iv, std::string *data,
std::string *result) {

typename Mode<T_BlockCipher>::Decryption d;
d.SetKeyWithIV(reinterpret_cast<const byte *>(key), key->size(),
reinterpret_cast<const byte *>(iv), iv->size());
StringSource s(*data, true,
new StreamTransformationFilter(d, new StringSink(*result)));
}

template <class T_BlockCipher>
void getModeAndRunDecrypt(jsi::Runtime &rt, std::string *key, std::string *iv,
std::string *data, std::string &mode,
std::string *result) {
if (mode == "ecb")
encrypt<ECB_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<ECB_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "cbc")
encrypt<CBC_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<CBC_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "cbc_cts")
encrypt<CBC_CTS_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<CBC_CTS_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "cfb")
encrypt<CFB_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<CFB_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "ofb")
encrypt<OFB_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<OFB_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "ctr")
encrypt<CTR_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<CTR_Mode, T_BlockCipher>(key, iv, data, result);
else if (mode == "xts")
encrypt<XTS_Mode<AES>>(&key, &iv, &data, result, encodingWith);
runDecrypt<XTS_Mode, T_BlockCipher>(key, iv, data, result);
else
throwJSError(rt,
"RNCryptopp: aes_encrypt mode is not a invalid mode value");
throwJSError(rt, "RNCryptopp: decrypt mode is not a valid mode value");
}

void aes_decrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result) {
template <class T_BlockCipher>
void decrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result) {
// Result is encoded with base64 by default
int encoding = rncryptopp::getEncodingFromArgs(rt, args, argCount, 4, 2);

Expand All @@ -91,22 +120,7 @@ void aes_decrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
if (!stringValueToString(rt, args[3], &mode))
throwJSError(rt, "RNCryptopp: aes_decrypt mode is not a string");

if (mode == "ecb")
decrypt<ECB_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "cbc")
decrypt<CBC_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "cbc_cts")
decrypt<CBC_CTS_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "cfb")
decrypt<CFB_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "ofb")
decrypt<OFB_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "ctr")
decrypt<CTR_Mode<AES>>(&key, &iv, &data, result);
else if (mode == "xts")
decrypt<XTS_Mode<AES>>(&key, &iv, &data, result);
else
throwJSError(rt,
"RNCryptopp: aes_decrypt mode is not a invalid mode value");
getModeAndRunDecrypt<T_BlockCipher>(rt, &key, &iv, &data, mode, result);
}
} // namespace aescandidates
} // namespace rncryptopp
22 changes: 14 additions & 8 deletions cpp/aes-candidates.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#include <jsi/jsilib.h>
#include <string>

#include "cryptopp/base64.h"
#include "cryptopp/cryptlib.h"
#include "cryptopp/filters.h"
#include "cryptopp/modes.h"
#include "cryptopp/osrng.h"

#include "cryptopp/ccm.h"
#include "cryptopp/eax.h"
#include "cryptopp/gcm.h"
#include "cryptopp/xts.h"

#include "helpers.h"
Expand All @@ -19,11 +19,17 @@ using namespace facebook::jsi::detail;
using namespace CryptoPP;

namespace rncryptopp {
void aes_encrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result);
namespace aescandidates {

template <class T_BlockCipher>
void encrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result);

template <class T_BlockCipher>
void decrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result);

void aes_decrypt(jsi::Runtime &rt, const jsi::Value *args, size_t argCount,
std::string *result);
} // namespace aescandidates
} // namespace rncryptopp

#endif // REACT_NATIVE_CRYPTOPP_AES_CANDIDATES_H
Loading

0 comments on commit 5918afb

Please # to comment.