xref: /openbmc/u-boot/lib/libavb/avb_sha.h (revision 4ac5df4b)
1*897a1d94STom Rini /* SPDX-License-Identifier: MIT */
2d8f9d2afSIgor Opaniuk /*
3d8f9d2afSIgor Opaniuk  * Copyright (C) 2016 The Android Open Source Project
4d8f9d2afSIgor Opaniuk  */
5d8f9d2afSIgor Opaniuk 
6d8f9d2afSIgor Opaniuk #ifdef AVB_INSIDE_LIBAVB_H
7d8f9d2afSIgor Opaniuk #error "You can't include avb_sha.h in the public header libavb.h."
8d8f9d2afSIgor Opaniuk #endif
9d8f9d2afSIgor Opaniuk 
10d8f9d2afSIgor Opaniuk #ifndef AVB_COMPILATION
11d8f9d2afSIgor Opaniuk #error "Never include this file, it may only be used from internal avb code."
12d8f9d2afSIgor Opaniuk #endif
13d8f9d2afSIgor Opaniuk 
14d8f9d2afSIgor Opaniuk #ifndef AVB_SHA_H_
15d8f9d2afSIgor Opaniuk #define AVB_SHA_H_
16d8f9d2afSIgor Opaniuk 
17d8f9d2afSIgor Opaniuk #ifdef __cplusplus
18d8f9d2afSIgor Opaniuk extern "C" {
19d8f9d2afSIgor Opaniuk #endif
20d8f9d2afSIgor Opaniuk 
21d8f9d2afSIgor Opaniuk #include "avb_crypto.h"
22d8f9d2afSIgor Opaniuk #include "avb_sysdeps.h"
23d8f9d2afSIgor Opaniuk 
24d8f9d2afSIgor Opaniuk /* Block size in bytes of a SHA-256 digest. */
25d8f9d2afSIgor Opaniuk #define AVB_SHA256_BLOCK_SIZE 64
26d8f9d2afSIgor Opaniuk 
27d8f9d2afSIgor Opaniuk 
28d8f9d2afSIgor Opaniuk /* Block size in bytes of a SHA-512 digest. */
29d8f9d2afSIgor Opaniuk #define AVB_SHA512_BLOCK_SIZE 128
30d8f9d2afSIgor Opaniuk 
31d8f9d2afSIgor Opaniuk /* Data structure used for SHA-256. */
32d8f9d2afSIgor Opaniuk typedef struct {
33d8f9d2afSIgor Opaniuk   uint32_t h[8];
34d8f9d2afSIgor Opaniuk   uint32_t tot_len;
35d8f9d2afSIgor Opaniuk   uint32_t len;
36d8f9d2afSIgor Opaniuk   uint8_t block[2 * AVB_SHA256_BLOCK_SIZE];
37d8f9d2afSIgor Opaniuk   uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */
38d8f9d2afSIgor Opaniuk } AvbSHA256Ctx;
39d8f9d2afSIgor Opaniuk 
40d8f9d2afSIgor Opaniuk /* Data structure used for SHA-512. */
41d8f9d2afSIgor Opaniuk typedef struct {
42d8f9d2afSIgor Opaniuk   uint64_t h[8];
43d8f9d2afSIgor Opaniuk   uint32_t tot_len;
44d8f9d2afSIgor Opaniuk   uint32_t len;
45d8f9d2afSIgor Opaniuk   uint8_t block[2 * AVB_SHA512_BLOCK_SIZE];
46d8f9d2afSIgor Opaniuk   uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */
47d8f9d2afSIgor Opaniuk } AvbSHA512Ctx;
48d8f9d2afSIgor Opaniuk 
49d8f9d2afSIgor Opaniuk /* Initializes the SHA-256 context. */
50d8f9d2afSIgor Opaniuk void avb_sha256_init(AvbSHA256Ctx* ctx);
51d8f9d2afSIgor Opaniuk 
52d8f9d2afSIgor Opaniuk /* Updates the SHA-256 context with |len| bytes from |data|. */
53d8f9d2afSIgor Opaniuk void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, uint32_t len);
54d8f9d2afSIgor Opaniuk 
55d8f9d2afSIgor Opaniuk /* Returns the SHA-256 digest. */
56d8f9d2afSIgor Opaniuk uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT;
57d8f9d2afSIgor Opaniuk 
58d8f9d2afSIgor Opaniuk /* Initializes the SHA-512 context. */
59d8f9d2afSIgor Opaniuk void avb_sha512_init(AvbSHA512Ctx* ctx);
60d8f9d2afSIgor Opaniuk 
61d8f9d2afSIgor Opaniuk /* Updates the SHA-512 context with |len| bytes from |data|. */
62d8f9d2afSIgor Opaniuk void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, uint32_t len);
63d8f9d2afSIgor Opaniuk 
64d8f9d2afSIgor Opaniuk /* Returns the SHA-512 digest. */
65d8f9d2afSIgor Opaniuk uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT;
66d8f9d2afSIgor Opaniuk 
67d8f9d2afSIgor Opaniuk #ifdef __cplusplus
68d8f9d2afSIgor Opaniuk }
69d8f9d2afSIgor Opaniuk #endif
70d8f9d2afSIgor Opaniuk 
71d8f9d2afSIgor Opaniuk #endif /* AVB_SHA_H_ */
72