rubysl-digest 2.0.3 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -10
  3. data/MRI_LICENSE +56 -0
  4. data/ext/rubysl/digest/bubblebabble/bubblebabble.c +27 -28
  5. data/ext/rubysl/digest/defs.h +1 -1
  6. data/ext/rubysl/digest/digest.c +142 -58
  7. data/ext/rubysl/digest/digest.h +25 -5
  8. data/ext/rubysl/digest/digest_conf.rb +28 -0
  9. data/ext/rubysl/digest/extconf.rb +1 -1
  10. data/ext/rubysl/digest/md5/extconf.rb +3 -13
  11. data/ext/rubysl/digest/md5/md5.c +9 -7
  12. data/ext/rubysl/digest/md5/md5.h +3 -3
  13. data/ext/rubysl/digest/md5/md5cc.h +12 -0
  14. data/ext/rubysl/digest/md5/md5init.c +12 -5
  15. data/ext/rubysl/digest/md5/md5ossl.h +4 -2
  16. data/ext/rubysl/digest/rmd160/extconf.rb +3 -12
  17. data/ext/rubysl/digest/rmd160/rmd160.c +12 -6
  18. data/ext/rubysl/digest/rmd160/rmd160.h +3 -3
  19. data/ext/rubysl/digest/rmd160/rmd160init.c +10 -5
  20. data/ext/rubysl/digest/rmd160/rmd160ossl.h +3 -2
  21. data/ext/rubysl/digest/sha1/extconf.rb +3 -12
  22. data/ext/rubysl/digest/sha1/sha1.c +5 -3
  23. data/ext/rubysl/digest/sha1/sha1.h +3 -3
  24. data/ext/rubysl/digest/sha1/sha1cc.h +14 -0
  25. data/ext/rubysl/digest/sha1/sha1init.c +12 -5
  26. data/ext/rubysl/digest/sha1/sha1ossl.h +4 -2
  27. data/ext/rubysl/digest/sha2/extconf.rb +7 -8
  28. data/ext/rubysl/digest/sha2/sha2.c +235 -73
  29. data/ext/rubysl/digest/sha2/sha2.h +142 -26
  30. data/ext/rubysl/digest/sha2/sha2cc.h +31 -0
  31. data/ext/rubysl/digest/sha2/sha2init.c +12 -4
  32. data/ext/rubysl/digest/sha2/sha2ossl.h +27 -0
  33. data/ext/rubysl/openssl/deprecation.rb +21 -0
  34. data/lib/digest/sha2.rb +42 -9
  35. data/lib/rubysl/digest/digest.rb +27 -7
  36. data/lib/rubysl/digest/version.rb +1 -1
  37. data/spec/fixtures/dir/common.rb +171 -0
  38. data/spec/md5/file_spec.rb +1 -1
  39. data/spec/sha1/file_spec.rb +1 -1
  40. data/spec/sha256/file_spec.rb +1 -1
  41. data/spec/sha384/file_spec.rb +1 -1
  42. data/spec/sha512/file_spec.rb +1 -1
  43. data/spec/shared/file/read.rb +21 -0
  44. metadata +15 -5
  45. data/ext/rubysl/digest/bubblebabble/depend +0 -3
  46. data/ext/rubysl/digest/bubblebabble/extconf.h +0 -4
@@ -1,4 +1,4 @@
1
- /* $Id: rmd160ossl.h 11708 2007-02-12 23:01:19Z shyouhei $ */
1
+ /* $Id$ */
2
2
 
3
3
  #ifndef RMD160OSSL_H_INCLUDED
4
4
  #define RMD160OSSL_H_INCLUDED
@@ -14,6 +14,7 @@
14
14
  #define RMD160_BLOCK_LENGTH RIPEMD160_CBLOCK
15
15
  #define RMD160_DIGEST_LENGTH RIPEMD160_DIGEST_LENGTH
16
16
 
17
- void RMD160_Finish(RMD160_CTX *ctx, char *buf);
17
+ static DEFINE_FINISH_FUNC_FROM_FINAL(RIPEMD160)
18
+ #define RMD160_Finish rb_digest_RIPEMD160_finish
18
19
 
19
20
  #endif
@@ -1,28 +1,19 @@
1
+ # -*- coding: us-ascii -*-
1
2
  # $RoughId: extconf.rb,v 1.3 2001/08/14 19:54:51 knu Exp $
2
3
  # $Id$
3
4
 
4
5
  require "mkmf"
6
+ require File.expand_path("../../digest_conf", __FILE__)
5
7
 
6
8
  $defs << "-DHAVE_CONFIG_H"
7
9
  $INCFLAGS << " -I$(srcdir)/.."
8
10
 
9
11
  $objs = [ "sha1init.#{$OBJEXT}" ]
10
12
 
11
- dir_config("openssl")
12
-
13
- if !with_config("bundled-sha1") &&
14
- have_library("crypto") && have_header("openssl/sha.h")
15
- $objs << "sha1ossl.#{$OBJEXT}"
16
- else
17
- $objs << "sha1.#{$OBJEXT}"
18
- end
13
+ digest_conf("sha1", "sha")
19
14
 
20
15
  have_header("sys/cdefs.h")
21
16
 
22
- have_header("inttypes.h")
23
-
24
- have_header("unistd.h")
25
-
26
17
  $preload = %w[digest]
27
18
 
28
19
  create_makefile("digest/sha1/sha1")
@@ -1,7 +1,7 @@
1
1
  /* $NetBSD: sha1.c,v 1.2 2001/03/22 09:51:48 agc Exp $ */
2
2
  /* $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $ */
3
3
  /* $RoughId: sha1.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
4
- /* $Id: sha1.c 11708 2007-02-12 23:01:19Z shyouhei $ */
4
+ /* $Id$ */
5
5
 
6
6
  /*
7
7
  * SHA-1 in C
@@ -199,7 +199,7 @@ void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
199
199
  /*
200
200
  * SHA1_Init - Initialize new context
201
201
  */
202
- void SHA1_Init(SHA1_CTX *context)
202
+ int SHA1_Init(SHA1_CTX *context)
203
203
  {
204
204
 
205
205
  _DIAGASSERT(context != 0);
@@ -211,6 +211,7 @@ void SHA1_Init(SHA1_CTX *context)
211
211
  context->state[3] = 0x10325476;
212
212
  context->state[4] = 0xC3D2E1F0;
213
213
  context->count[0] = context->count[1] = 0;
214
+ return 1;
214
215
  }
215
216
 
216
217
 
@@ -244,7 +245,7 @@ void SHA1_Update(SHA1_CTX *context, const uint8_t *data, size_t len)
244
245
  /*
245
246
  * Add padding and return the message digest.
246
247
  */
247
- void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
248
+ int SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
248
249
  {
249
250
  size_t i;
250
251
  uint8_t finalcount[8];
@@ -266,4 +267,5 @@ void SHA1_Finish(SHA1_CTX* context, uint8_t digest[20])
266
267
  digest[i] = (uint8_t)
267
268
  ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
268
269
  }
270
+ return 1;
269
271
  }
@@ -1,6 +1,6 @@
1
1
  /* $NetBSD: sha1.h,v 1.2 1998/05/29 22:55:44 thorpej Exp $ */
2
2
  /* $RoughId: sha1.h,v 1.3 2002/02/24 08:14:32 knu Exp $ */
3
- /* $Id: sha1.h 11708 2007-02-12 23:01:19Z shyouhei $ */
3
+ /* $Id$ */
4
4
 
5
5
  /*
6
6
  * SHA-1 in C
@@ -28,9 +28,9 @@ typedef struct {
28
28
  #endif
29
29
 
30
30
  void SHA1_Transform _((uint32_t state[5], const uint8_t buffer[64]));
31
- void SHA1_Init _((SHA1_CTX *context));
31
+ int SHA1_Init _((SHA1_CTX *context));
32
32
  void SHA1_Update _((SHA1_CTX *context, const uint8_t *data, size_t len));
33
- void SHA1_Finish _((SHA1_CTX *context, uint8_t digest[20]));
33
+ int SHA1_Finish _((SHA1_CTX *context, uint8_t digest[20]));
34
34
 
35
35
  #define SHA1_BLOCK_LENGTH 64
36
36
  #define SHA1_DIGEST_LENGTH 20
@@ -0,0 +1,14 @@
1
+ #define COMMON_DIGEST_FOR_OPENSSL 1
2
+ #include <CommonCrypto/CommonDigest.h>
3
+
4
+ #define SHA1_BLOCK_LENGTH CC_SHA1_BLOCK_BYTES
5
+ #define SHA1_DIGEST_LENGTH CC_SHA1_DIGEST_LENGTH
6
+ #define SHA1_CTX CC_SHA1_CTX
7
+
8
+ static DEFINE_UPDATE_FUNC_FOR_UINT(SHA1);
9
+ static DEFINE_FINISH_FUNC_FROM_FINAL(SHA1);
10
+
11
+ #undef SHA1_Update
12
+ #undef SHA1_Finish
13
+ #define SHA1_Update rb_digest_SHA1_update
14
+ #define SHA1_Finish rb_digest_SHA1_finish
@@ -1,9 +1,11 @@
1
1
  /* $RoughId: sha1init.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
2
- /* $Id: sha1init.c 19020 2008-09-01 07:48:53Z nobu $ */
2
+ /* $Id$ */
3
3
 
4
4
  #include "digest.h"
5
- #if defined(HAVE_OPENSSL_SHA_H)
5
+ #if defined(SHA1_USE_OPENSSL)
6
6
  #include "sha1ossl.h"
7
+ #elif defined(SHA1_USE_COMMONDIGEST)
8
+ #include "sha1cc.h"
7
9
  #else
8
10
  #include "sha1.h"
9
11
  #endif
@@ -24,15 +26,20 @@ static const rb_digest_metadata_t sha1 = {
24
26
  * Technology), described in FIPS PUB 180-1.
25
27
  */
26
28
  void
27
- Init_sha1()
29
+ Init_sha1(void)
28
30
  {
29
31
  VALUE mDigest, cDigest_Base, cDigest_SHA1;
30
32
 
33
+ #if 0
34
+ mDigest = rb_define_module("Digest"); /* let rdoc know */
35
+ #endif
31
36
  mDigest = rb_path2class("Digest");
32
37
  cDigest_Base = rb_path2class("Digest::Base");
33
38
 
34
39
  cDigest_SHA1 = rb_define_class_under(mDigest, "SHA1", cDigest_Base);
35
40
 
36
- rb_ivar_set(cDigest_SHA1, rb_intern("metadata"),
37
- Data_Wrap_Struct(rb_cObject, 0, 0, (void *)&sha1));
41
+ #undef RUBY_UNTYPED_DATA_WARNING
42
+ #define RUBY_UNTYPED_DATA_WARNING 0
43
+ rb_iv_set(cDigest_SHA1, "metadata",
44
+ Data_Wrap_Struct(0, 0, 0, (void *)&sha1));
38
45
  }
@@ -1,4 +1,4 @@
1
- /* $Id: sha1ossl.h 11708 2007-02-12 23:01:19Z shyouhei $ */
1
+ /* $Id$ */
2
2
 
3
3
  #ifndef SHA1OSSL_H_INCLUDED
4
4
  #define SHA1OSSL_H_INCLUDED
@@ -15,6 +15,8 @@
15
15
  #endif
16
16
  #define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
17
17
 
18
- void SHA1_Finish(SHA1_CTX *ctx, char *buf);
18
+ static DEFINE_FINISH_FUNC_FROM_FINAL(SHA1);
19
+ #undef SHA1_Finish
20
+ #define SHA1_Finish rb_digest_SHA1_finish
19
21
 
20
22
  #endif
@@ -1,21 +1,20 @@
1
+ # -*- coding: us-ascii -*-
1
2
  # $RoughId: extconf.rb,v 1.4 2001/08/14 19:54:51 knu Exp $
2
3
  # $Id$
3
4
 
4
5
  require "mkmf"
6
+ require File.expand_path("../../digest_conf", __FILE__)
5
7
 
6
8
  $defs << "-DHAVE_CONFIG_H"
7
9
  $INCFLAGS << " -I$(srcdir)/.."
8
10
 
9
- $objs = [
10
- "sha2.#{$OBJEXT}",
11
- "sha2init.#{$OBJEXT}",
12
- ]
11
+ $objs = [ "sha2init.#{$OBJEXT}" ]
13
12
 
14
- have_header("sys/cdefs.h")
15
-
16
- have_header("inttypes.h")
13
+ unless digest_conf("sha2", "sha", %w[SHA256 SHA512])
14
+ have_type("u_int8_t")
15
+ end
17
16
 
18
- have_header("unistd.h")
17
+ have_header("sys/cdefs.h")
19
18
 
20
19
  $preload = %w[digest]
21
20
 
@@ -1,11 +1,9 @@
1
1
  /*
2
- * sha2.c
2
+ * FILE: sha2.c
3
+ * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
3
4
  *
4
- * Version 1.0.0beta1
5
- *
6
- * Written by Aaron D. Gifford <me@aarongifford.com>
7
- *
8
- * Copyright 2000 Aaron D. Gifford. All rights reserved.
5
+ * Copyright (c) 2000-2001, Aaron D. Gifford
6
+ * All rights reserved.
9
7
  *
10
8
  * Redistribution and use in source and binary forms, with or without
11
9
  * modification, are permitted provided that the following conditions
@@ -19,10 +17,10 @@
19
17
  * may be used to endorse or promote products derived from this software
20
18
  * without specific prior written permission.
21
19
  *
22
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
20
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
23
21
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
22
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
23
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
26
24
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
25
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
26
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
@@ -31,15 +29,15 @@
31
29
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32
30
  * SUCH DAMAGE.
33
31
  *
32
+ * $OrigId: sha2.c,v 1.1 2001/11/08 00:01:51 adg Exp adg $
33
+ * $RoughId: sha2.c,v 1.3 2002/02/26 22:03:36 knu Exp $
34
+ * $Id$
34
35
  */
35
36
 
36
- /* $RoughId: sha2.c,v 1.3 2002/02/26 22:03:36 knu Exp $ */
37
- /* $Id: sha2.c 11708 2007-02-12 23:01:19Z shyouhei $ */
38
-
39
- #include "sha2.h"
40
- #include <stdio.h>
37
+ #include "defs.h"
41
38
  #include <string.h> /* memcpy()/memset() or bcopy()/bzero() */
42
39
  #include <assert.h> /* assert() */
40
+ #include "sha2.h"
43
41
 
44
42
  /*
45
43
  * ASSERT NOTE:
@@ -63,15 +61,64 @@
63
61
 
64
62
 
65
63
  /*** SHA-256/384/512 Machine Architecture Definitions *****************/
64
+ /*
65
+ * BYTE_ORDER NOTE:
66
+ *
67
+ * Please make sure that your system defines BYTE_ORDER. If your
68
+ * architecture is little-endian, make sure it also defines
69
+ * LITTLE_ENDIAN and that the two (BYTE_ORDER and LITTLE_ENDIAN) are
70
+ * equivalent.
71
+ *
72
+ * If your system does not define the above, then you can do so by
73
+ * hand like this:
74
+ *
75
+ * #define LITTLE_ENDIAN 1234
76
+ * #define BIG_ENDIAN 4321
77
+ *
78
+ * And for little-endian machines, add:
79
+ *
80
+ * #define BYTE_ORDER LITTLE_ENDIAN
81
+ *
82
+ * Or for big-endian machines:
83
+ *
84
+ * #define BYTE_ORDER BIG_ENDIAN
85
+ *
86
+ * The FreeBSD machine this was written on defines BYTE_ORDER
87
+ * appropriately by including <sys/types.h> (which in turn includes
88
+ * <machine/endian.h> where the appropriate definitions are actually
89
+ * made).
90
+ */
91
+ #if !defined(BYTE_ORDER) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
92
+ #error Define BYTE_ORDER to be equal to either LITTLE_ENDIAN or BIG_ENDIAN
93
+ #endif
94
+
95
+ /*
96
+ * Define the followingsha2_* types to types of the correct length on
97
+ * the native archtecture. Most BSD systems and Linux define u_intXX_t
98
+ * types. Machines with very recent ANSI C headers, can use the
99
+ * uintXX_t definintions from inttypes.h by defining SHA2_USE_INTTYPES_H
100
+ * during compile or in the sha.h header file.
101
+ *
102
+ * Machines that support neither u_intXX_t nor inttypes.h's uintXX_t
103
+ * will need to define these three typedefs below (and the appropriate
104
+ * ones in sha.h too) by hand according to their system architecture.
105
+ *
106
+ * Thank you, Jun-ichiro itojun Hagino, for suggesting using u_intXX_t
107
+ * types and pointing out recent ANSI C support for uintXX_t in inttypes.h.
108
+ */
109
+ #ifdef SHA2_USE_INTTYPES_H
110
+
66
111
  typedef uint8_t sha2_byte; /* Exactly 1 byte */
67
112
  typedef uint32_t sha2_word32; /* Exactly 4 bytes */
68
113
  typedef uint64_t sha2_word64; /* Exactly 8 bytes */
69
114
 
70
- #if defined(__GNUC__) || defined(_HPUX_SOURCE) || defined(__IBMC__)
71
- #define ULL(number) number##ULL
72
- #else
73
- #define ULL(number) (uint64_t)(number)
74
- #endif
115
+ #else /* SHA2_USE_INTTYPES_H */
116
+
117
+ typedef u_int8_t sha2_byte; /* Exactly 1 byte */
118
+ typedef u_int32_t sha2_word32; /* Exactly 4 bytes */
119
+ typedef u_int64_t sha2_word64; /* Exactly 8 bytes */
120
+
121
+ #endif /* SHA2_USE_INTTYPES_H */
75
122
 
76
123
 
77
124
  /*** SHA-256/384/512 Various Length Definitions ***********************/
@@ -81,12 +128,17 @@ typedef uint64_t sha2_word64; /* Exactly 8 bytes */
81
128
  #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
82
129
 
83
130
 
131
+ #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || defined(__GNUC__) || defined(_HPUX_SOURCE) || defined(__IBMC__)
132
+ #define ULL(number) number##ULL
133
+ #else
134
+ #define ULL(number) (uint64_t)(number)
135
+ #endif
84
136
  /*** ENDIAN REVERSAL MACROS *******************************************/
85
- #ifndef WORDS_BIGENDIAN
137
+ #if BYTE_ORDER == LITTLE_ENDIAN
86
138
  #define REVERSE32(w,x) { \
87
139
  sha2_word32 tmp = (w); \
88
140
  tmp = (tmp >> 16) | (tmp << 16); \
89
- (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
141
+ (x) = ((tmp & (sha2_word32)0xff00ff00UL) >> 8) | ((tmp & (sha2_word32)0x00ff00ffUL) << 8); \
90
142
  }
91
143
  #define REVERSE64(w,x) { \
92
144
  sha2_word64 tmp = (w); \
@@ -96,7 +148,7 @@ typedef uint64_t sha2_word64; /* Exactly 8 bytes */
96
148
  (x) = ((tmp & ULL(0xffff0000ffff0000)) >> 16) | \
97
149
  ((tmp & ULL(0x0000ffff0000ffff)) << 16); \
98
150
  }
99
- #endif
151
+ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
100
152
 
101
153
  /*
102
154
  * Macro for incrementally adding the unsigned 64-bit integer n to the
@@ -182,7 +234,7 @@ void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
182
234
 
183
235
  /*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
184
236
  /* Hash constant words K for SHA-256: */
185
- const static sha2_word32 K256[64] = {
237
+ static const sha2_word32 K256[64] = {
186
238
  0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
187
239
  0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
188
240
  0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
@@ -202,7 +254,7 @@ const static sha2_word32 K256[64] = {
202
254
  };
203
255
 
204
256
  /* Initial hash value H for SHA-256: */
205
- const static sha2_word32 sha256_initial_hash_value[8] = {
257
+ static const sha2_word32 sha256_initial_hash_value[8] = {
206
258
  0x6a09e667UL,
207
259
  0xbb67ae85UL,
208
260
  0x3c6ef372UL,
@@ -214,7 +266,7 @@ const static sha2_word32 sha256_initial_hash_value[8] = {
214
266
  };
215
267
 
216
268
  /* Hash constant words K for SHA-384 and SHA-512: */
217
- const static sha2_word64 K512[80] = {
269
+ static const sha2_word64 K512[80] = {
218
270
  ULL(0x428a2f98d728ae22), ULL(0x7137449123ef65cd),
219
271
  ULL(0xb5c0fbcfec4d3b2f), ULL(0xe9b5dba58189dbbc),
220
272
  ULL(0x3956c25bf348b538), ULL(0x59f111f1b605d019),
@@ -258,7 +310,7 @@ const static sha2_word64 K512[80] = {
258
310
  };
259
311
 
260
312
  /* Initial hash value H for SHA-384 */
261
- const static sha2_word64 sha384_initial_hash_value[8] = {
313
+ static const sha2_word64 sha384_initial_hash_value[8] = {
262
314
  ULL(0xcbbb9d5dc1059ed8),
263
315
  ULL(0x629a292a367cd507),
264
316
  ULL(0x9159015a3070dd17),
@@ -270,7 +322,7 @@ const static sha2_word64 sha384_initial_hash_value[8] = {
270
322
  };
271
323
 
272
324
  /* Initial hash value H for SHA-512 */
273
- const static sha2_word64 sha512_initial_hash_value[8] = {
325
+ static const sha2_word64 sha512_initial_hash_value[8] = {
274
326
  ULL(0x6a09e667f3bcc908),
275
327
  ULL(0xbb67ae8584caa73b),
276
328
  ULL(0x3c6ef372fe94f82b),
@@ -281,22 +333,29 @@ const static sha2_word64 sha512_initial_hash_value[8] = {
281
333
  ULL(0x5be0cd19137e2179)
282
334
  };
283
335
 
336
+ /*
337
+ * Constant used by SHA256/384/512_End() functions for converting the
338
+ * digest to a readable hexadecimal character string:
339
+ */
340
+ static const char *sha2_hex_digits = "0123456789abcdef";
341
+
284
342
 
285
343
  /*** SHA-256: *********************************************************/
286
- void SHA256_Init(SHA256_CTX* context) {
344
+ int SHA256_Init(SHA256_CTX* context) {
287
345
  if (context == (SHA256_CTX*)0) {
288
- return;
346
+ return 0;
289
347
  }
290
348
  MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
291
349
  MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
292
350
  context->bitcount = 0;
351
+ return 1;
293
352
  }
294
353
 
295
354
  #ifdef SHA2_UNROLL_TRANSFORM
296
355
 
297
356
  /* Unrolled SHA-256 round macros: */
298
357
 
299
- #ifndef WORDS_BIGENDIAN
358
+ #if BYTE_ORDER == LITTLE_ENDIAN
300
359
 
301
360
  #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
302
361
  REVERSE32(*data++, W256[j]); \
@@ -307,7 +366,7 @@ void SHA256_Init(SHA256_CTX* context) {
307
366
  j++
308
367
 
309
368
 
310
- #else
369
+ #else /* BYTE_ORDER == LITTLE_ENDIAN */
311
370
 
312
371
  #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
313
372
  T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
@@ -316,7 +375,7 @@ void SHA256_Init(SHA256_CTX* context) {
316
375
  (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
317
376
  j++
318
377
 
319
- #endif
378
+ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
320
379
 
321
380
  #define ROUND256(a,b,c,d,e,f,g,h) \
322
381
  s0 = W256[(j+1)&0x0f]; \
@@ -406,15 +465,15 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
406
465
 
407
466
  j = 0;
408
467
  do {
409
- #ifndef WORDS_BIGENDIAN
468
+ #if BYTE_ORDER == LITTLE_ENDIAN
410
469
  /* Copy data while converting to host byte order */
411
470
  REVERSE32(*data++,W256[j]);
412
471
  /* Apply the SHA-256 compression function to update a..h */
413
472
  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
414
- #else
473
+ #else /* BYTE_ORDER == LITTLE_ENDIAN */
415
474
  /* Apply the SHA-256 compression function to update a..h with copy */
416
475
  T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
417
- #endif
476
+ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
418
477
  T2 = Sigma0_256(a) + Maj(a, b, c);
419
478
  h = g;
420
479
  g = f;
@@ -432,7 +491,7 @@ void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
432
491
  /* Part of the message block expansion: */
433
492
  s0 = W256[(j+1)&0x0f];
434
493
  s0 = sigma0_256(s0);
435
- s1 = W256[(j+14)&0x0f];
494
+ s1 = W256[(j+14)&0x0f];
436
495
  s1 = sigma1_256(s1);
437
496
 
438
497
  /* Apply the SHA-256 compression function to update a..h */
@@ -476,9 +535,9 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
476
535
  }
477
536
 
478
537
  /* Sanity check: */
479
- assert(context != NULL && data != NULL);
538
+ assert(context != (SHA256_CTX*)0 && data != (sha2_byte*)0);
480
539
 
481
- usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
540
+ usedspace = (unsigned int)((context->bitcount >> 3) % SHA256_BLOCK_LENGTH);
482
541
  if (usedspace > 0) {
483
542
  /* Calculate how much free space is available in the buffer */
484
543
  freespace = SHA256_BLOCK_LENGTH - usedspace;
@@ -501,7 +560,8 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
501
560
  }
502
561
  while (len >= SHA256_BLOCK_LENGTH) {
503
562
  /* Process as many complete blocks as we can */
504
- SHA256_Transform(context, (const sha2_word32*)data);
563
+ MEMCPY_BCOPY(context->buffer, data, SHA256_BLOCK_LENGTH);
564
+ SHA256_Transform(context, (sha2_word32*)context->buffer);
505
565
  context->bitcount += SHA256_BLOCK_LENGTH << 3;
506
566
  len -= SHA256_BLOCK_LENGTH;
507
567
  data += SHA256_BLOCK_LENGTH;
@@ -515,17 +575,17 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
515
575
  usedspace = freespace = 0;
516
576
  }
517
577
 
518
- void SHA256_Finish(SHA256_CTX* context, sha2_byte digest[]) {
578
+ int SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
519
579
  sha2_word32 *d = (sha2_word32*)digest;
520
580
  unsigned int usedspace;
521
581
 
522
582
  /* Sanity check: */
523
- assert(context != NULL);
583
+ assert(context != (SHA256_CTX*)0);
524
584
 
525
585
  /* If no digest buffer is passed, we don't bother doing this: */
526
586
  if (digest != (sha2_byte*)0) {
527
- usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
528
- #ifndef WORDS_BIGENDIAN
587
+ usedspace = (unsigned int)((context->bitcount >> 3) % SHA256_BLOCK_LENGTH);
588
+ #if BYTE_ORDER == LITTLE_ENDIAN
529
589
  /* Convert FROM host byte order */
530
590
  REVERSE64(context->bitcount,context->bitcount);
531
591
  #endif
@@ -554,12 +614,13 @@ void SHA256_Finish(SHA256_CTX* context, sha2_byte digest[]) {
554
614
  *context->buffer = 0x80;
555
615
  }
556
616
  /* Set the bit count: */
557
- *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
617
+ MEMCPY_BCOPY(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount,
618
+ sizeof(sha2_word64));
558
619
 
559
620
  /* Final transform: */
560
621
  SHA256_Transform(context, (sha2_word32*)context->buffer);
561
622
 
562
- #ifndef WORDS_BIGENDIAN
623
+ #if BYTE_ORDER == LITTLE_ENDIAN
563
624
  {
564
625
  /* Convert TO host byte order */
565
626
  int j;
@@ -574,24 +635,57 @@ void SHA256_Finish(SHA256_CTX* context, sha2_byte digest[]) {
574
635
  }
575
636
 
576
637
  /* Clean up state data: */
577
- MEMSET_BZERO(context, sizeof(SHA256_CTX));
638
+ MEMSET_BZERO(context, sizeof(*context));
578
639
  usedspace = 0;
640
+ return 1;
641
+ }
642
+
643
+ char *SHA256_End(SHA256_CTX* context, char buffer[]) {
644
+ sha2_byte digest[SHA256_DIGEST_LENGTH], *d = digest;
645
+ int i;
646
+
647
+ /* Sanity check: */
648
+ assert(context != (SHA256_CTX*)0);
649
+
650
+ if (buffer != (char*)0) {
651
+ SHA256_Final(digest, context);
652
+ for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
653
+ *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
654
+ *buffer++ = sha2_hex_digits[*d & 0x0f];
655
+ d++;
656
+ }
657
+ *buffer = (char)0;
658
+ } else {
659
+ MEMSET_BZERO(context, sizeof(*context));
660
+ }
661
+ MEMSET_BZERO(digest, SHA256_DIGEST_LENGTH);
662
+ return buffer;
663
+ }
664
+
665
+ char* SHA256_Data(const sha2_byte* data, size_t len, char digest[SHA256_DIGEST_STRING_LENGTH]) {
666
+ SHA256_CTX context;
667
+
668
+ SHA256_Init(&context);
669
+ SHA256_Update(&context, data, len);
670
+ return SHA256_End(&context, digest);
579
671
  }
580
672
 
673
+
581
674
  /*** SHA-512: *********************************************************/
582
- void SHA512_Init(SHA512_CTX* context) {
675
+ int SHA512_Init(SHA512_CTX* context) {
583
676
  if (context == (SHA512_CTX*)0) {
584
- return;
677
+ return 0;
585
678
  }
586
679
  MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
587
680
  MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
588
681
  context->bitcount[0] = context->bitcount[1] = 0;
682
+ return 1;
589
683
  }
590
684
 
591
685
  #ifdef SHA2_UNROLL_TRANSFORM
592
686
 
593
687
  /* Unrolled SHA-512 round macros: */
594
- #ifndef WORDS_BIGENDIAN
688
+ #if BYTE_ORDER == LITTLE_ENDIAN
595
689
 
596
690
  #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
597
691
  REVERSE64(*data++, W512[j]); \
@@ -602,7 +696,7 @@ void SHA512_Init(SHA512_CTX* context) {
602
696
  j++
603
697
 
604
698
 
605
- #else
699
+ #else /* BYTE_ORDER == LITTLE_ENDIAN */
606
700
 
607
701
  #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
608
702
  T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
@@ -611,7 +705,7 @@ void SHA512_Init(SHA512_CTX* context) {
611
705
  (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
612
706
  j++
613
707
 
614
- #endif
708
+ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
615
709
 
616
710
  #define ROUND512(a,b,c,d,e,f,g,h) \
617
711
  s0 = W512[(j+1)&0x0f]; \
@@ -696,15 +790,15 @@ void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
696
790
 
697
791
  j = 0;
698
792
  do {
699
- #ifndef WORDS_BIGENDIAN
793
+ #if BYTE_ORDER == LITTLE_ENDIAN
700
794
  /* Convert TO host byte order */
701
795
  REVERSE64(*data++, W512[j]);
702
796
  /* Apply the SHA-512 compression function to update a..h */
703
797
  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
704
- #else
798
+ #else /* BYTE_ORDER == LITTLE_ENDIAN */
705
799
  /* Apply the SHA-512 compression function to update a..h with copy */
706
800
  T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
707
- #endif
801
+ #endif /* BYTE_ORDER == LITTLE_ENDIAN */
708
802
  T2 = Sigma0_512(a) + Maj(a, b, c);
709
803
  h = g;
710
804
  g = f;
@@ -766,9 +860,9 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
766
860
  }
767
861
 
768
862
  /* Sanity check: */
769
- assert(context != NULL && data != NULL);
863
+ assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);
770
864
 
771
- usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
865
+ usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH);
772
866
  if (usedspace > 0) {
773
867
  /* Calculate how much free space is available in the buffer */
774
868
  freespace = SHA512_BLOCK_LENGTH - usedspace;
@@ -779,7 +873,7 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
779
873
  ADDINC128(context->bitcount, freespace << 3);
780
874
  len -= freespace;
781
875
  data += freespace;
782
- SHA512_Transform(context, (const sha2_word64*)context->buffer);
876
+ SHA512_Transform(context, (sha2_word64*)context->buffer);
783
877
  } else {
784
878
  /* The buffer is not yet full */
785
879
  MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
@@ -791,7 +885,8 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
791
885
  }
792
886
  while (len >= SHA512_BLOCK_LENGTH) {
793
887
  /* Process as many complete blocks as we can */
794
- SHA512_Transform(context, (const sha2_word64*)data);
888
+ MEMCPY_BCOPY(context->buffer, data, SHA512_BLOCK_LENGTH);
889
+ SHA512_Transform(context, (sha2_word64*)context->buffer);
795
890
  ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
796
891
  len -= SHA512_BLOCK_LENGTH;
797
892
  data += SHA512_BLOCK_LENGTH;
@@ -808,8 +903,8 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
808
903
  void SHA512_Last(SHA512_CTX* context) {
809
904
  unsigned int usedspace;
810
905
 
811
- usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
812
- #ifndef WORDS_BIGENDIAN
906
+ usedspace = (unsigned int)((context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH);
907
+ #if BYTE_ORDER == LITTLE_ENDIAN
813
908
  /* Convert FROM host byte order */
814
909
  REVERSE64(context->bitcount[0],context->bitcount[0]);
815
910
  REVERSE64(context->bitcount[1],context->bitcount[1]);
@@ -826,7 +921,7 @@ void SHA512_Last(SHA512_CTX* context) {
826
921
  MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
827
922
  }
828
923
  /* Do second-to-last transform: */
829
- SHA512_Transform(context, (const sha2_word64*)context->buffer);
924
+ SHA512_Transform(context, (sha2_word64*)context->buffer);
830
925
 
831
926
  /* And set-up for the last transform: */
832
927
  MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
@@ -839,25 +934,27 @@ void SHA512_Last(SHA512_CTX* context) {
839
934
  *context->buffer = 0x80;
840
935
  }
841
936
  /* Store the length of input data (in bits): */
842
- *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
843
- *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
937
+ MEMCPY_BCOPY(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], &context->bitcount[1],
938
+ sizeof(sha2_word64));
939
+ MEMCPY_BCOPY(&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8], &context->bitcount[0],
940
+ sizeof(sha2_word64));
844
941
 
845
942
  /* Final transform: */
846
- SHA512_Transform(context, (const sha2_word64*)context->buffer);
943
+ SHA512_Transform(context, (sha2_word64*)context->buffer);
847
944
  }
848
945
 
849
- void SHA512_Finish(SHA512_CTX* context, sha2_byte digest[]) {
946
+ int SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
850
947
  sha2_word64 *d = (sha2_word64*)digest;
851
948
 
852
949
  /* Sanity check: */
853
- assert(context != NULL);
950
+ assert(context != (SHA512_CTX*)0);
854
951
 
855
952
  /* If no digest buffer is passed, we don't bother doing this: */
856
953
  if (digest != (sha2_byte*)0) {
857
954
  SHA512_Last(context);
858
955
 
859
956
  /* Save the hash data for output: */
860
- #ifndef WORDS_BIGENDIAN
957
+ #if BYTE_ORDER == LITTLE_ENDIAN
861
958
  {
862
959
  /* Convert TO host byte order */
863
960
  int j;
@@ -872,35 +969,68 @@ void SHA512_Finish(SHA512_CTX* context, sha2_byte digest[]) {
872
969
  }
873
970
 
874
971
  /* Zero out state data */
875
- MEMSET_BZERO(context, sizeof(SHA512_CTX));
972
+ MEMSET_BZERO(context, sizeof(*context));
973
+ return 1;
974
+ }
975
+
976
+ char *SHA512_End(SHA512_CTX* context, char buffer[]) {
977
+ sha2_byte digest[SHA512_DIGEST_LENGTH], *d = digest;
978
+ int i;
979
+
980
+ /* Sanity check: */
981
+ assert(context != (SHA512_CTX*)0);
982
+
983
+ if (buffer != (char*)0) {
984
+ SHA512_Final(digest, context);
985
+ for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
986
+ *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
987
+ *buffer++ = sha2_hex_digits[*d & 0x0f];
988
+ d++;
989
+ }
990
+ *buffer = (char)0;
991
+ } else {
992
+ MEMSET_BZERO(context, sizeof(*context));
993
+ }
994
+ MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);
995
+ return buffer;
996
+ }
997
+
998
+ char* SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {
999
+ SHA512_CTX context;
1000
+
1001
+ SHA512_Init(&context);
1002
+ SHA512_Update(&context, data, len);
1003
+ return SHA512_End(&context, digest);
876
1004
  }
877
1005
 
1006
+
878
1007
  /*** SHA-384: *********************************************************/
879
- void SHA384_Init(SHA384_CTX* context) {
1008
+ int SHA384_Init(SHA384_CTX* context) {
880
1009
  if (context == (SHA384_CTX*)0) {
881
- return;
1010
+ return 0;
882
1011
  }
883
1012
  MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
884
1013
  MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
885
1014
  context->bitcount[0] = context->bitcount[1] = 0;
1015
+ return 1;
886
1016
  }
887
1017
 
888
1018
  void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
889
1019
  SHA512_Update((SHA512_CTX*)context, data, len);
890
1020
  }
891
1021
 
892
- void SHA384_Finish(SHA384_CTX* context, sha2_byte digest[]) {
1022
+ int SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
893
1023
  sha2_word64 *d = (sha2_word64*)digest;
894
1024
 
895
1025
  /* Sanity check: */
896
- assert(context != NULL);
1026
+ assert(context != (SHA384_CTX*)0);
897
1027
 
898
1028
  /* If no digest buffer is passed, we don't bother doing this: */
899
1029
  if (digest != (sha2_byte*)0) {
900
1030
  SHA512_Last((SHA512_CTX*)context);
901
1031
 
902
1032
  /* Save the hash data for output: */
903
- #ifndef WORDS_BIGENDIAN
1033
+ #if BYTE_ORDER == LITTLE_ENDIAN
904
1034
  {
905
1035
  /* Convert TO host byte order */
906
1036
  int j;
@@ -915,5 +1045,37 @@ void SHA384_Finish(SHA384_CTX* context, sha2_byte digest[]) {
915
1045
  }
916
1046
 
917
1047
  /* Zero out state data */
918
- MEMSET_BZERO(context, sizeof(SHA384_CTX));
1048
+ MEMSET_BZERO(context, sizeof(*context));
1049
+ return 1;
1050
+ }
1051
+
1052
+ char *SHA384_End(SHA384_CTX* context, char buffer[]) {
1053
+ sha2_byte digest[SHA384_DIGEST_LENGTH], *d = digest;
1054
+ int i;
1055
+
1056
+ /* Sanity check: */
1057
+ assert(context != (SHA384_CTX*)0);
1058
+
1059
+ if (buffer != (char*)0) {
1060
+ SHA384_Final(digest, context);
1061
+ for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
1062
+ *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
1063
+ *buffer++ = sha2_hex_digits[*d & 0x0f];
1064
+ d++;
1065
+ }
1066
+ *buffer = (char)0;
1067
+ } else {
1068
+ MEMSET_BZERO(context, sizeof(*context));
1069
+ }
1070
+ MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);
1071
+ return buffer;
919
1072
  }
1073
+
1074
+ char* SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {
1075
+ SHA384_CTX context;
1076
+
1077
+ SHA384_Init(&context);
1078
+ SHA384_Update(&context, data, len);
1079
+ return SHA384_End(&context, digest);
1080
+ }
1081
+