1*5b69db07SJason Zhu /* 2*5b69db07SJason Zhu * Copyright (C) 2016 The Android Open Source Project 3*5b69db07SJason Zhu * 4*5b69db07SJason Zhu * Permission is hereby granted, free of charge, to any person 5*5b69db07SJason Zhu * obtaining a copy of this software and associated documentation 6*5b69db07SJason Zhu * files (the "Software"), to deal in the Software without 7*5b69db07SJason Zhu * restriction, including without limitation the rights to use, copy, 8*5b69db07SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 9*5b69db07SJason Zhu * of the Software, and to permit persons to whom the Software is 10*5b69db07SJason Zhu * furnished to do so, subject to the following conditions: 11*5b69db07SJason Zhu * 12*5b69db07SJason Zhu * The above copyright notice and this permission notice shall be 13*5b69db07SJason Zhu * included in all copies or substantial portions of the Software. 14*5b69db07SJason Zhu * 15*5b69db07SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*5b69db07SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*5b69db07SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*5b69db07SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*5b69db07SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*5b69db07SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*5b69db07SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*5b69db07SJason Zhu * SOFTWARE. 23*5b69db07SJason Zhu */ 24*5b69db07SJason Zhu 25*5b69db07SJason Zhu #ifdef AVB_INSIDE_LIBAVB_H 26*5b69db07SJason Zhu #error "You can't include avb_sha.h in the public header libavb.h." 27*5b69db07SJason Zhu #endif 28*5b69db07SJason Zhu 29*5b69db07SJason Zhu /* 30*5b69db07SJason Zhu #ifndef AVB_COMPILATION 31*5b69db07SJason Zhu #error "Never include this file, it may only be used from internal avb code." 32*5b69db07SJason Zhu #endif 33*5b69db07SJason Zhu */ 34*5b69db07SJason Zhu 35*5b69db07SJason Zhu #ifndef AVB_SHA_H_ 36*5b69db07SJason Zhu #define AVB_SHA_H_ 37*5b69db07SJason Zhu 38*5b69db07SJason Zhu #ifdef __cplusplus 39*5b69db07SJason Zhu extern "C" { 40*5b69db07SJason Zhu #endif 41*5b69db07SJason Zhu 42*5b69db07SJason Zhu #include <android_avb/avb_crypto.h> 43*5b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 44*5b69db07SJason Zhu 45*5b69db07SJason Zhu /* Block size in bytes of a SHA-256 digest. */ 46*5b69db07SJason Zhu #define AVB_SHA256_BLOCK_SIZE 64 47*5b69db07SJason Zhu 48*5b69db07SJason Zhu 49*5b69db07SJason Zhu /* Block size in bytes of a SHA-512 digest. */ 50*5b69db07SJason Zhu #define AVB_SHA512_BLOCK_SIZE 128 51*5b69db07SJason Zhu 52*5b69db07SJason Zhu /* Data structure used for SHA-256. */ 53*5b69db07SJason Zhu typedef struct { 54*5b69db07SJason Zhu uint32_t h[8]; 55*5b69db07SJason Zhu uint32_t tot_len; 56*5b69db07SJason Zhu uint32_t len; 57*5b69db07SJason Zhu uint8_t block[2 * AVB_SHA256_BLOCK_SIZE]; 58*5b69db07SJason Zhu uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */ 59*5b69db07SJason Zhu } AvbSHA256Ctx; 60*5b69db07SJason Zhu 61*5b69db07SJason Zhu /* Data structure used for SHA-512. */ 62*5b69db07SJason Zhu typedef struct { 63*5b69db07SJason Zhu uint64_t h[8]; 64*5b69db07SJason Zhu uint32_t tot_len; 65*5b69db07SJason Zhu uint32_t len; 66*5b69db07SJason Zhu uint8_t block[2 * AVB_SHA512_BLOCK_SIZE]; 67*5b69db07SJason Zhu uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */ 68*5b69db07SJason Zhu } AvbSHA512Ctx; 69*5b69db07SJason Zhu 70*5b69db07SJason Zhu /* Initializes the SHA-256 context. */ 71*5b69db07SJason Zhu void avb_sha256_init(AvbSHA256Ctx* ctx); 72*5b69db07SJason Zhu 73*5b69db07SJason Zhu /* Updates the SHA-256 context with |len| bytes from |data|. */ 74*5b69db07SJason Zhu void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, uint32_t len); 75*5b69db07SJason Zhu 76*5b69db07SJason Zhu /* Returns the SHA-256 digest. */ 77*5b69db07SJason Zhu uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 78*5b69db07SJason Zhu 79*5b69db07SJason Zhu /* Initializes the SHA-512 context. */ 80*5b69db07SJason Zhu void avb_sha512_init(AvbSHA512Ctx* ctx); 81*5b69db07SJason Zhu 82*5b69db07SJason Zhu /* Updates the SHA-512 context with |len| bytes from |data|. */ 83*5b69db07SJason Zhu void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, uint32_t len); 84*5b69db07SJason Zhu 85*5b69db07SJason Zhu /* Returns the SHA-512 digest. */ 86*5b69db07SJason Zhu uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 87*5b69db07SJason Zhu 88*5b69db07SJason Zhu #ifdef __cplusplus 89*5b69db07SJason Zhu } 90*5b69db07SJason Zhu #endif 91*5b69db07SJason Zhu 92*5b69db07SJason Zhu #endif /* AVB_SHA_H_ */ 93