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
use core::array::TryFromSliceError;
use core::convert::{From, TryFrom, TryInto};
use sgx_crypto_sys::*;
use sgx_types::error::{SgxResult, SgxStatus};
use sgx_types::marker::{BytewiseEquality, ContiguousMemory};
use sgx_types::types::{AlignKey128bit, Key128bit, SM4CBC_IV_SIZE};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Sm4Cbc {
key: AlignKey128bit,
iv: Nonce,
}
impl Sm4Cbc {
const MBS_SMS4: usize = 16;
pub fn new(key: &Key128bit, iv: Nonce) -> Sm4Cbc {
Sm4Cbc {
key: AlignKey128bit::from(key),
iv,
}
}
pub fn encrypt(&mut self, src: &[u8], dst: &mut [u8]) -> SgxResult {
let src_len = src.len();
let dst_len = dst.len();
ensure!(
(1..i32::MAX as usize).contains(&src_len),
SgxStatus::InvalidParameter
);
ensure!(src_len % Self::MBS_SMS4 == 0, SgxStatus::InvalidParameter);
ensure!(src_len == dst_len, SgxStatus::InvalidParameter);
let status = unsafe {
sgx_sm4_cbc_encrypt(
&self.key.key as *const Key128bit,
src.as_ptr(),
src_len as u32,
dst.as_mut_ptr(),
self.iv.as_ref().as_ptr(),
self.iv.as_ref().len() as u32,
)
};
ensure!(status.is_success(), status);
Ok(())
}
pub fn encrypt_in_place(&mut self, in_out: &mut [u8]) -> SgxResult {
let mut dst = vec![0_u8; in_out.len()];
self.encrypt(in_out, dst.as_mut_slice())?;
in_out.clone_from_slice(dst.as_slice());
Ok(())
}
pub fn decrypt(&mut self, src: &[u8], dst: &mut [u8]) -> SgxResult {
let src_len = src.len();
let dst_len = dst.len();
ensure!(
(1..i32::MAX as usize).contains(&src_len),
SgxStatus::InvalidParameter
);
ensure!(src_len % Self::MBS_SMS4 == 0, SgxStatus::InvalidParameter);
ensure!(src_len == dst_len, SgxStatus::InvalidParameter);
let status = unsafe {
sgx_sm4_cbc_decrypt(
&self.key.key as *const Key128bit,
src.as_ptr(),
src_len as u32,
dst.as_mut_ptr(),
self.iv.as_ref().as_ptr(),
self.iv.as_ref().len() as u32,
)
};
ensure!(status.is_success(), status);
Ok(())
}
pub fn decrypt_in_place(&mut self, in_out: &mut [u8]) -> SgxResult {
let mut dst = vec![0_u8; in_out.len()];
self.decrypt(in_out, dst.as_mut_slice())?;
in_out.clone_from_slice(dst.as_slice());
Ok(())
}
}
impl Default for Sm4Cbc {
fn default() -> Sm4Cbc {
let mut key = AlignKey128bit::default();
super::rand(&mut key.key);
Sm4Cbc {
key,
iv: Nonce::new(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Nonce([u8; SM4CBC_IV_SIZE]);
impl Nonce {
pub fn new() -> Nonce {
let mut nonce = [0_u8; SM4CBC_IV_SIZE];
super::rand(&mut nonce);
Nonce(nonce)
}
#[inline]
pub fn zeroed() -> Nonce {
Nonce([0_u8; SM4CBC_IV_SIZE])
}
}
impl Default for Nonce {
#[inline]
fn default() -> Nonce {
Nonce::new()
}
}
impl AsRef<[u8; SM4CBC_IV_SIZE]> for Nonce {
#[inline]
fn as_ref(&self) -> &[u8; SM4CBC_IV_SIZE] {
&self.0
}
}
impl From<[u8; SM4CBC_IV_SIZE]> for Nonce {
#[inline]
fn from(nonce: [u8; SM4CBC_IV_SIZE]) -> Nonce {
Nonce(nonce)
}
}
impl From<&[u8; SM4CBC_IV_SIZE]> for Nonce {
#[inline]
fn from(nonce: &[u8; SM4CBC_IV_SIZE]) -> Nonce {
Nonce(*nonce)
}
}
impl TryFrom<&[u8]> for Nonce {
type Error = TryFromSliceError;
fn try_from(nonce: &[u8]) -> Result<Nonce, TryFromSliceError> {
let nonce: &[u8; SM4CBC_IV_SIZE] = nonce.try_into()?;
Ok(Nonce(*nonce))
}
}
unsafe impl ContiguousMemory for Nonce {}
unsafe impl BytewiseEquality for Nonce {}