8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / pkcs11 / pkcs11_softtoken / common / softDESCrypt.c
blob8159e9362453563a996257f80e6b99723ef19949
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <pthread.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <sys/types.h>
31 #include <security/cryptoki.h>
32 #include <modes/modes.h>
33 #include <des_impl.h>
34 #include "softSession.h"
35 #include "softObject.h"
36 #include "softCrypt.h"
37 #include "softOps.h"
40 * Allocate context for the active encryption or decryption operation, and
41 * generate DES or DES3 key schedule to speed up the operation.
43 CK_RV
44 soft_des_crypt_init_common(soft_session_t *session_p,
45 CK_MECHANISM_PTR pMechanism, soft_object_t *key_p,
46 boolean_t encrypt)
49 size_t size;
50 soft_des_ctx_t *soft_des_ctx;
52 soft_des_ctx = calloc(1, sizeof (soft_des_ctx_t));
53 if (soft_des_ctx == NULL) {
54 return (CKR_HOST_MEMORY);
57 /* Allocate key schedule for DES or DES3 based on key type. */
58 if (key_p->key_type == CKK_DES)
59 soft_des_ctx->key_sched = des_alloc_keysched(&size, DES, 0);
60 else
61 soft_des_ctx->key_sched = des_alloc_keysched(&size, DES3, 0);
63 if (soft_des_ctx->key_sched == NULL) {
64 free(soft_des_ctx);
65 return (CKR_HOST_MEMORY);
68 soft_des_ctx->keysched_len = size;
69 soft_des_ctx->key_type = key_p->key_type;
71 (void) pthread_mutex_lock(&session_p->session_mutex);
72 if (encrypt) {
73 /* Called by C_EncryptInit. */
74 session_p->encrypt.context = soft_des_ctx;
75 session_p->encrypt.mech.mechanism = pMechanism->mechanism;
76 } else {
77 /* Called by C_DecryptInit. */
78 session_p->decrypt.context = soft_des_ctx;
79 session_p->decrypt.mech.mechanism = pMechanism->mechanism;
81 (void) pthread_mutex_unlock(&session_p->session_mutex);
84 * If this is a non-sensitive key and it does NOT have
85 * a key schedule yet, then allocate one and expand it.
86 * Otherwise, if its a non-sensitive key, and it DOES have
87 * a key schedule already attached to it, just copy the
88 * pre-expanded schedule to the context and avoid the
89 * extra key schedule expansion operation.
91 if (!(key_p->bool_attr_mask & SENSITIVE_BOOL_ON)) {
92 if (OBJ_KEY_SCHED(key_p) == NULL) {
93 void *ks;
94 (void) pthread_mutex_lock(&key_p->object_mutex);
95 if (OBJ_KEY_SCHED(key_p) == NULL) {
96 if (key_p->key_type == CKK_DES)
97 ks = des_alloc_keysched(&size, DES, 0);
98 else
99 ks = des_alloc_keysched(&size, DES3, 0);
100 if (ks == NULL) {
101 (void) pthread_mutex_unlock(
102 &key_p->object_mutex);
103 free(soft_des_ctx);
104 return (CKR_HOST_MEMORY);
106 /* Initialize key schedule for DES or DES3. */
107 if (key_p->key_type == CKK_DES)
108 des_init_keysched(
109 OBJ_SEC(key_p)->sk_value, DES, ks);
110 else if (key_p->key_type == CKK_DES2)
112 * DES3 encryption/decryption needs to
113 * support a DES2 key.
115 des_init_keysched(
116 OBJ_SEC(key_p)->sk_value, DES2, ks);
117 else
118 des_init_keysched(
119 OBJ_SEC(key_p)->sk_value, DES3, ks);
121 OBJ_KEY_SCHED_LEN(key_p) = size;
122 OBJ_KEY_SCHED(key_p) = ks;
124 (void) pthread_mutex_unlock(&key_p->object_mutex);
127 /* Copy the pre-expanded key schedule from the key object */
128 (void) memcpy(soft_des_ctx->key_sched, OBJ_KEY_SCHED(key_p),
129 OBJ_KEY_SCHED_LEN(key_p));
130 soft_des_ctx->keysched_len = OBJ_KEY_SCHED_LEN(key_p);
131 } else {
132 /* for sensitive keys, we cannot cache the key schedule */
133 if (key_p->key_type == CKK_DES)
134 des_init_keysched(OBJ_SEC(key_p)->sk_value,
135 DES, soft_des_ctx->key_sched);
136 else if (key_p->key_type == CKK_DES2)
138 * DES3 encryption/decryption needs to
139 * support a DES2 key.
141 des_init_keysched(OBJ_SEC(key_p)->sk_value,
142 DES2, soft_des_ctx->key_sched);
143 else
144 des_init_keysched(OBJ_SEC(key_p)->sk_value,
145 DES3, soft_des_ctx->key_sched);
148 return (CKR_OK);
153 * soft_des_encrypt_common()
155 * Arguments:
156 * session_p: pointer to soft_session_t struct
157 * pData: pointer to the input data to be encrypted
158 * ulDataLen: length of the input data
159 * pEncrypted: pointer to the output data after encryption
160 * pulEncryptedLen: pointer to the length of the output data
161 * update: boolean flag indicates caller is soft_encrypt
162 * or soft_encrypt_update
164 * Description:
165 * This function calls the corresponding encrypt routine based
166 * on the mechanism.
168 * Returns:
169 * CKR_OK: success
170 * CKR_BUFFER_TOO_SMALL: the output buffer provided by application
171 * is too small
172 * CKR_FUNCTION_FAILED: encrypt function failed
173 * CKR_DATA_LEN_RANGE: the input data is not a multiple of blocksize
175 CK_RV
176 soft_des_encrypt_common(soft_session_t *session_p, CK_BYTE_PTR pData,
177 CK_ULONG ulDataLen, CK_BYTE_PTR pEncrypted,
178 CK_ULONG_PTR pulEncryptedLen, boolean_t update)
180 int rc = 0;
181 CK_RV rv = CKR_OK;
182 soft_des_ctx_t *soft_des_ctx =
183 (soft_des_ctx_t *)session_p->encrypt.context;
184 des_ctx_t *des_ctx;
185 CK_MECHANISM_TYPE mechanism = session_p->encrypt.mech.mechanism;
186 CK_BYTE *in_buf = NULL;
187 CK_BYTE *out_buf = NULL;
188 CK_ULONG out_len;
189 CK_ULONG total_len;
190 CK_ULONG remain;
191 boolean_t pad_mechanism = B_FALSE;
193 pad_mechanism = (mechanism == CKM_DES_CBC_PAD ||
194 mechanism == CKM_DES3_CBC_PAD);
196 * DES only takes input length that is a multiple of blocksize
197 * for C_Encrypt function with the mechanism CKM_DES<n>_ECB or
198 * CKM_DES<n>_CBC.
200 * DES allows any input length for C_Encrypt function with the
201 * mechanism CKM_DES<n>_CBC_PAD and for C_EncryptUpdate function.
203 if (!update && !pad_mechanism) {
204 if ((ulDataLen % DES_BLOCK_LEN) != 0) {
205 rv = CKR_DATA_LEN_RANGE;
206 goto cleanup;
210 if (!update) {
212 * Called by C_Encrypt
214 if (pad_mechanism) {
216 * For CKM_DES<n>_CBC_PAD, compute output length to
217 * count for the padding. If the length of input
218 * data is a multiple of blocksize, then make output
219 * length to be the sum of the input length and
220 * one blocksize. Otherwise, output length will
221 * be rounded up to the next multiple of blocksize.
223 out_len = DES_BLOCK_LEN *
224 (ulDataLen / DES_BLOCK_LEN + 1);
225 } else {
227 * For non-padding mode, the output length will
228 * be same as the input length.
230 out_len = ulDataLen;
234 * If application asks for the length of the output buffer
235 * to hold the ciphertext?
237 if (pEncrypted == NULL) {
238 *pulEncryptedLen = out_len;
239 return (CKR_OK);
242 /* Is the application-supplied buffer large enough? */
243 if (*pulEncryptedLen < out_len) {
244 *pulEncryptedLen = out_len;
245 return (CKR_BUFFER_TOO_SMALL);
248 /* Encrypt pad bytes in a separate operation */
249 if (pad_mechanism) {
250 out_len -= DES_BLOCK_LEN;
253 in_buf = pData;
254 out_buf = pEncrypted;
255 } else {
257 * Called by C_EncryptUpdate
259 * Add the lengths of last remaining data and current
260 * plaintext together to get the total input length.
262 total_len = soft_des_ctx->remain_len + ulDataLen;
265 * If the total input length is less than one blocksize,
266 * or if the total input length is just one blocksize and
267 * the mechanism is CKM_DES<n>_CBC_PAD, we will need to delay
268 * encryption until when more data comes in next
269 * C_EncryptUpdate or when C_EncryptFinal is called.
271 if ((total_len < DES_BLOCK_LEN) ||
272 (pad_mechanism && (total_len == DES_BLOCK_LEN))) {
273 if (pData != NULL) {
275 * Save input data and its length in
276 * the remaining buffer of DES context.
278 (void) memcpy(soft_des_ctx->data +
279 soft_des_ctx->remain_len, pData, ulDataLen);
280 soft_des_ctx->remain_len += ulDataLen;
283 /* Set encrypted data length to 0. */
284 *pulEncryptedLen = 0;
285 return (CKR_OK);
288 /* Compute the length of remaing data. */
289 remain = total_len % DES_BLOCK_LEN;
292 * Make sure that the output length is a multiple of
293 * blocksize.
295 out_len = total_len - remain;
298 * If application asks for the length of the output buffer
299 * to hold the ciphertext?
301 if (pEncrypted == NULL) {
302 *pulEncryptedLen = out_len;
303 return (CKR_OK);
306 /* Is the application-supplied buffer large enough? */
307 if (*pulEncryptedLen < out_len) {
308 *pulEncryptedLen = out_len;
309 return (CKR_BUFFER_TOO_SMALL);
312 if (soft_des_ctx->remain_len != 0) {
314 * Copy last remaining data and current input data
315 * to the output buffer.
317 (void) memmove(pEncrypted + soft_des_ctx->remain_len,
318 pData, out_len - soft_des_ctx->remain_len);
319 (void) memcpy(pEncrypted, soft_des_ctx->data,
320 soft_des_ctx->remain_len);
321 bzero(soft_des_ctx->data, soft_des_ctx->remain_len);
323 in_buf = pEncrypted;
324 } else {
325 in_buf = pData;
327 out_buf = pEncrypted;
331 * Begin Encryption now.
333 switch (mechanism) {
335 case CKM_DES_ECB:
336 case CKM_DES3_ECB:
339 ulong_t i;
340 uint8_t *tmp_inbuf;
341 uint8_t *tmp_outbuf;
343 for (i = 0; i < out_len; i += DES_BLOCK_LEN) {
344 tmp_inbuf = &in_buf[i];
345 tmp_outbuf = &out_buf[i];
346 /* Crunch one block of data for DES. */
347 if (soft_des_ctx->key_type == CKK_DES)
348 (void) des_crunch_block(
349 soft_des_ctx->key_sched,
350 tmp_inbuf, tmp_outbuf, B_FALSE);
351 else
352 (void) des3_crunch_block(
353 soft_des_ctx->key_sched,
354 tmp_inbuf, tmp_outbuf, B_FALSE);
357 if (update) {
359 * For encrypt update, if there is remaining
360 * data, save it and its length in the context.
362 if (remain != 0)
363 (void) memcpy(soft_des_ctx->data, pData +
364 (ulDataLen - remain), remain);
365 soft_des_ctx->remain_len = remain;
368 *pulEncryptedLen = out_len;
369 break;
372 case CKM_DES_CBC:
373 case CKM_DES_CBC_PAD:
374 case CKM_DES3_CBC:
375 case CKM_DES3_CBC_PAD:
377 crypto_data_t out;
379 out.cd_format = CRYPTO_DATA_RAW;
380 out.cd_offset = 0;
381 out.cd_length = out_len;
382 out.cd_raw.iov_base = (char *)out_buf;
383 out.cd_raw.iov_len = out_len;
385 /* Encrypt multiple blocks of data. */
386 rc = des_encrypt_contiguous_blocks(
387 (des_ctx_t *)soft_des_ctx->des_cbc,
388 (char *)in_buf, out_len, &out);
390 if (rc != 0)
391 goto encrypt_failed;
393 if (update) {
395 * For encrypt update, if there is remaining data,
396 * save it and its length in the context.
398 if (remain != 0)
399 (void) memcpy(soft_des_ctx->data, pData +
400 (ulDataLen - remain), remain);
401 soft_des_ctx->remain_len = remain;
402 } else if (pad_mechanism) {
404 * Save the remainder of the input
405 * block in a temporary block because
406 * we don't want to overrun the input buffer
407 * by tacking on pad bytes.
409 CK_BYTE tmpblock[DES_BLOCK_LEN];
410 (void) memcpy(tmpblock, in_buf + out_len,
411 ulDataLen - out_len);
412 soft_add_pkcs7_padding(tmpblock +
413 (ulDataLen - out_len),
414 DES_BLOCK_LEN, ulDataLen - out_len);
416 out.cd_offset = out_len;
417 out.cd_length = DES_BLOCK_LEN;
418 out.cd_raw.iov_base = (char *)out_buf;
419 out.cd_raw.iov_len = out_len + DES_BLOCK_LEN;
421 /* Encrypt last block containing pad bytes. */
422 rc = des_encrypt_contiguous_blocks(
423 (des_ctx_t *)soft_des_ctx->des_cbc,
424 (char *)tmpblock, DES_BLOCK_LEN, &out);
425 out_len += DES_BLOCK_LEN;
428 if (rc == 0) {
429 *pulEncryptedLen = out_len;
430 break;
432 encrypt_failed:
433 *pulEncryptedLen = 0;
434 rv = CKR_FUNCTION_FAILED;
435 goto cleanup;
438 } /* end switch */
440 if (update)
441 return (CKR_OK);
444 * The following code will be executed if the caller is
445 * soft_encrypt() or an error occurred. The encryption
446 * operation will be terminated so we need to do some cleanup.
448 cleanup:
449 (void) pthread_mutex_lock(&session_p->session_mutex);
450 des_ctx = (des_ctx_t *)soft_des_ctx->des_cbc;
451 if (des_ctx != NULL) {
452 bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
453 free(soft_des_ctx->des_cbc);
456 bzero(soft_des_ctx->key_sched, soft_des_ctx->keysched_len);
457 free(soft_des_ctx->key_sched);
458 free(session_p->encrypt.context);
459 session_p->encrypt.context = NULL;
460 (void) pthread_mutex_unlock(&session_p->session_mutex);
462 return (rv);
467 * soft_des_decrypt_common()
469 * Arguments:
470 * session_p: pointer to soft_session_t struct
471 * pEncrypted: pointer to the input data to be decrypted
472 * ulEncryptedLen: length of the input data
473 * pData: pointer to the output data
474 * pulDataLen: pointer to the length of the output data
475 * Update: boolean flag indicates caller is soft_decrypt
476 * or soft_decrypt_update
478 * Description:
479 * This function calls the corresponding decrypt routine based
480 * on the mechanism.
482 * Returns:
483 * CKR_OK: success
484 * CKR_BUFFER_TOO_SMALL: the output buffer provided by application
485 * is too small
486 * CKR_ENCRYPTED_DATA_LEN_RANGE: the input data is not a multiple
487 * of blocksize
488 * CKR_FUNCTION_FAILED: decrypt function failed
490 CK_RV
491 soft_des_decrypt_common(soft_session_t *session_p, CK_BYTE_PTR pEncrypted,
492 CK_ULONG ulEncryptedLen, CK_BYTE_PTR pData,
493 CK_ULONG_PTR pulDataLen, boolean_t update)
496 int rc = 0;
497 CK_RV rv = CKR_OK;
498 soft_des_ctx_t *soft_des_ctx =
499 (soft_des_ctx_t *)session_p->decrypt.context;
500 des_ctx_t *des_ctx;
501 CK_MECHANISM_TYPE mechanism = session_p->decrypt.mech.mechanism;
502 CK_BYTE *in_buf = NULL;
503 CK_BYTE *out_buf = NULL;
504 CK_ULONG out_len;
505 CK_ULONG total_len;
506 CK_ULONG remain;
507 boolean_t pad_mechanism = B_FALSE;
509 pad_mechanism = (mechanism == CKM_DES_CBC_PAD ||
510 mechanism == CKM_DES3_CBC_PAD);
512 * DES only takes input length that is a multiple of 8 bytes
513 * for C_Decrypt function with the mechanism CKM_DES<n>_ECB,
514 * CKM_DES<n>_CBC or CKM_DES<n>_CBC_PAD.
516 * DES allows any input length for C_DecryptUpdate function.
518 if (!update) {
520 * Called by C_Decrypt
522 if ((ulEncryptedLen % DES_BLOCK_LEN) != 0) {
523 rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
524 goto cleanup;
528 * If application asks for the length of the output buffer
529 * to hold the plaintext?
531 if (pData == NULL) {
532 *pulDataLen = ulEncryptedLen;
533 return (CKR_OK);
536 /* Is the application-supplied buffer large enough? */
537 if (!pad_mechanism) {
538 if (*pulDataLen < ulEncryptedLen) {
539 *pulDataLen = ulEncryptedLen;
540 return (CKR_BUFFER_TOO_SMALL);
543 /* Set output length same as input length. */
544 out_len = ulEncryptedLen;
545 } else {
547 * For CKM_DES<n>_CBC_PAD, we don't know how
548 * many bytes for padding at this time, so
549 * we'd assume one block was padded.
551 if (*pulDataLen < (ulEncryptedLen - DES_BLOCK_LEN)) {
552 *pulDataLen = ulEncryptedLen - DES_BLOCK_LEN;
553 return (CKR_BUFFER_TOO_SMALL);
555 out_len = ulEncryptedLen - DES_BLOCK_LEN;
557 in_buf = pEncrypted;
558 out_buf = pData;
559 } else {
561 * Called by C_DecryptUpdate
563 * Add the lengths of last remaining data and current
564 * input data together to get the total input length.
566 total_len = soft_des_ctx->remain_len + ulEncryptedLen;
569 * If the total input length is less than one blocksize,
570 * or if the total input length is just one blocksize and
571 * the mechanism is CKM_DES<n>_CBC_PAD, we will need to delay
572 * decryption until when more data comes in next
573 * C_DecryptUpdate or when C_DecryptFinal is called.
575 if ((total_len < DES_BLOCK_LEN) ||
576 (pad_mechanism && (total_len == DES_BLOCK_LEN))) {
577 if (pEncrypted != NULL) {
579 * Save input data and its length in
580 * the remaining buffer of DES context.
582 (void) memcpy(soft_des_ctx->data +
583 soft_des_ctx->remain_len,
584 pEncrypted, ulEncryptedLen);
585 soft_des_ctx->remain_len += ulEncryptedLen;
588 /* Set output data length to 0. */
589 *pulDataLen = 0;
590 return (CKR_OK);
593 /* Compute the length of remaing data. */
594 remain = total_len % DES_BLOCK_LEN;
597 * Make sure that the output length is a multiple of
598 * blocksize.
600 out_len = total_len - remain;
602 if (pad_mechanism) {
604 * If the input data length is a multiple of
605 * blocksize, then save the last block of input
606 * data in the remaining buffer. C_DecryptFinal
607 * will handle this last block of data.
609 if (remain == 0) {
610 remain = DES_BLOCK_LEN;
611 out_len -= DES_BLOCK_LEN;
616 * If application asks for the length of the output buffer
617 * to hold the plaintext?
619 if (pData == NULL) {
620 *pulDataLen = out_len;
621 return (CKR_OK);
625 * Is the application-supplied buffer large enough?
627 if (*pulDataLen < out_len) {
628 *pulDataLen = out_len;
629 return (CKR_BUFFER_TOO_SMALL);
632 if (soft_des_ctx->remain_len != 0) {
634 * Copy last remaining data and current input data
635 * to the output buffer.
637 (void) memmove(pData + soft_des_ctx->remain_len,
638 pEncrypted, out_len - soft_des_ctx->remain_len);
639 (void) memcpy(pData, soft_des_ctx->data,
640 soft_des_ctx->remain_len);
641 bzero(soft_des_ctx->data, soft_des_ctx->remain_len);
643 in_buf = pData;
644 } else {
645 in_buf = pEncrypted;
647 out_buf = pData;
651 * Begin Decryption.
653 switch (mechanism) {
655 case CKM_DES_ECB:
656 case CKM_DES3_ECB:
658 uint8_t *tmp_inbuf;
659 uint8_t *tmp_outbuf;
660 ulong_t i;
662 for (i = 0; i < out_len; i += DES_BLOCK_LEN) {
663 tmp_inbuf = &in_buf[i];
664 tmp_outbuf = &out_buf[i];
665 /* Crunch one block of data for DES. */
666 if (soft_des_ctx->key_type == CKK_DES)
667 (void) des_crunch_block(
668 soft_des_ctx->key_sched,
669 tmp_inbuf, tmp_outbuf, B_TRUE);
670 else
671 (void) des3_crunch_block(
672 soft_des_ctx->key_sched,
673 tmp_inbuf, tmp_outbuf, B_TRUE);
676 if (update) {
678 * For decrypt update, if there is remaining
679 * data, save it and its length in the context.
681 if (remain != 0)
682 (void) memcpy(soft_des_ctx->data, pEncrypted +
683 (ulEncryptedLen - remain), remain);
684 soft_des_ctx->remain_len = remain;
687 *pulDataLen = out_len;
688 break;
691 case CKM_DES_CBC:
692 case CKM_DES_CBC_PAD:
693 case CKM_DES3_CBC:
694 case CKM_DES3_CBC_PAD:
696 crypto_data_t out;
697 CK_ULONG rem_len;
698 uint8_t last_block[DES_BLOCK_LEN];
700 out.cd_format = CRYPTO_DATA_RAW;
701 out.cd_offset = 0;
702 out.cd_length = out_len;
703 out.cd_raw.iov_base = (char *)out_buf;
704 out.cd_raw.iov_len = out_len;
706 /* Decrypt multiple blocks of data. */
707 rc = des_decrypt_contiguous_blocks(
708 (des_ctx_t *)soft_des_ctx->des_cbc,
709 (char *)in_buf, out_len, &out);
711 if (rc != 0)
712 goto decrypt_failed;
714 if (pad_mechanism && !update) {
715 /* Decrypt last block containing pad bytes. */
716 out.cd_offset = 0;
717 out.cd_length = DES_BLOCK_LEN;
718 out.cd_raw.iov_base = (char *)last_block;
719 out.cd_raw.iov_len = DES_BLOCK_LEN;
721 /* Decrypt last block containing pad bytes. */
722 rc = des_decrypt_contiguous_blocks(
723 (des_ctx_t *)soft_des_ctx->des_cbc,
724 (char *)in_buf + out_len, DES_BLOCK_LEN, &out);
726 if (rc != 0)
727 goto decrypt_failed;
730 * Remove padding bytes after decryption of
731 * ciphertext block to produce the original
732 * plaintext.
734 rv = soft_remove_pkcs7_padding(last_block,
735 DES_BLOCK_LEN, &rem_len);
736 if (rv == CKR_OK) {
737 if (rem_len != 0)
738 (void) memcpy(out_buf + out_len,
739 last_block, rem_len);
740 *pulDataLen = out_len + rem_len;
741 } else {
742 *pulDataLen = 0;
743 goto cleanup;
745 } else {
746 *pulDataLen = out_len;
749 if (update) {
751 * For decrypt update, if there is remaining data,
752 * save it and its length in the context.
754 if (remain != 0)
755 (void) memcpy(soft_des_ctx->data, pEncrypted +
756 (ulEncryptedLen - remain), remain);
757 soft_des_ctx->remain_len = remain;
760 if (rc == 0)
761 break;
762 decrypt_failed:
763 *pulDataLen = 0;
764 rv = CKR_FUNCTION_FAILED;
765 goto cleanup;
767 } /* end switch */
769 if (update)
770 return (CKR_OK);
773 * The following code will be executed if the caller is
774 * soft_decrypt() or an error occurred. The decryption
775 * operation will be terminated so we need to do some cleanup.
777 cleanup:
778 (void) pthread_mutex_lock(&session_p->session_mutex);
779 des_ctx = (des_ctx_t *)soft_des_ctx->des_cbc;
780 if (des_ctx != NULL) {
781 bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
782 free(soft_des_ctx->des_cbc);
785 bzero(soft_des_ctx->key_sched, soft_des_ctx->keysched_len);
786 free(soft_des_ctx->key_sched);
787 free(session_p->decrypt.context);
788 session_p->decrypt.context = NULL;
789 (void) pthread_mutex_unlock(&session_p->session_mutex);
791 return (rv);
796 * Allocate and initialize a context for DES CBC mode of operation.
798 void *
799 des_cbc_ctx_init(void *key_sched, size_t size, uint8_t *ivec, CK_KEY_TYPE type)
802 cbc_ctx_t *cbc_ctx;
804 if ((cbc_ctx = calloc(1, sizeof (cbc_ctx_t))) == NULL)
805 return (NULL);
807 cbc_ctx->cbc_keysched = key_sched;
809 (void) memcpy(&cbc_ctx->cbc_iv[0], ivec, DES_BLOCK_LEN);
811 cbc_ctx->cbc_lastp = (uint8_t *)&cbc_ctx->cbc_iv[0];
812 cbc_ctx->cbc_keysched_len = size;
813 if (type == CKK_DES)
814 cbc_ctx->cbc_flags |= CBC_MODE;
815 else
816 cbc_ctx->cbc_flags |= CBC_MODE | DES3_STRENGTH;
818 return (cbc_ctx);
823 * Allocate and initialize DES contexts for both signing and encrypting,
824 * saving both context pointers in the session struct. For general-length DES
825 * MAC, check the length in the parameter to see if it is in the right range.
827 CK_RV
828 soft_des_sign_verify_init_common(soft_session_t *session_p,
829 CK_MECHANISM_PTR pMechanism, soft_object_t *key_p, boolean_t sign_op)
831 soft_des_ctx_t *soft_des_ctx;
832 CK_MECHANISM encrypt_mech;
833 CK_RV rv;
835 if ((key_p->class != CKO_SECRET_KEY) || (key_p->key_type != CKK_DES)) {
836 return (CKR_KEY_TYPE_INCONSISTENT);
839 /* allocate memory for the sign/verify context */
840 soft_des_ctx = malloc(sizeof (soft_des_ctx_t));
841 if (soft_des_ctx == NULL) {
842 return (CKR_HOST_MEMORY);
845 soft_des_ctx->key_type = key_p->key_type;
847 /* initialization vector is zero for DES MAC */
848 bzero(soft_des_ctx->ivec, DES_BLOCK_LEN);
850 switch (pMechanism->mechanism) {
852 case CKM_DES_MAC_GENERAL:
854 if (pMechanism->ulParameterLen !=
855 sizeof (CK_MAC_GENERAL_PARAMS)) {
856 free(soft_des_ctx);
857 return (CKR_MECHANISM_PARAM_INVALID);
860 if (*(CK_MAC_GENERAL_PARAMS *)pMechanism->pParameter >
861 DES_BLOCK_LEN) {
862 free(soft_des_ctx);
863 return (CKR_MECHANISM_PARAM_INVALID);
866 soft_des_ctx->mac_len = *((CK_MAC_GENERAL_PARAMS_PTR)
867 pMechanism->pParameter);
869 /*FALLTHRU*/
870 case CKM_DES_MAC:
873 * For non-general DES MAC, output is always half as
874 * large as block size
876 if (pMechanism->mechanism == CKM_DES_MAC) {
877 soft_des_ctx->mac_len = DES_MAC_LEN;
880 /* allocate a context for DES encryption */
881 encrypt_mech.mechanism = CKM_DES_CBC_PAD;
882 encrypt_mech.pParameter = (void *)soft_des_ctx->ivec;
883 encrypt_mech.ulParameterLen = DES_BLOCK_LEN;
884 rv = soft_encrypt_init_internal(session_p, &encrypt_mech,
885 key_p);
886 if (rv != CKR_OK) {
887 free(soft_des_ctx);
888 return (rv);
891 (void) pthread_mutex_lock(&session_p->session_mutex);
893 if (sign_op) {
894 session_p->sign.context = soft_des_ctx;
895 session_p->sign.mech.mechanism = pMechanism->mechanism;
896 } else {
897 session_p->verify.context = soft_des_ctx;
898 session_p->verify.mech.mechanism =
899 pMechanism->mechanism;
902 (void) pthread_mutex_unlock(&session_p->session_mutex);
904 break;
906 return (CKR_OK);
910 * Called by soft_sign(), soft_sign_final(), soft_verify() or
911 * soft_verify_final().
913 CK_RV
914 soft_des_sign_verify_common(soft_session_t *session_p, CK_BYTE_PTR pData,
915 CK_ULONG ulDataLen, CK_BYTE_PTR pSigned, CK_ULONG_PTR pulSignedLen,
916 boolean_t sign_op, boolean_t Final)
918 soft_des_ctx_t *soft_des_ctx_sign_verify;
919 soft_des_ctx_t *soft_des_ctx_encrypt;
920 CK_RV rv;
921 CK_BYTE *pEncrypted = NULL;
922 CK_ULONG ulEncryptedLen = 0;
923 uint8_t remainder;
924 CK_BYTE last_block[DES_BLOCK_LEN];
925 des_ctx_t *des_ctx = NULL;
927 if (sign_op) {
928 soft_des_ctx_sign_verify =
929 (soft_des_ctx_t *)session_p->sign.context;
931 if (soft_des_ctx_sign_verify->mac_len == 0) {
932 *pulSignedLen = 0;
933 goto clean_exit;
936 /* Application asks for the length of the output buffer. */
937 if (pSigned == NULL) {
938 *pulSignedLen = soft_des_ctx_sign_verify->mac_len;
939 return (CKR_OK);
942 /* Is the application-supplied buffer large enough? */
943 if (*pulSignedLen < soft_des_ctx_sign_verify->mac_len) {
944 *pulSignedLen = soft_des_ctx_sign_verify->mac_len;
945 return (CKR_BUFFER_TOO_SMALL);
947 } else {
948 soft_des_ctx_sign_verify =
949 (soft_des_ctx_t *)session_p->verify.context;
952 if (Final) {
953 soft_des_ctx_encrypt =
954 (soft_des_ctx_t *)session_p->encrypt.context;
957 * If there is data left in the buffer from a previous
958 * SignUpdate() call, pass enough zeroed data to a
959 * soft_sign_update call to pad the remainder
961 if (soft_des_ctx_encrypt->remain_len != 0) {
962 bzero(last_block, DES_BLOCK_LEN);
963 ulEncryptedLen = DES_BLOCK_LEN;
966 * By passing a buffer to soft_encrypt_final,
967 * we force it to pad the remaining block
968 * and encrypt it.
970 rv = soft_encrypt_final(session_p, last_block,
971 &ulEncryptedLen);
972 if (rv != CKR_OK) {
973 goto clean_exit;
975 } else {
977 * The last block of enciphered data is stored in:
978 * soft_des_ctx_encrypt->des_cbc->des_ctx->dc_lastp
979 * Copy that data to last_block
981 soft_des_ctx_encrypt =
982 (soft_des_ctx_t *)session_p->encrypt.context;
983 des_ctx = (des_ctx_t *)soft_des_ctx_encrypt->des_cbc;
984 (void) memcpy(last_block, des_ctx->dc_lastp,
985 DES_BLOCK_LEN);
988 * Passing a NULL output buffer here
989 * forces the routine to just return.
991 rv = soft_encrypt_final(session_p, NULL,
992 &ulEncryptedLen);
995 } else {
997 * If the input length is not multiple of block size, then
998 * determine the correct encrypted data length by rounding
1000 remainder = ulDataLen % DES_BLOCK_LEN;
1002 * Because we always use DES_CBC_PAD mechanism
1003 * for sign/verify operations, the input will
1004 * be padded to the next 8 byte boundary.
1005 * Adjust the length fields here accordingly.
1007 ulEncryptedLen = ulDataLen + (DES_BLOCK_LEN - remainder);
1009 pEncrypted = malloc(sizeof (CK_BYTE) * ulEncryptedLen);
1010 if (pEncrypted == NULL) {
1011 rv = CKR_HOST_MEMORY;
1012 goto clean_exit;
1016 * Pad the last block with zeros by copying pData into a zeroed
1017 * pEncrypted. Then pass pEncrypted into soft_encrypt as input
1019 bzero(pEncrypted, ulEncryptedLen);
1020 (void) memcpy(pEncrypted, pData, ulDataLen);
1022 rv = soft_encrypt(session_p, pEncrypted, ulDataLen,
1023 pEncrypted, &ulEncryptedLen);
1024 (void) memcpy(last_block,
1025 &pEncrypted[ulEncryptedLen - DES_BLOCK_LEN], DES_BLOCK_LEN);
1028 if (rv == CKR_OK) {
1029 *pulSignedLen = soft_des_ctx_sign_verify->mac_len;
1031 /* the leftmost mac_len bytes of last_block is our MAC */
1032 (void) memcpy(pSigned, last_block, *pulSignedLen);
1035 clean_exit:
1037 (void) pthread_mutex_lock(&session_p->session_mutex);
1039 /* soft_encrypt_common() has freed the encrypt context */
1040 if (sign_op) {
1041 free(session_p->sign.context);
1042 session_p->sign.context = NULL;
1043 } else {
1044 free(session_p->verify.context);
1045 session_p->verify.context = NULL;
1047 session_p->encrypt.flags = 0;
1049 (void) pthread_mutex_unlock(&session_p->session_mutex);
1051 if (pEncrypted) {
1052 free(pEncrypted);
1055 return (rv);
1059 * Called by soft_sign_update()
1061 CK_RV
1062 soft_des_mac_sign_verify_update(soft_session_t *session_p, CK_BYTE_PTR pPart,
1063 CK_ULONG ulPartLen)
1066 * The DES MAC is calculated by taking the specified number of
1067 * left-most bytes within the last block of
1068 * encrypted data, while the context of the multi-part
1069 * encryption stores the block necessary for XORing with the
1070 * input as per cipher block chaining . Therefore, none of the
1071 * intermediary encrypted blocks of data are necessary for
1072 * the DES MAC, and we can create a placeholder local buffer
1073 * for the encrypted data, which is immediately throw away.
1076 soft_des_ctx_t *soft_des_ctx_encrypt;
1077 CK_BYTE *pEncrypted = NULL;
1078 CK_ULONG ulEncryptedLen;
1079 CK_ULONG total_len;
1080 uint8_t remainder;
1081 CK_RV rv;
1083 soft_des_ctx_encrypt = (soft_des_ctx_t *)session_p->encrypt.context;
1085 /* Avoid the malloc if we won't be encrypting any data */
1086 total_len = soft_des_ctx_encrypt->remain_len + ulPartLen;
1088 if (total_len < DES_BLOCK_LEN) {
1089 rv = soft_encrypt_update(session_p, pPart, ulPartLen, NULL,
1090 &ulEncryptedLen);
1091 } else {
1092 remainder = ulPartLen % DES_BLOCK_LEN;
1094 /* round up to the nearest multiple of block size */
1095 ulEncryptedLen = ulPartLen + (DES_BLOCK_LEN - remainder);
1096 pEncrypted = malloc(sizeof (CK_BYTE) * ulEncryptedLen);
1098 if (pEncrypted != NULL) {
1099 rv = soft_encrypt_update(session_p, pPart, ulPartLen,
1100 pEncrypted, &ulEncryptedLen);
1101 free(pEncrypted);
1102 } else {
1103 rv = CKR_HOST_MEMORY;
1106 return (rv);