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