Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / rxkad / domestic / crypt_conn.c
1 /* The rxkad security object.  This contains packet processing routines that
2  * are prohibited from being exported. */
3
4 /*
5 ****************************************************************************
6 *        Copyright IBM Corporation 1988, 1989 - All Rights Reserved        *
7 *                                                                          *
8 * Permission to use, copy, modify, and distribute this software and its    *
9 * documentation for any purpose and without fee is hereby granted,         *
10 * provided that the above copyright notice appear in all copies and        *
11 * that both that copyright notice and this permission notice appear in     *
12 * supporting documentation, and that the name of IBM not be used in        *
13 * advertising or publicity pertaining to distribution of the software      *
14 * without specific, written prior permission.                              *
15 *                                                                          *
16 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY      *
19 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER  *
20 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING   *
21 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.    *
22 ****************************************************************************
23 */
24
25
26 #ifdef KERNEL
27
28 #include "../afs/param.h"
29 #include "../afs/stds.h"
30 #ifndef UKERNEL
31 #include "../h/types.h"
32 #include "../rx/rx.h"
33 #include "../netinet/in.h"
34 #else /* !UKERNEL */
35 #include "../afs/sysincludes.h"
36 #include "../rx/rx.h"
37 #endif /* !UKERNEL */
38
39 #else /* KERNEL */
40
41 #include <afs/param.h>
42 #include <afs/stds.h>
43 #include <sys/types.h>
44 #include <rx/rx.h>
45 #ifdef AFS_NT40_ENV
46 #include <winsock2.h>
47 #else
48 #include <netinet/in.h>
49 #endif
50 #endif /* KERNEL */
51
52 #include "private_data.h"
53 #define XPRT_RXKAD_CRYPT
54 #ifdef KERNEL
55 #include "../afs/permit_xprt.h"
56 #else
57 #include "../permit_xprt.h"
58 #endif
59
60
61 AFS_HIDE
62 afs_int32 rxkad_DecryptPacket (conn, schedule, ivec, len, packet)
63   IN struct rx_connection *conn;
64   IN fc_KeySchedule *schedule;
65   IN fc_InitializationVector *ivec;
66   IN int len;
67   INOUT struct rx_packet *packet;
68 {
69     afs_uint32 xor[2];
70     struct rx_securityClass *obj;
71     struct rxkad_cprivate *tp;          /* s & c have type at same offset */
72     char * data;
73     int i,tlen;
74
75     if (!xprt_CryptOK (conn)) return RXKADILLEGALLEVEL;
76     obj = rx_SecurityObjectOf(conn);
77     tp = (struct rxkad_cprivate *)obj->privateData;
78     LOCK_RXKAD_STATS
79     rxkad_stats.bytesDecrypted[rxkad_TypeIndex(tp->type)] += len;
80     UNLOCK_RXKAD_STATS
81
82     bcopy ((void *)ivec, (void *)xor, sizeof(xor));
83     for (i = 0; len ; i++) {
84       data = rx_data(packet, i, tlen);
85       if (!data || !tlen)
86         break;
87       tlen = MIN(len, tlen);
88       fc_cbc_encrypt (data, data, tlen, schedule, xor, DECRYPT);
89       len -= tlen;
90     }
91     /* Do this if packet checksums are ever enabled (below), but
92      * current version just passes zero
93     afs_int32 cksum;
94     cksum = ntohl(rx_GetInt32(packet, 1));
95     */
96     return 0;
97 }
98
99 AFS_HIDE
100 afs_int32 rxkad_EncryptPacket (conn, schedule, ivec, len, packet)
101   IN struct rx_connection *conn;
102   IN fc_KeySchedule *schedule;
103   IN fc_InitializationVector *ivec;
104   IN int len;
105   INOUT struct rx_packet *packet;
106 {
107     afs_uint32 xor[2];
108     struct rx_securityClass *obj;
109     struct rxkad_cprivate *tp;          /* s & c have type at same offset */
110     char *data;
111     int i,tlen;
112
113     if (!xprt_CryptOK (conn)) return RXKADILLEGALLEVEL;
114     obj = rx_SecurityObjectOf(conn);
115     tp = (struct rxkad_cprivate *)obj->privateData;
116     LOCK_RXKAD_STATS
117     rxkad_stats.bytesEncrypted[rxkad_TypeIndex(tp->type)] += len;
118     UNLOCK_RXKAD_STATS
119
120     /*
121     afs_int32 cksum;
122     cksum = htonl(0);           
123     * Future option to add cksum here, but for now we just put 0
124     */
125     rx_PutInt32(packet, 1*sizeof(afs_int32), 0); 
126
127     bcopy ((void *)ivec, (void *)xor, sizeof(xor));
128     for (i = 0; len ; i++) {
129       data = rx_data(packet, i, tlen);
130       if (!data || !tlen)
131         break;
132       tlen = MIN(len, tlen);
133       fc_cbc_encrypt (data, data, tlen, schedule, xor, ENCRYPT);
134       len -= tlen;
135     }
136     return 0;
137 }
138