Pārlūkot izejas kodu

Added generic CHAP layer, independent of iSCSI

tags/v0.9.3
Michael Brown 18 gadus atpakaļ
vecāks
revīzija
c5428303e4
4 mainītis faili ar 180 papildinājumiem un 29 dzēšanām
  1. 108
    0
      src/crypto/chap.c
  2. 19
    10
      src/crypto/md5.c
  3. 51
    0
      src/include/gpxe/chap.h
  4. 2
    19
      src/include/gpxe/md5.h

+ 108
- 0
src/crypto/chap.c Parādīt failu

@@ -0,0 +1,108 @@
1
+/*
2
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stddef.h>
20
+#include <stdlib.h>
21
+#include <errno.h>
22
+#include <assert.h>
23
+#include <malloc.h>
24
+#include <gpxe/crypto.h>
25
+#include <gpxe/chap.h>
26
+
27
+/** @file
28
+ *
29
+ * CHAP protocol
30
+ *
31
+ */
32
+
33
+/**
34
+ * Initialise CHAP challenge/response
35
+ *
36
+ * @v chap		CHAP challenge/response
37
+ * @v digest		Digest algorithm to use
38
+ * @ret rc		Return status code
39
+ *
40
+ * Initialises a CHAP challenge/response structure.  This routine
41
+ * allocates memory, and so may fail.  The allocated memory must
42
+ * eventually be freed by a call to chap_finish().
43
+ */
44
+int chap_init ( struct chap_challenge *chap,
45
+		struct digest_algorithm *digest ) {
46
+	assert ( chap->digest == NULL );
47
+	assert ( chap->digest_context == NULL );
48
+	assert ( chap->response == NULL );
49
+
50
+	chap->digest = digest;
51
+	chap->digest_context = malloc ( digest->context_len );
52
+	if ( ! chap->digest_context )
53
+		goto err;
54
+	chap->response = malloc ( digest->digest_len );
55
+	if ( ! chap->response )
56
+		goto err;
57
+	chap->response_len = digest->digest_len;
58
+	chap->digest->init ( chap->digest_context );
59
+	return 0;
60
+
61
+ err:
62
+	chap_finish ( chap );
63
+	return -ENOMEM;
64
+}
65
+
66
+/**
67
+ * Add data to the CHAP challenge
68
+ *
69
+ * @v chap		CHAP challenge/response
70
+ * @v data		Data to add
71
+ * @v len		Length of data to add
72
+ */
73
+void chap_update ( struct chap_challenge *chap, const void *data,
74
+		   size_t len ) {
75
+	assert ( chap->digest != NULL );
76
+	assert ( chap->digest_context != NULL );
77
+
78
+	chap->digest->update ( chap->digest_context, data, len );
79
+}
80
+
81
+/**
82
+ * Respond to the CHAP challenge
83
+ *
84
+ * @v chap		CHAP challenge/response
85
+ *
86
+ * Calculates the final CHAP response value, and places it in @c
87
+ * chap->response, with a length of @c chap->response_len.
88
+ */
89
+void chap_respond ( struct chap_challenge *chap ) {
90
+	assert ( chap->digest != NULL );
91
+	assert ( chap->digest_context != NULL );
92
+	assert ( chap->response != NULL );
93
+
94
+	chap->digest->finish ( chap->digest_context, chap->response );
95
+}
96
+
97
+/**
98
+ * Free resources used by a CHAP challenge/response
99
+ *
100
+ * @v chap		CHAP challenge/response
101
+ */
102
+void chap_finish ( struct chap_challenge *chap ) {
103
+	free ( chap->digest_context );
104
+	chap->digest_context = NULL;
105
+	free ( chap->response );
106
+	chap->response = NULL;
107
+	chap->digest = NULL;
108
+}

+ 19
- 10
src/crypto/md5.c Parādīt failu

@@ -26,6 +26,16 @@
26 26
 #include <gpxe/crypto.h>
27 27
 #include <gpxe/md5.h>
28 28
 
29
+#define MD5_DIGEST_SIZE		16
30
+#define MD5_BLOCK_WORDS		16
31
+#define MD5_HASH_WORDS		4
32
+
33
+struct md5_ctx {
34
+	u32 hash[MD5_HASH_WORDS];
35
+	u32 block[MD5_BLOCK_WORDS];
36
+	u64 byte_count;
37
+};
38
+
29 39
 #define __md5step __attribute__ (( regparm ( 3 ) ))
30 40
 
31 41
 struct md5_step {
@@ -150,15 +160,16 @@ static inline void cpu_to_le32_array(u32 *buf, unsigned int words)
150 160
 	}
151 161
 }
152 162
 
153
-static inline void md5_transform_helper(struct md5_context *ctx)
163
+static inline void md5_transform_helper(struct md5_ctx *ctx)
154 164
 {
155 165
 	le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
156 166
 	md5_transform(ctx->hash, ctx->block);
157 167
 }
158 168
 
159
-void md5_init ( struct md5_context *context )
169
+static void md5_init(void *context)
160 170
 {
161
-	struct md5_context *mctx = context;
171
+	struct md5_ctx *mctx = context;
172
+
162 173
 	mctx->hash[0] = 0x67452301;
163 174
 	mctx->hash[1] = 0xefcdab89;
164 175
 	mctx->hash[2] = 0x98badcfe;
@@ -166,9 +177,9 @@ void md5_init ( struct md5_context *context )
166 177
 	mctx->byte_count = 0;
167 178
 }
168 179
 
169
-void md5_update ( struct md5_context *context, const void *data, size_t len )
180
+static void md5_update(void *context, const void *data, size_t len)
170 181
 {
171
-	struct md5_context *mctx = context;
182
+	struct md5_ctx *mctx = context;
172 183
 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
173 184
 
174 185
 	mctx->byte_count += len;
@@ -196,9 +207,9 @@ void md5_update ( struct md5_context *context, const void *data, size_t len )
196 207
 	memcpy(mctx->block, data, len);
197 208
 }
198 209
 
199
-void md5_finish ( struct md5_context *context, struct md5_hash *out )
210
+static void md5_finish(void *context, void *out)
200 211
 {
201
-	struct md5_context *mctx = context;
212
+	struct md5_ctx *mctx = context;
202 213
 	const unsigned int offset = mctx->byte_count & 0x3f;
203 214
 	char *p = (char *)mctx->block + offset;
204 215
 	int padding = 56 - (offset + 1);
@@ -222,12 +233,10 @@ void md5_finish ( struct md5_context *context, struct md5_hash *out )
222 233
 	memset(mctx, 0, sizeof(*mctx));
223 234
 }
224 235
 
225
-/*
226 236
 struct digest_algorithm md5_algorithm = {
227
-	.context_len	= sizeof ( struct md5_context ),
237
+	.context_len	= sizeof ( struct md5_ctx ),
228 238
 	.digest_len	= MD5_DIGEST_SIZE,
229 239
 	.init		= md5_init,
230 240
 	.update		= md5_update,
231 241
 	.finish		= md5_finish,
232 242
 };
233
-*/

+ 51
- 0
src/include/gpxe/chap.h Parādīt failu

@@ -0,0 +1,51 @@
1
+#ifndef _GPXE_CHAP_H
2
+#define _GPXE_CHAP_H
3
+
4
+/** @file
5
+ *
6
+ * CHAP protocol
7
+ *
8
+ */
9
+
10
+#include <stdint.h>
11
+#include <gpxe/md5.h>
12
+
13
+struct digest_algorithm;
14
+
15
+/** A CHAP challenge/response */
16
+struct chap_challenge {
17
+	/** Digest algorithm used for the response */
18
+	struct digest_algorithm *digest;
19
+	/** Context used by the digest algorithm */
20
+	uint8_t *digest_context;
21
+	/** CHAP response */
22
+	uint8_t *response;
23
+	/** Length of CHAP response */
24
+	size_t response_len;
25
+};
26
+
27
+extern int chap_init ( struct chap_challenge *chap,
28
+		       struct digest_algorithm *digest );
29
+extern void chap_update ( struct chap_challenge *chap, const void *data,
30
+			  size_t len );
31
+extern void chap_respond ( struct chap_challenge *chap );
32
+extern void chap_finish ( struct chap_challenge *chap );
33
+
34
+/**
35
+ * Add identifier data to the CHAP challenge
36
+ *
37
+ * @v chap		CHAP challenge/response
38
+ * @v identifier	CHAP identifier
39
+ *
40
+ * The CHAP identifier is the first byte of the CHAP challenge.  This
41
+ * function is a notational convenience for calling chap_update() for
42
+ * the identifier byte.
43
+ */
44
+static inline void chap_set_identifier ( struct chap_challenge *chap,
45
+					 unsigned int identifier ) {
46
+	uint8_t ident_byte = identifier;
47
+
48
+	chap_update ( chap, &ident_byte, sizeof ( ident_byte ) );
49
+}
50
+
51
+#endif /* _GPXE_CHAP_H */

+ 2
- 19
src/include/gpxe/md5.h Parādīt failu

@@ -1,25 +1,8 @@
1 1
 #ifndef _GPXE_MD5_H
2 2
 #define _GPXE_MD5_H
3 3
 
4
-#include <stdint.h>
4
+struct digest_algorithm;
5 5
 
6
-#define MD5_DIGEST_SIZE		16
7
-#define MD5_BLOCK_WORDS		16
8
-#define MD5_HASH_WORDS		4
9
-
10
-struct md5_context {
11
-	u32 hash[MD5_HASH_WORDS];
12
-	u32 block[MD5_BLOCK_WORDS];
13
-	u64 byte_count;
14
-};
15
-
16
-struct md5_hash {
17
-	u8 hash[MD5_DIGEST_SIZE];
18
-};
19
-
20
-extern void md5_init ( struct md5_context *context );
21
-extern void md5_update ( struct md5_context *context, const void *data,
22
-			 size_t len );
23
-extern void md5_finish ( struct md5_context *context, struct md5_hash *hash );
6
+extern struct digest_algorithm md5_algorithm;
24 7
 
25 8
 #endif /* _GPXE_MD5_H */

Notiek ielāde…
Atcelt
Saglabāt