1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
/** @file dk4md.h Message digests.
This module provides a simple API to use the message digest routines from
the OpenSSL library.
This product includes software developed by the OpenSSL Project for use in
the OpenSSL Toolkit (http://www.openssl.org/).
This product includes cryptographic software written by
Eric Young (eay@cryptsoft.com).
This product includes software written by Tim Hudson (tjh@cryptsoft.com).
*/
#ifndef DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif
#ifndef DK4BINENC_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4binenc.h"
#else
#include <dktools-4/dk4binenc.h>
#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif
#if DK4_HAVE_OPENSSL_MD5_H
#ifndef OPENSSL_MD5_H_INCLUDED
#include <openssl/md5.h>
#define OPENSSL_MD5_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_OPENSSL_SHA_H
#ifndef OPENSSL_SHA_H_INCLUDED
#include <openssl/sha.h>
#define OPENSSL_SHA_H_INCLUDED 1
#endif
#endif
#if DK4_HAVE_OPENSSL_RIPEMD_H
#ifndef OPENSSL_RIPEMD_H_INCLUDED
#include <openssl/ripemd.h>
#define OPENSSL_RIPEMD_H_INCLUDED 1
#endif
#endif
/*
At the moment the largest supported message digest is SHA-512,
producing 512 bit (64 bytes) of binary data.
*/
enum { /** Size of largest binary message digest.
*/
DK4MD_BUFFER_SIZE_BINARY = 64
};
enum { /** Size of largest text message digest,
2 * DK4MD_BUFFER_SIZE_BINARY + 16.
*/
DK4MD_BUFFER_SIZE_TEXT = 144
};
/** Message digest creator.
*/
typedef struct {
char obuf[DK4MD_BUFFER_SIZE_TEXT]; /**< Result buffer. */
unsigned char bbuf[16 + DK4MD_BUFFER_SIZE_BINARY]; /**< Binary buffer. */
union {
#if DK4_HAVE_SHA384 || DK4_HAVE_SHA512
SHA512_CTX sha512; /**< SHA-512, SHA-384. */
#endif
#if DK4_HAVE_SHA224 || DK4_HAVE_SHA256
SHA256_CTX sha256; /**< SHA-256, SHA-224. */
#endif
#if DK4_HAVE_OPENSSL_SHA_H
SHA_CTX sha; /**< SHA-1. */
#endif
#if DK4_HAVE_OPENSSL_RIPEMD_H
RIPEMD160_CTX ripemd160; /**< RIPEMD-160. */
#endif
#if DK4_HAVE_OPENSSL_MD5_H
MD5_CTX md5; /**< MD5. */
#endif
int dummy; /**< If none of the above defined. */
} ctx; /**< Message digest context. */
int mdtype; /**< Message digest type. */
int encoding; /**< Required output encoding. */
} dk4_md_ctx_t;
#ifdef __cplusplus
extern "C" {
#endif
/** Initialize CTX structure.
@param ctxptr Structure to initialize.
@param mdt Message digest type, one from:
DK4_MD_MD5, DK4_MD_SHA1, DK4_MD_SHA224,
DK4_MD_SHA256, DK4_MD_SHA384, DK4_MD_SHA512, or
DK4_MD_RIPEMD_160.
@param mde Message digest encoding, one from:
DK4_BINARY_TO_TEXT_ENCODING_HEX or
DK4_BINARY_TO_TEXT_ENCODING_A85.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if ctxptr is NULL,
- DK4_E_SYNTAX<br>
if an invalid value is specified for mdt or mde,
- DK4_E_NOT_SUPPORTED<br>
if the specified message digest type is not available.
- DK4_E_SYSTEM<br>
if the underlaying library indicated an error.<br>
Note: iDetails is not set to an errno value.
*/
int
dk4md_ctx_init(dk4_md_ctx_t *ctxptr, int mdt, int mde, dk4_er_t *erp);
/** Add bytes to CTX.
@param ctxptr CTX structure.
@param buffer Start address of buffer containing bytes to add.
@param sz Buffer size.
@param erp Error report, may be NULL.
@return 1 on success 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if ctxptr or buffer is NULL or sz is 0 or sz exceeds the
range for unsigned long,
- DK4_E_SYNTAX<br>
if an invalid value is specified for mdt.
- DK4_E_NOT_SUPPORTED<br>
if the specified message digest type is not available.
- DK4_E_SYSTEM<br>
if the underlaying library indicated an error.<br>
Note: iDetails is not set to an errno value.
*/
int
dk4md_ctx_add(
dk4_md_ctx_t *ctxptr, const void *buffer, size_t sz, dk4_er_t *erp
);
/** Finish checksum and create text representation.
@param rptr Address of pointer variable for result buffer,
the variable should be initialized to NULL.
@param rszptr Address of size variable for result buffer, the
variable should be initialized to 0.
@param ctxptr CTX structure.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if rptr, rszptr, or ctxptr is NULL,
- DK4_E_NOT_SUPPORTED<br>
if the specified message digest type is not available.
*/
int
dk4md_ctx_finish(
const char **rptr, size_t *rszptr, dk4_md_ctx_t *ctxptr, dk4_er_t *erp
);
/** Find message digest size (number of binary bytes or text characters
without finalizing 0 byte).
@param mdt Message digest type, one from: DK4_MD_MD5,
DK4_MD_SHA1, DK4_MD_SHA224, DK4_MD_SHA256,
DK4_MD_SHA384, DK4_MD_SHA512, or DK4_MD_RIPEMD_160.
@param mde Message digest encoding, one from:
DK4_BINARY_TO_TEXT_ENCODING_NONE,
DK4_BINARY_TO_TEXT_ENCODING_HEX, or
DK4_BINARY_TO_TEXT_ENCODING_A85.
@param erp Error report, may be NULL.
@return Number of bytes or text characters on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
for illegal mdt or mde values.
*/
size_t
dk4md_size(int mdt, int mde, dk4_er_t *erp);
/** Find numeric message digest type for string.
@param name Message digest type name.
@param erp Error report, may be NULL.
@return Non-negative message digest type on success, -1 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if name is NULL,
- DK4_E_SYNTAX<br>
if name is not a valid message digest type name.
*/
int
dk4md_choose_type(const dkChar *name, dk4_er_t *erp);
/** Check whether support for a message type is available.
@param mdtype Message type to check for.
@return 1 if supported, 0 otherwise.
*/
int
dk4md_check_type(int mdtype);
#ifdef __cplusplus
}
#endif
/** Message digest types.
*/
enum {
/** MD5.
*/
DK4_MD_MD5 = 0 ,
/** SHA-1.
*/
DK4_MD_SHA1 ,
/** SHA-224.
*/
DK4_MD_SHA224 ,
/** SHA-256.
*/
DK4_MD_SHA256 ,
/** SHA-384.
*/
DK4_MD_SHA384 ,
/** SHA-512.
*/
DK4_MD_SHA512 ,
/** RIPEMD-160.
*/
DK4_MD_RIPEMD_160
};
%% module
#include "dk4conf.h"
#include "dk4md.h"
#include "dk4mem.h"
#include "dk4numco.h"
#include "dk4binenc.h"
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
$!trace-include
int
dk4md_ctx_init(dk4_md_ctx_t *ctxptr, int mdt, int mde, dk4_er_t *erp)
{
int back = 0;
#if DK4_USE_ASSERT
assert(NULL != ctxptr);
#endif
if (NULL != ctxptr) {
DK4_MEMRES(ctxptr, sizeof(dk4_md_ctx_t));
ctxptr->mdtype = -1;
ctxptr->encoding = -1;
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_HEX:
case DK4_BINARY_TO_TEXT_ENCODING_A85:
{
ctxptr->encoding = mde;
} break;
default : {
dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
} break;
}
if (-1 != ctxptr->encoding) {
switch (mdt) {
case DK4_MD_MD5 : {
#if DK4_HAVE_OPENSSL_MD5_H
ctxptr->mdtype = mdt;
if (1 == MD5_Init(&((ctxptr->ctx).md5))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA1 : {
#if DK4_HAVE_OPENSSL_SHA_H
ctxptr->mdtype = mdt;
if (1 == SHA1_Init(&((ctxptr->ctx).sha))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA224 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA224
ctxptr->mdtype = mdt;
if (1 == SHA224_Init(&((ctxptr->ctx).sha256))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA256 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA256
ctxptr->mdtype = mdt;
if (1 == SHA256_Init(&((ctxptr->ctx).sha256))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA384 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA384
ctxptr->mdtype = mdt;
if (1 == SHA384_Init(&((ctxptr->ctx).sha512))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA512 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA512
ctxptr->mdtype = mdt;
if (1 == SHA512_Init(&((ctxptr->ctx).sha512))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_RIPEMD_160 : {
#if DK4_HAVE_OPENSSL_RIPEMD_H
ctxptr->mdtype = mdt;
if (1 == RIPEMD160_Init(&((ctxptr->ctx).ripemd160))) {
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4md_ctx_add(
dk4_md_ctx_t *ctxptr, const void *buffer, size_t sz, dk4_er_t *erp
)
{
int back = 0;
#if DK4_USE_ASSERT
assert(NULL != ctxptr);
assert(NULL != buffer);
assert(0 < sz);
#endif
if ((NULL != ctxptr) && (NULL != buffer) && (0 < sz)) {
if ((dk4_um_t)sz <= (dk4_um_t)ULONG_MAX) {
if (-1 != ctxptr->mdtype) {
switch (ctxptr->mdtype) {
case DK4_MD_MD5 : {
#if DK4_HAVE_OPENSSL_MD5_H
back = MD5_Update(
&((ctxptr->ctx).md5), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA1 : {
#if DK4_HAVE_OPENSSL_SHA_H
back = SHA1_Update(
&((ctxptr->ctx).sha), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA224 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA224
back = SHA224_Update(
&((ctxptr->ctx).sha256), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA256 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA256
back = SHA256_Update(
&((ctxptr->ctx).sha256), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA384 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA384
back = SHA384_Update(
&((ctxptr->ctx).sha512), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA512 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA512
back = SHA512_Update(
&((ctxptr->ctx).sha512), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_RIPEMD_160 : {
#if DK4_HAVE_OPENSSL_RIPEMD_H
back = RIPEMD160_Update(
&((ctxptr->ctx).ripemd160), buffer, (unsigned long)sz
);
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
default : {
dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
} break;
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4md_ctx_finish(
const char **rptr, size_t *rszptr, dk4_md_ctx_t *ctxptr, dk4_er_t *erp
)
{
int back = 0;
$? "+ dk4md_ctx_finish"
#if DK4_USE_ASSERT
assert(NULL != rptr);
assert(NULL != rszptr);
assert(NULL != ctxptr);
#endif
if ((NULL != rptr) && (NULL != rszptr) && (NULL != ctxptr)) {
if (-1 != ctxptr->mdtype) {
switch (ctxptr->mdtype) {
case DK4_MD_MD5 : {
#if DK4_HAVE_OPENSSL_MD5_H
back = MD5_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).md5));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 16,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 16; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 32; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 20; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA1 : {
#if DK4_HAVE_OPENSSL_SHA_H
back = SHA1_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).sha));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 20,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 20; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 40; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 25; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA224 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA224
back = SHA224_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).sha256));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 28,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 28; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 56; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 35; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA256 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA256
back = SHA256_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).sha256));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 32,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 32; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 64; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 40; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA384 : {
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA384
back = SHA384_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).sha512));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 48,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 48; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 96; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 60; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_SHA512 : { $? ". sha-512 wanted"
#if DK4_HAVE_OPENSSL_SHA_H && DK4_HAVE_SHA512
$? ". sha-512 supported"
back = SHA512_Final(&(ctxptr->bbuf[0]), &((ctxptr->ctx).sha512));
if (0 != back) { $? ". SHA512_Final()"
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 64,
ctxptr->encoding, erp
);
if (0 != back) { $? ". dk4binenc"
*rptr = &(ctxptr->obuf[0]); $? ". setting pointer"
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 64; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 128;} break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 80; } break;
}
} else { $? "! dk4binenc"
}
} else { $? "! SHA512_Final()"
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
case DK4_MD_RIPEMD_160 : {
#if DK4_HAVE_OPENSSL_RIPEMD_H
back = RIPEMD160_Final(&(ctxptr->bbuf[0]),&((ctxptr->ctx).ripemd160));
if (0 != back) {
back = dk4binenc(
&(ctxptr->obuf[0]), DK4MD_BUFFER_SIZE_TEXT,
&(ctxptr->bbuf[0]), 20,
ctxptr->encoding, erp
);
if (0 != back) {
*rptr = &(ctxptr->obuf[0]);
switch (ctxptr->encoding) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { *rszptr = 20; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { *rszptr = 40; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { *rszptr = 25; } break;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_SYSTEM);
}
#else
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
#endif
} break;
default : {
/* ERROR: Wrong MD type */
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
} break;
}
} else {
/* ERROR Wrong MD type */
dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
} $? "- dk4md_ctx_finish %d", back
return back;
}
size_t
dk4md_size(int mdt, int mde, dk4_er_t *erp)
{
size_t back = 0;
switch (mdt) {
case DK4_MD_MD5 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 16; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 32; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 20; } break;
}
} break;
case DK4_MD_SHA1 : case DK4_MD_RIPEMD_160 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 20; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 40; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 25; } break;
}
} break;
case DK4_MD_SHA224 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 28; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 56; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 35; } break;
}
} break;
case DK4_MD_SHA256 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 32; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 64; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 40; } break;
}
} break;
case DK4_MD_SHA384 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 48; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 96; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 60; } break;
}
} break;
case DK4_MD_SHA512 : {
switch (mde) {
case DK4_BINARY_TO_TEXT_ENCODING_NONE : { back = 64; } break;
case DK4_BINARY_TO_TEXT_ENCODING_HEX : { back = 128; } break;
case DK4_BINARY_TO_TEXT_ENCODING_A85 : { back = 80; } break;
}
} break;
}
if (0 == back) {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
|