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