Skip to content
Permalink
Browse files

crypto, feat: support aria.

  • Loading branch information...
xicilion committed Aug 31, 2019
1 parent 1829d24 commit ff4e9743cc488f4e5253879005b024791359b382
Showing with 151 additions and 42 deletions.
  1. +8 −0 fibjs/include/ifs/crypto.h
  2. +45 −32 fibjs/src/crypto/Cipher.cpp
  3. +14 −2 idl/zh-cn/crypto.idl
  4. +40 −0 test/crypto_case/aes.xts.json
  5. +34 −0 test/crypto_case/aria.json
  6. +9 −7 test/crypto_test.js
  7. +1 −1 vender
@@ -36,6 +36,8 @@ class crypto_base : public object_base {
_DES_EDE3 = 5,
_BLOWFISH = 6,
_ARC4 = 7,
_ARIA = 8,
_CHACHA20 = 9,
_ECB = 1,
_CBC = 2,
_CFB64 = 3,
@@ -45,6 +47,8 @@ class crypto_base : public object_base {
_GCM = 7,
_STREAM = 8,
_CCM = 9,
_XTS = 10,
_POLY1305 = 11,
_PKCS7 = 0,
_ONE_AND_ZEROS = 1,
_ZEROS_AND_LEN = 2,
@@ -152,6 +156,8 @@ inline ClassInfo& crypto_base::class_info()
{ "DES_EDE3", _DES_EDE3 },
{ "BLOWFISH", _BLOWFISH },
{ "ARC4", _ARC4 },
{ "ARIA", _ARIA },
{ "CHACHA20", _CHACHA20 },
{ "ECB", _ECB },
{ "CBC", _CBC },
{ "CFB64", _CFB64 },
@@ -161,6 +167,8 @@ inline ClassInfo& crypto_base::class_info()
{ "GCM", _GCM },
{ "STREAM", _STREAM },
{ "CCM", _CCM },
{ "XTS", _XTS },
{ "POLY1305", _POLY1305 },
{ "PKCS7", _PKCS7 },
{ "ONE_AND_ZEROS", _ONE_AND_ZEROS },
{ "ZEROS_AND_LEN", _ZEROS_AND_LEN },
@@ -15,34 +15,39 @@
namespace fibjs {

#define SIZE_COUNT 4
#define PROVIDER_COUNT 7
#define MODE_COUNT 10

static const char* s_modes[MODE_COUNT] = {
"", "-ECB", "-CBC", "-CFB64", "-CFB128", "-OFB", "-CTR", "-GCM", "", "-CCM"
static const char* s_modes[] = {
"", "-ECB", "-CBC", "-CFB64", "-CFB128", "-OFB", "-CTR", "-GCM", "", "-CCM", "-XTS", "-POLY1305"
};

#define MODE_COUNT ARRAYSIZE(s_modes)

static struct _cipher_size {
const char* name;
size_t size;
const mbedtls_cipher_info_t* cis[MODE_COUNT];
} s_sizes[][SIZE_COUNT] = {
{ { "AES-128", 0, {} },
{ "AES-192", 0, {} },
{ "AES-256", 0, {} } },
{ { "CAMELLIA-128", 0, {} },
{ "CAMELLIA-192", 0, {} },
{ "CAMELLIA-256", 0, {} } },
{ { "DES", 0, {} } },
{ { "DES-EDE", 0, {} } },
{ { "DES-EDE3", 0, {} } },
{ { "BLOWFISH", 0, {} } },
{ { "ARC4-40", 0, {} },
{ "ARC4-56", 0, {} },
{ "ARC4-64", 0, {} },
{ "ARC4-128", 0, {} } }
{ { "AES-128", {} },
{ "AES-192", {} },
{ "AES-256", {} } },
{ { "CAMELLIA-128", {} },
{ "CAMELLIA-192", {} },
{ "CAMELLIA-256", {} } },
{ { "DES", {} } },
{ { "DES-EDE", {} } },
{ { "DES-EDE3", {} } },
{ { "BLOWFISH", {} } },
{ { "ARC4-40", {} },
{ "ARC4-56", {} },
{ "ARC4-64", {} },
{ "ARC4-128", {} } },
{ { "ARIA-128", {} },
{ "ARIA-192", {} },
{ "ARIA-256", {} } },
{ { "CHACHA20", {} } }
};

#define PROVIDER_COUNT ARRAYSIZE(s_sizes)

class cipher_initer {
public:
cipher_initer()
@@ -57,8 +62,6 @@ class cipher_initer {

name.append(s_modes[k]);
s_sizes[i][j].cis[k] = mbedtls_cipher_info_from_string(name.c_str());
if (s_sizes[i][j].cis[k])
s_sizes[i][j].size = s_sizes[i][j].cis[k]->key_bitlen;
}
}
} s_cipher_initer;
@@ -67,13 +70,14 @@ result_t Cipher_base::_new(int32_t provider, int32_t mode, Buffer_base* key,
Buffer_base* iv, obj_ptr<Cipher_base>& retVal,
v8::Local<v8::Object> This)
{
if (provider < crypto_base::_AES || provider > crypto_base::_ARC4)
if (provider < crypto_base::_AES || provider > crypto_base::_CHACHA20)
return CHECK_ERROR(Runtime::setError("Cipher: Invalid provider"));
if (mode < crypto_base::_ECB || mode > crypto_base::_CCM)
if (mode < crypto_base::_ECB || mode > crypto_base::_POLY1305)
return CHECK_ERROR(Runtime::setError("Cipher: Invalid mode"));

exlib::string strKey;
const mbedtls_cipher_info_t* info = NULL;
bool bFoundMode = false;

key->toString(strKey);
size_t keylen = strKey.length();
@@ -86,13 +90,19 @@ result_t Cipher_base::_new(int32_t provider, int32_t mode, Buffer_base* key,
keylen = 24;
}

for (int32_t i = 0; i < SIZE_COUNT; i++)
if (s_sizes[provider - crypto_base::_AES][i].size == keylen * 8) {
info = s_sizes[provider - crypto_base::_AES][i].cis[mode];
if (info == NULL)
return CHECK_ERROR(Runtime::setError("Cipher: Invalid mode"));
break;
for (int32_t i = 0; i < SIZE_COUNT; i++) {
const mbedtls_cipher_info_t* mod_info = s_sizes[provider - crypto_base::_AES][i].cis[mode];
if (mod_info) {
bFoundMode = true;
if (mod_info->key_bitlen == keylen * 8) {
info = mod_info;
break;
}
}
}

if (!bFoundMode)
return CHECK_ERROR(Runtime::setError("Cipher: Invalid mode"));

if (info == NULL)
return CHECK_ERROR(Runtime::setError("Cipher: Invalid key size"));
@@ -220,11 +230,14 @@ result_t Cipher::process(const mbedtls_operation_t operation, Buffer_base* data,
size_t olen, ilen, offset, block_size, data_size;

data->toString(input);
block_size = mbedtls_cipher_get_block_size(&m_ctx);
data_size = input.length();

for (offset = 0; offset < data_size; offset += block_size) {
ilen = ((uint32_t)data_size - offset > block_size) ? block_size : (uint32_t)(data_size - offset);
block_size = mbedtls_cipher_get_block_size(&m_ctx);
if (block_size == 1)
block_size = sizeof(buffer);

for (offset = 0; offset < data_size; offset += olen) {
ilen = ((uint32_t)(data_size - offset) > block_size) ? block_size : (uint32_t)(data_size - offset);

ret = mbedtls_cipher_update(&m_ctx, (unsigned char*)input.c_str() + offset,
ilen, buffer, &olen);
@@ -8,10 +8,10 @@
*/
module crypto
{
/*! @brief 指定对称加密算法 AES,支持 128, 192, 256 位 key,分组密码工作模式支持 ECB, CBC, CFB128, CTR, GCM */
/*! @brief 指定对称加密算法 AES,支持 128, 192, 256 位 key,分组密码工作模式支持 ECB, CBC, CFB128, CTR, GCM,CCM,XTS */
const AES = 1;

/*! @brief 指定对称加密算法 CAMELLIA,支持 128, 192, 256 位 key,分组密码工作模式支持 ECB, CBC, CFB128, CTR, GCM */
/*! @brief 指定对称加密算法 CAMELLIA,支持 128, 192, 256 位 key,分组密码工作模式支持 ECB, CBC, CFB128, CTR, GCM,CCM */
const CAMELLIA = 2;

/*! @brief 指定对称加密算法 DES,支持 64 位 key,分组密码工作模式支持 ECB, CBC */
@@ -29,6 +29,12 @@ module crypto
/*! @brief 指定对称加密算法 ARC4,支持 40, 56, 64, 128 位 key */
const ARC4 = 7;

/*! @brief 指定对称加密算法 ARIA,支持 128, 192, 256 位 key,分组密码工作模式支持 ECB, CBC, CFB128, CTR, GCM,CCM */
const ARIA = 8;

/*! @brief 指定对称加密算法 CHACHA20,支持 256 位 key,分组密码工作模式支持 POLY1305 */
const CHACHA20 = 9;

/*! @brief 指定分组密码工作模式支持 ECB */
const ECB = 1;

@@ -56,6 +62,12 @@ module crypto
/*! @brief 指定分组密码工作模式支持 CCM */
const CCM = 9;

/*! @brief 指定分组密码工作模式支持 XTS */
const XTS = 10;

/*! @brief 指定分组密码工作模式支持 POLY1305 */
const POLY1305 = 11;

/*! @brief 指定填充模式为 PKCS7 */
const PKCS7 = 0;

0 comments on commit ff4e974

Please sign in to comment.
You can’t perform that action at this time.