|
@@ -63,6 +63,41 @@ int luat_crypto_trng(char* buff, size_t len) {
|
|
|
#include "mbedtls/cipher.h"
|
|
#include "mbedtls/cipher.h"
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+static void add_pkcs_padding( unsigned char *output, size_t output_len,
|
|
|
|
|
+ size_t data_len )
|
|
|
|
|
+{
|
|
|
|
|
+ size_t padding_len = output_len - data_len;
|
|
|
|
|
+ unsigned char i;
|
|
|
|
|
+
|
|
|
|
|
+ for( i = 0; i < padding_len; i++ )
|
|
|
|
|
+ output[data_len + i] = (unsigned char) padding_len;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static int get_pkcs_padding( unsigned char *input, size_t input_len,
|
|
|
|
|
+ size_t *data_len )
|
|
|
|
|
+{
|
|
|
|
|
+ size_t i, pad_idx;
|
|
|
|
|
+ unsigned char padding_len, bad = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if( NULL == input || NULL == data_len )
|
|
|
|
|
+ return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
|
|
|
|
|
+
|
|
|
|
|
+ padding_len = input[input_len - 1];
|
|
|
|
|
+ *data_len = input_len - padding_len;
|
|
|
|
|
+
|
|
|
|
|
+ /* Avoid logical || since it results in a branch */
|
|
|
|
|
+ bad |= padding_len > input_len;
|
|
|
|
|
+ bad |= padding_len == 0;
|
|
|
|
|
+
|
|
|
|
|
+ /* The number of bytes checked must be independent of padding_len,
|
|
|
|
|
+ * so pick input_len, which is usually 8 or 16 (one block) */
|
|
|
|
|
+ pad_idx = input_len - padding_len;
|
|
|
|
|
+ for( i = 0; i < input_len; i++ )
|
|
|
|
|
+ bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
|
|
|
|
|
+
|
|
|
|
|
+ return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
|
|
int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
|
|
|
size_t cipher_size = 0;
|
|
size_t cipher_size = 0;
|
|
|
size_t pad_size = 0;
|
|
size_t pad_size = 0;
|
|
@@ -74,14 +109,14 @@ int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
|
|
|
const char* str = luaL_checklstring(L, 3, &str_size);
|
|
const char* str = luaL_checklstring(L, 3, &str_size);
|
|
|
const char* key = luaL_checklstring(L, 4, &key_size);
|
|
const char* key = luaL_checklstring(L, 4, &key_size);
|
|
|
const char* iv = luaL_optlstring(L, 5, "", &iv_size);
|
|
const char* iv = luaL_optlstring(L, 5, "", &iv_size);
|
|
|
-
|
|
|
|
|
|
|
+ uint8_t *temp = NULL;
|
|
|
int ret = 0;
|
|
int ret = 0;
|
|
|
|
|
|
|
|
unsigned char output[32] = {0};
|
|
unsigned char output[32] = {0};
|
|
|
size_t input_size = 0;
|
|
size_t input_size = 0;
|
|
|
size_t output_size = 0;
|
|
size_t output_size = 0;
|
|
|
size_t block_size = 0;
|
|
size_t block_size = 0;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
luaL_Buffer buff;
|
|
luaL_Buffer buff;
|
|
|
|
|
|
|
|
#ifdef PKG_USING_MBEDTLS
|
|
#ifdef PKG_USING_MBEDTLS
|
|
@@ -135,20 +170,40 @@ int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
|
|
|
// 开始注入数据
|
|
// 开始注入数据
|
|
|
luaL_buffinit(L, &buff);
|
|
luaL_buffinit(L, &buff);
|
|
|
block_size = mbedtls_cipher_get_block_size(&ctx);
|
|
block_size = mbedtls_cipher_get_block_size(&ctx);
|
|
|
|
|
+
|
|
|
|
|
+ if ((ctx.cipher_info->mode == MBEDTLS_MODE_ECB) && !strcmp("PKCS7", pad) && (flags & 0x1)) {
|
|
|
|
|
+ uint32_t new_len = ((str_size / block_size) + 1) * block_size;
|
|
|
|
|
+ temp = luat_heap_malloc(new_len);
|
|
|
|
|
+ memcpy(temp, str, str_size);
|
|
|
|
|
+ add_pkcs_padding(temp + str_size - str_size % block_size, block_size, str_size % block_size);
|
|
|
|
|
+ str_size = new_len;
|
|
|
|
|
+ str = temp;
|
|
|
|
|
+ }
|
|
|
for (size_t i = 0; i < str_size; i+=block_size) {
|
|
for (size_t i = 0; i < str_size; i+=block_size) {
|
|
|
input_size = str_size - i;
|
|
input_size = str_size - i;
|
|
|
- if (input_size > block_size)
|
|
|
|
|
|
|
+ if (input_size > block_size) {
|
|
|
input_size = block_size;
|
|
input_size = block_size;
|
|
|
- ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
|
|
|
|
|
|
|
+ ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
|
|
|
|
|
+ }
|
|
|
|
|
+ else if ((ctx.cipher_info->mode == MBEDTLS_MODE_ECB) && !strcmp("PKCS7", pad) && !flags)
|
|
|
|
|
+ {
|
|
|
|
|
+ ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
|
|
|
|
|
+ get_pkcs_padding(output, output_size, &output_size);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ ret = mbedtls_cipher_update(&ctx, str+i, input_size, output, &output_size);
|
|
|
|
|
+ }
|
|
|
if (ret) {
|
|
if (ret) {
|
|
|
LLOGE("mbedtls_cipher_update fail %ld", ret);
|
|
LLOGE("mbedtls_cipher_update fail %ld", ret);
|
|
|
goto _exit;
|
|
goto _exit;
|
|
|
}
|
|
}
|
|
|
//else LLOGD("mbedtls_cipher_update, output size=%ld", output_size);
|
|
//else LLOGD("mbedtls_cipher_update, output size=%ld", output_size);
|
|
|
- if (output_size > 0)
|
|
|
|
|
|
|
+ if (output_size > 0) {
|
|
|
luaL_addlstring(&buff, output, output_size);
|
|
luaL_addlstring(&buff, output, output_size);
|
|
|
|
|
+ }
|
|
|
output_size = 0;
|
|
output_size = 0;
|
|
|
}
|
|
}
|
|
|
|
|
+ output_size = 0;
|
|
|
ret = mbedtls_cipher_finish(&ctx, output, &output_size);
|
|
ret = mbedtls_cipher_finish(&ctx, output, &output_size);
|
|
|
if (ret) {
|
|
if (ret) {
|
|
|
LLOGE("mbedtls_cipher_finish fail %ld", ret);
|
|
LLOGE("mbedtls_cipher_finish fail %ld", ret);
|
|
@@ -159,10 +214,12 @@ int l_crypto_cipher_xxx(lua_State *L, uint8_t flags) {
|
|
|
luaL_addlstring(&buff, output, output_size);
|
|
luaL_addlstring(&buff, output, output_size);
|
|
|
|
|
|
|
|
_exit:
|
|
_exit:
|
|
|
|
|
+ luat_heap_free(temp);
|
|
|
mbedtls_cipher_free(&ctx);
|
|
mbedtls_cipher_free(&ctx);
|
|
|
luaL_pushresult(&buff);
|
|
luaL_pushresult(&buff);
|
|
|
return 1;
|
|
return 1;
|
|
|
_error_exit:
|
|
_error_exit:
|
|
|
|
|
+ luat_heap_free(temp);
|
|
|
mbedtls_cipher_free(&ctx);
|
|
mbedtls_cipher_free(&ctx);
|
|
|
lua_pushboolean(L, 0);
|
|
lua_pushboolean(L, 0);
|
|
|
return 1;
|
|
return 1;
|