Print this page
9642 PKCS#11 softtoken should use explicit_bzero
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Alex Wilson <alex.wilson@joyent.com>


   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.
   7  *
   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.
  12  *
  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]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.

  24  */
  25 
  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 <arcfour.h>
  34 #include "softSession.h"
  35 #include "softObject.h"
  36 #include "softOps.h"
  37 #include "softCrypt.h"
  38 #include "softRSA.h"
  39 
  40 /*
  41  * Remove padding bytes.
  42  */
  43 CK_RV


 134 
 135                 rv = soft_des_crypt_init_common(session_p, pMechanism,
 136                     key_p, B_FALSE);
 137 
 138                 if (rv != CKR_OK)
 139                         return (rv);
 140 
 141                 (void) pthread_mutex_lock(&session_p->session_mutex);
 142 
 143                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 144                 /* Save Initialization Vector (IV) in the context. */
 145                 (void) memcpy(soft_des_ctx->ivec, pMechanism->pParameter,
 146                     DES_BLOCK_LEN);
 147 
 148                 /* Allocate a context for DES cipher-block chaining. */
 149                 soft_des_ctx->des_cbc = (void *)des_cbc_ctx_init(
 150                     soft_des_ctx->key_sched, soft_des_ctx->keysched_len,
 151                     soft_des_ctx->ivec, key_p->key_type);
 152 
 153                 if (soft_des_ctx->des_cbc == NULL) {
 154                         bzero(soft_des_ctx->key_sched,
 155                             soft_des_ctx->keysched_len);
 156                         free(soft_des_ctx->key_sched);
 157                         free(session_p->decrypt.context);
 158                         session_p->decrypt.context = NULL;
 159                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 160                         return (CKR_HOST_MEMORY);
 161                 }
 162 
 163                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 164 
 165                 return (rv);
 166         }
 167         case CKM_AES_ECB:
 168 
 169                 if (key_p->key_type != CKK_AES) {
 170                         return (CKR_KEY_TYPE_INCONSISTENT);
 171                 }
 172 
 173                 return (soft_aes_crypt_init_common(session_p, pMechanism,
 174                     key_p, B_FALSE));
 175 
 176         case CKM_AES_CBC:
 177         case CKM_AES_CBC_PAD:


 190                 rv = soft_aes_crypt_init_common(session_p, pMechanism,
 191                     key_p, B_FALSE);
 192 
 193                 if (rv != CKR_OK)
 194                         return (rv);
 195 
 196                 (void) pthread_mutex_lock(&session_p->session_mutex);
 197 
 198                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 199 
 200                 /* Save Initialization Vector (IV) in the context. */
 201                 (void) memcpy(soft_aes_ctx->ivec, pMechanism->pParameter,
 202                     AES_BLOCK_LEN);
 203 
 204                 /* Allocate a context for AES cipher-block chaining. */
 205                 soft_aes_ctx->aes_cbc = (void *)aes_cbc_ctx_init(
 206                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 207                     soft_aes_ctx->ivec);
 208 
 209                 if (soft_aes_ctx->aes_cbc == NULL) {
 210                         bzero(soft_aes_ctx->key_sched,
 211                             soft_aes_ctx->keysched_len);
 212                         free(soft_aes_ctx->key_sched);
 213                         free(session_p->decrypt.context);
 214                         session_p->decrypt.context = NULL;
 215                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 216                         return (CKR_HOST_MEMORY);
 217                 }
 218 
 219                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 220 
 221                 return (rv);
 222         }
 223         case CKM_AES_CTR:
 224         {
 225                 soft_aes_ctx_t *soft_aes_ctx;
 226 
 227                 if (key_p->key_type != CKK_AES) {
 228                         return (CKR_KEY_TYPE_INCONSISTENT);
 229                 }
 230 
 231                 if (pMechanism->pParameter == NULL ||
 232                     pMechanism->ulParameterLen != sizeof (CK_AES_CTR_PARAMS)) {
 233                         return (CKR_MECHANISM_PARAM_INVALID);
 234                 }
 235 
 236                 rv = soft_aes_crypt_init_common(session_p, pMechanism,
 237                     key_p, B_FALSE);
 238 
 239                 if (rv != CKR_OK)
 240                         return (rv);
 241 
 242                 (void) pthread_mutex_lock(&session_p->session_mutex);
 243 
 244                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 245                 soft_aes_ctx->aes_cbc = aes_ctr_ctx_init(
 246                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 247                     pMechanism->pParameter);
 248 
 249                 if (soft_aes_ctx->aes_cbc == NULL) {
 250                         bzero(soft_aes_ctx->key_sched,
 251                             soft_aes_ctx->keysched_len);
 252                         free(soft_aes_ctx->key_sched);
 253                         free(session_p->decrypt.context);
 254                         session_p->decrypt.context = NULL;
 255                         rv = CKR_HOST_MEMORY;
 256                 }
 257 
 258                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 259 
 260                 return (rv);
 261         }
 262         case CKM_BLOWFISH_CBC:
 263         {
 264                 soft_blowfish_ctx_t *soft_blowfish_ctx;
 265 
 266                 if (key_p->key_type != CKK_BLOWFISH)
 267                         return (CKR_KEY_TYPE_INCONSISTENT);
 268 
 269                 if ((pMechanism->pParameter == NULL) ||
 270                     (pMechanism->ulParameterLen != BLOWFISH_BLOCK_LEN))
 271                         return (CKR_MECHANISM_PARAM_INVALID);
 272 
 273                 rv = soft_blowfish_crypt_init_common(session_p, pMechanism,


 275 
 276                 if (rv != CKR_OK)
 277                         return (rv);
 278 
 279                 (void) pthread_mutex_lock(&session_p->session_mutex);
 280 
 281                 soft_blowfish_ctx =
 282                     (soft_blowfish_ctx_t *)session_p->decrypt.context;
 283 
 284                 /* Save Initialization Vector in the context. */
 285                 (void) memcpy(soft_blowfish_ctx->ivec, pMechanism->pParameter,
 286                     BLOWFISH_BLOCK_LEN);
 287 
 288                 /* Allocate a context for CBC */
 289                 soft_blowfish_ctx->blowfish_cbc =
 290                     (void *)blowfish_cbc_ctx_init(soft_blowfish_ctx->key_sched,
 291                     soft_blowfish_ctx->keysched_len,
 292                     soft_blowfish_ctx->ivec);
 293 
 294                 if (soft_blowfish_ctx->blowfish_cbc == NULL) {
 295                         bzero(soft_blowfish_ctx->key_sched,
 296                             soft_blowfish_ctx->keysched_len);
 297                         free(soft_blowfish_ctx->key_sched);
 298                         free(session_p->decrypt.context = NULL);

 299                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 300                         return (CKR_HOST_MEMORY);
 301                 }
 302 
 303                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 304                 return (rv);
 305         }
 306 
 307         case CKM_RC4:
 308 
 309                 if (key_p->key_type != CKK_RC4) {
 310                         return (CKR_KEY_TYPE_INCONSISTENT);
 311                 }
 312 
 313                 return (soft_arcfour_crypt_init(session_p, pMechanism, key_p,
 314                     B_FALSE));
 315 
 316         case CKM_RSA_X_509:
 317         case CKM_RSA_PKCS:
 318 


 537         }
 538         switch (mechanism) {
 539 
 540         case CKM_DES_CBC_PAD:
 541         case CKM_DES3_CBC_PAD:
 542         {
 543 
 544                 soft_des_ctx_t *soft_des_ctx;
 545 
 546                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 547 
 548                 /*
 549                  * We should have only one block of data left in the
 550                  * remaining buffer.
 551                  */
 552                 if (soft_des_ctx->remain_len != DES_BLOCK_LEN) {
 553                         *pulLastPartLen = 0;
 554                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 555                         /* Cleanup memory space. */
 556                         free(soft_des_ctx->des_cbc);
 557                         bzero(soft_des_ctx->key_sched,
 558                             soft_des_ctx->keysched_len);
 559                         free(soft_des_ctx->key_sched);
 560 
 561                         goto clean1;
 562                 }
 563 
 564                 out_len = DES_BLOCK_LEN;
 565 
 566                 /*
 567                  * If application asks for the length of the output buffer
 568                  * to hold the plaintext?
 569                  */
 570                 if (pLastPart == NULL) {
 571                         *pulLastPartLen = out_len;
 572                         rv = CKR_OK;
 573                         goto clean2;
 574                 } else {
 575                         crypto_data_t out;
 576 
 577                         /* Copy remaining data to the output buffer. */
 578                         (void) memcpy(pLastPart, soft_des_ctx->data,
 579                             DES_BLOCK_LEN);


 591 
 592                         if (rc == 0) {
 593                                 /*
 594                                  * Remove padding bytes after decryption of
 595                                  * ciphertext block to produce the original
 596                                  * plaintext.
 597                                  */
 598                                 rv = soft_remove_pkcs7_padding(pLastPart,
 599                                     DES_BLOCK_LEN, &out_len);
 600                                 if (rv != CKR_OK)
 601                                         *pulLastPartLen = 0;
 602                                 else
 603                                         *pulLastPartLen = out_len;
 604                         } else {
 605                                 *pulLastPartLen = 0;
 606                                 rv = CKR_FUNCTION_FAILED;
 607                         }
 608 
 609                         /* Cleanup memory space. */
 610                         free(soft_des_ctx->des_cbc);
 611                         bzero(soft_des_ctx->key_sched,
 612                             soft_des_ctx->keysched_len);
 613                         free(soft_des_ctx->key_sched);
 614 
 615                 }
 616 
 617                 break;
 618         }
 619 
 620         case CKM_DES_CBC:
 621         case CKM_DES_ECB:
 622         case CKM_DES3_CBC:
 623         case CKM_DES3_ECB:
 624         {
 625 
 626                 soft_des_ctx_t *soft_des_ctx;
 627 
 628                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 629                 /*
 630                  * CKM_DES_CBC and CKM_DES_ECB does not do any padding,
 631                  * so when the final is called, the remaining buffer
 632                  * should not contain any more data.
 633                  */
 634                 *pulLastPartLen = 0;
 635                 if (soft_des_ctx->remain_len != 0) {
 636                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 637                 } else {
 638                         if (pLastPart == NULL)
 639                                 goto clean2;
 640                 }
 641 
 642                 /* Cleanup memory space. */
 643                 free(soft_des_ctx->des_cbc);
 644                 bzero(soft_des_ctx->key_sched, soft_des_ctx->keysched_len);
 645                 free(soft_des_ctx->key_sched);
 646 
 647                 break;
 648         }
 649 
 650         case CKM_AES_CBC_PAD:
 651         {
 652 
 653                 soft_aes_ctx_t *soft_aes_ctx;
 654 
 655                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 656 
 657                 /*
 658                  * We should have only one block of data left in the
 659                  * remaining buffer.
 660                  */
 661                 if (soft_aes_ctx->remain_len != AES_BLOCK_LEN) {
 662                         *pulLastPartLen = 0;
 663                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 664                         /* Cleanup memory space. */
 665                         free(soft_aes_ctx->aes_cbc);
 666                         bzero(soft_aes_ctx->key_sched,
 667                             soft_aes_ctx->keysched_len);
 668                         free(soft_aes_ctx->key_sched);
 669 
 670                         goto clean1;
 671                 }
 672 
 673                 out_len = AES_BLOCK_LEN;
 674 
 675                 /*
 676                  * If application asks for the length of the output buffer
 677                  * to hold the plaintext?
 678                  */
 679                 if (pLastPart == NULL) {
 680                         *pulLastPartLen = out_len;
 681                         rv = CKR_OK;
 682                         goto clean2;
 683                 } else {
 684                         crypto_data_t out;
 685 
 686                         /* Copy remaining data to the output buffer. */
 687                         (void) memcpy(pLastPart, soft_aes_ctx->data,
 688                             AES_BLOCK_LEN);


 700 
 701                         if (rc == 0) {
 702                                 /*
 703                                  * Remove padding bytes after decryption of
 704                                  * ciphertext block to produce the original
 705                                  * plaintext.
 706                                  */
 707                                 rv = soft_remove_pkcs7_padding(pLastPart,
 708                                     AES_BLOCK_LEN, &out_len);
 709                                 if (rv != CKR_OK)
 710                                         *pulLastPartLen = 0;
 711                                 else
 712                                         *pulLastPartLen = out_len;
 713                         } else {
 714                                 *pulLastPartLen = 0;
 715                                 rv = CKR_FUNCTION_FAILED;
 716                         }
 717 
 718                         /* Cleanup memory space. */
 719                         free(soft_aes_ctx->aes_cbc);
 720                         bzero(soft_aes_ctx->key_sched,
 721                             soft_aes_ctx->keysched_len);
 722                         free(soft_aes_ctx->key_sched);
 723 
 724                 }
 725 
 726                 break;
 727         }
 728 
 729         case CKM_AES_CBC:
 730         case CKM_AES_ECB:
 731         {
 732                 soft_aes_ctx_t *soft_aes_ctx;
 733 
 734                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 735                 /*
 736                  * CKM_AES_CBC and CKM_AES_ECB does not do any padding,
 737                  * so when the final is called, the remaining buffer
 738                  * should not contain any more data.
 739                  */
 740                 *pulLastPartLen = 0;
 741                 if (soft_aes_ctx->remain_len != 0) {
 742                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 743                 } else {
 744                         if (pLastPart == NULL)
 745                                 goto clean2;
 746                 }
 747 
 748                 /* Cleanup memory space. */
 749                 free(soft_aes_ctx->aes_cbc);
 750                 bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 751                 free(soft_aes_ctx->key_sched);
 752 
 753                 break;
 754         }
 755         case CKM_AES_CTR:
 756         {
 757                 crypto_data_t out;
 758                 soft_aes_ctx_t *soft_aes_ctx;
 759                 ctr_ctx_t *ctr_ctx;
 760                 size_t len;
 761 
 762                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 763                 ctr_ctx = soft_aes_ctx->aes_cbc;
 764                 len = ctr_ctx->ctr_remainder_len;
 765                 if (pLastPart == NULL) {
 766                         *pulLastPartLen = len;
 767                         goto clean1;
 768                 }
 769                 if (len > 0) {
 770                         out.cd_format = CRYPTO_DATA_RAW;
 771                         out.cd_offset = 0;
 772                         out.cd_length = len;
 773                         out.cd_raw.iov_base = (char *)pLastPart;
 774                         out.cd_raw.iov_len = len;
 775 
 776                         rv = ctr_mode_final(ctr_ctx, &out, aes_encrypt_block);
 777                         if (rv == CRYPTO_DATA_LEN_RANGE)
 778                                 rv = CRYPTO_ENCRYPTED_DATA_LEN_RANGE;
 779                 }
 780                 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
 781                         *pulLastPartLen = len;
 782                         goto clean1;
 783                 }
 784 
 785                 /* Cleanup memory space. */
 786                 free(ctr_ctx);
 787                 bzero(soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len);
 788                 free(soft_aes_ctx->key_sched);
 789 
 790                 break;
 791         }
 792         case CKM_BLOWFISH_CBC:
 793         {
 794                 soft_blowfish_ctx_t *soft_blowfish_ctx;
 795 
 796                 soft_blowfish_ctx =
 797                     (soft_blowfish_ctx_t *)session_p->decrypt.context;
 798 
 799                 *pulLastPartLen = 0;
 800                 if (soft_blowfish_ctx->remain_len != 0)
 801                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 802                 else {
 803                         if (pLastPart == NULL)
 804                                 goto clean2;
 805                 }
 806 
 807                 free(soft_blowfish_ctx->blowfish_cbc);
 808                 bzero(soft_blowfish_ctx->key_sched,
 809                     soft_blowfish_ctx->keysched_len);
 810                 free(soft_blowfish_ctx->key_sched);
 811 
 812                 break;
 813         }
 814 
 815         case CKM_RC4:
 816         {
 817                 ARCFour_key *key = (ARCFour_key *)session_p->decrypt.context;
 818                 bzero(key, sizeof (*key));
 819                 *pulLastPartLen = 0;
 820                 break;
 821         }
 822 
 823         default:
 824                 /* PKCS11: The mechanism only supports single-part operation. */
 825                 rv = CKR_MECHANISM_INVALID;
 826                 break;
 827         }
 828 
 829 clean1:
 830         free(session_p->decrypt.context);
 831         session_p->decrypt.context = NULL;
 832 
 833 clean2:
 834         (void) pthread_mutex_unlock(&session_p->session_mutex);
 835 
 836         return (rv);
 837 
 838 }


   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.
   7  *
   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.
  12  *
  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]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2018, Joyent, Inc.
  25  */
  26 
  27 #include <pthread.h>
  28 #include <stdlib.h>
  29 #include <string.h>
  30 #include <strings.h>
  31 #include <sys/types.h>
  32 #include <security/cryptoki.h>
  33 #include <modes/modes.h>
  34 #include <arcfour.h>
  35 #include "softSession.h"
  36 #include "softObject.h"
  37 #include "softOps.h"
  38 #include "softCrypt.h"
  39 #include "softRSA.h"
  40 
  41 /*
  42  * Remove padding bytes.
  43  */
  44 CK_RV


 135 
 136                 rv = soft_des_crypt_init_common(session_p, pMechanism,
 137                     key_p, B_FALSE);
 138 
 139                 if (rv != CKR_OK)
 140                         return (rv);
 141 
 142                 (void) pthread_mutex_lock(&session_p->session_mutex);
 143 
 144                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 145                 /* Save Initialization Vector (IV) in the context. */
 146                 (void) memcpy(soft_des_ctx->ivec, pMechanism->pParameter,
 147                     DES_BLOCK_LEN);
 148 
 149                 /* Allocate a context for DES cipher-block chaining. */
 150                 soft_des_ctx->des_cbc = (void *)des_cbc_ctx_init(
 151                     soft_des_ctx->key_sched, soft_des_ctx->keysched_len,
 152                     soft_des_ctx->ivec, key_p->key_type);
 153 
 154                 if (soft_des_ctx->des_cbc == NULL) {
 155                         freezero(soft_des_ctx->key_sched,
 156                             soft_des_ctx->keysched_len);
 157                         freezero(session_p->decrypt.context,
 158                             sizeof (soft_des_ctx_t));
 159                         session_p->decrypt.context = NULL;
 160                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 161                         return (CKR_HOST_MEMORY);
 162                 }
 163 
 164                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 165 
 166                 return (rv);
 167         }
 168         case CKM_AES_ECB:
 169 
 170                 if (key_p->key_type != CKK_AES) {
 171                         return (CKR_KEY_TYPE_INCONSISTENT);
 172                 }
 173 
 174                 return (soft_aes_crypt_init_common(session_p, pMechanism,
 175                     key_p, B_FALSE));
 176 
 177         case CKM_AES_CBC:
 178         case CKM_AES_CBC_PAD:


 191                 rv = soft_aes_crypt_init_common(session_p, pMechanism,
 192                     key_p, B_FALSE);
 193 
 194                 if (rv != CKR_OK)
 195                         return (rv);
 196 
 197                 (void) pthread_mutex_lock(&session_p->session_mutex);
 198 
 199                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 200 
 201                 /* Save Initialization Vector (IV) in the context. */
 202                 (void) memcpy(soft_aes_ctx->ivec, pMechanism->pParameter,
 203                     AES_BLOCK_LEN);
 204 
 205                 /* Allocate a context for AES cipher-block chaining. */
 206                 soft_aes_ctx->aes_cbc = (void *)aes_cbc_ctx_init(
 207                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 208                     soft_aes_ctx->ivec);
 209 
 210                 if (soft_aes_ctx->aes_cbc == NULL) {
 211                         freezero(soft_aes_ctx->key_sched,
 212                             soft_aes_ctx->keysched_len);
 213                         freezero(session_p->decrypt.context,
 214                             sizeof (soft_aes_ctx_t));
 215                         session_p->decrypt.context = NULL;
 216                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 217                         return (CKR_HOST_MEMORY);
 218                 }
 219 
 220                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 221 
 222                 return (rv);
 223         }
 224         case CKM_AES_CTR:
 225         {
 226                 soft_aes_ctx_t *soft_aes_ctx;
 227 
 228                 if (key_p->key_type != CKK_AES) {
 229                         return (CKR_KEY_TYPE_INCONSISTENT);
 230                 }
 231 
 232                 if (pMechanism->pParameter == NULL ||
 233                     pMechanism->ulParameterLen != sizeof (CK_AES_CTR_PARAMS)) {
 234                         return (CKR_MECHANISM_PARAM_INVALID);
 235                 }
 236 
 237                 rv = soft_aes_crypt_init_common(session_p, pMechanism,
 238                     key_p, B_FALSE);
 239 
 240                 if (rv != CKR_OK)
 241                         return (rv);
 242 
 243                 (void) pthread_mutex_lock(&session_p->session_mutex);
 244 
 245                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 246                 soft_aes_ctx->aes_cbc = aes_ctr_ctx_init(
 247                     soft_aes_ctx->key_sched, soft_aes_ctx->keysched_len,
 248                     pMechanism->pParameter);
 249 
 250                 if (soft_aes_ctx->aes_cbc == NULL) {
 251                         freezero(soft_aes_ctx->key_sched,
 252                             soft_aes_ctx->keysched_len);
 253                         freezero(session_p->decrypt.context,
 254                             sizeof (soft_aes_ctx_t));
 255                         session_p->decrypt.context = NULL;
 256                         rv = CKR_HOST_MEMORY;
 257                 }
 258 
 259                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 260 
 261                 return (rv);
 262         }
 263         case CKM_BLOWFISH_CBC:
 264         {
 265                 soft_blowfish_ctx_t *soft_blowfish_ctx;
 266 
 267                 if (key_p->key_type != CKK_BLOWFISH)
 268                         return (CKR_KEY_TYPE_INCONSISTENT);
 269 
 270                 if ((pMechanism->pParameter == NULL) ||
 271                     (pMechanism->ulParameterLen != BLOWFISH_BLOCK_LEN))
 272                         return (CKR_MECHANISM_PARAM_INVALID);
 273 
 274                 rv = soft_blowfish_crypt_init_common(session_p, pMechanism,


 276 
 277                 if (rv != CKR_OK)
 278                         return (rv);
 279 
 280                 (void) pthread_mutex_lock(&session_p->session_mutex);
 281 
 282                 soft_blowfish_ctx =
 283                     (soft_blowfish_ctx_t *)session_p->decrypt.context;
 284 
 285                 /* Save Initialization Vector in the context. */
 286                 (void) memcpy(soft_blowfish_ctx->ivec, pMechanism->pParameter,
 287                     BLOWFISH_BLOCK_LEN);
 288 
 289                 /* Allocate a context for CBC */
 290                 soft_blowfish_ctx->blowfish_cbc =
 291                     (void *)blowfish_cbc_ctx_init(soft_blowfish_ctx->key_sched,
 292                     soft_blowfish_ctx->keysched_len,
 293                     soft_blowfish_ctx->ivec);
 294 
 295                 if (soft_blowfish_ctx->blowfish_cbc == NULL) {
 296                         freezero(soft_blowfish_ctx->key_sched,
 297                             soft_blowfish_ctx->keysched_len);
 298                         freezero(session_p->decrypt.context,
 299                             sizeof (soft_blowfish_ctx_t));
 300                         session_p->decrypt.context = NULL;
 301                         (void) pthread_mutex_unlock(&session_p->session_mutex);
 302                         return (CKR_HOST_MEMORY);
 303                 }
 304 
 305                 (void) pthread_mutex_unlock(&session_p->session_mutex);
 306                 return (rv);
 307         }
 308 
 309         case CKM_RC4:
 310 
 311                 if (key_p->key_type != CKK_RC4) {
 312                         return (CKR_KEY_TYPE_INCONSISTENT);
 313                 }
 314 
 315                 return (soft_arcfour_crypt_init(session_p, pMechanism, key_p,
 316                     B_FALSE));
 317 
 318         case CKM_RSA_X_509:
 319         case CKM_RSA_PKCS:
 320 


 539         }
 540         switch (mechanism) {
 541 
 542         case CKM_DES_CBC_PAD:
 543         case CKM_DES3_CBC_PAD:
 544         {
 545 
 546                 soft_des_ctx_t *soft_des_ctx;
 547 
 548                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 549 
 550                 /*
 551                  * We should have only one block of data left in the
 552                  * remaining buffer.
 553                  */
 554                 if (soft_des_ctx->remain_len != DES_BLOCK_LEN) {
 555                         *pulLastPartLen = 0;
 556                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 557                         /* Cleanup memory space. */
 558                         free(soft_des_ctx->des_cbc);
 559                         freezero(soft_des_ctx->key_sched,
 560                             soft_des_ctx->keysched_len);

 561 
 562                         goto clean1;
 563                 }
 564 
 565                 out_len = DES_BLOCK_LEN;
 566 
 567                 /*
 568                  * If application asks for the length of the output buffer
 569                  * to hold the plaintext?
 570                  */
 571                 if (pLastPart == NULL) {
 572                         *pulLastPartLen = out_len;
 573                         rv = CKR_OK;
 574                         goto clean2;
 575                 } else {
 576                         crypto_data_t out;
 577 
 578                         /* Copy remaining data to the output buffer. */
 579                         (void) memcpy(pLastPart, soft_des_ctx->data,
 580                             DES_BLOCK_LEN);


 592 
 593                         if (rc == 0) {
 594                                 /*
 595                                  * Remove padding bytes after decryption of
 596                                  * ciphertext block to produce the original
 597                                  * plaintext.
 598                                  */
 599                                 rv = soft_remove_pkcs7_padding(pLastPart,
 600                                     DES_BLOCK_LEN, &out_len);
 601                                 if (rv != CKR_OK)
 602                                         *pulLastPartLen = 0;
 603                                 else
 604                                         *pulLastPartLen = out_len;
 605                         } else {
 606                                 *pulLastPartLen = 0;
 607                                 rv = CKR_FUNCTION_FAILED;
 608                         }
 609 
 610                         /* Cleanup memory space. */
 611                         free(soft_des_ctx->des_cbc);
 612                         freezero(soft_des_ctx->key_sched,
 613                             soft_des_ctx->keysched_len);

 614 
 615                 }
 616 
 617                 break;
 618         }
 619 
 620         case CKM_DES_CBC:
 621         case CKM_DES_ECB:
 622         case CKM_DES3_CBC:
 623         case CKM_DES3_ECB:
 624         {
 625 
 626                 soft_des_ctx_t *soft_des_ctx;
 627 
 628                 soft_des_ctx = (soft_des_ctx_t *)session_p->decrypt.context;
 629                 /*
 630                  * CKM_DES_CBC and CKM_DES_ECB does not do any padding,
 631                  * so when the final is called, the remaining buffer
 632                  * should not contain any more data.
 633                  */
 634                 *pulLastPartLen = 0;
 635                 if (soft_des_ctx->remain_len != 0) {
 636                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 637                 } else {
 638                         if (pLastPart == NULL)
 639                                 goto clean2;
 640                 }
 641 
 642                 /* Cleanup memory space. */
 643                 free(soft_des_ctx->des_cbc);
 644                 freezero(soft_des_ctx->key_sched,
 645                     soft_des_ctx->keysched_len);
 646 
 647                 break;
 648         }
 649 
 650         case CKM_AES_CBC_PAD:
 651         {
 652 
 653                 soft_aes_ctx_t *soft_aes_ctx;
 654 
 655                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 656 
 657                 /*
 658                  * We should have only one block of data left in the
 659                  * remaining buffer.
 660                  */
 661                 if (soft_aes_ctx->remain_len != AES_BLOCK_LEN) {
 662                         *pulLastPartLen = 0;
 663                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 664                         /* Cleanup memory space. */
 665                         free(soft_aes_ctx->aes_cbc);
 666                         freezero(soft_aes_ctx->key_sched,
 667                             soft_aes_ctx->keysched_len);

 668 
 669                         goto clean1;
 670                 }
 671 
 672                 out_len = AES_BLOCK_LEN;
 673 
 674                 /*
 675                  * If application asks for the length of the output buffer
 676                  * to hold the plaintext?
 677                  */
 678                 if (pLastPart == NULL) {
 679                         *pulLastPartLen = out_len;
 680                         rv = CKR_OK;
 681                         goto clean2;
 682                 } else {
 683                         crypto_data_t out;
 684 
 685                         /* Copy remaining data to the output buffer. */
 686                         (void) memcpy(pLastPart, soft_aes_ctx->data,
 687                             AES_BLOCK_LEN);


 699 
 700                         if (rc == 0) {
 701                                 /*
 702                                  * Remove padding bytes after decryption of
 703                                  * ciphertext block to produce the original
 704                                  * plaintext.
 705                                  */
 706                                 rv = soft_remove_pkcs7_padding(pLastPart,
 707                                     AES_BLOCK_LEN, &out_len);
 708                                 if (rv != CKR_OK)
 709                                         *pulLastPartLen = 0;
 710                                 else
 711                                         *pulLastPartLen = out_len;
 712                         } else {
 713                                 *pulLastPartLen = 0;
 714                                 rv = CKR_FUNCTION_FAILED;
 715                         }
 716 
 717                         /* Cleanup memory space. */
 718                         free(soft_aes_ctx->aes_cbc);
 719                         freezero(soft_aes_ctx->key_sched,
 720                             soft_aes_ctx->keysched_len);

 721 
 722                 }
 723 
 724                 break;
 725         }
 726 
 727         case CKM_AES_CBC:
 728         case CKM_AES_ECB:
 729         {
 730                 soft_aes_ctx_t *soft_aes_ctx;
 731 
 732                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 733                 /*
 734                  * CKM_AES_CBC and CKM_AES_ECB does not do any padding,
 735                  * so when the final is called, the remaining buffer
 736                  * should not contain any more data.
 737                  */
 738                 *pulLastPartLen = 0;
 739                 if (soft_aes_ctx->remain_len != 0) {
 740                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 741                 } else {
 742                         if (pLastPart == NULL)
 743                                 goto clean2;
 744                 }
 745 
 746                 /* Cleanup memory space. */
 747                 free(soft_aes_ctx->aes_cbc);
 748                 freezero(soft_aes_ctx->key_sched,
 749                     soft_aes_ctx->keysched_len);
 750 
 751                 break;
 752         }
 753         case CKM_AES_CTR:
 754         {
 755                 crypto_data_t out;
 756                 soft_aes_ctx_t *soft_aes_ctx;
 757                 ctr_ctx_t *ctr_ctx;
 758                 size_t len;
 759 
 760                 soft_aes_ctx = (soft_aes_ctx_t *)session_p->decrypt.context;
 761                 ctr_ctx = soft_aes_ctx->aes_cbc;
 762                 len = ctr_ctx->ctr_remainder_len;
 763                 if (pLastPart == NULL) {
 764                         *pulLastPartLen = len;
 765                         goto clean1;
 766                 }
 767                 if (len > 0) {
 768                         out.cd_format = CRYPTO_DATA_RAW;
 769                         out.cd_offset = 0;
 770                         out.cd_length = len;
 771                         out.cd_raw.iov_base = (char *)pLastPart;
 772                         out.cd_raw.iov_len = len;
 773 
 774                         rv = ctr_mode_final(ctr_ctx, &out, aes_encrypt_block);
 775                         if (rv == CRYPTO_DATA_LEN_RANGE)
 776                                 rv = CRYPTO_ENCRYPTED_DATA_LEN_RANGE;
 777                 }
 778                 if (rv == CRYPTO_BUFFER_TOO_SMALL) {
 779                         *pulLastPartLen = len;
 780                         goto clean1;
 781                 }
 782 
 783                 /* Cleanup memory space. */
 784                 free(ctr_ctx);
 785                 freezero(soft_aes_ctx->key_sched,
 786                     soft_aes_ctx->keysched_len);
 787 
 788                 break;
 789         }
 790         case CKM_BLOWFISH_CBC:
 791         {
 792                 soft_blowfish_ctx_t *soft_blowfish_ctx;
 793 
 794                 soft_blowfish_ctx =
 795                     (soft_blowfish_ctx_t *)session_p->decrypt.context;
 796 
 797                 *pulLastPartLen = 0;
 798                 if (soft_blowfish_ctx->remain_len != 0)
 799                         rv = CKR_ENCRYPTED_DATA_LEN_RANGE;
 800                 else {
 801                         if (pLastPart == NULL)
 802                                 goto clean2;
 803                 }
 804 
 805                 free(soft_blowfish_ctx->blowfish_cbc);
 806                 freezero(soft_blowfish_ctx->key_sched,
 807                     soft_blowfish_ctx->keysched_len);

 808 
 809                 break;
 810         }
 811 
 812         case CKM_RC4:
 813         {
 814                 ARCFour_key *key = (ARCFour_key *)session_p->decrypt.context;
 815                 explicit_bzero(key, sizeof (*key));
 816                 *pulLastPartLen = 0;
 817                 break;
 818         }
 819 
 820         default:
 821                 /* PKCS11: The mechanism only supports single-part operation. */
 822                 rv = CKR_MECHANISM_INVALID;
 823                 break;
 824         }
 825 
 826 clean1:
 827         free(session_p->decrypt.context);
 828         session_p->decrypt.context = NULL;
 829 
 830 clean2:
 831         (void) pthread_mutex_unlock(&session_p->session_mutex);
 832 
 833         return (rv);
 834 
 835 }