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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stddef.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <assert.h>
00026 #include <gpxe/crypto.h>
00027 #include <gpxe/chap.h>
00028 
00029 /** @file
00030  *
00031  * CHAP protocol
00032  *
00033  */
00034 
00035 /**
00036  * Initialise CHAP challenge/response
00037  *
00038  * @v chap              CHAP challenge/response
00039  * @v digest            Digest algorithm to use
00040  * @ret rc              Return status code
00041  *
00042  * Initialises a CHAP challenge/response structure.  This routine
00043  * allocates memory, and so may fail.  The allocated memory must
00044  * eventually be freed by a call to chap_finish().
00045  */
00046 int chap_init ( struct chap_response *chap,
00047                 struct digest_algorithm *digest ) {
00048         size_t state_len;
00049         void *state;
00050 
00051         assert ( chap->digest == NULL );
00052         assert ( chap->digest_context == NULL );
00053         assert ( chap->response == NULL );
00054 
00055         DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
00056 
00057         state_len = ( digest->ctxsize + digest->digestsize );
00058         state = malloc ( state_len );
00059         if ( ! state ) {
00060                 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
00061                       chap, state_len );
00062                 return -ENOMEM;
00063         }
00064         
00065         chap->digest = digest;
00066         chap->digest_context = state;
00067         chap->response = ( state + digest->ctxsize );
00068         chap->response_len = digest->digestsize;
00069         digest_init ( chap->digest, chap->digest_context );
00070         return 0;
00071 }
00072 
00073 /**
00074  * Add data to the CHAP challenge
00075  *
00076  * @v chap              CHAP response
00077  * @v data              Data to add
00078  * @v len               Length of data to add
00079  */
00080 void chap_update ( struct chap_response *chap, const void *data,
00081                    size_t len ) {
00082         assert ( chap->digest != NULL );
00083         assert ( chap->digest_context != NULL );
00084 
00085         if ( ! chap->digest )
00086                 return;
00087 
00088         digest_update ( chap->digest, chap->digest_context, data, len );
00089 }
00090 
00091 /**
00092  * Respond to the CHAP challenge
00093  *
00094  * @v chap              CHAP response
00095  *
00096  * Calculates the final CHAP response value, and places it in @c
00097  * chap->response, with a length of @c chap->response_len.
00098  */
00099 void chap_respond ( struct chap_response *chap ) {
00100         assert ( chap->digest != NULL );
00101         assert ( chap->digest_context != NULL );
00102         assert ( chap->response != NULL );
00103 
00104         DBG ( "CHAP %p responding to challenge\n", chap );
00105 
00106         if ( ! chap->digest )
00107                 return;
00108 
00109         digest_final ( chap->digest, chap->digest_context, chap->response );
00110 }
00111 
00112 /**
00113  * Free resources used by a CHAP response
00114  *
00115  * @v chap              CHAP response
00116  */
00117 void chap_finish ( struct chap_response *chap ) {
00118         void *state = chap->digest_context;
00119 
00120         DBG ( "CHAP %p finished\n", chap );
00121 
00122         free ( state );
00123         memset ( chap, 0, sizeof ( *chap ) );
00124 }

Generated on Tue Apr 6 20:00:52 2010 for gPXE by  doxygen 1.5.7.1