1 /**
2  * \file sha256.h
3  *
4  * \brief SHA-224 and SHA-256 cryptographic hash function
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  SPDX-License-Identifier: Apache-2.0
8  *
9  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
10  *  not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  *
13  *  http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 #ifndef MBEDTLS_SHA256_H
24 #define MBEDTLS_SHA256_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #include <stddef.h>
33 #include <stdint.h>
34 
35 #if !defined(MBEDTLS_SHA256_ALT)
36 /* Regular implementation */
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * \brief          SHA-256 context structure
44  */
45 typedef struct
46 {
47     uint32_t total[2];          /*!< number of bytes processed  */
48     uint32_t state[8];          /*!< intermediate digest state  */
49     unsigned char buffer[64];   /*!< data block being processed */
50     int is224;                  /*!< 0 => SHA-256, else SHA-224 */
51 }
52 mbedtls_sha256_context;
53 
54 /**
55  * \brief          Initialize SHA-256 context
56  *
57  * \param ctx      SHA-256 context to be initialized
58  */
59 void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
60 
61 /**
62  * \brief          Clear SHA-256 context
63  *
64  * \param ctx      SHA-256 context to be cleared
65  */
66 void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
67 
68 /**
69  * \brief          Clone (the state of) a SHA-256 context
70  *
71  * \param dst      The destination context
72  * \param src      The context to be cloned
73  */
74 void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
75                            const mbedtls_sha256_context *src );
76 
77 /**
78  * \brief          SHA-256 context setup
79  *
80  * \param ctx      context to be initialized
81  * \param is224    0 = use SHA256, 1 = use SHA224
82  */
83 void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 );
84 
85 /**
86  * \brief          SHA-256 process buffer
87  *
88  * \param ctx      SHA-256 context
89  * \param input    buffer holding the  data
90  * \param ilen     length of the input data
91  */
92 void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input,
93                     size_t ilen );
94 
95 /**
96  * \brief          SHA-256 final digest
97  *
98  * \param ctx      SHA-256 context
99  * \param output   SHA-224/256 checksum result
100  */
101 void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] );
102 
103 /* Internal use */
104 void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] );
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #else  /* MBEDTLS_SHA256_ALT */
111 #include "sha256_alt.h"
112 #endif /* MBEDTLS_SHA256_ALT */
113 
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117 
118 /**
119  * \brief          Output = SHA-256( input buffer )
120  *
121  * \param input    buffer holding the  data
122  * \param ilen     length of the input data
123  * \param output   SHA-224/256 checksum result
124  * \param is224    0 = use SHA256, 1 = use SHA224
125  */
126 void mbedtls_sha256( const unsigned char *input, size_t ilen,
127            unsigned char output[32], int is224 );
128 
129 /**
130  * \brief          Checkup routine
131  *
132  * \return         0 if successful, or 1 if the test failed
133  */
134 int mbedtls_sha256_self_test( int verbose );
135 
136 #ifdef __cplusplus
137 }
138 #endif
139 
140 #endif /* mbedtls_sha256.h */
141