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