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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.

  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 "softSession.h"
  33 #include "softObject.h"
  34 #include "softCrypt.h"
  35 #include <blowfish_impl.h>
  36 
  37 CK_RV
  38 soft_blowfish_crypt_init_common(soft_session_t *session_p,
  39     CK_MECHANISM_PTR pMechanism, soft_object_t *key_p, boolean_t encrypt) {
  40 
  41         size_t size;
  42         soft_blowfish_ctx_t *soft_blowfish_ctx;
  43 


 280                         /*
 281                          * For encrypt update, if there is remaining data,
 282                          * save it and it's length in the context.
 283                          */
 284                         if (remain != 0)
 285                                 (void) memcpy(soft_blowfish_ctx->data, pData +
 286                                     (ulDataLen - remain), remain);
 287 
 288                         soft_blowfish_ctx->remain_len = remain;
 289                         return (CKR_OK);
 290                 }
 291 
 292         } else {
 293                 *pulEncryptedLen = 0;
 294                 rv = CKR_FUNCTION_FAILED;
 295         }
 296 
 297 cleanup:
 298         (void) pthread_mutex_lock(&session_p->session_mutex);
 299         blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
 300         if (blowfish_ctx != NULL) {
 301                 bzero(blowfish_ctx->bc_keysched,
 302                     blowfish_ctx->bc_keysched_len);
 303                 free(soft_blowfish_ctx->blowfish_cbc);
 304         }
 305 
 306         bzero(soft_blowfish_ctx->key_sched, soft_blowfish_ctx->keysched_len);
 307         free(soft_blowfish_ctx->key_sched);
 308         free(session_p->encrypt.context);
 309         session_p->encrypt.context = NULL;
 310         (void) pthread_mutex_unlock(&session_p->session_mutex);
 311 
 312         return (rv);
 313 }
 314 
 315 
 316 CK_RV
 317 soft_blowfish_decrypt_common(soft_session_t *session_p, CK_BYTE_PTR pEncrypted,
 318     CK_ULONG ulEncryptedLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen,
 319     boolean_t update) {
 320 
 321         int rc = 0;
 322         CK_RV rv = CKR_OK;
 323         soft_blowfish_ctx_t *soft_blowfish_ctx =
 324             (soft_blowfish_ctx_t *)session_p->decrypt.context;
 325         blowfish_ctx_t *blowfish_ctx;
 326         CK_BYTE *in_buf = NULL;
 327         CK_BYTE *out_buf = NULL;
 328         CK_ULONG out_len;


 448                          * For decrypt update, if there is remaining data,
 449                          * save it and its length in the context.
 450                          */
 451                         if (remain != 0)
 452                                 (void) memcpy(soft_blowfish_ctx->data,
 453                                     pEncrypted + (ulEncryptedLen - remain),
 454                                     remain);
 455                         soft_blowfish_ctx->remain_len = remain;
 456                         return (CKR_OK);
 457                 }
 458 
 459 
 460         } else {
 461                 *pulDataLen = 0;
 462                 rv = CKR_FUNCTION_FAILED;
 463         }
 464 
 465 cleanup:
 466         (void) pthread_mutex_lock(&session_p->session_mutex);
 467         blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
 468         if (blowfish_ctx != NULL) {
 469                 bzero(blowfish_ctx->bc_keysched,
 470                     blowfish_ctx->bc_keysched_len);
 471                 free(soft_blowfish_ctx->blowfish_cbc);
 472         }
 473 
 474         bzero(soft_blowfish_ctx->key_sched, soft_blowfish_ctx->keysched_len);
 475         free(soft_blowfish_ctx->key_sched);
 476         free(session_p->decrypt.context);
 477         session_p->decrypt.context = NULL;
 478         (void) pthread_mutex_unlock(&session_p->session_mutex);
 479 
 480         return (rv);
 481 }
 482 
 483 /*
 484  * Allocate and initialize a context for BLOWFISH CBC mode of operation.
 485  */
 486 
 487 void *
 488 blowfish_cbc_ctx_init(void *key_sched, size_t size, uint8_t *ivec)
 489 {
 490 
 491         cbc_ctx_t *cbc_ctx;
 492 
 493         if ((cbc_ctx = calloc(1, sizeof (cbc_ctx_t))) == NULL)
 494                 return (NULL);
 495 
 496         cbc_ctx->cbc_keysched = key_sched;


   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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  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 "softSession.h"
  34 #include "softObject.h"
  35 #include "softCrypt.h"
  36 #include <blowfish_impl.h>
  37 
  38 CK_RV
  39 soft_blowfish_crypt_init_common(soft_session_t *session_p,
  40     CK_MECHANISM_PTR pMechanism, soft_object_t *key_p, boolean_t encrypt) {
  41 
  42         size_t size;
  43         soft_blowfish_ctx_t *soft_blowfish_ctx;
  44 


 281                         /*
 282                          * For encrypt update, if there is remaining data,
 283                          * save it and it's length in the context.
 284                          */
 285                         if (remain != 0)
 286                                 (void) memcpy(soft_blowfish_ctx->data, pData +
 287                                     (ulDataLen - remain), remain);
 288 
 289                         soft_blowfish_ctx->remain_len = remain;
 290                         return (CKR_OK);
 291                 }
 292 
 293         } else {
 294                 *pulEncryptedLen = 0;
 295                 rv = CKR_FUNCTION_FAILED;
 296         }
 297 
 298 cleanup:
 299         (void) pthread_mutex_lock(&session_p->session_mutex);
 300         blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
 301         freezero(blowfish_ctx, sizeof (cbc_ctx_t));
 302         freezero(soft_blowfish_ctx->key_sched,
 303             soft_blowfish_ctx->keysched_len);
 304         freezero(session_p->encrypt.context,
 305             sizeof (soft_blowfish_ctx_t));




 306         session_p->encrypt.context = NULL;
 307         (void) pthread_mutex_unlock(&session_p->session_mutex);
 308 
 309         return (rv);
 310 }
 311 
 312 
 313 CK_RV
 314 soft_blowfish_decrypt_common(soft_session_t *session_p, CK_BYTE_PTR pEncrypted,
 315     CK_ULONG ulEncryptedLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen,
 316     boolean_t update) {
 317 
 318         int rc = 0;
 319         CK_RV rv = CKR_OK;
 320         soft_blowfish_ctx_t *soft_blowfish_ctx =
 321             (soft_blowfish_ctx_t *)session_p->decrypt.context;
 322         blowfish_ctx_t *blowfish_ctx;
 323         CK_BYTE *in_buf = NULL;
 324         CK_BYTE *out_buf = NULL;
 325         CK_ULONG out_len;


 445                          * For decrypt update, if there is remaining data,
 446                          * save it and its length in the context.
 447                          */
 448                         if (remain != 0)
 449                                 (void) memcpy(soft_blowfish_ctx->data,
 450                                     pEncrypted + (ulEncryptedLen - remain),
 451                                     remain);
 452                         soft_blowfish_ctx->remain_len = remain;
 453                         return (CKR_OK);
 454                 }
 455 
 456 
 457         } else {
 458                 *pulDataLen = 0;
 459                 rv = CKR_FUNCTION_FAILED;
 460         }
 461 
 462 cleanup:
 463         (void) pthread_mutex_lock(&session_p->session_mutex);
 464         blowfish_ctx = (blowfish_ctx_t *)soft_blowfish_ctx->blowfish_cbc;
 465         free(blowfish_ctx);
 466         freezero(soft_blowfish_ctx->key_sched,
 467             soft_blowfish_ctx->keysched_len);
 468         freezero(session_p->decrypt.context,
 469             sizeof (soft_blowfish_ctx_t));




 470         session_p->decrypt.context = NULL;
 471         (void) pthread_mutex_unlock(&session_p->session_mutex);
 472 
 473         return (rv);
 474 }
 475 
 476 /*
 477  * Allocate and initialize a context for BLOWFISH CBC mode of operation.
 478  */
 479 
 480 void *
 481 blowfish_cbc_ctx_init(void *key_sched, size_t size, uint8_t *ivec)
 482 {
 483 
 484         cbc_ctx_t *cbc_ctx;
 485 
 486         if ((cbc_ctx = calloc(1, sizeof (cbc_ctx_t))) == NULL)
 487                 return (NULL);
 488 
 489         cbc_ctx->cbc_keysched = key_sched;