4f78e0d69e6d172d840a66a03a71738f03b49712
[openafs.git] / src / rxkad / crypt_conn.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* The rxkad security object.  This contains packet processing routines that
11  * are prohibited from being exported. */
12
13
14 #include <afsconfig.h>
15 #ifdef KERNEL
16 #include "afs/param.h"
17 #else
18 #include <afs/param.h>
19 #endif
20
21
22 #ifdef KERNEL
23 #include "afs/stds.h"
24 #ifndef UKERNEL
25 #include "h/types.h"
26 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_XBSD_ENV)
27 #include "h/systm.h"
28 #endif
29 #include "rx/rx.h"
30 #include "netinet/in.h"
31 #else /* !UKERNEL */
32 #include "afs/sysincludes.h"
33 #include "rx/rx.h"
34 #endif /* !UKERNEL */
35 #else /* !KERNEL */
36 #include <afs/stds.h>
37 #include <sys/types.h>
38 #include <string.h>
39 #include <rx/rx.h>
40 #ifdef AFS_NT40_ENV
41 #include <winsock2.h>
42 #else
43 #include <netinet/in.h>
44 #endif
45 #endif /* KERNEL */
46
47 #include <des/stats.h>
48 #include "private_data.h"
49 #define XPRT_RXKAD_CRYPT
50
51 afs_int32
52 rxkad_DecryptPacket(const struct rx_connection *conn,
53                     const fc_KeySchedule * schedule,
54                     const fc_InitializationVector * ivec, const int inlen,
55                     struct rx_packet *packet)
56 {
57     afs_uint32 xor[2];
58     struct rx_securityClass *obj;
59     struct rxkad_cprivate *tp;  /* s & c have type at same offset */
60     char *data;
61     int i, tlen, len;
62
63     len = inlen;
64
65     obj = rx_SecurityObjectOf(conn);
66     tp = (struct rxkad_cprivate *)obj->privateData;
67     ADD_RXKAD_STATS(bytesDecrypted[rxkad_TypeIndex(tp->type)],len);
68     memcpy((void *)xor, (void *)ivec, sizeof(xor));
69     for (i = 0; len; i++) {
70         data = rx_data(packet, i, tlen);
71         if (!data || !tlen)
72             break;
73         tlen = MIN(len, tlen);
74         fc_cbc_encrypt(data, data, tlen, *schedule, xor, DECRYPT);
75         len -= tlen;
76     }
77     /* Do this if packet checksums are ever enabled (below), but
78      * current version just passes zero
79      afs_int32 cksum;
80      cksum = ntohl(rx_GetInt32(packet, 1));
81      */
82     return 0;
83 }
84
85 afs_int32
86 rxkad_EncryptPacket(const struct rx_connection * conn,
87                     const fc_KeySchedule * schedule,
88                     const fc_InitializationVector * ivec, const int inlen,
89                     struct rx_packet * packet)
90 {
91     afs_uint32 xor[2];
92     struct rx_securityClass *obj;
93     struct rxkad_cprivate *tp;  /* s & c have type at same offset */
94     char *data;
95     int i, tlen, len;
96
97     len = inlen;
98
99     obj = rx_SecurityObjectOf(conn);
100     tp = (struct rxkad_cprivate *)obj->privateData;
101     ADD_RXKAD_STATS(bytesEncrypted[rxkad_TypeIndex(tp->type)],len);
102     /*
103      * afs_int32 cksum;
104      * cksum = htonl(0);
105      * * Future option to add cksum here, but for now we just put 0
106      */
107     rx_PutInt32(packet, 1 * sizeof(afs_int32), 0);
108
109     memcpy((void *)xor, (void *)ivec, sizeof(xor));
110     for (i = 0; len; i++) {
111         data = rx_data(packet, i, tlen);
112         if (!data || !tlen)
113             break;
114         tlen = MIN(len, tlen);
115         fc_cbc_encrypt(data, data, tlen, *schedule, xor, ENCRYPT);
116         len -= tlen;
117     }
118     return 0;
119 }