Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

sha2.h
Go to the documentation of this file.
1 /*
2 * FILE: sha2.h
3 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
4 *
5 * Copyright (c) 2000-2001, Aaron D. Gifford
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $OrigId: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
33 * $RoughId: sha2.h,v 1.3 2002/02/24 08:14:32 knu Exp $
34 * $Id$
35 */
36
37 #ifndef __SHA2_H__
38 #define __SHA2_H__
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44
45 /*
46 * Import u_intXX_t size_t type definitions from system headers. You
47 * may need to change this, or define these things yourself in this
48 * file.
49 */
50 #include <sys/types.h>
51
52 #ifdef RUBY
53 # ifdef HAVE_PROTOTYPES
54 # undef NOPROTO
55 # else
56 # define NOPROTO
57 # endif /* HAVE_PROTOTYPES */
58 # ifndef BYTE_ORDER
59 # define LITTLE_ENDIAN 1234
60 # define BIG_ENDIAN 4321
61 # ifdef WORDS_BIGENDIAN
62 # define BYTE_ORDER BIG_ENDIAN
63 # else
64 # define BYTE_ORDER LITTLE_ENDIAN
65 # endif
66 # endif /* BYTE_ORDER */
67 # define SHA2_USE_INTTYPES_H
68 #else /* RUBY */
69 #ifdef SHA2_USE_INTTYPES_H
70
71 #include <inttypes.h>
72
73 #endif /* SHA2_USE_INTTYPES_H */
74 #endif /* RUBY */
75
76
77 /*** SHA-256/384/512 Various Length Definitions ***********************/
78 #define SHA256_BLOCK_LENGTH 64
79 #define SHA256_DIGEST_LENGTH 32
80 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
81 #define SHA384_BLOCK_LENGTH 128
82 #define SHA384_DIGEST_LENGTH 48
83 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
84 #define SHA512_BLOCK_LENGTH 128
85 #define SHA512_DIGEST_LENGTH 64
86 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
87
88
89 /*** SHA-256/384/512 Context Structures *******************************/
90 /* NOTE: If your architecture does not define either u_intXX_t types or
91 * uintXX_t (from inttypes.h), you may need to define things by hand
92 * for your system:
93 */
94 #ifndef SHA2_USE_INTTYPES_H
95 # ifdef HAVE_U_INT8_T
96 typedef u_int8_t uint8_t; /* 1-byte (8-bits) */
97 typedef u_int32_t uint32_t; /* 4-bytes (32-bits) */
98 typedef u_int64_t uint64_t; /* 8-bytes (64-bits) */
99 # else
100 typedef unsigned char uint8_t; /* 1-byte (8-bits) */
101 typedef unsigned int uint32_t; /* 4-bytes (32-bits) */
102 typedef unsigned long long uint64_t; /* 8-bytes (64-bits) */
103 # endif
104 #endif
105
106 /*
107 * Most BSD systems already define u_intXX_t types, as does Linux.
108 * Some systems, however, like Compaq's Tru64 Unix instead can use
109 * uintXX_t types defined by very recent ANSI C standards and included
110 * in the file:
111 *
112 * #include <inttypes.h>
113 *
114 * If you choose to use <inttypes.h> then please define:
115 *
116 * #define SHA2_USE_INTTYPES_H
117 *
118 * Or on the command line during compile:
119 *
120 * cc -DSHA2_USE_INTTYPES_H ...
121 */
122 typedef struct _SHA256_CTX {
123 uint32_t state[8];
124 uint64_t bitcount;
125 uint8_t buffer[SHA256_BLOCK_LENGTH];
126 } SHA256_CTX;
127 typedef struct _SHA512_CTX {
128 uint64_t state[8];
129 uint64_t bitcount[2];
130 uint8_t buffer[SHA512_BLOCK_LENGTH];
131 } SHA512_CTX;
132
133 typedef SHA512_CTX SHA384_CTX;
134
135
136 /*** SHA-256/384/512 Function Prototypes ******************************/
137 #ifdef RUBY
138 #define SHA256_Init rb_Digest_SHA256_Init
139 #define SHA256_Update rb_Digest_SHA256_Update
140 #define SHA256_Finish rb_Digest_SHA256_Finish
141 #define SHA256_Data rb_Digest_SHA256_Data
142 #define SHA256_End rb_Digest_SHA256_End
143 #define SHA256_Last rb_Digest_SHA256_Last
144 #define SHA256_Transform rb_Digest_SHA256_Transform
145 #define SHA256_Final(d, c) SHA256_Finish(c, d)
146
147 #define SHA384_Init rb_Digest_SHA384_Init
148 #define SHA384_Update rb_Digest_SHA384_Update
149 #define SHA384_Finish rb_Digest_SHA384_Finish
150 #define SHA384_Data rb_Digest_SHA384_Data
151 #define SHA384_End rb_Digest_SHA384_End
152 #define SHA384_Last rb_Digest_SHA384_Last
153 #define SHA384_Transform rb_Digest_SHA384_Transform
154 #define SHA384_Final(d, c) SHA384_Finish(c, d)
155
156 #define SHA512_Init rb_Digest_SHA512_Init
157 #define SHA512_Update rb_Digest_SHA512_Update
158 #define SHA512_Finish rb_Digest_SHA512_Finish
159 #define SHA512_Data rb_Digest_SHA512_Data
160 #define SHA512_End rb_Digest_SHA512_End
161 #define SHA512_Last rb_Digest_SHA512_Last
162 #define SHA512_Transform rb_Digest_SHA512_Transform
163 #define SHA512_Final(d, c) SHA512_Finish(c, d)
164 #endif /* RUBY */
165
166 #ifndef NOPROTO
167
168 int SHA256_Init(SHA256_CTX *);
169 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
170 int SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
171 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
172 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
173
174 int SHA384_Init(SHA384_CTX*);
175 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
176 int SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
177 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
178 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
179
180 int SHA512_Init(SHA512_CTX*);
181 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
182 int SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
183 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
184 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
185
186 #else /* NOPROTO */
187
188 int SHA256_Init();
189 void SHA256_Update();
190 #ifdef RUBY
191 int SHA256_Finish();
192 #else
193 int SHA256_Final();
194 #endif /* RUBY */
195 char* SHA256_End();
196 char* SHA256_Data();
197
198 int SHA384_Init();
199 void SHA384_Update();
200 #ifdef RUBY
201 int SHA384_Finish();
202 #else
203 int SHA384_Final();
204 #endif /* RUBY */
205 char* SHA384_End();
206 char* SHA384_Data();
207
208 int SHA512_Init();
209 void SHA512_Update();
210 #ifdef RUBY
211 int SHA512_Finish();
212 #else
213 int SHA512_Final();
214 #endif /* RUBY */
215 char* SHA512_End();
216 char* SHA512_Data();
217
218 #endif /* NOPROTO */
219
220 #ifdef __cplusplus
221 }
222 #endif /* __cplusplus */
223
224 #endif /* __SHA2_H__ */
225
SHA512_DIGEST_LENGTH
#define SHA512_DIGEST_LENGTH
Definition: sha2.h:85
_SHA256_CTX::bitcount
uint64_t bitcount
Definition: sha2.h:124
SHA384_Finish
#define SHA384_Finish
SHA384_DIGEST_STRING_LENGTH
#define SHA384_DIGEST_STRING_LENGTH
Definition: sha2.h:83
SHA384_Update
void SHA384_Update(SHA384_CTX *, const uint8_t *, size_t)
Definition: sha2.c:1018
SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH
Definition: sha2.h:79
SHA512_CTX
struct _SHA512_CTX SHA512_CTX
SHA384_Data
char * SHA384_Data(const uint8_t *, size_t, char[SHA384_DIGEST_STRING_LENGTH])
Definition: sha2.c:1074
_SHA256_CTX
Definition: sha2.h:122
SHA256_DIGEST_STRING_LENGTH
#define SHA256_DIGEST_STRING_LENGTH
Definition: sha2.h:80
uint64_t
unsigned long long uint64_t
Definition: sha2.h:102
u_int32_t
__uint32_t u_int32_t
Definition: rb_mjit_min_header-2.7.0.h:1159
_SHA512_CTX::bitcount
uint64_t bitcount[2]
Definition: sha2.h:129
uint32_t
unsigned int uint32_t
Definition: sha2.h:101
SHA512_Finish
#define SHA512_Finish
SHA256_Finish
#define SHA256_Finish
u_int64_t
__uint64_t u_int64_t
Definition: rb_mjit_min_header-2.7.0.h:1160
SHA384_CTX
SHA512_CTX SHA384_CTX
Definition: sha2.h:133
SHA512_Update
void SHA512_Update(SHA512_CTX *, const uint8_t *, size_t)
Definition: sha2.c:854
_SHA512_CTX
Definition: sha2.h:127
SHA512_DIGEST_STRING_LENGTH
#define SHA512_DIGEST_STRING_LENGTH
Definition: sha2.h:86
SHA256_End
char * SHA256_End(SHA256_CTX *, char[SHA256_DIGEST_STRING_LENGTH])
SHA256_BLOCK_LENGTH
#define SHA256_BLOCK_LENGTH
Definition: sha2.h:78
SHA512_Data
char * SHA512_Data(const uint8_t *, size_t, char[SHA512_DIGEST_STRING_LENGTH])
Definition: sha2.c:998
SHA512_Final
int SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX *)
SHA256_Update
void SHA256_Update(SHA256_CTX *, const uint8_t *, size_t)
Definition: sha2.c:529
SHA512_Init
int SHA512_Init(SHA512_CTX *)
Definition: sha2.c:675
uint8_t
unsigned char uint8_t
Definition: sha2.h:100
_SHA512_CTX::state
uint64_t state[8]
Definition: sha2.h:128
SHA384_Final
int SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX *)
_SHA512_CTX::buffer
uint8_t buffer[SHA512_BLOCK_LENGTH]
Definition: sha2.h:130
SHA384_Init
int SHA384_Init(SHA384_CTX *)
Definition: sha2.c:1008
SHA384_DIGEST_LENGTH
#define SHA384_DIGEST_LENGTH
Definition: sha2.h:82
SHA256_Data
char * SHA256_Data(const uint8_t *, size_t, char[SHA256_DIGEST_STRING_LENGTH])
Definition: sha2.c:665
SHA512_End
char * SHA512_End(SHA512_CTX *, char[SHA512_DIGEST_STRING_LENGTH])
SHA256_CTX
struct _SHA256_CTX SHA256_CTX
SHA384_End
char * SHA384_End(SHA384_CTX *, char[SHA384_DIGEST_STRING_LENGTH])
SHA256_Init
int SHA256_Init(SHA256_CTX *)
Definition: sha2.c:344
SHA512_BLOCK_LENGTH
#define SHA512_BLOCK_LENGTH
Definition: sha2.h:84
_SHA256_CTX::state
uint32_t state[8]
Definition: sha2.h:123
u_int8_t
__uint8_t u_int8_t
Definition: rb_mjit_min_header-2.7.0.h:1157
SHA256_Final
int SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX *)
_SHA256_CTX::buffer
uint8_t buffer[SHA256_BLOCK_LENGTH]
Definition: sha2.h:125