1 /* 2 * CDDL HEADER START 3 * 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 2010 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 <security/pkcs11.h> 33 #include <arcfour.h> 34 #include "softSession.h" 35 #include "softObject.h" 36 #include "softCrypt.h" 37 38 39 /* 40 * Allocate the ARCFour key stream for the active encryption or decryption 41 * operation. 42 */ 43 CK_RV 44 soft_arcfour_crypt_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism, 45 soft_object_t *key_p, boolean_t encrypt) 46 { 47 48 uint8_t *keyval; 49 int keyvallen; 50 ARCFour_key *keystream; 51 crypto_active_op_t *active_op; 52 53 #ifdef __sparcv9 54 /* LINTED */ 55 keyvallen = (int)OBJ_SEC_VALUE_LEN(key_p); 56 #else /* !__sparcv9 */ 57 keyvallen = OBJ_SEC_VALUE_LEN(key_p); 58 #endif /* __sparcv9 */ 59 60 if ((keyvallen < ARCFOUR_MIN_KEY_BYTES) || 61 (keyvallen > ARCFOUR_MAX_KEY_BYTES)) 62 return (CKR_KEY_SIZE_RANGE); 63 64 keyval = OBJ_SEC_VALUE(key_p); 65 66 if (keyval == NULL) 67 return (CKR_KEY_TYPE_INCONSISTENT); 68 69 keystream = malloc(sizeof (ARCFour_key)); 70 if (keystream == NULL) { 71 return (CKR_HOST_MEMORY); 72 } 73 arcfour_key_init(keystream, keyval, keyvallen); 74 75 (void) pthread_mutex_lock(&session_p->session_mutex); 76 active_op = (encrypt) ? &(session_p->encrypt) : &(session_p->decrypt); 77 active_op->context = keystream; 78 active_op->mech.mechanism = pMechanism->mechanism; 79 (void) pthread_mutex_unlock(&session_p->session_mutex); 80 81 return (CKR_OK); 82 } 83 84 85 /* 86 * soft_arcfour_crypt() 87 * 88 * Arguments: 89 * active_op: pointer to the active operation in the session 90 * input: pointer to the input data to be transformed 91 * inputlen: length of the input. 92 * output: pointer to the output storage. 93 * outputlenp: pointer to the length of the output 94 * 95 * Description: 96 * Encrypts/Decrypts the 'input' and gets the result in the 'output' 97 * 98 * Returns: 99 * CKR_OK: success 100 * CKR_BUFFER_TOO_SMALL: the output buffer provided by application 101 * is too small 102 * CKR_ARGUMENTS_BAD: keystream is a NULL pointer, cipher is not 103 * initialized 104 */ 105 CK_RV 106 soft_arcfour_crypt(crypto_active_op_t *active_op, CK_BYTE_PTR input, 107 CK_ULONG inputlen, CK_BYTE_PTR output, CK_ULONG_PTR outputlenp) 108 { 109 ARCFour_key *keystream = active_op->context; 110 111 if (keystream == NULL) { 112 return (CKR_ARGUMENTS_BAD); 113 } 114 115 /* 116 * If application asks for the length of the output buffer 117 * to hold the transformed text 118 */ 119 if (output == NULL) { 120 *outputlenp = inputlen; 121 return (CKR_OK); 122 } 123 124 /* Is the application-supplied buffer large enough? */ 125 if (*outputlenp < inputlen) { 126 *outputlenp = inputlen; 127 return (CKR_BUFFER_TOO_SMALL); 128 } 129 arcfour_crypt(keystream, input, output, inputlen); 130 *outputlenp = inputlen; 131 132 return (CKR_OK); 133 } 134