Coverage Report

Created: 2025-10-13 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/libtomcrypt/src/headers/tomcrypt_hash.h
Line
Count
Source
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2
 *
3
 * LibTomCrypt is a library that provides various cryptographic
4
 * algorithms in a highly modular and flexible manner.
5
 *
6
 * The library is free for all purposes without any express
7
 * guarantee it works.
8
 */
9
10
/* ---- HASH FUNCTIONS ---- */
11
#ifdef LTC_SHA3
12
struct sha3_state {
13
    ulong64 saved;                  /* the portion of the input message that we didn't consume yet */
14
    ulong64 s[25];
15
    unsigned char sb[25 * 8];       /* used for storing `ulong64 s[25]` as little-endian bytes */
16
    unsigned short byte_index;      /* 0..7--the next byte after the set one (starts from 0; 0--none are buffered) */
17
    unsigned short word_index;      /* 0..24--the next word to integrate input (starts from 0) */
18
    unsigned short capacity_words;  /* the double size of the hash output in words (e.g. 16 for Keccak 512) */
19
    unsigned short xof_flag;
20
};
21
#endif
22
23
#ifdef LTC_SHA512
24
struct sha512_state {
25
    ulong64  length, state[8];
26
    unsigned long curlen;
27
    unsigned char buf[128];
28
};
29
#endif
30
31
#ifdef LTC_SHA256
32
struct sha256_state {
33
    ulong64 length;
34
    ulong32 state[8], curlen;
35
    unsigned char buf[64];
36
};
37
#endif
38
39
#ifdef LTC_SHA1
40
struct sha1_state {
41
    ulong64 length;
42
    ulong32 state[5], curlen;
43
    unsigned char buf[64];
44
};
45
#endif
46
47
#ifdef LTC_MD5
48
struct md5_state {
49
    ulong64 length;
50
    ulong32 state[4], curlen;
51
    unsigned char buf[64];
52
};
53
#endif
54
55
#ifdef LTC_MD4
56
struct md4_state {
57
    ulong64 length;
58
    ulong32 state[4], curlen;
59
    unsigned char buf[64];
60
};
61
#endif
62
63
#ifdef LTC_TIGER
64
struct tiger_state {
65
    ulong64 state[3], length;
66
    unsigned long curlen;
67
    unsigned char buf[64];
68
};
69
#endif
70
71
#ifdef LTC_MD2
72
struct md2_state {
73
    unsigned char chksum[16], X[48], buf[16];
74
    unsigned long curlen;
75
};
76
#endif
77
78
#ifdef LTC_RIPEMD128
79
struct rmd128_state {
80
    ulong64 length;
81
    unsigned char buf[64];
82
    ulong32 curlen, state[4];
83
};
84
#endif
85
86
#ifdef LTC_RIPEMD160
87
struct rmd160_state {
88
    ulong64 length;
89
    unsigned char buf[64];
90
    ulong32 curlen, state[5];
91
};
92
#endif
93
94
#ifdef LTC_RIPEMD256
95
struct rmd256_state {
96
    ulong64 length;
97
    unsigned char buf[64];
98
    ulong32 curlen, state[8];
99
};
100
#endif
101
102
#ifdef LTC_RIPEMD320
103
struct rmd320_state {
104
    ulong64 length;
105
    unsigned char buf[64];
106
    ulong32 curlen, state[10];
107
};
108
#endif
109
110
#ifdef LTC_WHIRLPOOL
111
struct whirlpool_state {
112
    ulong64 length, state[8];
113
    unsigned char buf[64];
114
    ulong32 curlen;
115
};
116
#endif
117
118
#ifdef LTC_CHC_HASH
119
struct chc_state {
120
    ulong64 length;
121
    unsigned char state[MAXBLOCKSIZE], buf[MAXBLOCKSIZE];
122
    ulong32 curlen;
123
};
124
#endif
125
126
#ifdef LTC_BLAKE2S
127
struct blake2s_state {
128
    ulong32 h[8];
129
    ulong32 t[2];
130
    ulong32 f[2];
131
    unsigned char buf[64];
132
    unsigned long curlen;
133
    unsigned long outlen;
134
    unsigned char last_node;
135
};
136
#endif
137
138
#ifdef LTC_BLAKE2B
139
struct blake2b_state {
140
    ulong64 h[8];
141
    ulong64 t[2];
142
    ulong64 f[2];
143
    unsigned char buf[128];
144
    unsigned long curlen;
145
    unsigned long outlen;
146
    unsigned char last_node;
147
};
148
#endif
149
150
typedef union Hash_state {
151
    char dummy[1];
152
#ifdef LTC_CHC_HASH
153
    struct chc_state chc;
154
#endif
155
#ifdef LTC_WHIRLPOOL
156
    struct whirlpool_state whirlpool;
157
#endif
158
#ifdef LTC_SHA3
159
    struct sha3_state sha3;
160
#endif
161
#ifdef LTC_SHA512
162
    struct sha512_state sha512;
163
#endif
164
#ifdef LTC_SHA256
165
    struct sha256_state sha256;
166
#endif
167
#ifdef LTC_SHA1
168
    struct sha1_state   sha1;
169
#endif
170
#ifdef LTC_MD5
171
    struct md5_state    md5;
172
#endif
173
#ifdef LTC_MD4
174
    struct md4_state    md4;
175
#endif
176
#ifdef LTC_MD2
177
    struct md2_state    md2;
178
#endif
179
#ifdef LTC_TIGER
180
    struct tiger_state  tiger;
181
#endif
182
#ifdef LTC_RIPEMD128
183
    struct rmd128_state rmd128;
184
#endif
185
#ifdef LTC_RIPEMD160
186
    struct rmd160_state rmd160;
187
#endif
188
#ifdef LTC_RIPEMD256
189
    struct rmd256_state rmd256;
190
#endif
191
#ifdef LTC_RIPEMD320
192
    struct rmd320_state rmd320;
193
#endif
194
#ifdef LTC_BLAKE2S
195
    struct blake2s_state blake2s;
196
#endif
197
#ifdef LTC_BLAKE2B
198
    struct blake2b_state blake2b;
199
#endif
200
201
    void *data;
202
} hash_state;
203
204
/** hash descriptor */
205
extern  struct ltc_hash_descriptor {
206
    /** name of hash */
207
    const char *name;
208
    /** internal ID */
209
    unsigned char ID;
210
    /** Size of digest in octets */
211
    unsigned long hashsize;
212
    /** Input block size in octets */
213
    unsigned long blocksize;
214
    /** ASN.1 OID */
215
    unsigned long OID[16];
216
    /** Length of DER encoding */
217
    unsigned long OIDlen;
218
219
    /** Init a hash state
220
      @param hash   The hash to initialize
221
      @return CRYPT_OK if successful
222
    */
223
    int (*init)(hash_state *hash);
224
    /** Process a block of data
225
      @param hash   The hash state
226
      @param in     The data to hash
227
      @param inlen  The length of the data (octets)
228
      @return CRYPT_OK if successful
229
    */
230
    int (*process)(hash_state *hash, const unsigned char *in, unsigned long inlen);
231
    /** Produce the digest and store it
232
      @param hash   The hash state
233
      @param out    [out] The destination of the digest
234
      @return CRYPT_OK if successful
235
    */
236
    int (*done)(hash_state *hash, unsigned char *out);
237
    /** Self-test
238
      @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
239
    */
240
    int (*test)(void);
241
242
    /* accelerated hmac callback: if you need to-do multiple packets just use the generic hmac_memory and provide a hash callback */
243
    int  (*hmac_block)(const unsigned char *key, unsigned long  keylen,
244
                       const unsigned char *in,  unsigned long  inlen,
245
                             unsigned char *out, unsigned long *outlen);
246
247
} hash_descriptor[];
248
249
#ifdef LTC_CHC_HASH
250
int chc_register(int cipher);
251
int chc_init(hash_state * md);
252
int chc_process(hash_state * md, const unsigned char *in, unsigned long inlen);
253
int chc_done(hash_state * md, unsigned char *hash);
254
int chc_test(void);
255
extern const struct ltc_hash_descriptor chc_desc;
256
#endif
257
258
#ifdef LTC_WHIRLPOOL
259
int whirlpool_init(hash_state * md);
260
int whirlpool_process(hash_state * md, const unsigned char *in, unsigned long inlen);
261
int whirlpool_done(hash_state * md, unsigned char *hash);
262
int whirlpool_test(void);
263
extern const struct ltc_hash_descriptor whirlpool_desc;
264
#endif
265
266
#ifdef LTC_SHA3
267
int sha3_512_init(hash_state * md);
268
int sha3_512_test(void);
269
extern const struct ltc_hash_descriptor sha3_512_desc;
270
int sha3_384_init(hash_state * md);
271
int sha3_384_test(void);
272
extern const struct ltc_hash_descriptor sha3_384_desc;
273
int sha3_256_init(hash_state * md);
274
int sha3_256_test(void);
275
extern const struct ltc_hash_descriptor sha3_256_desc;
276
int sha3_224_init(hash_state * md);
277
int sha3_224_test(void);
278
extern const struct ltc_hash_descriptor sha3_224_desc;
279
/* process + done are the same for all variants */
280
int sha3_process(hash_state * md, const unsigned char *in, unsigned long inlen);
281
int sha3_done(hash_state *md, unsigned char *hash);
282
/* SHAKE128 + SHAKE256 */
283
int sha3_shake_init(hash_state *md, int num);
284
#define sha3_shake_process(a,b,c) sha3_process(a,b,c)
285
int sha3_shake_done(hash_state *md, unsigned char *out, unsigned long outlen);
286
int sha3_shake_test(void);
287
int sha3_shake_memory(int num, const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen);
288
#endif
289
290
#ifdef LTC_SHA512
291
int sha512_init(hash_state * md);
292
int sha512_process(hash_state * md, const unsigned char *in, unsigned long inlen);
293
int sha512_done(hash_state * md, unsigned char *hash);
294
int sha512_test(void);
295
extern const struct ltc_hash_descriptor sha512_desc;
296
#endif
297
298
#ifdef LTC_SHA384
299
#ifndef LTC_SHA512
300
   #error LTC_SHA512 is required for LTC_SHA384
301
#endif
302
int sha384_init(hash_state * md);
303
#define sha384_process sha512_process
304
int sha384_done(hash_state * md, unsigned char *hash);
305
int sha384_test(void);
306
extern const struct ltc_hash_descriptor sha384_desc;
307
#endif
308
309
#ifdef LTC_SHA512_256
310
#ifndef LTC_SHA512
311
   #error LTC_SHA512 is required for LTC_SHA512_256
312
#endif
313
int sha512_256_init(hash_state * md);
314
#define sha512_256_process sha512_process
315
int sha512_256_done(hash_state * md, unsigned char *hash);
316
int sha512_256_test(void);
317
extern const struct ltc_hash_descriptor sha512_256_desc;
318
#endif
319
320
#ifdef LTC_SHA512_224
321
#ifndef LTC_SHA512
322
   #error LTC_SHA512 is required for LTC_SHA512_224
323
#endif
324
int sha512_224_init(hash_state * md);
325
#define sha512_224_process sha512_process
326
int sha512_224_done(hash_state * md, unsigned char *hash);
327
int sha512_224_test(void);
328
extern const struct ltc_hash_descriptor sha512_224_desc;
329
#endif
330
331
#ifdef LTC_SHA256
332
int sha256_init(hash_state * md);
333
int sha256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
334
int sha256_done(hash_state * md, unsigned char *hash);
335
int sha256_test(void);
336
extern const struct ltc_hash_descriptor sha256_desc;
337
338
#ifdef LTC_SHA224
339
#ifndef LTC_SHA256
340
   #error LTC_SHA256 is required for LTC_SHA224
341
#endif
342
int sha224_init(hash_state * md);
343
#define sha224_process sha256_process
344
int sha224_done(hash_state * md, unsigned char *hash);
345
int sha224_test(void);
346
extern const struct ltc_hash_descriptor sha224_desc;
347
#endif
348
#endif
349
350
#ifdef LTC_SHA1
351
int sha1_init(hash_state * md);
352
int sha1_process(hash_state * md, const unsigned char *in, unsigned long inlen);
353
int sha1_done(hash_state * md, unsigned char *hash);
354
int sha1_test(void);
355
extern const struct ltc_hash_descriptor sha1_desc;
356
#endif
357
358
#ifdef LTC_BLAKE2S
359
extern const struct ltc_hash_descriptor blake2s_256_desc;
360
int blake2s_256_init(hash_state * md);
361
int blake2s_256_test(void);
362
363
extern const struct ltc_hash_descriptor blake2s_224_desc;
364
int blake2s_224_init(hash_state * md);
365
int blake2s_224_test(void);
366
367
extern const struct ltc_hash_descriptor blake2s_160_desc;
368
int blake2s_160_init(hash_state * md);
369
int blake2s_160_test(void);
370
371
extern const struct ltc_hash_descriptor blake2s_128_desc;
372
int blake2s_128_init(hash_state * md);
373
int blake2s_128_test(void);
374
375
int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
376
int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen);
377
int blake2s_done(hash_state * md, unsigned char *hash);
378
#endif
379
380
#ifdef LTC_BLAKE2B
381
extern const struct ltc_hash_descriptor blake2b_512_desc;
382
int blake2b_512_init(hash_state * md);
383
int blake2b_512_test(void);
384
385
extern const struct ltc_hash_descriptor blake2b_384_desc;
386
int blake2b_384_init(hash_state * md);
387
int blake2b_384_test(void);
388
389
extern const struct ltc_hash_descriptor blake2b_256_desc;
390
int blake2b_256_init(hash_state * md);
391
int blake2b_256_test(void);
392
393
extern const struct ltc_hash_descriptor blake2b_160_desc;
394
int blake2b_160_init(hash_state * md);
395
int blake2b_160_test(void);
396
397
int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
398
int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen);
399
int blake2b_done(hash_state * md, unsigned char *hash);
400
#endif
401
402
#ifdef LTC_MD5
403
int md5_init(hash_state * md);
404
int md5_process(hash_state * md, const unsigned char *in, unsigned long inlen);
405
int md5_done(hash_state * md, unsigned char *hash);
406
int md5_test(void);
407
extern const struct ltc_hash_descriptor md5_desc;
408
#endif
409
410
#ifdef LTC_MD4
411
int md4_init(hash_state * md);
412
int md4_process(hash_state * md, const unsigned char *in, unsigned long inlen);
413
int md4_done(hash_state * md, unsigned char *hash);
414
int md4_test(void);
415
extern const struct ltc_hash_descriptor md4_desc;
416
#endif
417
418
#ifdef LTC_MD2
419
int md2_init(hash_state * md);
420
int md2_process(hash_state * md, const unsigned char *in, unsigned long inlen);
421
int md2_done(hash_state * md, unsigned char *hash);
422
int md2_test(void);
423
extern const struct ltc_hash_descriptor md2_desc;
424
#endif
425
426
#ifdef LTC_TIGER
427
int tiger_init(hash_state * md);
428
int tiger_process(hash_state * md, const unsigned char *in, unsigned long inlen);
429
int tiger_done(hash_state * md, unsigned char *hash);
430
int tiger_test(void);
431
extern const struct ltc_hash_descriptor tiger_desc;
432
#endif
433
434
#ifdef LTC_RIPEMD128
435
int rmd128_init(hash_state * md);
436
int rmd128_process(hash_state * md, const unsigned char *in, unsigned long inlen);
437
int rmd128_done(hash_state * md, unsigned char *hash);
438
int rmd128_test(void);
439
extern const struct ltc_hash_descriptor rmd128_desc;
440
#endif
441
442
#ifdef LTC_RIPEMD160
443
int rmd160_init(hash_state * md);
444
int rmd160_process(hash_state * md, const unsigned char *in, unsigned long inlen);
445
int rmd160_done(hash_state * md, unsigned char *hash);
446
int rmd160_test(void);
447
extern const struct ltc_hash_descriptor rmd160_desc;
448
#endif
449
450
#ifdef LTC_RIPEMD256
451
int rmd256_init(hash_state * md);
452
int rmd256_process(hash_state * md, const unsigned char *in, unsigned long inlen);
453
int rmd256_done(hash_state * md, unsigned char *hash);
454
int rmd256_test(void);
455
extern const struct ltc_hash_descriptor rmd256_desc;
456
#endif
457
458
#ifdef LTC_RIPEMD320
459
int rmd320_init(hash_state * md);
460
int rmd320_process(hash_state * md, const unsigned char *in, unsigned long inlen);
461
int rmd320_done(hash_state * md, unsigned char *hash);
462
int rmd320_test(void);
463
extern const struct ltc_hash_descriptor rmd320_desc;
464
#endif
465
466
467
int find_hash(const char *name);
468
int find_hash_id(unsigned char ID);
469
int find_hash_oid(const unsigned long *ID, unsigned long IDlen);
470
int find_hash_any(const char *name, int digestlen);
471
int register_hash(const struct ltc_hash_descriptor *hash);
472
int unregister_hash(const struct ltc_hash_descriptor *hash);
473
int register_all_hashes(void);
474
int hash_is_valid(int idx);
475
476
LTC_MUTEX_PROTO(ltc_hash_mutex)
477
478
int hash_memory(int hash,
479
                const unsigned char *in,  unsigned long inlen,
480
                      unsigned char *out, unsigned long *outlen);
481
int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
482
                      const unsigned char *in, unsigned long inlen, ...);
483
484
#ifndef LTC_NO_FILE
485
int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outlen);
486
int hash_file(int hash, const char *fname, unsigned char *out, unsigned long *outlen);
487
#endif
488
489
/* a simple macro for making hash "process" functions */
490
#define HASH_PROCESS(func_name, compress_name, state_var, block_size)                       \
491
4.39k
int func_name (hash_state * md, const unsigned char *in, unsigned long inlen)               \
492
4.39k
{                                                                                           \
493
4.39k
    unsigned long n;                                                                        \
494
4.39k
    int           err;                                                                      \
495
4.39k
    LTC_ARGCHK(md != NULL);                                                                 \
496
4.39k
    LTC_ARGCHK(in != NULL);                                                                 \
497
4.39k
    if (md-> state_var .curlen > sizeof(md-> state_var .buf)) {                             \
498
0
       return CRYPT_INVALID_ARG;                                                            \
499
0
    }                                                                                       \
500
4.39k
    if ((md-> state_var .length + inlen) < md-> state_var .length) {                        \
501
0
      return CRYPT_HASH_OVERFLOW;                                                           \
502
0
    }                                                                                       \
503
13.8k
    while (inlen > 0) {                                                                     \
504
9.46k
        if (md-> state_var .curlen == 0 && inlen >= block_size) {                           \
505
5.09k
           if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) {               \
506
0
              return err;                                                                   \
507
0
           }                                                                                \
508
5.09k
           md-> state_var .length += block_size * 8;                                        \
509
5.09k
           in             += block_size;                                                    \
510
5.09k
           inlen          -= block_size;                                                    \
511
5.09k
        } else {                                                                            \
512
4.37k
           n = MIN(inlen, (block_size - md-> state_var .curlen));                           \
513
4.37k
           XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n);             \
514
4.37k
           md-> state_var .curlen += n;                                                     \
515
4.37k
           in             += n;                                                             \
516
4.37k
           inlen          -= n;                                                             \
517
4.37k
           if (md-> state_var .curlen == block_size) {                                      \
518
0
              if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {            \
519
0
                 return err;                                                                \
520
0
              }                                                                             \
521
0
              md-> state_var .length += 8*block_size;                                       \
522
0
              md-> state_var .curlen = 0;                                                   \
523
0
           }                                                                                \
524
4.37k
       }                                                                                    \
525
9.46k
    }                                                                                       \
526
4.39k
    return CRYPT_OK;                                                                        \
527
4.39k
}
Unexecuted instantiation: sha1_process
sha256_process
Line
Count
Source
491
4.39k
int func_name (hash_state * md, const unsigned char *in, unsigned long inlen)               \
492
4.39k
{                                                                                           \
493
4.39k
    unsigned long n;                                                                        \
494
4.39k
    int           err;                                                                      \
495
4.39k
    LTC_ARGCHK(md != NULL);                                                                 \
496
4.39k
    LTC_ARGCHK(in != NULL);                                                                 \
497
4.39k
    if (md-> state_var .curlen > sizeof(md-> state_var .buf)) {                             \
498
0
       return CRYPT_INVALID_ARG;                                                            \
499
0
    }                                                                                       \
500
4.39k
    if ((md-> state_var .length + inlen) < md-> state_var .length) {                        \
501
0
      return CRYPT_HASH_OVERFLOW;                                                           \
502
0
    }                                                                                       \
503
13.8k
    while (inlen > 0) {                                                                     \
504
9.46k
        if (md-> state_var .curlen == 0 && inlen >= block_size) {                           \
505
5.09k
           if ((err = compress_name (md, (unsigned char *)in)) != CRYPT_OK) {               \
506
0
              return err;                                                                   \
507
0
           }                                                                                \
508
5.09k
           md-> state_var .length += block_size * 8;                                        \
509
5.09k
           in             += block_size;                                                    \
510
5.09k
           inlen          -= block_size;                                                    \
511
5.09k
        } else {                                                                            \
512
4.37k
           n = MIN(inlen, (block_size - md-> state_var .curlen));                           \
513
4.37k
           XMEMCPY(md-> state_var .buf + md-> state_var.curlen, in, (size_t)n);             \
514
4.37k
           md-> state_var .curlen += n;                                                     \
515
4.37k
           in             += n;                                                             \
516
4.37k
           inlen          -= n;                                                             \
517
4.37k
           if (md-> state_var .curlen == block_size) {                                      \
518
0
              if ((err = compress_name (md, md-> state_var .buf)) != CRYPT_OK) {            \
519
0
                 return err;                                                                \
520
0
              }                                                                             \
521
0
              md-> state_var .length += 8*block_size;                                       \
522
0
              md-> state_var .curlen = 0;                                                   \
523
0
           }                                                                                \
524
4.37k
       }                                                                                    \
525
9.46k
    }                                                                                       \
526
4.39k
    return CRYPT_OK;                                                                        \
527
4.39k
}
Unexecuted instantiation: sha512_process
528
529
/* ref:         $Format:%D$ */
530
/* git commit:  $Format:%H$ */
531
/* commit time: $Format:%ai$ */