Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
wolfSSL 3.15.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* sha256.h
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22 /*!
wolfSSL 15:117db924cf7c 23 \file wolfssl/wolfcrypt/sha256.h
wolfSSL 15:117db924cf7c 24 */
wolfSSL 15:117db924cf7c 25
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 /* code submitted by raphael.huck@efixo.com */
wolfSSL 15:117db924cf7c 28
wolfSSL 15:117db924cf7c 29 #ifndef WOLF_CRYPT_SHA256_H
wolfSSL 15:117db924cf7c 30 #define WOLF_CRYPT_SHA256_H
wolfSSL 15:117db924cf7c 31
wolfSSL 15:117db924cf7c 32 #include <wolfssl/wolfcrypt/types.h>
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 35
wolfSSL 15:117db924cf7c 36 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 37 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
wolfSSL 15:117db924cf7c 38 #include <wolfssl/wolfcrypt/fips.h>
wolfSSL 15:117db924cf7c 39 #endif /* HAVE_FIPS_VERSION >= 2 */
wolfSSL 15:117db924cf7c 40
wolfSSL 15:117db924cf7c 41 #if defined(HAVE_FIPS) && \
wolfSSL 15:117db924cf7c 42 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
wolfSSL 15:117db924cf7c 43 #define wc_Sha256 Sha256
wolfSSL 15:117db924cf7c 44 #define WC_SHA256 SHA256
wolfSSL 15:117db924cf7c 45 #define WC_SHA256_BLOCK_SIZE SHA256_BLOCK_SIZE
wolfSSL 15:117db924cf7c 46 #define WC_SHA256_DIGEST_SIZE SHA256_DIGEST_SIZE
wolfSSL 15:117db924cf7c 47 #define WC_SHA256_PAD_SIZE SHA256_PAD_SIZE
wolfSSL 15:117db924cf7c 48
wolfSSL 15:117db924cf7c 49 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 50 #define wc_Sha224 Sha224
wolfSSL 15:117db924cf7c 51 #define WC_SHA224 SHA224
wolfSSL 15:117db924cf7c 52 #define WC_SHA224_BLOCK_SIZE SHA224_BLOCK_SIZE
wolfSSL 15:117db924cf7c 53 #define WC_SHA224_DIGEST_SIZE SHA224_DIGEST_SIZE
wolfSSL 15:117db924cf7c 54 #define WC_SHA224_PAD_SIZE SHA224_PAD_SIZE
wolfSSL 15:117db924cf7c 55 #endif
wolfSSL 15:117db924cf7c 56
wolfSSL 15:117db924cf7c 57 /* for fips @wc_fips */
wolfSSL 15:117db924cf7c 58 #include <cyassl/ctaocrypt/sha256.h>
wolfSSL 15:117db924cf7c 59 #endif
wolfSSL 15:117db924cf7c 60
wolfSSL 15:117db924cf7c 61 #ifdef FREESCALE_LTC_SHA
wolfSSL 15:117db924cf7c 62 #include "fsl_ltc.h"
wolfSSL 15:117db924cf7c 63 #endif
wolfSSL 15:117db924cf7c 64
wolfSSL 15:117db924cf7c 65
wolfSSL 15:117db924cf7c 66 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 67 extern "C" {
wolfSSL 15:117db924cf7c 68 #endif
wolfSSL 15:117db924cf7c 69
wolfSSL 15:117db924cf7c 70 /* avoid redefinition of structs */
wolfSSL 15:117db924cf7c 71 #if !defined(HAVE_FIPS) || \
wolfSSL 15:117db924cf7c 72 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
wolfSSL 15:117db924cf7c 73
wolfSSL 15:117db924cf7c 74 #ifdef WOLFSSL_MICROCHIP_PIC32MZ
wolfSSL 15:117db924cf7c 75 #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
wolfSSL 15:117db924cf7c 76 #endif
wolfSSL 15:117db924cf7c 77 #ifdef STM32_HASH
wolfSSL 15:117db924cf7c 78 #include <wolfssl/wolfcrypt/port/st/stm32.h>
wolfSSL 15:117db924cf7c 79 #endif
wolfSSL 15:117db924cf7c 80 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 81 #include <wolfssl/wolfcrypt/async.h>
wolfSSL 15:117db924cf7c 82 #endif
wolfSSL 15:117db924cf7c 83
wolfSSL 15:117db924cf7c 84 #if defined(_MSC_VER)
wolfSSL 15:117db924cf7c 85 #define SHA256_NOINLINE __declspec(noinline)
wolfSSL 15:117db924cf7c 86 #elif defined(__GNUC__)
wolfSSL 15:117db924cf7c 87 #define SHA256_NOINLINE __attribute__((noinline))
wolfSSL 15:117db924cf7c 88 #else
wolfSSL 15:117db924cf7c 89 #define SHA256_NOINLINE
wolfSSL 15:117db924cf7c 90 #endif
wolfSSL 15:117db924cf7c 91
wolfSSL 15:117db924cf7c 92 #if !defined(NO_OLD_SHA_NAMES)
wolfSSL 15:117db924cf7c 93 #define SHA256 WC_SHA256
wolfSSL 15:117db924cf7c 94 #endif
wolfSSL 15:117db924cf7c 95
wolfSSL 15:117db924cf7c 96 #ifndef NO_OLD_WC_NAMES
wolfSSL 15:117db924cf7c 97 #define Sha256 wc_Sha256
wolfSSL 15:117db924cf7c 98 #define SHA256_BLOCK_SIZE WC_SHA256_BLOCK_SIZE
wolfSSL 15:117db924cf7c 99 #define SHA256_DIGEST_SIZE WC_SHA256_DIGEST_SIZE
wolfSSL 15:117db924cf7c 100 #define SHA256_PAD_SIZE WC_SHA256_PAD_SIZE
wolfSSL 15:117db924cf7c 101 #endif
wolfSSL 15:117db924cf7c 102
wolfSSL 15:117db924cf7c 103 /* in bytes */
wolfSSL 15:117db924cf7c 104 enum {
wolfSSL 15:117db924cf7c 105 WC_SHA256 = WC_HASH_TYPE_SHA256,
wolfSSL 15:117db924cf7c 106 WC_SHA256_BLOCK_SIZE = 64,
wolfSSL 15:117db924cf7c 107 WC_SHA256_DIGEST_SIZE = 32,
wolfSSL 15:117db924cf7c 108 WC_SHA256_PAD_SIZE = 56
wolfSSL 15:117db924cf7c 109 };
wolfSSL 15:117db924cf7c 110
wolfSSL 15:117db924cf7c 111
wolfSSL 15:117db924cf7c 112 #ifdef WOLFSSL_TI_HASH
wolfSSL 15:117db924cf7c 113 #include "wolfssl/wolfcrypt/port/ti/ti-hash.h"
wolfSSL 15:117db924cf7c 114 #elif defined(WOLFSSL_IMX6_CAAM)
wolfSSL 15:117db924cf7c 115 #include "wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h"
wolfSSL 15:117db924cf7c 116 #else
wolfSSL 15:117db924cf7c 117 /* wc_Sha256 digest */
wolfSSL 15:117db924cf7c 118 typedef struct wc_Sha256 {
wolfSSL 15:117db924cf7c 119 #ifdef FREESCALE_LTC_SHA
wolfSSL 15:117db924cf7c 120 ltc_hash_ctx_t ctx;
wolfSSL 15:117db924cf7c 121 #elif defined(STM32_HASH)
wolfSSL 15:117db924cf7c 122 STM32_HASH_Context stmCtx;
wolfSSL 15:117db924cf7c 123 #else
wolfSSL 15:117db924cf7c 124 /* alignment on digest and buffer speeds up ARMv8 crypto operations */
wolfSSL 15:117db924cf7c 125 ALIGN16 word32 digest[WC_SHA256_DIGEST_SIZE / sizeof(word32)];
wolfSSL 15:117db924cf7c 126 ALIGN16 word32 buffer[WC_SHA256_BLOCK_SIZE / sizeof(word32)];
wolfSSL 15:117db924cf7c 127 word32 buffLen; /* in bytes */
wolfSSL 15:117db924cf7c 128 word32 loLen; /* length in bytes */
wolfSSL 15:117db924cf7c 129 word32 hiLen; /* length in bytes */
wolfSSL 15:117db924cf7c 130 void* heap;
wolfSSL 15:117db924cf7c 131 #ifdef USE_INTEL_SPEEDUP
wolfSSL 15:117db924cf7c 132 const byte* data;
wolfSSL 15:117db924cf7c 133 #endif
wolfSSL 15:117db924cf7c 134 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 15:117db924cf7c 135 hashUpdCache cache; /* cache for updates */
wolfSSL 15:117db924cf7c 136 #endif
wolfSSL 15:117db924cf7c 137 #ifdef WOLFSSL_ASYNC_CRYPT
wolfSSL 15:117db924cf7c 138 WC_ASYNC_DEV asyncDev;
wolfSSL 15:117db924cf7c 139 #endif /* WOLFSSL_ASYNC_CRYPT */
wolfSSL 15:117db924cf7c 140 #ifdef WOLFSSL_SMALL_STACK_CACHE
wolfSSL 15:117db924cf7c 141 word32* W;
wolfSSL 15:117db924cf7c 142 #endif
wolfSSL 15:117db924cf7c 143 #endif
wolfSSL 15:117db924cf7c 144 } wc_Sha256;
wolfSSL 15:117db924cf7c 145
wolfSSL 15:117db924cf7c 146 #endif
wolfSSL 15:117db924cf7c 147
wolfSSL 15:117db924cf7c 148 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 149
wolfSSL 15:117db924cf7c 150 WOLFSSL_API int wc_InitSha256(wc_Sha256*);
wolfSSL 15:117db924cf7c 151 WOLFSSL_API int wc_InitSha256_ex(wc_Sha256*, void*, int);
wolfSSL 15:117db924cf7c 152 WOLFSSL_API int wc_Sha256Update(wc_Sha256*, const byte*, word32);
wolfSSL 15:117db924cf7c 153 WOLFSSL_API int wc_Sha256FinalRaw(wc_Sha256*, byte*);
wolfSSL 15:117db924cf7c 154 WOLFSSL_API int wc_Sha256Final(wc_Sha256*, byte*);
wolfSSL 15:117db924cf7c 155 WOLFSSL_API void wc_Sha256Free(wc_Sha256*);
wolfSSL 15:117db924cf7c 156
wolfSSL 15:117db924cf7c 157 WOLFSSL_API int wc_Sha256GetHash(wc_Sha256*, byte*);
wolfSSL 15:117db924cf7c 158 WOLFSSL_API int wc_Sha256Copy(wc_Sha256* src, wc_Sha256* dst);
wolfSSL 15:117db924cf7c 159
wolfSSL 15:117db924cf7c 160 #ifdef WOLFSSL_PIC32MZ_HASH
wolfSSL 15:117db924cf7c 161 WOLFSSL_API void wc_Sha256SizeSet(wc_Sha256*, word32);
wolfSSL 15:117db924cf7c 162 #endif
wolfSSL 15:117db924cf7c 163
wolfSSL 15:117db924cf7c 164 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 165 /* avoid redefinition of structs */
wolfSSL 15:117db924cf7c 166 #if !defined(HAVE_FIPS) || \
wolfSSL 15:117db924cf7c 167 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
wolfSSL 15:117db924cf7c 168
wolfSSL 15:117db924cf7c 169 #ifndef NO_OLD_WC_NAMES
wolfSSL 15:117db924cf7c 170 #define Sha224 wc_Sha224
wolfSSL 15:117db924cf7c 171 #define SHA224 WC_SHA224
wolfSSL 15:117db924cf7c 172 #define SHA224_BLOCK_SIZE WC_SHA224_BLOCK_SIZE
wolfSSL 15:117db924cf7c 173 #define SHA224_DIGEST_SIZE WC_SHA224_DIGEST_SIZE
wolfSSL 15:117db924cf7c 174 #define SHA224_PAD_SIZE WC_SHA224_PAD_SIZE
wolfSSL 15:117db924cf7c 175 #endif
wolfSSL 15:117db924cf7c 176
wolfSSL 15:117db924cf7c 177 /* in bytes */
wolfSSL 15:117db924cf7c 178 enum {
wolfSSL 15:117db924cf7c 179 WC_SHA224 = WC_HASH_TYPE_SHA224,
wolfSSL 15:117db924cf7c 180 WC_SHA224_BLOCK_SIZE = WC_SHA256_BLOCK_SIZE,
wolfSSL 15:117db924cf7c 181 WC_SHA224_DIGEST_SIZE = 28,
wolfSSL 15:117db924cf7c 182 WC_SHA224_PAD_SIZE = WC_SHA256_PAD_SIZE
wolfSSL 15:117db924cf7c 183 };
wolfSSL 15:117db924cf7c 184
wolfSSL 15:117db924cf7c 185
wolfSSL 15:117db924cf7c 186 typedef wc_Sha256 wc_Sha224;
wolfSSL 15:117db924cf7c 187 #endif /* HAVE_FIPS */
wolfSSL 15:117db924cf7c 188
wolfSSL 15:117db924cf7c 189 WOLFSSL_API int wc_InitSha224(wc_Sha224*);
wolfSSL 15:117db924cf7c 190 WOLFSSL_API int wc_InitSha224_ex(wc_Sha224*, void*, int);
wolfSSL 15:117db924cf7c 191 WOLFSSL_API int wc_Sha224Update(wc_Sha224*, const byte*, word32);
wolfSSL 15:117db924cf7c 192 WOLFSSL_API int wc_Sha224Final(wc_Sha224*, byte*);
wolfSSL 15:117db924cf7c 193 WOLFSSL_API void wc_Sha224Free(wc_Sha224*);
wolfSSL 15:117db924cf7c 194
wolfSSL 15:117db924cf7c 195 WOLFSSL_API int wc_Sha224GetHash(wc_Sha224*, byte*);
wolfSSL 15:117db924cf7c 196 WOLFSSL_API int wc_Sha224Copy(wc_Sha224* src, wc_Sha224* dst);
wolfSSL 15:117db924cf7c 197
wolfSSL 15:117db924cf7c 198 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 199
wolfSSL 15:117db924cf7c 200 #ifdef __cplusplus
wolfSSL 15:117db924cf7c 201 } /* extern "C" */
wolfSSL 15:117db924cf7c 202 #endif
wolfSSL 15:117db924cf7c 203
wolfSSL 15:117db924cf7c 204 #endif /* NO_SHA256 */
wolfSSL 15:117db924cf7c 205 #endif /* WOLF_CRYPT_SHA256_H */
wolfSSL 15:117db924cf7c 206
wolfSSL 15:117db924cf7c 207