2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
24 #include <gpxe/crypto.h>
25 #include <gpxe/chap.h>
34 * Initialise CHAP challenge/response
36 * @v chap CHAP challenge/response
37 * @v digest Digest algorithm to use
38 * @ret rc Return status code
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().
44 int chap_init ( struct chap_challenge *chap,
45 struct crypto_algorithm *digest ) {
49 assert ( chap->digest == NULL );
50 assert ( chap->digest_context == NULL );
51 assert ( chap->response == NULL );
53 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
55 state_len = ( digest->ctxsize + digest->digestsize );
56 state = malloc ( state_len );
58 DBG ( "CHAP %p could not allocate %d bytes for state\n",
63 chap->digest = digest;
64 chap->digest_context = state;
65 chap->response = ( state + digest->ctxsize );
66 chap->response_len = digest->digestsize;
67 digest_init ( chap->digest, chap->digest_context );
72 * Add data to the CHAP challenge
74 * @v chap CHAP challenge/response
76 * @v len Length of data to add
78 void chap_update ( struct chap_challenge *chap, const void *data,
80 assert ( chap->digest != NULL );
81 assert ( chap->digest_context != NULL );
86 digest_update ( chap->digest, chap->digest_context, data, len );
90 * Respond to the CHAP challenge
92 * @v chap CHAP challenge/response
94 * Calculates the final CHAP response value, and places it in @c
95 * chap->response, with a length of @c chap->response_len.
97 void chap_respond ( struct chap_challenge *chap ) {
98 assert ( chap->digest != NULL );
99 assert ( chap->digest_context != NULL );
100 assert ( chap->response != NULL );
102 DBG ( "CHAP %p responding to challenge\n", chap );
104 if ( ! chap->digest )
107 digest_final ( chap->digest, chap->digest_context, chap->response );
111 * Free resources used by a CHAP challenge/response
113 * @v chap CHAP challenge/response
115 void chap_finish ( struct chap_challenge *chap ) {
116 void *state = chap->digest_context;
118 DBG ( "CHAP %p finished\n", chap );
121 memset ( chap, 0, sizeof ( *chap ) );