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