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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
* Copyright 2020 RackTop Systems, Inc.
*/
/*
* Helper functions for SMB3 encryption using the
* Kernel Cryptographic Framework (KCF)
*
* There are two implementations of these functions:
* This one (for kernel) and another for user space:
* See: lib/smbsrv/libfksmbsrv/common/fksmb_encrypt_pkcs.c
*/
#include <sys/crypto/api.h>
#include <smbsrv/smb_kcrypt.h>
#include <smbsrv/smb2_kproto.h>
#include <sys/cmn_err.h>
/*
* Common function to see if a mech is available.
*/
static int
find_mech(smb_crypto_mech_t *mech, const char *name)
{
crypto_mech_type_t t;
t = crypto_mech2id(name);
if (t == CRYPTO_MECH_INVALID) {
cmn_err(CE_NOTE, "smb: no kcf mech: %s", name);
return (-1);
}
mech->cm_type = t;
return (0);
}
/*
* SMB3 encryption helpers:
* (getmech, init, update, final)
*/
int
smb3_aes_ccm_getmech(smb_crypto_mech_t *mech)
{
return (find_mech(mech, SUN_CKM_AES_CCM));
}
int
smb3_aes_gcm_getmech(smb_crypto_mech_t *mech)
{
return (find_mech(mech, SUN_CKM_AES_GCM));
}
void
smb3_crypto_init_ccm_param(smb3_crypto_param_t *param,
uint8_t *nonce, size_t noncesize, uint8_t *auth, size_t authsize,
size_t datasize)
{
param->ccm.ulMACSize = SMB2_SIG_SIZE;
param->ccm.ulNonceSize = noncesize;
param->ccm.nonce = nonce;
param->ccm.ulDataSize = datasize;
param->ccm.ulAuthDataSize = authsize;
param->ccm.authData = auth;
}
void
smb3_crypto_init_gcm_param(smb3_crypto_param_t *param,
uint8_t *nonce, size_t noncesize, uint8_t *auth, size_t authsize)
{
ASSERT3U(noncesize, ==, 12);
param->gcm.pIv = nonce;
param->gcm.ulIvLen = noncesize; /* should be 12 bytes */
/* tform hdr size - (protcolo id + signing) == 32 bytes */
param->gcm.ulTagBits = SMB2_SIG_SIZE << 3; /* convert bytes to bits */
param->gcm.pAAD = auth; /* auth data */
param->gcm.ulAADLen = authsize; /* auth data len */
}
/*
* Start the KCF session, load the key
*/
static int
smb3_crypto_init(smb3_enc_ctx_t *ctxp, smb_crypto_mech_t *mech,
uint8_t *key, size_t key_len, smb3_crypto_param_t *param,
boolean_t is_encrypt)
{
crypto_key_t ckey;
int rv;
bzero(&ckey, sizeof (ckey));
ckey.ck_format = CRYPTO_KEY_RAW;
ckey.ck_data = key;
ckey.ck_length = key_len * 8; /* in bits */
mech->cm_param = (caddr_t)param;
mech->cm_param_len = sizeof (*param);
if (is_encrypt)
rv = crypto_encrypt_init(mech, &ckey, NULL, &ctxp->ctx, NULL);
else
rv = crypto_decrypt_init(mech, &ckey, NULL, &ctxp->ctx, NULL);
if (rv != CRYPTO_SUCCESS) {
if (is_encrypt)
cmn_err(CE_WARN,
"crypto_encrypt_init failed: 0x%x", rv);
else
cmn_err(CE_WARN,
"crypto_decrypt_init failed: 0x%x", rv);
}
return (rv == CRYPTO_SUCCESS ? 0 : -1);
}
int
smb3_encrypt_init(smb3_enc_ctx_t *ctxp, smb_crypto_mech_t *mech,
smb3_crypto_param_t *param, uint8_t *key, size_t keylen,
uint8_t *buf, size_t buflen)
{
bzero(&ctxp->output, sizeof (ctxp->output));
ctxp->output.cd_format = CRYPTO_DATA_RAW;
ctxp->output.cd_length = buflen;
ctxp->output.cd_raw.iov_len = buflen;
ctxp->output.cd_raw.iov_base = (void *)buf;
return (smb3_crypto_init(ctxp, mech, key, keylen,
param, B_TRUE));
}
int
smb3_decrypt_init(smb3_enc_ctx_t *ctxp, smb_crypto_mech_t *mech,
smb3_crypto_param_t *param, uint8_t *key, size_t keylen)
{
return (smb3_crypto_init(ctxp, mech, key, keylen,
param, B_FALSE));
}
/*
* Digest one segment
*/
int
smb3_encrypt_update(smb3_enc_ctx_t *ctxp, uint8_t *in, size_t len)
{
crypto_data_t data;
int rv;
bzero(&data, sizeof (data));
data.cd_format = CRYPTO_DATA_RAW;
data.cd_length = len;
data.cd_raw.iov_base = (void *)in;
data.cd_raw.iov_len = len;
rv = crypto_encrypt_update(ctxp->ctx, &data, &ctxp->output, NULL);
if (rv != CRYPTO_SUCCESS) {
cmn_err(CE_WARN, "crypto_encrypt_update failed: 0x%x", rv);
crypto_cancel_ctx(ctxp->ctx);
return (-1);
}
len = ctxp->output.cd_length;
ctxp->len -= len;
ctxp->output.cd_offset += len;
ctxp->output.cd_length = ctxp->len;
return (0);
}
int
smb3_decrypt_update(smb3_enc_ctx_t *ctxp, uint8_t *in, size_t len)
{
crypto_data_t data;
int rv;
bzero(&data, sizeof (data));
data.cd_format = CRYPTO_DATA_RAW;
data.cd_length = len;
data.cd_raw.iov_base = (void *)in;
data.cd_raw.iov_len = len;
/*
* AES_CCM does not output data until decrypt_final,
* and only does so if the signature matches.
*/
rv = crypto_decrypt_update(ctxp->ctx, &data, NULL, NULL);
if (rv != CRYPTO_SUCCESS) {
cmn_err(CE_WARN, "crypto_decrypt_update failed: 0x%x", rv);
crypto_cancel_ctx(ctxp->ctx);
return (-1);
}
return (0);
}
int
smb3_encrypt_final(smb3_enc_ctx_t *ctxp, uint8_t *digest16)
{
crypto_data_t out;
int rv;
uint8_t buf[SMB2_SIG_SIZE + 16] = {0};
size_t outlen;
bzero(&out, sizeof (out));
out.cd_format = CRYPTO_DATA_RAW;
out.cd_length = sizeof (buf);
out.cd_raw.iov_len = sizeof (buf);
out.cd_raw.iov_base = (void *)buf;
rv = crypto_encrypt_final(ctxp->ctx, &out, 0);
if (rv != CRYPTO_SUCCESS) {
cmn_err(CE_WARN, "crypto_encrypt_final failed: 0x%x", rv);
return (-1);
}
/*
* For some reason AES module processes ccm_encrypt_final and
* gcm_encrypt_final differently.
* For GCM it restores original offset (which is 0) and updates
* cd_length to size of residual data + mac len.
* For CCM it does nothing, what means offset is updated and cd_length
* is decreased by size of residual data + mac len.
*/
if (out.cd_offset == 0) {
/* GCM */
outlen = out.cd_length - SMB2_SIG_SIZE;
} else {
/* CCM */
outlen = out.cd_offset - SMB2_SIG_SIZE;
}
if (outlen > 0)
bcopy(buf, ctxp->output.cd_raw.iov_base +
ctxp->output.cd_offset, outlen);
bcopy(buf + outlen, digest16, SMB2_SIG_SIZE);
return (0);
}
int
smb3_decrypt_final(smb3_enc_ctx_t *ctxp, uint8_t *buf, size_t buflen)
{
crypto_data_t out;
int rv;
bzero(&out, sizeof (out));
out.cd_format = CRYPTO_DATA_RAW;
out.cd_length = buflen;
out.cd_raw.iov_len = buflen;
out.cd_raw.iov_base = (void *)buf;
rv = crypto_decrypt_final(ctxp->ctx, &out, NULL);
if (rv != CRYPTO_SUCCESS)
cmn_err(CE_WARN, "crypto_decrypt_final failed: 0x%x", rv);
return (rv == CRYPTO_SUCCESS ? 0 : -1);
}
void
smb3_encrypt_cancel(smb3_enc_ctx_t *ctxp)
{
crypto_cancel_ctx(ctxp->ctx);
}
|