blob: 7d456a82017a0db8c891902439d5f8ed6fecb301 [file] [log] [blame]
Raymond Maof51f3552024-10-03 14:50:19 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Hash shim layer on MbedTLS Crypto library
4 *
5 * Copyright (c) 2024 Linaro Limited
6 * Author: Raymond Mao <raymond.mao@linaro.org>
7 */
8#ifndef USE_HOSTCC
9#include <cyclic.h>
10#endif /* USE_HOSTCC */
11#include <u-boot/sha256.h>
12
Philippe Reynes496c0062024-12-19 14:05:50 +010013#include <mbedtls/md.h>
14
Raymond Maof51f3552024-10-03 14:50:19 -070015const u8 sha256_der_prefix[SHA256_DER_LEN] = {
16 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
17 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
18 0x00, 0x04, 0x20
19};
20
21void sha256_starts(sha256_context *ctx)
22{
23 mbedtls_sha256_init(ctx);
24 mbedtls_sha256_starts(ctx, 0);
25}
26
27void
28sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
29{
30 mbedtls_sha256_update(ctx, input, length);
31}
32
33void sha256_finish(sha256_context *ctx, uint8_t digest[SHA256_SUM_LEN])
34{
35 mbedtls_sha256_finish(ctx, digest);
36 mbedtls_sha256_free(ctx);
37}
Philippe Reynes496c0062024-12-19 14:05:50 +010038
39int sha256_hmac(const unsigned char *key, int keylen,
40 const unsigned char *input, unsigned int ilen,
41 unsigned char *output)
42{
43 const mbedtls_md_info_t *md;
44
45 md = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
46 if (!md)
47 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
48
49 return mbedtls_md_hmac(md, key, keylen, input, ilen, output);
50}