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