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