Windows global stuff:
[krb5.git] / src / lib / gssapi / krb5 / util_crypt.c
1 /*
2  * Copyright 1993 by OpenVision Technologies, Inc.
3  * 
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without fee,
6  * provided that the above copyright notice appears in all copies and
7  * that both that copyright notice and this permission notice appear in
8  * supporting documentation, and that the name of OpenVision not be used
9  * in advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission. OpenVision makes no
11  * representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  * 
14  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20  * PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include "gssapiP_krb5.h"
24 #include <memory.h>
25
26 static unsigned char zeros[8] = {0,0,0,0,0,0,0,0};
27
28 int
29 kg_confounder_size(ed)
30      krb5_gss_enc_desc *ed;
31 {
32    /* XXX Is this an abstraction violation? */
33
34    return(ed->eblock.crypto_entry->block_length);
35 }
36
37 krb5_error_code
38 kg_make_confounder(ed, buf)
39      krb5_gss_enc_desc *ed;
40      unsigned char *buf;
41 {
42    return(krb5_random_confounder( ed->eblock.crypto_entry->block_length, buf));
43 }
44
45 int
46 kg_encrypt_size(ed, n)
47      krb5_gss_enc_desc *ed;
48      int n;
49 {
50    return(krb5_encrypt_size(n, ed->eblock.crypto_entry));
51 }
52
53 krb5_error_code
54 kg_encrypt(ed, iv, in, out, length)
55      krb5_gss_enc_desc *ed;
56      krb5_pointer iv;
57      krb5_pointer in;
58      krb5_pointer out;
59      int length;
60 {
61    krb5_error_code code;
62
63    if (!kg_context && (code=kg_get_context()))
64            return code;
65    
66    if (! ed->processed) {
67       if (code = krb5_process_key(kg_context, &ed->eblock, ed->key))
68          return(code);
69       ed->processed = 1;
70    }
71
72    if (code = krb5_encrypt(kg_context, in, out, length, &ed->eblock, 
73                            iv?iv:(krb5_pointer)zeros))
74       return(code);
75
76    return(0);
77 }
78
79 /* length is the length of the cleartext. */
80
81 krb5_error_code
82 kg_decrypt(ed, iv, in, out, length)
83      krb5_gss_enc_desc *ed;
84      krb5_pointer iv;
85      krb5_pointer in;
86      krb5_pointer out;
87      int length;
88 {
89    krb5_error_code code;
90    int elen;
91    char *buf;
92
93    if (!kg_context && (code=kg_get_context()))
94            return code;
95    
96    if (! ed->processed) {
97       if (code = krb5_process_key(kg_context, &ed->eblock, ed->key))
98          return(code);
99       ed->processed = 1;
100    }
101
102    elen = krb5_encrypt_size(length, ed->eblock.crypto_entry);
103    if ((buf = (char *) xmalloc(elen)) == NULL)
104       return(ENOMEM);
105
106    if (code = krb5_decrypt(kg_context, in, buf, elen, &ed->eblock, 
107                            iv?iv:(krb5_pointer)zeros)) {
108       xfree(buf);
109       return(code);
110    }
111
112    memcpy(out, buf, length);
113    xfree(buf);
114
115    return(0);
116 }