Merge commit '06307114472bd8aad5ff18ccdb8e25f128ae6652'
[unleashed.git] / usr / src / lib / pkcs11 / pkcs11_softtoken / common / softARCFourCrypt.c
blob98f74184c57296c737a7aacf8c11636278cb423f
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
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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"
40 * Allocate the ARCFour key stream for the active encryption or decryption
41 * operation.
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)
48 uint8_t *keyval;
49 int keyvallen;
50 ARCFour_key *keystream;
51 crypto_active_op_t *active_op;
53 keyvallen = OBJ_SEC_VALUE_LEN(key_p);
55 if ((keyvallen < ARCFOUR_MIN_KEY_BYTES) ||
56 (keyvallen > ARCFOUR_MAX_KEY_BYTES))
57 return (CKR_KEY_SIZE_RANGE);
59 keyval = OBJ_SEC_VALUE(key_p);
61 if (keyval == NULL)
62 return (CKR_KEY_TYPE_INCONSISTENT);
64 keystream = malloc(sizeof (ARCFour_key));
65 if (keystream == NULL) {
66 return (CKR_HOST_MEMORY);
68 arcfour_key_init(keystream, keyval, keyvallen);
70 (void) pthread_mutex_lock(&session_p->session_mutex);
71 active_op = (encrypt) ? &(session_p->encrypt) : &(session_p->decrypt);
72 active_op->context = keystream;
73 active_op->mech.mechanism = pMechanism->mechanism;
74 (void) pthread_mutex_unlock(&session_p->session_mutex);
76 return (CKR_OK);
81 * soft_arcfour_crypt()
83 * Arguments:
84 * active_op: pointer to the active operation in the session
85 * input: pointer to the input data to be transformed
86 * inputlen: length of the input.
87 * output: pointer to the output storage.
88 * outputlenp: pointer to the length of the output
90 * Description:
91 * Encrypts/Decrypts the 'input' and gets the result in the 'output'
93 * Returns:
94 * CKR_OK: success
95 * CKR_BUFFER_TOO_SMALL: the output buffer provided by application
96 * is too small
97 * CKR_ARGUMENTS_BAD: keystream is a NULL pointer, cipher is not
98 * initialized
100 CK_RV
101 soft_arcfour_crypt(crypto_active_op_t *active_op, CK_BYTE_PTR input,
102 CK_ULONG inputlen, CK_BYTE_PTR output, CK_ULONG_PTR outputlenp)
104 ARCFour_key *keystream = active_op->context;
106 if (keystream == NULL) {
107 return (CKR_ARGUMENTS_BAD);
111 * If application asks for the length of the output buffer
112 * to hold the transformed text
114 if (output == NULL) {
115 *outputlenp = inputlen;
116 return (CKR_OK);
119 /* Is the application-supplied buffer large enough? */
120 if (*outputlenp < inputlen) {
121 *outputlenp = inputlen;
122 return (CKR_BUFFER_TOO_SMALL);
124 arcfour_crypt(keystream, input, output, inputlen);
125 *outputlenp = inputlen;
127 return (CKR_OK);