]> Git Repo - u-boot.git/blob - lib/mbedtls/sha256.c
Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh
[u-boot.git] / lib / mbedtls / sha256.c
1 // 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 <[email protected]>
7  */
8 #ifndef USE_HOSTCC
9 #include <cyclic.h>
10 #endif /* USE_HOSTCC */
11 #include <u-boot/sha256.h>
12
13 #include <mbedtls/md.h>
14
15 #if CONFIG_IS_ENABLED(HKDF_MBEDTLS)
16 #include <mbedtls/hkdf.h>
17 #endif
18
19 const u8 sha256_der_prefix[SHA256_DER_LEN] = {
20         0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
21         0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
22         0x00, 0x04, 0x20
23 };
24
25 void sha256_starts(sha256_context *ctx)
26 {
27         mbedtls_sha256_init(ctx);
28         mbedtls_sha256_starts(ctx, 0);
29 }
30
31 void
32 sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length)
33 {
34         mbedtls_sha256_update(ctx, input, length);
35 }
36
37 void sha256_finish(sha256_context *ctx, uint8_t digest[SHA256_SUM_LEN])
38 {
39         mbedtls_sha256_finish(ctx, digest);
40         mbedtls_sha256_free(ctx);
41 }
42
43 int sha256_hmac(const unsigned char *key, int keylen,
44                 const unsigned char *input, unsigned int ilen,
45                 unsigned char *output)
46 {
47         const mbedtls_md_info_t *md;
48
49         md = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
50         if (!md)
51                 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
52
53         return mbedtls_md_hmac(md, key, keylen, input, ilen, output);
54 }
55
56 #if CONFIG_IS_ENABLED(HKDF_MBEDTLS)
57 int sha256_hkdf(const unsigned char *salt, int saltlen,
58                 const unsigned char *ikm, int ikmlen,
59                 const unsigned char *info, int infolen,
60                 unsigned char *output, int outputlen)
61 {
62         const mbedtls_md_info_t *md;
63
64         md = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
65         if (!md)
66                 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
67
68         return mbedtls_hkdf(md, salt, saltlen,
69                             ikm, ikmlen,
70                             info, infolen,
71                             output, outputlen);
72 }
73 #endif
This page took 0.027954 seconds and 4 git commands to generate.