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.
19 #include <kripto/macros.h>
20 #include <kripto/memwipe.h>
21 #include <kripto/block.h>
22 #include <kripto/mode.h>
23 #include <kripto/mode_desc.h>
24 #include <kripto/stream.h>
25 #include <kripto/stream_desc.h>
27 #include <kripto/mode/ofb.h>
31 kripto_stream_desc
*desc
;
32 const kripto_block
*block
;
34 unsigned int block_size
;
38 static size_t ofb_crypt
48 for(i
= 0; i
< len
; i
++)
50 if(s
->used
== s
->block_size
)
52 kripto_block_encrypt(s
->block
, s
->prev
, s
->prev
);
56 U8(out
)[i
] = CU8(in
)[i
] ^ s
->prev
[s
->used
++];
62 static size_t ofb_prng
71 for(i
= 0; i
< len
; i
++)
73 if(s
->used
== s
->block_size
)
75 kripto_block_encrypt(s
->block
, s
->prev
, s
->prev
);
79 U8(out
)[i
] = s
->prev
[s
->used
++];
85 static void ofb_destroy(kripto_stream
*s
)
87 kripto_memwipe(s
, sizeof(struct kripto_stream
)
88 + sizeof(struct kripto_stream_desc
)
95 static kripto_stream
*ofb_create
97 const kripto_block
*block
,
99 const unsigned int iv_len
103 kripto_block_desc
*b
;
104 struct kripto_stream_desc
*stream
;
106 b
= kripto_block_get_desc(block
);
108 s
= malloc(sizeof(struct kripto_stream
)
109 + sizeof(struct kripto_stream_desc
)
110 + kripto_block_size(b
)
114 s
->block_size
= kripto_block_size(b
);
116 stream
= (struct kripto_stream_desc
*)
117 ((uint8_t *)s
+ sizeof(struct kripto_stream
));
119 s
->prev
= (uint8_t *)stream
+ sizeof(struct kripto_stream_desc
);
121 stream
->encrypt
= &ofb_crypt
;
122 stream
->decrypt
= &ofb_crypt
;
123 stream
->prng
= &ofb_prng
;
125 stream
->destroy
= &ofb_destroy
;
126 stream
->max_key
= kripto_block_max_key(b
);
127 stream
->max_iv
= s
->block_size
;
131 if(iv_len
) memcpy(s
->prev
, iv
, iv_len
);
132 memset(s
->prev
+ iv_len
, 0, s
->block_size
- iv_len
);
134 s
->used
= s
->block_size
;
140 static const struct kripto_mode_desc ofb
=
146 kripto_mode_desc
*const kripto_mode_ofb
= &ofb
;