Print this page
6189743 Need an ARCFOUR implementation optimized for AMD64
   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, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "@(#)arcfour_crypt.c    1.5     05/08/23 SMI"
  28 
  29 #include "arcfour.h"
  30 








  31 /* Initialize the key stream 'key' using the key value */
  32 void
  33 arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
  34 {
  35 /* EXPORT DELETE START */
  36 
  37         uchar_t ext_keyval[256];
  38         uchar_t tmp;
  39         int i, j;
  40 
  41         for (i = j = 0; i < 256; i++, j++) {
  42                 if (j == keyvallen)
  43                         j = 0;
  44 
  45                 ext_keyval[i] = keyval[j];
  46         }
  47         for (i = 0; i < 256; i++)
  48                 key->arr[i] = (uchar_t)i;
  49 
  50         j = 0;
  51         for (i = 0; i < 256; i++) {
  52                 j = (j + key->arr[i] + ext_keyval[i]) % 256;
  53                 tmp = key->arr[i];
  54                 key->arr[i] = key->arr[j];
  55                 key->arr[j] = tmp;
  56         }
  57         key->i = 0;
  58         key->j = 0;
  59 
  60 /* EXPORT DELETE END */
  61 }
  62 
  63 

  64 /*
  65  * Encipher 'in' using 'key.
  66  * in and out can point to the same location
  67  */
  68 void
  69 arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
  70 {
  71         size_t ii;
  72         uchar_t tmp, i, j;
  73 
  74 /* EXPORT DELETE START */
  75 
  76         /*
  77          * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for
  78          * the cases where the input and output  buffers are aligned on
  79          * a multiple of 8-byte boundary.
  80          */
  81 #ifdef  sun4u
  82         int index;
  83 
  84         index = (((uint64_t)(uintptr_t)in) & 0x7);
  85 


 113                 j = key->j;
 114                 for (ii = 0; ii < len; ii++) {
 115                         i = i + 1;
 116                         j = j + key->arr[i];
 117                         tmp = key->arr[i];
 118                         key->arr[i] = key->arr[j];
 119                         key->arr[j] = tmp;
 120                         tmp = key->arr[i] + key->arr[j];
 121                         out[ii] = in[ii] ^ key->arr[tmp];
 122                 }
 123                 key->i = i;
 124                 key->j = j;
 125 #ifdef  sun4u
 126         } else {
 127                 arcfour_crypt_aligned(key, len, in, out);
 128         }
 129 #endif  /* sun4u */
 130 
 131 /* EXPORT DELETE END */
 132 }

   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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #pragma ident   "@(#)arcfour_crypt.c    1.6     08/01/02 SMI"
  27 
  28 #include "arcfour.h"
  29 
  30 #if defined(__amd64)
  31 /*
  32  * Use hand-tuned, processor-specific assembly version of arcfour_crypt()
  33  * for 64-bit x86:
  34  */
  35 #define USE_PSR_VERSION_OF_ARCFOUR_CRYPT
  36 #endif /* __amd64 */
  37 
  38 /* Initialize the key stream 'key' using the key value */
  39 void
  40 arcfour_key_init(ARCFour_key *key, uchar_t *keyval, int keyvallen)
  41 {
  42 /* EXPORT DELETE START */
  43 
  44         uchar_t ext_keyval[256];
  45         uchar_t tmp;
  46         int i, j;
  47 
  48         for (i = j = 0; i < 256; i++, j++) {
  49                 if (j == keyvallen)
  50                         j = 0;
  51 
  52                 ext_keyval[i] = keyval[j];
  53         }
  54         for (i = 0; i < 256; i++)
  55                 key->arr[i] = (uchar_t)i;
  56 
  57         j = 0;
  58         for (i = 0; i < 256; i++) {
  59                 j = (j + key->arr[i] + ext_keyval[i]) % 256;
  60                 tmp = key->arr[i];
  61                 key->arr[i] = key->arr[j];
  62                 key->arr[j] = tmp;
  63         }
  64         key->i = 0;
  65         key->j = 0;
  66 
  67 /* EXPORT DELETE END */
  68 }
  69 
  70 
  71 #if !defined(USE_PSR_VERSION_OF_ARCFOUR_CRYPT)
  72 /*
  73  * Encipher 'in' using 'key'.
  74  * in and out can point to the same location
  75  */
  76 void
  77 arcfour_crypt(ARCFour_key *key, uchar_t *in, uchar_t *out, size_t len)
  78 {
  79         size_t ii;
  80         uchar_t tmp, i, j;
  81 
  82 /* EXPORT DELETE START */
  83 
  84         /*
  85          * The sun4u has a version of arcfour_crypt_aligned() hand-tuned for
  86          * the cases where the input and output  buffers are aligned on
  87          * a multiple of 8-byte boundary.
  88          */
  89 #ifdef  sun4u
  90         int index;
  91 
  92         index = (((uint64_t)(uintptr_t)in) & 0x7);
  93 


 121                 j = key->j;
 122                 for (ii = 0; ii < len; ii++) {
 123                         i = i + 1;
 124                         j = j + key->arr[i];
 125                         tmp = key->arr[i];
 126                         key->arr[i] = key->arr[j];
 127                         key->arr[j] = tmp;
 128                         tmp = key->arr[i] + key->arr[j];
 129                         out[ii] = in[ii] ^ key->arr[tmp];
 130                 }
 131                 key->i = i;
 132                 key->j = j;
 133 #ifdef  sun4u
 134         } else {
 135                 arcfour_crypt_aligned(key, len, in, out);
 136         }
 137 #endif  /* sun4u */
 138 
 139 /* EXPORT DELETE END */
 140 }
 141 #endif  /* !USE_PSR_VERSION_OF_ARCFOUR_CRYPT */