Statistics
| Branch: | Revision:

iof-bird-daemon / lib / sha1.h @ 5e173e9f

History | View | Annotate | Download (2.79 KB)

1 5d0c36f1 Pavel Tvrdík
/*
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
20 5126380b Ondrej Zajicek (work)
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
26 5d0c36f1 Pavel Tvrdík
/*
27
 * Internal SHA1 state.
28
 * You should use it just as an opaque handle only.
29
 */
30
struct sha1_context {
31 5126380b Ondrej Zajicek (work)
  u32 h0, h1, h2, h3, h4;
32
  byte buf[SHA1_BLOCK_SIZE];
33
  uint nblocks;
34
  uint count;
35
};
36
37 5d0c36f1 Pavel Tvrdík
38 5126380b Ondrej Zajicek (work)
void sha1_init(struct sha1_context *ctx); /* Initialize new algorithm run in the @ctx context. **/
39 5d0c36f1 Pavel Tvrdík
/*
40 5126380b Ondrej Zajicek (work)
 * 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.
44 5d0c36f1 Pavel Tvrdík
 */
45 5126380b Ondrej Zajicek (work)
void sha1_update(struct sha1_context *ctx, const byte *buf, uint len);
46 5d0c36f1 Pavel Tvrdík
/*
47 5126380b Ondrej Zajicek (work)
 * No more @sha1_update() calls will be done. This terminates the hash and
48
 * returns a pointer to it.
49 5d0c36f1 Pavel Tvrdík
 *
50 5126380b Ondrej Zajicek (work)
 * Note that the pointer points into data in the @ctx context. If it ceases to
51
 * exist, the pointer becomes invalid.
52 5d0c36f1 Pavel Tvrdík
 */
53 5126380b Ondrej Zajicek (work)
byte *sha1_final(struct sha1_context *ctx);
54 5d0c36f1 Pavel Tvrdík
55
/*
56 5126380b Ondrej Zajicek (work)
 * A convenience one-shot function for SHA1 hash. It is equivalent to this
57
 * snippet of code:
58 5d0c36f1 Pavel Tvrdík
 *
59 5126380b Ondrej Zajicek (work)
 *  sha1_context ctx;
60
 *  sha1_init(&ctx);
61
 *  sha1_update(&ctx, buffer, length);
62
 *  memcpy(outbuf, sha1_final(&ctx), SHA1_SIZE);
63 5d0c36f1 Pavel Tvrdík
 */
64
void sha1_hash_buffer(byte *outbuf, const byte *buffer, uint length);
65
66
/*
67 5126380b Ondrej Zajicek (work)
 * SHA1 HMAC message authentication. If you provide @key and @data, the result
68
 * will be stored in @outbuf.
69 5d0c36f1 Pavel Tvrdík
 */
70
void sha1_hmac(byte *outbuf, const byte *key, uint keylen, const byte *data, uint datalen);
71
72
/*
73 5126380b Ondrej Zajicek (work)
 * The HMAC also exists in a stream version in a way analogous to the plain
74
 * SHA1. Pass this as a context.
75 5d0c36f1 Pavel Tvrdík
 */
76
struct sha1_hmac_context {
77
  struct sha1_context ictx;
78
  struct sha1_context octx;
79
};
80
81 5126380b Ondrej Zajicek (work)
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(). */
84 5d0c36f1 Pavel Tvrdík
85
86
#endif /* _BIRD_SHA1_H_ */