New functions that emulate the device's hashing functions

This commit is contained in:
Alexandre Paillier
2022-05-02 15:29:19 +02:00
parent 1375571957
commit 3d9089b395
2 changed files with 50 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
#include <stdint.h>
#include <string.h>
#include "hash_wrap.h"
cx_sha3_t global_sha3;
int cx_keccak_init(cx_hash_t *hash, size_t size)
{
sha3_context *ctx = (sha3_context*)hash;
(void)size;
sha3_Init256(ctx);
sha3_SetFlags(ctx, SHA3_FLAGS_KECCAK);
return 0;
}
int cx_hash(cx_hash_t *hash, int mode, const unsigned char *in,
unsigned int len, unsigned char *out, unsigned int out_len)
{
sha3_context *ctx = (sha3_context*)hash;
const uint8_t *result;
sha3_Update(ctx, in, len);
if (mode == CX_LAST)
{
result = sha3_Finalize(ctx);
if (out != NULL)
{
memmove(out, result, out_len);
}
}
return out_len;
}

View File

@@ -0,0 +1,17 @@
#ifndef HASH_WRAP_H_
#define HASH_WRAP_H_
#include <stdlib.h>
#include "sha3.h"
#define CX_LAST 1
typedef sha3_context cx_sha3_t;
typedef struct {} cx_hash_t;
extern cx_sha3_t global_sha3;
int cx_keccak_init(cx_hash_t *hash, size_t size);
int cx_hash(cx_hash_t *hash, int mode, const unsigned char *in,
unsigned int len, unsigned char *out, unsigned int out_len);
#endif // HASH_WRAP_H_