Browse Source

Generalise digest_algorithm to crypto_algorithm.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
db2fde474e
5 changed files with 86 additions and 44 deletions
  1. 7
    7
      src/crypto/chap.c
  2. 9
    7
      src/crypto/md5.c
  3. 3
    3
      src/include/gpxe/chap.h
  4. 65
    25
      src/include/gpxe/crypto.h
  5. 2
    2
      src/include/gpxe/md5.h

+ 7
- 7
src/crypto/chap.c View File

@@ -42,7 +42,7 @@
42 42
  * eventually be freed by a call to chap_finish().
43 43
  */
44 44
 int chap_init ( struct chap_challenge *chap,
45
-		struct digest_algorithm *digest ) {
45
+		struct crypto_algorithm *digest ) {
46 46
 	size_t state_len;
47 47
 	void *state;
48 48
 
@@ -52,7 +52,7 @@ int chap_init ( struct chap_challenge *chap,
52 52
 
53 53
 	DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
54 54
 
55
-	state_len = ( digest->context_len + digest->digest_len );
55
+	state_len = ( digest->ctxsize + digest->digestsize );
56 56
 	state = malloc ( state_len );
57 57
 	if ( ! state ) {
58 58
 		DBG ( "CHAP %p could not allocate %d bytes for state\n",
@@ -62,9 +62,9 @@ int chap_init ( struct chap_challenge *chap,
62 62
 	
63 63
 	chap->digest = digest;
64 64
 	chap->digest_context = state;
65
-	chap->response = ( state + digest->context_len );
66
-	chap->response_len = digest->digest_len;
67
-	chap->digest->init ( chap->digest_context );
65
+	chap->response = ( state + digest->ctxsize );
66
+	chap->response_len = digest->digestsize;
67
+	digest_init ( chap->digest, chap->digest_context );
68 68
 	return 0;
69 69
 }
70 70
 
@@ -83,7 +83,7 @@ void chap_update ( struct chap_challenge *chap, const void *data,
83 83
 	if ( ! chap->digest )
84 84
 		return;
85 85
 
86
-	chap->digest->update ( chap->digest_context, data, len );
86
+	digest_update ( chap->digest, chap->digest_context, data, len );
87 87
 }
88 88
 
89 89
 /**
@@ -104,7 +104,7 @@ void chap_respond ( struct chap_challenge *chap ) {
104 104
 	if ( ! chap->digest )
105 105
 		return;
106 106
 
107
-	chap->digest->finish ( chap->digest_context, chap->response );
107
+	digest_final ( chap->digest, chap->digest_context, chap->response );
108 108
 }
109 109
 
110 110
 /**

+ 9
- 7
src/crypto/md5.c View File

@@ -177,7 +177,8 @@ static void md5_init(void *context)
177 177
 	mctx->byte_count = 0;
178 178
 }
179 179
 
180
-static void md5_update(void *context, const void *data, size_t len)
180
+static void md5_update(void *context, const void *data, void *dst __unused,
181
+		       size_t len)
181 182
 {
182 183
 	struct md5_ctx *mctx = context;
183 184
 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
@@ -207,7 +208,7 @@ static void md5_update(void *context, const void *data, size_t len)
207 208
 	memcpy(mctx->block, data, len);
208 209
 }
209 210
 
210
-static void md5_finish(void *context, void *out)
211
+static void md5_final(void *context, void *out)
211 212
 {
212 213
 	struct md5_ctx *mctx = context;
213 214
 	const unsigned int offset = mctx->byte_count & 0x3f;
@@ -233,11 +234,12 @@ static void md5_finish(void *context, void *out)
233 234
 	memset(mctx, 0, sizeof(*mctx));
234 235
 }
235 236
 
236
-struct digest_algorithm md5_algorithm = {
237
+struct crypto_algorithm md5_algorithm = {
237 238
 	.name		= "md5",
238
-	.context_len	= sizeof ( struct md5_ctx ),
239
-	.digest_len	= MD5_DIGEST_SIZE,
239
+	.ctxsize	= sizeof ( struct md5_ctx ),
240
+	.blocksize	= 1,
241
+	.digestsize	= MD5_DIGEST_SIZE,
240 242
 	.init		= md5_init,
241
-	.update		= md5_update,
242
-	.finish		= md5_finish,
243
+	.encode		= md5_update,
244
+	.final		= md5_final,
243 245
 };

+ 3
- 3
src/include/gpxe/chap.h View File

@@ -10,12 +10,12 @@
10 10
 #include <stdint.h>
11 11
 #include <gpxe/md5.h>
12 12
 
13
-struct digest_algorithm;
13
+struct crypto_algorithm;
14 14
 
15 15
 /** A CHAP challenge/response */
16 16
 struct chap_challenge {
17 17
 	/** Digest algorithm used for the response */
18
-	struct digest_algorithm *digest;
18
+	struct crypto_algorithm *digest;
19 19
 	/** Context used by the digest algorithm */
20 20
 	uint8_t *digest_context;
21 21
 	/** CHAP response */
@@ -25,7 +25,7 @@ struct chap_challenge {
25 25
 };
26 26
 
27 27
 extern int chap_init ( struct chap_challenge *chap,
28
-		       struct digest_algorithm *digest );
28
+		       struct crypto_algorithm *digest );
29 29
 extern void chap_update ( struct chap_challenge *chap, const void *data,
30 30
 			  size_t len );
31 31
 extern void chap_respond ( struct chap_challenge *chap );

+ 65
- 25
src/include/gpxe/crypto.h View File

@@ -9,38 +9,78 @@
9 9
 
10 10
 #include <stdint.h>
11 11
 
12
-/**
13
- * A message-digest algorithm
14
- *
15
- */
16
-struct digest_algorithm {
12
+/** A cryptographic algorithm */
13
+struct crypto_algorithm {
17 14
 	/** Algorithm name */
18 15
 	const char *name;
19
-	/** Size of a context for this algorithm */
20
-	size_t context_len;
21
-	/** Size of a message digest for this algorithm */
22
-	size_t digest_len;
23
-	/**
24
-	 * Initialise digest algorithm
25
-	 *
26
-	 * @v context		Context for digest operations
16
+	/** Context size */
17
+	size_t ctxsize;
18
+	/** Block size */
19
+	size_t blocksize;
20
+	/** Final output size */
21
+	size_t digestsize;
22
+	/** Initialise algorithm
23
+	 *
24
+	 * @v ctx		Context
27 25
 	 */
28
-	void ( * init ) ( void *context );
29
-	/**
30
-	 * Calculate digest over data buffer
26
+	void ( * init ) ( void *ctx );
27
+	/** Set key
31 28
 	 *
32
-	 * @v context		Context for digest operations
33
-	 * @v data		Data buffer
34
-	 * @v len		Length of data buffer
29
+	 * @v ctx		Context
30
+	 * @v key		Key
31
+	 * @v keylen		Key length
32
+	 * @ret rc		Return status code
35 33
 	 */
36
-	void ( * update ) ( void *context, const void *data, size_t len );
37
-	/**
38
-	 * Finish calculating digest
34
+	int ( * setkey ) ( void *ctx, void *key, size_t keylen );
35
+	/** Encode data
36
+	 *
37
+	 * @v ctx		Context
38
+	 * @v src		Data to encode
39
+	 * @v dst		Encoded data, or NULL
40
+	 * @v len		Length of data
41
+	 * @ret rc		Return status code
39 42
 	 *
40
-	 * @v context		Context for digest operations
41
-	 * @v digest		Buffer for message digest
43
+	 * For a cipher algorithm, the enciphered data should be
44
+	 * placed in @c dst.  For a digest algorithm, only the digest
45
+	 * state should be updated, and @c dst will be NULL.
46
+	 *
47
+	 * @v len is guaranteed to be a multiple of @c blocksize.
42 48
 	 */
43
-	void ( * finish ) ( void *context, void *digest );
49
+	void ( * encode ) ( void *ctx, const void *src, void *dst,
50
+			    size_t len );
51
+	/** Decode data
52
+	 *
53
+	 * @v ctx		Context
54
+	 * @v src		Data to decode
55
+	 * @v dst		Decoded data
56
+	 * @v len		Length of data
57
+	 * @ret rc		Return status code
58
+	 *
59
+	 * @v len is guaranteed to be a multiple of @c blocksize.
60
+	 */
61
+	void ( * decode ) ( void *ctx, const void *src, void *dst,
62
+			    size_t len );
63
+	/** Finalise algorithm
64
+	 *
65
+	 * @v ctx		Context
66
+	 * @v out		Algorithm final output
67
+	 */
68
+	void ( * final ) ( void *ctx, void *out );
44 69
 };
45 70
 
71
+static inline void digest_init ( struct crypto_algorithm *crypto,
72
+				 void *ctx ) {
73
+	crypto->init ( ctx );
74
+}
75
+
76
+static inline void digest_update ( struct crypto_algorithm *crypto,
77
+				   void *ctx, const void *data, size_t len ) {
78
+	crypto->encode ( ctx, data, NULL, len );
79
+}
80
+
81
+static inline void digest_final ( struct crypto_algorithm *crypto,
82
+				  void *ctx, void *out ) {
83
+	crypto->final ( ctx, out );
84
+}
85
+
46 86
 #endif /* _GPXE_CRYPTO_H */

+ 2
- 2
src/include/gpxe/md5.h View File

@@ -1,8 +1,8 @@
1 1
 #ifndef _GPXE_MD5_H
2 2
 #define _GPXE_MD5_H
3 3
 
4
-struct digest_algorithm;
4
+struct crypto_algorithm;
5 5
 
6
-extern struct digest_algorithm md5_algorithm;
6
+extern struct crypto_algorithm md5_algorithm;
7 7
 
8 8
 #endif /* _GPXE_MD5_H */

Loading…
Cancel
Save