multof moved from descriptor to object
[rofl0r-kripto.git] / lib / authstream.c
blobb1c5b17d2467da6d55b04a9733b9f66be3c792a6
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 <assert.h>
16 #include <stdint.h>
18 #include <kripto/desc/authstream.h>
20 #include <kripto/authstream.h>
22 struct kripto_authstream
24 const kripto_authstream_desc *desc;
25 unsigned int multof;
28 kripto_authstream *kripto_authstream_create
30 const kripto_authstream_desc *desc,
31 unsigned int rounds,
32 const void *key,
33 unsigned int key_len,
34 const void *iv,
35 unsigned int iv_len,
36 unsigned int tag_len
39 assert(desc);
40 assert(desc->create);
42 assert(key);
43 assert(key_len);
44 assert(key_len <= kripto_authstream_maxkey(desc));
45 assert(iv_len <= kripto_authstream_maxiv(desc));
46 if(iv_len) assert(iv);
47 assert(tag_len <= kripto_authstream_maxtag(desc));
49 return desc->create(desc, rounds, key, key_len, iv, iv_len, tag_len);
52 kripto_authstream *kripto_authstream_recreate
54 kripto_authstream *s,
55 unsigned int rounds,
56 const void *key,
57 unsigned int key_len,
58 const void *iv,
59 unsigned int iv_len,
60 unsigned int tag_len
63 assert(s);
64 assert(s->desc);
65 assert(s->desc->recreate);
67 assert(key);
68 assert(key_len);
69 assert(key_len <= kripto_authstream_maxkey(s->desc));
70 assert(iv_len <= kripto_authstream_maxiv(s->desc));
71 if(iv_len) assert(iv);
72 assert(tag_len <= kripto_authstream_maxtag(s->desc));
74 return s->desc->recreate(s, rounds, key, key_len, iv, iv_len, tag_len);
77 void kripto_authstream_encrypt
79 kripto_authstream *s,
80 const void *pt,
81 void *ct,
82 size_t len
85 assert(s);
86 assert(s->desc);
87 assert(s->desc->encrypt);
88 assert(len % kripto_authstream_multof(s->desc) == 0);
90 s->desc->encrypt(s, pt, ct, len);
93 void kripto_authstream_decrypt
95 kripto_authstream *s,
96 const void *ct,
97 void *pt,
98 size_t len
101 assert(s);
102 assert(s->desc);
103 assert(s->desc->decrypt);
104 assert(len % kripto_authstream_multof(s->desc) == 0);
106 s->desc->decrypt(s, ct, pt, len);
109 void kripto_authstream_header
111 kripto_authstream *s,
112 const void *header,
113 size_t len
116 assert(s);
117 assert(s->desc);
118 assert(s->desc->header);
120 s->desc->header(s, header, len);
123 void kripto_authstream_tag
125 kripto_authstream *s,
126 void *tag,
127 unsigned int len
130 assert(s);
131 assert(s->desc);
132 assert(s->desc->tag);
134 s->desc->tag(s, tag, len);
137 void kripto_authstream_destroy(kripto_authstream *s)
139 assert(s);
140 assert(s->desc);
141 assert(s->desc->destroy);
143 s->desc->destroy(s);
146 unsigned int kripto_authstream_multof(const kripto_authstream *s)
148 assert(s);
149 assert(s->multof);
151 return s->multof;
154 const kripto_authstream_desc *kripto_authstream_getdesc(const kripto_authstream *s)
156 assert(s);
157 assert(s->desc);
159 return s->desc;
162 unsigned int kripto_authstream_maxkey(const kripto_authstream_desc *desc)
164 assert(desc);
165 assert(desc->maxkey);
167 return desc->maxkey;
170 unsigned int kripto_authstream_maxiv(const kripto_authstream_desc *desc)
172 assert(desc);
174 return desc->maxiv;
177 unsigned int kripto_authstream_maxtag(const kripto_authstream_desc *desc)
179 assert(desc);
181 return desc->maxtag;