6a3484b65bf48104ebcc882977e76602976ad12e
[openafs.git] / src / rxkad / domestic / 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 RCSID
22     ("$Header$");
23
24 #ifdef KERNEL
25 #include "afs/stds.h"
26 #ifndef UKERNEL
27 #include "h/types.h"
28 #include "rx/rx.h"
29 #include "netinet/in.h"
30 #else /* !UKERNEL */
31 #include "afs/sysincludes.h"
32 #include "rx/rx.h"
33 #endif /* !UKERNEL */
34 #else /* !KERNEL */
35 #include <afs/stds.h>
36 #include <sys/types.h>
37 #include <rx/rx.h>
38 #ifdef AFS_NT40_ENV
39 #include <winsock2.h>
40 #else
41 #include <netinet/in.h>
42 #endif
43 #endif /* KERNEL */
44
45 #include <des/stats.h>
46 #include "private_data.h"
47 #define XPRT_RXKAD_CRYPT
48
49 afs_int32
50 rxkad_DecryptPacket(const struct rx_connection *conn,
51                     const fc_KeySchedule * schedule,
52                     const fc_InitializationVector * ivec, const int inlen,
53                     struct rx_packet *packet)
54 {
55     afs_uint32 xor[2];
56     struct rx_securityClass *obj;
57     struct rxkad_cprivate *tp;  /* s & c have type at same offset */
58     char *data;
59     int i, tlen, len;
60
61     len = inlen;
62
63     obj = rx_SecurityObjectOf(conn);
64     tp = (struct rxkad_cprivate *)obj->privateData;
65     ADD_RXKAD_STATS(bytesDecrypted[rxkad_TypeIndex(tp->type)],len);
66     memcpy((void *)xor, (void *)ivec, sizeof(xor));
67     for (i = 0; len; i++) {
68         data = rx_data(packet, i, tlen);
69         if (!data || !tlen)
70             break;
71         tlen = MIN(len, tlen);
72         fc_cbc_encrypt(data, data, tlen, schedule, xor, DECRYPT);
73         len -= tlen;
74     }
75     /* Do this if packet checksums are ever enabled (below), but
76      * current version just passes zero
77      afs_int32 cksum;
78      cksum = ntohl(rx_GetInt32(packet, 1));
79      */
80     return 0;
81 }
82
83 afs_int32
84 rxkad_EncryptPacket(const struct rx_connection * conn,
85                     const fc_KeySchedule * schedule,
86                     const fc_InitializationVector * ivec, const int inlen,
87                     struct rx_packet * packet)
88 {
89     afs_uint32 xor[2];
90     struct rx_securityClass *obj;
91     struct rxkad_cprivate *tp;  /* s & c have type at same offset */
92     char *data;
93     int i, tlen, len;
94
95     len = inlen;
96
97     obj = rx_SecurityObjectOf(conn);
98     tp = (struct rxkad_cprivate *)obj->privateData;
99     ADD_RXKAD_STATS(bytesEncrypted[rxkad_TypeIndex(tp->type)],len);
100     /*
101      * afs_int32 cksum;
102      * cksum = htonl(0);                
103      * * Future option to add cksum here, but for now we just put 0
104      */
105     rx_PutInt32(packet, 1 * sizeof(afs_int32), 0);
106
107     memcpy((void *)xor, (void *)ivec, sizeof(xor));
108     for (i = 0; len; i++) {
109         data = rx_data(packet, i, tlen);
110         if (!data || !tlen)
111             break;
112         tlen = MIN(len, tlen);
113         fc_cbc_encrypt(data, data, tlen, schedule, xor, ENCRYPT);
114         len -= tlen;
115     }
116     return 0;
117 }