xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/enc_provider/des.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
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 #include <enc_provider.h>
37 
38 static krb5_error_code
39 k5_des_docrypt(krb5_context context, krb5_const krb5_keyblock *key,
40 	krb5_const krb5_data *ivec, krb5_const krb5_data *input,
41 	krb5_data *output, int encrypt)
42 {
43     krb5_error_code ret;
44 
45     KRB5_LOG(KRB5_INFO, "k5_des_docrypt() start encrypt=%d\n", encrypt);
46     /* key->enctype was checked by the caller */
47 
48     if (key->length != 8)
49 	return(KRB5_BAD_KEYSIZE);
50     if ((input->length%8) != 0)
51 	return(KRB5_BAD_MSIZE);
52     if (ivec && (ivec->length != 8))
53 	return(KRB5_BAD_MSIZE);
54     if (input->length != output->length)
55 	return(KRB5_BAD_MSIZE);
56 
57     ret = mit_des_cbc_encrypt(context, (krb5_pointer) input->data,
58 	(krb5_pointer) output->data, input->length,
59 	(krb5_keyblock *)key,
60 	ivec?(unsigned char *)ivec->data:
61 	(unsigned char *)mit_des_zeroblock, encrypt);
62 
63     KRB5_LOG0(KRB5_INFO, "k5_des_docrypt() end\n");
64     return(ret);
65 }
66 
67 static krb5_error_code
68 k5_des_encrypt(krb5_context context, krb5_const krb5_keyblock *key,
69 		krb5_const krb5_data *ivec, krb5_const krb5_data *input,
70 		krb5_data *output)
71 {
72     return(k5_des_docrypt(context, key, ivec, input, output, 1));
73 }
74 
75 static krb5_error_code
76 k5_des_decrypt(krb5_context context, krb5_const krb5_keyblock *key,
77 		krb5_const krb5_data *ivec, krb5_const krb5_data *input,
78 		krb5_data *output)
79 {
80     return(k5_des_docrypt(context, key, ivec, input, output, 0));
81 }
82 
83 static krb5_error_code
84 k5_des_make_key(krb5_context context, krb5_const krb5_data *randombits,
85 		krb5_keyblock *key)
86 {
87     krb5_error_code ret = 0;
88     if (key->length != 8)
89 	return(KRB5_BAD_KEYSIZE);
90     if (randombits->length != 7)
91 	return(KRB5_CRYPTO_INTERNAL);
92 
93     key->magic = KV5M_KEYBLOCK;
94     key->length = 8;
95     key->dk_list = NULL;
96 
97     /* take the seven bytes, move them around into the top 7 bits of the
98        8 key bytes, then compute the parity bits */
99 
100     (void) memcpy(key->contents, randombits->data, randombits->length);
101     key->contents[7] = (((key->contents[0]&1)<<1) | ((key->contents[1]&1)<<2) |
102 			((key->contents[2]&1)<<3) | ((key->contents[3]&1)<<4) |
103 			((key->contents[4]&1)<<5) | ((key->contents[5]&1)<<6) |
104 			((key->contents[6]&1)<<7));
105 
106     mit_des_fixup_key_parity(key->contents);
107 
108 #ifdef _KERNEL
109     key->kef_key.ck_data = NULL;
110     key->key_tmpl = NULL;
111     ret = init_key_kef(context->kef_cipher_mt, key);
112 #else
113     key->hKey = CK_INVALID_HANDLE;
114     ret = init_key_uef(krb_ctx_hSession(context), key);
115 #endif /* _KERNEL */
116 
117     return (ret);
118 }
119 
120 const struct krb5_enc_provider krb5_enc_des = {
121     8,
122     7, 8,
123     k5_des_encrypt,
124     k5_des_decrypt,
125     k5_des_make_key,
126     krb5int_des_init_state,
127     krb5int_default_free_state
128 };
129