Print this page
first pass

Split Close
Expand all
Collapse all
          --- old/usr/src/common/crypto/des/des_impl.c
          +++ new/usr/src/common/crypto/des/des_impl.c
↓ open down ↓ 34 lines elided ↑ open up ↑
  35   35  #ifndef _KERNEL
  36   36  #include <strings.h>
  37   37  #include <stdlib.h>
  38   38  #endif  /* !_KERNEL */
  39   39  
  40   40  #if defined(__i386) || defined(__amd64)
  41   41  #include <sys/byteorder.h>
  42   42  #define UNALIGNED_POINTERS_PERMITTED
  43   43  #endif
  44   44  
  45      -/* EXPORT DELETE START */
  46      -
  47   45  typedef struct keysched_s {
  48   46          uint64_t ksch_encrypt[16];
  49   47          uint64_t ksch_decrypt[16];
  50   48  } keysched_t;
  51   49  
  52   50  typedef struct keysched3_s {
  53   51          uint64_t ksch_encrypt[48];
  54   52          uint64_t ksch_decrypt[48];
  55   53  } keysched3_t;
  56   54  
↓ open down ↓ 437 lines elided ↑ open up ↑
 494  492                          r = t;
 495  493                  }
 496  494                  r = l;
 497  495                  l = t;
 498  496          }
 499  497  
 500  498          return (des_fp(l, r));
 501  499  }
 502  500  #endif /* !sun4u */
 503  501  
 504      -/* EXPORT DELETE END */
 505      -
 506  502  int
 507  503  des3_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN],
 508  504      uint8_t out_block[DES_BLOCK_LEN], boolean_t decrypt)
 509  505  {
 510      -/* EXPORT DELETE START */
 511  506          keysched3_t *ksch = (keysched3_t *)cookie;
 512  507  
 513  508          /*
 514  509           * The code below, that is always executed on LITTLE_ENDIAN machines,
 515  510           * reverses bytes in the block.  On BIG_ENDIAN, the same code
 516  511           * copies the block without reversing bytes.
 517  512           */
 518  513  #ifdef _BIG_ENDIAN
 519  514          if (IS_P2ALIGNED(block, sizeof (uint64_t)) &&
 520  515              IS_P2ALIGNED(out_block, sizeof (uint64_t))) {
↓ open down ↓ 32 lines elided ↑ open up ↑
 553  548                  out_block[0] = tmp >> 56;
 554  549                  out_block[1] = tmp >> 48;
 555  550                  out_block[2] = tmp >> 40;
 556  551                  out_block[3] = tmp >> 32;
 557  552                  out_block[4] = tmp >> 24;
 558  553                  out_block[5] = tmp >> 16;
 559  554                  out_block[6] = tmp >> 8;
 560  555                  out_block[7] = (uint8_t)tmp;
 561  556  #endif  /* UNALIGNED_POINTERS_PERMITTED */
 562  557          }
 563      -/* EXPORT DELETE END */
 564  558          return (CRYPTO_SUCCESS);
 565  559  }
 566  560  
 567  561  int
 568  562  des_crunch_block(const void *cookie, const uint8_t block[DES_BLOCK_LEN],
 569  563      uint8_t out_block[DES_BLOCK_LEN], boolean_t decrypt)
 570  564  {
 571      -/* EXPORT DELETE START */
 572  565          keysched_t *ksch = (keysched_t *)cookie;
 573  566  
 574  567          /*
 575  568           * The code below, that is always executed on LITTLE_ENDIAN machines,
 576  569           * reverses bytes in the block.  On BIG_ENDIAN, the same code
 577  570           * copies the block without reversing bytes.
 578  571           */
 579  572  #ifdef _BIG_ENDIAN
 580  573          if (IS_P2ALIGNED(block, sizeof (uint64_t)) &&
 581  574              IS_P2ALIGNED(out_block, sizeof (uint64_t))) {
↓ open down ↓ 34 lines elided ↑ open up ↑
 616  609                  out_block[0] = tmp >> 56;
 617  610                  out_block[1] = tmp >> 48;
 618  611                  out_block[2] = tmp >> 40;
 619  612                  out_block[3] = tmp >> 32;
 620  613                  out_block[4] = tmp >> 24;
 621  614                  out_block[5] = tmp >> 16;
 622  615                  out_block[6] = tmp >> 8;
 623  616                  out_block[7] = (uint8_t)tmp;
 624  617  #endif  /* UNALIGNED_POINTERS_PERMITTED */
 625  618          }
 626      -/* EXPORT DELETE END */
 627  619          return (CRYPTO_SUCCESS);
 628  620  }
 629  621  
 630  622  static boolean_t
 631  623  keycheck(uint8_t *key, uint8_t *corrected_key)
 632  624  {
 633      -/* EXPORT DELETE START */
 634  625          uint64_t key_so_far;
 635  626          uint_t i;
 636  627          /*
 637  628           * Table of weak and semi-weak keys.  Fortunately, weak keys are
 638  629           * endian-independent, and some semi-weak keys can be paired up in
 639  630           * endian-opposite order.  Since keys are stored as uint64_t's,
 640  631           * use the ifdef _LITTLE_ENDIAN where appropriate.
 641  632           */
 642  633          static uint64_t des_weak_keys[] = {
 643  634                  /* Really weak keys.  Byte-order independent values. */
↓ open down ↓ 65 lines elided ↑ open up ↑
 709  700                  corrected_key[0] = key_so_far >> 56;
 710  701                  corrected_key[1] = key_so_far >> 48;
 711  702                  corrected_key[2] = key_so_far >> 40;
 712  703                  corrected_key[3] = key_so_far >> 32;
 713  704                  corrected_key[4] = key_so_far >> 24;
 714  705                  corrected_key[5] = key_so_far >> 16;
 715  706                  corrected_key[6] = key_so_far >> 8;
 716  707                  corrected_key[7] = (uint8_t)key_so_far;
 717  708  #endif  /* UNALIGNED_POINTERS_PERMITTED */
 718  709          }
 719      -/* EXPORT DELETE END */
 720  710          return (B_TRUE);
 721  711  }
 722  712  
 723  713  static boolean_t
 724  714  des23_keycheck(uint8_t *key, uint8_t *corrected_key, boolean_t des3)
 725  715  {
 726      -/* EXPORT DELETE START */
 727  716          uint64_t aligned_key[DES3_KEYSIZE / sizeof (uint64_t)];
 728  717          uint64_t key_so_far, scratch, *currentkey;
 729  718          uint_t j, num_weakkeys = 0;
 730  719          uint8_t keysize = DES3_KEYSIZE;
 731  720          uint8_t checks = 3;
 732  721  
 733  722          if (key == NULL) {
 734  723                  return (B_FALSE);
 735  724          }
 736  725  
↓ open down ↓ 37 lines elided ↑ open up ↑
 774  763           * 1st and 2nd keys must be unique, the 3rd key can be the same as
 775  764           * the 1st key for the 2 key variant of 3DES.
 776  765           */
 777  766          if (currentkey[0] == currentkey[1] || currentkey[1] == currentkey[2])
 778  767                  return (B_FALSE);
 779  768  
 780  769          if (corrected_key != NULL) {
 781  770                  bcopy(currentkey, corrected_key, keysize);
 782  771          }
 783  772  
 784      -/* EXPORT DELETE END */
 785  773          return (B_TRUE);
 786  774  }
 787  775  
 788  776  boolean_t
 789  777  des_keycheck(uint8_t *key, des_strength_t strength, uint8_t *corrected_key)
 790  778  {
 791  779          if (strength == DES) {
 792  780                  return (keycheck(key, corrected_key));
 793  781          } else if (strength == DES2) {
 794  782                  return (des23_keycheck(key, corrected_key, B_FALSE));
 795  783          } else if (strength == DES3) {
 796  784                  return (des23_keycheck(key, corrected_key, B_TRUE));
 797  785          } else {
 798  786                  return (B_FALSE);
 799  787          }
 800  788  }
 801  789  
 802  790  void
 803  791  des_parity_fix(uint8_t *key, des_strength_t strength, uint8_t *corrected_key)
 804  792  {
 805      -/* EXPORT DELETE START */
 806  793          uint64_t aligned_key[DES3_KEYSIZE / sizeof (uint64_t)];
 807  794          uint8_t *paritied_key;
 808  795          uint64_t key_so_far;
 809  796          int i = 0, offset = 0;
 810  797  
 811  798          if (strength == DES)
 812  799                  bcopy(key, aligned_key, DES_KEYSIZE);
 813  800          else
 814  801                  bcopy(key, aligned_key, DES3_KEYSIZE);
 815  802  
↓ open down ↓ 25 lines elided ↑ open up ↑
 841  828                  paritied_key[offset + 4] = key_so_far >> 24;
 842  829                  paritied_key[offset + 5] = key_so_far >> 16;
 843  830                  paritied_key[offset + 6] = key_so_far >> 8;
 844  831                  paritied_key[offset + 7] = (uint8_t)key_so_far;
 845  832  #endif  /* UNALIGNED_POINTERS_PERMITTED */
 846  833  
 847  834                  i++;
 848  835          }
 849  836  
 850  837          bcopy(paritied_key, corrected_key, DES_KEYSIZE * strength);
 851      -/* EXPORT DELETE END */
 852  838  }
 853  839  
 854  840  
 855  841  /*
 856  842   * Initialize key schedule for DES, DES2, and DES3
 857  843   */
 858  844  void
 859  845  des_init_keysched(uint8_t *cipherKey, des_strength_t strength, void *ks)
 860  846  {
 861      -/* EXPORT DELETE START */
 862  847          uint64_t *encryption_ks;
 863  848          uint64_t *decryption_ks;
 864  849          uint64_t keysched[48];
 865  850          uint64_t key_uint64[3];
 866  851          uint64_t tmp;
 867  852          uint_t keysize, i, j;
 868  853  
 869  854          switch (strength) {
 870  855          case DES:
 871  856                  keysize = DES_KEYSIZE;
↓ open down ↓ 68 lines elided ↑ open up ↑
 940  925  
 941  926          /* reverse the key schedule */
 942  927          for (i = 0; i < keysize; i++) {
 943  928                  tmp = keysched[i];
 944  929                  keysched[i] = keysched[2 * keysize - 1 - i];
 945  930                  keysched[2 * keysize -1 -i] = tmp;
 946  931          }
 947  932  
 948  933          /* save the decryption keyschedule */
 949  934          bcopy(keysched, decryption_ks, keysize * 16);
 950      -/* EXPORT DELETE END */
 951  935  }
 952  936  
 953  937  /*
 954  938   * Allocate key schedule.
 955  939   */
 956  940  /*ARGSUSED*/
 957  941  void *
 958  942  des_alloc_keysched(size_t *keysched_size, des_strength_t strength, int kmflag)
 959  943  {
 960  944          void *keysched;
 961  945  
 962      -/* EXPORT DELETE START */
 963      -
 964  946          size_t size;
 965  947  
 966  948          switch (strength) {
 967  949          case DES:
 968  950                  size = sizeof (keysched_t);
 969  951                  break;
 970  952          case DES2:
 971  953          case DES3:
 972  954                  size = sizeof (keysched3_t);
 973  955          }
↓ open down ↓ 3 lines elided ↑ open up ↑
 977  959  #else   /* !_KERNEL */
 978  960          keysched = (keysched_t *)malloc(size);
 979  961  #endif  /* _KERNEL */
 980  962  
 981  963          if (keysched == NULL)
 982  964                  return (NULL);
 983  965  
 984  966          if (keysched_size != NULL)
 985  967                  *keysched_size = size;
 986  968  
 987      -/* EXPORT DELETE END */
 988      -
 989  969          return (keysched);
 990  970  }
 991  971  
 992  972  /*
 993  973   * Replace the LSB of each byte by the xor of the other
 994  974   * 7 bits.  The tricky thing is that the original contents of the LSBs
 995  975   * are nullified by including them twice in the xor computation.
 996  976   */
 997  977  static void
 998  978  fix_des_parity(uint64_t *keyp)
 999  979  {
1000      -/* EXPORT DELETE START */
1001  980          uint64_t k = *keyp;
1002  981          k ^= k >> 1;
1003  982          k ^= k >> 2;
1004  983          k ^= k >> 4;
1005  984          *keyp ^= (k & 0x0101010101010101ULL);
1006  985          *keyp ^= 0x0101010101010101ULL;
1007      -/* EXPORT DELETE END */
1008  986  }
1009  987  
1010  988  void
1011  989  des_copy_block(uint8_t *in, uint8_t *out)
1012  990  {
1013  991          if (IS_P2ALIGNED(in, sizeof (uint32_t)) &&
1014  992              IS_P2ALIGNED(out, sizeof (uint32_t))) {
1015  993                  /* LINTED: pointer alignment */
1016  994                  *(uint32_t *)&out[0] = *(uint32_t *)&in[0];
1017  995                  /* LINTED: pointer alignment */
↓ open down ↓ 116 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX