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.
20 #include <kripto/macros.h>
21 #include <kripto/memwipe.h>
22 #include <kripto/block.h>
23 #include <kripto/mode.h>
24 #include <kripto/mode_desc.h>
25 #include <kripto/stream.h>
26 #include <kripto/stream_desc.h>
28 #include <kripto/mode/ofb.h>
32 kripto_stream_desc
*desc
;
33 const kripto_block
*block
;
34 unsigned int block_size
;
39 static size_t ofb_crypt
52 for(i
= 0; i
< len
; i
++)
54 if(s
->used
== s
->block_size
)
56 kripto_block_encrypt(s
->block
, s
->prev
, s
->prev
);
60 U8(out
)[i
] = CU8(in
)[i
] ^ s
->prev
[s
->used
++];
66 static size_t ofb_prng
77 for(i
= 0; i
< len
; i
++)
79 if(s
->used
== s
->block_size
)
81 kripto_block_encrypt(s
->block
, s
->prev
, s
->prev
);
85 U8(out
)[i
] = s
->prev
[s
->used
++];
91 static void ofb_destroy(kripto_stream
*s
)
93 kripto_memwipe(s
, sizeof(struct kripto_stream
)
94 + sizeof(struct kripto_stream_desc
)
100 static kripto_stream
*ofb_create
102 const kripto_block
*block
,
104 const unsigned int iv_len
108 kripto_block_desc
*b
;
109 struct kripto_stream_desc
*stream
;
113 b
= kripto_block_get_desc(block
);
115 assert(iv_len
> kripto_block_size(b
));
117 s
= malloc(sizeof(struct kripto_stream
)
118 + sizeof(struct kripto_stream_desc
)
119 + kripto_block_size(b
)
123 s
->block_size
= kripto_block_size(b
);
125 stream
= (struct kripto_stream_desc
*)
126 ((uint8_t *)s
+ sizeof(struct kripto_stream
));
128 s
->prev
= (uint8_t *)stream
+ sizeof(struct kripto_stream_desc
);
130 stream
->encrypt
= &ofb_crypt
;
131 stream
->decrypt
= &ofb_crypt
;
132 stream
->prng
= &ofb_prng
;
134 stream
->destroy
= &ofb_destroy
;
135 stream
->max_key
= kripto_block_max_key(b
);
136 stream
->max_iv
= s
->block_size
;
140 if(iv_len
) memcpy(s
->prev
, iv
, iv_len
);
141 memset(s
->prev
+ iv_len
, 0, s
->block_size
- iv_len
);
143 s
->used
= s
->block_size
;
149 static const struct kripto_mode_desc ofb
=
155 kripto_mode_desc
*const kripto_mode_ofb
= &ofb
;