1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use libc::c_void;
use std::ptr;
use crate::yaca_common as common;
use crate::yaca_lib as lib;
use crate::yaca_conv as conv;
use crate::crypto::{Context, ContextWithPadding, ContextWithRc2Supported,
ContextWithXcmEncryptProperties, ContextWithXcmDecryptProperties};
use crate::*;
pub struct SealContext {
handle: *mut c_void,
}
impl Drop for SealContext {
fn drop(&mut self)
{
unsafe {
lib::yaca_context_destroy(self.handle)
}
}
}
impl Context for SealContext {
fn get_handle(&self) -> *mut c_void
{
self.handle
}
}
impl ContextWithPadding for SealContext {}
impl ContextWithRc2Supported for SealContext {}
impl ContextWithXcmEncryptProperties for SealContext {
fn set_property_ccm_aad(&self, ccm_aad: &[u8], plaintext_len: usize) -> Result<()>
{
seal_set_input_length(self, plaintext_len)?;
crypto::context_set_property_multiple(self, types::Property::CcmAad, ccm_aad)
}
}
impl SealContext {
pub fn initialize(pub_key: &Key, algo: &EncryptAlgorithm, bcm: &BlockCipherMode,
sym_key_length: &KeyLength) -> Result<(SealContext, Key, Option<Key>)>
{
seal_initialize(pub_key, algo, bcm, sym_key_length)
}
pub fn update(&self, plaintext: &[u8]) -> Result<Vec<u8>>
{
seal_update(self, plaintext)
}
pub fn finalize(&self) -> Result<Vec<u8>>
{
seal_finalize(&self)
}
}
#[inline]
fn seal_initialize(pub_key: &Key, algo: &EncryptAlgorithm, bcm: &BlockCipherMode,
sym_key_length: &KeyLength) -> Result<(SealContext, Key, Option<Key>)>
{
let pub_key = key::get_handle(&pub_key);
let algo = conv::encrypt_rs_to_c(algo);
let bcm = conv::bcm_rs_to_c(bcm);
let sym_key_bit_length = conv::key_length_rs_to_c(sym_key_length);
let mut sym_key = ptr::null();
let mut iv = ptr::null();
let mut handle = ptr::null_mut();
let r = unsafe {
lib::yaca_seal_initialize(&mut handle, pub_key, algo, bcm, sym_key_bit_length,
&mut sym_key, &mut iv)
};
conv::res_c_to_rs(r)?;
debug_assert!(!sym_key.is_null());
debug_assert!(!handle.is_null());
let iv = if !iv.is_null() {
Some(key::new_key(iv))
} else {
None
};
let sym_key = key::new_key(sym_key);
let ctx = SealContext{handle};
Ok((ctx, sym_key, iv))
}
#[inline]
fn seal_set_input_length(ctx: &SealContext, input_len: usize) -> Result<()>
{
common::enc_set_input_length(ctx, input_len, lib::yaca_seal_update)
}
#[inline]
fn seal_update(ctx: &SealContext, plaintext: &[u8]) -> Result<Vec<u8>>
{
common::enc_upd(ctx, plaintext, lib::yaca_seal_update)
}
#[inline]
fn seal_finalize(ctx: &SealContext) -> Result<Vec<u8>>
{
common::enc_fin(ctx, lib::yaca_seal_finalize)
}
pub struct OpenContext {
handle: *mut c_void,
}
impl Drop for OpenContext {
fn drop(&mut self)
{
unsafe {
lib::yaca_context_destroy(self.handle)
}
}
}
impl Context for OpenContext {
fn get_handle(&self) -> *mut c_void
{
self.handle
}
}
impl ContextWithPadding for OpenContext {}
impl ContextWithRc2Supported for OpenContext {}
impl ContextWithXcmDecryptProperties for OpenContext {
fn set_property_ccm_aad(&self, ccm_aad: &[u8], ciphertext_len: usize) -> Result<()>
{
open_set_input_length(self, ciphertext_len)?;
crypto::context_set_property_multiple(self, types::Property::CcmAad, ccm_aad)
}
}
impl OpenContext {
pub fn initialize(prv_key: &Key, algo: &EncryptAlgorithm, bcm: &BlockCipherMode,
sym_key_length: &KeyLength, sym_key: &Key,
iv: Option<&Key>) -> Result<OpenContext>
{
open_initialize(prv_key, algo, bcm, sym_key_length, sym_key, iv)
}
pub fn update(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
{
open_update(self, ciphertext)
}
pub fn finalize(&self) -> Result<Vec<u8>>
{
open_finalize(&self)
}
}
#[inline]
fn open_initialize(prv_key: &Key, algo: &EncryptAlgorithm, bcm: &BlockCipherMode,
sym_key_length: &KeyLength, sym_key: &Key,
iv: Option<&Key>) -> Result<OpenContext>
{
let prv_key = key::get_handle(&prv_key);
let algo = conv::encrypt_rs_to_c(algo);
let bcm = conv::bcm_rs_to_c(bcm);
let sym_key_bit_len = conv::key_length_rs_to_c(&sym_key_length);
let sym_key = key::get_handle(sym_key);
let iv = match iv {
Some(i) => key::get_handle(&i),
None => ptr::null(),
};
let mut handle = ptr::null_mut();
let r = unsafe {
lib::yaca_open_initialize(&mut handle, prv_key, algo, bcm,
sym_key_bit_len, sym_key, iv)
};
conv::res_c_to_rs(r)?;
debug_assert!(!handle.is_null());
Ok(OpenContext{handle})
}
#[inline]
fn open_set_input_length(ctx: &OpenContext, input_len: usize) -> Result<()>
{
common::enc_set_input_length(ctx, input_len, lib::yaca_open_update)
}
#[inline]
fn open_update(ctx: &OpenContext, ciphertext: &[u8]) -> Result<Vec<u8>>
{
common::enc_upd(ctx, ciphertext, lib::yaca_open_update)
}
#[inline]
fn open_finalize(ctx: &OpenContext) -> Result<Vec<u8>>
{
common::enc_fin(ctx, lib::yaca_open_finalize)
}