qa: Add [[fallthrough]] annotations where appropriate
[quassel.git] / 3rdparty / sha512 / sha512.h
1 /**
2  * \file sha512.h
3  *
4  * \brief SHA-384 and SHA-512 cryptographic hash function
5  *
6  *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
7  *
8  *  This file is part of mbed TLS (https://polarssl.org)
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 #ifndef POLARSSL_SHA512_H
25 #define POLARSSL_SHA512_H
26
27 // Don't include polarssl/config.h since we are compiling outside of PolarSSL
28 /*
29 #if !defined(POLARSSL_CONFIG_FILE)
30 #include "config.h"
31 #else
32 #include POLARSSL_CONFIG_FILE
33 #endif
34 */
35
36 #include <string.h>
37
38 #if defined(_MSC_VER) || defined(__WATCOMC__)
39   #define UL64(x) x##ui64
40   typedef unsigned __int64 uint64_t;
41 #else
42   #include <inttypes.h>
43   #define UL64(x) x##ULL
44 #endif
45
46 #define POLARSSL_ERR_SHA512_FILE_IO_ERROR              -0x007A  /**< Read/write error in file. */
47
48 #if !defined(POLARSSL_SHA512_ALT)
49 // Regular implementation
50 //
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /**
57  * \brief          SHA-512 context structure
58  */
59 typedef struct
60 {
61     uint64_t total[2];          /*!< number of bytes processed  */
62     uint64_t state[8];          /*!< intermediate digest state  */
63     unsigned char buffer[128];  /*!< data block being processed */
64
65     unsigned char ipad[128];    /*!< HMAC: inner padding        */
66     unsigned char opad[128];    /*!< HMAC: outer padding        */
67     int is384;                  /*!< 0 => SHA-512, else SHA-384 */
68 }
69 sha512_context;
70
71 /**
72  * \brief          Initialize SHA-512 context
73  *
74  * \param ctx      SHA-512 context to be initialized
75  */
76 void sha512_init( sha512_context *ctx );
77
78 /**
79  * \brief          Clear SHA-512 context
80  *
81  * \param ctx      SHA-512 context to be cleared
82  */
83 void sha512_free( sha512_context *ctx );
84
85 /**
86  * \brief          SHA-512 context setup
87  *
88  * \param ctx      context to be initialized
89  * \param is384    0 = use SHA512, 1 = use SHA384
90  */
91 void sha512_starts( sha512_context *ctx, int is384 );
92
93 /**
94  * \brief          SHA-512 process buffer
95  *
96  * \param ctx      SHA-512 context
97  * \param input    buffer holding the  data
98  * \param ilen     length of the input data
99  */
100 void sha512_update( sha512_context *ctx, const unsigned char *input,
101                     size_t ilen );
102
103 /**
104  * \brief          SHA-512 final digest
105  *
106  * \param ctx      SHA-512 context
107  * \param output   SHA-384/512 checksum result
108  */
109 void sha512_finish( sha512_context *ctx, unsigned char output[64] );
110
111 #ifdef __cplusplus
112 }
113 #endif
114
115 #else  /* POLARSSL_SHA512_ALT */
116 #include "sha512_alt.h"
117 #endif /* POLARSSL_SHA512_ALT */
118
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
122
123 /**
124  * \brief          Output = SHA-512( input buffer )
125  *
126  * \param input    buffer holding the  data
127  * \param ilen     length of the input data
128  * \param output   SHA-384/512 checksum result
129  * \param is384    0 = use SHA512, 1 = use SHA384
130  */
131 void sha512( const unsigned char *input, size_t ilen,
132              unsigned char output[64], int is384 );
133
134 /**
135  * \brief          Output = SHA-512( file contents )
136  *
137  * \param path     input file name
138  * \param output   SHA-384/512 checksum result
139  * \param is384    0 = use SHA512, 1 = use SHA384
140  *
141  * \return         0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
142  */
143 int sha512_file( const char *path, unsigned char output[64], int is384 );
144
145 /**
146  * \brief          SHA-512 HMAC context setup
147  *
148  * \param ctx      HMAC context to be initialized
149  * \param is384    0 = use SHA512, 1 = use SHA384
150  * \param key      HMAC secret key
151  * \param keylen   length of the HMAC key
152  */
153 void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
154                          size_t keylen, int is384 );
155
156 /**
157  * \brief          SHA-512 HMAC process buffer
158  *
159  * \param ctx      HMAC context
160  * \param input    buffer holding the  data
161  * \param ilen     length of the input data
162  */
163 void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
164                          size_t ilen );
165
166 /**
167  * \brief          SHA-512 HMAC final digest
168  *
169  * \param ctx      HMAC context
170  * \param output   SHA-384/512 HMAC checksum result
171  */
172 void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
173
174 /**
175  * \brief          SHA-512 HMAC context reset
176  *
177  * \param ctx      HMAC context to be reset
178  */
179 void sha512_hmac_reset( sha512_context *ctx );
180
181 /**
182  * \brief          Output = HMAC-SHA-512( hmac key, input buffer )
183  *
184  * \param key      HMAC secret key
185  * \param keylen   length of the HMAC key
186  * \param input    buffer holding the  data
187  * \param ilen     length of the input data
188  * \param output   HMAC-SHA-384/512 result
189  * \param is384    0 = use SHA512, 1 = use SHA384
190  */
191 void sha512_hmac( const unsigned char *key, size_t keylen,
192                 const unsigned char *input, size_t ilen,
193                 unsigned char output[64], int is384 );
194
195 /**
196  * \brief          Checkup routine
197  *
198  * \return         0 if successful, or 1 if the test failed
199  */
200 int sha512_self_test( int verbose );
201
202 /* Internal use */
203 void sha512_process( sha512_context *ctx, const unsigned char data[128] );
204
205 #ifdef __cplusplus
206 }
207 #endif
208
209 #endif /* sha512.h */