iPXE
chap.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stddef.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <errno.h>
00030 #include <assert.h>
00031 #include <ipxe/crypto.h>
00032 #include <ipxe/chap.h>
00033 
00034 /** @file
00035  *
00036  * CHAP protocol
00037  *
00038  */
00039 
00040 /**
00041  * Initialise CHAP challenge/response
00042  *
00043  * @v chap              CHAP challenge/response
00044  * @v digest            Digest algorithm to use
00045  * @ret rc              Return status code
00046  *
00047  * Initialises a CHAP challenge/response structure.  This routine
00048  * allocates memory, and so may fail.  The allocated memory must
00049  * eventually be freed by a call to chap_finish().
00050  */
00051 int chap_init ( struct chap_response *chap,
00052                 struct digest_algorithm *digest ) {
00053         size_t state_len;
00054         void *state;
00055 
00056         assert ( chap->digest == NULL );
00057         assert ( chap->digest_context == NULL );
00058         assert ( chap->response == NULL );
00059 
00060         DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
00061 
00062         state_len = ( digest->ctxsize + digest->digestsize );
00063         state = malloc ( state_len );
00064         if ( ! state ) {
00065                 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
00066                       chap, state_len );
00067                 return -ENOMEM;
00068         }
00069         
00070         chap->digest = digest;
00071         chap->digest_context = state;
00072         chap->response = ( state + digest->ctxsize );
00073         chap->response_len = digest->digestsize;
00074         digest_init ( chap->digest, chap->digest_context );
00075         return 0;
00076 }
00077 
00078 /**
00079  * Add data to the CHAP challenge
00080  *
00081  * @v chap              CHAP response
00082  * @v data              Data to add
00083  * @v len               Length of data to add
00084  */
00085 void chap_update ( struct chap_response *chap, const void *data,
00086                    size_t len ) {
00087         assert ( chap->digest != NULL );
00088         assert ( chap->digest_context != NULL );
00089 
00090         if ( ! chap->digest )
00091                 return;
00092 
00093         digest_update ( chap->digest, chap->digest_context, data, len );
00094 }
00095 
00096 /**
00097  * Respond to the CHAP challenge
00098  *
00099  * @v chap              CHAP response
00100  *
00101  * Calculates the final CHAP response value, and places it in @c
00102  * chap->response, with a length of @c chap->response_len.
00103  */
00104 void chap_respond ( struct chap_response *chap ) {
00105         assert ( chap->digest != NULL );
00106         assert ( chap->digest_context != NULL );
00107         assert ( chap->response != NULL );
00108 
00109         DBG ( "CHAP %p responding to challenge\n", chap );
00110 
00111         if ( ! chap->digest )
00112                 return;
00113 
00114         digest_final ( chap->digest, chap->digest_context, chap->response );
00115 }
00116 
00117 /**
00118  * Free resources used by a CHAP response
00119  *
00120  * @v chap              CHAP response
00121  */
00122 void chap_finish ( struct chap_response *chap ) {
00123         void *state = chap->digest_context;
00124 
00125         DBG ( "CHAP %p finished\n", chap );
00126 
00127         free ( state );
00128         memset ( chap, 0, sizeof ( *chap ) );
00129 }