summaryrefslogtreecommitdiff
path: root/src/rexmpp_digest.h
blob: 5001e1262a09dec182d056cd574fd5674bb03463 (plain)
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
/**
   @file rexmpp_digest.h
   @brief Cryptographic functions
   @author defanor <defanor@uberspace.net>
   @date 2023
   @copyright MIT license.
*/

#ifndef REXMPP_DIGEST_H
#define REXMPP_DIGEST_H

typedef enum {
  REXMPP_DIGEST_SHA1,
  REXMPP_DIGEST_SHA256,
  REXMPP_DIGEST_SHA3_256
} rexmpp_digest_algorithm;


#if defined(HAVE_GCRYPT)
#include <gcrypt.h>
typedef gcry_md_hd_t rexmpp_digest_t;
#elif defined(HAVE_NETTLE)
#include <nettle/nettle-meta.h>
struct rexmpp_digest {
  const struct nettle_hash *nh;
  void *nh_ctx;
};
typedef struct rexmpp_digest rexmpp_digest_t;
#elif defined(HAVE_OPENSSL)
#include <openssl/evp.h>
typedef EVP_MD_CTX* rexmpp_digest_t;
#endif

/**
   @brief Finds the digest length for a given algorithm.
   @param[in] algo An algorithm.
   @returns Digest length in bytes.
*/
size_t rexmpp_digest_len (rexmpp_digest_algorithm algo);

/**
   @brief Computes a digest for a buffer.
   @param[in] algo An algorithm.
   @param[in] in Input data.
   @param[in] in_len Input data length.
   @param[out] out Output buffer.
   @param[in] out_len Output buffer length.
   @returns 0 on success, non-zero on failure.
*/
int rexmpp_digest_buffer (rexmpp_digest_algorithm algo,
                          const void *in,
                          size_t in_len,
                          void *out,
                          size_t out_len);

/**
   @brief Initializes a digest context.
   @param[out] ctx Pointer to an allocated ::rexmpp_digest_t context
   to initialize.
   @param[in] algo An algorithm to use.
   @returns 0 on success, non-zero on failure.
*/
int rexmpp_digest_init (rexmpp_digest_t *ctx, rexmpp_digest_algorithm algo);

/**
   @brief Updates a digest computation.
   @param[in,out] ctx Context pointer.
   @param[in] in Input data.
   @param[in] len Length of the input buffer.
   @returns 0 on success, non-zero on failure.
*/
int rexmpp_digest_update (rexmpp_digest_t *ctx, const void *in, size_t len);

/**
   @brief Finishes a digest computation, freeing the context and
   providing the output.
   @param[in,out] ctx Context pointer.
   @param[out] out A place to write the computed digest into, can be
   NULL to just free the context.
   @param[in] len Length of the allocated output buffer.
   @returns 0 on success, non-zero on failure.
*/
int rexmpp_digest_finish (rexmpp_digest_t *ctx, void *out, size_t len);

#endif