status update, probably last commit
[rofl0r-kripto.git] / lib / mac / keccak.c
blob1a448c2683d9ece1c14f9fb4e858dcaf95c3a473
1 /*
2 * Written in 2013 by Gregor Pintar <grpintar@gmail.com>
4 * To the extent possible under law, the author(s) have dedicated
5 * all copyright and related and neighboring rights to this software
6 * to the public domain worldwide.
7 *
8 * This software is distributed without any warranty.
10 * You should have received a copy of the CC0 Public Domain Dedication.
11 * If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <limits.h>
18 #include <kripto/cast.h>
19 #include <kripto/memwipe.h>
20 #include <kripto/hash.h>
21 #include <kripto/hash/keccak1600.h>
22 #include <kripto/hash/keccak800.h>
23 #include <kripto/mac.h>
24 #include <kripto/desc/mac.h>
25 #include <kripto/object/mac.h>
27 #include <kripto/mac/keccak1600.h>
28 #include <kripto/mac/keccak800.h>
30 struct kripto_mac
32 struct kripto_mac_object obj;
33 kripto_hash *hash;
36 static void keccak_input
38 kripto_mac *s,
39 const void *in,
40 size_t len
43 kripto_hash_input(s->hash, in, len);
46 static void keccak_tag(kripto_mac *s, void *tag, unsigned int len)
48 kripto_hash_output(s->hash, tag, len);
51 static kripto_mac *keccak_recreate
53 kripto_mac *s,
54 unsigned int r,
55 const void *key,
56 unsigned int key_len,
57 unsigned int tag_len
60 (void)kripto_hash_recreate(s->hash, r, tag_len);
62 kripto_hash_input(s->hash, key, key_len);
64 return s;
67 static kripto_mac *keccak1600_create
69 const kripto_mac_desc *desc,
70 unsigned int r,
71 const void *key,
72 unsigned int key_len,
73 unsigned int tag_len
76 kripto_mac *s;
78 (void)desc;
80 s = malloc(sizeof(kripto_mac));
81 if(!s) return 0;
83 s->obj.desc = kripto_mac_keccak1600;
85 s->hash = kripto_hash_create(kripto_hash_keccak1600, r, tag_len);
86 if(!s->hash)
88 free(s);
89 return 0;
92 kripto_hash_input(s->hash, key, key_len);
94 return s;
97 static kripto_mac *keccak800_create
99 const kripto_mac_desc *desc,
100 unsigned int r,
101 const void *key,
102 unsigned int key_len,
103 unsigned int tag_len
106 kripto_mac *s;
108 (void)desc;
110 s = malloc(sizeof(kripto_mac));
111 if(!s) return 0;
113 s->obj.desc = kripto_mac_keccak800;
115 s->hash = kripto_hash_create(kripto_hash_keccak800, r, tag_len);
116 if(!s->hash)
118 free(s);
119 return 0;
122 kripto_hash_input(s->hash, key, key_len);
124 return s;
127 static void keccak_destroy(kripto_mac *s)
129 kripto_hash_destroy(s->hash);
130 free(s);
133 static const kripto_mac_desc keccak1600 =
135 &keccak1600_create,
136 &keccak_recreate,
137 &keccak_input,
138 &keccak_tag,
139 &keccak_destroy,
140 99, /* max tag */
141 UINT_MAX /* max key */
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 */
154 UINT_MAX /* max key */
157 const kripto_mac_desc *const kripto_mac_keccak800 = &keccak800;