digest 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +15 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/digest.gemspec +45 -0
- data/ext/digest/bubblebabble/bubblebabble.c +147 -0
- data/ext/digest/bubblebabble/extconf.rb +6 -0
- data/ext/digest/defs.h +19 -0
- data/ext/digest/digest.c +821 -0
- data/ext/digest/digest.h +64 -0
- data/ext/digest/digest_conf.rb +17 -0
- data/ext/digest/extconf.rb +11 -0
- data/ext/digest/md5/extconf.rb +19 -0
- data/ext/digest/md5/md5.c +424 -0
- data/ext/digest/md5/md5.h +80 -0
- data/ext/digest/md5/md5cc.h +19 -0
- data/ext/digest/md5/md5init.c +64 -0
- data/ext/digest/rmd160/extconf.rb +19 -0
- data/ext/digest/rmd160/rmd160.c +463 -0
- data/ext/digest/rmd160/rmd160.h +56 -0
- data/ext/digest/rmd160/rmd160init.c +60 -0
- data/ext/digest/sha1/extconf.rb +19 -0
- data/ext/digest/sha1/sha1.c +271 -0
- data/ext/digest/sha1/sha1.h +39 -0
- data/ext/digest/sha1/sha1cc.h +14 -0
- data/ext/digest/sha1/sha1init.c +66 -0
- data/ext/digest/sha2/extconf.rb +21 -0
- data/ext/digest/sha2/lib/sha2.rb +142 -0
- data/ext/digest/sha2/sha2.c +1081 -0
- data/ext/digest/sha2/sha2.h +225 -0
- data/ext/digest/sha2/sha2cc.h +31 -0
- data/ext/digest/sha2/sha2init.c +55 -0
- data/ext/digest/test.sh +30 -0
- data/ext/openssl/deprecation.rb +23 -0
- data/lib/digest.rb +109 -0
- metadata +132 -0
@@ -0,0 +1,80 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
|
3
|
+
|
4
|
+
This software is provided 'as-is', without any express or implied
|
5
|
+
warranty. In no event will the authors be held liable for any damages
|
6
|
+
arising from the use of this software.
|
7
|
+
|
8
|
+
Permission is granted to anyone to use this software for any purpose,
|
9
|
+
including commercial applications, and to alter it and redistribute it
|
10
|
+
freely, subject to the following restrictions:
|
11
|
+
|
12
|
+
1. The origin of this software must not be misrepresented; you must not
|
13
|
+
claim that you wrote the original software. If you use this software
|
14
|
+
in a product, an acknowledgment in the product documentation would be
|
15
|
+
appreciated but is not required.
|
16
|
+
2. Altered source versions must be plainly marked as such, and must not be
|
17
|
+
misrepresented as being the original software.
|
18
|
+
3. This notice may not be removed or altered from any source distribution.
|
19
|
+
|
20
|
+
L. Peter Deutsch
|
21
|
+
ghost@aladdin.com
|
22
|
+
|
23
|
+
*/
|
24
|
+
/*
|
25
|
+
Independent implementation of MD5 (RFC 1321).
|
26
|
+
|
27
|
+
This code implements the MD5 Algorithm defined in RFC 1321.
|
28
|
+
It is derived directly from the text of the RFC and not from the
|
29
|
+
reference implementation.
|
30
|
+
|
31
|
+
The original and principal author of md5.h is L. Peter Deutsch
|
32
|
+
<ghost@aladdin.com>. Other authors are noted in the change history
|
33
|
+
that follows (in reverse chronological order):
|
34
|
+
|
35
|
+
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
|
36
|
+
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
|
37
|
+
added conditionalization for C++ compilation from Martin
|
38
|
+
Purschke <purschke@bnl.gov>.
|
39
|
+
1999-05-03 lpd Original version.
|
40
|
+
*/
|
41
|
+
|
42
|
+
/* $OrigId: md5.h,v 1.2 2001/03/26 08:57:14 matz Exp $ */
|
43
|
+
/* $RoughId: md5.h,v 1.3 2002/02/24 08:14:31 knu Exp $ */
|
44
|
+
/* $Id$ */
|
45
|
+
|
46
|
+
#ifndef MD5_INCLUDED
|
47
|
+
# define MD5_INCLUDED
|
48
|
+
|
49
|
+
#include "../defs.h"
|
50
|
+
|
51
|
+
/*
|
52
|
+
* This code has some adaptations for the Ghostscript environment, but it
|
53
|
+
* will compile and run correctly in any environment with 8-bit chars and
|
54
|
+
* 32-bit ints. Specifically, it assumes that if the following are
|
55
|
+
* defined, they have the same meaning as in Ghostscript: P1, P2, P3.
|
56
|
+
*/
|
57
|
+
|
58
|
+
/* Define the state of the MD5 Algorithm. */
|
59
|
+
typedef struct md5_state_s {
|
60
|
+
uint32_t count[2]; /* message length in bits, lsw first */
|
61
|
+
uint32_t state[4]; /* digest buffer */
|
62
|
+
uint8_t buffer[64]; /* accumulate block */
|
63
|
+
} MD5_CTX;
|
64
|
+
|
65
|
+
#ifdef RUBY
|
66
|
+
/* avoid name clash */
|
67
|
+
#define MD5_Init rb_Digest_MD5_Init
|
68
|
+
#define MD5_Update rb_Digest_MD5_Update
|
69
|
+
#define MD5_Finish rb_Digest_MD5_Finish
|
70
|
+
#endif
|
71
|
+
|
72
|
+
int MD5_Init _((MD5_CTX *pms));
|
73
|
+
void MD5_Update _((MD5_CTX *pms, const uint8_t *data, size_t nbytes));
|
74
|
+
int MD5_Finish _((MD5_CTX *pms, uint8_t *digest));
|
75
|
+
|
76
|
+
#define MD5_BLOCK_LENGTH 64
|
77
|
+
#define MD5_DIGEST_LENGTH 16
|
78
|
+
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
|
79
|
+
|
80
|
+
#endif /* MD5_INCLUDED */
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#define COMMON_DIGEST_FOR_OPENSSL 1
|
2
|
+
#include <CommonCrypto/CommonDigest.h>
|
3
|
+
|
4
|
+
#ifdef __clang__
|
5
|
+
# pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
6
|
+
/* Suppress deprecation warnings of MD5 from Xcode 11.1 */
|
7
|
+
/* Although we know MD5 is deprecated too, provide just for backward
|
8
|
+
* compatibility, as well as Apple does. */
|
9
|
+
#endif
|
10
|
+
|
11
|
+
#define MD5_BLOCK_LENGTH CC_MD5_BLOCK_BYTES
|
12
|
+
|
13
|
+
static DEFINE_UPDATE_FUNC_FOR_UINT(MD5)
|
14
|
+
static DEFINE_FINISH_FUNC_FROM_FINAL(MD5)
|
15
|
+
|
16
|
+
#undef MD5_Update
|
17
|
+
#undef MD5_Finish
|
18
|
+
#define MD5_Update rb_digest_MD5_update
|
19
|
+
#define MD5_Finish rb_digest_MD5_finish
|
@@ -0,0 +1,64 @@
|
|
1
|
+
/* $RoughId: md5init.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
|
2
|
+
/* $Id$ */
|
3
|
+
|
4
|
+
#include <ruby/ruby.h>
|
5
|
+
#include "../digest.h"
|
6
|
+
#if defined(MD5_USE_COMMONDIGEST)
|
7
|
+
#include "md5cc.h"
|
8
|
+
#else
|
9
|
+
#include "md5.h"
|
10
|
+
#endif
|
11
|
+
|
12
|
+
static const rb_digest_metadata_t md5 = {
|
13
|
+
RUBY_DIGEST_API_VERSION,
|
14
|
+
MD5_DIGEST_LENGTH,
|
15
|
+
MD5_BLOCK_LENGTH,
|
16
|
+
sizeof(MD5_CTX),
|
17
|
+
(rb_digest_hash_init_func_t)MD5_Init,
|
18
|
+
(rb_digest_hash_update_func_t)MD5_Update,
|
19
|
+
(rb_digest_hash_finish_func_t)MD5_Finish,
|
20
|
+
};
|
21
|
+
|
22
|
+
/*
|
23
|
+
* Document-class: Digest::MD5 < Digest::Base
|
24
|
+
* A class for calculating message digests using the MD5
|
25
|
+
* Message-Digest Algorithm by RSA Data Security, Inc., described in
|
26
|
+
* RFC1321.
|
27
|
+
*
|
28
|
+
* MD5 calculates a digest of 128 bits (16 bytes).
|
29
|
+
*
|
30
|
+
* == Examples
|
31
|
+
* require 'digest'
|
32
|
+
*
|
33
|
+
* # Compute a complete digest
|
34
|
+
* Digest::MD5.hexdigest 'abc' #=> "90015098..."
|
35
|
+
*
|
36
|
+
* # Compute digest by chunks
|
37
|
+
* md5 = Digest::MD5.new # =>#<Digest::MD5>
|
38
|
+
* md5.update "ab"
|
39
|
+
* md5 << "c" # alias for #update
|
40
|
+
* md5.hexdigest # => "90015098..."
|
41
|
+
*
|
42
|
+
* # Use the same object to compute another digest
|
43
|
+
* md5.reset
|
44
|
+
* md5 << "message"
|
45
|
+
* md5.hexdigest # => "78e73102..."
|
46
|
+
*/
|
47
|
+
void
|
48
|
+
Init_md5(void)
|
49
|
+
{
|
50
|
+
VALUE mDigest, cDigest_Base, cDigest_MD5;
|
51
|
+
|
52
|
+
#if 0
|
53
|
+
mDigest = rb_define_module("Digest"); /* let rdoc know */
|
54
|
+
#endif
|
55
|
+
mDigest = rb_digest_namespace();
|
56
|
+
cDigest_Base = rb_path2class("Digest::Base");
|
57
|
+
|
58
|
+
cDigest_MD5 = rb_define_class_under(mDigest, "MD5", cDigest_Base);
|
59
|
+
|
60
|
+
#undef RUBY_UNTYPED_DATA_WARNING
|
61
|
+
#define RUBY_UNTYPED_DATA_WARNING 0
|
62
|
+
rb_iv_set(cDigest_MD5, "metadata",
|
63
|
+
Data_Wrap_Struct(0, 0, 0, (void *)&md5));
|
64
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- coding: us-ascii -*-
|
2
|
+
# frozen_string_literal: false
|
3
|
+
# $RoughId: extconf.rb,v 1.3 2001/08/14 19:54:51 knu Exp $
|
4
|
+
# $Id$
|
5
|
+
|
6
|
+
require "mkmf"
|
7
|
+
require File.expand_path("../../digest_conf", __FILE__)
|
8
|
+
|
9
|
+
$defs << "-DNDEBUG" << "-DHAVE_CONFIG_H"
|
10
|
+
|
11
|
+
$objs = [ "rmd160init.#{$OBJEXT}" ]
|
12
|
+
|
13
|
+
digest_conf("rmd160")
|
14
|
+
|
15
|
+
have_header("sys/cdefs.h")
|
16
|
+
|
17
|
+
$preload = %w[digest]
|
18
|
+
|
19
|
+
create_makefile("digest/rmd160")
|
@@ -0,0 +1,463 @@
|
|
1
|
+
/* $NetBSD: rmd160.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $ */
|
2
|
+
/* $RoughId: rmd160.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
|
3
|
+
/* $Id$ */
|
4
|
+
|
5
|
+
/********************************************************************\
|
6
|
+
*
|
7
|
+
* FILE: rmd160.c
|
8
|
+
*
|
9
|
+
* CONTENTS: A sample C-implementation of the RIPEMD-160
|
10
|
+
* hash-function.
|
11
|
+
* TARGET: any computer with an ANSI C compiler
|
12
|
+
*
|
13
|
+
* AUTHOR: Antoon Bosselaers, ESAT-COSIC
|
14
|
+
* (Arranged for libc by Todd C. Miller)
|
15
|
+
* DATE: 1 March 1996
|
16
|
+
* VERSION: 1.0
|
17
|
+
*
|
18
|
+
* Copyright (c) Katholieke Universiteit Leuven
|
19
|
+
* 1996, All Rights Reserved
|
20
|
+
*
|
21
|
+
\********************************************************************/
|
22
|
+
|
23
|
+
#include "rmd160.h"
|
24
|
+
|
25
|
+
#ifndef lint
|
26
|
+
/* __RCSID("$NetBSD: rmd160.c,v 1.1.1.1 2001/03/06 11:21:05 agc Exp $"); */
|
27
|
+
#endif /* not lint */
|
28
|
+
|
29
|
+
/* header files */
|
30
|
+
|
31
|
+
#ifdef HAVE_SYS_ENDIAN_H_
|
32
|
+
#include <sys/endian.h>
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#ifdef HAVE_MACHINE_ENDIAN_H_
|
36
|
+
#include <machine/endian.h>
|
37
|
+
#endif
|
38
|
+
|
39
|
+
/* #include "namespace.h" */
|
40
|
+
|
41
|
+
#include <assert.h>
|
42
|
+
#include <stdio.h>
|
43
|
+
#include <stdlib.h>
|
44
|
+
#include <string.h>
|
45
|
+
|
46
|
+
#ifndef _DIAGASSERT
|
47
|
+
#define _DIAGASSERT(cond) assert(cond)
|
48
|
+
#endif
|
49
|
+
|
50
|
+
|
51
|
+
/********************************************************************/
|
52
|
+
|
53
|
+
/* macro definitions */
|
54
|
+
|
55
|
+
/* collect four bytes into one word: */
|
56
|
+
#define BYTES_TO_DWORD(strptr) \
|
57
|
+
(((uint32_t) *((strptr)+3) << 24) | \
|
58
|
+
((uint32_t) *((strptr)+2) << 16) | \
|
59
|
+
((uint32_t) *((strptr)+1) << 8) | \
|
60
|
+
((uint32_t) *(strptr)))
|
61
|
+
|
62
|
+
/* ROL(x, n) cyclically rotates x over n bits to the left */
|
63
|
+
/* x must be of an unsigned 32 bits type and 0 <= n < 32. */
|
64
|
+
#define ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
65
|
+
|
66
|
+
/* the three basic functions F(), G() and H() */
|
67
|
+
#define F(x, y, z) ((x) ^ (y) ^ (z))
|
68
|
+
#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
|
69
|
+
#define H(x, y, z) (((x) | ~(y)) ^ (z))
|
70
|
+
#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
|
71
|
+
#define J(x, y, z) ((x) ^ ((y) | ~(z)))
|
72
|
+
|
73
|
+
/* the eight basic operations FF() through III() */
|
74
|
+
#define FF(a, b, c, d, e, x, s) { \
|
75
|
+
(a) += F((b), (c), (d)) + (x); \
|
76
|
+
(a) = ROL((a), (s)) + (e); \
|
77
|
+
(c) = ROL((c), 10); \
|
78
|
+
}
|
79
|
+
#define GG(a, b, c, d, e, x, s) { \
|
80
|
+
(a) += G((b), (c), (d)) + (x) + 0x5a827999U; \
|
81
|
+
(a) = ROL((a), (s)) + (e); \
|
82
|
+
(c) = ROL((c), 10); \
|
83
|
+
}
|
84
|
+
#define HH(a, b, c, d, e, x, s) { \
|
85
|
+
(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1U; \
|
86
|
+
(a) = ROL((a), (s)) + (e); \
|
87
|
+
(c) = ROL((c), 10); \
|
88
|
+
}
|
89
|
+
#define II(a, b, c, d, e, x, s) { \
|
90
|
+
(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcU; \
|
91
|
+
(a) = ROL((a), (s)) + (e); \
|
92
|
+
(c) = ROL((c), 10); \
|
93
|
+
}
|
94
|
+
#define JJ(a, b, c, d, e, x, s) { \
|
95
|
+
(a) += J((b), (c), (d)) + (x) + 0xa953fd4eU; \
|
96
|
+
(a) = ROL((a), (s)) + (e); \
|
97
|
+
(c) = ROL((c), 10); \
|
98
|
+
}
|
99
|
+
#define FFF(a, b, c, d, e, x, s) { \
|
100
|
+
(a) += F((b), (c), (d)) + (x); \
|
101
|
+
(a) = ROL((a), (s)) + (e); \
|
102
|
+
(c) = ROL((c), 10); \
|
103
|
+
}
|
104
|
+
#define GGG(a, b, c, d, e, x, s) { \
|
105
|
+
(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9U; \
|
106
|
+
(a) = ROL((a), (s)) + (e); \
|
107
|
+
(c) = ROL((c), 10); \
|
108
|
+
}
|
109
|
+
#define HHH(a, b, c, d, e, x, s) { \
|
110
|
+
(a) += H((b), (c), (d)) + (x) + 0x6d703ef3U; \
|
111
|
+
(a) = ROL((a), (s)) + (e); \
|
112
|
+
(c) = ROL((c), 10); \
|
113
|
+
}
|
114
|
+
#define III(a, b, c, d, e, x, s) { \
|
115
|
+
(a) += I((b), (c), (d)) + (x) + 0x5c4dd124U; \
|
116
|
+
(a) = ROL((a), (s)) + (e); \
|
117
|
+
(c) = ROL((c), 10); \
|
118
|
+
}
|
119
|
+
#define JJJ(a, b, c, d, e, x, s) { \
|
120
|
+
(a) += J((b), (c), (d)) + (x) + 0x50a28be6U; \
|
121
|
+
(a) = ROL((a), (s)) + (e); \
|
122
|
+
(c) = ROL((c), 10); \
|
123
|
+
}
|
124
|
+
|
125
|
+
/********************************************************************/
|
126
|
+
|
127
|
+
int
|
128
|
+
RMD160_Init(RMD160_CTX *context)
|
129
|
+
{
|
130
|
+
|
131
|
+
_DIAGASSERT(context != NULL);
|
132
|
+
|
133
|
+
/* ripemd-160 initialization constants */
|
134
|
+
context->state[0] = 0x67452301U;
|
135
|
+
context->state[1] = 0xefcdab89U;
|
136
|
+
context->state[2] = 0x98badcfeU;
|
137
|
+
context->state[3] = 0x10325476U;
|
138
|
+
context->state[4] = 0xc3d2e1f0U;
|
139
|
+
context->length[0] = context->length[1] = 0;
|
140
|
+
context->buflen = 0;
|
141
|
+
return 1;
|
142
|
+
}
|
143
|
+
|
144
|
+
/********************************************************************/
|
145
|
+
|
146
|
+
void
|
147
|
+
RMD160_Transform(uint32_t state[5], const uint32_t block[16])
|
148
|
+
{
|
149
|
+
uint32_t aa, bb, cc, dd, ee;
|
150
|
+
uint32_t aaa, bbb, ccc, ddd, eee;
|
151
|
+
|
152
|
+
_DIAGASSERT(state != NULL);
|
153
|
+
_DIAGASSERT(block != NULL);
|
154
|
+
|
155
|
+
aa = aaa = state[0];
|
156
|
+
bb = bbb = state[1];
|
157
|
+
cc = ccc = state[2];
|
158
|
+
dd = ddd = state[3];
|
159
|
+
ee = eee = state[4];
|
160
|
+
|
161
|
+
/* round 1 */
|
162
|
+
FF(aa, bb, cc, dd, ee, block[ 0], 11);
|
163
|
+
FF(ee, aa, bb, cc, dd, block[ 1], 14);
|
164
|
+
FF(dd, ee, aa, bb, cc, block[ 2], 15);
|
165
|
+
FF(cc, dd, ee, aa, bb, block[ 3], 12);
|
166
|
+
FF(bb, cc, dd, ee, aa, block[ 4], 5);
|
167
|
+
FF(aa, bb, cc, dd, ee, block[ 5], 8);
|
168
|
+
FF(ee, aa, bb, cc, dd, block[ 6], 7);
|
169
|
+
FF(dd, ee, aa, bb, cc, block[ 7], 9);
|
170
|
+
FF(cc, dd, ee, aa, bb, block[ 8], 11);
|
171
|
+
FF(bb, cc, dd, ee, aa, block[ 9], 13);
|
172
|
+
FF(aa, bb, cc, dd, ee, block[10], 14);
|
173
|
+
FF(ee, aa, bb, cc, dd, block[11], 15);
|
174
|
+
FF(dd, ee, aa, bb, cc, block[12], 6);
|
175
|
+
FF(cc, dd, ee, aa, bb, block[13], 7);
|
176
|
+
FF(bb, cc, dd, ee, aa, block[14], 9);
|
177
|
+
FF(aa, bb, cc, dd, ee, block[15], 8);
|
178
|
+
|
179
|
+
/* round 2 */
|
180
|
+
GG(ee, aa, bb, cc, dd, block[ 7], 7);
|
181
|
+
GG(dd, ee, aa, bb, cc, block[ 4], 6);
|
182
|
+
GG(cc, dd, ee, aa, bb, block[13], 8);
|
183
|
+
GG(bb, cc, dd, ee, aa, block[ 1], 13);
|
184
|
+
GG(aa, bb, cc, dd, ee, block[10], 11);
|
185
|
+
GG(ee, aa, bb, cc, dd, block[ 6], 9);
|
186
|
+
GG(dd, ee, aa, bb, cc, block[15], 7);
|
187
|
+
GG(cc, dd, ee, aa, bb, block[ 3], 15);
|
188
|
+
GG(bb, cc, dd, ee, aa, block[12], 7);
|
189
|
+
GG(aa, bb, cc, dd, ee, block[ 0], 12);
|
190
|
+
GG(ee, aa, bb, cc, dd, block[ 9], 15);
|
191
|
+
GG(dd, ee, aa, bb, cc, block[ 5], 9);
|
192
|
+
GG(cc, dd, ee, aa, bb, block[ 2], 11);
|
193
|
+
GG(bb, cc, dd, ee, aa, block[14], 7);
|
194
|
+
GG(aa, bb, cc, dd, ee, block[11], 13);
|
195
|
+
GG(ee, aa, bb, cc, dd, block[ 8], 12);
|
196
|
+
|
197
|
+
/* round 3 */
|
198
|
+
HH(dd, ee, aa, bb, cc, block[ 3], 11);
|
199
|
+
HH(cc, dd, ee, aa, bb, block[10], 13);
|
200
|
+
HH(bb, cc, dd, ee, aa, block[14], 6);
|
201
|
+
HH(aa, bb, cc, dd, ee, block[ 4], 7);
|
202
|
+
HH(ee, aa, bb, cc, dd, block[ 9], 14);
|
203
|
+
HH(dd, ee, aa, bb, cc, block[15], 9);
|
204
|
+
HH(cc, dd, ee, aa, bb, block[ 8], 13);
|
205
|
+
HH(bb, cc, dd, ee, aa, block[ 1], 15);
|
206
|
+
HH(aa, bb, cc, dd, ee, block[ 2], 14);
|
207
|
+
HH(ee, aa, bb, cc, dd, block[ 7], 8);
|
208
|
+
HH(dd, ee, aa, bb, cc, block[ 0], 13);
|
209
|
+
HH(cc, dd, ee, aa, bb, block[ 6], 6);
|
210
|
+
HH(bb, cc, dd, ee, aa, block[13], 5);
|
211
|
+
HH(aa, bb, cc, dd, ee, block[11], 12);
|
212
|
+
HH(ee, aa, bb, cc, dd, block[ 5], 7);
|
213
|
+
HH(dd, ee, aa, bb, cc, block[12], 5);
|
214
|
+
|
215
|
+
/* round 4 */
|
216
|
+
II(cc, dd, ee, aa, bb, block[ 1], 11);
|
217
|
+
II(bb, cc, dd, ee, aa, block[ 9], 12);
|
218
|
+
II(aa, bb, cc, dd, ee, block[11], 14);
|
219
|
+
II(ee, aa, bb, cc, dd, block[10], 15);
|
220
|
+
II(dd, ee, aa, bb, cc, block[ 0], 14);
|
221
|
+
II(cc, dd, ee, aa, bb, block[ 8], 15);
|
222
|
+
II(bb, cc, dd, ee, aa, block[12], 9);
|
223
|
+
II(aa, bb, cc, dd, ee, block[ 4], 8);
|
224
|
+
II(ee, aa, bb, cc, dd, block[13], 9);
|
225
|
+
II(dd, ee, aa, bb, cc, block[ 3], 14);
|
226
|
+
II(cc, dd, ee, aa, bb, block[ 7], 5);
|
227
|
+
II(bb, cc, dd, ee, aa, block[15], 6);
|
228
|
+
II(aa, bb, cc, dd, ee, block[14], 8);
|
229
|
+
II(ee, aa, bb, cc, dd, block[ 5], 6);
|
230
|
+
II(dd, ee, aa, bb, cc, block[ 6], 5);
|
231
|
+
II(cc, dd, ee, aa, bb, block[ 2], 12);
|
232
|
+
|
233
|
+
/* round 5 */
|
234
|
+
JJ(bb, cc, dd, ee, aa, block[ 4], 9);
|
235
|
+
JJ(aa, bb, cc, dd, ee, block[ 0], 15);
|
236
|
+
JJ(ee, aa, bb, cc, dd, block[ 5], 5);
|
237
|
+
JJ(dd, ee, aa, bb, cc, block[ 9], 11);
|
238
|
+
JJ(cc, dd, ee, aa, bb, block[ 7], 6);
|
239
|
+
JJ(bb, cc, dd, ee, aa, block[12], 8);
|
240
|
+
JJ(aa, bb, cc, dd, ee, block[ 2], 13);
|
241
|
+
JJ(ee, aa, bb, cc, dd, block[10], 12);
|
242
|
+
JJ(dd, ee, aa, bb, cc, block[14], 5);
|
243
|
+
JJ(cc, dd, ee, aa, bb, block[ 1], 12);
|
244
|
+
JJ(bb, cc, dd, ee, aa, block[ 3], 13);
|
245
|
+
JJ(aa, bb, cc, dd, ee, block[ 8], 14);
|
246
|
+
JJ(ee, aa, bb, cc, dd, block[11], 11);
|
247
|
+
JJ(dd, ee, aa, bb, cc, block[ 6], 8);
|
248
|
+
JJ(cc, dd, ee, aa, bb, block[15], 5);
|
249
|
+
JJ(bb, cc, dd, ee, aa, block[13], 6);
|
250
|
+
|
251
|
+
/* parallel round 1 */
|
252
|
+
JJJ(aaa, bbb, ccc, ddd, eee, block[ 5], 8);
|
253
|
+
JJJ(eee, aaa, bbb, ccc, ddd, block[14], 9);
|
254
|
+
JJJ(ddd, eee, aaa, bbb, ccc, block[ 7], 9);
|
255
|
+
JJJ(ccc, ddd, eee, aaa, bbb, block[ 0], 11);
|
256
|
+
JJJ(bbb, ccc, ddd, eee, aaa, block[ 9], 13);
|
257
|
+
JJJ(aaa, bbb, ccc, ddd, eee, block[ 2], 15);
|
258
|
+
JJJ(eee, aaa, bbb, ccc, ddd, block[11], 15);
|
259
|
+
JJJ(ddd, eee, aaa, bbb, ccc, block[ 4], 5);
|
260
|
+
JJJ(ccc, ddd, eee, aaa, bbb, block[13], 7);
|
261
|
+
JJJ(bbb, ccc, ddd, eee, aaa, block[ 6], 7);
|
262
|
+
JJJ(aaa, bbb, ccc, ddd, eee, block[15], 8);
|
263
|
+
JJJ(eee, aaa, bbb, ccc, ddd, block[ 8], 11);
|
264
|
+
JJJ(ddd, eee, aaa, bbb, ccc, block[ 1], 14);
|
265
|
+
JJJ(ccc, ddd, eee, aaa, bbb, block[10], 14);
|
266
|
+
JJJ(bbb, ccc, ddd, eee, aaa, block[ 3], 12);
|
267
|
+
JJJ(aaa, bbb, ccc, ddd, eee, block[12], 6);
|
268
|
+
|
269
|
+
/* parallel round 2 */
|
270
|
+
III(eee, aaa, bbb, ccc, ddd, block[ 6], 9);
|
271
|
+
III(ddd, eee, aaa, bbb, ccc, block[11], 13);
|
272
|
+
III(ccc, ddd, eee, aaa, bbb, block[ 3], 15);
|
273
|
+
III(bbb, ccc, ddd, eee, aaa, block[ 7], 7);
|
274
|
+
III(aaa, bbb, ccc, ddd, eee, block[ 0], 12);
|
275
|
+
III(eee, aaa, bbb, ccc, ddd, block[13], 8);
|
276
|
+
III(ddd, eee, aaa, bbb, ccc, block[ 5], 9);
|
277
|
+
III(ccc, ddd, eee, aaa, bbb, block[10], 11);
|
278
|
+
III(bbb, ccc, ddd, eee, aaa, block[14], 7);
|
279
|
+
III(aaa, bbb, ccc, ddd, eee, block[15], 7);
|
280
|
+
III(eee, aaa, bbb, ccc, ddd, block[ 8], 12);
|
281
|
+
III(ddd, eee, aaa, bbb, ccc, block[12], 7);
|
282
|
+
III(ccc, ddd, eee, aaa, bbb, block[ 4], 6);
|
283
|
+
III(bbb, ccc, ddd, eee, aaa, block[ 9], 15);
|
284
|
+
III(aaa, bbb, ccc, ddd, eee, block[ 1], 13);
|
285
|
+
III(eee, aaa, bbb, ccc, ddd, block[ 2], 11);
|
286
|
+
|
287
|
+
/* parallel round 3 */
|
288
|
+
HHH(ddd, eee, aaa, bbb, ccc, block[15], 9);
|
289
|
+
HHH(ccc, ddd, eee, aaa, bbb, block[ 5], 7);
|
290
|
+
HHH(bbb, ccc, ddd, eee, aaa, block[ 1], 15);
|
291
|
+
HHH(aaa, bbb, ccc, ddd, eee, block[ 3], 11);
|
292
|
+
HHH(eee, aaa, bbb, ccc, ddd, block[ 7], 8);
|
293
|
+
HHH(ddd, eee, aaa, bbb, ccc, block[14], 6);
|
294
|
+
HHH(ccc, ddd, eee, aaa, bbb, block[ 6], 6);
|
295
|
+
HHH(bbb, ccc, ddd, eee, aaa, block[ 9], 14);
|
296
|
+
HHH(aaa, bbb, ccc, ddd, eee, block[11], 12);
|
297
|
+
HHH(eee, aaa, bbb, ccc, ddd, block[ 8], 13);
|
298
|
+
HHH(ddd, eee, aaa, bbb, ccc, block[12], 5);
|
299
|
+
HHH(ccc, ddd, eee, aaa, bbb, block[ 2], 14);
|
300
|
+
HHH(bbb, ccc, ddd, eee, aaa, block[10], 13);
|
301
|
+
HHH(aaa, bbb, ccc, ddd, eee, block[ 0], 13);
|
302
|
+
HHH(eee, aaa, bbb, ccc, ddd, block[ 4], 7);
|
303
|
+
HHH(ddd, eee, aaa, bbb, ccc, block[13], 5);
|
304
|
+
|
305
|
+
/* parallel round 4 */
|
306
|
+
GGG(ccc, ddd, eee, aaa, bbb, block[ 8], 15);
|
307
|
+
GGG(bbb, ccc, ddd, eee, aaa, block[ 6], 5);
|
308
|
+
GGG(aaa, bbb, ccc, ddd, eee, block[ 4], 8);
|
309
|
+
GGG(eee, aaa, bbb, ccc, ddd, block[ 1], 11);
|
310
|
+
GGG(ddd, eee, aaa, bbb, ccc, block[ 3], 14);
|
311
|
+
GGG(ccc, ddd, eee, aaa, bbb, block[11], 14);
|
312
|
+
GGG(bbb, ccc, ddd, eee, aaa, block[15], 6);
|
313
|
+
GGG(aaa, bbb, ccc, ddd, eee, block[ 0], 14);
|
314
|
+
GGG(eee, aaa, bbb, ccc, ddd, block[ 5], 6);
|
315
|
+
GGG(ddd, eee, aaa, bbb, ccc, block[12], 9);
|
316
|
+
GGG(ccc, ddd, eee, aaa, bbb, block[ 2], 12);
|
317
|
+
GGG(bbb, ccc, ddd, eee, aaa, block[13], 9);
|
318
|
+
GGG(aaa, bbb, ccc, ddd, eee, block[ 9], 12);
|
319
|
+
GGG(eee, aaa, bbb, ccc, ddd, block[ 7], 5);
|
320
|
+
GGG(ddd, eee, aaa, bbb, ccc, block[10], 15);
|
321
|
+
GGG(ccc, ddd, eee, aaa, bbb, block[14], 8);
|
322
|
+
|
323
|
+
/* parallel round 5 */
|
324
|
+
FFF(bbb, ccc, ddd, eee, aaa, block[12] , 8);
|
325
|
+
FFF(aaa, bbb, ccc, ddd, eee, block[15] , 5);
|
326
|
+
FFF(eee, aaa, bbb, ccc, ddd, block[10] , 12);
|
327
|
+
FFF(ddd, eee, aaa, bbb, ccc, block[ 4] , 9);
|
328
|
+
FFF(ccc, ddd, eee, aaa, bbb, block[ 1] , 12);
|
329
|
+
FFF(bbb, ccc, ddd, eee, aaa, block[ 5] , 5);
|
330
|
+
FFF(aaa, bbb, ccc, ddd, eee, block[ 8] , 14);
|
331
|
+
FFF(eee, aaa, bbb, ccc, ddd, block[ 7] , 6);
|
332
|
+
FFF(ddd, eee, aaa, bbb, ccc, block[ 6] , 8);
|
333
|
+
FFF(ccc, ddd, eee, aaa, bbb, block[ 2] , 13);
|
334
|
+
FFF(bbb, ccc, ddd, eee, aaa, block[13] , 6);
|
335
|
+
FFF(aaa, bbb, ccc, ddd, eee, block[14] , 5);
|
336
|
+
FFF(eee, aaa, bbb, ccc, ddd, block[ 0] , 15);
|
337
|
+
FFF(ddd, eee, aaa, bbb, ccc, block[ 3] , 13);
|
338
|
+
FFF(ccc, ddd, eee, aaa, bbb, block[ 9] , 11);
|
339
|
+
FFF(bbb, ccc, ddd, eee, aaa, block[11] , 11);
|
340
|
+
|
341
|
+
/* combine results */
|
342
|
+
ddd += cc + state[1]; /* final result for state[0] */
|
343
|
+
state[1] = state[2] + dd + eee;
|
344
|
+
state[2] = state[3] + ee + aaa;
|
345
|
+
state[3] = state[4] + aa + bbb;
|
346
|
+
state[4] = state[0] + bb + ccc;
|
347
|
+
state[0] = ddd;
|
348
|
+
}
|
349
|
+
|
350
|
+
/********************************************************************/
|
351
|
+
|
352
|
+
void
|
353
|
+
RMD160_Update(RMD160_CTX *context, const uint8_t *data, size_t nbytes)
|
354
|
+
{
|
355
|
+
uint32_t X[16];
|
356
|
+
uint32_t ofs = 0;
|
357
|
+
uint32_t i;
|
358
|
+
#ifdef WORDS_BIGENDIAN
|
359
|
+
uint32_t j;
|
360
|
+
#endif
|
361
|
+
|
362
|
+
_DIAGASSERT(context != NULL);
|
363
|
+
_DIAGASSERT(data != NULL);
|
364
|
+
|
365
|
+
/* update length[] */
|
366
|
+
#if SIZEOF_SIZE_T * CHAR_BIT > 32
|
367
|
+
context->length[1] += (uint32_t)((context->length[0] + nbytes) >> 32);
|
368
|
+
#else
|
369
|
+
if (context->length[0] + nbytes < context->length[0])
|
370
|
+
context->length[1]++; /* overflow to msb of length */
|
371
|
+
#endif
|
372
|
+
context->length[0] += (uint32_t)nbytes;
|
373
|
+
|
374
|
+
(void)memset(X, 0, sizeof(X));
|
375
|
+
|
376
|
+
if ( context->buflen + nbytes < 64 )
|
377
|
+
{
|
378
|
+
(void)memcpy(context->bbuffer + context->buflen, data, nbytes);
|
379
|
+
context->buflen += (uint32_t)nbytes;
|
380
|
+
}
|
381
|
+
else
|
382
|
+
{
|
383
|
+
/* process first block */
|
384
|
+
ofs = 64 - context->buflen;
|
385
|
+
(void)memcpy(context->bbuffer + context->buflen, data, ofs);
|
386
|
+
#ifndef WORDS_BIGENDIAN
|
387
|
+
(void)memcpy(X, context->bbuffer, sizeof(X));
|
388
|
+
#else
|
389
|
+
for (j=0; j < 16; j++)
|
390
|
+
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
|
391
|
+
#endif
|
392
|
+
RMD160_Transform(context->state, X);
|
393
|
+
nbytes -= ofs;
|
394
|
+
|
395
|
+
/* process remaining complete blocks */
|
396
|
+
for (i = 0; i < (nbytes >> 6); i++) {
|
397
|
+
#ifndef WORDS_BIGENDIAN
|
398
|
+
(void)memcpy(X, data + (64 * i) + ofs, sizeof(X));
|
399
|
+
#else
|
400
|
+
for (j=0; j < 16; j++)
|
401
|
+
X[j] = BYTES_TO_DWORD(data + (64 * i) + (4 * j) + ofs);
|
402
|
+
#endif
|
403
|
+
RMD160_Transform(context->state, X);
|
404
|
+
}
|
405
|
+
|
406
|
+
/*
|
407
|
+
* Put last bytes from data into context's buffer
|
408
|
+
*/
|
409
|
+
context->buflen = (uint32_t)nbytes & 63;
|
410
|
+
memcpy(context->bbuffer, data + (64 * i) + ofs, context->buflen);
|
411
|
+
}
|
412
|
+
}
|
413
|
+
|
414
|
+
/********************************************************************/
|
415
|
+
|
416
|
+
int
|
417
|
+
RMD160_Finish(RMD160_CTX *context, uint8_t digest[20])
|
418
|
+
{
|
419
|
+
uint32_t i;
|
420
|
+
uint32_t X[16];
|
421
|
+
#ifdef WORDS_BIGENDIAN
|
422
|
+
uint32_t j;
|
423
|
+
#endif
|
424
|
+
|
425
|
+
_DIAGASSERT(digest != NULL);
|
426
|
+
_DIAGASSERT(context != NULL);
|
427
|
+
|
428
|
+
/* append the bit m_n == 1 */
|
429
|
+
context->bbuffer[context->buflen] = (uint8_t)'\200';
|
430
|
+
|
431
|
+
(void)memset(context->bbuffer + context->buflen + 1, 0,
|
432
|
+
63 - context->buflen);
|
433
|
+
#ifndef WORDS_BIGENDIAN
|
434
|
+
(void)memcpy(X, context->bbuffer, sizeof(X));
|
435
|
+
#else
|
436
|
+
for (j=0; j < 16; j++)
|
437
|
+
X[j] = BYTES_TO_DWORD(context->bbuffer + (4 * j));
|
438
|
+
#endif
|
439
|
+
if ((context->buflen) > 55) {
|
440
|
+
/* length goes to next block */
|
441
|
+
RMD160_Transform(context->state, X);
|
442
|
+
(void)memset(X, 0, sizeof(X));
|
443
|
+
}
|
444
|
+
|
445
|
+
/* append length in bits */
|
446
|
+
X[14] = context->length[0] << 3;
|
447
|
+
X[15] = (context->length[0] >> 29) |
|
448
|
+
(context->length[1] << 3);
|
449
|
+
RMD160_Transform(context->state, X);
|
450
|
+
|
451
|
+
if (digest != NULL) {
|
452
|
+
for (i = 0; i < 20; i += 4) {
|
453
|
+
/* extracts the 8 least significant bits. */
|
454
|
+
digest[i] = context->state[i>>2];
|
455
|
+
digest[i + 1] = (context->state[i>>2] >> 8);
|
456
|
+
digest[i + 2] = (context->state[i>>2] >> 16);
|
457
|
+
digest[i + 3] = (context->state[i>>2] >> 24);
|
458
|
+
}
|
459
|
+
}
|
460
|
+
return 1;
|
461
|
+
}
|
462
|
+
|
463
|
+
/************************ end of file rmd160.c **********************/
|