613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * CDDL HEADER START
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * The contents of this file are subject to the terms of the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Common Development and Distribution License (the "License").
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * You may not use this file except in compliance with the License.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * See the License for the specific language governing permissions
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * and limitations under the License.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * When distributing Covered Code, include this CDDL HEADER in each
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * If applicable, add the following below this CDDL HEADER, with the
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * fields enclosed by brackets "[]" replaced with your own identifying
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * information: Portions Copyright [yyyy] [name of copyright owner]
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * CDDL HEADER END
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Use is subject to license terms.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Crypto support, using libpkcs11
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Some code copied from the server: libsmb smb_crypt.c
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * with minor changes, i.e. errno.h return values.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * XXX: Later, make the server use these.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rosssmb_initlmkey(uchar_t *keyout, const uchar_t *keyin);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Like libsmb smb_auth_DES,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * but use uchar_t, return errno.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Calculate proper number of iterations.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Known call cases include:
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * ResultLen=16, KeyLen=14, DataLen=8
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * ResultLen=24, KeyLen=21, DataLen=8
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * ResultLen=16, KeyLen=14, DataLen=16
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross if (K == 0 || D == 0)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Use SUNW convenience function to initialize the cryptoki
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * library, and open a session with a slot that supports
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * the mechanism we plan on using.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross rv = SUNW_C_GetMechSession(mechanism.mechanism, &hSession);
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross for (d = k = 0; k < K; k++, d++) {
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross /* Cycle the input again, as necessary. */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross rv = SUNW_C_KeyToObject(hSession, mechanism.mechanism,
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /* Initialize the encryption operation in the session */
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross /* Read in the data and encrypt this portion */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * See "Netlogon Credential Computation" section of MS-NRPC document.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Same as in libsmb, but output arg first.
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Rosssmb_initlmkey(uchar_t *keyout, const uchar_t *keyin)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[1] = ((keyin[0] & 0x01) << 6) | (keyin[1] >> 2);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[2] = ((keyin[1] & 0x03) << 5) | (keyin[2] >> 3);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[3] = ((keyin[2] & 0x07) << 4) | (keyin[3] >> 4);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[4] = ((keyin[3] & 0x0f) << 3) | (keyin[4] >> 5);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[5] = ((keyin[4] & 0x1f) << 2) | (keyin[5] >> 6);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross keyout[6] = ((keyin[5] & 0x3f) << 1) | (keyin[6] >> 7);
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross for (i = 0; i < 8; i++)
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * Use SUNW convenience function to initialize the cryptoki
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * library, and open a session with a slot that supports
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross * the mechanism we plan on using.
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross rv = SUNW_C_GetMechSession(mechanism.mechanism, &hSession);
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross rv = SUNW_C_KeyToObject(hSession, mechanism.mechanism,
85e6b6747d07050e01ec91acef2453655821f9abGordon Ross /* Initialize the encryption operation in the session */
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Get some random bytes from /dev/urandom
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * There may be a preferred way to call this via libpkcs11
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * XXX: (see: C_GenerateRandom, etc. -- later...)
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * Just read from /dev/urandom for now.