multof moved from descriptor to object
[rofl0r-kripto.git] / lib / stream.c
blobdef91a9a169c91aa923266af0b352b83cbd8ed06
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/stream.h>
20 #include <kripto/stream.h>
22 struct kripto_stream
24 const kripto_stream_desc *desc;
25 unsigned int multof;
28 kripto_stream *kripto_stream_create
30 const kripto_stream_desc *desc,
31 unsigned int rounds,
32 const void *key,
33 unsigned int key_len,
34 const void *iv,
35 unsigned int iv_len
38 assert(desc);
39 assert(desc->create);
41 assert(key);
42 assert(key_len);
43 assert(key_len <= kripto_stream_maxkey(desc));
44 assert(iv_len <= kripto_stream_maxiv(desc));
45 if(iv_len) assert(iv);
47 return desc->create(desc, rounds, key, key_len, iv, iv_len);
50 kripto_stream *kripto_stream_recreate
52 kripto_stream *s,
53 unsigned int rounds,
54 const void *key,
55 unsigned int key_len,
56 const void *iv,
57 unsigned int iv_len
60 assert(s);
61 assert(s->desc);
62 assert(s->desc->recreate);
64 assert(key);
65 assert(key_len);
66 assert(key_len <= kripto_stream_maxkey(s->desc));
67 assert(iv_len <= kripto_stream_maxiv(s->desc));
68 if(iv_len) assert(iv);
70 return s->desc->recreate(s, rounds, key, key_len, iv, iv_len);
73 void kripto_stream_encrypt
75 kripto_stream *s,
76 const void *pt,
77 void *ct,
78 size_t len
81 assert(s);
82 assert(s->desc);
83 assert(s->desc->encrypt);
84 assert(len % kripto_stream_multof(s->desc) == 0);
86 s->desc->encrypt(s, pt, ct, len);
89 void kripto_stream_decrypt
91 kripto_stream *s,
92 const void *ct,
93 void *pt,
94 size_t len
97 assert(s);
98 assert(s->desc);
99 assert(s->desc->decrypt);
100 assert(len % kripto_stream_multof(s->desc) == 0);
102 s->desc->decrypt(s, ct, pt, len);
105 void kripto_stream_prng
107 kripto_stream *s,
108 void *out,
109 size_t len
112 assert(s);
113 assert(s->desc);
114 assert(s->desc->prng);
116 s->desc->prng(s, out, len);
119 void kripto_stream_destroy(kripto_stream *s)
121 assert(s);
122 assert(s->desc);
123 assert(s->desc->destroy);
125 s->desc->destroy(s);
128 unsigned int kripto_stream_multof(const kripto_stream *s)
130 assert(s);
131 assert(s->multof);
133 return s->multof;
136 const kripto_stream_desc *kripto_stream_getdesc(const kripto_stream *s)
138 assert(s);
139 assert(s->desc);
141 return s->desc;
144 unsigned int kripto_stream_maxkey(const kripto_stream_desc *desc)
146 assert(desc);
147 assert(desc->maxkey);
149 return desc->maxkey;
152 unsigned int kripto_stream_maxiv(const kripto_stream_desc *desc)
154 assert(desc);
156 return desc->maxiv;