2 * Copyright (C) 2011, 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.
19 #include <kripto/cast.h>
20 #include <kripto/memwipe.h>
21 #include <kripto/block.h>
22 #include <kripto/stream.h>
23 #include <kripto/desc/stream.h>
24 #include <kripto/object/stream.h>
26 #include <kripto/stream/ctr.h>
30 struct kripto_stream_object obj
;
34 unsigned int blocksize
;
49 for(i
= 0; i
< len
; i
++)
51 if(s
->used
== s
->blocksize
)
53 kripto_block_encrypt(s
->block
, s
->x
, s
->buf
);
56 for(n
= s
->blocksize
- 1; n
; n
--)
60 U8(out
)[i
] = CU8(in
)[i
] ^ s
->buf
[s
->used
++];
74 for(i
= 0; i
< len
; i
++)
76 if(s
->used
== s
->blocksize
)
78 kripto_block_encrypt(s
->block
, s
->x
, s
->buf
);
81 for(n
= s
->blocksize
- 1; n
; n
--)
85 U8(out
)[i
] = s
->buf
[s
->used
++];
89 static void ctr_destroy(kripto_stream
*s
)
91 kripto_block_destroy(s
->block
);
92 kripto_memwipe(s
, sizeof(kripto_stream
) + (s
->blocksize
<< 1));
98 kripto_stream_desc desc
;
99 const kripto_block_desc
*block
;
102 #define EXT(X) ((const struct ext *)(X))
104 static kripto_stream
*ctr_create
106 const kripto_stream_desc
*desc
,
109 unsigned int key_len
,
116 s
= malloc(sizeof(kripto_stream
) + (desc
->maxiv
<< 1));
122 s
->used
= s
->blocksize
= desc
->maxiv
;
124 s
->x
= (uint8_t *)s
+ sizeof(kripto_stream
);
125 s
->buf
= s
->x
+ s
->blocksize
;
128 s
->block
= kripto_block_create(EXT(desc
)->block
, rounds
, key
, key_len
);
131 kripto_memwipe(s
, sizeof(kripto_stream
) + (s
->blocksize
<< 1));
137 if(iv_len
) memcpy(s
->x
, iv
, iv_len
);
138 memset(s
->x
+ iv_len
, 0, s
->blocksize
- iv_len
);
143 static kripto_stream
*ctr_recreate
148 unsigned int key_len
,
154 s
->block
= kripto_block_recreate(s
->block
, rounds
, key
, key_len
);
157 kripto_memwipe(s
, sizeof(kripto_stream
) + (s
->blocksize
<< 1));
163 if(iv_len
) memcpy(s
->x
, iv
, iv_len
);
164 memset(s
->x
+ iv_len
, 0, s
->blocksize
- iv_len
);
166 s
->used
= s
->blocksize
;
171 kripto_stream_desc
*kripto_stream_ctr(const kripto_block_desc
*block
)
175 s
= malloc(sizeof(struct ext
));
180 s
->desc
.create
= &ctr_create
;
181 s
->desc
.recreate
= &ctr_recreate
;
182 s
->desc
.encrypt
= &ctr_crypt
;
183 s
->desc
.decrypt
= &ctr_crypt
;
184 s
->desc
.prng
= &ctr_prng
;
185 s
->desc
.destroy
= &ctr_destroy
;
186 s
->desc
.maxkey
= kripto_block_maxkey(block
);
187 s
->desc
.maxiv
= kripto_block_size(block
);
189 return (kripto_stream_desc
*)s
;