1 /** 2 * \file sha256.h 3 * 4 * \brief SHA-224 and SHA-256 cryptographic hash function 5 * 6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 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. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 #ifndef MBEDTLS_SHA256_H 24 #define MBEDTLS_SHA256_H 25 26 #if !defined(MBEDTLS_CONFIG_FILE) 27 #include "config.h" 28 #else 29 #include MBEDTLS_CONFIG_FILE 30 #endif 31 32 #include <stddef.h> 33 #include <stdint.h> 34 35 #if !defined(MBEDTLS_SHA256_ALT) 36 // Regular implementation 37 // 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif 42 43 /** 44 * \brief SHA-256 context structure 45 */ 46 typedef struct 47 { 48 uint32_t total[2]; /*!< number of bytes processed */ 49 uint32_t state[8]; /*!< intermediate digest state */ 50 unsigned char buffer[64]; /*!< data block being processed */ 51 int is224; /*!< 0 => SHA-256, else SHA-224 */ 52 } 53 mbedtls_sha256_context; 54 55 /** 56 * \brief Initialize SHA-256 context 57 * 58 * \param ctx SHA-256 context to be initialized 59 */ 60 void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); 61 62 /** 63 * \brief Clear SHA-256 context 64 * 65 * \param ctx SHA-256 context to be cleared 66 */ 67 void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); 68 69 /** 70 * \brief Clone (the state of) a SHA-256 context 71 * 72 * \param dst The destination context 73 * \param src The context to be cloned 74 */ 75 void mbedtls_sha256_clone( mbedtls_sha256_context *dst, 76 const mbedtls_sha256_context *src ); 77 78 /** 79 * \brief SHA-256 context setup 80 * 81 * \param ctx context to be initialized 82 * \param is224 0 = use SHA256, 1 = use SHA224 83 */ 84 void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); 85 86 /** 87 * \brief SHA-256 process buffer 88 * 89 * \param ctx SHA-256 context 90 * \param input buffer holding the data 91 * \param ilen length of the input data 92 */ 93 void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, 94 size_t ilen ); 95 96 /** 97 * \brief SHA-256 final digest 98 * 99 * \param ctx SHA-256 context 100 * \param output SHA-224/256 checksum result 101 */ 102 void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); 103 104 /* Internal use */ 105 void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); 106 107 #ifdef __cplusplus 108 } 109 #endif 110 111 #else /* MBEDTLS_SHA256_ALT */ 112 #include "sha256_alt.h" 113 #endif /* MBEDTLS_SHA256_ALT */ 114 115 #ifdef __cplusplus 116 extern "C" { 117 #endif 118 119 /** 120 * \brief Output = SHA-256( input buffer ) 121 * 122 * \param input buffer holding the data 123 * \param ilen length of the input data 124 * \param output SHA-224/256 checksum result 125 * \param is224 0 = use SHA256, 1 = use SHA224 126 */ 127 void mbedtls_sha256( const unsigned char *input, size_t ilen, 128 unsigned char output[32], int is224 ); 129 130 /** 131 * \brief Checkup routine 132 * 133 * \return 0 if successful, or 1 if the test failed 134 */ 135 int mbedtls_sha256_self_test( int verbose ); 136 137 #ifdef __cplusplus 138 } 139 #endif 140 141 #endif /* mbedtls_sha256.h */ 142