Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / chap.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <ipxe/crypto.h>
28 #include <ipxe/chap.h>
29
30 /** @file
31  *
32  * CHAP protocol
33  *
34  */
35
36 /**
37  * Initialise CHAP challenge/response
38  *
39  * @v chap              CHAP challenge/response
40  * @v digest            Digest algorithm to use
41  * @ret rc              Return status code
42  *
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().
46  */
47 int chap_init ( struct chap_response *chap,
48                 struct digest_algorithm *digest ) {
49         size_t state_len;
50         void *state;
51
52         assert ( chap->digest == NULL );
53         assert ( chap->digest_context == NULL );
54         assert ( chap->response == NULL );
55
56         DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
57
58         state_len = ( digest->ctxsize + digest->digestsize );
59         state = malloc ( state_len );
60         if ( ! state ) {
61                 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
62                       chap, state_len );
63                 return -ENOMEM;
64         }
65         
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 );
71         return 0;
72 }
73
74 /**
75  * Add data to the CHAP challenge
76  *
77  * @v chap              CHAP response
78  * @v data              Data to add
79  * @v len               Length of data to add
80  */
81 void chap_update ( struct chap_response *chap, const void *data,
82                    size_t len ) {
83         assert ( chap->digest != NULL );
84         assert ( chap->digest_context != NULL );
85
86         if ( ! chap->digest )
87                 return;
88
89         digest_update ( chap->digest, chap->digest_context, data, len );
90 }
91
92 /**
93  * Respond to the CHAP challenge
94  *
95  * @v chap              CHAP response
96  *
97  * Calculates the final CHAP response value, and places it in @c
98  * chap->response, with a length of @c chap->response_len.
99  */
100 void chap_respond ( struct chap_response *chap ) {
101         assert ( chap->digest != NULL );
102         assert ( chap->digest_context != NULL );
103         assert ( chap->response != NULL );
104
105         DBG ( "CHAP %p responding to challenge\n", chap );
106
107         if ( ! chap->digest )
108                 return;
109
110         digest_final ( chap->digest, chap->digest_context, chap->response );
111 }
112
113 /**
114  * Free resources used by a CHAP response
115  *
116  * @v chap              CHAP response
117  */
118 void chap_finish ( struct chap_response *chap ) {
119         void *state = chap->digest_context;
120
121         DBG ( "CHAP %p finished\n", chap );
122
123         free ( state );
124         memset ( chap, 0, sizeof ( *chap ) );
125 }