libfossil
hash.h
Go to the documentation of this file.
1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* vim: set ts=2 et sw=2 tw=80: */
3#if !defined(ORG_FOSSIL_SCM_FSL_HASH_H_INCLUDED)
4#define ORG_FOSSIL_SCM_FSL_HASH_H_INCLUDED
5/*
6 Copyright 2013-2021 The Libfossil Authors, see LICENSES/BSD-2-Clause.txt
7
8 SPDX-License-Identifier: BSD-2-Clause-FreeBSD
9 SPDX-FileCopyrightText: 2021 The Libfossil Authors
10 SPDX-FileType: Code
11
12 Heavily indebted to the Fossil SCM project (https://fossil-scm.org).
13
14 *****************************************************************************
15 This file declares public APIs relating to hashing.
16*/
17
18#include "util.h" /* MUST come first b/c of config macros */
19#if !defined(FSL_SHA1_HARDENED)
20# define FSL_SHA1_HARDENED 1
21#endif
22#if defined(__cplusplus)
23extern "C" {
24#endif
25
26/**
27 Various set-in-stone constants used by the API.
28*/
30/**
31 The length, in bytes, of a hex-format SHA1 hash string.
32*/
34/**
35 The length, in bytes, of a hex-format SHA3-256 hash string.
36*/
38/**
39 The length, in bytes, of a hex-format MD5 hash string.
40*/
42
43/** Minimum length of a full fossil UUID. */
45/** Maximum length of a full fossil UUID. */
47};
48
49/**
50 Unique IDs for hash types supported by fossil.
51*/
53/** Invalid hash type. */
55/** SHA1. */
57/** SHA3-256. */
59};
61
62typedef struct fsl_md5_cx fsl_md5_cx;
63typedef struct fsl_sha1_cx fsl_sha1_cx;
64typedef struct fsl_sha3_cx fsl_sha3_cx;
65
66/**
67 The hash string of the initial MD5 state. Used as an
68 optimization for some places where we need an MD5 but know it
69 will not hash any data.
70
71 Equivalent to what the md5sum command outputs for empty input:
72
73 ```
74 # md5sum < /dev/null
75 d41d8cd98f00b204e9800998ecf8427e -
76 ```
77*/
78#define FSL_MD5_INITIAL_HASH "d41d8cd98f00b204e9800998ecf8427e"
79
80/**
81 Holds state for MD5 calculations. It is intended to be used like
82 this:
83
84 ```
85 unsigned char digest[FSL_STRLEN_MD5/2];
86 char hex[FSL_STRLEN_MD5+1];
87 fsl_md5_cx cx = fsl_md5_cx_empty;
88 // alternately: fsl_md5_init(&cx);
89 ...call fsl_md5_update(&cx,...) any number of times to
90 ...incrementally calculate the hash.
91 fsl_md5_final(&cx, digest); // ends the calculation
92 fsl_md5_digest_to_base16(digest, hex);
93 // digest now contains the raw 16-byte MD5 digest.
94 // hex now contains the 32-byte MD5 + a trailing NUL
95 ```
96*/
97struct fsl_md5_cx {
98 int isInit;
99 uint32_t buf[4];
100 uint32_t bits[2];
101 unsigned char in[64];
102};
103
104/**
105 A filled-with-defaults fsl_md5_cx instance appropriate for
106 const-copy initialization. Instances must either be
107 copy-initialized from this instance or they must be
108 passed to fsl_md5_init() before they are used.
109*/
110#define fsl_md5_cx_empty_m { \
111 1/*isInit*/, \
112 {/*buf*/0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }, \
113 {/*bits*/0,0}, \
114 {0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
115 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
116 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0, \
117 0,0,0,0}}
118
119/**
120 Equivalent to fsl_md5_cx_empty_m but suitable for non-const
121 copy initialization.
122*/
124
125/**
126 Initializes the given context pointer. It must not be NULL. This
127 must be the first routine called on any fsl_md5_cx instances.
128 Alternately, copy-constructing from fsl_md5_cx_empty has the same
129 effect.
130
131 @see fsl_md5_update()
132 @see fsl_md5_final()
133*/
135
136/**
137 Updates cx's state to reflect the addition of the data specified by
138 the range (buf, buf+len]. Neither cx nor buf may be NULL. This may
139 be called an arbitrary number of times between fsl_md5_init() and
140 fsl_md5_final().
141
142 @see fsl_md5_init()
143 @see fsl_md5_final()
144*/
145FSL_EXPORT void fsl_md5_update(fsl_md5_cx *cx, void const * buf, fsl_size_t len);
146
147/**
148 Finishes up the calculation of the md5 for the given context and
149 writes a 16-byte digest value to the 2nd parameter. Use
150 fsl_md5_digest_to_base16() to convert the digest output value to
151 hexadecimal form.
152
153 @see fsl_md5_init()
154 @see fsl_md5_update()
155 @see fsl_md5_digest_to_base16()
156*/
157FSL_EXPORT void fsl_md5_final(fsl_md5_cx * cx, unsigned char * digest);
158
159/**
160 Converts an md5 digest value (from fsl_md5_final()'s 2nd
161 parameter) to a 32-byte (FSL_STRLEN_MD5) CRC string plus a
162 terminating NUL byte. i.e. zBuf must be at least
163 (FSL_STRLEN_MD5+1) bytes long.
164
165 @see fsl_md5_final()
166*/
167FSL_EXPORT void fsl_md5_digest_to_base16(unsigned char const *digest,
168 char *zBuf);
169
170/**
171 The md5 counterpart of fsl_sha1sum_buffer(), identical in
172 semantics except that its result is an MD5 hash instead of an
173 SHA1 hash and the resulting hex string is FSL_STRLEN_MD5 bytes
174 long plus a terminating NUL.
175*/
177
178/**
179 The md5 counterpart of fsl_sha1sum_cstr(), identical in
180 semantics except that its result is an MD5 hash instead of an
181 SHA1 hash and the resulting string is FSL_STRLEN_MD5 bytes long
182 plus a terminating NUL.
183*/
184FSL_EXPORT char *fsl_md5sum_cstr(const char *zIn, fsl_int_t len);
185
186/**
187 The MD5 counter part to fsl_sha1sum_stream(), with identical
188 semantics except that the generated hash is an MD5 string
189 instead of SHA1.
190*/
191FSL_EXPORT int fsl_md5sum_stream(fsl_input_f src, void * srcState, fsl_buffer *pCksum);
192
193/**
194 Reads all input from src() and passes it through fsl_md5_update(cx,...).
195 Returns 0 on success, FSL_RC_MISUSE if !cx or !src. If src returns
196 a non-0 code, that code is returned from here.
197*/
199
200/**
201 Equivalent to fsl_md5_update(cx, b->mem, b->used). Results are undefined
202 if either pointer is invalid or NULL.
203*/
205
206/**
207 Passes the first len bytes of str to fsl_md5_update(cx). If len
208 is less than 0 then fsl_strlen() is used to calculate the
209 length. Results are undefined if either pointer is invalid or
210 NULL. This is a no-op if !len or (len<0 && !*str).
211*/
212FSL_EXPORT void fsl_md5_update_cstr(fsl_md5_cx *cx, char const * str, fsl_int_t len);
213
214/**
215 A fsl_md5_update_stream() proxy which updates cx to include the
216 contents of the given file.
217*/
218FSL_EXPORT int fsl_md5_update_filename(fsl_md5_cx *cx, char const * fname);
219
220/**
221 The MD5 counter part to fsl_sha1sum_filename(), with identical
222 semantics except that the generated hash is an MD5 string
223 instead of SHA1.
224*/
225FSL_EXPORT int fsl_md5sum_filename(const char *zFilename, fsl_buffer *pCksum);
226
227
228#if FSL_SHA1_HARDENED
229typedef void(*fsl_sha1h_collision_callback)(uint64_t, const uint32_t*, const uint32_t*, const uint32_t*, const uint32_t*);
230#endif
231/**
232 Holds state for SHA1 calculations. It is intended to be used
233 like this:
234
235 ```
236 unsigned char digest[FSL_STRLEN_SHA1/2]
237 char hex[FSL_STRLEN_SHA1+1];
238 fsl_sha1_cx cx;
239 fsl_sha1_init(&cx); // REQUIRED, unlike its md5 counterpart
240
241 ...call fsl_sha1_update(&cx,...) any number of times to
242 ...incrementally calculate the hash.
243
244 fsl_sha1_final(&cx, digest); // ends the calculation
245 fsl_sha1_digest_to_base16(digest, hex);
246
247 // digest now contains the raw 20-byte SHA1 digest.
248 // hex now contains the 40-byte SHA1 + a trailing NUL
249 ```
250*/
252#if FSL_SHA1_HARDENED
253 uint64_t total;
254 uint32_t ihv[5];
255 unsigned char buffer[64];
256 int bigendian;
257 int found_collision;
258 int safe_hash;
259 int detect_coll;
260 int ubc_check;
261 int reduced_round_coll;
262 fsl_sha1h_collision_callback callback;
263 uint32_t ihv1[5];
264 uint32_t ihv2[5];
265 uint32_t m1[80];
266 uint32_t m2[80];
267 uint32_t states[80][5];
268#else
269 unsigned int state[5];
270 unsigned int count[2];
271 unsigned char buffer[64];
272#endif
273};
274/**
275 fsl_sha1_cx instance intended for in-struct copy initialization.
276*/
277#if FSL_SHA1_HARDENED
278#define fsl_sha1_cx_empty_m {0}
279#else
280#define fsl_sha1_cx_empty_m { \
281 {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 }, \
282 {0,0}, \
283 {0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, \
284 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 \
285 } \
286 }
287#endif
288/**
289 fsl_sha1_cx instance intended for copy initialization. For build
290 config portability, the copied-to object must still be passed to
291 fsl_sha1_init() to initialize it.
292*/
294
295/**
296 Initializes the given context with the initial SHA1 state. This
297 must be the first routine called on an SHA1 context, and passing
298 this context to other SHA1 routines without first having passed
299 it to this will lead to undefined results.
300
301 @see fsl_sha1_update()
302 @see fsl_sha1_final()
303*/
305
306/**
307 Updates the given context to include the hash of the first len
308 bytes of the given data.
309
310 @see fsl_sha1_init()
311 @see fsl_sha1_final()
312*/
313FSL_EXPORT void fsl_sha1_update( fsl_sha1_cx *context, void const *data, fsl_size_t len);
314
315/**
316 Add padding and finalizes the message digest. If digest is not NULL
317 then it writes 20 bytes of digest to the 2nd parameter. If this
318 library is configured with hardened SHA1 hashes, this function
319 returns non-0 if a collision was detected while hashing. If it is
320 not configured for hardened SHA1, or no collision was detected, it
321 returns 0.
322
323 @see fsl_sha1_update()
324 @see fsl_sha1_digest_to_base16()
325*/
326FSL_EXPORT int fsl_sha1_final(fsl_sha1_cx *context, unsigned char * digest);
327
328/**
329 A convenience form of fsl_sha1_final() which writes
330 FSL_STRLEN_SHA1+1 bytes (hash plus terminating NUL byte) to the
331 2nd argument and returns a (const char *)-type cast of the 2nd
332 argument.
333*/
334FSL_EXPORT const char * fsl_sha1_final_hex(fsl_sha1_cx *context, char * zHex);
335
336/**
337 Convert a digest into base-16. digest must be at least 20 bytes
338 long and hold an SHA1 digest. zBuf must be at least (FSL_STRLEN_SHA1
339 + 1) bytes long, to which FSL_STRLEN_SHA1 characters of
340 hexidecimal-form SHA1 hash and 1 NUL byte will be written.
341
342 @see fsl_sha1_final()
343*/
344FSL_EXPORT void fsl_sha1_digest_to_base16(unsigned char const *digest,
345 char *zBuf);
346
347/**
348 Computes the SHA1 checksum of pIn and stores the resulting
349 checksum in the buffer pCksum. pCksum's memory is re-used if is
350 has any allocated to it. pCksum may == pIn, in which case this
351 is a destructive operation (replacing the hashed data with its
352 hash code).
353
354 Return 0 on success, FSL_RC_OOM if (re)allocating pCksum fails.
355*/
357
358/**
359 Works like fsl_sha1sum_buffer() except that it appends the hash to
360 the target buffer, rather than re-using the whole target. It is
361 legal for both arguments to refer to the same object.
362*/
364
365/**
366 Computes the SHA1 checksum of the first len bytes of the given
367 string. If len is negative then zIn must be NUL-terminated and
368 fsl_strlen() is used to find its length. The result is a
369 FSL_UUID_STRLEN-byte string (+NUL byte) returned in memory
370 obtained from fsl_malloc(), so it must be passed to fsl_free()
371 to free it. If NULL==zIn or !len then NULL is returned.
372*/
373FSL_EXPORT char *fsl_sha1sum_cstr(const char *zIn, fsl_int_t len);
374
375/**
376 Consumes all input from src and calculates its SHA1 hash. The
377 result is set in pCksum (its contents, if any, are overwritten,
378 not appended to). Returns 0 on success. Returns FSL_RC_MISUSE if
379 !src or !pCksum. It keeps consuming input from src() until that
380 function reads fewer bytes than requested, at which point EOF is
381 assumed. If src() returns non-0, that code is returned from this
382 function.
383*/
384FSL_EXPORT int fsl_sha1sum_stream(fsl_input_f src, void * srcState, fsl_buffer *pCksum);
385
386
387/**
388 A fsl_sha1sum_stream() wrapper which calculates the SHA1 of
389 given file.
390
391 Returns FSL_RC_IO if the file cannot be opened, FSL_RC_MISUSE if
392 !zFilename or !pCksum, else as per fsl_sha1sum_stream().
393
394 TODO: the v1 impl has special behaviour for symlinks which this
395 function lacks. For that support we need a variant of this
396 function which takes a fsl_cx parameter (for the allow-symlinks
397 setting).
398*/
399FSL_EXPORT int fsl_sha1sum_filename(const char *zFilename, fsl_buffer *pCksum);
400
401/**
402 Reads all input from src() and passes it through fsl_sha1_update(cx,...).
403 Returns 0 on success, FSL_RC_MISUSE if !cx or !src. If src returns
404 a non-0 code, that code is returned from here.
405*/
407 void * srcState);
408
409/**
410 Legal values for SHA3 hash sizes, in bits: an increment of 32 bits
411 in the inclusive range (128..512).
412
413 The hexidecimal-code size, in bytes, of any given bit size in this
414 enum is the bit size/4.
415*/
417/** Sentinel value. Must be 0. */
424/* Default SHA3 flavor */
426};
427
428/**
429 Type for holding SHA3 processing state. Each instance must be
430 initialized with fsl_sha3_init(), populated with fsl_sha3_update(),
431 and "sealed" with fsl_sha3_end().
432
433 Sample usage:
434
435 ```
436 fsl_sha3_cx cx;
437 fsl_sha3_init(&cx, FSL_SHA3_DEFAULT);
438 fsl_sha3_update(&cx, memory, lengthOfMemory);
439 fsl_sha3_end(&cx);
440 printf("Hash = %s\n", (char const *)cx.hex);
441 ```
442
443 After fsl_sha3_end() is called cx.hex contains the hex-string forms
444 of the digest. fsl_sha3_update() may be called an arbitrary number
445 of times to feed in chunks of memory (e.g. to stream in arbitrarily
446 large data).
447*/
449 union {
450 uint64_t s[25]; /* Keccak state. 5x5 lines of 64 bits each */
451 unsigned char x[1600]; /* ... or 1600 bytes */
452 } u;
453 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
454 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
455 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
456 enum fsl_sha3_hash_size size; /* Size of the hash, in bits. */
457 unsigned char hex[132]; /* Hex form of final digest: 56-128 bytes
458 plus terminating NUL. */
459};
460
461/**
462 If the given number is a valid fsl_sha3_hash_size value, its enum
463 entry is returned, else FSL_SHA3_INVALID is returned.
464
465 @see fsl_sha3_init()
466*/
468
469/**
470 Initialize a new hash. The second argument specifies the size of
471 the hash in bits. Results are undefined if cx is NULL or sz is not
472 a valid positive value.
473
474 After calling this, use fsl_sha3_update() to hash data and
475 fsl_sha3_end() to finalize the hashing process and generate a digest.
476*/
478
479/**
480 Equivalent to fsl_sha3_init2(cx, FSL_SHA3_DEFAULT).
481*/
483
484/**
485 Updates cx's state to include the first len bytes of data.
486
487 If cx is NULL results are undefined (segfault!). If mem is not
488 NULL then it must be at least n bytes long. If n is 0 then this
489 function has no side-effects.
490
491 @see fsl_sha3_init()
492 @see fsl_sha3_end()
493*/
494FSL_EXPORT void fsl_sha3_update( fsl_sha3_cx *cx, void const *data, unsigned int len);
495
496/**
497 To be called when SHA3 hashing is complete: finishes the hash
498 calculation and populates cx->hex with the final hash code in
499 hexidecimal-string form. Returns the binary-form digest value,
500 which refers to cx->size/8 bytes of memory which lives in the cx
501 object. After this call cx->hex will be populated with cx->size/4
502 bytes of lower-case ASCII hex codes plus a terminating NUL byte.
503
504 Potential TODO: change fsl_sha1_final() and fsl_md5_final() to use
505 these same return semantics.
506
507 @see fsl_sha3_init()
508 @see fsl_sha3_update()
509*/
510FSL_EXPORT unsigned char const * fsl_sha3_end(fsl_sha3_cx *cx);
511
512/**
513 SHA3-256 counterpart of fsl_sha1_digest_to_base16(). digest must be at least
514 32 bytes long and hold an SHA3 digest. zBuf must be at least (FSL_STRLEN_K256+1)
515 bytes long, to which FSL_STRLEN_K256 characters of
516 hexidecimal-form SHA3 hash and 1 NUL byte will be written
517
518 @see fsl_sha3_end().
519*/
520FSL_EXPORT void fsl_sha3_digest_to_base16(unsigned char const *digest,
521 char *zBuf);
522/**
523 SHA3 counter part of fsl_sha1sum_buffer().
524*/
526/**
527 SHA3 counter part of fsl_sha1sum_cstr().
528*/
529FSL_EXPORT char *fsl_sha3sum_cstr(const char *zIn, fsl_int_t len);
530/**
531 SHA3 counterpart of fsl_sha1sum_stream().
532*/
534 fsl_buffer *pCksum);
535/**
536 SHA3 counterpart of fsl_sha1sum_filename().
537*/
538FSL_EXPORT int fsl_sha3sum_filename(const char *zFilename, fsl_buffer *pCksum);
539
540/**
541 Expects zHash to be a full-length hash value of one of the
542 fsl_hash_types_t-specified types, and nHash to be the length, in
543 bytes, of zHash's contents (which must be the full hash length, not
544 a prefix). If zHash can be validated as a hash, its corresponding
545 hash type is returned, else FSL_HTYPE_ERROR is returned.
546*/
548 fsl_size_t nHash);
549
550/**
551 Expects (zHash, nHash) to refer to a full hash (of a supported
552 content hash type) of pIn's contents. This routine hashes pIn's
553 contents and, if it compares equivalent to zHash then the ID of the
554 hash type is returned. On a mismatch, FSL_HTYPE_ERROR is returned.
555*/
557 const char *zHash, int nHash);
558
559/**
560 Returns a human-readable name for the given hash type, or its
561 second argument h is not a supported hash type.
562 */
564 const char *zUnknown);
565
566#if defined(__cplusplus)
567} /*extern "C"*/
568#endif
569#endif
570/* ORG_FOSSIL_SCM_FSL_HASH_H_INCLUDED */
uint64_t fsl_size_t
fsl_size_t is an unsigned integer type used to denote absolute ranges and lengths.
Definition config.h:192
int64_t fsl_int_t
fsl_int_t is a signed integer type used to denote "relative" ranges and lengths, or to tell a routine...
Definition config.h:143
#define FSL_EXPORT
Definition config.h:66
FSL_EXPORT const fsl_md5_cx fsl_md5_cx_empty
Equivalent to fsl_md5_cx_empty_m but suitable for non-const copy initialization.
Definition hash.h:123
FSL_EXPORT void fsl_sha3_digest_to_base16(unsigned char const *digest, char *zBuf)
SHA3-256 counterpart of fsl_sha1_digest_to_base16().
FSL_EXPORT void fsl_md5_update_buffer(fsl_md5_cx *cx, fsl_buffer const *b)
Equivalent to fsl_md5_update(cx, b->mem, b->used).
FSL_EXPORT int fsl_md5_update_stream(fsl_md5_cx *cx, fsl_input_f src, void *srcState)
Reads all input from src() and passes it through fsl_md5_update(cx,...).
FSL_EXPORT int fsl_sha1sum_buffer(fsl_buffer const *pIn, fsl_buffer *pCksum)
Computes the SHA1 checksum of pIn and stores the resulting checksum in the buffer pCksum.
FSL_EXPORT void fsl_md5_init(fsl_md5_cx *cx)
Initializes the given context pointer.
FSL_EXPORT char * fsl_md5sum_cstr(const char *zIn, fsl_int_t len)
The md5 counterpart of fsl_sha1sum_cstr(), identical in semantics except that its result is an MD5 ha...
FSL_EXPORT int fsl_md5sum_buffer(fsl_buffer const *pIn, fsl_buffer *pCksum)
The md5 counterpart of fsl_sha1sum_buffer(), identical in semantics except that its result is an MD5 ...
FSL_EXPORT int fsl_sha1sum_stream(fsl_input_f src, void *srcState, fsl_buffer *pCksum)
Consumes all input from src and calculates its SHA1 hash.
FSL_EXPORT int fsl_sha1sum_buffer2(fsl_buffer const *pIn, fsl_buffer *pCksum)
Works like fsl_sha1sum_buffer() except that it appends the hash to the target buffer,...
FSL_EXPORT int fsl_md5sum_stream(fsl_input_f src, void *srcState, fsl_buffer *pCksum)
The MD5 counter part to fsl_sha1sum_stream(), with identical semantics except that the generated hash...
FSL_EXPORT int fsl_md5_update_filename(fsl_md5_cx *cx, char const *fname)
A fsl_md5_update_stream() proxy which updates cx to include the contents of the given file.
FSL_EXPORT char * fsl_sha3sum_cstr(const char *zIn, fsl_int_t len)
SHA3 counter part of fsl_sha1sum_cstr().
FSL_EXPORT int fsl_sha1sum_filename(const char *zFilename, fsl_buffer *pCksum)
A fsl_sha1sum_stream() wrapper which calculates the SHA1 of given file.
FSL_EXPORT fsl_hash_types_e fsl_validate_hash(const char *zHash, fsl_size_t nHash)
Expects zHash to be a full-length hash value of one of the fsl_hash_types_t-specified types,...
FSL_EXPORT fsl_hash_types_e fsl_verify_blob_hash(fsl_buffer const *pIn, const char *zHash, int nHash)
Expects (zHash, nHash) to refer to a full hash (of a supported content hash type) of pIn's contents.
FSL_EXPORT int fsl_md5sum_filename(const char *zFilename, fsl_buffer *pCksum)
The MD5 counter part to fsl_sha1sum_filename(), with identical semantics except that the generated ha...
FSL_EXPORT void fsl_md5_digest_to_base16(unsigned char const *digest, char *zBuf)
Converts an md5 digest value (from fsl_md5_final()'s 2nd parameter) to a 32-byte (FSL_STRLEN_MD5) CRC...
FSL_EXPORT void fsl_sha1_update(fsl_sha1_cx *context, void const *data, fsl_size_t len)
Updates the given context to include the hash of the first len bytes of the given data.
FSL_EXPORT void fsl_sha3_update(fsl_sha3_cx *cx, void const *data, unsigned int len)
Updates cx's state to include the first len bytes of data.
FSL_EXPORT unsigned char const * fsl_sha3_end(fsl_sha3_cx *cx)
To be called when SHA3 hashing is complete: finishes the hash calculation and populates cx->hex with ...
FSL_EXPORT void fsl_sha1_init(fsl_sha1_cx *context)
Initializes the given context with the initial SHA1 state.
FSL_EXPORT int fsl_sha3sum_stream(fsl_input_f src, void *srcState, fsl_buffer *pCksum)
SHA3 counterpart of fsl_sha1sum_stream().
FSL_EXPORT void fsl_md5_final(fsl_md5_cx *cx, unsigned char *digest)
Finishes up the calculation of the md5 for the given context and writes a 16-byte digest value to the...
FSL_EXPORT int fsl_sha3sum_buffer(fsl_buffer const *pIn, fsl_buffer *pCksum)
SHA3 counter part of fsl_sha1sum_buffer().
fsl_sha3_hash_size
Legal values for SHA3 hash sizes, in bits: an increment of 32 bits in the inclusive range (128....
Definition hash.h:416
@ FSL_SHA3_160
Definition hash.h:419
@ FSL_SHA3_512
Definition hash.h:423
@ FSL_SHA3_320
Definition hash.h:421
@ FSL_SHA3_416
Definition hash.h:422
@ FSL_SHA3_288
Definition hash.h:420
@ FSL_SHA3_224
Definition hash.h:420
@ FSL_SHA3_INVALID
Sentinel value.
Definition hash.h:418
@ FSL_SHA3_352
Definition hash.h:421
@ FSL_SHA3_384
Definition hash.h:421
@ FSL_SHA3_448
Definition hash.h:422
@ FSL_SHA3_192
Definition hash.h:419
@ FSL_SHA3_480
Definition hash.h:422
@ FSL_SHA3_DEFAULT
Definition hash.h:425
@ FSL_SHA3_256
Definition hash.h:420
@ FSL_SHA3_128
Definition hash.h:419
FSL_EXPORT enum fsl_sha3_hash_size fsl_sha3_hash_size_for_int(int)
If the given number is a valid fsl_sha3_hash_size value, its enum entry is returned,...
FSL_EXPORT void fsl_sha3_init2(fsl_sha3_cx *cx, enum fsl_sha3_hash_size sz)
Initialize a new hash.
FSL_EXPORT void fsl_sha3_init(fsl_sha3_cx *cx)
Equivalent to fsl_sha3_init2(cx, FSL_SHA3_DEFAULT).
FSL_EXPORT void fsl_md5_update_cstr(fsl_md5_cx *cx, char const *str, fsl_int_t len)
Passes the first len bytes of str to fsl_md5_update(cx).
FSL_EXPORT const char * fsl_sha1_final_hex(fsl_sha1_cx *context, char *zHex)
A convenience form of fsl_sha1_final() which writes FSL_STRLEN_SHA1+1 bytes (hash plus terminating NU...
FSL_EXPORT char * fsl_sha1sum_cstr(const char *zIn, fsl_int_t len)
Computes the SHA1 checksum of the first len bytes of the given string.
FSL_EXPORT void fsl_md5_update(fsl_md5_cx *cx, void const *buf, fsl_size_t len)
Updates cx's state to reflect the addition of the data specified by the range (buf,...
FSL_EXPORT void fsl_sha1_digest_to_base16(unsigned char const *digest, char *zBuf)
Convert a digest into base-16.
FSL_EXPORT const char * fsl_hash_type_name(fsl_hash_types_e h, const char *zUnknown)
Returns a human-readable name for the given hash type, or its second argument h is not a supported ha...
FSL_EXPORT const fsl_sha1_cx fsl_sha1_cx_empty
fsl_sha1_cx instance intended for copy initialization.
Definition hash.h:293
fsl_hash_constants
Various set-in-stone constants used by the API.
Definition hash.h:29
@ FSL_STRLEN_SHA1
The length, in bytes, of a hex-format SHA1 hash string.
Definition hash.h:33
@ FSL_STRLEN_MD5
The length, in bytes, of a hex-format MD5 hash string.
Definition hash.h:41
@ FSL_UUID_STRLEN_MIN
Minimum length of a full fossil UUID.
Definition hash.h:44
@ FSL_STRLEN_K256
The length, in bytes, of a hex-format SHA3-256 hash string.
Definition hash.h:37
@ FSL_UUID_STRLEN_MAX
Maximum length of a full fossil UUID.
Definition hash.h:46
fsl_hash_types_e
Unique IDs for hash types supported by fossil.
Definition hash.h:52
@ FSL_HTYPE_SHA1
SHA1.
Definition hash.h:56
@ FSL_HTYPE_ERROR
Invalid hash type.
Definition hash.h:54
@ FSL_HTYPE_K256
SHA3-256.
Definition hash.h:58
FSL_EXPORT int fsl_sha1sum_update_stream(fsl_sha1_cx *ctx, fsl_input_f src, void *srcState)
Reads all input from src() and passes it through fsl_sha1_update(cx,...).
FSL_EXPORT int fsl_sha3sum_filename(const char *zFilename, fsl_buffer *pCksum)
SHA3 counterpart of fsl_sha1sum_filename().
FSL_EXPORT int fsl_sha1_final(fsl_sha1_cx *context, unsigned char *digest)
Add padding and finalizes the message digest.
A general-purpose buffer class, analog to Fossil's Blob class, but it is not called fsl_blob to avoid...
Definition util.h:684
Holds state for MD5 calculations.
Definition hash.h:97
unsigned char in[64]
Definition hash.h:101
uint32_t buf[4]
Definition hash.h:99
uint32_t bits[2]
Definition hash.h:100
int isInit
Definition hash.h:98
Holds state for SHA1 calculations.
Definition hash.h:251
unsigned int count[2]
Definition hash.h:270
unsigned char buffer[64]
Definition hash.h:271
unsigned int state[5]
Definition hash.h:269
Type for holding SHA3 processing state.
Definition hash.h:448
unsigned nRate
Definition hash.h:453
unsigned char hex[132]
Definition hash.h:457
unsigned nLoaded
Definition hash.h:454
enum fsl_sha3_hash_size size
Definition hash.h:456
unsigned ixMask
Definition hash.h:455
unsigned char x[1600]
Definition hash.h:451
union fsl_sha3_cx::@110263043236320153224353116264214062327335250213 u
uint64_t s[25]
Definition hash.h:450
This file declares a number of utility classes and routines used by libfossil.
int(* fsl_input_f)(void *state, void *dest, fsl_size_t *n)
Generic interface for streaming in data.
Definition util.h:318