15b69db07SJason Zhu /* 25b69db07SJason Zhu * Copyright (C) 2016 The Android Open Source Project 35b69db07SJason Zhu * 45b69db07SJason Zhu * Permission is hereby granted, free of charge, to any person 55b69db07SJason Zhu * obtaining a copy of this software and associated documentation 65b69db07SJason Zhu * files (the "Software"), to deal in the Software without 75b69db07SJason Zhu * restriction, including without limitation the rights to use, copy, 85b69db07SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 95b69db07SJason Zhu * of the Software, and to permit persons to whom the Software is 105b69db07SJason Zhu * furnished to do so, subject to the following conditions: 115b69db07SJason Zhu * 125b69db07SJason Zhu * The above copyright notice and this permission notice shall be 135b69db07SJason Zhu * included in all copies or substantial portions of the Software. 145b69db07SJason Zhu * 155b69db07SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 165b69db07SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 175b69db07SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 185b69db07SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 195b69db07SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 205b69db07SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 215b69db07SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 225b69db07SJason Zhu * SOFTWARE. 235b69db07SJason Zhu */ 245b69db07SJason Zhu 2537a7bc39SJason Zhu /* 265b69db07SJason Zhu #ifdef AVB_INSIDE_LIBAVB_H 275b69db07SJason Zhu #error "You can't include avb_sha.h in the public header libavb.h." 285b69db07SJason Zhu #endif 295b69db07SJason Zhu 305b69db07SJason Zhu #ifndef AVB_COMPILATION 315b69db07SJason Zhu #error "Never include this file, it may only be used from internal avb code." 325b69db07SJason Zhu #endif 335b69db07SJason Zhu */ 345b69db07SJason Zhu 355b69db07SJason Zhu #ifndef AVB_SHA_H_ 365b69db07SJason Zhu #define AVB_SHA_H_ 375b69db07SJason Zhu 385b69db07SJason Zhu #ifdef __cplusplus 395b69db07SJason Zhu extern "C" { 405b69db07SJason Zhu #endif 415b69db07SJason Zhu 425b69db07SJason Zhu #include <android_avb/avb_crypto.h> 435b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 445b69db07SJason Zhu 455b69db07SJason Zhu /* Block size in bytes of a SHA-256 digest. */ 465b69db07SJason Zhu #define AVB_SHA256_BLOCK_SIZE 64 475b69db07SJason Zhu 485b69db07SJason Zhu 495b69db07SJason Zhu /* Block size in bytes of a SHA-512 digest. */ 505b69db07SJason Zhu #define AVB_SHA512_BLOCK_SIZE 128 515b69db07SJason Zhu 525b69db07SJason Zhu /* Data structure used for SHA-256. */ 535b69db07SJason Zhu typedef struct { 545b69db07SJason Zhu uint32_t h[8]; 55*69fdc596SJason Zhu uint64_t tot_len; 56*69fdc596SJason Zhu size_t len; 575b69db07SJason Zhu uint8_t block[2 * AVB_SHA256_BLOCK_SIZE]; 585b69db07SJason Zhu uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */ 595b69db07SJason Zhu } AvbSHA256Ctx; 605b69db07SJason Zhu 615b69db07SJason Zhu /* Data structure used for SHA-512. */ 625b69db07SJason Zhu typedef struct { 635b69db07SJason Zhu uint64_t h[8]; 64*69fdc596SJason Zhu uint64_t tot_len; 65*69fdc596SJason Zhu size_t len; 665b69db07SJason Zhu uint8_t block[2 * AVB_SHA512_BLOCK_SIZE]; 675b69db07SJason Zhu uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */ 685b69db07SJason Zhu } AvbSHA512Ctx; 695b69db07SJason Zhu 705b69db07SJason Zhu /* Initializes the SHA-256 context. */ 715b69db07SJason Zhu void avb_sha256_init(AvbSHA256Ctx* ctx); 725b69db07SJason Zhu 735b69db07SJason Zhu /* Updates the SHA-256 context with |len| bytes from |data|. */ 74*69fdc596SJason Zhu void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, size_t len); 755b69db07SJason Zhu 765b69db07SJason Zhu /* Returns the SHA-256 digest. */ 775b69db07SJason Zhu uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 785b69db07SJason Zhu 795b69db07SJason Zhu /* Initializes the SHA-512 context. */ 805b69db07SJason Zhu void avb_sha512_init(AvbSHA512Ctx* ctx); 815b69db07SJason Zhu 825b69db07SJason Zhu /* Updates the SHA-512 context with |len| bytes from |data|. */ 83*69fdc596SJason Zhu void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, size_t len); 845b69db07SJason Zhu 855b69db07SJason Zhu /* Returns the SHA-512 digest. */ 865b69db07SJason Zhu uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 875b69db07SJason Zhu 885b69db07SJason Zhu #ifdef __cplusplus 895b69db07SJason Zhu } 905b69db07SJason Zhu #endif 915b69db07SJason Zhu 925b69db07SJason Zhu #endif /* AVB_SHA_H_ */ 93