chap.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stddef.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <assert.h>
00026 #include <gpxe/crypto.h>
00027 #include <gpxe/chap.h>
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 int chap_init ( struct chap_response *chap,
00047 struct digest_algorithm *digest ) {
00048 size_t state_len;
00049 void *state;
00050
00051 assert ( chap->digest == NULL );
00052 assert ( chap->digest_context == NULL );
00053 assert ( chap->response == NULL );
00054
00055 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
00056
00057 state_len = ( digest->ctxsize + digest->digestsize );
00058 state = malloc ( state_len );
00059 if ( ! state ) {
00060 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
00061 chap, state_len );
00062 return -ENOMEM;
00063 }
00064
00065 chap->digest = digest;
00066 chap->digest_context = state;
00067 chap->response = ( state + digest->ctxsize );
00068 chap->response_len = digest->digestsize;
00069 digest_init ( chap->digest, chap->digest_context );
00070 return 0;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080 void chap_update ( struct chap_response *chap, const void *data,
00081 size_t len ) {
00082 assert ( chap->digest != NULL );
00083 assert ( chap->digest_context != NULL );
00084
00085 if ( ! chap->digest )
00086 return;
00087
00088 digest_update ( chap->digest, chap->digest_context, data, len );
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 void chap_respond ( struct chap_response *chap ) {
00100 assert ( chap->digest != NULL );
00101 assert ( chap->digest_context != NULL );
00102 assert ( chap->response != NULL );
00103
00104 DBG ( "CHAP %p responding to challenge\n", chap );
00105
00106 if ( ! chap->digest )
00107 return;
00108
00109 digest_final ( chap->digest, chap->digest_context, chap->response );
00110 }
00111
00112
00113
00114
00115
00116
00117 void chap_finish ( struct chap_response *chap ) {
00118 void *state = chap->digest_context;
00119
00120 DBG ( "CHAP %p finished\n", chap );
00121
00122 free ( state );
00123 memset ( chap, 0, sizeof ( *chap ) );
00124 }