|
| 1 | +/* |
| 2 | + * sha256_alt.h SHA-256 hash |
| 3 | + ******************************************************************************* |
| 4 | + * Copyright (C) 2017, STMicroelectronics |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | + * not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | +#ifndef MBEDTLS_SHA256_ALT_H |
| 21 | +#define MBEDTLS_SHA256_ALT_H |
| 22 | + |
| 23 | +#if defined (MBEDTLS_SHA256_ALT) |
| 24 | +#include "mbedtls/platform.h" |
| 25 | +#include "mbedtls/config.h" |
| 26 | + |
| 27 | +#include "cmsis.h" |
| 28 | +#include <string.h> |
| 29 | +#ifdef __cplusplus |
| 30 | +extern "C" { |
| 31 | +#endif |
| 32 | + |
| 33 | +/** |
| 34 | + * \brief SHA-256 context structure |
| 35 | + */ |
| 36 | +typedef struct |
| 37 | +{ |
| 38 | + int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 39 | + HASH_HandleTypeDef hhash_sha256; |
| 40 | +} |
| 41 | +mbedtls_sha256_context; |
| 42 | + |
| 43 | +/** |
| 44 | + * \brief Initialize SHA-256 context |
| 45 | + * |
| 46 | + * \param ctx SHA-256 context to be initialized |
| 47 | + */ |
| 48 | +void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); |
| 49 | + |
| 50 | +/** |
| 51 | + * \brief Clear SHA-256 context |
| 52 | + * |
| 53 | + * \param ctx SHA-256 context to be cleared |
| 54 | + */ |
| 55 | +void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); |
| 56 | + |
| 57 | +/** |
| 58 | + * \brief Clone (the state of) a SHA-256 context |
| 59 | + * |
| 60 | + * \param dst The destination context |
| 61 | + * \param src The context to be cloned |
| 62 | + */ |
| 63 | +void mbedtls_sha256_clone( mbedtls_sha256_context *dst, |
| 64 | + const mbedtls_sha256_context *src ); |
| 65 | + |
| 66 | +/** |
| 67 | + * \brief SHA-256 context setup |
| 68 | + * |
| 69 | + * \param ctx context to be initialized |
| 70 | + * \param is224 0 = use SHA256, 1 = use SHA224 |
| 71 | + */ |
| 72 | +void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, int is224 ); |
| 73 | + |
| 74 | +/** |
| 75 | + * \brief SHA-256 process buffer |
| 76 | + * |
| 77 | + * \param ctx SHA-256 context |
| 78 | + * \param input buffer holding the data |
| 79 | + * \param ilen length of the input data |
| 80 | + */ |
| 81 | +void mbedtls_sha256_update( mbedtls_sha256_context *ctx, const unsigned char *input, |
| 82 | + size_t ilen ); |
| 83 | + |
| 84 | +/** |
| 85 | + * \brief SHA-256 final digest |
| 86 | + * |
| 87 | + * \param ctx SHA-256 context |
| 88 | + * \param output SHA-224/256 checksum result |
| 89 | + */ |
| 90 | +void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, unsigned char output[32] ); |
| 91 | + |
| 92 | +/* Internal use */ |
| 93 | +void mbedtls_sha256_process( mbedtls_sha256_context *ctx, const unsigned char data[64] ); |
| 94 | + |
| 95 | +#ifdef __cplusplus |
| 96 | +} |
| 97 | +#endif |
| 98 | + |
| 99 | +#ifdef __cplusplus |
| 100 | +extern "C" { |
| 101 | +#endif |
| 102 | + |
| 103 | +/** |
| 104 | + * \brief Output = SHA-256( input buffer ) |
| 105 | + * |
| 106 | + * \param input buffer holding the data |
| 107 | + * \param ilen length of the input data |
| 108 | + * \param output SHA-224/256 checksum result |
| 109 | + * \param is224 0 = use SHA256, 1 = use SHA224 |
| 110 | + */ |
| 111 | +void mbedtls_sha256( const unsigned char *input, size_t ilen, |
| 112 | + unsigned char output[32], int is224 ); |
| 113 | + |
| 114 | +/** |
| 115 | + * \brief Checkup routine |
| 116 | + * |
| 117 | + * \return 0 if successful, or 1 if the test failed |
| 118 | + */ |
| 119 | +int mbedtls_sha256_self_test( int verbose ); |
| 120 | + |
| 121 | +#ifdef __cplusplus |
| 122 | +} |
| 123 | +#endif |
| 124 | + |
| 125 | +#endif /* MBEDTLS_SHA256_ALT */ |
| 126 | + |
| 127 | +#endif /* sha1_alt.h */ |
0 commit comments