xref: /rk3399_rockchip-uboot/include/u-boot/sha512.h (revision b27ae02dfdf0e26d23901e9b898629d6ec470a60)
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  *  FIPS-180-2 compliant SHA-384/512 implementation
4  *
5  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 /*
22  *  The SHA-512 Secure Hash Standard was published by NIST in 2002.
23  *
24  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
25  */
26 
27 #ifndef _SHA512_H
28 #define _SHA512_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * \brief          The SHA-512 context structure.
36  *
37  *                 The structure is used both for SHA-384 and for SHA-512
38  *                 checksum calculations. The choice between these two is
39  *                 made in the call to sha512_starts().
40  */
41 typedef struct sha512_context {
42     uint64_t total[2];          /*!< The number of Bytes processed. */
43     uint64_t state[8];          /*!< The intermediate digest state. */
44     unsigned char buffer[128];  /*!< The data block being processed. */
45     int is384;                  /*!< Determines which function to use:
46                                      0: Use SHA-512, or 1: Use SHA-384. */
47 } sha512_context;
48 
49 /**
50  * \brief          This function starts a SHA-384 or SHA-512 checksum
51  *                 calculation.
52  *
53  * \param ctx      The SHA-512 context to initialize.
54  * \param is384    Determines which function to use:
55  *                 0: Use SHA-512, or 1: Use SHA-384.
56  *
57  * \return         \c 0 on success.
58  */
59 int sha512_starts(sha512_context *ctx);
60 
61 /**
62  * \brief          This function feeds an input buffer into an ongoing
63  *                 SHA-512 checksum calculation.
64  *
65  * \param ctx      The SHA-512 context.
66  * \param input    The buffer holding the input data.
67  * \param ilen     The length of the input data.
68  *
69  * \return         \c 0 on success.
70  */
71 int sha512_update(sha512_context *ctx, const unsigned char *input, size_t ilen);
72 
73 /**
74  * \brief          This function finishes the SHA-512 operation, and writes
75  *                 the result to the output buffer.
76  *
77  * \param ctx      The SHA-512 context.
78  * \param output   The SHA-384 or SHA-512 checksum result.
79  *
80  * \return         \c 0 on success.
81  */
82 int sha512_finish(sha512_context *ctx, unsigned char output[64]);
83 
84 /**
85  * \brief          This function starts SHA-512 checksum for the input data.
86  *
87  * \param input    The buffer holding the input data.
88  * \param ilen     The length of the input data.
89  * \param output   The SHA-384 or SHA-512 checksum result.
90  */
91 void sha512_csum(const unsigned char *input, unsigned int ilen,
92 		 unsigned char output[64]);
93 
94 #ifdef __cplusplus
95 }
96 #endif
97 
98 #endif /* sha512.h */
99