Statistics
| Branch: | Revision:

iof-bird-daemon / lib / sha256.h @ 4035e0e7

History | View | Annotate | Download (2.13 KB)

1
/*
2
 *        BIRD Library -- SHA-256 and SHA-224 Hash Functions,
3
 *                        HMAC-SHA-256 and HMAC-SHA-224 Functions
4
 *
5
 *        (c) 2015 CZ.NIC z.s.p.o.
6
 *
7
 *        Based on the code from libgcrypt-1.6.0, which is
8
 *        (c) 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
9
 *
10
 *        Can be freely distributed and used under the terms of the GNU GPL.
11
 */
12

    
13
#ifndef _BIRD_SHA256_H_
14
#define _BIRD_SHA256_H_
15

    
16
#include "nest/bird.h"
17

    
18
#define SHA224_SIZE                 28
19
#define SHA224_HEX_SIZE                57
20
#define SHA224_BLOCK_SIZE         64
21

    
22
#define SHA256_SIZE                 32
23
#define SHA256_HEX_SIZE                65
24
#define SHA256_BLOCK_SIZE         64
25

    
26
struct sha256_context {
27
  u32  h0,h1,h2,h3,h4,h5,h6,h7;
28
  byte buf[128];                        /* 128 is for SHA384 and SHA512 support, otherwise for SHA224 and SHA256 is 64 enough */
29
  u32 nblocks;
30
  u32 nblocks_high;
31
  int count;
32
  u32 blocksize;
33
  uint (*transform)(void *c, const byte *blks, size_t nblks);
34
};
35
#define sha224_context sha256_context        /* aliasing 'struct sha224_context' to 'struct sha256_context' */
36

    
37
void sha256_init(struct sha256_context *ctx);
38
void sha224_init(struct sha224_context *ctx);
39

    
40
void sha256_update(struct sha256_context *ctx, const byte *in_buf, size_t in_len);
41
static inline void sha224_update(struct sha224_context *ctx, const byte *in_buf, size_t in_len)
42
{
43
  sha256_update(ctx, in_buf, in_len);
44
}
45

    
46
byte* sha256_final(struct sha256_context *ctx);
47
static inline byte* sha224_final(struct sha224_context *ctx)
48
{
49
  return sha256_final(ctx);
50
}
51

    
52
/*
53
 *        HMAC-SHA256, HMAC-SHA224
54
 */
55
struct sha256_hmac_context
56
{
57
  struct sha256_context ictx;
58
  struct sha256_context octx;
59
};
60
#define sha224_hmac_context sha256_hmac_context        /* aliasing 'struct sha224_hmac_context' to 'struct sha256_hmac_context' */
61

    
62
void sha256_hmac_init(struct sha256_hmac_context *ctx, const byte *key, size_t keylen);
63
void sha224_hmac_init(struct sha224_hmac_context *ctx, const byte *key,  size_t keylen);
64

    
65
void sha256_hmac_update(struct sha256_hmac_context *ctx, const byte *buf, size_t buflen);
66
void sha224_hmac_update(struct sha224_hmac_context *ctx, const byte *buf, size_t buflen);
67

    
68
byte *sha256_hmac_final(struct sha256_hmac_context *ctx);
69
byte *sha224_hmac_final(struct sha224_hmac_context *ctx);
70

    
71
#endif /* _BIRD_SHA256_H_ */