Print this page
    
10076 make usr/src/test smatch clean
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/test/crypto-tests/tests/common/cryptotest_kcf.c
          +++ new/usr/src/test/crypto-tests/tests/common/cryptotest_kcf.c
   1    1  /*
   2    2   * This file and its contents are supplied under the terms of the
   3    3   * Common Development and Distribution License ("CDDL"), version 1.0.
   4    4   * You may only use this file in accordance with the terms of version
   5    5   * 1.0 of the CDDL.
   6    6   *
   7    7   * A full copy of the text of the CDDL should have accompanied this
   8    8   * source.  A copy of the CDDL is also available via the Internet at
   9    9   * http://www.illumos.org/license/CDDL.
  10   10   */
  11   11  
  12   12  /*
  13   13   * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  14   14   * Copyright 2018, Joyent, Inc.
  15   15   */
  16   16  
  17   17  #include <fcntl.h>
  18   18  #include <strings.h>
  19   19  #include <unistd.h>
  20   20  #include <errno.h>
  21   21  #include <stdio.h>
  22   22  #include <stdlib.h>
  23   23  
  24   24  #include "cryptotest.h"
  25   25  
  26   26  struct crypto_op {
  27   27          char *in;
  28   28          char *out;
  29   29          char *key;
  30   30          char *param;
  31   31  
  32   32          size_t inlen;
  33   33          size_t outlen;
  34   34          size_t keylen;
  35   35          size_t paramlen;
  36   36          size_t updatelen;
  37   37  
  38   38          char *mechname;
  39   39  
  40   40          /* internal */
  41   41          crypto_mech_type_t mech;
  42   42          crypto_session_id_t hsession;
  43   43          crypto_func_group_t fg;
  44   44  };
  45   45  
  46   46  static int fd;
  47   47  static const char CRYPTO_DEVICE[] = "/dev/crypto";
  48   48  
  49   49  int
  50   50  kcf_do_ioctl(int opcode, uint_t *arg, char *opstr)
  51   51  {
  52   52          int ret;
  53   53  
  54   54          while ((ret = ioctl(fd, opcode, arg)) < 0) {
  55   55                  if (errno != EINTR)
  56   56                          break;
  57   57          }
  58   58  
  59   59          if (ret < 0 || *arg != CRYPTO_SUCCESS)
  60   60                  (void) fprintf(stderr, "%s: Error = %d %d 0x%02x\n",
  61   61                      (opstr == NULL) ? "ioctl" : opstr,
  62   62                      ret, errno, *arg);
  63   63  
  64   64          if (ret < 0)
  65   65                  return (errno);
  66   66  
  67   67          return (*arg);
  68   68  }
  
    | 
      ↓ open down ↓ | 
    68 lines elided | 
    
      ↑ open up ↑ | 
  
  69   69  
  70   70  crypto_op_t *
  71   71  cryptotest_init(cryptotest_t *arg, crypto_func_group_t fg)
  72   72  {
  73   73          crypto_op_t *op = malloc(sizeof (*op));
  74   74  
  75   75          if (op == NULL)
  76   76                  return (NULL);
  77   77  
  78   78          while ((fd = open(CRYPTO_DEVICE, O_RDWR)) < 0) {
  79      -                if (errno != EINTR)
       79 +                if (errno != EINTR) {
       80 +                        free(op);
  80   81                          return (NULL);
       82 +                }
  81   83          }
  82   84  
  83   85          op->in = (char *)arg->in;
  84   86          op->out = (char *)arg->out;
  85   87          op->key = (char *)arg->key;
  86   88          op->param = (char *)arg->param;
  87   89  
  88   90          op->inlen = arg->inlen;
  89   91          op->outlen = arg->outlen;
  90   92          op->keylen = arg->keylen * 8; /* kcf uses keylen in bits */
  91   93          op->paramlen = arg->plen;
  92   94          op->updatelen = arg->updatelen;
  93   95  
  94   96          op->mechname = arg->mechname;
  95   97  
  96   98          op->hsession = CRYPTO_INVALID_SESSION;
  97   99          op->fg = fg;
  98  100  
  99  101          if (op->out == NULL)
 100  102                  op->outlen = op->inlen;
 101  103          return (op);
 102  104  }
 103  105  
 104  106  int
 105  107  cryptotest_close_session(crypto_session_id_t session)
 106  108  {
 107  109          crypto_close_session_t cs;
 108  110  
 109  111          cs.cs_session = session;
 110  112          return (kcf_do_ioctl(CRYPTO_CLOSE_SESSION, (uint_t *)&cs, "session"));
 111  113  }
 112  114  
 113  115  int
 114  116  cryptotest_close(crypto_op_t *op)
 115  117  {
 116  118          if (op->hsession != CRYPTO_INVALID_SESSION)
 117  119                  (void) cryptotest_close_session(op->hsession);
 118  120          free(op);
 119  121          if (fd >= 0)
 120  122                  return (close(fd));
 121  123          return (0);
 122  124  }
 123  125  
 124  126  int
 125  127  get_mech_info(crypto_op_t *op)
 126  128  {
 127  129          crypto_get_mechanism_number_t get_number;
 128  130  
 129  131          bzero(&get_number, sizeof (get_number));
 130  132  
 131  133          get_number.pn_mechanism_string = op->mechname;
 132  134          get_number.pn_mechanism_len = strlen(op->mechname) + 1;
 133  135  
 134  136          if (kcf_do_ioctl(CRYPTO_GET_MECHANISM_NUMBER,
 135  137              (uint_t *)&get_number, "get_mech_info") != CRYPTO_SUCCESS) {
 136  138                  (void) fprintf(stderr, "failed to resolve mechanism name %s\n",
 137  139                      op->mechname);
 138  140                  return (CTEST_NAME_RESOLVE_FAILED);
 139  141          }
 140  142          op->mech = get_number.pn_internal_number;
 141  143          return (CRYPTO_SUCCESS);
 142  144  }
 143  145  
 144  146  int
 145  147  get_hsession_by_mech(crypto_op_t *op)
 146  148  {
 147  149          crypto_by_mech_t mech;
 148  150          int rv;
 149  151  
 150  152          mech.mech_keylen = op->keylen;
 151  153          mech.mech_type = op->mech;
 152  154          mech.mech_fg = op->fg;
 153  155  
 154  156          rv = kcf_do_ioctl(CRYPTO_GET_PROVIDER_BY_MECH, (uint_t *)&mech,
 155  157              "get_hsession_by_mech");
 156  158  
 157  159          if (rv != 0 || mech.rv != CRYPTO_SUCCESS) {
 158  160                  (void) fprintf(stderr,
 159  161                      "could not find provider for mechanism %llu\n",
 160  162                      mech.mech_type);
 161  163                  return (CTEST_MECH_NO_PROVIDER);
 162  164          }
 163  165  
 164  166          op->hsession = mech.session_id;
 165  167  
 166  168          return (CRYPTO_SUCCESS);
 167  169  }
 168  170  
 169  171  /*
 170  172   * CRYPTO_MAC_* functions
 171  173   */
 172  174  int
 173  175  mac_init(crypto_op_t *op)
 174  176  {
 175  177          crypto_mac_init_t init;
 176  178  
 177  179          bzero((void *)&init, sizeof (init));
 178  180  
 179  181          init.mi_session = op->hsession;
 180  182  
 181  183          init.mi_key.ck_data = op->key;
 182  184          init.mi_key.ck_format = CRYPTO_KEY_RAW; /* must be this */
 183  185          init.mi_key.ck_length = op->keylen;
 184  186  
 185  187          init.mi_mech.cm_type = op->mech;
 186  188          init.mi_mech.cm_param = NULL;
 187  189          init.mi_mech.cm_param_len = 0;
 188  190  
 189  191          return (kcf_do_ioctl(CRYPTO_MAC_INIT, (uint_t *)&init, "init"));
 190  192  }
 191  193  
 192  194  int
 193  195  mac_single(crypto_op_t *op)
 194  196  {
 195  197          crypto_mac_t mac;
 196  198  
 197  199          bzero(&mac, sizeof (mac));
 198  200          mac.cm_session = op->hsession;
 199  201          mac.cm_datalen = op->inlen;
 200  202          mac.cm_databuf = op->in;
 201  203          mac.cm_maclen = op->outlen;
 202  204          mac.cm_macbuf = op->out;
 203  205  
 204  206          return (kcf_do_ioctl(CRYPTO_MAC, (uint_t *)&mac, "single"));
 205  207  }
 206  208  
 207  209  int
 208  210  mac_update(crypto_op_t *op, int offset)
 209  211  {
 210  212          crypto_mac_update_t update;
 211  213  
 212  214          bzero((void *)&update, sizeof (update));
 213  215  
 214  216          update.mu_session = op->hsession;
 215  217          update.mu_databuf = op->in + offset;
 216  218          update.mu_datalen = op->updatelen;
 217  219  
 218  220          return (kcf_do_ioctl(CRYPTO_MAC_UPDATE, (uint_t *)&update, "update"));
 219  221  }
 220  222  
 221  223  int
 222  224  mac_final(crypto_op_t *op)
 223  225  {
 224  226          crypto_mac_final_t final;
 225  227  
 226  228          bzero((void *)&final, sizeof (final));
 227  229  
 228  230          final.mf_session = op->hsession;
 229  231          final.mf_maclen = op->outlen;
 230  232          final.mf_macbuf = op->out;
 231  233  
 232  234          return (kcf_do_ioctl(CRYPTO_MAC_FINAL, (uint_t *)&final, "final"));
 233  235  }
 234  236  
 235  237  
 236  238  /*
 237  239   * CRYPTO_ENCRYPT_* functions
 238  240   */
 239  241  
 240  242  int
 241  243  encrypt_init(crypto_op_t *op)
 242  244  {
 243  245          crypto_encrypt_init_t init;
 244  246  
 245  247          bzero((void *)&init, sizeof (init));
 246  248  
 247  249          init.ei_session = op->hsession;
 248  250  
 249  251          init.ei_key.ck_data = op->key;
 250  252          init.ei_key.ck_format = CRYPTO_KEY_RAW; /* must be this */
 251  253          init.ei_key.ck_length = op->keylen;
 252  254  
 253  255          init.ei_mech.cm_type = op->mech;
 254  256          init.ei_mech.cm_param = op->param;
 255  257          init.ei_mech.cm_param_len = op->paramlen;
 256  258  
 257  259          return (kcf_do_ioctl(CRYPTO_ENCRYPT_INIT, (uint_t *)&init, "init"));
 258  260  }
 259  261  
 260  262  int
 261  263  encrypt_single(crypto_op_t *op)
 262  264  {
 263  265          crypto_encrypt_t encrypt;
 264  266  
 265  267          bzero(&encrypt, sizeof (encrypt));
 266  268          encrypt.ce_session = op->hsession;
 267  269          encrypt.ce_datalen = op->inlen;
 268  270          encrypt.ce_databuf = op->in;
 269  271          encrypt.ce_encrlen = op->outlen;
 270  272          encrypt.ce_encrbuf = op->out;
 271  273  
 272  274          return (kcf_do_ioctl(CRYPTO_ENCRYPT, (uint_t *)&encrypt, "single"));
 273  275  }
 274  276  
 275  277  int
 276  278  encrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
 277  279  {
 278  280          crypto_encrypt_update_t update;
 279  281          int ret;
 280  282          bzero((void *)&update, sizeof (update));
 281  283  
 282  284          update.eu_session = op->hsession;
 283  285          update.eu_databuf = op->in + offset;
 284  286          update.eu_datalen = op->updatelen;
 285  287          update.eu_encrlen = op->outlen - *encrlen;
 286  288          update.eu_encrbuf = op->out + *encrlen;
 287  289  
 288  290          ret = kcf_do_ioctl(CRYPTO_ENCRYPT_UPDATE, (uint_t *)&update, "update");
 289  291          *encrlen += update.eu_encrlen;
 290  292          return (ret);
 291  293  }
 292  294  
 293  295  int
 294  296  encrypt_final(crypto_op_t *op, size_t encrlen)
 295  297  {
 296  298          crypto_encrypt_final_t final;
 297  299  
 298  300          bzero((void *)&final, sizeof (final));
 299  301  
 300  302          final.ef_session = op->hsession;
 301  303          final.ef_encrlen = op->outlen - encrlen;
 302  304          final.ef_encrbuf = op->out + encrlen;
 303  305  
 304  306          return (kcf_do_ioctl(CRYPTO_ENCRYPT_FINAL, (uint_t *)&final, "final"));
 305  307  }
 306  308  
 307  309  /*
 308  310   * CRYPTO_DECRYPT_* functions
 309  311   */
 310  312  
 311  313  int
 312  314  decrypt_init(crypto_op_t *op)
 313  315  {
 314  316          crypto_decrypt_init_t init;
 315  317  
 316  318          bzero((void *)&init, sizeof (init));
 317  319  
 318  320          init.di_session = op->hsession;
 319  321  
 320  322          init.di_key.ck_data = op->key;
 321  323          init.di_key.ck_format = CRYPTO_KEY_RAW; /* must be this */
 322  324          init.di_key.ck_length = op->keylen;
 323  325  
 324  326          init.di_mech.cm_type = op->mech;
 325  327          init.di_mech.cm_param = op->param;
 326  328          init.di_mech.cm_param_len = op->paramlen;
 327  329  
 328  330          return (kcf_do_ioctl(CRYPTO_DECRYPT_INIT, (uint_t *)&init, "init"));
 329  331  }
 330  332  
 331  333  int
 332  334  decrypt_single(crypto_op_t *op)
 333  335  {
 334  336          crypto_decrypt_t decrypt;
 335  337  
 336  338          bzero(&decrypt, sizeof (decrypt));
 337  339          decrypt.cd_session = op->hsession;
 338  340          decrypt.cd_datalen = op->outlen;
 339  341          decrypt.cd_databuf = op->out;
 340  342          decrypt.cd_encrlen = op->inlen;
 341  343          decrypt.cd_encrbuf = op->in;
 342  344  
 343  345          return (kcf_do_ioctl(CRYPTO_DECRYPT, (uint_t *)&decrypt, "single"));
 344  346  }
 345  347  
 346  348  int
 347  349  decrypt_update(crypto_op_t *op, int offset, size_t *encrlen)
 348  350  {
 349  351          crypto_decrypt_update_t update;
 350  352          int ret;
 351  353  
 352  354          bzero((void *)&update, sizeof (update));
 353  355  
 354  356          update.du_session = op->hsession;
 355  357          update.du_databuf = op->out + *encrlen;
 356  358          update.du_datalen = op->outlen - *encrlen;
 357  359          update.du_encrlen = op->updatelen;
 358  360          update.du_encrbuf = op->in + offset;
 359  361  
 360  362          ret = kcf_do_ioctl(CRYPTO_DECRYPT_UPDATE, (uint_t *)&update, "update");
 361  363          *encrlen += update.du_datalen;
 362  364          return (ret);
 363  365  }
 364  366  
 365  367  int
 366  368  decrypt_final(crypto_op_t *op, size_t encrlen)
 367  369  {
 368  370          crypto_decrypt_final_t final;
 369  371  
 370  372          bzero((void *)&final, sizeof (final));
 371  373  
 372  374          final.df_session = op->hsession;
 373  375          final.df_datalen = op->outlen - encrlen;
 374  376          final.df_databuf = op->out + encrlen;
 375  377  
 376  378          return (kcf_do_ioctl(CRYPTO_DECRYPT_FINAL, (uint_t *)&final, "final"));
 377  379  }
 378  380  
 379  381  int
 380  382  digest_init(crypto_op_t *op)
 381  383  {
 382  384          crypto_digest_init_t init;
 383  385  
 384  386          bzero(&init, sizeof (init));
 385  387  
 386  388          init.di_session = op->hsession;
 387  389  
 388  390          init.di_mech.cm_type = op->mech;
 389  391          init.di_mech.cm_param = NULL;
 390  392          init.di_mech.cm_param_len = 0;
 391  393  
 392  394          return (kcf_do_ioctl(CRYPTO_DIGEST_INIT, (uint_t *)&init, "init"));
 393  395  }
 394  396  
 395  397  int
 396  398  digest_single(crypto_op_t *op)
 397  399  {
 398  400          crypto_digest_t digest;
 399  401  
 400  402          bzero(&digest, sizeof (digest));
 401  403  
 402  404          digest.cd_session = op->hsession;
 403  405  
 404  406          digest.cd_datalen = op->inlen;
 405  407          digest.cd_databuf = op->in;
 406  408          digest.cd_digestlen = op->outlen;
 407  409          digest.cd_digestbuf = op->out;
 408  410  
 409  411          return (kcf_do_ioctl(CRYPTO_DIGEST, (uint_t *)&digest, "digest"));
 410  412  }
 411  413  
 412  414  int
 413  415  digest_update(crypto_op_t *op, int offset)
 414  416  {
 415  417          crypto_digest_update_t update;
 416  418  
 417  419          bzero(&update, sizeof (update));
 418  420  
 419  421          update.du_session = op->hsession;
 420  422  
 421  423          update.du_datalen = op->updatelen;
 422  424          update.du_databuf = op->in + offset;
 423  425  
 424  426          return (kcf_do_ioctl(CRYPTO_DIGEST_UPDATE, (uint_t *)&update,
 425  427              "update"));
 426  428  }
 427  429  
 428  430  int
 429  431  digest_final(crypto_op_t *op)
 430  432  {
 431  433          crypto_digest_final_t final;
 432  434  
 433  435          bzero(&final, sizeof (final));
 434  436  
 435  437          final.df_session = op->hsession;
 436  438  
 437  439          final.df_digestlen = op->outlen;
 438  440          final.df_digestbuf = op->out;
 439  441  
 440  442          return (kcf_do_ioctl(CRYPTO_DIGEST_FINAL, (uint_t *)&final, "final"));
 441  443  }
 442  444  void
 443  445  ccm_init_params(void *buf, ulong_t ulDataLen, uchar_t *pNonce,
 444  446      ulong_t ulNonceLen, uchar_t *pAAD, ulong_t ulAADLen, ulong_t ulMACLen)
 445  447  {
 446  448          CK_AES_CCM_PARAMS *pp = buf;
 447  449  
 448  450          pp->ulDataSize = ulDataLen;
 449  451          pp->nonce = pNonce;
 450  452          pp->ulNonceSize = ulNonceLen;
 451  453          pp->authData = pAAD;
 452  454          pp->ulAuthDataSize = ulAADLen;
 453  455          pp->ulMACSize = ulMACLen;
 454  456  }
 455  457  
 456  458  size_t
 457  459  ccm_param_len(void)
 458  460  {
 459  461          return (sizeof (CK_AES_CCM_PARAMS));
 460  462  }
  
    | 
      ↓ open down ↓ | 
    370 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX