Print this page
6418676 encrypt(1) and decrypt(1) could benefit from being 64-bit programs

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/cmd-crypto/decrypt/decrypt.c
          +++ new/usr/src/cmd/cmd-crypto/decrypt/decrypt.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  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  /* Portions Copyright 2005 Richard Lowe */
  22   22  /*
  23      - * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
       23 + * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26   26  
  27      -#pragma ident   "@(#)decrypt.c  1.13    07/10/04 SMI"
       27 +#pragma ident   "@(#)decrypt.c  1.14    08/04/30 SMI"
  28   28  
  29   29  /*
  30   30   * decrypt.c
  31   31   *
  32   32   * Implements encrypt(1) and decrypt(1) commands
  33   33   *
  34   34   * One binary performs both encrypt/decrypt operation.
  35   35   *
  36      - * usage:
       36 + * Usage:
       37 + *  -a algorithm mechanism name without CKM_ prefix. Case
       38 + *               does not matter
       39 + *  -k keyfile   file containing key data. If not specified user is
       40 + *               prompted to enter key. key length > 0 is required
       41 + *  -i infile    input file to encrypt/decrypt. If omitted, stdin used.
       42 + *  -o outfile   output file to encrypt/decrypt. If omitted, stdout used.
       43 + *               if infile & outfile are same, a temp file is used for
       44 + *               output and infile is replaced with this file after
       45 + *               operation is complete
       46 + *  -l           Display the list of  algorithms
       47 + *  -v           Display verbose information
       48 + *  -T tokenspec Specify a PKCS#11 token (optionally used with -K)
       49 + *  -K keylabel  Specify the symmetric PKCS#11 token key label
  37   50   *
  38      - *  algorithm - mechanism name without CKM_ prefix. Case
  39      - *              does not matter
  40      - *  keyfile - file containing key data. If not specified user is
  41      - *            prompted to enter key. key length > 0 is required
  42      - *  infile  - input file to encrypt/decrypt. If omitted, stdin used.
  43      - *  outfile - output file to encrypt/decrypt. If omitted, stdout used.
  44      - *            if infile & outfile are same, a temp file is used for
  45      - *            output and infile is replaced with this file after
  46      - *            operation is complete.
  47      - *
  48   51   * Implementation notes:
  49      - *   iv data - It is generated by random bytes equal to one block size.
       52 + *   IV data - It is generated by random bytes equal to one block size.
  50   53   *
  51      - *   encrypted output format -
       54 + *   Encrypted output format -
  52   55   *   - Output format version number - 4 bytes in network byte order.
  53   56   *   - Iterations used in key gen function, 4 bytes in  network byte order.
  54      - *   - IV ( 'ivlen' bytes)
       57 + *   - IV ('ivlen' bytes).  Length is algorithm-dependent (see mech_aliases)
  55   58   *   - Salt data used in key gen (16 bytes)
  56      - *   - cipher text data.
  57      - *
       59 + *   - Cipher text data (remainder of the file)
  58   60   */
  59   61  
  60   62  #include <stdio.h>
  61   63  #include <stdlib.h>
  62   64  #include <unistd.h>
  63   65  #include <errno.h>
  64   66  #include <fcntl.h>
  65   67  #include <ctype.h>
  66   68  #include <strings.h>
  67   69  #include <libintl.h>
↓ open down ↓ 90 lines elided ↑ open up ↑
 158  160  
 159  161  static CK_BBOOL truevalue = TRUE;
 160  162  static CK_BBOOL falsevalue = FALSE;
 161  163  
 162  164  static boolean_t aflag = B_FALSE; /* -a <algorithm> flag, required */
 163  165  static boolean_t kflag = B_FALSE; /* -k <keyfile> flag */
 164  166  static boolean_t iflag = B_FALSE; /* -i <infile> flag, use stdin if absent */
 165  167  static boolean_t oflag = B_FALSE; /* -o <outfile> flag, use stdout if absent */
 166  168  static boolean_t lflag = B_FALSE; /* -l flag (list) */
 167  169  static boolean_t vflag = B_FALSE; /* -v flag (verbose) */
 168      -static boolean_t Tflag = B_FALSE;
 169      -static boolean_t Kflag = B_FALSE;
      170 +static boolean_t Tflag = B_FALSE; /* -T flag (tokenspec) */
      171 +static boolean_t Kflag = B_FALSE; /* -K flag (keylabel) */
 170  172  
 171      -static char *keyfile = NULL;    /* name of keyfile */
 172      -static char *inputfile = NULL;  /* name of input file */
 173      -static char *outputfile = NULL; /* name of output file */
 174      -static char *token_label = NULL;
 175      -static char *key_label = NULL;
      173 +static char *keyfile = NULL;     /* name of keyfile */
      174 +static char *inputfile = NULL;   /* name of input file */
      175 +static char *outputfile = NULL;  /* name of output file */
      176 +static char *token_label = NULL; /* name of PKCS#11 token */
      177 +static char *key_label = NULL;   /* name of PKCS#11 token key label */
 176  178  
 177  179  static int status_pos = 0; /* current position of progress bar element */
 178  180  
 179  181  /*
 180  182   * function prototypes
 181  183   */
 182  184  static void usage(struct CommandInfo *cmd);
 183  185  static int execute_cmd(struct CommandInfo *cmd, char *algo_str);
 184  186  static int crypt_multipart(struct CommandInfo *cmd, CK_SESSION_HANDLE hSession,
 185  187          int infd, int outfd, off_t insize);
↓ open down ↓ 5 lines elided ↑ open up ↑
 191  193          extern char *optarg;
 192  194          extern int optind;
 193  195          char *optstr;
 194  196          char c;                 /* current getopts flag */
 195  197          char *algo_str = NULL;  /* algorithm string */
 196  198          struct CommandInfo *cmd;
 197  199          char *cmdname;          /* name of command */
 198  200          boolean_t errflag = B_FALSE;
 199  201  
 200  202          (void) setlocale(LC_ALL, "");
 201      -#if !defined(TEXT_DOMAIN)       /* Should be defiend by cc -D */
      203 +#if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
 202  204  #define TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't */
 203  205  #endif
 204  206          (void) textdomain(TEXT_DOMAIN);
 205  207  
 206  208          /*
 207  209           * Based on command name, determine
 208  210           * type of command.
 209  211           */
 210  212          cmdname = basename(argv[0]);
 211  213  
↓ open down ↓ 216 lines elided ↑ open up ↑
 428  430          struct stat     outsbuf;        /* stat buf for outfile */
 429  431          char    tmpnam[PATH_MAX];       /* tmp file name */
 430  432          CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
 431  433          int infd = 0;                   /* input file, stdin default */
 432  434          int outfd = 1;                  /* output file, stdout default */
 433  435          char *outfilename = NULL;
 434  436          boolean_t errflag = B_TRUE;
 435  437          boolean_t inoutsame = B_FALSE;  /* if both input & output are same */
 436  438          CK_BYTE_PTR     pivbuf = NULL_PTR;
 437  439          CK_ULONG        ivlen = 0L;
 438      -        int mech_match = 0;
 439      -        CK_ULONG        iterations = CK_PKCS5_PBKD2_ITERATIONS;
      440 +        int             mech_match = 0;
      441 +        uint32_t        iterations = CK_PKCS5_PBKD2_ITERATIONS;
 440  442          CK_ULONG        keylen;
 441      -        int version = SUNW_ENCRYPT_FILE_VERSION;
      443 +        uint32_t        version = SUNW_ENCRYPT_FILE_VERSION;
 442  444          CK_KEY_TYPE keytype;
 443  445          KMF_RETURN kmfrv;
 444  446          CK_SLOT_ID token_slot_id;
 445  447  
 446  448          if (aflag) {
 447  449                  /* Determine if algorithm is valid */
 448  450                  for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
 449  451                      mech_match++) {
 450  452                          if (strcmp(algo_str,
 451  453                              mech_aliases[mech_match].alias) == 0) {
↓ open down ↓ 281 lines elided ↑ open up ↑
 733  735                              "input file."));
 734  736                          goto cleanup;
 735  737                  }
 736  738                  /* convert to host byte order */
 737  739                  version = ntohl(version);
 738  740  
 739  741                  switch (version) {
 740  742                  case 1:
 741  743                  /*
 742  744                   * Version 1 output format:
      745 +                 *  - Output format version 1 (4 bytes)
 743  746                   *  - Iterations used in key gen function (4 bytes)
 744      -                 *  - IV ( 'ivlen' bytes)
      747 +                 *  - IV ('ivlen' bytes). The length algorithm-dependent
 745  748                   *  - Salt data used in key gen (16 bytes)
      749 +                 *  - Cipher text data (remainder of the file)
 746  750                   *
 747  751                   * An encrypted file has IV as first block (0 or
 748  752                   * more bytes depending on mechanism) followed
 749  753                   * by cipher text.  Get the IV from the encrypted
 750  754                   * file.
 751  755                   */
 752  756                          /*
 753  757                           * Read iteration count and salt data.
 754  758                           */
 755  759                          if (read(infd, &iterations,
↓ open down ↓ 135 lines elided ↑ open up ↑
 891  895          if ((rv = cmd->Init(hSession, &mech, key)) != CKR_OK) {
 892  896                  cryptoerror(LOG_STDERR, gettext(
 893  897                      "failed to initialize crypto operation: %s"),
 894  898                      pkcs11_strerror(rv));
 895  899                  goto cleanup;
 896  900          }
 897  901  
 898  902          /* Write the version header encrypt command */
 899  903          if (cmd->type == CKA_ENCRYPT) {
 900  904                  /* convert to network order for storage */
 901      -                int netversion = htonl(version);
 902      -                CK_ULONG netiter;
      905 +                uint32_t        netversion = htonl(version);
      906 +                uint32_t        netiter;
 903  907  
 904  908                  if (write(outfd, &netversion, sizeof (netversion))
 905  909                      != sizeof (netversion)) {
 906  910                          cryptoerror(LOG_STDERR, gettext(
 907  911                              "failed to write version number "
 908  912                              "to output file."));
 909  913                          goto cleanup;
 910  914                  }
 911  915                  /*
 912  916                   * Write the iteration and salt data, even if they
↓ open down ↓ 285 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX