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 digest_algorithm *digest ) {
46 assert ( chap->digest == NULL );
47 assert ( chap->digest_context == NULL );
48 assert ( chap->response == NULL );
50 chap->digest = digest;
51 chap->digest_context = malloc ( digest->context_len );
52 if ( ! chap->digest_context )
54 chap->response = malloc ( digest->digest_len );
55 if ( ! chap->response )
57 chap->response_len = digest->digest_len;
58 chap->digest->init ( chap->digest_context );
67 * Add data to the CHAP challenge
69 * @v chap CHAP challenge/response
71 * @v len Length of data to add
73 void chap_update ( struct chap_challenge *chap, const void *data,
75 assert ( chap->digest != NULL );
76 assert ( chap->digest_context != NULL );
78 chap->digest->update ( chap->digest_context, data, len );
82 * Respond to the CHAP challenge
84 * @v chap CHAP challenge/response
86 * Calculates the final CHAP response value, and places it in @c
87 * chap->response, with a length of @c chap->response_len.
89 void chap_respond ( struct chap_challenge *chap ) {
90 assert ( chap->digest != NULL );
91 assert ( chap->digest_context != NULL );
92 assert ( chap->response != NULL );
94 chap->digest->finish ( chap->digest_context, chap->response );
98 * Free resources used by a CHAP challenge/response
100 * @v chap CHAP challenge/response
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;