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
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, SM4CTR_CTR_SIZE};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Sm4Ctr {
key: AlignKey128bit,
ctr: Counter,
}
impl Sm4Ctr {
const CTR_INC_BITS: u32 = 128;
pub fn new(key: &Key128bit, ctr: Counter) -> Sm4Ctr {
Sm4Ctr {
key: AlignKey128bit::from(key),
ctr,
}
}
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 == dst_len, SgxStatus::InvalidParameter);
let mut ctr = self.ctr;
let status = unsafe {
sgx_sm4_ctr_encrypt(
&self.key.key as *const Key128bit,
src.as_ptr(),
src_len as u32,
ctr.as_mut() as *mut u8,
Self::CTR_INC_BITS,
dst.as_mut_ptr(),
)
};
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 == dst_len, SgxStatus::InvalidParameter);
let mut ctr = self.ctr;
let status = unsafe {
sgx_sm4_ctr_decrypt(
&self.key.key as *const Key128bit,
src.as_ptr(),
src_len as u32,
ctr.as_mut() as *mut u8,
Self::CTR_INC_BITS,
dst.as_mut_ptr(),
)
};
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 Sm4Ctr {
fn default() -> Sm4Ctr {
let mut key = AlignKey128bit::default();
super::rand(&mut key.key);
Sm4Ctr {
key,
ctr: Counter::nonce(),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Counter([u8; SM4CTR_CTR_SIZE]);
impl Counter {
pub fn nonce() -> Counter {
let mut nonce = [0_u8; SM4CTR_CTR_SIZE];
super::rand(&mut nonce);
Counter(nonce)
}
#[inline]
pub fn zeroed() -> Counter {
Counter([0; SM4CTR_CTR_SIZE])
}
}
impl Default for Counter {
fn default() -> Counter {
Counter::nonce()
}
}
impl AsRef<[u8; SM4CTR_CTR_SIZE]> for Counter {
fn as_ref(&self) -> &[u8; SM4CTR_CTR_SIZE] {
&self.0
}
}
impl AsMut<[u8; SM4CTR_CTR_SIZE]> for Counter {
fn as_mut(&mut self) -> &mut [u8; SM4CTR_CTR_SIZE] {
&mut self.0
}
}
impl From<[u8; SM4CTR_CTR_SIZE]> for Counter {
fn from(ctr: [u8; SM4CTR_CTR_SIZE]) -> Counter {
Counter(ctr)
}
}
impl TryFrom<&[u8]> for Counter {
type Error = TryFromSliceError;
fn try_from(ctr: &[u8]) -> Result<Counter, TryFromSliceError> {
let ctr: &[u8; SM4CTR_CTR_SIZE] = ctr.try_into()?;
Ok(Counter(*ctr))
}
}
unsafe impl ContiguousMemory for Counter {}
unsafe impl BytewiseEquality for Counter {}