Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file sha1.h |
| 3 | * |
| 4 | * \brief This file contains SHA-1 definitions and functions. |
| 5 | * |
| 6 | * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in |
| 7 | * <em>FIPS 180-4: Secure Hash Standard (SHS)</em>. |
| 8 | * |
| 9 | * \warning SHA-1 is considered a weak message digest and its use constitutes |
| 10 | * a security risk. We recommend considering stronger message |
| 11 | * digests instead. |
| 12 | */ |
| 13 | /* |
| 14 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 15 | * SPDX-License-Identifier: Apache-2.0 |
| 16 | * |
| 17 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 18 | * not use this file except in compliance with the License. |
| 19 | * You may obtain a copy of the License at |
| 20 | * |
| 21 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | * |
| 23 | * Unless required by applicable law or agreed to in writing, software |
| 24 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 25 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | * See the License for the specific language governing permissions and |
| 27 | * limitations under the License. |
| 28 | * |
| 29 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 30 | */ |
| 31 | #ifndef MBEDCRYPTO_SHA1_H |
| 32 | #define MBEDCRYPTO_SHA1_H |
| 33 | |
| 34 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 35 | #include "config.h" |
| 36 | #else |
| 37 | #include MBEDCRYPTO_CONFIG_FILE |
| 38 | #endif |
| 39 | |
| 40 | #include <stddef.h> |
| 41 | #include <stdint.h> |
| 42 | |
| 43 | #define MBEDCRYPTO_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */ |
| 44 | |
| 45 | #ifdef __cplusplus |
| 46 | extern "C" { |
| 47 | #endif |
| 48 | |
| 49 | #if !defined(MBEDCRYPTO_SHA1_ALT) |
| 50 | // Regular implementation |
| 51 | // |
| 52 | |
| 53 | /** |
| 54 | * \brief The SHA-1 context structure. |
| 55 | * |
| 56 | * \warning SHA-1 is considered a weak message digest and its use |
| 57 | * constitutes a security risk. We recommend considering |
| 58 | * stronger message digests instead. |
| 59 | * |
| 60 | */ |
| 61 | typedef struct |
| 62 | { |
| 63 | uint32_t total[2]; /*!< The number of Bytes processed. */ |
| 64 | uint32_t state[5]; /*!< The intermediate digest state. */ |
| 65 | unsigned char buffer[64]; /*!< The data block being processed. */ |
| 66 | } |
| 67 | mbedcrypto_sha1_context; |
| 68 | |
| 69 | #else /* MBEDCRYPTO_SHA1_ALT */ |
| 70 | #include "sha1_alt.h" |
| 71 | #endif /* MBEDCRYPTO_SHA1_ALT */ |
| 72 | |
| 73 | /** |
| 74 | * \brief This function initializes a SHA-1 context. |
| 75 | * |
| 76 | * \warning SHA-1 is considered a weak message digest and its use |
| 77 | * constitutes a security risk. We recommend considering |
| 78 | * stronger message digests instead. |
| 79 | * |
| 80 | * \param ctx The SHA-1 context to initialize. |
| 81 | * |
| 82 | */ |
| 83 | void mbedcrypto_sha1_init( mbedcrypto_sha1_context *ctx ); |
| 84 | |
| 85 | /** |
| 86 | * \brief This function clears a SHA-1 context. |
| 87 | * |
| 88 | * \warning SHA-1 is considered a weak message digest and its use |
| 89 | * constitutes a security risk. We recommend considering |
| 90 | * stronger message digests instead. |
| 91 | * |
| 92 | * \param ctx The SHA-1 context to clear. |
| 93 | * |
| 94 | */ |
| 95 | void mbedcrypto_sha1_free( mbedcrypto_sha1_context *ctx ); |
| 96 | |
| 97 | /** |
| 98 | * \brief This function clones the state of a SHA-1 context. |
| 99 | * |
| 100 | * \warning SHA-1 is considered a weak message digest and its use |
| 101 | * constitutes a security risk. We recommend considering |
| 102 | * stronger message digests instead. |
| 103 | * |
| 104 | * \param dst The SHA-1 context to clone to. |
| 105 | * \param src The SHA-1 context to clone from. |
| 106 | * |
| 107 | */ |
| 108 | void mbedcrypto_sha1_clone( mbedcrypto_sha1_context *dst, |
| 109 | const mbedcrypto_sha1_context *src ); |
| 110 | |
| 111 | /** |
| 112 | * \brief This function starts a SHA-1 checksum calculation. |
| 113 | * |
| 114 | * \warning SHA-1 is considered a weak message digest and its use |
| 115 | * constitutes a security risk. We recommend considering |
| 116 | * stronger message digests instead. |
| 117 | * |
| 118 | * \param ctx The SHA-1 context to initialize. |
| 119 | * |
| 120 | * \return \c 0 on success. |
| 121 | * |
| 122 | */ |
| 123 | int mbedcrypto_sha1_starts_ret( mbedcrypto_sha1_context *ctx ); |
| 124 | |
| 125 | /** |
| 126 | * \brief This function feeds an input buffer into an ongoing SHA-1 |
| 127 | * checksum calculation. |
| 128 | * |
| 129 | * \warning SHA-1 is considered a weak message digest and its use |
| 130 | * constitutes a security risk. We recommend considering |
| 131 | * stronger message digests instead. |
| 132 | * |
| 133 | * \param ctx The SHA-1 context. |
| 134 | * \param input The buffer holding the input data. |
| 135 | * \param ilen The length of the input data. |
| 136 | * |
| 137 | * \return \c 0 on success. |
| 138 | */ |
| 139 | int mbedcrypto_sha1_update_ret( mbedcrypto_sha1_context *ctx, |
| 140 | const unsigned char *input, |
| 141 | size_t ilen ); |
| 142 | |
| 143 | /** |
| 144 | * \brief This function finishes the SHA-1 operation, and writes |
| 145 | * the result to the output buffer. |
| 146 | * |
| 147 | * \warning SHA-1 is considered a weak message digest and its use |
| 148 | * constitutes a security risk. We recommend considering |
| 149 | * stronger message digests instead. |
| 150 | * |
| 151 | * \param ctx The SHA-1 context. |
| 152 | * \param output The SHA-1 checksum result. |
| 153 | * |
| 154 | * \return \c 0 on success. |
| 155 | */ |
| 156 | int mbedcrypto_sha1_finish_ret( mbedcrypto_sha1_context *ctx, |
| 157 | unsigned char output[20] ); |
| 158 | |
| 159 | /** |
| 160 | * \brief SHA-1 process data block (internal use only). |
| 161 | * |
| 162 | * \warning SHA-1 is considered a weak message digest and its use |
| 163 | * constitutes a security risk. We recommend considering |
| 164 | * stronger message digests instead. |
| 165 | * |
| 166 | * \param ctx The SHA-1 context. |
| 167 | * \param data The data block being processed. |
| 168 | * |
| 169 | * \return \c 0 on success. |
| 170 | * |
| 171 | */ |
| 172 | int mbedcrypto_internal_sha1_process( mbedcrypto_sha1_context *ctx, |
| 173 | const unsigned char data[64] ); |
| 174 | |
| 175 | #if !defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 176 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 177 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 178 | #else |
| 179 | #define MBEDCRYPTO_DEPRECATED |
| 180 | #endif |
| 181 | /** |
| 182 | * \brief This function starts a SHA-1 checksum calculation. |
| 183 | * |
| 184 | * \warning SHA-1 is considered a weak message digest and its use |
| 185 | * constitutes a security risk. We recommend considering |
| 186 | * stronger message digests instead. |
| 187 | * |
| 188 | * \deprecated Superseded by mbedcrypto_sha1_starts_ret() in 2.7.0. |
| 189 | * |
| 190 | * \param ctx The SHA-1 context to initialize. |
| 191 | * |
| 192 | */ |
| 193 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha1_starts( mbedcrypto_sha1_context *ctx ); |
| 194 | |
| 195 | /** |
| 196 | * \brief This function feeds an input buffer into an ongoing SHA-1 |
| 197 | * checksum calculation. |
| 198 | * |
| 199 | * \warning SHA-1 is considered a weak message digest and its use |
| 200 | * constitutes a security risk. We recommend considering |
| 201 | * stronger message digests instead. |
| 202 | * |
| 203 | * \deprecated Superseded by mbedcrypto_sha1_update_ret() in 2.7.0. |
| 204 | * |
| 205 | * \param ctx The SHA-1 context. |
| 206 | * \param input The buffer holding the input data. |
| 207 | * \param ilen The length of the input data. |
| 208 | * |
| 209 | */ |
| 210 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha1_update( mbedcrypto_sha1_context *ctx, |
| 211 | const unsigned char *input, |
| 212 | size_t ilen ); |
| 213 | |
| 214 | /** |
| 215 | * \brief This function finishes the SHA-1 operation, and writes |
| 216 | * the result to the output buffer. |
| 217 | * |
| 218 | * \warning SHA-1 is considered a weak message digest and its use |
| 219 | * constitutes a security risk. We recommend considering |
| 220 | * stronger message digests instead. |
| 221 | * |
| 222 | * \deprecated Superseded by mbedcrypto_sha1_finish_ret() in 2.7.0. |
| 223 | * |
| 224 | * \param ctx The SHA-1 context. |
| 225 | * \param output The SHA-1 checksum result. |
| 226 | * |
| 227 | */ |
| 228 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha1_finish( mbedcrypto_sha1_context *ctx, |
| 229 | unsigned char output[20] ); |
| 230 | |
| 231 | /** |
| 232 | * \brief SHA-1 process data block (internal use only). |
| 233 | * |
| 234 | * \warning SHA-1 is considered a weak message digest and its use |
| 235 | * constitutes a security risk. We recommend considering |
| 236 | * stronger message digests instead. |
| 237 | * |
| 238 | * \deprecated Superseded by mbedcrypto_internal_sha1_process() in 2.7.0. |
| 239 | * |
| 240 | * \param ctx The SHA-1 context. |
| 241 | * \param data The data block being processed. |
| 242 | * |
| 243 | */ |
| 244 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha1_process( mbedcrypto_sha1_context *ctx, |
| 245 | const unsigned char data[64] ); |
| 246 | |
| 247 | #undef MBEDCRYPTO_DEPRECATED |
| 248 | #endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 249 | |
| 250 | /** |
| 251 | * \brief This function calculates the SHA-1 checksum of a buffer. |
| 252 | * |
| 253 | * The function allocates the context, performs the |
| 254 | * calculation, and frees the context. |
| 255 | * |
| 256 | * The SHA-1 result is calculated as |
| 257 | * output = SHA-1(input buffer). |
| 258 | * |
| 259 | * \warning SHA-1 is considered a weak message digest and its use |
| 260 | * constitutes a security risk. We recommend considering |
| 261 | * stronger message digests instead. |
| 262 | * |
| 263 | * \param input The buffer holding the input data. |
| 264 | * \param ilen The length of the input data. |
| 265 | * \param output The SHA-1 checksum result. |
| 266 | * |
| 267 | * \return \c 0 on success. |
| 268 | * |
| 269 | */ |
| 270 | int mbedcrypto_sha1_ret( const unsigned char *input, |
| 271 | size_t ilen, |
| 272 | unsigned char output[20] ); |
| 273 | |
| 274 | #if !defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 275 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 276 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 277 | #else |
| 278 | #define MBEDCRYPTO_DEPRECATED |
| 279 | #endif |
| 280 | /** |
| 281 | * \brief This function calculates the SHA-1 checksum of a buffer. |
| 282 | * |
| 283 | * The function allocates the context, performs the |
| 284 | * calculation, and frees the context. |
| 285 | * |
| 286 | * The SHA-1 result is calculated as |
| 287 | * output = SHA-1(input buffer). |
| 288 | * |
| 289 | * \warning SHA-1 is considered a weak message digest and its use |
| 290 | * constitutes a security risk. We recommend considering |
| 291 | * stronger message digests instead. |
| 292 | * |
| 293 | * \deprecated Superseded by mbedcrypto_sha1_ret() in 2.7.0 |
| 294 | * |
| 295 | * \param input The buffer holding the input data. |
| 296 | * \param ilen The length of the input data. |
| 297 | * \param output The SHA-1 checksum result. |
| 298 | * |
| 299 | */ |
| 300 | MBEDCRYPTO_DEPRECATED void mbedcrypto_sha1( const unsigned char *input, |
| 301 | size_t ilen, |
| 302 | unsigned char output[20] ); |
| 303 | |
| 304 | #undef MBEDCRYPTO_DEPRECATED |
| 305 | #endif /* !MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 306 | |
| 307 | /** |
| 308 | * \brief The SHA-1 checkup routine. |
| 309 | * |
| 310 | * \warning SHA-1 is considered a weak message digest and its use |
| 311 | * constitutes a security risk. We recommend considering |
| 312 | * stronger message digests instead. |
| 313 | * |
| 314 | * \return \c 0 on success. |
| 315 | * \return \c 1 on failure. |
| 316 | * |
| 317 | */ |
| 318 | int mbedcrypto_sha1_self_test( int verbose ); |
| 319 | |
| 320 | #ifdef __cplusplus |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | #endif /* mbedcrypto_sha1.h */ |