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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
27 #include <ipxe/crypto.h>
28 #include <ipxe/chap.h>
37 * Initialise CHAP challenge/response
39 * @v chap CHAP challenge/response
40 * @v digest Digest algorithm to use
41 * @ret rc Return status code
43 * Initialises a CHAP challenge/response structure. This routine
44 * allocates memory, and so may fail. The allocated memory must
45 * eventually be freed by a call to chap_finish().
47 int chap_init ( struct chap_response *chap,
48 struct digest_algorithm *digest ) {
52 assert ( chap->digest == NULL );
53 assert ( chap->digest_context == NULL );
54 assert ( chap->response == NULL );
56 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
58 state_len = ( digest->ctxsize + digest->digestsize );
59 state = malloc ( state_len );
61 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
66 chap->digest = digest;
67 chap->digest_context = state;
68 chap->response = ( state + digest->ctxsize );
69 chap->response_len = digest->digestsize;
70 digest_init ( chap->digest, chap->digest_context );
75 * Add data to the CHAP challenge
77 * @v chap CHAP response
79 * @v len Length of data to add
81 void chap_update ( struct chap_response *chap, const void *data,
83 assert ( chap->digest != NULL );
84 assert ( chap->digest_context != NULL );
89 digest_update ( chap->digest, chap->digest_context, data, len );
93 * Respond to the CHAP challenge
95 * @v chap CHAP response
97 * Calculates the final CHAP response value, and places it in @c
98 * chap->response, with a length of @c chap->response_len.
100 void chap_respond ( struct chap_response *chap ) {
101 assert ( chap->digest != NULL );
102 assert ( chap->digest_context != NULL );
103 assert ( chap->response != NULL );
105 DBG ( "CHAP %p responding to challenge\n", chap );
107 if ( ! chap->digest )
110 digest_final ( chap->digest, chap->digest_context, chap->response );
114 * Free resources used by a CHAP response
116 * @v chap CHAP response
118 void chap_finish ( struct chap_response *chap ) {
119 void *state = chap->digest_context;
121 DBG ( "CHAP %p finished\n", chap );
124 memset ( chap, 0, sizeof ( *chap ) );