1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * Copyright (C) 1998 by the FundsXpress, INC.
10  *
11  * All rights reserved.
12  *
13  * Export of this software from the United States of America may require
14  * a specific license from the United States Government.  It is the
15  * responsibility of any person or organization contemplating export to
16  * obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19  * distribute this software and its documentation for any purpose and
20  * without fee is hereby granted, provided that the above copyright
21  * notice appear in all copies and that both that copyright notice and
22  * this permission notice appear in supporting documentation, and that
23  * the name of FundsXpress. not be used in advertising or publicity pertaining
24  * to distribution of the software without specific, written prior
25  * permission.  FundsXpress makes no representations about the suitability of
26  * this software for any purpose.  It is provided "as is" without express
27  * or implied warranty.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  */
33 
34 #include <k5-int.h>
35 #include <des_int.h>
36 
37 static krb5_error_code
38 k5_des3_docrypt(krb5_context context,
39 	krb5_const krb5_keyblock *key, krb5_const krb5_data *ivec,
40 	krb5_const krb5_data *input, krb5_data *output, int encrypt)
41 {
42     /* LINTED */
43     krb5_error_code ret;
44 
45     KRB5_LOG0(KRB5_INFO, "k5_des3_docrypt() start");
46 
47     /* key->enctype was checked by the caller */
48 
49     if (key->length != 24)
50 	return(KRB5_BAD_KEYSIZE);
51     if ((input->length%8) != 0)
52 	return(KRB5_BAD_MSIZE);
53     if (ivec && (ivec->length != 8))
54 	return(KRB5_BAD_MSIZE);
55     if (input->length != output->length)
56 	return(KRB5_BAD_MSIZE);
57 
58     ret = mit_des3_cbc_encrypt(context, (krb5_pointer) input->data,
59 	(krb5_pointer) output->data, input->length,
60 	(krb5_keyblock *)key,
61 	ivec?(unsigned char *)ivec->data:(unsigned char *)mit_des_zeroblock,
62 	encrypt);
63 
64     KRB5_LOG0(KRB5_INFO, "k5_des3_docrypt() end\n");
65     return(ret);
66 }
67 
68 static krb5_error_code
69 k5_des3_encrypt(krb5_context context,
70 	krb5_const krb5_keyblock *key, krb5_const krb5_data *ivec,
71 	krb5_const krb5_data *input, krb5_data *output)
72 {
73     return(k5_des3_docrypt(context, key, ivec, input, output, 1));
74 }
75 
76 static krb5_error_code
77 k5_des3_decrypt(krb5_context context,
78 	krb5_const krb5_keyblock *key, krb5_const krb5_data *ivec,
79 	krb5_const krb5_data *input, krb5_data *output)
80 {
81     return(k5_des3_docrypt(context, key, ivec, input, output, 0));
82 }
83 
84 static krb5_error_code
85 k5_des3_make_key(krb5_context context, krb5_const krb5_data *randombits,
86 		krb5_keyblock *key)
87 {
88     int i;
89     krb5_error_code ret = 0;
90 
91     KRB5_LOG0(KRB5_INFO, "k5_des3_make_key() start\n");
92 
93     if (key->length != 24)
94 	return(KRB5_BAD_KEYSIZE);
95     if (randombits->length != 21)
96 	return(KRB5_CRYPTO_INTERNAL);
97 
98     key->magic = KV5M_KEYBLOCK;
99     key->length = 24;
100     key->dk_list = NULL;
101 
102     /* take the seven bytes, move them around into the top 7 bits of the
103        8 key bytes, then compute the parity bits.  Do this three times. */
104 
105     for (i=0; i<3; i++) {
106 	(void) memcpy(key->contents+i*8, randombits->data+i*7, 7);
107 	key->contents[i*8+7] = (((key->contents[i*8]&1)<<1) |
108 				((key->contents[i*8+1]&1)<<2) |
109 				((key->contents[i*8+2]&1)<<3) |
110 				((key->contents[i*8+3]&1)<<4) |
111 				((key->contents[i*8+4]&1)<<5) |
112 				((key->contents[i*8+5]&1)<<6) |
113 				((key->contents[i*8+6]&1)<<7));
114 
115 	mit_des_fixup_key_parity(key->contents+i*8);
116     }
117 #ifdef _KERNEL
118     key->kef_key.ck_data = NULL;
119     key->key_tmpl = NULL;
120     ret = init_key_kef(context->kef_cipher_mt, key);
121 #else
122     key->hKey = CK_INVALID_HANDLE;
123     ret = init_key_uef(krb_ctx_hSession(context), key);
124 #endif /* _KERNEL */
125     KRB5_LOG0(KRB5_INFO, "k5_des3_make_key() end\n");
126     return(ret);
127 }
128 
129 const struct krb5_enc_provider krb5_enc_des3 = {
130     8,
131     21, 24,
132     k5_des3_encrypt,
133     k5_des3_decrypt,
134     k5_des3_make_key,
135     krb5int_des_init_state,
136     krb5int_default_free_state
137 };
138