enzoic 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +3 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +5 -0
  7. data/README.md +90 -0
  8. data/Rakefile +28 -0
  9. data/enzoic.gemspec +36 -0
  10. data/ext/.DS_Store +0 -0
  11. data/ext/argon2-wrapper/Makefile +74 -0
  12. data/ext/argon2-wrapper/argon2-wrapper.c +165 -0
  13. data/ext/argon2-wrapper/extconf.rb +1 -0
  14. data/ext/argon2_import/.DS_Store +0 -0
  15. data/ext/digest/whirlpool/extconf.rb +10 -0
  16. data/ext/digest/whirlpool/whirlpool-algorithm.c +476 -0
  17. data/ext/digest/whirlpool/whirlpool-algorithm.h +86 -0
  18. data/ext/digest/whirlpool/whirlpool-constants.h +1116 -0
  19. data/ext/digest/whirlpool/whirlpool-portability.h +142 -0
  20. data/ext/digest/whirlpool/whirlpool.c +51 -0
  21. data/ext/phc-winner-argon2/.gitattributes +10 -0
  22. data/ext/phc-winner-argon2/.gitignore +21 -0
  23. data/ext/phc-winner-argon2/.travis.yml +14 -0
  24. data/ext/phc-winner-argon2/Argon2.sln +160 -0
  25. data/ext/phc-winner-argon2/CHANGELOG.md +25 -0
  26. data/ext/phc-winner-argon2/LICENSE +314 -0
  27. data/ext/phc-winner-argon2/Makefile +187 -0
  28. data/ext/phc-winner-argon2/README.md +290 -0
  29. data/ext/phc-winner-argon2/appveyor.yml +25 -0
  30. data/ext/phc-winner-argon2/argon2-specs.pdf +0 -0
  31. data/ext/phc-winner-argon2/export.sh +7 -0
  32. data/ext/phc-winner-argon2/include/argon2.h +435 -0
  33. data/ext/phc-winner-argon2/kats/argon2d +12304 -0
  34. data/ext/phc-winner-argon2/kats/argon2d.shasum +1 -0
  35. data/ext/phc-winner-argon2/kats/argon2d_v16 +12304 -0
  36. data/ext/phc-winner-argon2/kats/argon2d_v16.shasum +1 -0
  37. data/ext/phc-winner-argon2/kats/argon2i +12304 -0
  38. data/ext/phc-winner-argon2/kats/argon2i.shasum +1 -0
  39. data/ext/phc-winner-argon2/kats/argon2i_v16 +12304 -0
  40. data/ext/phc-winner-argon2/kats/argon2i_v16.shasum +1 -0
  41. data/ext/phc-winner-argon2/kats/argon2id +12304 -0
  42. data/ext/phc-winner-argon2/kats/argon2id.shasum +1 -0
  43. data/ext/phc-winner-argon2/kats/argon2id_v16 +12304 -0
  44. data/ext/phc-winner-argon2/kats/argon2id_v16.shasum +1 -0
  45. data/ext/phc-winner-argon2/kats/check-sums.ps1 +42 -0
  46. data/ext/phc-winner-argon2/kats/check-sums.sh +13 -0
  47. data/ext/phc-winner-argon2/kats/test.ps1 +50 -0
  48. data/ext/phc-winner-argon2/kats/test.sh +49 -0
  49. data/ext/phc-winner-argon2/latex/IEEEtran.cls +6347 -0
  50. data/ext/phc-winner-argon2/latex/Makefile +18 -0
  51. data/ext/phc-winner-argon2/latex/argon2-specs.tex +920 -0
  52. data/ext/phc-winner-argon2/latex/pics/argon2-par.pdf +0 -0
  53. data/ext/phc-winner-argon2/latex/pics/compression.pdf +0 -0
  54. data/ext/phc-winner-argon2/latex/pics/generic.pdf +0 -0
  55. data/ext/phc-winner-argon2/latex/pics/power-distribution.jpg +0 -0
  56. data/ext/phc-winner-argon2/latex/tradeoff.bib +822 -0
  57. data/ext/phc-winner-argon2/libargon2.pc +16 -0
  58. data/ext/phc-winner-argon2/man/argon2.1 +57 -0
  59. data/ext/phc-winner-argon2/src/argon2.c +452 -0
  60. data/ext/phc-winner-argon2/src/bench.c +111 -0
  61. data/ext/phc-winner-argon2/src/blake2/blake2-impl.h +156 -0
  62. data/ext/phc-winner-argon2/src/blake2/blake2.h +91 -0
  63. data/ext/phc-winner-argon2/src/blake2/blake2b.c +390 -0
  64. data/ext/phc-winner-argon2/src/blake2/blamka-round-opt.h +328 -0
  65. data/ext/phc-winner-argon2/src/blake2/blamka-round-ref.h +56 -0
  66. data/ext/phc-winner-argon2/src/core.c +635 -0
  67. data/ext/phc-winner-argon2/src/core.h +227 -0
  68. data/ext/phc-winner-argon2/src/encoding.c +463 -0
  69. data/ext/phc-winner-argon2/src/encoding.h +57 -0
  70. data/ext/phc-winner-argon2/src/genkat.c +208 -0
  71. data/ext/phc-winner-argon2/src/genkat.h +49 -0
  72. data/ext/phc-winner-argon2/src/opt.c +241 -0
  73. data/ext/phc-winner-argon2/src/ref.c +194 -0
  74. data/ext/phc-winner-argon2/src/run.c +317 -0
  75. data/ext/phc-winner-argon2/src/test.c +254 -0
  76. data/ext/phc-winner-argon2/src/thread.c +57 -0
  77. data/ext/phc-winner-argon2/src/thread.h +67 -0
  78. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj +226 -0
  79. data/ext/phc-winner-argon2/vs2015/Argon2Opt/Argon2Opt.vcxproj.filters +69 -0
  80. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj +226 -0
  81. data/ext/phc-winner-argon2/vs2015/Argon2OptBench/Argon2OptBench.vcxproj.filters +69 -0
  82. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj +225 -0
  83. data/ext/phc-winner-argon2/vs2015/Argon2OptDll/Argon2OptDll.vcxproj.filters +66 -0
  84. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj +239 -0
  85. data/ext/phc-winner-argon2/vs2015/Argon2OptGenKAT/Argon2OptGenKAT.vcxproj.filters +72 -0
  86. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj +227 -0
  87. data/ext/phc-winner-argon2/vs2015/Argon2OptTestCI/Argon2OptTestCI.vcxproj.filters +69 -0
  88. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj +226 -0
  89. data/ext/phc-winner-argon2/vs2015/Argon2Ref/Argon2Ref.vcxproj.filters +69 -0
  90. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj +226 -0
  91. data/ext/phc-winner-argon2/vs2015/Argon2RefBench/Argon2RefBench.vcxproj.filters +69 -0
  92. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj +225 -0
  93. data/ext/phc-winner-argon2/vs2015/Argon2RefDll/Argon2RefDll.vcxproj.filters +66 -0
  94. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj +227 -0
  95. data/ext/phc-winner-argon2/vs2015/Argon2RefGenKAT/Argon2RefGenKAT.vcxproj.filters +72 -0
  96. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj +226 -0
  97. data/ext/phc-winner-argon2/vs2015/Argon2RefTestCI/Argon2RefTestCI.vcxproj.filters +69 -0
  98. data/lib/enzoic.rb +189 -0
  99. data/lib/enzoic/argon2_errors.rb +39 -0
  100. data/lib/enzoic/argon2_wrapper_ffi.rb +89 -0
  101. data/lib/enzoic/constants.rb +10 -0
  102. data/lib/enzoic/errors.rb +3 -0
  103. data/lib/enzoic/hashing.rb +258 -0
  104. data/lib/enzoic/password_type.rb +25 -0
  105. data/lib/enzoic/version.rb +5 -0
  106. metadata +354 -0
@@ -0,0 +1 @@
1
+ #placeholder
@@ -0,0 +1,10 @@
1
+ require 'mkmf'
2
+
3
+ $defs << "-DHAVE_CONFIG_H"
4
+ $INCFLAGS << " -I$(srcdir)/.. -I$(hdrdir)/ruby"
5
+
6
+ have_header('ruby/digest.h')
7
+
8
+ $preload = %w[digest]
9
+
10
+ create_makefile('digest/whirlpool')
@@ -0,0 +1,476 @@
1
+ /**
2
+ * The Whirlpool hashing function.
3
+ *
4
+ * The Whirlpool algorithm was developed by
5
+ * Paulo S. L. M. Barreto and Vincent Rijmen.
6
+ *
7
+ * See
8
+ * P.S.L.M. Barreto, V. Rijmen,
9
+ * ``The Whirlpool hashing function,''
10
+ * NESSIE submission, 2000 (tweaked version, 2001),
11
+ * <https://www.cosic.esat.kuleuven.ac.be/nessie/workshop/submissions/whirlpool.zip>
12
+ *
13
+ * @version 3.0 (2003.03.12)
14
+ *
15
+ * Modified for use in this software package.
16
+ */
17
+ #include <stdlib.h>
18
+ #include <string.h>
19
+ #include <limits.h>
20
+ #include "whirlpool-algorithm.h"
21
+ #include "whirlpool-portability.h"
22
+ #include "whirlpool-constants.h"
23
+
24
+ #ifdef __cplusplus
25
+ extern "C" {
26
+ #endif
27
+
28
+
29
+ /**
30
+ * The core Whirlpool transform.
31
+ */
32
+ static void
33
+ processBuffer(WP_Struct * const structpointer) {
34
+ int i, r;
35
+ u64 K[8]; /* the round key */
36
+ u64 block[8]; /* mu(buffer) */
37
+ u64 state[8]; /* the cipher state */
38
+ u64 L[8];
39
+ u8 *buffer = structpointer->buffer;
40
+
41
+ /*
42
+ * map the buffer to a block:
43
+ */
44
+ for (i = 0; i < 8; i++, buffer += 8) {
45
+ block[i] =
46
+ (((u64)buffer[0] ) << 56) ^
47
+ (((u64)buffer[1] & 0xffL) << 48) ^
48
+ (((u64)buffer[2] & 0xffL) << 40) ^
49
+ (((u64)buffer[3] & 0xffL) << 32) ^
50
+ (((u64)buffer[4] & 0xffL) << 24) ^
51
+ (((u64)buffer[5] & 0xffL) << 16) ^
52
+ (((u64)buffer[6] & 0xffL) << 8) ^
53
+ (((u64)buffer[7] & 0xffL) );
54
+ }
55
+ /*
56
+ * compute and apply K^0 to the cipher state:
57
+ */
58
+ state[0] = block[0] ^ (K[0] = structpointer->hash[0]);
59
+ state[1] = block[1] ^ (K[1] = structpointer->hash[1]);
60
+ state[2] = block[2] ^ (K[2] = structpointer->hash[2]);
61
+ state[3] = block[3] ^ (K[3] = structpointer->hash[3]);
62
+ state[4] = block[4] ^ (K[4] = structpointer->hash[4]);
63
+ state[5] = block[5] ^ (K[5] = structpointer->hash[5]);
64
+ state[6] = block[6] ^ (K[6] = structpointer->hash[6]);
65
+ state[7] = block[7] ^ (K[7] = structpointer->hash[7]);
66
+
67
+ /*
68
+ * iterate over all rounds:
69
+ */
70
+ for (r = 1; r <= R; r++) {
71
+ /*
72
+ * compute K^r from K^{r-1}:
73
+ */
74
+ L[0] =
75
+ C0[(int)(K[0] >> 56) ] ^
76
+ C1[(int)(K[7] >> 48) & 0xff] ^
77
+ C2[(int)(K[6] >> 40) & 0xff] ^
78
+ C3[(int)(K[5] >> 32) & 0xff] ^
79
+ C4[(int)(K[4] >> 24) & 0xff] ^
80
+ C5[(int)(K[3] >> 16) & 0xff] ^
81
+ C6[(int)(K[2] >> 8) & 0xff] ^
82
+ C7[(int)(K[1] ) & 0xff] ^
83
+ rc[r];
84
+ L[1] =
85
+ C0[(int)(K[1] >> 56) ] ^
86
+ C1[(int)(K[0] >> 48) & 0xff] ^
87
+ C2[(int)(K[7] >> 40) & 0xff] ^
88
+ C3[(int)(K[6] >> 32) & 0xff] ^
89
+ C4[(int)(K[5] >> 24) & 0xff] ^
90
+ C5[(int)(K[4] >> 16) & 0xff] ^
91
+ C6[(int)(K[3] >> 8) & 0xff] ^
92
+ C7[(int)(K[2] ) & 0xff];
93
+ L[2] =
94
+ C0[(int)(K[2] >> 56) ] ^
95
+ C1[(int)(K[1] >> 48) & 0xff] ^
96
+ C2[(int)(K[0] >> 40) & 0xff] ^
97
+ C3[(int)(K[7] >> 32) & 0xff] ^
98
+ C4[(int)(K[6] >> 24) & 0xff] ^
99
+ C5[(int)(K[5] >> 16) & 0xff] ^
100
+ C6[(int)(K[4] >> 8) & 0xff] ^
101
+ C7[(int)(K[3] ) & 0xff];
102
+ L[3] =
103
+ C0[(int)(K[3] >> 56) ] ^
104
+ C1[(int)(K[2] >> 48) & 0xff] ^
105
+ C2[(int)(K[1] >> 40) & 0xff] ^
106
+ C3[(int)(K[0] >> 32) & 0xff] ^
107
+ C4[(int)(K[7] >> 24) & 0xff] ^
108
+ C5[(int)(K[6] >> 16) & 0xff] ^
109
+ C6[(int)(K[5] >> 8) & 0xff] ^
110
+ C7[(int)(K[4] ) & 0xff];
111
+ L[4] =
112
+ C0[(int)(K[4] >> 56) ] ^
113
+ C1[(int)(K[3] >> 48) & 0xff] ^
114
+ C2[(int)(K[2] >> 40) & 0xff] ^
115
+ C3[(int)(K[1] >> 32) & 0xff] ^
116
+ C4[(int)(K[0] >> 24) & 0xff] ^
117
+ C5[(int)(K[7] >> 16) & 0xff] ^
118
+ C6[(int)(K[6] >> 8) & 0xff] ^
119
+ C7[(int)(K[5] ) & 0xff];
120
+ L[5] =
121
+ C0[(int)(K[5] >> 56) ] ^
122
+ C1[(int)(K[4] >> 48) & 0xff] ^
123
+ C2[(int)(K[3] >> 40) & 0xff] ^
124
+ C3[(int)(K[2] >> 32) & 0xff] ^
125
+ C4[(int)(K[1] >> 24) & 0xff] ^
126
+ C5[(int)(K[0] >> 16) & 0xff] ^
127
+ C6[(int)(K[7] >> 8) & 0xff] ^
128
+ C7[(int)(K[6] ) & 0xff];
129
+ L[6] =
130
+ C0[(int)(K[6] >> 56) ] ^
131
+ C1[(int)(K[5] >> 48) & 0xff] ^
132
+ C2[(int)(K[4] >> 40) & 0xff] ^
133
+ C3[(int)(K[3] >> 32) & 0xff] ^
134
+ C4[(int)(K[2] >> 24) & 0xff] ^
135
+ C5[(int)(K[1] >> 16) & 0xff] ^
136
+ C6[(int)(K[0] >> 8) & 0xff] ^
137
+ C7[(int)(K[7] ) & 0xff];
138
+ L[7] =
139
+ C0[(int)(K[7] >> 56) ] ^
140
+ C1[(int)(K[6] >> 48) & 0xff] ^
141
+ C2[(int)(K[5] >> 40) & 0xff] ^
142
+ C3[(int)(K[4] >> 32) & 0xff] ^
143
+ C4[(int)(K[3] >> 24) & 0xff] ^
144
+ C5[(int)(K[2] >> 16) & 0xff] ^
145
+ C6[(int)(K[1] >> 8) & 0xff] ^
146
+ C7[(int)(K[0] ) & 0xff];
147
+ K[0] = L[0];
148
+ K[1] = L[1];
149
+ K[2] = L[2];
150
+ K[3] = L[3];
151
+ K[4] = L[4];
152
+ K[5] = L[5];
153
+ K[6] = L[6];
154
+ K[7] = L[7];
155
+ /*
156
+ * apply the r-th round transformation:
157
+ */
158
+ L[0] =
159
+ C0[(int)(state[0] >> 56) ] ^
160
+ C1[(int)(state[7] >> 48) & 0xff] ^
161
+ C2[(int)(state[6] >> 40) & 0xff] ^
162
+ C3[(int)(state[5] >> 32) & 0xff] ^
163
+ C4[(int)(state[4] >> 24) & 0xff] ^
164
+ C5[(int)(state[3] >> 16) & 0xff] ^
165
+ C6[(int)(state[2] >> 8) & 0xff] ^
166
+ C7[(int)(state[1] ) & 0xff] ^
167
+ K[0];
168
+ L[1] =
169
+ C0[(int)(state[1] >> 56) ] ^
170
+ C1[(int)(state[0] >> 48) & 0xff] ^
171
+ C2[(int)(state[7] >> 40) & 0xff] ^
172
+ C3[(int)(state[6] >> 32) & 0xff] ^
173
+ C4[(int)(state[5] >> 24) & 0xff] ^
174
+ C5[(int)(state[4] >> 16) & 0xff] ^
175
+ C6[(int)(state[3] >> 8) & 0xff] ^
176
+ C7[(int)(state[2] ) & 0xff] ^
177
+ K[1];
178
+ L[2] =
179
+ C0[(int)(state[2] >> 56) ] ^
180
+ C1[(int)(state[1] >> 48) & 0xff] ^
181
+ C2[(int)(state[0] >> 40) & 0xff] ^
182
+ C3[(int)(state[7] >> 32) & 0xff] ^
183
+ C4[(int)(state[6] >> 24) & 0xff] ^
184
+ C5[(int)(state[5] >> 16) & 0xff] ^
185
+ C6[(int)(state[4] >> 8) & 0xff] ^
186
+ C7[(int)(state[3] ) & 0xff] ^
187
+ K[2];
188
+ L[3] =
189
+ C0[(int)(state[3] >> 56) ] ^
190
+ C1[(int)(state[2] >> 48) & 0xff] ^
191
+ C2[(int)(state[1] >> 40) & 0xff] ^
192
+ C3[(int)(state[0] >> 32) & 0xff] ^
193
+ C4[(int)(state[7] >> 24) & 0xff] ^
194
+ C5[(int)(state[6] >> 16) & 0xff] ^
195
+ C6[(int)(state[5] >> 8) & 0xff] ^
196
+ C7[(int)(state[4] ) & 0xff] ^
197
+ K[3];
198
+ L[4] =
199
+ C0[(int)(state[4] >> 56) ] ^
200
+ C1[(int)(state[3] >> 48) & 0xff] ^
201
+ C2[(int)(state[2] >> 40) & 0xff] ^
202
+ C3[(int)(state[1] >> 32) & 0xff] ^
203
+ C4[(int)(state[0] >> 24) & 0xff] ^
204
+ C5[(int)(state[7] >> 16) & 0xff] ^
205
+ C6[(int)(state[6] >> 8) & 0xff] ^
206
+ C7[(int)(state[5] ) & 0xff] ^
207
+ K[4];
208
+ L[5] =
209
+ C0[(int)(state[5] >> 56) ] ^
210
+ C1[(int)(state[4] >> 48) & 0xff] ^
211
+ C2[(int)(state[3] >> 40) & 0xff] ^
212
+ C3[(int)(state[2] >> 32) & 0xff] ^
213
+ C4[(int)(state[1] >> 24) & 0xff] ^
214
+ C5[(int)(state[0] >> 16) & 0xff] ^
215
+ C6[(int)(state[7] >> 8) & 0xff] ^
216
+ C7[(int)(state[6] ) & 0xff] ^
217
+ K[5];
218
+ L[6] =
219
+ C0[(int)(state[6] >> 56) ] ^
220
+ C1[(int)(state[5] >> 48) & 0xff] ^
221
+ C2[(int)(state[4] >> 40) & 0xff] ^
222
+ C3[(int)(state[3] >> 32) & 0xff] ^
223
+ C4[(int)(state[2] >> 24) & 0xff] ^
224
+ C5[(int)(state[1] >> 16) & 0xff] ^
225
+ C6[(int)(state[0] >> 8) & 0xff] ^
226
+ C7[(int)(state[7] ) & 0xff] ^
227
+ K[6];
228
+ L[7] =
229
+ C0[(int)(state[7] >> 56) ] ^
230
+ C1[(int)(state[6] >> 48) & 0xff] ^
231
+ C2[(int)(state[5] >> 40) & 0xff] ^
232
+ C3[(int)(state[4] >> 32) & 0xff] ^
233
+ C4[(int)(state[3] >> 24) & 0xff] ^
234
+ C5[(int)(state[2] >> 16) & 0xff] ^
235
+ C6[(int)(state[1] >> 8) & 0xff] ^
236
+ C7[(int)(state[0] ) & 0xff] ^
237
+ K[7];
238
+ state[0] = L[0];
239
+ state[1] = L[1];
240
+ state[2] = L[2];
241
+ state[3] = L[3];
242
+ state[4] = L[4];
243
+ state[5] = L[5];
244
+ state[6] = L[6];
245
+ state[7] = L[7];
246
+ }
247
+
248
+ /*
249
+ * apply the Miyaguchi-Preneel compression function:
250
+ */
251
+ structpointer->hash[0] ^= state[0] ^ block[0];
252
+ structpointer->hash[1] ^= state[1] ^ block[1];
253
+ structpointer->hash[2] ^= state[2] ^ block[2];
254
+ structpointer->hash[3] ^= state[3] ^ block[3];
255
+ structpointer->hash[4] ^= state[4] ^ block[4];
256
+ structpointer->hash[5] ^= state[5] ^ block[5];
257
+ structpointer->hash[6] ^= state[6] ^ block[6];
258
+ structpointer->hash[7] ^= state[7] ^ block[7];
259
+ }
260
+
261
+ WP_Struct *
262
+ WP_Create() {
263
+ WP_Struct *wp;
264
+
265
+ wp = (WP_Struct *) malloc(sizeof(WP_Struct));
266
+ if (wp != NULL) {
267
+ WP_Init(wp);
268
+ }
269
+ return wp;
270
+ }
271
+
272
+ int
273
+ WP_Init(WP_Struct *wp) {
274
+ int i;
275
+
276
+ memset(wp->bitLength, 0, 32);
277
+ wp->bufferBits = wp->bufferPos = 0;
278
+ wp->buffer[0] = 0; /* it's only necessary to cleanup buffer[bufferPos] */
279
+ for (i = 0; i < 8; i++) {
280
+ wp->hash[i] = 0L; /* initial value */
281
+ }
282
+ return 1;
283
+ }
284
+
285
+ void WP_Add(const unsigned char * const source,
286
+ unsigned long sourceBits,
287
+ WP_Struct * const structpointer) {
288
+ /*
289
+ sourcePos
290
+ |
291
+ +-------+-------+-------
292
+ ||||||||||||||||||||| source
293
+ +-------+-------+-------
294
+ +-------+-------+-------+-------+-------+-------
295
+ |||||||||||||||||||||| buffer
296
+ +-------+-------+-------+-------+-------+-------
297
+ |
298
+ bufferPos
299
+ */
300
+ int sourcePos = 0; /* index of leftmost source u8 containing data (1 to 8 bits). */
301
+ int sourceGap = (8 - ((int)sourceBits & 7)) & 7; /* space on source[sourcePos]. */
302
+ int bufferRem = structpointer->bufferBits & 7; /* occupied bits on buffer[bufferPos]. */
303
+ int i;
304
+ u32 b, carry;
305
+ u8 *buffer = structpointer->buffer;
306
+ u8 *bitLength = structpointer->bitLength;
307
+ int bufferBits = structpointer->bufferBits;
308
+ int bufferPos = structpointer->bufferPos;
309
+
310
+ /*
311
+ * This method maintains the invariant: bufferBits < DIGESTBITS
312
+ */
313
+
314
+ /*
315
+ * tally the length of the added data:
316
+ */
317
+ u64 value = sourceBits;
318
+ for (i = 31, carry = 0; i >= 0 && (carry != 0 || value != LL(0)); i--) {
319
+ carry += bitLength[i] + ((u32)value & 0xff);
320
+ bitLength[i] = (u8)carry;
321
+ carry >>= 8;
322
+ value >>= 8;
323
+ }
324
+ /*
325
+ * process data in chunks of 8 bits (a more efficient approach would be to take whole-word chunks):
326
+ */
327
+ while (sourceBits > 8) {
328
+ /* N.B. at least source[sourcePos] and source[sourcePos+1] contain data. */
329
+ /*
330
+ * take a byte from the source:
331
+ */
332
+ b = ((source[sourcePos] << sourceGap) & 0xff) |
333
+ ((source[sourcePos + 1] & 0xff) >> (8 - sourceGap));
334
+ /*
335
+ * process this byte:
336
+ */
337
+ buffer[bufferPos++] |= (u8)(b >> bufferRem);
338
+ bufferBits += 8 - bufferRem; /* bufferBits = 8*bufferPos; */
339
+ if (bufferBits == DIGESTBITS) {
340
+ /*
341
+ * process data block:
342
+ */
343
+ processBuffer(structpointer);
344
+ /*
345
+ * reset buffer:
346
+ */
347
+ bufferBits = bufferPos = 0;
348
+ }
349
+ buffer[bufferPos] = b << (8 - bufferRem);
350
+ bufferBits += bufferRem;
351
+ /*
352
+ * proceed to remaining data:
353
+ */
354
+ sourceBits -= 8;
355
+ sourcePos++;
356
+ }
357
+ /* now 0 <= sourceBits <= 8;
358
+ * furthermore, all data (if any is left) is in source[sourcePos].
359
+ */
360
+ if (sourceBits > 0) {
361
+ b = (source[sourcePos] << sourceGap) & 0xff; /* bits are left-justified on b. */
362
+ /*
363
+ * process the remaining bits:
364
+ */
365
+ buffer[bufferPos] |= b >> bufferRem;
366
+ } else {
367
+ b = 0;
368
+ }
369
+ if (bufferRem + sourceBits < 8) {
370
+ /*
371
+ * all remaining data fits on buffer[bufferPos],
372
+ * and there still remains some space.
373
+ */
374
+ bufferBits += sourceBits;
375
+ } else {
376
+ /*
377
+ * buffer[bufferPos] is full:
378
+ */
379
+ bufferPos++;
380
+ bufferBits += 8 - bufferRem; /* bufferBits = 8*bufferPos; */
381
+ sourceBits -= 8 - bufferRem;
382
+ /* now 0 <= sourceBits < 8;
383
+ * furthermore, all data (if any is left) is in source[sourcePos].
384
+ */
385
+ if (bufferBits == DIGESTBITS) {
386
+ /*
387
+ * process data block:
388
+ */
389
+ processBuffer(structpointer);
390
+ /*
391
+ * reset buffer:
392
+ */
393
+ bufferBits = bufferPos = 0;
394
+ }
395
+ buffer[bufferPos] = b << (8 - bufferRem);
396
+ bufferBits += (int)sourceBits;
397
+ }
398
+ structpointer->bufferBits = bufferBits;
399
+ structpointer->bufferPos = bufferPos;
400
+ }
401
+
402
+ int WP_Finalize(WP_Struct * const structpointer,
403
+ unsigned char * const result) {
404
+ int i;
405
+ u8 *buffer = structpointer->buffer;
406
+ u8 *bitLength = structpointer->bitLength;
407
+ int bufferBits = structpointer->bufferBits;
408
+ int bufferPos = structpointer->bufferPos;
409
+ u8 *digest = result;
410
+
411
+ /*
412
+ * This method uses the invariant: bufferBits < DIGESTBITS
413
+ */
414
+
415
+ /*
416
+ * append a '1'-bit:
417
+ */
418
+ buffer[bufferPos] |= 0x80U >> (bufferBits & 7);
419
+ bufferPos++; /* all remaining bits on the current u8 are set to zero. */
420
+ /*
421
+ * pad with zero bits to complete (N*WBLOCKBITS - LENGTHBITS) bits:
422
+ */
423
+ if (bufferPos > WBLOCKBYTES - LENGTHBYTES) {
424
+ if (bufferPos < WBLOCKBYTES) {
425
+ memset(&buffer[bufferPos], 0, WBLOCKBYTES - bufferPos);
426
+ }
427
+ /*
428
+ * process data block:
429
+ */
430
+ processBuffer(structpointer);
431
+ /*
432
+ * reset buffer:
433
+ */
434
+ bufferPos = 0;
435
+ }
436
+ if (bufferPos < WBLOCKBYTES - LENGTHBYTES) {
437
+ memset(&buffer[bufferPos], 0, (WBLOCKBYTES - LENGTHBYTES) - bufferPos);
438
+ }
439
+ bufferPos = WBLOCKBYTES - LENGTHBYTES;
440
+ /*
441
+ * append bit length of hashed data:
442
+ */
443
+ memcpy(&buffer[WBLOCKBYTES - LENGTHBYTES], bitLength, LENGTHBYTES);
444
+ /*
445
+ * process data block:
446
+ */
447
+ processBuffer(structpointer);
448
+ /*
449
+ * return the completed message digest:
450
+ */
451
+ for (i = 0; i < DIGESTBYTES/8; i++) {
452
+ digest[0] = (u8)(structpointer->hash[i] >> 56);
453
+ digest[1] = (u8)(structpointer->hash[i] >> 48);
454
+ digest[2] = (u8)(structpointer->hash[i] >> 40);
455
+ digest[3] = (u8)(structpointer->hash[i] >> 32);
456
+ digest[4] = (u8)(structpointer->hash[i] >> 24);
457
+ digest[5] = (u8)(structpointer->hash[i] >> 16);
458
+ digest[6] = (u8)(structpointer->hash[i] >> 8);
459
+ digest[7] = (u8)(structpointer->hash[i] );
460
+ digest += 8;
461
+ }
462
+ structpointer->bufferBits = bufferBits;
463
+ structpointer->bufferPos = bufferPos;
464
+
465
+ return 1;
466
+ }
467
+
468
+ void
469
+ WP_Free(WP_Struct *wp) {
470
+ free(wp);
471
+ }
472
+
473
+
474
+ #ifdef __cplusplus
475
+ }
476
+ #endif