sctp_hashdriver.c revision 1.1 1 1.1 rjs /* $KAME: sctp_hashdriver.c,v 1.6 2004/02/24 21:52:26 itojun Exp $ */
2 1.1 rjs /* $NetBSD: sctp_hashdriver.c,v 1.1 2015/10/13 21:28:35 rjs Exp $ */
3 1.1 rjs
4 1.1 rjs /*
5 1.1 rjs * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6 1.1 rjs * All rights reserved.
7 1.1 rjs *
8 1.1 rjs * Redistribution and use in source and binary forms, with or without
9 1.1 rjs * modification, are permitted provided that the following conditions
10 1.1 rjs * are met:
11 1.1 rjs * 1. Redistributions of source code must retain the above copyright
12 1.1 rjs * notice, this list of conditions and the following disclaimer.
13 1.1 rjs * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 rjs * notice, this list of conditions and the following disclaimer in the
15 1.1 rjs * documentation and/or other materials provided with the distribution.
16 1.1 rjs * 3. All advertising materials mentioning features or use of this software
17 1.1 rjs * must display the following acknowledgement:
18 1.1 rjs * This product includes software developed by Cisco Systems, Inc.
19 1.1 rjs * 4. Neither the name of the project nor the names of its contributors
20 1.1 rjs * may be used to endorse or promote products derived from this software
21 1.1 rjs * without specific prior written permission.
22 1.1 rjs *
23 1.1 rjs * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 rjs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 rjs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 rjs * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 1.1 rjs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 rjs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 rjs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 rjs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 rjs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 rjs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 rjs * SUCH DAMAGE.
34 1.1 rjs */
35 1.1 rjs #include <sys/cdefs.h>
36 1.1 rjs __KERNEL_RCSID(0, "$NetBSD: sctp_hashdriver.c,v 1.1 2015/10/13 21:28:35 rjs Exp $");
37 1.1 rjs
38 1.1 rjs #include <sys/param.h>
39 1.1 rjs #include <sys/systm.h>
40 1.1 rjs #include <sys/malloc.h>
41 1.1 rjs #include <sys/mbuf.h>
42 1.1 rjs #include <sys/domain.h>
43 1.1 rjs #include <sys/protosw.h>
44 1.1 rjs #include <sys/socket.h>
45 1.1 rjs #include <sys/socketvar.h>
46 1.1 rjs #include <sys/proc.h>
47 1.1 rjs #include <sys/kernel.h>
48 1.1 rjs #include <sys/sysctl.h>
49 1.1 rjs #include <sys/types.h>
50 1.1 rjs #include <netinet/sctp_constants.h>
51 1.1 rjs #ifdef USE_MD5
52 1.1 rjs #include <sys/md5.h>
53 1.1 rjs #else
54 1.1 rjs #include <sys/sha1.h>
55 1.1 rjs #endif
56 1.1 rjs #include <netinet/sctp_hashdriver.h>
57 1.1 rjs
58 1.1 rjs /*
59 1.1 rjs * Main driver for SCTP's hashing.
60 1.1 rjs * passing a two pointers and two lengths, returning a digest pointer
61 1.1 rjs * filled. The md5 code was taken directly from the RFC (2104) so to
62 1.1 rjs * understand it you may want to go look at the RFC referenced in the
63 1.1 rjs * SCTP spec. We did modify this code to either user OURs implementation
64 1.1 rjs * of SLA1 or the MD5 that comes from its RFC. SLA1 may have IPR issues
65 1.1 rjs * so you need to check in to this if you wish to use it... Or at least
66 1.1 rjs * that is what the FIP-180.1 web page says.
67 1.1 rjs */
68 1.1 rjs
69 1.1 rjs void sctp_hash_digest(char *key, int key_len, char *text, int text_len,
70 1.1 rjs unsigned char *digest)
71 1.1 rjs {
72 1.1 rjs #ifdef USE_MD5
73 1.1 rjs MD5Context context;
74 1.1 rjs #else
75 1.1 rjs SHA1_CTX context;
76 1.1 rjs #endif /* USE_MD5 */
77 1.1 rjs /* inner padding - key XORd with ipad */
78 1.1 rjs unsigned char k_ipad[65];
79 1.1 rjs /* outer padding - key XORd with opad */
80 1.1 rjs unsigned char k_opad[65];
81 1.1 rjs unsigned char tk[20];
82 1.1 rjs int i;
83 1.1 rjs
84 1.1 rjs if (key_len > 64) {
85 1.1 rjs #ifdef USE_MD5
86 1.1 rjs md5_ctxt tctx;
87 1.1 rjs MD5Init(&tctx);
88 1.1 rjs MD5Update(&tctx, key, key_len);
89 1.1 rjs MD5Final(tk, &tctx);
90 1.1 rjs key = tk;
91 1.1 rjs key_len = 16;
92 1.1 rjs #else
93 1.1 rjs SHA1_CTX tctx;
94 1.1 rjs SHA1Init(&tctx);
95 1.1 rjs SHA1Update(&tctx, key, key_len);
96 1.1 rjs SHA1Final(tk, &tctx);
97 1.1 rjs key = tk;
98 1.1 rjs key_len = 20;
99 1.1 rjs #endif /* USE_MD5 */
100 1.1 rjs }
101 1.1 rjs
102 1.1 rjs /*
103 1.1 rjs * the HMAC_MD5 transform looks like:
104 1.1 rjs *
105 1.1 rjs * MD5(K XOR opad, MD5(K XOR ipad, text))
106 1.1 rjs *
107 1.1 rjs * where K is an n byte key
108 1.1 rjs * ipad is the byte 0x36 repeated 64 times
109 1.1 rjs * opad is the byte 0x5c repeated 64 times
110 1.1 rjs * and text is the data being protected
111 1.1 rjs */
112 1.1 rjs
113 1.1 rjs /* start out by storing key in pads */
114 1.1 rjs memset(k_ipad, 0, sizeof k_ipad);
115 1.1 rjs memset(k_opad, 0, sizeof k_opad);
116 1.1 rjs bcopy(key, k_ipad, key_len);
117 1.1 rjs bcopy(key, k_opad, key_len);
118 1.1 rjs
119 1.1 rjs /* XOR key with ipad and opad values */
120 1.1 rjs for (i = 0; i < 64; i++) {
121 1.1 rjs k_ipad[i] ^= 0x36;
122 1.1 rjs k_opad[i] ^= 0x5c;
123 1.1 rjs }
124 1.1 rjs /*
125 1.1 rjs * perform inner MD5
126 1.1 rjs */
127 1.1 rjs #ifdef USE_MD5
128 1.1 rjs MD5Init(&context); /* init context for 1st pass */
129 1.1 rjs MD5Update(&context, k_ipad, 64); /* start with inner pad */
130 1.1 rjs MD5Update(&context, text, text_len); /* then text of datagram */
131 1.1 rjs MD5Final(digest, &context); /* finish up 1st pass */
132 1.1 rjs #else
133 1.1 rjs SHA1Init(&context); /* init context for 1st pass */
134 1.1 rjs SHA1Update(&context, k_ipad, 64); /* start with inner pad */
135 1.1 rjs SHA1Update(&context, text, text_len); /* then text of datagram */
136 1.1 rjs SHA1Final(digest, &context); /* finish up 1st pass */
137 1.1 rjs #endif /* USE_MD5 */
138 1.1 rjs
139 1.1 rjs /*
140 1.1 rjs * perform outer MD5
141 1.1 rjs */
142 1.1 rjs #ifdef USE_MD5
143 1.1 rjs MD5Init(&context); /* init context for 2nd pass */
144 1.1 rjs MD5Update(&context, k_opad, 64); /* start with outer pad */
145 1.1 rjs MD5Update(&context, digest, 16); /* then results of 1st hash */
146 1.1 rjs MD5Final(digest, &context); /* finish up 2nd pass */
147 1.1 rjs #else
148 1.1 rjs SHA1Init(&context); /* init context for 2nd pass */
149 1.1 rjs SHA1Update(&context, k_opad, 64); /* start with outer pad */
150 1.1 rjs SHA1Update(&context, digest, 20); /* then results of 1st hash */
151 1.1 rjs SHA1Final(digest, &context); /* finish up 2nd pass */
152 1.1 rjs #endif /* USE_MD5 */
153 1.1 rjs }
154 1.1 rjs
155 1.1 rjs void sctp_hash_digest_m(char *key, int key_len, struct mbuf *m, int offset,
156 1.1 rjs unsigned char *digest)
157 1.1 rjs {
158 1.1 rjs struct mbuf *m_at;
159 1.1 rjs #ifdef USE_MD5
160 1.1 rjs MD5Context context;
161 1.1 rjs #else
162 1.1 rjs SHA1_CTX context;
163 1.1 rjs #endif /* USE_MD5 */
164 1.1 rjs /* inner padding - key XORd with ipad */
165 1.1 rjs unsigned char k_ipad[65];
166 1.1 rjs /* outer padding - key XORd with opad */
167 1.1 rjs unsigned char k_opad[65];
168 1.1 rjs unsigned char tk[20];
169 1.1 rjs int i;
170 1.1 rjs
171 1.1 rjs if (key_len > 64) {
172 1.1 rjs #ifdef USE_MD5
173 1.1 rjs MD5Context tctx;
174 1.1 rjs MD5Init(&tctx);
175 1.1 rjs MD5Update(&tctx, key, key_len);
176 1.1 rjs MD5Final(tk, &tctx);
177 1.1 rjs key = tk;
178 1.1 rjs key_len = 16;
179 1.1 rjs #else
180 1.1 rjs SHA1_CTX tctx;
181 1.1 rjs SHA1Init(&tctx);
182 1.1 rjs SHA1Update(&tctx, key, key_len);
183 1.1 rjs SHA1Final(tk, &tctx);
184 1.1 rjs key = tk;
185 1.1 rjs key_len = 20;
186 1.1 rjs #endif /* USE_MD5 */
187 1.1 rjs }
188 1.1 rjs
189 1.1 rjs /*
190 1.1 rjs * the HMAC_MD5 transform looks like:
191 1.1 rjs *
192 1.1 rjs * MD5(K XOR opad, MD5(K XOR ipad, text))
193 1.1 rjs *
194 1.1 rjs * where K is an n byte key
195 1.1 rjs * ipad is the byte 0x36 repeated 64 times
196 1.1 rjs * opad is the byte 0x5c repeated 64 times
197 1.1 rjs * and text is the data being protected
198 1.1 rjs */
199 1.1 rjs
200 1.1 rjs /* start out by storing key in pads */
201 1.1 rjs memset(k_ipad, 0, sizeof k_ipad);
202 1.1 rjs memset(k_opad, 0, sizeof k_opad);
203 1.1 rjs bcopy(key, k_ipad, key_len);
204 1.1 rjs bcopy(key, k_opad, key_len);
205 1.1 rjs
206 1.1 rjs /* XOR key with ipad and opad values */
207 1.1 rjs for (i = 0; i < 64; i++) {
208 1.1 rjs k_ipad[i] ^= 0x36;
209 1.1 rjs k_opad[i] ^= 0x5c;
210 1.1 rjs }
211 1.1 rjs
212 1.1 rjs /* find the correct mbuf and offset into mbuf */
213 1.1 rjs m_at = m;
214 1.1 rjs while ((m_at != NULL) && (offset > m_at->m_len)) {
215 1.1 rjs offset -= m_at->m_len; /* update remaining offset left */
216 1.1 rjs m_at = m_at->m_next;
217 1.1 rjs }
218 1.1 rjs /*
219 1.1 rjs * perform inner MD5
220 1.1 rjs */
221 1.1 rjs #ifdef USE_MD5
222 1.1 rjs MD5Init(&context); /* init context for 1st pass */
223 1.1 rjs MD5Update(&context, k_ipad, 64); /* start with inner pad */
224 1.1 rjs /******/
225 1.1 rjs while (m_at != NULL) {
226 1.1 rjs /* then text of datagram... */
227 1.1 rjs MD5Update(&context, mtod(m_at, char *)+offset,
228 1.1 rjs m_at->m_len-offset);
229 1.1 rjs /* only offset on the first mbuf */
230 1.1 rjs offset = 0;
231 1.1 rjs m_at = m_at->m_next;
232 1.1 rjs }
233 1.1 rjs /******/
234 1.1 rjs MD5Final(digest, &context); /* finish up 1st pass */
235 1.1 rjs #else
236 1.1 rjs SHA1Init(&context); /* init context for 1st pass */
237 1.1 rjs SHA1Update(&context, k_ipad, 64); /* start with inner pad */
238 1.1 rjs /******/
239 1.1 rjs while (m_at != NULL) {
240 1.1 rjs /* then text of datagram */
241 1.1 rjs SHA1Update(&context, mtod(m_at, char *)+offset,
242 1.1 rjs m_at->m_len-offset);
243 1.1 rjs /* only offset on the first mbuf */
244 1.1 rjs offset = 0;
245 1.1 rjs m_at = m_at->m_next;
246 1.1 rjs }
247 1.1 rjs /******/
248 1.1 rjs SHA1Final(digest, &context); /* finish up 1st pass */
249 1.1 rjs #endif /* USE_MD5 */
250 1.1 rjs
251 1.1 rjs /*
252 1.1 rjs * perform outer MD5
253 1.1 rjs */
254 1.1 rjs #ifdef USE_MD5
255 1.1 rjs MD5Init(&context); /* init context for 2nd pass */
256 1.1 rjs MD5Update(&context, k_opad, 64); /* start with outer pad */
257 1.1 rjs MD5Update(&context, digest, 16); /* then results of 1st hash */
258 1.1 rjs MD5Final(digest, &context); /* finish up 2nd pass */
259 1.1 rjs #else
260 1.1 rjs SHA1Init(&context); /* init context for 2nd pass */
261 1.1 rjs SHA1Update(&context, k_opad, 64); /* start with outer pad */
262 1.1 rjs SHA1Update(&context, digest, 20); /* then results of 1st hash */
263 1.1 rjs SHA1Final(digest, &context); /* finish up 2nd pass */
264 1.1 rjs #endif /* USE_MD5 */
265 1.1 rjs }
266