File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / bird / lib / mac.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Aug 22 12:33:54 2017 UTC (6 years, 11 months ago) by misho
Branches: bird, MAIN
CVS tags: v1_6_8p3, v1_6_3p0, v1_6_3, HEAD
bird 1.6.3

    1: /*
    2:  *	BIRD Library -- Message Authentication Codes
    3:  *
    4:  *	(c) 2016 Ondrej Zajicek <santiago@crfreenet.org>
    5:  *	(c) 2016 CZ.NIC z.s.p.o.
    6:  *
    7:  *	Can be freely distributed and used under the terms of the GNU GPL.
    8:  */
    9: 
   10: #ifndef _BIRD_MAC_H_
   11: #define _BIRD_MAC_H_
   12: 
   13: #include "nest/bird.h"
   14: #include "lib/sha512.h"
   15: 
   16: 
   17: #define ALG_UNDEFINED		0
   18: #define ALG_MD5			0x01
   19: #define ALG_SHA1		0x02
   20: #define ALG_SHA224		0x03
   21: #define ALG_SHA256		0x04
   22: #define ALG_SHA384		0x05
   23: #define ALG_SHA512		0x06
   24: #define ALG_HMAC		0x10
   25: #define ALG_HMAC_MD5		0x11
   26: #define ALG_HMAC_SHA1		0x12
   27: #define ALG_HMAC_SHA224		0x13
   28: #define ALG_HMAC_SHA256		0x14
   29: #define ALG_HMAC_SHA384		0x15
   30: #define ALG_HMAC_SHA512		0x16
   31: #define ALG_MAX			0x17
   32: 
   33: /* These are maximums for HASH/MAC lengths and required context space */
   34: #define MAX_HASH_SIZE		SHA512_SIZE
   35: #define HASH_STORAGE		sizeof(struct sha512_context)
   36: #define MAC_STORAGE		sizeof(struct hmac_context)
   37: 
   38: /* This value is used by several IETF protocols for padding */
   39: #define HMAC_MAGIC		htonl(0x878FE1F3)
   40: 
   41: /* Generic context used by hash functions */
   42: struct hash_context
   43: {
   44:   u8 data[HASH_STORAGE];
   45:   u64 align[0];
   46: };
   47: 
   48: /* Context for embedded hash (not-really-MAC hash) */
   49: struct nrmh_context {
   50:   const struct mac_desc *type;
   51:   struct hash_context ictx;
   52: };
   53: 
   54: /* Context for hash based HMAC */
   55: struct hmac_context {
   56:   const struct mac_desc *type;
   57:   struct hash_context ictx;
   58:   struct hash_context octx;
   59: };
   60: 
   61: /* Generic context used by MAC functions */
   62: struct mac_context
   63: {
   64:   const struct mac_desc *type;
   65:   u8 data[MAC_STORAGE - sizeof(void *)];
   66:   u64 align[0];
   67: };
   68: 
   69: /* Union to satisfy C aliasing rules */
   70: union mac_context_union {
   71:   struct mac_context mac;
   72:   struct nrmh_context nrmh;
   73:   struct hmac_context hmac;
   74: };
   75: 
   76: 
   77: struct mac_desc {
   78:   const char *name;			/* Name of MAC algorithm */
   79:   uint mac_length;			/* Length of authentication code */
   80:   uint ctx_length;			/* Length of algorithm context */
   81:   void (*init)(struct mac_context *ctx, const byte *key, uint keylen);
   82:   void (*update)(struct mac_context *ctx, const byte *data, uint datalen);
   83:   byte *(*final)(struct mac_context *ctx);
   84: 
   85:   uint hash_size;			/* Hash length, for hash-based MACs */
   86:   uint block_size;			/* Hash block size, for hash-based MACs */
   87:   void (*hash_init)(struct hash_context *ctx);
   88:   void (*hash_update)(struct hash_context *ctx, const byte *data, uint datalen);
   89:   byte *(*hash_final)(struct hash_context *ctx);
   90: };
   91: 
   92: extern const struct mac_desc mac_table[ALG_MAX];
   93: 
   94: static inline const char *mac_type_name(uint id)
   95: { return mac_table[id].name; }
   96: 
   97: static inline uint mac_type_length(uint id)
   98: { return mac_table[id].mac_length; }
   99: 
  100: static inline const char *mac_get_name(struct mac_context *ctx)
  101: { return ctx->type->name; }
  102: 
  103: static inline uint mac_get_length(struct mac_context *ctx)
  104: { return ctx->type->mac_length; }
  105: 
  106: void mac_init(struct mac_context *ctx, uint id, const byte *key, uint keylen);
  107: 
  108: static inline void mac_update(struct mac_context *ctx, const byte *data, uint datalen)
  109: { ctx->type->update(ctx, data, datalen); }
  110: 
  111: static inline byte *mac_final(struct mac_context *ctx)
  112: { return ctx->type->final(ctx); }
  113: 
  114: static inline void mac_cleanup(struct mac_context *ctx)
  115: { memset(ctx, 0, ctx->type->ctx_length); }
  116: 
  117: void mac_fill(uint id, const byte *key, uint keylen, const byte *data, uint datalen, byte *mac);
  118: int mac_verify(uint id, const byte *key, uint keylen, const byte *data, uint datalen, const byte *mac);
  119: 
  120: 
  121: #endif /* _BIRD_MAC_H_ */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>