8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / pkcs11 / pkcs11_softtoken / common / softARCFourCrypt.c
blob4c869c7789149b2737fa9f5aae5148f415d70d17
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 #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 */
60 if ((keyvallen < ARCFOUR_MIN_KEY_BYTES) ||
61 (keyvallen > ARCFOUR_MAX_KEY_BYTES))
62 return (CKR_KEY_SIZE_RANGE);
64 keyval = OBJ_SEC_VALUE(key_p);
66 if (keyval == NULL)
67 return (CKR_KEY_TYPE_INCONSISTENT);
69 keystream = malloc(sizeof (ARCFour_key));
70 if (keystream == NULL) {
71 return (CKR_HOST_MEMORY);
73 arcfour_key_init(keystream, keyval, keyvallen);
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);
81 return (CKR_OK);
86 * soft_arcfour_crypt()
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
95 * Description:
96 * Encrypts/Decrypts the 'input' and gets the result in the 'output'
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
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)
109 ARCFour_key *keystream = active_op->context;
111 if (keystream == NULL) {
112 return (CKR_ARGUMENTS_BAD);
116 * If application asks for the length of the output buffer
117 * to hold the transformed text
119 if (output == NULL) {
120 *outputlenp = inputlen;
121 return (CKR_OK);
124 /* Is the application-supplied buffer large enough? */
125 if (*outputlenp < inputlen) {
126 *outputlenp = inputlen;
127 return (CKR_BUFFER_TOO_SMALL);
129 arcfour_crypt(keystream, input, output, inputlen);
130 *outputlenp = inputlen;
132 return (CKR_OK);