Print this page
5031131 perf: pkcs11_kernel can benefit from a more efficient pkcs11_mech2str()

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelUtil.c
          +++ new/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelUtil.c
↓ open down ↓ 11 lines elided ↑ open up ↑
  12   12   *
  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  /*
  22      - * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
       22 + * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  23   23   * Use is subject to license terms.
  24   24   */
  25   25  
  26      -#pragma ident   "@(#)kernelUtil.c       1.16    07/09/11 SMI"
       26 +#pragma ident   "@(#)kernelUtil.c       1.17    08/06/30 SMI"
  27   27  
  28   28  #include <stdlib.h>
  29   29  #include <string.h>
  30   30  #include <strings.h>
       31 +#include <stdio.h>
  31   32  #include <cryptoutil.h>
  32   33  #include <errno.h>
  33   34  #include <security/cryptoki.h>
  34   35  #include <sys/crypto/common.h>
  35   36  #include <sys/crypto/ioctl.h>
  36   37  #include "kernelGlobal.h"
  37   38  #include "kernelObject.h"
  38   39  #include "kernelSlot.h"
  39   40  
  40   41  #define ENCODE_ATTR(type, value, len) {         \
↓ open down ↓ 144 lines elided ↑ open up ↑
 185  186          kernel_mechhash[h] = elem;
 186  187          (void) pthread_mutex_unlock(&mechhash_mutex);
 187  188  
 188  189          return (CKR_OK);
 189  190  }
 190  191  
 191  192  CK_RV
 192  193  kernel_mech(CK_MECHANISM_TYPE type, crypto_mech_type_t *k_number)
 193  194  {
 194  195          crypto_get_mechanism_number_t get_number;
 195      -        char *string;
      196 +        const char *string;
 196  197          CK_RV rv;
 197  198          int r;
 198  199          kmh_elem_t *elem;
 199  200          uint_t h;
      201 +        char buf[11];   /* Num chars for representing ulong in ASCII */
 200  202  
 201  203          /*
 202  204           * Search for an existing entry. No need to lock since we are
 203  205           * just a reader and we never free the entries in the hash table.
 204  206           */
 205  207          h = MECH_HASH(type);
 206  208          for (elem = kernel_mechhash[h]; elem != NULL; elem = elem->knext) {
 207  209                  if (type == elem->type) {
 208  210                          *k_number = elem->kmech;
 209  211                          return (CKR_OK);
 210  212                  }
 211  213          }
 212  214  
 213      -        string = pkcs11_mech2str(type);
      215 +        if (type > CKM_VENDOR_DEFINED) {
      216 +                (void) snprintf(buf, sizeof (buf), "%#lx", type);
      217 +                string = buf;
      218 +        } else {
      219 +                string = pkcs11_mech2str(type);
      220 +        }
      221 +
 214  222          if (string == NULL)
 215  223                  return (CKR_MECHANISM_INVALID);
 216  224  
 217      -        get_number.pn_mechanism_string = string;
      225 +        get_number.pn_mechanism_string = (char *)string;
 218  226          get_number.pn_mechanism_len = strlen(string) + 1;
 219  227  
 220  228          while ((r = ioctl(kernel_fd, CRYPTO_GET_MECHANISM_NUMBER,
 221  229              &get_number)) < 0) {
 222  230                  if (errno != EINTR)
 223  231                          break;
 224  232          }
 225  233          if (r < 0) {
 226  234                  rv = CKR_MECHANISM_INVALID;
 227  235          } else {
↓ open down ↓ 4 lines elided ↑ open up ↑
 232  240                          rv = CKR_OK;
 233  241                  }
 234  242          }
 235  243  
 236  244          if (rv == CKR_OK) {
 237  245                  *k_number = get_number.pn_internal_number;
 238  246                  /* Add this to the hash table */
 239  247                  (void) kmech_hash_insert(type, *k_number);
 240  248          }
 241  249  
 242      -        free(string);
 243  250          return (rv);
 244  251  }
 245  252  
 246  253  
 247  254  /*
 248  255   * Return the value of a secret key object.
 249  256   * This routine allocates memory for the value.
 250  257   * A null pointer is returned on error.
 251  258   */
 252  259  unsigned char *
↓ open down ↓ 914 lines elided ↑ open up ↑
1167 1174          }
1168 1175  
1169 1176          return (rv);
1170 1177  }
1171 1178  
1172 1179  
1173 1180  /*
1174 1181   * Get the value of the CKA_PRIVATE attribute for the object just returned
1175 1182   * from the HW provider.  This function will be called by any function
1176 1183   * that creates a new object, because the CKA_PRIVATE value of an object is
1177      - * token sepecific.  The CKA_PRIVATE attribute value of the new object will be
     1184 + * token specific.  The CKA_PRIVATE attribute value of the new object will be
1178 1185   * stored in the object structure in the library, which will be used later at
1179 1186   * C_Logout to clean up all private objects.
1180 1187   */
1181 1188  CK_RV
1182 1189  get_cka_private_value(kernel_session_t *sp, crypto_object_id_t oid,
1183 1190      CK_BBOOL *is_pri_obj)
1184 1191  {
1185 1192          CK_RV  rv = CKR_OK;
1186 1193          crypto_object_get_attribute_value_t obj_ga;
1187 1194          crypto_object_attribute_t obj_attr;
↓ open down ↓ 26 lines elided ↑ open up ↑
1214 1221  
1215 1222          return (rv);
1216 1223  }
1217 1224  
1218 1225  
1219 1226  CK_RV
1220 1227  get_mechanism_info(kernel_slot_t *pslot, CK_MECHANISM_TYPE type,
1221 1228      CK_MECHANISM_INFO_PTR pInfo, uint32_t *k_mi_flags)
1222 1229  {
1223 1230          crypto_get_provider_mechanism_info_t mechanism_info;
1224      -        char *string;
     1231 +        const char *string;
1225 1232          CK_FLAGS flags, mi_flags;
1226 1233          CK_RV rv;
1227 1234          int r;
     1235 +        char buf[11];   /* Num chars for representing ulong in ASCII */
1228 1236  
1229      -        string = pkcs11_mech2str(type);
     1237 +        if (type > CKM_VENDOR_DEFINED) {
     1238 +                /* allocate/build a string containing the mechanism number */
     1239 +                (void) snprintf(buf, sizeof (buf), "%#lx", type);
     1240 +                string = buf;
     1241 +        } else {
     1242 +                string = pkcs11_mech2str(type);
     1243 +        }
     1244 +
1230 1245          if (string == NULL)
1231 1246                  return (CKR_MECHANISM_INVALID);
1232 1247  
1233 1248          (void) strcpy(mechanism_info.mi_mechanism_name, string);
1234 1249          mechanism_info.mi_provider_id = pslot->sl_provider_id;
1235 1250  
1236 1251          while ((r = ioctl(kernel_fd, CRYPTO_GET_PROVIDER_MECHANISM_INFO,
1237 1252              &mechanism_info)) < 0) {
1238 1253                  if (errno != EINTR)
1239 1254                          break;
1240 1255          }
1241 1256          if (r < 0) {
1242 1257                  rv = CKR_FUNCTION_FAILED;
1243 1258          } else {
1244 1259                  rv = crypto2pkcs11_error_number(
1245 1260                      mechanism_info.mi_return_value);
1246 1261          }
1247 1262  
1248 1263          if (rv != CKR_OK) {
1249      -                goto out;
     1264 +                return (rv);
1250 1265          }
1251 1266  
1252 1267          /*
1253 1268           * Atomic flags are not part of PKCS#11 so we filter
1254 1269           * them out here.
1255 1270           */
1256 1271          mi_flags = mechanism_info.mi_flags;
1257 1272          mi_flags &= ~(CRYPTO_FG_DIGEST_ATOMIC | CRYPTO_FG_ENCRYPT_ATOMIC |
1258 1273              CRYPTO_FG_DECRYPT_ATOMIC | CRYPTO_FG_MAC_ATOMIC |
1259 1274              CRYPTO_FG_SIGN_ATOMIC | CRYPTO_FG_VERIFY_ATOMIC |
1260 1275              CRYPTO_FG_SIGN_RECOVER_ATOMIC |
1261 1276              CRYPTO_FG_VERIFY_RECOVER_ATOMIC |
1262 1277              CRYPTO_FG_ENCRYPT_MAC_ATOMIC |
1263 1278              CRYPTO_FG_MAC_DECRYPT_ATOMIC);
1264 1279  
1265 1280          if (mi_flags == 0) {
1266      -                rv = CKR_MECHANISM_INVALID;
1267      -                goto out;
     1281 +                return (CKR_MECHANISM_INVALID);
1268 1282          }
1269 1283  
1270 1284          if (rv == CKR_OK) {
1271 1285                  /* set the value of k_mi_flags first */
1272 1286                  *k_mi_flags = mi_flags;
1273 1287  
1274 1288                  /* convert KEF flags into pkcs11 flags */
1275 1289                  flags = CKF_HW;
1276 1290                  if (mi_flags & CRYPTO_FG_ENCRYPT)
1277 1291                          flags |= CKF_ENCRYPT;
↓ open down ↓ 25 lines elided ↑ open up ↑
1303 1317                          flags |= CKF_UNWRAP;
1304 1318                  if (mi_flags & CRYPTO_FG_DERIVE)
1305 1319                          flags |= CKF_DERIVE;
1306 1320  
1307 1321                  pInfo->ulMinKeySize = mechanism_info.mi_min_key_size;
1308 1322                  pInfo->ulMaxKeySize = mechanism_info.mi_max_key_size;
1309 1323                  pInfo->flags = flags;
1310 1324  
1311 1325          }
1312 1326  
1313      -out:
1314      -        free(string);
1315 1327          return (rv);
1316 1328  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX