|
5 | 5 |
|
6 | 6 | #include <stdbool.h>
|
7 | 7 |
|
| 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 | + |
8 | 54 | bool mgos_mbedtls_init(void) {
|
9 | 55 | return true;
|
10 | 56 | }
|
0 commit comments