Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ns_sha256.h Source File

ns_sha256.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006-2017, 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 {
00117     uint32_t total[2];          /*!< number of bytes processed  */
00118     uint32_t state[8];          /*!< intermediate digest state  */
00119     unsigned char buffer[64];   /*!< data block being processed */
00120 }
00121 ns_sha256_context;
00122 
00123 /**
00124  * \brief          Initialize SHA-256 context
00125  *
00126  * \param ctx      SHA-256 context to be initialized
00127  */
00128 void ns_sha256_init( ns_sha256_context *ctx );
00129 
00130 /**
00131  * \brief          Clear SHA-256 context
00132  *
00133  * \param ctx      SHA-256 context to be cleared
00134  */
00135 void ns_sha256_free( ns_sha256_context *ctx );
00136 
00137 /**
00138  * \brief          Clone (the state of) a SHA-256 context
00139  *
00140  * \param dst      The destination context
00141  * \param src      The context to be cloned
00142  */
00143 void ns_sha256_clone( ns_sha256_context *dst,
00144                       const ns_sha256_context *src );
00145 
00146 /**
00147  * \brief          SHA-256 context setup
00148  *
00149  * \param ctx      context to be initialized
00150  */
00151 void ns_sha256_starts( ns_sha256_context *ctx );
00152 
00153 /**
00154  * \brief          SHA-256 process buffer
00155  *
00156  * \param ctx      SHA-256 context
00157  * \param input    buffer holding the  data
00158  * \param ilen     length of the input data
00159  */
00160 void ns_sha256_update( ns_sha256_context *ctx, const void *input,
00161                        size_t ilen );
00162 
00163 /**
00164  * \brief          SHA-256 final digest
00165  *
00166  * \param ctx      SHA-256 context
00167  * \param output   SHA-256 checksum result
00168  */
00169 void ns_sha256_finish( ns_sha256_context *ctx, void *output );
00170 
00171 /**
00172  * \brief          SHA-256 final digest
00173  *
00174  * \param ctx      SHA-256 context
00175  * \param output   SHA-256 checksum result
00176  * \param obits    Number of bits of to output - must be multiple of 32
00177  */
00178 void ns_sha256_finish_nbits( ns_sha256_context *ctx,
00179                              void *output, unsigned obits );
00180 
00181 /**
00182  * \brief          Output = SHA-256( input buffer )
00183  *
00184  * \param input    buffer holding the  data
00185  * \param ilen     length of the input data
00186  * \param output   SHA-256 checksum result
00187  */
00188 void ns_sha256( const void *input, size_t ilen,
00189                 void *output );
00190 
00191 /**
00192  * \brief          Output = SHA-256( input buffer )
00193  *
00194  * \param input    buffer holding the  data
00195  * \param ilen     length of the input data
00196  * \param output   SHA-256 checksum result
00197  * \param obits    Number of bits of to output - must be multiple of 32
00198  */
00199 void ns_sha256_nbits( const void *input, size_t ilen,
00200                       void *output, unsigned obits );
00201 
00202 #endif /* NS_USE_EXTERNAL_MBED_TLS */
00203 
00204 
00205 #endif /* NS_SHA256_H_ */