|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-bindings/hashlib/Hash.h" |
| 28 | + |
| 29 | +#include "components/mbedtls/mbedtls/include/mbedtls/ssl.h" |
| 30 | + |
| 31 | +void common_hal_hashlib_hash_update(hashlib_hash_obj_t *self, const uint8_t *data, size_t datalen) { |
| 32 | + if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) { |
| 33 | + mbedtls_sha1_update_ret(&self->sha1, data, datalen); |
| 34 | + return; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +void common_hal_hashlib_hash_digest(hashlib_hash_obj_t *self, uint8_t *data, size_t datalen) { |
| 39 | + if (datalen < common_hal_hashlib_hash_get_digest_size(self)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) { |
| 43 | + // We copy the sha1 state so we can continue to update if needed or get |
| 44 | + // the digest a second time. |
| 45 | + mbedtls_sha1_context copy; |
| 46 | + mbedtls_sha1_clone(©, &self->sha1); |
| 47 | + mbedtls_sha1_finish_ret(&self->sha1, data); |
| 48 | + mbedtls_sha1_clone(&self->sha1, ©); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +size_t common_hal_hashlib_hash_get_digest_size(hashlib_hash_obj_t *self) { |
| 53 | + if (self->hash_type == MBEDTLS_SSL_HASH_SHA1) { |
| 54 | + return 20; |
| 55 | + } |
| 56 | + return 0; |
| 57 | +} |
0 commit comments