1 /*
   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 2009 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 /*
  26  * Copyright 2015 by Saso Kiselkov. All rights reserved.
  27  */
  28 
  29 #ifndef _DES_IMPL_H
  30 #define _DES_IMPL_H
  31 
  32 /*
  33  * Common definitions used by DES
  34  */
  35 
  36 #ifdef  __cplusplus
  37 extern "C" {
  38 #endif
  39 
  40 #define DES_BLOCK_LEN   8
  41 
  42 #define DES_COPY_BLOCK(src, dst) \
  43         (dst)[0] = (src)[0]; \
  44         (dst)[1] = (src)[1]; \
  45         (dst)[2] = (src)[2]; \
  46         (dst)[3] = (src)[3]; \
  47         (dst)[4] = (src)[4]; \
  48         (dst)[5] = (src)[5]; \
  49         (dst)[6] = (src)[6]; \
  50         (dst)[7] = (src)[7];
  51 
  52 #define DES_XOR_BLOCK(src, dst) \
  53         (dst)[0] ^= (src)[0]; \
  54         (dst)[1] ^= (src)[1]; \
  55         (dst)[2] ^= (src)[2]; \
  56         (dst)[3] ^= (src)[3]; \
  57         (dst)[4] ^= (src)[4]; \
  58         (dst)[5] ^= (src)[5]; \
  59         (dst)[6] ^= (src)[6]; \
  60         (dst)[7] ^= (src)[7]
  61 
  62 typedef enum des_strength {
  63         DES = 1,
  64         DES2,
  65         DES3
  66 } des_strength_t;
  67 
  68 #define DES3_STRENGTH   0x08000000
  69 
  70 #define DES_KEYSIZE     8
  71 #define DES_MINBITS     64
  72 #define DES_MAXBITS     64
  73 #define DES_MINBYTES    (DES_MINBITS / 8)
  74 #define DES_MAXBYTES    (DES_MAXBITS / 8)
  75 #define DES_IV_LEN      8
  76 
  77 #define DES2_KEYSIZE    (2 * DES_KEYSIZE)
  78 #define DES2_MINBITS    (2 * DES_MINBITS)
  79 #define DES2_MAXBITS    (2 * DES_MAXBITS)
  80 #define DES2_MINBYTES   (DES2_MINBITS / 8)
  81 #define DES2_MAXBYTES   (DES2_MAXBITS / 8)
  82 
  83 #define DES3_KEYSIZE    (3 * DES_KEYSIZE)
  84 #define DES3_MINBITS    (2 * DES_MINBITS)       /* DES3 handles CKK_DES2 keys */
  85 #define DES3_MAXBITS    (3 * DES_MAXBITS)
  86 #define DES3_MINBYTES   (DES3_MINBITS / 8)
  87 #define DES3_MAXBYTES   (DES3_MAXBITS / 8)
  88 
  89 extern int des_encrypt_contiguous_blocks(void *, char *, size_t,
  90     crypto_data_t *);
  91 extern int des_decrypt_contiguous_blocks(void *, char *, size_t,
  92     crypto_data_t *);
  93 extern uint64_t des_crypt_impl(uint64_t *, uint64_t, int);
  94 extern void des_ks(uint64_t *, uint64_t);
  95 extern int des_crunch_block(const void *, const uint8_t *, uint8_t *,
  96     boolean_t);
  97 extern int des3_crunch_block(const void *, const uint8_t *, uint8_t *,
  98     boolean_t);
  99 extern void des_init_keysched(uint8_t *, des_strength_t, void *);
 100 extern void *des_alloc_keysched(size_t *, des_strength_t, int);
 101 extern boolean_t des_keycheck(uint8_t *, des_strength_t, uint8_t *);
 102 extern void des_parity_fix(uint8_t *, des_strength_t, uint8_t *);
 103 extern void des_copy_block(const uint8_t *, uint8_t *);
 104 extern void des_xor_block(const uint8_t *, uint8_t *);
 105 extern int des_encrypt_block(const void *, const uint8_t *, uint8_t *);
 106 extern int des3_encrypt_block(const void *, const uint8_t *, uint8_t *);
 107 extern int des_decrypt_block(const void *, const uint8_t *, uint8_t *);
 108 extern int des3_decrypt_block(const void *, const uint8_t *, uint8_t *);
 109 
 110 #ifdef _DES_IMPL
 111 
 112 #ifdef _KERNEL
 113 typedef enum des_mech_type {
 114         DES_ECB_MECH_INFO_TYPE,         /* SUN_CKM_DES_ECB */
 115         DES_CBC_MECH_INFO_TYPE,         /* SUN_CKM_DES_CBC */
 116         DES_CFB_MECH_INFO_TYPE,         /* SUN_CKM_DES_CFB */
 117         DES3_ECB_MECH_INFO_TYPE,        /* SUN_CKM_DES3_ECB */
 118         DES3_CBC_MECH_INFO_TYPE,        /* SUN_CKM_DES3_CBC */
 119         DES3_CFB_MECH_INFO_TYPE         /* SUN_CKM_DES3_CFB */
 120 } des_mech_type_t;
 121 
 122 #endif  /* _KERNEL */
 123 #endif  /* _DES_IMPL */
 124 
 125 #ifdef  __cplusplus
 126 }
 127 #endif
 128 
 129 #endif  /* _DES_IMPL_H */