554ff184129088135ad2643c1c9832174a17be88kais * CDDL HEADER START
554ff184129088135ad2643c1c9832174a17be88kais * The contents of this file are subject to the terms of the
b6c3f7863936abeae522e48a13887dddeb691a45bubbva * Common Development and Distribution License (the "License").
b6c3f7863936abeae522e48a13887dddeb691a45bubbva * You may not use this file except in compliance with the License.
554ff184129088135ad2643c1c9832174a17be88kais * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
554ff184129088135ad2643c1c9832174a17be88kais * See the License for the specific language governing permissions
554ff184129088135ad2643c1c9832174a17be88kais * and limitations under the License.
554ff184129088135ad2643c1c9832174a17be88kais * When distributing Covered Code, include this CDDL HEADER in each
554ff184129088135ad2643c1c9832174a17be88kais * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
554ff184129088135ad2643c1c9832174a17be88kais * If applicable, add the following below this CDDL HEADER, with the
554ff184129088135ad2643c1c9832174a17be88kais * fields enclosed by brackets "[]" replaced with your own identifying
554ff184129088135ad2643c1c9832174a17be88kais * information: Portions Copyright [yyyy] [name of copyright owner]
554ff184129088135ad2643c1c9832174a17be88kais * CDDL HEADER END
726fad2a65f16c200a03969c29cb5c86c2d427dbDina K Nimeh * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
554ff184129088135ad2643c1c9832174a17be88kais#include "../arcfour.h"
554ff184129088135ad2643c1c9832174a17be88kais/* Initialize the key stream 'key' using the key value */
554ff184129088135ad2643c1c9832174a17be88kaisarcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
554ff184129088135ad2643c1c9832174a17be88kais for (i = j = 0; i < 256; i++, j++) {
554ff184129088135ad2643c1c9832174a17be88kais for (i = 0; i < 256; i++)
554ff184129088135ad2643c1c9832174a17be88kais for (i = 0; i < 256; i++) {
554ff184129088135ad2643c1c9832174a17be88kais * Encipher 'in' using 'key.
554ff184129088135ad2643c1c9832174a17be88kais * in and out can point to the same location
554ff184129088135ad2643c1c9832174a17be88kaisarcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino unsigned long long in0, merge = 0, merge0 = 0, merge1, mask = 0;
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino unsigned int tmp0, tmp1, i_accum, shift = 0, i1;
554ff184129088135ad2643c1c9832174a17be88kais /* Get the 'in' on an 8-byte alignment */
554ff184129088135ad2643c1c9832174a17be88kais i = i + 1;
554ff184129088135ad2643c1c9832174a17be88kais /* See if we're fortunate and 'out' got aligned as well */
554ff184129088135ad2643c1c9832174a17be88kais * Niagara optimized version for
554ff184129088135ad2643c1c9832174a17be88kais * the cases where the input and output buffers are aligned on
554ff184129088135ad2643c1c9832174a17be88kais * a multiple of 8-byte boundary.
554ff184129088135ad2643c1c9832174a17be88kais#endif /* sun4v */
554ff184129088135ad2643c1c9832174a17be88kais i = i + 1;
554ff184129088135ad2643c1c9832174a17be88kais * Want to align base[i] on a 2B boundary -- allows updates
554ff184129088135ad2643c1c9832174a17be88kais * via [i] to be performed in 2B chunks (reducing # of stores).
554ff184129088135ad2643c1c9832174a17be88kais * Requires appropriate alias detection.
554ff184129088135ad2643c1c9832174a17be88kais i = i + 1;
554ff184129088135ad2643c1c9832174a17be88kais * Note - in and out may now be misaligned -
554ff184129088135ad2643c1c9832174a17be88kais * as updating [out] in 8B chunks need to handle this
554ff184129088135ad2643c1c9832174a17be88kais * possibility. Also could have a 1B overrun.
554ff184129088135ad2643c1c9832174a17be88kais * Need to drop out of loop early as a result.
554ff184129088135ad2643c1c9832174a17be88kais * If i < less than 248, know wont wrap around
554ff184129088135ad2643c1c9832174a17be88kais * (i % 256), so don't need to bother with masking i
554ff184129088135ad2643c1c9832174a17be88kais * after each increment
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 0 */
554ff184129088135ad2643c1c9832174a17be88kais * Creating this base pointer reduces subsequent
554ff184129088135ad2643c1c9832174a17be88kais * arihmetic ops required to load [i]
554ff184129088135ad2643c1c9832174a17be88kais * N.B. don't need to check if [j] aliases.
554ff184129088135ad2643c1c9832174a17be88kais * [i] and [j] end up with the same values
554ff184129088135ad2643c1c9832174a17be88kais * Don't store [i] yet
554ff184129088135ad2643c1c9832174a17be88kais * Check [tmp0] doesn't alias with [i]
554ff184129088135ad2643c1c9832174a17be88kais * Updating [out] in 8B chunks
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 1 */
554ff184129088135ad2643c1c9832174a17be88kais * [j] can now alias with [i] and [i-1]
554ff184129088135ad2643c1c9832174a17be88kais * If alias abort speculation
554ff184129088135ad2643c1c9832174a17be88kais merge |= (unsigned long long)
554ff184129088135ad2643c1c9832174a17be88kais * Speculation suceeded! Update [i]
554ff184129088135ad2643c1c9832174a17be88kais * in 2B chunk
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
554ff184129088135ad2643c1c9832174a17be88kais * Too expensive to perform [i] speculation for
554ff184129088135ad2643c1c9832174a17be88kais * every byte. Just need to reduce frequency
554ff184129088135ad2643c1c9832174a17be88kais * of stores until store buffer full stalls
554ff184129088135ad2643c1c9832174a17be88kais * are not the bottleneck.
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 2 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 3 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 4 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 5 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 6 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 7 */
554ff184129088135ad2643c1c9832174a17be88kais * Perform [i] speculation again. Indentical
554ff184129088135ad2643c1c9832174a17be88kais * to that performed for BYTE0 and BYTE1.
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
554ff184129088135ad2643c1c9832174a17be88kais * i is too close to wrap-around to allow
554ff184129088135ad2643c1c9832174a17be88kais * masking to be disregarded
554ff184129088135ad2643c1c9832174a17be88kais * Same old speculation for BYTE 0 and BYTE 1
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 0 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 1 */
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 2 */
554ff184129088135ad2643c1c9832174a17be88kais * As know i must be even when enter loop (to
554ff184129088135ad2643c1c9832174a17be88kais * satisfy alignment), can only wrap around
554ff184129088135ad2643c1c9832174a17be88kais * on the even bytes. So just need to perform
554ff184129088135ad2643c1c9832174a17be88kais * mask every 2nd byte
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 3 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 4 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 5 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 6 */
554ff184129088135ad2643c1c9832174a17be88kais /* BYTE 7 */
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
554ff184129088135ad2643c1c9832174a17be88kais * Perform update to [out]
554ff184129088135ad2643c1c9832174a17be88kais * Remember could be alignment issues
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
71269a2275bf5a143dad6461eee2710a344e7261Anthony Scarpino /* LINTED E_BAD_PTR_CAST_ALIGN */
554ff184129088135ad2643c1c9832174a17be88kais * Handle any overrun
554ff184129088135ad2643c1c9832174a17be88kais * Handle final few bytes
554ff184129088135ad2643c1c9832174a17be88kais i = i + 1;
554ff184129088135ad2643c1c9832174a17be88kais#endif /* sun4v */