1 /*
2  * Copyright (c) 2023 O.S.Systems
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __UPDATEHUB_INTEGRITY_H__
8 #define __UPDATEHUB_INTEGRITY_H__
9 
10 #if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY_MBEDTLS)
11 #include <mbedtls/md.h>
12 #elif defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY_TC)
13 #include <tinycrypt/sha256.h>
14 #include <tinycrypt/constants.h>
15 #else
16 #error "Integrity check method not defined"
17 #endif
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #define SHA256_BIN_DIGEST_SIZE	(32)
24 #define SHA256_HEX_DIGEST_SIZE	((SHA256_BIN_DIGEST_SIZE * 2) + 1)
25 
26 struct updatehub_crypto_context {
27 #if defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY_MBEDTLS)
28 	mbedtls_md_context_t md_ctx;
29 	const mbedtls_md_info_t *md_info;
30 #elif defined(CONFIG_FLASH_AREA_CHECK_INTEGRITY_TC)
31 	struct tc_sha256_state_struct sha256sum;
32 #endif
33 };
34 
35 int updatehub_integrity_init(struct updatehub_crypto_context *ctx);
36 int updatehub_integrity_update(struct updatehub_crypto_context *ctx,
37 			       const uint8_t *buffer, const uint32_t len);
38 int updatehub_integrity_finish(struct updatehub_crypto_context *ctx,
39 			       uint8_t *hash, const uint32_t size);
40 
41 #ifdef __cplusplus
42 }
43 #endif
44 
45 #endif /* __UPDATEHUB_INTEGRITY_H__ */
46