Skip to content

Commit 526bdae

Browse files
committed
Provide crypto functions for Mongoose
1 parent 8216c1a commit 526bdae

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

mos.yml

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ cdefs:
2929
# but we don't care: Mongoose frees them immediately anyway
3030
# (`MG_SSL_IF_MBEDTLS_FREE_CERTS` is set).
3131
MBEDTLS_FREE_CERT_CHAIN: 1
32+
# Provide crypto functions for Mongoose
33+
MG_EXT_MD5: 1
34+
MG_EXT_SHA1: 1
35+
MG_EXT_SHA256: 1
3236

3337
config_schema:
3438
- ["debug.mbedtls_level", "i", 1, {title: "mbedTLS debug level"}]

src/cc32xx/cc32xx_hash.c

-23
Original file line numberDiff line numberDiff line change
@@ -219,29 +219,6 @@ int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
219219
__attribute__((alias("cc32xx_hash_internal_process")));
220220
#endif /* MBEDTLS_SHA256_ALT */
221221

222-
/* Mongoose external hash interface. */
223-
void mg_hash_md5_v(uint32_t num_msgs, const uint8_t *msgs[],
224-
const uint32_t *msg_lens, uint8_t *digest) {
225-
struct cc32xx_hash_ctx ctx;
226-
cc32xx_hash_init(&ctx);
227-
cc32xx_hash_start(&ctx, CC32XX_HASH_ALGO_MD5);
228-
for (int i = 0; i < num_msgs; i++) {
229-
cc32xx_hash_update(&ctx, msgs[i], msg_lens[i]);
230-
}
231-
cc32xx_hash_finish(&ctx, digest);
232-
}
233-
234-
void mg_hash_sha1_v(uint32_t num_msgs, const uint8_t *msgs[],
235-
const uint32_t *msg_lens, uint8_t *digest) {
236-
struct cc32xx_hash_ctx ctx;
237-
cc32xx_hash_init(&ctx);
238-
cc32xx_hash_start(&ctx, CC32XX_HASH_ALGO_SHA1);
239-
for (int i = 0; i < num_msgs; i++) {
240-
cc32xx_hash_update(&ctx, msgs[i], msg_lens[i]);
241-
}
242-
cc32xx_hash_finish(&ctx, digest);
243-
}
244-
245222
void cc32xx_hash_module_init(void) {
246223
s_engine_lock = mgos_rlock_create();
247224
}

src/mgos_mbedtls.c

+46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,52 @@
55

66
#include <stdbool.h>
77

8+
#include "mongoose.h"
9+
10+
#include "mbedtls/md5.h"
11+
#include "mbedtls/sha1.h"
12+
#include "mbedtls/sha256.h"
13+
14+
/* Crypto functions for Mongoose. */
15+
void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[],
16+
const size_t *msg_lens, uint8_t *digest) {
17+
size_t i;
18+
mbedtls_md5_context ctx;
19+
mbedtls_md5_init(&ctx);
20+
mbedtls_md5_starts_ret(&ctx);
21+
for (i = 0; i < num_msgs; i++) {
22+
mbedtls_md5_update_ret(&ctx, msgs[i], msg_lens[i]);
23+
}
24+
mbedtls_md5_finish_ret(&ctx, digest);
25+
mbedtls_md5_free(&ctx);
26+
}
27+
28+
void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[],
29+
const size_t *msg_lens, uint8_t *digest) {
30+
size_t i;
31+
mbedtls_sha1_context ctx;
32+
mbedtls_sha1_init(&ctx);
33+
mbedtls_sha1_starts_ret(&ctx);
34+
for (i = 0; i < num_msgs; i++) {
35+
mbedtls_sha1_update_ret(&ctx, msgs[i], msg_lens[i]);
36+
}
37+
mbedtls_sha1_finish_ret(&ctx, digest);
38+
mbedtls_sha1_free(&ctx);
39+
}
40+
41+
void mg_hash_sha256_v(size_t num_msgs, const uint8_t *msgs[],
42+
const size_t *msg_lens, uint8_t *digest) {
43+
size_t i;
44+
mbedtls_sha256_context ctx;
45+
mbedtls_sha256_init(&ctx);
46+
mbedtls_sha256_starts_ret(&ctx, false /* is224 */);
47+
for (i = 0; i < num_msgs; i++) {
48+
mbedtls_sha256_update_ret(&ctx, msgs[i], msg_lens[i]);
49+
}
50+
mbedtls_sha256_finish_ret(&ctx, digest);
51+
mbedtls_sha256_free(&ctx);
52+
}
53+
854
bool mgos_mbedtls_init(void) {
955
return true;
1056
}

0 commit comments

Comments
 (0)