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