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