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