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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 #include <ipxe/crypto.h>
32 #include <ipxe/chap.h>
41 * Initialise CHAP challenge/response
43 * @v chap CHAP challenge/response
44 * @v digest Digest algorithm to use
45 * @ret rc Return status code
47 * Initialises a CHAP challenge/response structure. This routine
48 * allocates memory, and so may fail. The allocated memory must
49 * eventually be freed by a call to chap_finish().
51 int chap_init ( struct chap_response *chap,
52 struct digest_algorithm *digest ) {
56 assert ( chap->digest == NULL );
57 assert ( chap->digest_context == NULL );
58 assert ( chap->response == NULL );
60 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
62 state_len = ( digest->ctxsize + digest->digestsize );
63 state = malloc ( state_len );
65 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
70 chap->digest = digest;
71 chap->digest_context = state;
72 chap->response = ( state + digest->ctxsize );
73 chap->response_len = digest->digestsize;
74 digest_init ( chap->digest, chap->digest_context );
79 * Add data to the CHAP challenge
81 * @v chap CHAP response
83 * @v len Length of data to add
85 void chap_update ( struct chap_response *chap, const void *data,
87 assert ( chap->digest != NULL );
88 assert ( chap->digest_context != NULL );
93 digest_update ( chap->digest, chap->digest_context, data, len );
97 * Respond to the CHAP challenge
99 * @v chap CHAP response
101 * Calculates the final CHAP response value, and places it in @c
102 * chap->response, with a length of @c chap->response_len.
104 void chap_respond ( struct chap_response *chap ) {
105 assert ( chap->digest != NULL );
106 assert ( chap->digest_context != NULL );
107 assert ( chap->response != NULL );
109 DBG ( "CHAP %p responding to challenge\n", chap );
111 if ( ! chap->digest )
114 digest_final ( chap->digest, chap->digest_context, chap->response );
118 * Free resources used by a CHAP response
120 * @v chap CHAP response
122 void chap_finish ( struct chap_response *chap ) {
123 void *state = chap->digest_context;
125 DBG ( "CHAP %p finished\n", chap );
128 memset ( chap, 0, sizeof ( *chap ) );