iPXE
chap.c
Go to the documentation of this file.
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 );
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 }
A CHAP response.
Definition: chap.h:18
uint8_t * response
CHAP response.
Definition: chap.h:24
uint8_t state
State.
Definition: eth_slow.h:47
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:22
Error codes.
void chap_respond(struct chap_response *chap)
Respond to the CHAP challenge.
Definition: chap.c:104
Cryptographic API.
#define ENOMEM
Not enough space.
Definition: errno.h:534
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static void struct digest_algorithm * digest
HMAC-MD5 digest.
Definition: crypto.h:308
int chap_init(struct chap_response *chap, struct digest_algorithm *digest)
Initialise CHAP challenge/response.
Definition: chap.c:51
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:20
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void chap_finish(struct chap_response *chap)
Free resources used by a CHAP response.
Definition: chap.c:122
CHAP protocol.
size_t response_len
Length of CHAP response.
Definition: chap.h:26
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
uint32_t len
Length.
Definition: ena.h:14
size_t ctxsize
Context size.
Definition: crypto.h:21
size_t digestsize
Digest size.
Definition: crypto.h:25
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const char * name
Algorithm name.
Definition: crypto.h:19
A message digest algorithm.
Definition: crypto.h:17
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
void chap_update(struct chap_response *chap, const void *data, size_t len)
Add data to the CHAP challenge.
Definition: chap.c:85
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
void * memset(void *dest, int character, size_t len) __nonnull