Debian Codegraph @

libs/pplib/pplib-src/src/util/utilsha.h

raw
1 /* sha2 implementation excerpted from code by Aaron D. Gifford */
2
3 #ifndef UTIL_SHA_H
4 #define UTIL_SHA_H
5
6 #include <stddef.h>
7 #include <stdint.h>
8 #include "utildecl.h"
9
10 #define SHA256_BLOCK_LENGTH 64
11 #define SHA256_DIGEST_LENGTH 32
12 #define SHA256_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
13 #define SHA384_BLOCK_LENGTH 128
14 #define SHA384_DIGEST_LENGTH 48
15 #define SHA384_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
16 #define SHA512_BLOCK_LENGTH 128
17 #define SHA512_DIGEST_LENGTH 64
18 #define SHA512_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
19
20 //#define sha256_state sha256_state_t
21 //#define sha384_state sha384_state_t
22 //#define sha512_state sha512_state_t
23
24 typedef struct {
25 uint32_t words[8];
26 uint64_t bitcount;
27 union {
28 uint8_t buffer[SHA256_BLOCK_LENGTH];
29 uint32_t buffer32[SHA256_BLOCK_LENGTH / sizeof(uint32_t)];
30 uint64_t buffer64[SHA256_BLOCK_LENGTH / sizeof(uint64_t)];
31 };
32 } sha256_state;
33
34 typedef struct {
35 uint64_t words[8];
36 uint64_t bitcount[2];
37 union {
38 uint8_t buffer[SHA512_BLOCK_LENGTH];
39 uint64_t buffer64[SHA512_BLOCK_LENGTH / sizeof(uint64_t)];
40 };
41 } sha512_state;
42
43 typedef sha512_state sha384_state;
44
45 enum {
46 SHA_BYTES = 0,
47 SHA_UCHEX = (1<<0),
48 SHA_LCHEX = (1<<1)
49 };
50
51 #define SHA_DEFAULT SHA_BYTES
52 #define SHA_HEX (SHA_UCHEX|SHA_LCHEX)
53
54 UTILAPI sha256_state * sha256_digest_init (sha256_state *state);
55 UTILAPI sha384_state * sha384_digest_init (sha384_state *state);
56 UTILAPI sha512_state * sha512_digest_init (sha512_state *state);
57
58 UTILAPI void sha256_digest_add (sha256_state *state, const void *data, size_t size);
59 UTILAPI void sha384_digest_add (sha384_state *state, const void *data, size_t size);
60 UTILAPI void sha512_digest_add (sha512_state *state, const void *data, size_t size);
61
62 UTILAPI void sha256_digest_get (sha256_state *state, uint8_t digest[], int flags);
63 UTILAPI void sha384_digest_get (sha384_state *state, uint8_t digest[], int flags);
64 UTILAPI void sha512_digest_get (sha512_state *state, uint8_t digest[], int flags);
65
66 UTILAPI void sha256_digest (const void *data, size_t size, uint8_t digest[], int flags);
67 UTILAPI void sha384_digest (const void *data, size_t size, uint8_t digest[], int flags);
68 UTILAPI void sha512_digest (const void *data, size_t size, uint8_t digest[], int flags);
69
70 UTILAPI int sha256_digest_add_file (sha256_state *state, const char *filename);
71 UTILAPI int sha256_digest_file (const char *filename, uint8_t digest[], int flags);
72
73 UTILAPI int sha384_digest_add_file (sha384_state *state, const char *filename);
74 UTILAPI int sha384_digest_file (const char *filename, uint8_t digest[], int flags);
75
76 UTILAPI int sha512_digest_add_file (sha512_state *state, const char *filename);
77 UTILAPI int sha512_digest_file (const char *filename, uint8_t digest[], int flags);
78
79 #endif