| 1 | /** |
| 2 | * \file mbedtls_sha256.h |
| 3 | * |
| 4 | * \brief SHA-224 and SHA-256 cryptographic hash function |
| 5 | * |
| 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
| 7 | * |
| 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | #ifndef MBEDTLS_SHA256_H |
| 25 | #define MBEDTLS_SHA256_H |
| 26 | |
| 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 28 | #include "config.h" |
| 29 | #else |
| 30 | #include MBEDTLS_CONFIG_FILE |
| 31 | #endif |
| 32 | |
| 33 | #include <stddef.h> |
| 34 | #include <stdint.h> |
| 35 | |
| 36 | #if !defined(MBEDTLS_SHA256_ALT) |
| 37 | // Regular implementation |
| 38 | // |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | extern "C" { |
| 42 | #endif |
| 43 | |
| 44 | /** |
| 45 | * \brief SHA-256 context structure |
| 46 | */ |
| 47 | typedef struct |
| 48 | { |
| 49 | uint32_t total[2]; /*!< number of bytes processed */ |
| 50 | uint32_t state[8]; /*!< intermediate digest state */ |
| 51 | unsigned char buffer[64]; /*!< data block being processed */ |
| 52 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 53 | } |
| 54 | mbedtls_sha256_context; |
| 55 | |
| 56 | /** |
| 57 | * \brief Initialize SHA-256 context |
| 58 | * |
| 59 | * \param ctx SHA-256 context to be initialized |
| 60 | */ |
| 61 | void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); |
| 62 | |
| 63 | /** |
| 64 | * \brief Clear SHA-256 context |
| 65 | * |
| 66 | * \param ctx SHA-256 context to be cleared |
| 67 | */ |
| 68 | void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); |
| 69 | |
| 70 | /** |
| 71 | * \brief Clone (the state of) a SHA-256 context |
| 72 | * |
| 73 | * \param dst The destination context |
| 74 | * \param src The context to be cloned |
| 75 | */ |
| 76 | void mbedtls_sha256_clone( mbedtls_sha256_context *dst, |
| 77 | const mbedtls_sha256_context *src ); |
| 78 | |
| 79 | /** |
| 80 | * \brief SHA-256 context setup |
| 81 | * |
| 82 | * \param ctx context to be initialized |
| 83 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 84 | */ |
| 85 | void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); |
| 86 | |
| 87 | /** |
| 88 | * \brief SHA-256 process buffer |
| 89 | * |
| 90 | * \param ctx SHA-256 context |
| 91 | * \param input buffer holding the data |
| 92 | * \param ilen length of the input data |
| 93 | */ |
| 94 | void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, |
| 95 | size_t ilen ); |
| 96 | |
| 97 | /** |
| 98 | * \brief SHA-256 final digest |
| 99 | * |
| 100 | * \param ctx SHA-256 context |
| 101 | * \param output SHA-224/256 checksum result |
| 102 | */ |
| 103 | void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); |
| 104 | |
| 105 | /* Internal use */ |
| 106 | void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); |
| 107 | |
| 108 | #ifdef __cplusplus |
| 109 | } |
| 110 | #endif |
| 111 | |
| 112 | #else /* MBEDTLS_SHA256_ALT */ |
| 113 | #include "sha256_alt.h" |
| 114 | #endif /* MBEDTLS_SHA256_ALT */ |
| 115 | |
| 116 | #ifdef __cplusplus |
| 117 | extern "C" { |
| 118 | #endif |
| 119 | |
| 120 | /** |
| 121 | * \brief Output = SHA-256( input buffer ) |
| 122 | * |
| 123 | * \param input buffer holding the data |
| 124 | * \param ilen length of the input data |
| 125 | * \param output SHA-224/256 checksum result |
| 126 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 127 | */ |
| 128 | void mbedtls_sha256( const unsigned char *input, size_t ilen, |
| 129 | unsigned char output[32], int is224 ); |
| 130 | |
| 131 | /** |
| 132 | * \brief Checkup routine |
| 133 | * |
| 134 | * \return 0 if successful, or 1 if the test failed |
| 135 | */ |
| 136 | int mbedtls_sha256_self_test( int verbose ); |
| 137 | |
| 138 | #ifdef __cplusplus |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | #endif /* mbedtls_sha256.h */ |