forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.c
More file actions
91 lines (77 loc) · 2.99 KB
/
Copy pathHash.c
File metadata and controls
91 lines (77 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-bindings/hashlib/Hash.h"
#include "shared-module/hashlib/__init__.h"
#include "mbedtls/version.h"
#if MBEDTLS_VERSION_MAJOR >= 4
#include "psa/crypto.h"
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
psa_hash_update(&self->hash_op, data, datalen);
}
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
return;
}
// Clone the operation so we can continue to update or get digest again.
psa_hash_operation_t clone = PSA_HASH_OPERATION_INIT;
psa_hash_clone(&self->hash_op, &clone);
size_t hash_len;
psa_hash_finish(&clone, data, datalen, &hash_len);
}
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
return PSA_HASH_LENGTH(self->hash_alg);
}
#else
#include "mbedtls/ssl.h"
// In mbedtls 2.x, the _ret suffix functions are the recommended API.
// In mbedtls 3.x, the _ret suffix was removed and the base names return int.
#if MBEDTLS_VERSION_MAJOR < 3
#define SHA1_UPDATE mbedtls_sha1_update_ret
#define SHA1_FINISH mbedtls_sha1_finish_ret
#define SHA256_UPDATE mbedtls_sha256_update_ret
#define SHA256_FINISH mbedtls_sha256_finish_ret
#else
#define SHA1_UPDATE mbedtls_sha1_update
#define SHA1_FINISH mbedtls_sha1_finish
#define SHA256_UPDATE mbedtls_sha256_update
#define SHA256_FINISH mbedtls_sha256_finish
#endif
void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) {
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
SHA1_UPDATE(&self->sha1, data, datalen);
return;
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
SHA256_UPDATE(&self->sha256, data, datalen);
return;
}
}
void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) {
if (datalen < common_hal_hashlib_hash_get_digest_size(self)) {
return;
}
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
// We copy the sha1 state so we can continue to update if needed or get
// the digest a second time.
mbedtls_sha1_context copy;
mbedtls_sha1_clone(©, &self->sha1);
SHA1_FINISH(&self->sha1, data);
mbedtls_sha1_clone(&self->sha1, ©);
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
mbedtls_sha256_context copy;
mbedtls_sha256_clone(©, &self->sha256);
SHA256_FINISH(&self->sha256, data);
mbedtls_sha256_clone(&self->sha256, ©);
}
}
size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) {
if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) {
return 20;
} else if (self->hash_type == MBEDTLS_SSL_HASH_SHA256) {
return 32;
}
return 0;
}
#endif