ssh lib

Dependents:   OS

Committer:
sPymbed
Date:
Mon Nov 25 14:23:49 2019 +0000
Revision:
1:e4ea39eba2fb
Parent:
0:1387ff3eed4a
improved

Who changed what in which revision?

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