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