Print this page
5007142 Add ntohll and htonll to sys/byteorder.h
6717509 Need to use bswap/bswapq for byte swap of 64-bit integer on x32/x64
PSARC 2008/474

Split Close
Expand all
Collapse all
          --- old/usr/src/common/crypto/modes/ctr.c
          +++ new/usr/src/common/crypto/modes/ctr.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  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   25  
  26      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  27      -
  28   26  #ifndef _KERNEL
  29   27  #include <strings.h>
  30   28  #include <limits.h>
  31   29  #include <assert.h>
  32   30  #include <security/cryptoki.h>
  33   31  #endif
  34   32  
  35   33  #include <sys/types.h>
  36   34  #include <modes/modes.h>
  37   35  #include <sys/crypto/common.h>
  38   36  #include <sys/crypto/impl.h>
  39   37  
       38 +#ifdef _LITTLE_ENDIAN
       39 +#include <sys/byteorder.h>
       40 +#endif
       41 +
  40   42  /*
  41   43   * Encrypt and decrypt multiple blocks of data in counter mode.
  42   44   */
  43   45  int
  44   46  ctr_mode_contiguous_blocks(ctr_ctx_t *ctx, char *data, size_t length,
  45   47      crypto_data_t *out, size_t block_size,
  46   48      int (*cipher)(const void *ks, const uint8_t *pt, uint8_t *ct),
  47   49      void (*xor_block)(uint8_t *, uint8_t *))
  48   50  {
  49   51          size_t remainder = length;
↓ open down ↓ 44 lines elided ↑ open up ↑
  94   96                  /* ctr_cb is the counter block */
  95   97                  cipher(ctx->ctr_keysched, (uint8_t *)ctx->ctr_cb,
  96   98                      (uint8_t *)ctx->ctr_tmp);
  97   99  
  98  100                  lastp = (uint8_t *)ctx->ctr_tmp;
  99  101  
 100  102                  /*
 101  103                   * Increment counter. Counter bits are confined
 102  104                   * to the bottom 64 bits of the counter block.
 103  105                   */
 104      -                counter = ctx->ctr_cb[1] & ctx->ctr_counter_mask;
 105  106  #ifdef _LITTLE_ENDIAN
 106      -                p = (uint8_t *)&counter;
 107      -                counter = (((uint64_t)p[0] << 56) |
 108      -                    ((uint64_t)p[1] << 48) |
 109      -                    ((uint64_t)p[2] << 40) |
 110      -                    ((uint64_t)p[3] << 32) |
 111      -                    ((uint64_t)p[4] << 24) |
 112      -                    ((uint64_t)p[5] << 16) |
 113      -                    ((uint64_t)p[6] << 8) |
 114      -                    (uint64_t)p[7]);
 115      -#endif
      107 +                counter = ntohll(ctx->ctr_cb[1] & ctx->ctr_counter_mask);
      108 +                counter = htonll(counter + 1);
      109 +#else
      110 +                counter = ctx->ctr_cb[1] & ctx->ctr_counter_mask;
 116  111                  counter++;
 117      -#ifdef _LITTLE_ENDIAN
 118      -                counter = (((uint64_t)p[0] << 56) |
 119      -                    ((uint64_t)p[1] << 48) |
 120      -                    ((uint64_t)p[2] << 40) |
 121      -                    ((uint64_t)p[3] << 32) |
 122      -                    ((uint64_t)p[4] << 24) |
 123      -                    ((uint64_t)p[5] << 16) |
 124      -                    ((uint64_t)p[6] << 8) |
 125      -                    (uint64_t)p[7]);
 126      -#endif
      112 +#endif  /* _LITTLE_ENDIAN */
 127  113                  counter &= ctx->ctr_counter_mask;
 128  114                  ctx->ctr_cb[1] =
 129  115                      (ctx->ctr_cb[1] & ~(ctx->ctr_counter_mask)) | counter;
 130  116  
 131  117                  /*
 132  118                   * XOR the previous cipher block or IV with the
 133  119                   * current clear block.
 134  120                   */
 135  121                  xor_block(blockp, lastp);
 136  122  
↓ open down ↓ 80 lines elided ↑ open up ↑
 217  203          out->cd_offset += ctx->ctr_remainder_len;
 218  204          ctx->ctr_remainder_len = 0;
 219  205          return (CRYPTO_SUCCESS);
 220  206  }
 221  207  
 222  208  int
 223  209  ctr_init_ctx(ctr_ctx_t *ctr_ctx, ulong_t count, uint8_t *cb,
 224  210  void (*copy_block)(uint8_t *, uint8_t *))
 225  211  {
 226  212          uint64_t mask = 0;
 227      -#ifdef _LITTLE_ENDIAN
 228      -        uint8_t *p8;
 229      -#endif
 230  213  
 231  214          if (count == 0 || count > 64) {
 232  215                  return (CRYPTO_MECHANISM_PARAM_INVALID);
 233  216          }
 234  217          while (count-- > 0)
 235  218                  mask |= (1ULL << count);
      219 +
 236  220  #ifdef _LITTLE_ENDIAN
 237      -        p8 = (uint8_t *)&mask;
 238      -        mask = (((uint64_t)p8[0] << 56) |
 239      -            ((uint64_t)p8[1] << 48) |
 240      -            ((uint64_t)p8[2] << 40) |
 241      -            ((uint64_t)p8[3] << 32) |
 242      -            ((uint64_t)p8[4] << 24) |
 243      -            ((uint64_t)p8[5] << 16) |
 244      -            ((uint64_t)p8[6] << 8) |
 245      -            (uint64_t)p8[7]);
      221 +        mask = htonll(mask);
 246  222  #endif
 247  223          ctr_ctx->ctr_counter_mask = mask;
 248  224          copy_block(cb, (uchar_t *)ctr_ctx->ctr_cb);
 249  225          ctr_ctx->ctr_lastp = (uint8_t *)&ctr_ctx->ctr_cb[0];
 250  226          ctr_ctx->ctr_flags |= CTR_MODE;
 251  227          return (CRYPTO_SUCCESS);
 252  228  }
 253  229  
 254  230  /* ARGSUSED */
 255  231  void *
↓ open down ↓ 14 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX