better standard compliance
[rofl0r-kripto.git] / lib / mac / keccak.c
blob6dd0c881f4757e3fa48d0797ed9ce6bf3ce62147
1 /*
2 * Copyright (C) 2013 Gregor Pintar <grpintar@gmail.com>
4 * Permission is granted to deal in this work without any restriction,
5 * including unlimited rights to use, publicly perform, publish,
6 * reproduce, relicence, modify, merge, and/or distribute in any form,
7 * for any purpose, with or without fee, and by any means.
9 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind,
10 * to the utmost extent permitted by applicable law. In no event
11 * shall a licensor, author or contributor be held liable for any
12 * issues arising in any way out of dealing in the work.
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <limits.h>
19 #include <kripto/cast.h>
20 #include <kripto/memwipe.h>
21 #include <kripto/hash.h>
22 #include <kripto/hash/keccak1600.h>
23 #include <kripto/hash/keccak800.h>
24 #include <kripto/mac.h>
25 #include <kripto/desc/mac.h>
26 #include <kripto/object/mac.h>
28 #include <kripto/mac/keccak1600.h>
29 #include <kripto/mac/keccak800.h>
31 struct kripto_mac
33 struct kripto_mac_object obj;
34 kripto_hash *hash;
37 static void keccak_input
39 kripto_mac *s,
40 const void *in,
41 size_t len
44 kripto_hash_input(s->hash, in, len);
47 static void keccak_tag(kripto_mac *s, void *tag, unsigned int len)
49 kripto_hash_output(s->hash, tag, len);
52 static kripto_mac *keccak_recreate
54 kripto_mac *s,
55 unsigned int r,
56 const void *key,
57 unsigned int key_len,
58 unsigned int tag_len
61 (void)kripto_hash_recreate(s->hash, r, tag_len);
63 kripto_hash_input(s->hash, key, key_len);
65 return s;
68 static kripto_mac *keccak1600_create
70 const kripto_mac_desc *desc,
71 unsigned int r,
72 const void *key,
73 unsigned int key_len,
74 unsigned int tag_len
77 kripto_mac *s;
79 (void)desc;
81 s = malloc(sizeof(kripto_mac));
82 if(!s) return 0;
84 s->obj.desc = kripto_mac_keccak1600;
86 s->hash = kripto_hash_create(kripto_hash_keccak1600, r, tag_len);
87 if(!s->hash)
89 free(s);
90 return 0;
93 kripto_hash_input(s->hash, key, key_len);
95 return s;
98 static kripto_mac *keccak800_create
100 const kripto_mac_desc *desc,
101 unsigned int r,
102 const void *key,
103 unsigned int key_len,
104 unsigned int tag_len
107 kripto_mac *s;
109 (void)desc;
111 s = malloc(sizeof(kripto_mac));
112 if(!s) return 0;
114 s->obj.desc = kripto_mac_keccak800;
116 s->hash = kripto_hash_create(kripto_hash_keccak800, r, tag_len);
117 if(!s->hash)
119 free(s);
120 return 0;
123 kripto_hash_input(s->hash, key, key_len);
125 return s;
128 static void keccak_destroy(kripto_mac *s)
130 kripto_hash_destroy(s->hash);
131 free(s);
134 static const kripto_mac_desc keccak1600 =
136 &keccak1600_create,
137 &keccak_recreate,
138 &keccak_input,
139 &keccak_tag,
140 &keccak_destroy,
141 99 /* max tag */
144 const kripto_mac_desc *const kripto_mac_keccak1600 = &keccak1600;
146 static const kripto_mac_desc keccak800 =
148 &keccak800_create,
149 &keccak_recreate,
150 &keccak_input,
151 &keccak_tag,
152 &keccak_destroy,
153 49 /* max tag */
156 const kripto_mac_desc *const kripto_mac_keccak800 = &keccak800;