Browse Source

Reduce from 157 to 123 bytes

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
a9415d3da2
1 changed files with 15 additions and 15 deletions
  1. 15
    15
      src/crypto/chap.c

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

18
 
18
 
19
 #include <stddef.h>
19
 #include <stddef.h>
20
 #include <stdlib.h>
20
 #include <stdlib.h>
21
+#include <string.h>
21
 #include <errno.h>
22
 #include <errno.h>
22
 #include <assert.h>
23
 #include <assert.h>
23
 #include <malloc.h>
24
 #include <malloc.h>
43
  */
44
  */
44
 int chap_init ( struct chap_challenge *chap,
45
 int chap_init ( struct chap_challenge *chap,
45
 		struct digest_algorithm *digest ) {
46
 		struct digest_algorithm *digest ) {
47
+	size_t state_len;
48
+	void *state;
49
+
46
 	assert ( chap->digest == NULL );
50
 	assert ( chap->digest == NULL );
47
 	assert ( chap->digest_context == NULL );
51
 	assert ( chap->digest_context == NULL );
48
 	assert ( chap->response == NULL );
52
 	assert ( chap->response == NULL );
49
 
53
 
54
+	state_len = ( digest->context_len + digest->digest_len );
55
+	state = malloc ( state_len );
56
+	if ( ! state )
57
+		return -ENOMEM;
58
+	
50
 	chap->digest = digest;
59
 	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;
60
+	chap->digest_context = state;
61
+	chap->response = ( state + digest->context_len );
57
 	chap->response_len = digest->digest_len;
62
 	chap->response_len = digest->digest_len;
58
 	chap->digest->init ( chap->digest_context );
63
 	chap->digest->init ( chap->digest_context );
59
 	return 0;
64
 	return 0;
60
-
61
- err:
62
-	chap_finish ( chap );
63
-	return -ENOMEM;
64
 }
65
 }
65
 
66
 
66
 /**
67
 /**
100
  * @v chap		CHAP challenge/response
101
  * @v chap		CHAP challenge/response
101
  */
102
  */
102
 void chap_finish ( struct chap_challenge *chap ) {
103
 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;
104
+	void *state = chap->digest_context;
105
+
106
+	free ( state );
107
+	memset ( chap, 0, sizeof ( *chap ) );
108
 }
108
 }

Loading…
Cancel
Save