Add hash functions using OTP as feed when available.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2025-09-28 20:26:46 +02:00
parent d63ed56e0e
commit 5048e07f81
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
2 changed files with 32 additions and 2 deletions

View file

@ -25,6 +25,7 @@
#include "mbedtls/aes.h"
#include "crypto_utils.h"
#include "pico_keys.h"
#include "otp.h"
void double_hash_pin(const uint8_t *pin, uint16_t len, uint8_t output[32]) {
uint8_t o1[32];
@ -35,14 +36,28 @@ void double_hash_pin(const uint8_t *pin, uint16_t len, uint8_t output[32]) {
hash_multi(o1, sizeof(o1), output);
}
void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]) {
void double_hash_pin_otp(const uint8_t *pin, uint16_t len, uint8_t output[32]) {
uint8_t o1[32];
hash_multi_otp(pin, len, o1);
for (int i = 0; i < sizeof(o1); i++) {
o1[i] ^= pin[i % len];
}
hash_multi_otp(o1, sizeof(o1), output);
}
void hash_multi_ext(const uint8_t *input, uint16_t len, const uint8_t *init, uint16_t len_init, uint8_t output[32]) {
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);
uint16_t iters = 256;
mbedtls_sha256_starts(&ctx, 0);
if (init && len_init > 0) {
mbedtls_sha256_update(&ctx, init, len_init);
}
else {
#ifndef ENABLE_EMULATION
mbedtls_sha256_update(&ctx, pico_serial.id, sizeof(pico_serial.id));
mbedtls_sha256_update(&ctx, pico_serial.id, sizeof(pico_serial.id));
#endif
}
while (iters > len) {
mbedtls_sha256_update(&ctx, input, len);
@ -55,6 +70,18 @@ void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]) {
mbedtls_sha256_free(&ctx);
}
void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]) {
hash_multi_ext(input, len, NULL, 0, output);
}
void hash_multi_otp(const uint8_t *input, uint16_t len, uint8_t output[32]) {
if (otp_key_1) {
hash_multi_ext(input, len, otp_key_1, 32, output);
} else {
hash_multi(input, len, output);
}
}
void hash256(const uint8_t *input, size_t len, uint8_t output[32]) {
mbedtls_sha256_context ctx;
mbedtls_sha256_init(&ctx);

View file

@ -39,7 +39,10 @@
#define IV_SIZE 16
extern void double_hash_pin(const uint8_t *pin, uint16_t len, uint8_t output[32]);
extern void double_hash_pin_otp(const uint8_t *pin, uint16_t len, uint8_t output[32]);
extern void hash_multi_ext(const uint8_t *input, uint16_t len, const uint8_t *init, uint16_t len_init, uint8_t output[32]);
extern void hash_multi(const uint8_t *input, uint16_t len, uint8_t output[32]);
extern void hash_multi_otp(const uint8_t *input, uint16_t len, uint8_t output[32]);
extern void hash256(const uint8_t *input, size_t len, uint8_t output[32]);
extern void generic_hash(mbedtls_md_type_t md, const uint8_t *input, size_t len, uint8_t *output);
extern int aes_encrypt(const uint8_t *key, const uint8_t *iv, uint16_t key_size, int mode, uint8_t *data, uint16_t len);