Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 2 | * \file sha512.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 4 | * \brief SHA-384 and SHA-512 cryptographic hash function |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 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. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_SHA512_H |
| 24 | #define MBEDTLS_SHA512_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 27 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 32 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 33 | #include <stdint.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 35 | #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \ |
| 36 | !defined(inline) && !defined(__cplusplus) |
| 37 | #define inline __inline |
| 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if !defined(MBEDTLS_SHA512_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 41 | // Regular implementation |
| 42 | // |
| 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | /** |
| 49 | * \brief SHA-512 context structure |
| 50 | */ |
| 51 | typedef struct |
| 52 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 53 | uint64_t total[2]; /*!< number of bytes processed */ |
| 54 | uint64_t state[8]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | unsigned char buffer[128]; /*!< data block being processed */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | int is384; /*!< 0 => SHA-512, else SHA-384 */ |
| 57 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | mbedtls_sha512_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 61 | * \brief Initialize SHA-512 context |
| 62 | * |
| 63 | * \param ctx SHA-512 context to be initialized |
| 64 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | void mbedtls_sha512_init( mbedtls_sha512_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * \brief Clear SHA-512 context |
| 69 | * |
| 70 | * \param ctx SHA-512 context to be cleared |
| 71 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | void mbedtls_sha512_free( mbedtls_sha512_context *ctx ); |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
Manuel Pégourié-Gonnard | 16d412f | 2015-07-06 15:26:26 +0200 | [diff] [blame] | 75 | * \brief Clone (the state of) a SHA-512 context |
| 76 | * |
| 77 | * \param dst The destination context |
| 78 | * \param src The context to be cloned |
| 79 | */ |
| 80 | void mbedtls_sha512_clone( mbedtls_sha512_context *dst, |
| 81 | const mbedtls_sha512_context *src ); |
| 82 | |
| 83 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | * \brief SHA-512 context setup |
| 85 | * |
| 86 | * \param ctx context to be initialized |
| 87 | * \param is384 0 = use SHA512, 1 = use SHA384 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 88 | * |
| 89 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 90 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 91 | int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * \brief SHA-512 process buffer |
| 95 | * |
| 96 | * \param ctx SHA-512 context |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 97 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | * \param ilen length of the input data |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 99 | * |
| 100 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 102 | int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx, |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 103 | const unsigned char *input, |
| 104 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief SHA-512 final digest |
| 108 | * |
| 109 | * \param ctx SHA-512 context |
| 110 | * \param output SHA-384/512 checksum result |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 111 | * |
| 112 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 113 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 114 | int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx, |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 115 | unsigned char output[64] ); |
| 116 | |
| 117 | /** |
| 118 | * \brief SHA-512 process data block (internal use only) |
| 119 | * |
| 120 | * \param ctx SHA-512 context |
| 121 | * \param data buffer holding one block of data |
| 122 | * |
| 123 | * \return 0 if successful |
| 124 | */ |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 125 | int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, |
| 126 | const unsigned char data[128] ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 127 | |
| 128 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 129 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 130 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 131 | #else |
| 132 | #define MBEDTLS_DEPRECATED |
| 133 | #endif |
| 134 | /** |
| 135 | * \brief SHA-512 context setup |
| 136 | * |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 137 | * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.5.0 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 138 | * |
| 139 | * \param ctx context to be initialized |
| 140 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 141 | */ |
| 142 | MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts( |
| 143 | mbedtls_sha512_context *ctx, |
| 144 | int is384 ) |
| 145 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 146 | mbedtls_sha512_starts_ret( ctx, is384 ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * \brief SHA-512 process buffer |
| 151 | * |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 152 | * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.5.0 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 153 | * |
| 154 | * \param ctx SHA-512 context |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 155 | * \param input buffer holding the data |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 156 | * \param ilen length of the input data |
| 157 | */ |
| 158 | MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update( |
| 159 | mbedtls_sha512_context *ctx, |
| 160 | const unsigned char *input, |
| 161 | size_t ilen ) |
| 162 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 163 | mbedtls_sha512_update_ret( ctx, input, ilen ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * \brief SHA-512 final digest |
| 168 | * |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 169 | * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.5.0 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 170 | * |
| 171 | * \param ctx SHA-512 context |
| 172 | * \param output SHA-384/512 checksum result |
| 173 | */ |
| 174 | MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish( |
| 175 | mbedtls_sha512_context *ctx, |
| 176 | unsigned char output[64] ) |
| 177 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 178 | mbedtls_sha512_finish_ret( ctx, output ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * \brief SHA-512 process data block (internal use only) |
| 183 | * |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 184 | * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.5.0 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 185 | * |
| 186 | * \param ctx SHA-512 context |
| 187 | * \param data buffer holding one block of data |
| 188 | */ |
| 189 | MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process( |
| 190 | mbedtls_sha512_context *ctx, |
| 191 | const unsigned char data[128] ) |
| 192 | { |
Andres Amaya Garcia | cccfe08 | 2017-06-28 10:36:39 +0100 | [diff] [blame] | 193 | mbedtls_internal_sha512_process( ctx, data ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | #undef MBEDTLS_DEPRECATED |
| 197 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 199 | #ifdef __cplusplus |
| 200 | } |
| 201 | #endif |
| 202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | #else /* MBEDTLS_SHA512_ALT */ |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 204 | #include "sha512_alt.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | #endif /* MBEDTLS_SHA512_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 206 | |
| 207 | #ifdef __cplusplus |
| 208 | extern "C" { |
| 209 | #endif |
| 210 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | /** |
| 212 | * \brief Output = SHA-512( input buffer ) |
| 213 | * |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 214 | * \param input buffer holding the data |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 215 | * \param ilen length of the input data |
| 216 | * \param output SHA-384/512 checksum result |
| 217 | * \param is384 0 = use SHA512, 1 = use SHA384 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 218 | * |
| 219 | * \return 0 if successful |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | */ |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 221 | int mbedtls_sha512_ret( const unsigned char *input, |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 222 | size_t ilen, |
| 223 | unsigned char output[64], |
| 224 | int is384 ); |
| 225 | |
| 226 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 227 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 228 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
| 229 | #else |
| 230 | #define MBEDTLS_DEPRECATED |
| 231 | #endif |
| 232 | /** |
| 233 | * \brief Output = SHA-512( input buffer ) |
| 234 | * |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 235 | * \deprecated Superseded by mbedtls_sha512_ret() in 2.5.0 |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 236 | * |
Andres Amaya Garcia | a21247e | 2017-07-20 14:01:08 +0100 | [diff] [blame] | 237 | * \param input buffer holding the data |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 238 | * \param ilen length of the input data |
| 239 | * \param output SHA-384/512 checksum result |
| 240 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 241 | */ |
| 242 | MBEDTLS_DEPRECATED static inline void mbedtls_sha512( |
| 243 | const unsigned char *input, |
| 244 | size_t ilen, |
| 245 | unsigned char output[64], |
| 246 | int is384 ) |
| 247 | { |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame^] | 248 | mbedtls_sha512_ret( input, ilen, output, is384 ); |
Andres Amaya Garcia | 614c689 | 2017-05-02 12:07:26 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | #undef MBEDTLS_DEPRECATED |
| 252 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | |
| 254 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | * \brief Checkup routine |
| 256 | * |
| 257 | * \return 0 if successful, or 1 if the test failed |
| 258 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | int mbedtls_sha512_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 261 | #ifdef __cplusplus |
| 262 | } |
| 263 | #endif |
| 264 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | #endif /* mbedtls_sha512.h */ |