Print this page
6658907 digest(1) and mac(1) could benefit from being 64-bit programs


   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 2007 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #pragma ident   "@(#)digest.c   1.11    07/10/04 SMI"
  27 
  28 /*
  29  * digest.c
  30  *
  31  * Implements digest(1) and mac(1) commands
  32  * If command name is mac, performs mac operation
  33  * else perform digest operation
  34  *
  35  * See the man pages for digest and mac for details on
  36  * how these commands work.
  37  */
  38 
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <unistd.h>
  42 #include <fcntl.h>
  43 #include <ctype.h>
  44 #include <strings.h>
  45 #include <libintl.h>
  46 #include <libgen.h>


 354  *   filelist - list of files
 355  *   mac_cmd - if true do mac else do digest
 356  */
 357 static int
 358 execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd)
 359 {
 360         int fd;
 361         char *filename = NULL;
 362         CK_RV rv;
 363         CK_ULONG slotcount;
 364         CK_SLOT_ID slotID;
 365         CK_SLOT_ID_PTR pSlotList = NULL;
 366         CK_MECHANISM_TYPE mech_type;
 367         CK_MECHANISM_INFO info;
 368         CK_MECHANISM mech;
 369         CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
 370         CK_BYTE_PTR resultbuf = NULL;
 371         CK_ULONG resultlen;
 372         CK_BYTE_PTR     pkeydata = NULL;
 373         CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
 374         int keylen = 0;         /* key length */
 375         char *resultstr = NULL; /* result in hex string */
 376         int resultstrlen;       /* result string length */
 377         int i;
 378         int exitcode = EXIT_SUCCESS;            /* return code */
 379         int slot, mek;                  /* index variables */
 380         int mech_match = 0;
 381         CK_BYTE         salt[CK_PKCS5_PBKD2_SALT_SIZE];
 382         CK_ULONG        keysize;
 383         CK_ULONG        iterations = CK_PKCS5_PBKD2_ITERATIONS;
 384         CK_KEY_TYPE keytype;
 385         KMF_RETURN kmfrv;
 386         CK_SLOT_ID token_slot_id;
 387 
 388         if (aflag) {
 389                 /*
 390                  * Determine if algorithm/mechanism is valid
 391                  */
 392                 for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
 393                     mech_match++) {
 394                         if (strcmp(algo_str,


 400                 }
 401 
 402                 if (mech_match == MECH_ALIASES_COUNT) {
 403                         cryptoerror(LOG_STDERR,
 404                             gettext("unknown algorithm -- %s"), algo_str);
 405                         return (EXIT_FAILURE);
 406                 }
 407 
 408                 /* Get key to do a MAC operation */
 409                 if (mac_cmd) {
 410                         int status;
 411 
 412                         if (Kflag) {
 413                                 /* get the pin of the token */
 414                                 if (token_label == NULL ||
 415                                     !strlen(token_label)) {
 416                                         token_label = pkcs11_default_token();
 417                                 }
 418 
 419                                 status = pkcs11_get_pass(token_label,
 420                                     (char **)&pkeydata, (size_t *)&keylen,
 421                                     0, B_FALSE);
 422                         } else if (keyfile != NULL) {
 423                                 /* get the key file */
 424                                 status = pkcs11_read_data(keyfile,
 425                                     (void **)&pkeydata, (size_t *)&keylen);
 426                         } else {
 427                                 /* get the key from input */
 428                                 status = pkcs11_get_pass(NULL,
 429                                     (char **)&pkeydata, (size_t *)&keylen,
 430                                     0, B_FALSE);
 431                         }
 432 
 433                         if (status == -1 || keylen == 0 || pkeydata == NULL) {
 434                                 cryptoerror(LOG_STDERR,
 435                                     Kflag ? gettext("invalid passphrase.") :
 436                                     gettext("invalid key."));
 437                                 return (EXIT_FAILURE);
 438                         }
 439                 }
 440         }
 441 
 442         /* Initialize, and get list of slots */
 443         rv = C_Initialize(NULL);
 444         if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
 445                 cryptoerror(LOG_STDERR,
 446                     gettext("failed to initialize PKCS #11 framework: %s"),
 447                     pkcs11_strerror(rv));
 448                 return (EXIT_FAILURE);
 449         }




   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   "@(#)digest.c   1.12    08/03/20 SMI"
  27 
  28 /*
  29  * digest.c
  30  *
  31  * Implements digest(1) and mac(1) commands
  32  * If command name is mac, performs mac operation
  33  * else perform digest operation
  34  *
  35  * See the man pages for digest and mac for details on
  36  * how these commands work.
  37  */
  38 
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <unistd.h>
  42 #include <fcntl.h>
  43 #include <ctype.h>
  44 #include <strings.h>
  45 #include <libintl.h>
  46 #include <libgen.h>


 354  *   filelist - list of files
 355  *   mac_cmd - if true do mac else do digest
 356  */
 357 static int
 358 execute_cmd(char *algo_str, int filecount, char **filelist, boolean_t mac_cmd)
 359 {
 360         int fd;
 361         char *filename = NULL;
 362         CK_RV rv;
 363         CK_ULONG slotcount;
 364         CK_SLOT_ID slotID;
 365         CK_SLOT_ID_PTR pSlotList = NULL;
 366         CK_MECHANISM_TYPE mech_type;
 367         CK_MECHANISM_INFO info;
 368         CK_MECHANISM mech;
 369         CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
 370         CK_BYTE_PTR resultbuf = NULL;
 371         CK_ULONG resultlen;
 372         CK_BYTE_PTR     pkeydata = NULL;
 373         CK_OBJECT_HANDLE key = (CK_OBJECT_HANDLE) 0;
 374         size_t keylen = 0;              /* key length */
 375         char *resultstr = NULL; /* result in hex string */
 376         int resultstrlen;       /* result string length */
 377         int i;
 378         int exitcode = EXIT_SUCCESS;            /* return code */
 379         int slot, mek;                  /* index variables */
 380         int mech_match = 0;
 381         CK_BYTE         salt[CK_PKCS5_PBKD2_SALT_SIZE];
 382         CK_ULONG        keysize;
 383         CK_ULONG        iterations = CK_PKCS5_PBKD2_ITERATIONS;
 384         CK_KEY_TYPE keytype;
 385         KMF_RETURN kmfrv;
 386         CK_SLOT_ID token_slot_id;
 387 
 388         if (aflag) {
 389                 /*
 390                  * Determine if algorithm/mechanism is valid
 391                  */
 392                 for (mech_match = 0; mech_match < MECH_ALIASES_COUNT;
 393                     mech_match++) {
 394                         if (strcmp(algo_str,


 400                 }
 401 
 402                 if (mech_match == MECH_ALIASES_COUNT) {
 403                         cryptoerror(LOG_STDERR,
 404                             gettext("unknown algorithm -- %s"), algo_str);
 405                         return (EXIT_FAILURE);
 406                 }
 407 
 408                 /* Get key to do a MAC operation */
 409                 if (mac_cmd) {
 410                         int status;
 411 
 412                         if (Kflag) {
 413                                 /* get the pin of the token */
 414                                 if (token_label == NULL ||
 415                                     !strlen(token_label)) {
 416                                         token_label = pkcs11_default_token();
 417                                 }
 418 
 419                                 status = pkcs11_get_pass(token_label,
 420                                     (char **)&pkeydata, &keylen,
 421                                     0, B_FALSE);
 422                         } else if (keyfile != NULL) {
 423                                 /* get the key file */
 424                                 status = pkcs11_read_data(keyfile,
 425                                     (void **)&pkeydata, &keylen);
 426                         } else {
 427                                 /* get the key from input */
 428                                 status = pkcs11_get_pass(NULL,
 429                                     (char **)&pkeydata, &keylen,
 430                                     0, B_FALSE);
 431                         }
 432 
 433                         if (status == -1 || keylen == 0 || pkeydata == NULL) {
 434                                 cryptoerror(LOG_STDERR,
 435                                     Kflag ? gettext("invalid passphrase.") :
 436                                     gettext("invalid key."));
 437                                 return (EXIT_FAILURE);
 438                         }
 439                 }
 440         }
 441 
 442         /* Initialize, and get list of slots */
 443         rv = C_Initialize(NULL);
 444         if (rv != CKR_OK && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
 445                 cryptoerror(LOG_STDERR,
 446                     gettext("failed to initialize PKCS #11 framework: %s"),
 447                     pkcs11_strerror(rv));
 448                 return (EXIT_FAILURE);
 449         }