]>
Commit | Line | Data |
---|---|---|
5d0c36f1 PT |
1 | /* |
2 | * BIRD Library -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1 | |
3 | * | |
4 | * (c) 2015 CZ.NIC z.s.p.o. | |
5 | * | |
6 | * Based on the code from libucw-6.4 | |
7 | * (c) 2008--2009 Martin Mares <mj@ucw.cz> | |
8 | * | |
9 | * Based on the code from libgcrypt-1.2.3, which is | |
10 | * (c) 1998, 2001, 2002, 2003 Free Software Foundation, Inc. | |
11 | * | |
12 | * Can be freely distributed and used under the terms of the GNU GPL. | |
13 | */ | |
14 | ||
15 | #ifndef _BIRD_SHA1_H_ | |
16 | #define _BIRD_SHA1_H_ | |
17 | ||
18 | #include "nest/bird.h" | |
19 | ||
5126380b OZ |
20 | |
21 | #define SHA1_SIZE 20 /* Size of the SHA1 hash in its binary representation */ | |
22 | #define SHA1_HEX_SIZE 41 /* Buffer length for a string containing SHA1 in hexadecimal format. */ | |
23 | #define SHA1_BLOCK_SIZE 64 /* SHA1 splits input to blocks of this size. */ | |
24 | ||
25 | ||
5d0c36f1 PT |
26 | /* |
27 | * Internal SHA1 state. | |
28 | * You should use it just as an opaque handle only. | |
29 | */ | |
30 | struct sha1_context { | |
5126380b OZ |
31 | u32 h0, h1, h2, h3, h4; |
32 | byte buf[SHA1_BLOCK_SIZE]; | |
33 | uint nblocks; | |
34 | uint count; | |
35 | }; | |
36 | ||
5d0c36f1 | 37 | |
5126380b | 38 | void sha1_init(struct sha1_context *ctx); /* Initialize new algorithm run in the @ctx context. **/ |
5d0c36f1 | 39 | /* |
5126380b OZ |
40 | * Push another @len bytes of data pointed to by @buf onto the SHA1 hash |
41 | * currently in @ctx. You can call this any times you want on the same hash (and | |
42 | * you do not need to reinitialize it by @sha1_init()). It has the same effect | |
43 | * as concatenating all the data together and passing them at once. | |
5d0c36f1 | 44 | */ |
5126380b | 45 | void sha1_update(struct sha1_context *ctx, const byte *buf, uint len); |
5d0c36f1 | 46 | /* |
5126380b OZ |
47 | * No more @sha1_update() calls will be done. This terminates the hash and |
48 | * returns a pointer to it. | |
5d0c36f1 | 49 | * |
5126380b OZ |
50 | * Note that the pointer points into data in the @ctx context. If it ceases to |
51 | * exist, the pointer becomes invalid. | |
5d0c36f1 | 52 | */ |
5126380b | 53 | byte *sha1_final(struct sha1_context *ctx); |
5d0c36f1 PT |
54 | |
55 | /* | |
5126380b OZ |
56 | * A convenience one-shot function for SHA1 hash. It is equivalent to this |
57 | * snippet of code: | |
5d0c36f1 | 58 | * |
5126380b OZ |
59 | * sha1_context ctx; |
60 | * sha1_init(&ctx); | |
61 | * sha1_update(&ctx, buffer, length); | |
62 | * memcpy(outbuf, sha1_final(&ctx), SHA1_SIZE); | |
5d0c36f1 PT |
63 | */ |
64 | void sha1_hash_buffer(byte *outbuf, const byte *buffer, uint length); | |
65 | ||
66 | /* | |
5126380b OZ |
67 | * SHA1 HMAC message authentication. If you provide @key and @data, the result |
68 | * will be stored in @outbuf. | |
5d0c36f1 PT |
69 | */ |
70 | void sha1_hmac(byte *outbuf, const byte *key, uint keylen, const byte *data, uint datalen); | |
71 | ||
72 | /* | |
5126380b OZ |
73 | * The HMAC also exists in a stream version in a way analogous to the plain |
74 | * SHA1. Pass this as a context. | |
5d0c36f1 PT |
75 | */ |
76 | struct sha1_hmac_context { | |
77 | struct sha1_context ictx; | |
78 | struct sha1_context octx; | |
79 | }; | |
80 | ||
5126380b OZ |
81 | void sha1_hmac_init(struct sha1_hmac_context *ctx, const byte *key, uint keylen); /* Initialize HMAC with context @ctx and the given key. See sha1_init(). */ |
82 | void sha1_hmac_update(struct sha1_hmac_context *ctx, const byte *data, uint datalen); /* Hash another @datalen bytes of data. See sha1_update(). */ | |
83 | byte *sha1_hmac_final(struct sha1_hmac_context *ctx); /* Terminate the HMAC and return a pointer to the allocated hash. See sha1_final(). */ | |
5d0c36f1 | 84 | |
5d0c36f1 PT |
85 | |
86 | #endif /* _BIRD_SHA1_H_ */ |