Print this page
4896 Performance improvements for KCF AES modes

Split Close
Expand all
Collapse all
          --- old/usr/src/common/crypto/modes/ecb.c
          +++ new/usr/src/common/crypto/modes/ecb.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*
  22   22   * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
       25 +/*
       26 + * Copyright 2015 by Saso Kiselkov. All rights reserved.
       27 + */
  25   28  
  26   29  #ifndef _KERNEL
  27   30  #include <strings.h>
  28   31  #include <limits.h>
  29   32  #include <assert.h>
  30   33  #include <security/cryptoki.h>
  31   34  #endif
  32   35  
  33   36  #include <sys/types.h>
       37 +#define INLINE_CRYPTO_GET_PTRS
  34   38  #include <modes/modes.h>
  35   39  #include <sys/crypto/common.h>
  36   40  #include <sys/crypto/impl.h>
  37   41  
       42 +boolean_t ecb_fastpath_enabled = B_TRUE;
       43 +
  38   44  /*
  39   45   * Algorithm independent ECB functions.
       46 + * `cipher' is a single-block version of the cipher function to be performed
       47 + * on each input block. `cipher_ecb' is an optional parameter, which if
       48 + * passed and the input/output conditions allow it, will be invoked for the
       49 + * entire input buffer once to accelerate the operation.
  40   50   */
  41   51  int
  42   52  ecb_cipher_contiguous_blocks(ecb_ctx_t *ctx, char *data, size_t length,
  43   53      crypto_data_t *out, size_t block_size,
  44      -    int (*cipher)(const void *ks, const uint8_t *pt, uint8_t *ct))
       54 +    int (*cipher)(const void *ks, const uint8_t *pt, uint8_t *ct),
       55 +    int (*cipher_ecb)(const void *ks, const uint8_t *pt, uint8_t *ct,
       56 +    uint64_t len))
  45   57  {
  46   58          size_t remainder = length;
  47   59          size_t need;
  48   60          uint8_t *datap = (uint8_t *)data;
  49   61          uint8_t *blockp;
  50   62          uint8_t *lastp;
  51   63          void *iov_or_mp;
  52   64          offset_t offset;
  53   65          uint8_t *out_data_1;
  54   66          uint8_t *out_data_2;
  55   67          size_t out_data_1_len;
  56   68  
       69 +        /*
       70 +         * ECB encryption/decryption fastpath requirements:
       71 +         * - fastpath is enabled
       72 +         * - caller passed an accelerated ECB version of the cipher function
       73 +         * - input is block-aligned
       74 +         * - output is a single contiguous region or the user requested that
       75 +         *   we overwrite their input buffer (input/output aliasing allowed)
       76 +         */
       77 +        if (ecb_fastpath_enabled && cipher_ecb != NULL &&
       78 +            ctx->ecb_remainder_len == 0 && length % block_size == 0 &&
       79 +            (out == NULL || CRYPTO_DATA_IS_SINGLE_BLOCK(out))) {
       80 +                if (out == NULL) {
       81 +                        cipher_ecb(ctx->ecb_keysched, (uint8_t *)data,
       82 +                            (uint8_t *)data, length);
       83 +                } else {
       84 +                        cipher_ecb(ctx->ecb_keysched, (uint8_t *)data,
       85 +                            CRYPTO_DATA_FIRST_BLOCK(out), length);
       86 +                        out->cd_offset += length;
       87 +                }
       88 +                return (CRYPTO_SUCCESS);
       89 +        }
       90 +
  57   91          if (length + ctx->ecb_remainder_len < block_size) {
  58   92                  /* accumulate bytes here and return */
  59   93                  bcopy(datap,
  60   94                      (uint8_t *)ctx->ecb_remainder + ctx->ecb_remainder_len,
  61   95                      length);
  62   96                  ctx->ecb_remainder_len += length;
  63   97                  ctx->ecb_copy_to = datap;
  64   98                  return (CRYPTO_SUCCESS);
  65   99          }
  66  100  
↓ open down ↓ 88 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX