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 42*5b0bc491SJoseph Chen #ifdef CONFIG_DM_CRYPTO 43*5b0bc491SJoseph Chen #include <crypto.h> 44*5b0bc491SJoseph Chen #endif 455b69db07SJason Zhu #include <android_avb/avb_crypto.h> 465b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 47*5b0bc491SJoseph Chen #include <dm/device.h> 485b69db07SJason Zhu 495b69db07SJason Zhu /* Block size in bytes of a SHA-256 digest. */ 505b69db07SJason Zhu #define AVB_SHA256_BLOCK_SIZE 64 515b69db07SJason Zhu 525b69db07SJason Zhu 535b69db07SJason Zhu /* Block size in bytes of a SHA-512 digest. */ 545b69db07SJason Zhu #define AVB_SHA512_BLOCK_SIZE 128 555b69db07SJason Zhu 565b69db07SJason Zhu /* Data structure used for SHA-256. */ 575b69db07SJason Zhu typedef struct { 585b69db07SJason Zhu uint32_t h[8]; 5969fdc596SJason Zhu uint64_t tot_len; 6069fdc596SJason Zhu size_t len; 615b69db07SJason Zhu uint8_t block[2 * AVB_SHA256_BLOCK_SIZE]; 625b69db07SJason Zhu uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */ 63*5b0bc491SJoseph Chen #ifdef CONFIG_DM_CRYPTO 64*5b0bc491SJoseph Chen struct udevice *crypto_dev; 65*5b0bc491SJoseph Chen sha_context crypto_ctx; 66*5b0bc491SJoseph Chen #endif 675b69db07SJason Zhu } AvbSHA256Ctx; 685b69db07SJason Zhu 695b69db07SJason Zhu /* Data structure used for SHA-512. */ 705b69db07SJason Zhu typedef struct { 715b69db07SJason Zhu uint64_t h[8]; 7269fdc596SJason Zhu uint64_t tot_len; 7369fdc596SJason Zhu size_t len; 745b69db07SJason Zhu uint8_t block[2 * AVB_SHA512_BLOCK_SIZE]; 755b69db07SJason Zhu uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */ 76*5b0bc491SJoseph Chen #ifdef CONFIG_DM_CRYPTO 77*5b0bc491SJoseph Chen struct udevice *crypto_dev; 78*5b0bc491SJoseph Chen sha_context crypto_ctx; 79*5b0bc491SJoseph Chen #endif 805b69db07SJason Zhu } AvbSHA512Ctx; 815b69db07SJason Zhu 825b69db07SJason Zhu /* Initializes the SHA-256 context. */ 835b69db07SJason Zhu void avb_sha256_init(AvbSHA256Ctx* ctx); 845b69db07SJason Zhu 855b69db07SJason Zhu /* Updates the SHA-256 context with |len| bytes from |data|. */ 8669fdc596SJason Zhu void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, size_t len); 875b69db07SJason Zhu 885b69db07SJason Zhu /* Returns the SHA-256 digest. */ 895b69db07SJason Zhu uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 905b69db07SJason Zhu 915b69db07SJason Zhu /* Initializes the SHA-512 context. */ 925b69db07SJason Zhu void avb_sha512_init(AvbSHA512Ctx* ctx); 935b69db07SJason Zhu 945b69db07SJason Zhu /* Updates the SHA-512 context with |len| bytes from |data|. */ 9569fdc596SJason Zhu void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, size_t len); 965b69db07SJason Zhu 975b69db07SJason Zhu /* Returns the SHA-512 digest. */ 985b69db07SJason Zhu uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 995b69db07SJason Zhu 1005b69db07SJason Zhu #ifdef __cplusplus 1015b69db07SJason Zhu } 1025b69db07SJason Zhu #endif 1035b69db07SJason Zhu 1045b69db07SJason Zhu #endif /* AVB_SHA_H_ */ 105