Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file sha512.h |
| 3 | * \brief This file contains SHA-384 and SHA-512 definitions and functions. |
| 4 | * |
| 5 | * The Secure Hash Algorithms 384 and 512 (SHA-384 and SHA-512) cryptographic |
| 6 | * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>. |
| 7 | */ |
| 8 | /* |
| 9 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
| 23 | * |
| 24 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 25 | */ |
| 26 | #ifndef MBEDCRYPTO_SHA512_H |
| 27 | #define MBEDCRYPTO_SHA512_H |
| 28 | |
| 29 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 30 | #include "config.h" |
| 31 | #else |
| 32 | #include MBEDCRYPTO_CONFIG_FILE |
| 33 | #endif |
| 34 | |
| 35 | #include <stddef.h> |
| 36 | #include <stdint.h> |
| 37 | |
| 38 | #define MBEDCRYPTO_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */ |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | #if !defined(MBEDCRYPTO_SHA512_ALT) |
| 45 | // Regular implementation |
| 46 | // |
| 47 | |
| 48 | /** |
| 49 | * \brief The SHA-512 context structure. |
| 50 | * |
| 51 | * The structure is used both for SHA-384 and for SHA-512 |
| 52 | * checksum calculations. The choice between these two is |
| 53 | * made in the call to mbedcrypto_sha512_starts_ret(). |
| 54 | */ |
| 55 | typedef struct |
| 56 | { |
| 57 | uint64_t total[2]; /*!< The number of Bytes processed. */ |
| 58 | uint64_t state[8]; /*!< The intermediate digest state. */ |
| 59 | unsigned char buffer[128]; /*!< The data block being processed. */ |
| 60 | int is384; /*!< Determines which function to use: |
| 61 | 0: Use SHA-512, or 1: Use SHA-384. */ |
| 62 | } |
| 63 | mbedcrypto_sha512_context; |
| 64 | |
| 65 | #else /* MBEDCRYPTO_SHA512_ALT */ |
| 66 | #include "sha512_alt.h" |
| 67 | #endif /* MBEDCRYPTO_SHA512_ALT */ |
| 68 | |
| 69 | /** |
| 70 | * \brief This function initializes a SHA-512 context. |
| 71 | * |
| 72 | * \param ctx The SHA-512 context to initialize. |
| 73 | */ |
| 74 | void mbedcrypto_sha512_init( mbedcrypto_sha512_context *ctx ); |
| 75 | |
| 76 | /** |
| 77 | * \brief This function clears a SHA-512 context. |
| 78 | * |
| 79 | * \param ctx The SHA-512 context to clear. |
| 80 | */ |
| 81 | void mbedcrypto_sha512_free( mbedcrypto_sha512_context *ctx ); |
| 82 | |
| 83 | /** |
| 84 | * \brief This function clones the state of a SHA-512 context. |
| 85 | * |
| 86 | * \param dst The destination context. |
| 87 | * \param src The context to clone. |
| 88 | */ |
| 89 | void mbedcrypto_sha512_clone( mbedcrypto_sha512_context *dst, |
| 90 | const mbedcrypto_sha512_context *src ); |
| 91 | |
| 92 | /** |
| 93 | * \brief This function starts a SHA-384 or SHA-512 checksum |
| 94 | * calculation. |
| 95 | * |
| 96 | * \param ctx The SHA-512 context to initialize. |
| 97 | * \param is384 Determines which function to use: |
| 98 | * 0: Use SHA-512, or 1: Use SHA-384. |
| 99 | * |
| 100 | * \return \c 0 on success. |
| 101 | */ |
| 102 | int mbedcrypto_sha512_starts_ret( mbedcrypto_sha512_context *ctx, int is384 ); |
| 103 | |
| 104 | /** |
| 105 | * \brief This function feeds an input buffer into an ongoing |
| 106 | * SHA-512 checksum calculation. |
| 107 | * |
| 108 | * \param ctx The SHA-512 context. |
| 109 | * \param input The buffer holding the input data. |
| 110 | * \param ilen The length of the input data. |
| 111 | * |
| 112 | * \return \c 0 on success. |
| 113 | */ |
| 114 | int mbedcrypto_sha512_update_ret( mbedcrypto_sha512_context *ctx, |
| 115 | const unsigned char *input, |
| 116 | size_t ilen ); |
| 117 | |
| 118 | /** |
| 119 | * \brief This function finishes the SHA-512 operation, and writes |
| 120 | * the result to the output buffer. This function is for |
| 121 | * internal use only. |
| 122 | * |
| 123 | * \param ctx The SHA-512 context. |
| 124 | * \param output The SHA-384 or SHA-512 checksum result. |
| 125 | * |
| 126 | * \return \c 0 on success. |
| 127 | */ |
| 128 | int mbedcrypto_sha512_finish_ret( mbedcrypto_sha512_context *ctx, |
| 129 | unsigned char output[64] ); |
| 130 | |
| 131 | /** |
| 132 | * \brief This function processes a single data block within |
| 133 | * the ongoing SHA-512 computation. |
| 134 | * |
| 135 | * \param ctx The SHA-512 context. |
| 136 | * \param data The buffer holding one block of data. |
| 137 | * |
| 138 | * \return \c 0 on success. |
| 139 | */ |
| 140 | int mbedcrypto_internal_sha512_process( mbedcrypto_sha512_context *ctx, |
| 141 | const unsigned char data[128] ); |
| 142 | #if !defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 143 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 144 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 145 | #else |
| 146 | #define MBEDCRYPTO_DEPRECATED |
| 147 | #endif |
| 148 | /** |
| 149 | * \brief This function starts a SHA-384 or SHA-512 checksum |
| 150 | * calculation. |
| 151 | * |
| 152 | * \deprecated Superseded by mbedcrypto_sha512_starts_ret() in 2.7.0 |
| 153 | * |
| 154 | * \param ctx The SHA-512 context to initialize. |
| 155 | * \param is384 Determines which function to use: |
| 156 | * 0: Use SHA-512, or 1: Use SHA-384. |
| 157 | */ |
| 158 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha512_starts( mbedcrypto_sha512_context *ctx, |
| 159 | int is384 ); |
| 160 | |
| 161 | /** |
| 162 | * \brief This function feeds an input buffer into an ongoing |
| 163 | * SHA-512 checksum calculation. |
| 164 | * |
| 165 | * \deprecated Superseded by mbedcrypto_sha512_update_ret() in 2.7.0. |
| 166 | * |
| 167 | * \param ctx The SHA-512 context. |
| 168 | * \param input The buffer holding the data. |
| 169 | * \param ilen The length of the input data. |
| 170 | */ |
| 171 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha512_update( mbedcrypto_sha512_context *ctx, |
| 172 | const unsigned char *input, |
| 173 | size_t ilen ); |
| 174 | |
| 175 | /** |
| 176 | * \brief This function finishes the SHA-512 operation, and writes |
| 177 | * the result to the output buffer. |
| 178 | * |
| 179 | * \deprecated Superseded by mbedcrypto_sha512_finish_ret() in 2.7.0. |
| 180 | * |
| 181 | * \param ctx The SHA-512 context. |
| 182 | * \param output The SHA-384 or SHA-512 checksum result. |
| 183 | */ |
| 184 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha512_finish( mbedcrypto_sha512_context *ctx, |
| 185 | unsigned char output[64] ); |
| 186 | |
| 187 | /** |
| 188 | * \brief This function processes a single data block within |
| 189 | * the ongoing SHA-512 computation. This function is for |
| 190 | * internal use only. |
| 191 | * |
| 192 | * \deprecated Superseded by mbedcrypto_internal_sha512_process() in 2.7.0. |
| 193 | * |
| 194 | * \param ctx The SHA-512 context. |
| 195 | * \param data The buffer holding one block of data. |
| 196 | */ |
| 197 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha512_process( |
| 198 | mbedcrypto_sha512_context *ctx, |
| 199 | const unsigned char data[128] ); |
| 200 | |
| 201 | #undef MBEDCRYPTO_DEPRECATED |
| 202 | #endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 203 | |
| 204 | /** |
| 205 | * \brief This function calculates the SHA-512 or SHA-384 |
| 206 | * checksum of a buffer. |
| 207 | * |
| 208 | * The function allocates the context, performs the |
| 209 | * calculation, and frees the context. |
| 210 | * |
| 211 | * The SHA-512 result is calculated as |
| 212 | * output = SHA-512(input buffer). |
| 213 | * |
| 214 | * \param input The buffer holding the input data. |
| 215 | * \param ilen The length of the input data. |
| 216 | * \param output The SHA-384 or SHA-512 checksum result. |
| 217 | * \param is384 Determines which function to use: |
| 218 | * 0: Use SHA-512, or 1: Use SHA-384. |
| 219 | * |
| 220 | * \return \c 0 on success. |
| 221 | */ |
| 222 | int mbedcrypto_sha512_ret( const unsigned char *input, |
| 223 | size_t ilen, |
| 224 | unsigned char output[64], |
| 225 | int is384 ); |
| 226 | |
| 227 | #if !defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 228 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 229 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 230 | #else |
| 231 | #define MBEDCRYPTO_DEPRECATED |
| 232 | #endif |
| 233 | /** |
| 234 | * \brief This function calculates the SHA-512 or SHA-384 |
| 235 | * checksum of a buffer. |
| 236 | * |
| 237 | * The function allocates the context, performs the |
| 238 | * calculation, and frees the context. |
| 239 | * |
| 240 | * The SHA-512 result is calculated as |
| 241 | * output = SHA-512(input buffer). |
| 242 | * |
| 243 | * \deprecated Superseded by mbedcrypto_sha512_ret() in 2.7.0 |
| 244 | * |
| 245 | * \param input The buffer holding the data. |
| 246 | * \param ilen The length of the input data. |
| 247 | * \param output The SHA-384 or SHA-512 checksum result. |
| 248 | * \param is384 Determines which function to use: |
| 249 | * 0: Use SHA-512, or 1: Use SHA-384. |
| 250 | */ |
| 251 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha512( const unsigned char *input, |
| 252 | size_t ilen, |
| 253 | unsigned char output[64], |
| 254 | int is384 ); |
| 255 | |
| 256 | #undef MBEDCRYPTO_DEPRECATED |
| 257 | #endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 258 | /** |
| 259 | * \brief The SHA-384 or SHA-512 checkup routine. |
| 260 | * |
| 261 | * \return \c 0 on success. |
| 262 | * \return \c 1 on failure. |
| 263 | */ |
| 264 | int mbedcrypto_sha512_self_test( int verbose ); |
| 265 | |
| 266 | #ifdef __cplusplus |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | #endif /* mbedcrypto_sha512.h */ |