Skip to content

Commit eaac346

Browse files
committed
Fix zephyr mbedtls
1 parent 90c8a3e commit eaac346

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

shared-module/hashlib/Hash.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "shared-bindings/hashlib/Hash.h"
88
#include "shared-module/hashlib/__init__.h"
99

10+
#include "mbedtls/version.h"
11+
12+
#if MBEDTLS_VERSION_MAJOR >= 4
13+
1014
#include "psa/crypto.h"
1115

1216
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
@@ -27,3 +31,47 @@ void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, siz
2731
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
2832
return PSA_HASH_LENGTH(self->hash_alg);
2933
}
34+
35+
#else
36+
37+
#include "mbedtls/ssl.h"
38+
39+
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
40+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
41+
mbedtls_sha1_update_ret(&self->sha1, data, datalen);
42+
return;
43+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
44+
mbedtls_sha256_update_ret(&self->sha256, data, datalen);
45+
return;
46+
}
47+
}
48+
49+
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
50+
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
51+
return;
52+
}
53+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
54+
// We copy the sha1 state so we can continue to update if needed or get
55+
// the digest a second time.
56+
mbedtls_sha1_context copy;
57+
mbedtls_sha1_clone(&copy, &self->sha1);
58+
mbedtls_sha1_finish_ret(&self->sha1, data);
59+
mbedtls_sha1_clone(&self->sha1, &copy);
60+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
61+
mbedtls_sha256_context copy;
62+
mbedtls_sha256_clone(&copy, &self->sha256);
63+
mbedtls_sha256_finish_ret(&self->sha256, data);
64+
mbedtls_sha256_clone(&self->sha256, &copy);
65+
}
66+
}
67+
68+
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
69+
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
70+
return 20;
71+
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
72+
return 32;
73+
}
74+
return 0;
75+
}
76+
77+
#endif

shared-module/hashlib/Hash.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,31 @@
66

77
#pragma once
88

9+
#include "mbedtls/version.h"
10+
11+
#if MBEDTLS_VERSION_MAJOR >= 4
12+
913
#include "psa/crypto.h"
1014

1115
typedef struct {
1216
mp_obj_base_t base;
1317
psa_hash_operation_t hash_op;
1418
psa_algorithm_t hash_alg;
1519
} hashlib_hash_obj_t;
20+
21+
#else
22+
23+
#include "mbedtls/sha1.h"
24+
#include "mbedtls/sha256.h"
25+
26+
typedef struct {
27+
mp_obj_base_t base;
28+
union {
29+
mbedtls_sha1_context sha1;
30+
mbedtls_sha256_context sha256;
31+
};
32+
// Of MBEDTLS_SSL_HASH_*
33+
uint8_t hash_type;
34+
} hashlib_hash_obj_t;
35+
36+
#endif

shared-module/hashlib/__init__.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "shared-bindings/hashlib/__init__.h"
88
#include "shared-module/hashlib/__init__.h"
99

10+
#include "mbedtls/version.h"
11+
12+
#if MBEDTLS_VERSION_MAJOR >= 4
13+
1014
#include "psa/crypto.h"
1115

1216
bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
@@ -21,3 +25,24 @@ bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
2125
psa_hash_setup(&self->hash_op, self->hash_alg);
2226
return true;
2327
}
28+
29+
#else
30+
31+
#include "mbedtls/ssl.h"
32+
33+
bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) {
34+
if (strcmp(algorithm, "sha1") == 0) {
35+
self->hash_type = MBEDTLS_SSL_HASH_SHA1;
36+
mbedtls_sha1_init(&self->sha1);
37+
mbedtls_sha1_starts_ret(&self->sha1);
38+
return true;
39+
} else if (strcmp(algorithm, "sha256") == 0) {
40+
self->hash_type = MBEDTLS_SSL_HASH_SHA256;
41+
mbedtls_sha256_init(&self->sha256);
42+
mbedtls_sha256_starts_ret(&self->sha256, 0);
43+
return true;
44+
}
45+
return false;
46+
}
47+
48+
#endif

0 commit comments

Comments
 (0)