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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2018, Joyent, Inc.
*/
#include <strings.h>
#include <md5.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/sha1.h>
#include <sys/sha2.h>
#include <sys/types.h>
#include <security/cryptoki.h>
#include "softGlobal.h"
#include "softOps.h"
#include "softSession.h"
#include "softObject.h"
/*
* soft_digest_init()
*
* Arguments:
* session_p: pointer to soft_session_t struct
* pMechanism: pointer to CK_MECHANISM struct provided by application
*
* Description:
* called by C_DigestInit(). This function allocates space for
* context, then calls the corresponding software provided digest
* init routine based on the mechanism.
*
* Returns:
* CKR_OK: success
* CKR_HOST_MEMORY: run out of system memory
* CKR_MECHANISM_INVALID: invalid mechanism type
*/
CK_RV
soft_digest_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism)
{
switch (pMechanism->mechanism) {
case CKM_MD5:
(void) pthread_mutex_lock(&session_p->session_mutex);
session_p->digest.context = malloc(sizeof (MD5_CTX));
if (session_p->digest.context == NULL) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
return (CKR_HOST_MEMORY);
}
session_p->digest.mech.mechanism = CKM_MD5;
(void) pthread_mutex_unlock(&session_p->session_mutex);
MD5Init((MD5_CTX *)session_p->digest.context);
break;
case CKM_SHA_1:
(void) pthread_mutex_lock(&session_p->session_mutex);
session_p->digest.context = malloc(sizeof (SHA1_CTX));
if (session_p->digest.context == NULL) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
return (CKR_HOST_MEMORY);
}
session_p->digest.mech.mechanism = CKM_SHA_1;
session_p->digest.mech.pParameter = pMechanism->pParameter;
session_p->digest.mech.ulParameterLen =
pMechanism->ulParameterLen;
(void) pthread_mutex_unlock(&session_p->session_mutex);
SHA1Init((SHA1_CTX *)session_p->digest.context);
break;
case CKM_SHA256:
case CKM_SHA384:
case CKM_SHA512:
case CKM_SHA512_224:
case CKM_SHA512_256:
(void) pthread_mutex_lock(&session_p->session_mutex);
session_p->digest.context = malloc(sizeof (SHA2_CTX));
if (session_p->digest.context == NULL) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
return (CKR_HOST_MEMORY);
}
session_p->digest.mech.mechanism = pMechanism->mechanism;
(void) pthread_mutex_unlock(&session_p->session_mutex);
switch (pMechanism->mechanism) {
case CKM_SHA256:
SHA2Init(SHA256,
(SHA2_CTX *)session_p->digest.context);
break;
case CKM_SHA384:
SHA2Init(SHA384,
(SHA2_CTX *)session_p->digest.context);
break;
case CKM_SHA512:
SHA2Init(SHA512,
(SHA2_CTX *)session_p->digest.context);
break;
case CKM_SHA512_224:
SHA2Init(SHA512_224,
(SHA2_CTX *)session_p->digest.context);
break;
case CKM_SHA512_256:
SHA2Init(SHA512_256,
(SHA2_CTX *)session_p->digest.context);
break;
}
break;
default:
return (CKR_MECHANISM_INVALID);
}
return (CKR_OK);
}
/*
* soft_digest_common()
*
* Arguments:
* session_p: pointer to soft_session_t struct
* pData: pointer to the input data to be digested
* ulDataLen: length of the input data
* pDigest: pointer to the output data after digesting
* pulDigestLen: length of the output data
*
* Description:
* called by soft_digest() or soft_digest_final(). This function
* determines the length of output buffer and calls the corresponding
* software provided digest routine based on the mechanism.
*
* Returns:
* CKR_OK: success
* CKR_MECHANISM_INVALID: invalid mechanism type
* CKR_BUFFER_TOO_SMALL: the output buffer provided by application
* is too small
*/
CK_RV
soft_digest_common(soft_session_t *session_p, CK_BYTE_PTR pData,
CK_ULONG ulDataLen, CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
{
CK_ULONG digestLen = 0;
size_t len = 0;
/*
* Determine the output data length based on the mechanism
*/
switch (session_p->digest.mech.mechanism) {
case CKM_MD5:
digestLen = 16;
break;
case CKM_SHA_1:
digestLen = SHA1_DIGEST_LENGTH;
break;
case CKM_SHA256:
digestLen = SHA256_DIGEST_LENGTH;
break;
case CKM_SHA384:
digestLen = SHA384_DIGEST_LENGTH;
break;
case CKM_SHA512:
digestLen = SHA512_DIGEST_LENGTH;
break;
case CKM_SHA512_224:
digestLen = SHA512_224_DIGEST_LENGTH;
break;
case CKM_SHA512_256:
digestLen = SHA512_256_DIGEST_LENGTH;
break;
default:
return (CKR_MECHANISM_INVALID);
}
if (pDigest == NULL) {
/*
* Application only wants to know the length of the
* buffer needed to hold the message digest.
*/
*pulDigestLen = digestLen;
return (CKR_OK);
}
if (*pulDigestLen < digestLen) {
/*
* Application provides buffer too small to hold the
* digest message. Return the length of buffer needed
* to the application.
*/
*pulDigestLen = digestLen;
return (CKR_BUFFER_TOO_SMALL);
}
/*
* Call the corresponding system provided software digest routine.
* If the soft_digest_common() is called by soft_digest_final()
* the pData is NULL, and the ulDataLen is zero.
*/
switch (session_p->digest.mech.mechanism) {
case CKM_MD5:
if (pData != NULL) {
/*
* this is called by soft_digest()
*/
#ifdef __sparcv9
MD5Update((MD5_CTX *)session_p->digest.context,
/* LINTED */
pData, (uint_t)ulDataLen);
#else /* !__sparcv9 */
MD5Update((MD5_CTX *)session_p->digest.context,
pData, ulDataLen);
#endif /* __sparcv9 */
MD5Final(pDigest, (MD5_CTX *)session_p->digest.context);
} else {
/*
* this is called by soft_digest_final()
*/
MD5Final(pDigest, (MD5_CTX *)session_p->digest.context);
len = sizeof (MD5_CTX);
}
break;
case CKM_SHA_1:
if (pData != NULL) {
/*
* this is called by soft_digest()
*/
#ifdef __sparcv9
SHA1Update((SHA1_CTX *)session_p->digest.context,
/* LINTED */
pData, (uint32_t)ulDataLen);
#else /* !__sparcv9 */
SHA1Update((SHA1_CTX *)session_p->digest.context,
pData, ulDataLen);
#endif /* __sparcv9 */
SHA1Final(pDigest,
(SHA1_CTX *)session_p->digest.context);
} else {
/*
* this is called by soft_digest_final()
*/
SHA1Final(pDigest,
(SHA1_CTX *)session_p->digest.context);
len = sizeof (SHA1_CTX);
}
break;
case CKM_SHA256:
case CKM_SHA384:
case CKM_SHA512:
case CKM_SHA512_224:
case CKM_SHA512_256:
if (pData != NULL) {
/*
* this is called by soft_digest()
*/
SHA2Update((SHA2_CTX *)session_p->digest.context,
pData, ulDataLen);
SHA2Final(pDigest,
(SHA2_CTX *)session_p->digest.context);
} else {
/*
* this is called by soft_digest_final()
*/
SHA2Final(pDigest,
(SHA2_CTX *)session_p->digest.context);
len = sizeof (SHA2_CTX);
}
break;
}
/* Paranoia on behalf of C_DigestKey callers: bzero the context */
if (session_p->digest.flags & CRYPTO_KEY_DIGESTED) {
explicit_bzero(session_p->digest.context, len);
session_p->digest.flags &= ~CRYPTO_KEY_DIGESTED;
}
*pulDigestLen = digestLen;
(void) pthread_mutex_lock(&session_p->session_mutex);
free(session_p->digest.context);
session_p->digest.context = NULL;
(void) pthread_mutex_unlock(&session_p->session_mutex);
return (CKR_OK);
}
/*
* soft_digest()
*
* Arguments:
* session_p: pointer to soft_session_t struct
* pData: pointer to the input data to be digested
* ulDataLen: length of the input data
* pDigest: pointer to the output data after digesting
* pulDigestLen: length of the output data
*
* Description:
* called by C_Digest(). This function calls soft_digest_common().
*
* Returns:
* see return values in soft_digest_common().
*/
CK_RV
soft_digest(soft_session_t *session_p, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
{
return (soft_digest_common(session_p, pData, ulDataLen,
pDigest, pulDigestLen));
}
/*
* soft_digest_update()
*
* Arguments:
* session_p: pointer to soft_session_t struct
* pPart: pointer to the input data to be digested
* ulPartLen: length of the input data
*
* Description:
* called by C_DigestUpdate(). This function calls the corresponding
* software provided digest update routine based on the mechanism.
*
* Returns:
* CKR_OK: success
* CKR_MECHANISM_INVALID: invalid MECHANISM type.
*/
CK_RV
soft_digest_update(soft_session_t *session_p, CK_BYTE_PTR pPart,
CK_ULONG ulPartLen)
{
switch (session_p->digest.mech.mechanism) {
case CKM_MD5:
#ifdef __sparcv9
MD5Update((MD5_CTX *)session_p->digest.context,
/* LINTED */
pPart, (uint_t)ulPartLen);
#else /* !__sparcv9 */
MD5Update((MD5_CTX *)session_p->digest.context,
pPart, ulPartLen);
#endif /* __sparcv9 */
break;
case CKM_SHA_1:
#ifdef __sparcv9
SHA1Update((SHA1_CTX *)session_p->digest.context,
/* LINTED */
pPart, (uint32_t)ulPartLen);
#else /* !__sparcv9 */
SHA1Update((SHA1_CTX *)session_p->digest.context,
pPart, ulPartLen);
#endif /* __sparcv9 */
break;
case CKM_SHA256:
case CKM_SHA384:
case CKM_SHA512:
case CKM_SHA512_224:
case CKM_SHA512_256:
SHA2Update((SHA2_CTX *)session_p->digest.context,
pPart, ulPartLen);
break;
default:
return (CKR_MECHANISM_INVALID);
}
return (CKR_OK);
}
/*
* soft_digest_final()
*
* Arguments:
* session_p: pointer to soft_session_t struct
* pDigest: pointer to the output data after digesting
* pulDigestLen: length of the output data
*
* Description:
* called by C_DigestFinal(). This function calls soft_digest_common().
*
* Returns:
* see return values in soft_digest_common().
*/
CK_RV
soft_digest_final(soft_session_t *session_p, CK_BYTE_PTR pDigest,
CK_ULONG_PTR pulDigestLen)
{
return (soft_digest_common(session_p, NULL, 0,
pDigest, pulDigestLen));
}
/*
* Perform digest init operation internally for the support of
* CKM_MD5_RSA_PKCS, CKM_SHA1_RSA_PKCS, CKM_SHA1_KEY_DERIVATION
* and CKM_MD5_KEY_DERIVATION mechanisms.
*
* This function is called with the session being held, and without
* its mutex taken.
*/
CK_RV
soft_digest_init_internal(soft_session_t *session_p,
CK_MECHANISM_PTR pMechanism)
{
CK_RV rv;
(void) pthread_mutex_lock(&session_p->session_mutex);
/* Check to see if digest operation is already active */
if (session_p->digest.flags & CRYPTO_OPERATION_ACTIVE) {
(void) pthread_mutex_unlock(&session_p->session_mutex);
return (CKR_OPERATION_ACTIVE);
}
session_p->digest.flags = CRYPTO_OPERATION_ACTIVE;
(void) pthread_mutex_unlock(&session_p->session_mutex);
rv = soft_digest_init(session_p, pMechanism);
if (rv != CKR_OK) {
(void) pthread_mutex_lock(&session_p->session_mutex);
session_p->digest.flags &= ~CRYPTO_OPERATION_ACTIVE;
(void) pthread_mutex_unlock(&session_p->session_mutex);
}
return (rv);
}
/*
* Call soft_digest_update() function with the value of a secret key.
*/
CK_RV
soft_digest_key(soft_session_t *session_p, soft_object_t *key_p)
{
CK_RV rv;
/* Only secret key is allowed to be digested */
if (key_p->class != CKO_SECRET_KEY)
return (CKR_KEY_INDIGESTIBLE);
if ((OBJ_SEC_VALUE(key_p) == NULL) ||
(OBJ_SEC_VALUE_LEN(key_p) == 0))
return (CKR_KEY_SIZE_RANGE);
rv = soft_digest_update(session_p, OBJ_SEC_VALUE(key_p),
OBJ_SEC_VALUE_LEN(key_p));
return (rv);
}
/*
* This function releases allocated digest context. The caller
* may (lock_held == B_TRUE) or may not (lock_held == B_FALSE)
* hold a session mutex.
*/
void
soft_digest_cleanup(soft_session_t *session_p, boolean_t lock_held)
{
boolean_t lock_true = B_TRUE;
if (!lock_held)
(void) pthread_mutex_lock(&session_p->session_mutex);
if (session_p->digest.context != NULL) {
free(session_p->digest.context);
session_p->digest.context = NULL;
}
session_p->digest.flags = 0;
if (!lock_held)
SES_REFRELE(session_p, lock_true);
}
|