Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
ns_sha256.h
00001 /* 00002 * Copyright (c) 2006-2018, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 /** 00018 * \file ns_sha256.h 00019 * 00020 * \brief SHA-256 cryptographic hash function 00021 * 00022 * This file is derived from sha256.h in mbed TLS 2.3.0. 00023 * 00024 * This file provides an API very similar to mbed TLS, either implemented 00025 * locally, or by calling mbed TLS, depending on NS_USE_EXTERNAL_MBED_TLS. 00026 * 00027 * Differences from mbed TLS: 00028 * 00029 * a) ns_ prefix instead of mbedtls_; 00030 * b) Pointers are void * instead of unsigned char * to avoid type clashes; 00031 * c) SHA-224 not supported; 00032 * d) Ability to output truncated hashes. 00033 */ 00034 00035 00036 #ifndef NS_SHA256_H_ 00037 #define NS_SHA256_H_ 00038 00039 #ifdef NS_USE_EXTERNAL_MBED_TLS 00040 00041 #include <string.h> 00042 #include "mbedtls/sha256.h" 00043 00044 typedef mbedtls_sha256_context ns_sha256_context; 00045 00046 static inline void ns_sha256_init(ns_sha256_context *ctx) 00047 { 00048 mbedtls_sha256_init(ctx); 00049 } 00050 00051 static inline void ns_sha256_free(ns_sha256_context *ctx) 00052 { 00053 mbedtls_sha256_free(ctx); 00054 } 00055 00056 static inline void ns_sha256_clone(ns_sha256_context *dst, 00057 const ns_sha256_context *src) 00058 { 00059 mbedtls_sha256_clone(dst, src); 00060 } 00061 00062 static inline void ns_sha256_starts(ns_sha256_context *ctx) 00063 { 00064 mbedtls_sha256_starts(ctx, 0); 00065 } 00066 00067 static inline void ns_sha256_update(ns_sha256_context *ctx, const void *input, 00068 size_t ilen) 00069 { 00070 mbedtls_sha256_update(ctx, input, ilen); 00071 } 00072 00073 static inline void ns_sha256_finish(ns_sha256_context *ctx, void *output) 00074 { 00075 mbedtls_sha256_finish(ctx, output); 00076 } 00077 00078 static inline void ns_sha256(const void *input, size_t ilen, void *output) 00079 { 00080 mbedtls_sha256(input, ilen, output, 0); 00081 } 00082 00083 /* Extensions to standard mbed TLS - output the first bits of a hash only */ 00084 /* Number of bits must be a multiple of 32, and <=256 */ 00085 static inline void ns_sha256_finish_nbits(ns_sha256_context *ctx, void *output, unsigned obits) 00086 { 00087 if (obits == 256) { 00088 mbedtls_sha256_finish(ctx, output); 00089 } else { 00090 uint8_t sha256[32]; 00091 mbedtls_sha256_finish(ctx, sha256); 00092 memcpy(output, sha256, obits / 8); 00093 } 00094 } 00095 00096 static inline void ns_sha256_nbits(const void *input, size_t ilen, void *output, unsigned obits) 00097 { 00098 if (obits == 256) { 00099 mbedtls_sha256(input, ilen, output, 0); 00100 } else { 00101 uint8_t sha256[32]; 00102 mbedtls_sha256(input, ilen, sha256, 0); 00103 memcpy(output, sha256, obits / 8); 00104 } 00105 } 00106 00107 #else /* NS_USE_EXTERNAL_MBED_TLS */ 00108 00109 #include <stddef.h> 00110 #include <stdint.h> 00111 00112 /** 00113 * \brief SHA-256 context structure 00114 */ 00115 typedef struct { 00116 uint32_t total[2]; /*!< number of bytes processed */ 00117 uint32_t state[8]; /*!< intermediate digest state */ 00118 unsigned char buffer[64]; /*!< data block being processed */ 00119 } 00120 ns_sha256_context; 00121 00122 /** 00123 * \brief Initialize SHA-256 context 00124 * 00125 * \param ctx SHA-256 context to be initialized 00126 */ 00127 void ns_sha256_init(ns_sha256_context *ctx); 00128 00129 /** 00130 * \brief Clear SHA-256 context 00131 * 00132 * \param ctx SHA-256 context to be cleared 00133 */ 00134 void ns_sha256_free(ns_sha256_context *ctx); 00135 00136 /** 00137 * \brief Clone (the state of) a SHA-256 context 00138 * 00139 * \param dst The destination context 00140 * \param src The context to be cloned 00141 */ 00142 void ns_sha256_clone(ns_sha256_context *dst, 00143 const ns_sha256_context *src); 00144 00145 /** 00146 * \brief SHA-256 context setup 00147 * 00148 * \param ctx context to be initialized 00149 */ 00150 void ns_sha256_starts(ns_sha256_context *ctx); 00151 00152 /** 00153 * \brief SHA-256 process buffer 00154 * 00155 * \param ctx SHA-256 context 00156 * \param input buffer holding the data 00157 * \param ilen length of the input data 00158 */ 00159 void ns_sha256_update(ns_sha256_context *ctx, const void *input, 00160 size_t ilen); 00161 00162 /** 00163 * \brief SHA-256 final digest 00164 * 00165 * \param ctx SHA-256 context 00166 * \param output SHA-256 checksum result 00167 */ 00168 void ns_sha256_finish(ns_sha256_context *ctx, void *output); 00169 00170 /** 00171 * \brief SHA-256 final digest 00172 * 00173 * \param ctx SHA-256 context 00174 * \param output SHA-256 checksum result 00175 * \param obits Number of bits of to output - must be multiple of 32 00176 */ 00177 void ns_sha256_finish_nbits(ns_sha256_context *ctx, 00178 void *output, unsigned obits); 00179 00180 /** 00181 * \brief Output = SHA-256( input buffer ) 00182 * 00183 * \param input buffer holding the data 00184 * \param ilen length of the input data 00185 * \param output SHA-256 checksum result 00186 */ 00187 void ns_sha256(const void *input, size_t ilen, 00188 void *output); 00189 00190 /** 00191 * \brief Output = SHA-256( input buffer ) 00192 * 00193 * \param input buffer holding the data 00194 * \param ilen length of the input data 00195 * \param output SHA-256 checksum result 00196 * \param obits Number of bits of to output - must be multiple of 32 00197 */ 00198 void ns_sha256_nbits(const void *input, size_t ilen, 00199 void *output, unsigned obits); 00200 00201 #endif /* NS_USE_EXTERNAL_MBED_TLS */ 00202 00203 00204 #endif /* NS_SHA256_H_ */
Generated on Tue Jul 12 2022 13:54:39 by
