status update, probably last commit
[rofl0r-kripto.git] / lib / stream.c
blob642a089d7acb1c8a631127300751a0f0fb8995c2
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 <assert.h>
15 #include <stdint.h>
17 #include <kripto/stream.h>
18 #include <kripto/desc/stream.h>
20 struct kripto_stream
22 const kripto_stream_desc *desc;
23 unsigned int multof;
26 kripto_stream *kripto_stream_create
28 const kripto_stream_desc *desc,
29 unsigned int rounds,
30 const void *key,
31 unsigned int key_len,
32 const void *iv,
33 unsigned int iv_len
36 assert(desc);
37 assert(desc->create);
39 assert(key);
40 assert(key_len);
41 assert(key_len <= kripto_stream_maxkey(desc));
42 assert(iv_len <= kripto_stream_maxiv(desc));
43 if(iv_len) assert(iv);
45 return desc->create(desc, rounds, key, key_len, iv, iv_len);
48 kripto_stream *kripto_stream_recreate
50 kripto_stream *s,
51 unsigned int rounds,
52 const void *key,
53 unsigned int key_len,
54 const void *iv,
55 unsigned int iv_len
58 assert(s);
59 assert(s->desc);
60 assert(s->desc->recreate);
62 assert(key);
63 assert(key_len);
64 assert(key_len <= kripto_stream_maxkey(s->desc));
65 assert(iv_len <= kripto_stream_maxiv(s->desc));
66 if(iv_len) assert(iv);
68 return s->desc->recreate(s, rounds, key, key_len, iv, iv_len);
71 void kripto_stream_encrypt
73 kripto_stream *s,
74 const void *pt,
75 void *ct,
76 size_t len
79 assert(s);
80 assert(s->desc);
81 assert(s->desc->encrypt);
82 assert(len % kripto_stream_multof(s) == 0);
84 s->desc->encrypt(s, pt, ct, len);
87 void kripto_stream_decrypt
89 kripto_stream *s,
90 const void *ct,
91 void *pt,
92 size_t len
95 assert(s);
96 assert(s->desc);
97 assert(s->desc->decrypt);
98 assert(len % kripto_stream_multof(s) == 0);
100 s->desc->decrypt(s, ct, pt, len);
103 void kripto_stream_prng
105 kripto_stream *s,
106 void *out,
107 size_t len
110 assert(s);
111 assert(s->desc);
112 assert(s->desc->prng);
114 s->desc->prng(s, out, len);
117 void kripto_stream_destroy(kripto_stream *s)
119 assert(s);
120 assert(s->desc);
121 assert(s->desc->destroy);
123 s->desc->destroy(s);
126 unsigned int kripto_stream_multof(const kripto_stream *s)
128 assert(s);
129 assert(s->multof);
131 return s->multof;
134 const kripto_stream_desc *kripto_stream_getdesc(const kripto_stream *s)
136 assert(s);
137 assert(s->desc);
139 return s->desc;
142 unsigned int kripto_stream_maxkey(const kripto_stream_desc *desc)
144 assert(desc);
145 assert(desc->maxkey);
147 return desc->maxkey;
150 unsigned int kripto_stream_maxiv(const kripto_stream_desc *desc)
152 assert(desc);
154 return desc->maxiv;