pure sdk for main
This commit is contained in:
@@ -0,0 +1,528 @@
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_AES_C)
|
||||
|
||||
#if defined(MBEDTLS_AES_ALT)
|
||||
|
||||
#include <string.h>
|
||||
#include "aes_alt.h"
|
||||
#include "mbedtls/aes.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
void mbedtls_aes_init(mbedtls_aes_context *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(mbedtls_aes_context));
|
||||
ctx->hw = (struct sysaes_dev *)dev_get(HG_HWAES0_DEVID);
|
||||
}
|
||||
|
||||
void mbedtls_aes_free(mbedtls_aes_context *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_aes_context));
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx)
|
||||
{
|
||||
mbedtls_aes_init(&ctx->crypt);
|
||||
mbedtls_aes_init(&ctx->tweak);
|
||||
}
|
||||
|
||||
void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx)
|
||||
{
|
||||
if (ctx == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
mbedtls_aes_free(&ctx->crypt);
|
||||
mbedtls_aes_free(&ctx->tweak);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
|
||||
/*
|
||||
* AES key schedule (encryption)
|
||||
*/
|
||||
int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
switch (keybits) {
|
||||
case 128: ctx->para.key_len = AES_KEY_LEN_BIT_128; break;
|
||||
case 192: ctx->para.key_len = AES_KEY_LEN_BIT_192; break;
|
||||
case 256: ctx->para.key_len = AES_KEY_LEN_BIT_256; break;
|
||||
default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
||||
}
|
||||
memcpy(ctx->para.key, key, keybits/8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* AES key schedule (decryption)
|
||||
*/
|
||||
int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
return mbedtls_aes_setkey_enc(ctx, key, keybits);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
static int mbedtls_aes_xts_decode_keys(const unsigned char *key,
|
||||
unsigned int keybits,
|
||||
const unsigned char **key1,
|
||||
unsigned int *key1bits,
|
||||
const unsigned char **key2,
|
||||
unsigned int *key2bits)
|
||||
{
|
||||
const unsigned int half_keybits = keybits / 2;
|
||||
const unsigned int half_keybytes = half_keybits / 8;
|
||||
|
||||
switch (keybits) {
|
||||
case 256: break;
|
||||
case 512: break;
|
||||
default: return MBEDTLS_ERR_AES_INVALID_KEY_LENGTH;
|
||||
}
|
||||
|
||||
*key1bits = half_keybits;
|
||||
*key2bits = half_keybits;
|
||||
*key1 = &key[0];
|
||||
*key2 = &key[half_keybytes];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
const unsigned char *key1, *key2;
|
||||
unsigned int key1bits, key2bits;
|
||||
|
||||
ret = mbedtls_aes_xts_decode_keys(key, keybits, &key1, &key1bits,
|
||||
&key2, &key2bits);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Set the tweak key. Always set tweak key for the encryption mode. */
|
||||
ret = mbedtls_aes_setkey_enc(&ctx->tweak, key2, key2bits);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Set crypt key for encryption. */
|
||||
return mbedtls_aes_setkey_enc(&ctx->crypt, key1, key1bits);
|
||||
}
|
||||
|
||||
int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
|
||||
const unsigned char *key,
|
||||
unsigned int keybits)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
const unsigned char *key1, *key2;
|
||||
unsigned int key1bits, key2bits;
|
||||
|
||||
ret = mbedtls_aes_xts_decode_keys(key, keybits, &key1, &key1bits,
|
||||
&key2, &key2bits);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Set the tweak key. Always set tweak key for encryption. */
|
||||
ret = mbedtls_aes_setkey_enc(&ctx->tweak, key2, key2bits);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Set crypt key for decryption. */
|
||||
return mbedtls_aes_setkey_dec(&ctx->crypt, key1, key1bits);
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
|
||||
/*
|
||||
* AES-ECB block encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
const unsigned char input[16],
|
||||
unsigned char output[16])
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (input && output) {
|
||||
ctx->para.mode = AES_MODE_ECB;
|
||||
ctx->para.src = (uint8 *)input;
|
||||
ctx->para.dest = (uint8 *)output;
|
||||
ctx->para.aes_len = 16; // mbedtls默认ecb操作16字节
|
||||
if (mode == MBEDTLS_AES_ENCRYPT) {
|
||||
ret = sysaes_encrypt(ctx->hw, &ctx->para);
|
||||
} else {
|
||||
ret = sysaes_decrypt(ctx->hw, &ctx->para);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
||||
/*
|
||||
* AES-CBC buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char temp[16];
|
||||
|
||||
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (length % 16) {
|
||||
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
||||
}
|
||||
if (input && output && iv) {
|
||||
ctx->para.mode = AES_MODE_CBC;
|
||||
ctx->para.src = (uint8 *)input;
|
||||
ctx->para.dest = (uint8 *)output;
|
||||
ctx->para.aes_len = length;
|
||||
ctx->para.iv = iv;
|
||||
if (mode == MBEDTLS_AES_ENCRYPT) {
|
||||
ret = sysaes_encrypt(ctx->hw, &ctx->para);
|
||||
memcpy(iv, output, 16); // iv更新为加密后的output
|
||||
} else {
|
||||
memcpy(temp, input, 16);
|
||||
ret = sysaes_decrypt(ctx->hw, &ctx->para);
|
||||
memcpy(iv, temp, 16); // iv更新为解密前的input
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
|
||||
typedef unsigned char mbedtls_be128[16];
|
||||
|
||||
/*
|
||||
* GF(2^128) multiplication function
|
||||
*
|
||||
* This function multiplies a field element by x in the polynomial field
|
||||
* representation. It uses 64-bit word operations to gain speed but compensates
|
||||
* for machine endianness and hence works correctly on both big and little
|
||||
* endian machines.
|
||||
*/
|
||||
static void mbedtls_gf128mul_x_ble(unsigned char r[16],
|
||||
const unsigned char x[16])
|
||||
{
|
||||
uint64_t a, b, ra, rb;
|
||||
|
||||
a = MBEDTLS_GET_UINT64_LE(x, 0);
|
||||
b = MBEDTLS_GET_UINT64_LE(x, 8);
|
||||
|
||||
ra = (a << 1) ^ 0x0087 >> (8 - ((b >> 63) << 3));
|
||||
rb = (a >> 63) | (b << 1);
|
||||
|
||||
MBEDTLS_PUT_UINT64_LE(ra, r, 0);
|
||||
MBEDTLS_PUT_UINT64_LE(rb, r, 8);
|
||||
}
|
||||
|
||||
/*
|
||||
* AES-XTS buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
const unsigned char data_unit[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t blocks = length / 16;
|
||||
size_t leftover = length % 16;
|
||||
unsigned char tweak[16];
|
||||
unsigned char prev_tweak[16];
|
||||
unsigned char tmp[16];
|
||||
|
||||
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
/* Data units must be at least 16 bytes long. */
|
||||
if (length < 16) {
|
||||
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
||||
}
|
||||
|
||||
/* NIST SP 800-38E disallows data units larger than 2**20 blocks. */
|
||||
if (length > (1 << 20) * 16) {
|
||||
return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH;
|
||||
}
|
||||
|
||||
/* Compute the tweak. */
|
||||
ret = mbedtls_aes_crypt_ecb(&ctx->tweak, MBEDTLS_AES_ENCRYPT,
|
||||
data_unit, tweak);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
while (blocks--) {
|
||||
if (leftover && (mode == MBEDTLS_AES_DECRYPT) && blocks == 0) {
|
||||
/* We are on the last block in a decrypt operation that has
|
||||
* leftover bytes, so we need to use the next tweak for this block,
|
||||
* and this tweak for the leftover bytes. Save the current tweak for
|
||||
* the leftovers and then update the current tweak for use on this,
|
||||
* the last full block. */
|
||||
memcpy(prev_tweak, tweak, sizeof(tweak));
|
||||
mbedtls_gf128mul_x_ble(tweak, tweak);
|
||||
}
|
||||
|
||||
mbedtls_xor(tmp, input, tweak, 16);
|
||||
|
||||
ret = mbedtls_aes_crypt_ecb(&ctx->crypt, mode, tmp, tmp);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
mbedtls_xor(output, tmp, tweak, 16);
|
||||
|
||||
/* Update the tweak for the next block. */
|
||||
mbedtls_gf128mul_x_ble(tweak, tweak);
|
||||
|
||||
output += 16;
|
||||
input += 16;
|
||||
}
|
||||
|
||||
if (leftover) {
|
||||
/* If we are on the leftover bytes in a decrypt operation, we need to
|
||||
* use the previous tweak for these bytes (as saved in prev_tweak). */
|
||||
unsigned char *t = mode == MBEDTLS_AES_DECRYPT ? prev_tweak : tweak;
|
||||
|
||||
/* We are now on the final part of the data unit, which doesn't divide
|
||||
* evenly by 16. It's time for ciphertext stealing. */
|
||||
size_t i;
|
||||
unsigned char *prev_output = output - 16;
|
||||
|
||||
/* Copy ciphertext bytes from the previous block to our output for each
|
||||
* byte of ciphertext we won't steal. */
|
||||
for (i = 0; i < leftover; i++) {
|
||||
output[i] = prev_output[i];
|
||||
}
|
||||
|
||||
/* Copy the remainder of the input for this final round. */
|
||||
mbedtls_xor(tmp, input, t, leftover);
|
||||
|
||||
/* Copy ciphertext bytes from the previous block for input in this
|
||||
* round. */
|
||||
mbedtls_xor(tmp + i, prev_output + i, t + i, 16 - i);
|
||||
|
||||
ret = mbedtls_aes_crypt_ecb(&ctx->crypt, mode, tmp, tmp);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Write the result back to the previous block, overriding the previous
|
||||
* output we copied. */
|
||||
mbedtls_xor(prev_output, tmp, t, 16);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CFB)
|
||||
/*
|
||||
* AES-CFB128 buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int c;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t n;
|
||||
|
||||
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
n = *iv_off;
|
||||
|
||||
if (n > 15) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (mode == MBEDTLS_AES_DECRYPT) {
|
||||
while (length--) {
|
||||
if (n == 0) {
|
||||
ret = mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
c = *input++;
|
||||
*output++ = (unsigned char) (c ^ iv[n]);
|
||||
iv[n] = (unsigned char) c;
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
}
|
||||
} else {
|
||||
while (length--) {
|
||||
if (n == 0) {
|
||||
ret = mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
iv[n] = *output++ = (unsigned char) (iv[n] ^ *input++);
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
}
|
||||
}
|
||||
|
||||
*iv_off = n;
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* AES-CFB8 buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
|
||||
int mode,
|
||||
size_t length,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char c;
|
||||
unsigned char ov[17];
|
||||
|
||||
if (mode != MBEDTLS_AES_ENCRYPT && mode != MBEDTLS_AES_DECRYPT) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
while (length--) {
|
||||
memcpy(ov, iv, 16);
|
||||
ret = mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mode == MBEDTLS_AES_DECRYPT) {
|
||||
ov[16] = *input;
|
||||
}
|
||||
|
||||
c = *output++ = (unsigned char) (iv[0] ^ *input++);
|
||||
|
||||
if (mode == MBEDTLS_AES_ENCRYPT) {
|
||||
ov[16] = c;
|
||||
}
|
||||
|
||||
memcpy(iv, ov + 1, 16);
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CFB */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_OFB)
|
||||
/*
|
||||
* AES-OFB (Output Feedback Mode) buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
|
||||
size_t length,
|
||||
size_t *iv_off,
|
||||
unsigned char iv[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int ret = 0;
|
||||
size_t n;
|
||||
|
||||
n = *iv_off;
|
||||
|
||||
if (n > 15) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
while (length--) {
|
||||
if (n == 0) {
|
||||
ret = mbedtls_aes_crypt_ecb(ctx, MBEDTLS_AES_ENCRYPT, iv, iv);
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
*output++ = *input++ ^ iv[n];
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
}
|
||||
|
||||
*iv_off = n;
|
||||
|
||||
exit:
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_OFB */
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_CTR)
|
||||
/*
|
||||
* AES-CTR buffer encryption/decryption
|
||||
*/
|
||||
int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
|
||||
size_t length,
|
||||
size_t *nc_off,
|
||||
unsigned char nonce_counter[16],
|
||||
unsigned char stream_block[16],
|
||||
const unsigned char *input,
|
||||
unsigned char *output)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char temp[16];
|
||||
size_t n;
|
||||
|
||||
// 硬件上其实并没有使用nc_off和stram_block参数
|
||||
n = *nc_off;
|
||||
|
||||
if (n > 0x0F) {
|
||||
return MBEDTLS_ERR_AES_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
if (input && output && nonce_counter) {
|
||||
ctx->para.mode = AES_MODE_CTR;
|
||||
ctx->para.src = (uint8 *)input;
|
||||
ctx->para.dest = (uint8 *)output;
|
||||
ctx->para.aes_len = length;
|
||||
// 硬件设计counter字节序反了,占用16bit,直接交换高2字节
|
||||
memcpy(temp, nonce_counter, 14);
|
||||
temp[14] = nonce_counter[15];
|
||||
temp[15] = nonce_counter[14];
|
||||
ctx->para.iv = temp;
|
||||
ret = sysaes_encrypt(ctx->hw, &ctx->para);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_CIPHER_MODE_CTR */
|
||||
|
||||
#endif /* MBEDTLS_AES_ALT */
|
||||
|
||||
#endif /* MBEDTLS_AES_C */
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef MBEDTLS_AES_ALT_H
|
||||
#define MBEDTLS_AES_ALT_H
|
||||
|
||||
#if defined(MBEDTLS_AES_ALT)
|
||||
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
|
||||
#include "mbedtls/private_access.h"
|
||||
#include "mbedtls/build_info.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "hal/sysaes.h"
|
||||
|
||||
typedef struct mbedtls_aes_context {
|
||||
struct sysaes_dev *MBEDTLS_PRIVATE(hw);
|
||||
struct sysaes_para MBEDTLS_PRIVATE(para);
|
||||
}
|
||||
mbedtls_aes_context;
|
||||
|
||||
#if defined(MBEDTLS_CIPHER_MODE_XTS)
|
||||
/**
|
||||
* \brief The AES XTS context-type definition.
|
||||
*/
|
||||
typedef struct mbedtls_aes_xts_context {
|
||||
mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
|
||||
encryption or decryption. */
|
||||
mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
|
||||
computation. */
|
||||
} mbedtls_aes_xts_context;
|
||||
#endif /* MBEDTLS_CIPHER_MODE_XTS */
|
||||
|
||||
#endif /* MBEDTLS_AES_ALT */
|
||||
|
||||
#endif /* MBEDTLS_AES_ALT_H */
|
||||
@@ -0,0 +1,254 @@
|
||||
#include "common.h"
|
||||
|
||||
#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
|
||||
|
||||
#if defined(MBEDTLS_SHA256_ALT)
|
||||
|
||||
#include <string.h>
|
||||
#include "sha256_alt.h"
|
||||
#include "mbedtls/sha256.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/platform.h"
|
||||
|
||||
|
||||
#include "osal/task.h"
|
||||
#include "osal/mutex.h"
|
||||
|
||||
|
||||
#define SHA256_HW_VERSION 1
|
||||
|
||||
struct os_mutex mbedtls_lock;
|
||||
|
||||
void mbedtls_sha256_mod_init()
|
||||
{
|
||||
os_mutex_init(&mbedtls_lock);
|
||||
}
|
||||
|
||||
|
||||
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
// os_printf("%s CTX: 0x%x task:0x%x\r\n", __FUNCTION__, ctx, os_task_current());
|
||||
struct sha_pack_mgr *mgr = os_malloc_psram(sizeof(struct sha_pack_mgr));
|
||||
memset(mgr, 0, sizeof(struct sha_pack_mgr));
|
||||
memset(ctx, 0, sizeof(struct mbedtls_sha256_context));
|
||||
|
||||
ctx->hw = (struct sha_dev *)dev_get(HG_SHA_DEVID);
|
||||
ctx->mgr = mgr;
|
||||
ctx->mgr->isexceed = 0;
|
||||
ctx->mgr->total_len = 0;
|
||||
INIT_LIST_HEAD(&ctx->mgr->packs);
|
||||
}
|
||||
|
||||
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
|
||||
{
|
||||
// os_printf("%s CTX: 0x%x task:0x%x\r\n", __FUNCTION__, ctx, os_task_current());
|
||||
struct sha_pack *pack, *n;
|
||||
list_for_each_entry_safe(pack, n, &ctx->mgr->packs, list) {
|
||||
if(*pack->refs > 0) {
|
||||
(*pack->refs)--;
|
||||
}
|
||||
if((*pack->refs) == 0) {
|
||||
if(pack->buf) {
|
||||
// printf("free buf : 0x%x\r\n", pack->buf);
|
||||
os_free_psram(pack->buf);
|
||||
}
|
||||
os_free_psram(pack->refs);
|
||||
}
|
||||
list_del(&pack->list);
|
||||
// printf("free pack : 0x%x\r\n", pack);
|
||||
os_free_psram(pack);
|
||||
}
|
||||
|
||||
|
||||
if(ctx->mgr) {
|
||||
os_free_psram(ctx->mgr);
|
||||
} else {
|
||||
os_printf("mbed free error\r\n");
|
||||
}
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha256_context));
|
||||
}
|
||||
|
||||
void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
|
||||
const mbedtls_sha256_context *src)
|
||||
{
|
||||
// os_printf("%s src: 0x%x dst: 0x%x task:0x%x\r\n", __FUNCTION__, src, dst, os_task_current());
|
||||
struct sha_pack *pack, *n;
|
||||
struct sha_pack *new_pack;
|
||||
struct sha_pack_mgr *mgr_tmp = dst->mgr;
|
||||
uint32_t test_cnt = 0;
|
||||
*dst = *src;
|
||||
dst->mgr = mgr_tmp;
|
||||
dst->mgr->total_len = src->mgr->total_len;
|
||||
// printf("clone start\r\n");
|
||||
list_for_each_entry_safe(pack, n, &dst->mgr->packs, list) {
|
||||
test_cnt++;
|
||||
}
|
||||
if(test_cnt) {
|
||||
os_printf("mbed clone error\r\n");
|
||||
}
|
||||
|
||||
list_for_each_entry_safe(pack, n, &src->mgr->packs, list) {
|
||||
(*pack->refs)++;
|
||||
new_pack = os_malloc_psram(sizeof(struct sha_pack));
|
||||
*new_pack = *pack;
|
||||
list_add_tail(&new_pack->list, &dst->mgr->packs);
|
||||
// printf("clone: \r\n");
|
||||
// printf("opack -> buf: 0x%x len: 0x%x\r\n", pack->buf, pack->len);
|
||||
// printf("npack -> buf: 0x%x len: 0x%x\r\n", new_pack->buf, new_pack->len);
|
||||
}
|
||||
// printf("end start\r\n");
|
||||
// printf("mgr[0x%x, 0x%x]\r\n", src->mgr, dst->mgr);
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 context setup
|
||||
*/
|
||||
int mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
|
||||
{
|
||||
// os_printf("%s CTX: 0x%x task:0x%x\r\n", __FUNCTION__, ctx, os_task_current());
|
||||
struct sha_pack *pack, *n;
|
||||
uint32_t cnt = 0;
|
||||
list_for_each_entry_safe(pack, n, &ctx->mgr->packs, list) {
|
||||
cnt++;
|
||||
}
|
||||
if(cnt || (ctx->mgr == NULL)) {
|
||||
os_printf("mbed starts error cnt: %d mgr: 0x%x\r\n", cnt, ctx->mgr);
|
||||
}
|
||||
ctx->mgr->total_len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 process buffer
|
||||
*/
|
||||
int mbedtls_sha256_update(mbedtls_sha256_context *ctx,
|
||||
const unsigned char *input,
|
||||
size_t ilen)
|
||||
{
|
||||
// os_printf("%s CTX: 0x%x task:0x%x\r\n", __FUNCTION__, ctx, os_task_current());
|
||||
if (ilen == 0) {
|
||||
return 0;
|
||||
}
|
||||
struct sha_pack *new_pack = os_malloc_psram(sizeof(struct sha_pack));
|
||||
struct sha_pack *pack, *n;
|
||||
if(ctx->mgr->isexceed) {
|
||||
//调用软件
|
||||
//mbedtls_sha256_update_sw(ctx, input, ilen);
|
||||
} else if(0) {
|
||||
//这里判断是否要转变为软件计算,比如说mgr->total_len + ilen > 某一数值
|
||||
ctx->mgr->isexceed = 1;
|
||||
list_for_each_entry_safe(pack, n, &ctx->mgr->packs, list) {
|
||||
//mbedtls_sha256_update_sw(ctx, pack->buf, pack->len);
|
||||
os_free_psram(pack->buf);
|
||||
list_del(&pack->list);
|
||||
os_free_psram(pack);
|
||||
}
|
||||
//mbedtls_sha256_update_sw(ctx, input, ilen);
|
||||
} else {
|
||||
//存下来,以求得在finish时调用硬件
|
||||
|
||||
new_pack->buf = os_malloc_psram(ilen);
|
||||
new_pack->refs = os_malloc_psram(4);
|
||||
new_pack->len = ilen;
|
||||
*new_pack->refs = 1;
|
||||
ctx->mgr->total_len += ilen;
|
||||
memcpy(new_pack->buf, input, ilen);
|
||||
// printf("add tail buf: 0x%x len: 0x%x\r\n", new_pack->buf, new_pack->len);
|
||||
list_add_tail(&new_pack->list, &ctx->mgr->packs);
|
||||
}
|
||||
// return sha_update(ctx->hw, (uint8 *)input, (uint32)ilen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int fill_test(uint8_t *buf, uint8_t dat, uint32_t len)
|
||||
{
|
||||
for(uint32_t i = 0;i<len;i++)
|
||||
{
|
||||
if(buf[i] != dat) {
|
||||
// printf("fill fail \r\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* SHA-256 final digest
|
||||
*/
|
||||
int mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
|
||||
unsigned char *output)
|
||||
{
|
||||
// os_printf("%s CTX: 0x%x task:0x%x\r\n", __FUNCTION__, ctx, os_task_current());
|
||||
struct sha_pack *pack, *n;
|
||||
int ret = 0;
|
||||
unsigned int ac_len = 0;
|
||||
// printf("finish total len : %x\r\n", ctx->mgr->total_len);
|
||||
// ret = sha_final(ctx->hw, (uint8 *)output);
|
||||
// sha_ioctl(ctx->hw, SHA_IOCTL_CMD_END, 0);
|
||||
sha_ioctl(ctx->hw, SHA_IOCTL_CMD_START, 0);
|
||||
if(ctx->mgr->isexceed) {
|
||||
// mbedtls_sha256_finish_sw(ctx, output);
|
||||
} else {
|
||||
|
||||
sha_ioctl(ctx->hw, SHA_IOCTL_CMD_RESET, 0);
|
||||
sha_init(ctx->hw, SHA_CALC_SHA256);
|
||||
list_for_each_entry_safe(pack, n, &ctx->mgr->packs, list) {
|
||||
// printf("update buf: 0x%x len: 0x%x\r\n", pack->buf, pack->len);
|
||||
sha_update(ctx->hw, pack->buf, pack->len);
|
||||
// if (fill_test(pack->buf, 'a', pack->len)) {
|
||||
// printf("fill fail\r\n");
|
||||
// }
|
||||
// printf("input:\r\n");
|
||||
ac_len += pack->len;
|
||||
for(uint32_t i = 0;i<pack->len;i++)
|
||||
{
|
||||
// printf("%x", pack->buf[i]);
|
||||
}
|
||||
// printf("\r\n");
|
||||
}
|
||||
}
|
||||
// printf("ac total len : %x\r\n", ctx->mgr->total_len);
|
||||
// printf("update end\r\n");
|
||||
ret = sha_final(ctx->hw, (uint8 *)output);
|
||||
|
||||
list_for_each_entry_safe(pack, n, &ctx->mgr->packs, list) {
|
||||
if(*pack->refs > 0) {
|
||||
(*pack->refs)--;
|
||||
}
|
||||
if((*pack->refs) == 0) {
|
||||
if(pack->buf) {
|
||||
// printf("free buf : 0x%x\r\n", pack->buf);
|
||||
os_free_psram(pack->buf);
|
||||
}
|
||||
os_free_psram(pack->refs);
|
||||
}
|
||||
list_del(&pack->list);
|
||||
// printf("free pack : 0x%x\r\n", pack);
|
||||
os_free_psram(pack);
|
||||
}
|
||||
sha_ioctl(ctx->hw, SHA_IOCTL_CMD_END, 0);
|
||||
// printf("free end\r\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
void mbedtls_sha256_sp(unsigned char input,
|
||||
unsigned int ilen,
|
||||
unsigned char *output){
|
||||
struct sha_dev *hw = (struct sha_dev *)dev_get(HG_SHA_DEVID);
|
||||
uint8_t tmp[512];
|
||||
unsigned int uplen;
|
||||
memset(tmp, input, 512);
|
||||
sha_ioctl(hw, SHA_IOCTL_CMD_RESET, 0);
|
||||
sha_init(hw, SHA_CALC_SHA256);
|
||||
while(ilen)
|
||||
{
|
||||
uplen = (ilen > 512) ? 512 : ilen;
|
||||
sha_update(hw, tmp, uplen);
|
||||
ilen -= uplen;
|
||||
}
|
||||
sha_final(hw, (uint8 *)output);
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_SHA256_ALT_H */
|
||||
|
||||
#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA224_C */
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef MBEDTLS_SHA256_ALT_H
|
||||
#define MBEDTLS_SHA256_ALT_H
|
||||
|
||||
#if defined(MBEDTLS_SHA256_ALT)
|
||||
|
||||
#include "typesdef.h"
|
||||
#include "list.h"
|
||||
#include "dev.h"
|
||||
#include "devid.h"
|
||||
|
||||
#include "mbedtls/private_access.h"
|
||||
#include "mbedtls/build_info.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
#include "hal/sha.h"
|
||||
|
||||
struct sha_pack {
|
||||
struct list_head list;
|
||||
uint8_t *buf;
|
||||
uint32_t len;
|
||||
uint32_t *refs;
|
||||
};
|
||||
|
||||
struct sha_pack_mgr {
|
||||
struct list_head packs;
|
||||
uint32_t isexceed;
|
||||
uint32_t total_len;
|
||||
uint32_t refs;
|
||||
};
|
||||
|
||||
typedef struct mbedtls_sha256_context {
|
||||
struct sha_dev *MBEDTLS_PRIVATE(hw);
|
||||
struct sha_pack_mgr *mgr;
|
||||
uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
|
||||
uint32_t MBEDTLS_PRIVATE(state)[8]; /*!< The intermediate digest state. */
|
||||
unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
|
||||
int MBEDTLS_PRIVATE(is224); /*!< Determines which function to use:
|
||||
0: Use SHA-256, or 1: Use SHA-224. */
|
||||
}
|
||||
mbedtls_sha256_context;
|
||||
|
||||
|
||||
#endif /* MBEDTLS_SHA256_ALT */
|
||||
|
||||
#endif /* MBEDTLS_SHA256_ALT_H */
|
||||
Reference in New Issue
Block a user