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 425b0bc491SJoseph Chen #ifdef CONFIG_DM_CRYPTO 435b0bc491SJoseph Chen #include <crypto.h> 445b0bc491SJoseph Chen #endif 455b69db07SJason Zhu #include <android_avb/avb_crypto.h> 465b69db07SJason Zhu #include <android_avb/avb_sysdeps.h> 475b0bc491SJoseph Chen #include <dm/device.h> 48*439c95c9SJoseph Chen #include <u-boot/sha256.h> 495b69db07SJason Zhu 505b69db07SJason Zhu /* Block size in bytes of a SHA-256 digest. */ 515b69db07SJason Zhu #define AVB_SHA256_BLOCK_SIZE 64 525b69db07SJason Zhu 535b69db07SJason Zhu 545b69db07SJason Zhu /* Block size in bytes of a SHA-512 digest. */ 555b69db07SJason Zhu #define AVB_SHA512_BLOCK_SIZE 128 565b69db07SJason Zhu 575b69db07SJason Zhu /* Data structure used for SHA-256. */ 585b69db07SJason Zhu typedef struct { 595b69db07SJason Zhu uint32_t h[8]; 6069fdc596SJason Zhu uint64_t tot_len; 6169fdc596SJason Zhu size_t len; 625b69db07SJason Zhu uint8_t block[2 * AVB_SHA256_BLOCK_SIZE]; 635b69db07SJason Zhu uint8_t buf[AVB_SHA256_DIGEST_SIZE]; /* Used for storing the final digest. */ 64*439c95c9SJoseph Chen sha256_context sha256ctx; 655b69db07SJason Zhu } AvbSHA256Ctx; 665b69db07SJason Zhu 675b69db07SJason Zhu /* Data structure used for SHA-512. */ 685b69db07SJason Zhu typedef struct { 695b69db07SJason Zhu uint64_t h[8]; 7069fdc596SJason Zhu uint64_t tot_len; 7169fdc596SJason Zhu size_t len; 725b69db07SJason Zhu uint8_t block[2 * AVB_SHA512_BLOCK_SIZE]; 735b69db07SJason Zhu uint8_t buf[AVB_SHA512_DIGEST_SIZE]; /* Used for storing the final digest. */ 745b0bc491SJoseph Chen #ifdef CONFIG_DM_CRYPTO 755b0bc491SJoseph Chen struct udevice *crypto_dev; 765b0bc491SJoseph Chen sha_context crypto_ctx; 775b0bc491SJoseph Chen #endif 785b69db07SJason Zhu } AvbSHA512Ctx; 795b69db07SJason Zhu 805b69db07SJason Zhu /* Initializes the SHA-256 context. */ 815b69db07SJason Zhu void avb_sha256_init(AvbSHA256Ctx* ctx); 825b69db07SJason Zhu 835b69db07SJason Zhu /* Updates the SHA-256 context with |len| bytes from |data|. */ 8469fdc596SJason Zhu void avb_sha256_update(AvbSHA256Ctx* ctx, const uint8_t* data, size_t len); 855b69db07SJason Zhu 865b69db07SJason Zhu /* Returns the SHA-256 digest. */ 875b69db07SJason Zhu uint8_t* avb_sha256_final(AvbSHA256Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 885b69db07SJason Zhu 895b69db07SJason Zhu /* Initializes the SHA-512 context. */ 905b69db07SJason Zhu void avb_sha512_init(AvbSHA512Ctx* ctx); 915b69db07SJason Zhu 925b69db07SJason Zhu /* Updates the SHA-512 context with |len| bytes from |data|. */ 9369fdc596SJason Zhu void avb_sha512_update(AvbSHA512Ctx* ctx, const uint8_t* data, size_t len); 945b69db07SJason Zhu 955b69db07SJason Zhu /* Returns the SHA-512 digest. */ 965b69db07SJason Zhu uint8_t* avb_sha512_final(AvbSHA512Ctx* ctx) AVB_ATTR_WARN_UNUSED_RESULT; 975b69db07SJason Zhu 985b69db07SJason Zhu #ifdef __cplusplus 995b69db07SJason Zhu } 1005b69db07SJason Zhu #endif 1015b69db07SJason Zhu 1025b69db07SJason Zhu #endif /* AVB_SHA_H_ */ 103