[pci] Add support for PCI MSI-X interrupts
[ipxe.git] / 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 * 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.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <ipxe/crypto.h>
32 #include <ipxe/chap.h>
33
34 /** @file
35 *
36 * CHAP protocol
37 *
38 */
39
40 /**
41 * Initialise CHAP challenge/response
42 *
43 * @v chap CHAP challenge/response
44 * @v digest Digest algorithm to use
45 * @ret rc Return status code
46 *
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().
50 */
51 int chap_init ( struct chap_response *chap,
52 struct digest_algorithm *digest ) {
53 size_t state_len;
54 void *state;
55
56 assert ( chap->digest == NULL );
57 assert ( chap->digest_context == NULL );
58 assert ( chap->response == NULL );
59
60 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
61
62 state_len = ( digest->ctxsize + digest->digestsize );
63 state = malloc ( state_len );
64 if ( ! state ) {
65 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
66 chap, state_len );
67 return -ENOMEM;
68 }
69
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 );
75 return 0;
76 }
77
78 /**
79 * Add data to the CHAP challenge
80 *
81 * @v chap CHAP response
82 * @v data Data to add
83 * @v len Length of data to add
84 */
85 void chap_update ( struct chap_response *chap, const void *data,
86 size_t len ) {
87 assert ( chap->digest != NULL );
88 assert ( chap->digest_context != NULL );
89
90 if ( ! chap->digest )
91 return;
92
93 digest_update ( chap->digest, chap->digest_context, data, len );
94 }
95
96 /**
97 * Respond to the CHAP challenge
98 *
99 * @v chap CHAP response
100 *
101 * Calculates the final CHAP response value, and places it in @c
102 * chap->response, with a length of @c chap->response_len.
103 */
104 void chap_respond ( struct chap_response *chap ) {
105 assert ( chap->digest != NULL );
106 assert ( chap->digest_context != NULL );
107 assert ( chap->response != NULL );
108
109 DBG ( "CHAP %p responding to challenge\n", chap );
110
111 if ( ! chap->digest )
112 return;
113
114 digest_final ( chap->digest, chap->digest_context, chap->response );
115 }
116
117 /**
118 * Free resources used by a CHAP response
119 *
120 * @v chap CHAP response
121 */
122 void chap_finish ( struct chap_response *chap ) {
123 void *state = chap->digest_context;
124
125 DBG ( "CHAP %p finished\n", chap );
126
127 free ( state );
128 memset ( chap, 0, sizeof ( *chap ) );
129 }