]> git.ozlabs.org Git - ccan/blob - ccan/crypto/ripemd160/ripemd160.h
e29e42bb044b3ac63134205731dd3c9c51e5bab8
[ccan] / ccan / crypto / ripemd160 / ripemd160.h
1 #ifndef CCAN_CRYPTO_RIPEMD160_H
2 #define CCAN_CRYPTO_RIPEMD160_H
3 /* BSD-MIT - see LICENSE file for details */
4 #include "config.h"
5 #include <stdint.h>
6 #include <stdlib.h>
7
8 /* Uncomment this to use openssl's RIPEMD160 routines (and link with -lcrypto) */
9 //#define CCAN_CRYPTO_RIPEMD160_USE_OPENSSL 1
10
11 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
12 #include <openssl/ripemd.h>
13 #endif
14
15 /**
16  * struct ripemd160 - structure representing a completed RIPEMD160.
17  * @u.u8: an unsigned char array.
18  * @u.u32: a 32-bit integer array.
19  *
20  * Other fields may be added to the union in future.
21  */
22 struct ripemd160 {
23         union {
24                 /* Array of chars */
25                 unsigned char u8[20];
26                 /* Array of uint32_t */
27                 uint32_t u32[5];
28         } u;
29 };
30
31 /**
32  * ripemd160 - return ripemd160 of an object.
33  * @ripemd160: the ripemd160 to fill in
34  * @p: pointer to memory,
35  * @size: the number of bytes pointed to by @p
36  *
37  * The bytes pointed to by @p is RIPEMD160 hashed into @ripemd160.  This is
38  * equivalent to ripemd160_init(), ripemd160_update() then ripemd160_done().
39  */
40 void ripemd160(struct ripemd160 *sha, const void *p, size_t size);
41
42 /**
43  * struct ripemd160_ctx - structure to store running context for ripemd160
44  */
45 struct ripemd160_ctx {
46 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
47         RIPEMD160_CTX c;
48 #else
49         uint32_t s[5];
50         uint64_t bytes;
51         union {
52                 uint32_t u32[8];
53                 unsigned char u8[64];
54         } buf;
55 #endif
56 };
57
58 /**
59  * ripemd160_init - initialize an RIPEMD160 context.
60  * @ctx: the ripemd160_ctx to initialize
61  *
62  * This must be called before ripemd160_update or ripemd160_done, or
63  * alternately you can assign RIPEMD160_INIT.
64  *
65  * If it was already initialized, this forgets anything which was
66  * hashed before.
67  *
68  * Example:
69  * static void hash_all(const char **arr, struct ripemd160 *hash)
70  * {
71  *      size_t i;
72  *      struct ripemd160_ctx ctx;
73  *
74  *      ripemd160_init(&ctx);
75  *      for (i = 0; arr[i]; i++)
76  *              ripemd160_update(&ctx, arr[i], strlen(arr[i]));
77  *      ripemd160_done(&ctx, hash);
78  * }
79  */
80 void ripemd160_init(struct ripemd160_ctx *ctx);
81
82 /**
83  * RIPEMD160_INIT - initializer for an RIPEMD160 context.
84  *
85  * This can be used to staticly initialize an RIPEMD160 context (instead
86  * of ripemd160_init()).
87  *
88  * Example:
89  * static void hash_all(const char **arr, struct ripemd160 *hash)
90  * {
91  *      size_t i;
92  *      struct ripemd160_ctx ctx = RIPEMD160_INIT;
93  *
94  *      for (i = 0; arr[i]; i++)
95  *              ripemd160_update(&ctx, arr[i], strlen(arr[i]));
96  *      ripemd160_done(&ctx, hash);
97  * }
98  */
99 #ifdef CCAN_CRYPTO_RIPEMD160_USE_OPENSSL
100 #define RIPEMD160_INIT                                                  \
101         { { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul,     \
102             0xC3D2E1F0ul,                                               \
103             0x0, 0x0,                                                   \
104             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },         \
105             0 } }
106 #else
107 #define RIPEMD160_INIT                                                  \
108         { { 0x67452301ul, 0xEFCDAB89ul, 0x98BADCFEul, 0x10325476ul,     \
109             0xC3D2E1F0ul }, 0 }
110 #endif
111
112 /**
113  * ripemd160_update - include some memory in the hash.
114  * @ctx: the ripemd160_ctx to use
115  * @p: pointer to memory,
116  * @size: the number of bytes pointed to by @p
117  *
118  * You can call this multiple times to hash more data, before calling
119  * ripemd160_done().
120  */
121 void ripemd160_update(struct ripemd160_ctx *ctx, const void *p, size_t size);
122
123 /**
124  * ripemd160_done - finish RIPEMD160 and return the hash
125  * @ctx: the ripemd160_ctx to complete
126  * @res: the hash to return.
127  *
128  * Note that @ctx is *destroyed* by this, and must be reinitialized.
129  * To avoid that, pass a copy instead.
130  */
131 void ripemd160_done(struct ripemd160_ctx *ripemd160, struct ripemd160 *res);
132
133 /* Add various types to an RIPEMD160 hash */
134 void ripemd160_u8(struct ripemd160_ctx *ctx, uint8_t v);
135 void ripemd160_u16(struct ripemd160_ctx *ctx, uint16_t v);
136 void ripemd160_u32(struct ripemd160_ctx *ctx, uint32_t v);
137 void ripemd160_u64(struct ripemd160_ctx *ctx, uint64_t v);
138
139 /* Add as little-endian */
140 void ripemd160_le16(struct ripemd160_ctx *ctx, uint16_t v);
141 void ripemd160_le32(struct ripemd160_ctx *ctx, uint32_t v);
142 void ripemd160_le64(struct ripemd160_ctx *ctx, uint64_t v);
143
144 /* Add as big-endian */
145 void ripemd160_be16(struct ripemd160_ctx *ctx, uint16_t v);
146 void ripemd160_be32(struct ripemd160_ctx *ctx, uint32_t v);
147 void ripemd160_be64(struct ripemd160_ctx *ctx, uint64_t v);
148 #endif /* CCAN_CRYPTO_RIPEMD160_H */