1 /* 2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the OpenSSL license (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #ifndef HEADER_SHA_H 11 # define HEADER_SHA_H 12 13 #ifdef USE_LIBC_SHA2 14 # include <sha2.h> 15 #endif 16 17 # include <openssl/e_os2.h> 18 # include <stddef.h> 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 /*- 25 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 26 * ! SHA_LONG has to be at least 32 bits wide. ! 27 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 28 */ 29 # define SHA_LONG unsigned int 30 31 # define SHA_LBLOCK 16 32 # define SHA_CBLOCK (SHA_LBLOCK*4)/* SHA treats input data as a 33 * contiguous array of 32 bit wide 34 * big-endian values. */ 35 # define SHA_LAST_BLOCK (SHA_CBLOCK-8) 36 # define SHA_DIGEST_LENGTH 20 37 38 typedef struct SHAstate_st { 39 SHA_LONG h0, h1, h2, h3, h4; 40 SHA_LONG Nl, Nh; 41 SHA_LONG data[SHA_LBLOCK]; 42 unsigned int num; 43 } SHA_CTX; 44 45 int SHA1_Init(SHA_CTX *c); 46 int SHA1_Update(SHA_CTX *c, const void *data, size_t len); 47 int SHA1_Final(unsigned char *md, SHA_CTX *c); 48 unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md); 49 void SHA1_Transform(SHA_CTX *c, const unsigned char *data); 50 51 #ifndef USE_LIBC_SHA2 52 # define SHA256_CBLOCK (SHA_LBLOCK*4)/* SHA-256 treats input data as a 53 * contiguous array of 32 bit wide 54 * big-endian values. */ 55 56 typedef struct SHA256state_st { 57 SHA_LONG h[8]; 58 SHA_LONG Nl, Nh; 59 SHA_LONG data[SHA_LBLOCK]; 60 unsigned int num, md_len; 61 } SHA256_CTX; 62 63 int SHA224_Init(SHA256_CTX *c); 64 int SHA224_Update(SHA256_CTX *c, const void *data, size_t len); 65 int SHA224_Final(unsigned char *md, SHA256_CTX *c); 66 unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md); 67 int SHA256_Init(SHA256_CTX *c); 68 int SHA256_Update(SHA256_CTX *c, const void *data, size_t len); 69 int SHA256_Final(unsigned char *md, SHA256_CTX *c); 70 unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md); 71 void SHA256_Transform(SHA256_CTX *c, const unsigned char *data); 72 73 # define SHA224_DIGEST_LENGTH 28 74 # define SHA256_DIGEST_LENGTH 32 75 # define SHA384_DIGEST_LENGTH 48 76 # define SHA512_DIGEST_LENGTH 64 77 78 /* 79 * Unlike 32-bit digest algorithms, SHA-512 *relies* on SHA_LONG64 80 * being exactly 64-bit wide. See Implementation Notes in sha512.c 81 * for further details. 82 */ 83 /* 84 * SHA-512 treats input data as a 85 * contiguous array of 64 bit 86 * wide big-endian values. 87 */ 88 # define SHA512_CBLOCK (SHA_LBLOCK*8) 89 # if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__) 90 # define SHA_LONG64 unsigned __int64 91 # define U64(C) C##UI64 92 # elif defined(__arch64__) 93 # define SHA_LONG64 unsigned long 94 # define U64(C) C##UL 95 # else 96 # define SHA_LONG64 unsigned long long 97 # define U64(C) C##ULL 98 # endif 99 100 typedef struct SHA512state_st { 101 SHA_LONG64 h[8]; 102 SHA_LONG64 Nl, Nh; 103 union { 104 SHA_LONG64 d[SHA_LBLOCK]; 105 unsigned char p[SHA512_CBLOCK]; 106 } u; 107 unsigned int num, md_len; 108 } SHA512_CTX; 109 110 int SHA384_Init(SHA512_CTX *c); 111 int SHA384_Update(SHA512_CTX *c, const void *data, size_t len); 112 int SHA384_Final(unsigned char *md, SHA512_CTX *c); 113 unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md); 114 int SHA512_Init(SHA512_CTX *c); 115 int SHA512_Update(SHA512_CTX *c, const void *data, size_t len); 116 int SHA512_Final(unsigned char *md, SHA512_CTX *c); 117 unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md); 118 void SHA512_Transform(SHA512_CTX *c, const unsigned char *data); 119 120 #else 121 #define SHA256_CBLOCK 64 122 #define SHA512_CBLOCK 128 123 unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md); 124 unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md); 125 unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md); 126 #endif 127 128 #ifdef __cplusplus 129 } 130 #endif 131 132 #endif 133