ssh lib

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha256.h Source File

sha256.h

00001 /* sha256.h
00002  *
00003  * Copyright (C) 2006-2017 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 /*!
00023     \file wolfssl/wolfcrypt/sha256.h
00024 */
00025 
00026 
00027 /* code submitted by raphael.huck@efixo.com */
00028 
00029 #ifndef WOLF_CRYPT_SHA256_H
00030 #define WOLF_CRYPT_SHA256_H
00031 
00032 #include <wolfcrypt/types.h>
00033 
00034 #ifndef NO_SHA256
00035 
00036 #if defined(HAVE_FIPS) && \
00037     defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
00038     #include <wolfcrypt/fips.h>
00039 #endif /* HAVE_FIPS_VERSION >= 2 */
00040 
00041 #if defined(HAVE_FIPS) && \
00042     (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
00043     #define wc_Sha256             Sha256
00044     #define WC_SHA256             SHA256
00045     #define WC_SHA256_BLOCK_SIZE  SHA256_BLOCK_SIZE
00046     #define WC_SHA256_DIGEST_SIZE SHA256_DIGEST_SIZE
00047     #define WC_SHA256_PAD_SIZE    SHA256_PAD_SIZE
00048 
00049     #ifdef WOLFSSL_SHA224
00050         #define wc_Sha224             Sha224
00051         #define WC_SHA224             SHA224
00052         #define WC_SHA224_BLOCK_SIZE  SHA224_BLOCK_SIZE
00053         #define WC_SHA224_DIGEST_SIZE SHA224_DIGEST_SIZE
00054         #define WC_SHA224_PAD_SIZE    SHA224_PAD_SIZE
00055     #endif
00056 
00057     /* for fips @wc_fips */
00058     #include <cyassl/ctaocrypt/sha256.h>
00059 #endif
00060 
00061 #ifdef FREESCALE_LTC_SHA
00062     #include "fsl_ltc.h"
00063 #endif
00064 
00065 
00066 #ifdef __cplusplus
00067     extern "C" {
00068 #endif
00069 
00070 /* avoid redefinition of structs */
00071 #if !defined(HAVE_FIPS) || \
00072     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
00073 
00074 #ifdef WOLFSSL_MICROCHIP_PIC32MZ
00075     #include <wolfcrypt/port/pic32/pic32mz-crypt.h>
00076 #endif
00077 #ifdef STM32_HASH
00078     #include <wolfcrypt/port/st/stm32.h>
00079 #endif
00080 #ifdef WOLFSSL_ASYNC_CRYPT
00081     #include <wolfcrypt/async.h>
00082 #endif
00083 
00084 #if defined(_MSC_VER)
00085     #define SHA256_NOINLINE __declspec(noinline)
00086 #elif defined(__GNUC__)
00087     #define SHA256_NOINLINE __attribute__((noinline))
00088 #else
00089     #define SHA256_NOINLINE
00090 #endif
00091 
00092 #if !defined(NO_OLD_SHA_NAMES)
00093     #define SHA256             WC_SHA256
00094 #endif
00095 
00096 #ifndef NO_OLD_WC_NAMES
00097     #define Sha256             wc_Sha256
00098     #define SHA256_BLOCK_SIZE  WC_SHA256_BLOCK_SIZE
00099     #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
00100     #define SHA256_PAD_SIZE    WC_SHA256_PAD_SIZE
00101 #endif
00102 
00103 /* in bytes */
00104 enum {
00105     WC_SHA256              =  WC_HASH_TYPE_SHA256,
00106     WC_SHA256_BLOCK_SIZE   = 64,
00107     WC_SHA256_DIGEST_SIZE  = 32,
00108     WC_SHA256_PAD_SIZE     = 56
00109 };
00110 
00111 
00112 #ifdef WOLFSSL_TI_HASH
00113     #include "wolfcrypt/port/ti/ti-hash.h"
00114 #elif defined(WOLFSSL_IMX6_CAAM)
00115     #include "wolfcrypt/port/caam/wolfcaam_sha.h"
00116 #else
00117 /* wc_Sha256 digest */
00118 typedef struct wc_Sha256 {
00119 #ifdef FREESCALE_LTC_SHA
00120     ltc_hash_ctx_t ctx;
00121 #elif defined(STM32_HASH)
00122     STM32_HASH_Context stmCtx;
00123 #else
00124     /* alignment on digest and buffer speeds up ARMv8 crypto operations */
00125     ALIGN16 word32  digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
00126     ALIGN16 word32  buffer[WC_SHA256_BLOCK_SIZE  / sizeof(word32)];
00127     word32  buffLen;   /* in bytes          */
00128     word32  loLen;     /* length in bytes   */
00129     word32  hiLen;     /* length in bytes   */
00130     void*   heap;
00131 #ifdef USE_INTEL_SPEEDUP
00132     const byte* data;
00133 #endif
00134 #ifdef WOLFSSL_PIC32MZ_HASH
00135     hashUpdCache cache; /* cache for updates */
00136 #endif
00137 #ifdef WOLFSSL_ASYNC_CRYPT
00138     WC_ASYNC_DEV asyncDev;
00139 #endif /* WOLFSSL_ASYNC_CRYPT */
00140 #ifdef WOLFSSL_SMALL_STACK_CACHE
00141     word32* W;
00142 #endif
00143 #endif
00144 } wc_Sha256;
00145 
00146 #endif
00147 
00148 #endif /* HAVE_FIPS */
00149 
00150 WOLFSSL_API int wc_InitSha256(wc_Sha256*);
00151 WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int);
00152 WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32);
00153 WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*);
00154 WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*);
00155 WOLFSSL_API void wc_Sha256Free(wc_Sha256*);
00156 
00157 WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*);
00158 WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
00159 
00160 #ifdef WOLFSSL_PIC32MZ_HASH
00161 WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
00162 #endif
00163 
00164 #ifdef WOLFSSL_SHA224
00165 /* avoid redefinition of structs */
00166 #if !defined(HAVE_FIPS) || \
00167     (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
00168 
00169 #ifndef NO_OLD_WC_NAMES
00170     #define Sha224             wc_Sha224
00171     #define SHA224             WC_SHA224
00172     #define SHA224_BLOCK_SIZE  WC_SHA224_BLOCK_SIZE
00173     #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
00174     #define SHA224_PAD_SIZE    WC_SHA224_PAD_SIZE
00175 #endif
00176 
00177 /* in bytes */
00178 enum {
00179     WC_SHA224              =   WC_HASH_TYPE_SHA224,
00180     WC_SHA224_BLOCK_SIZE   =   WC_SHA256_BLOCK_SIZE,
00181     WC_SHA224_DIGEST_SIZE  =   28,
00182     WC_SHA224_PAD_SIZE     =   WC_SHA256_PAD_SIZE
00183 };
00184 
00185 
00186 typedef wc_Sha256 wc_Sha224;
00187 #endif /* HAVE_FIPS */
00188 
00189 WOLFSSL_API int wc_InitSha224(wc_Sha224*);
00190 WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int);
00191 WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32);
00192 WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*);
00193 WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
00194 
00195 WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
00196 WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
00197 
00198 #endif /* WOLFSSL_SHA224 */
00199 
00200 #ifdef __cplusplus
00201     } /* extern "C" */
00202 #endif
00203 
00204 #endif /* NO_SHA256 */
00205 #endif /* WOLF_CRYPT_SHA256_H */
00206 
00207