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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stddef.h>
28#include <stdlib.h>
29#include <string.h>
30#include <errno.h>
31#include <assert.h>
32#include <ipxe/crypto.h>
33#include <ipxe/chap.h>
34
35/** @file
36 *
37 * CHAP protocol
38 *
39 */
40
41/**
42 * Initialise CHAP challenge/response
43 *
44 * @v chap CHAP challenge/response
45 * @v digest Digest algorithm to use
46 * @ret rc Return status code
47 *
48 * Initialises a CHAP challenge/response structure. This routine
49 * allocates memory, and so may fail. The allocated memory must
50 * eventually be freed by a call to chap_finish().
51 */
52int chap_init ( struct chap_response *chap,
53 struct digest_algorithm *digest ) {
54 size_t state_len;
55 void *state;
56
57 assert ( chap->digest == NULL );
58 assert ( chap->digest_context == NULL );
59 assert ( chap->response == NULL );
60
61 DBG ( "CHAP %p initialising with %s digest\n", chap, digest->name );
62
63 state_len = ( digest->ctxsize + digest->digestsize );
64 state = malloc ( state_len );
65 if ( ! state ) {
66 DBG ( "CHAP %p could not allocate %zd bytes for state\n",
67 chap, state_len );
68 return -ENOMEM;
69 }
70
71 chap->digest = digest;
72 chap->digest_context = state;
73 chap->response = ( state + digest->ctxsize );
74 chap->response_len = digest->digestsize;
75 digest_init ( chap->digest, chap->digest_context );
76 return 0;
77}
78
79/**
80 * Add data to the CHAP challenge
81 *
82 * @v chap CHAP response
83 * @v data Data to add
84 * @v len Length of data to add
85 */
86void chap_update ( struct chap_response *chap, const void *data,
87 size_t len ) {
88 assert ( chap->digest != NULL );
89 assert ( chap->digest_context != NULL );
90
91 if ( ! chap->digest )
92 return;
93
94 digest_update ( chap->digest, chap->digest_context, data, len );
95}
96
97/**
98 * Respond to the CHAP challenge
99 *
100 * @v chap CHAP response
101 *
102 * Calculates the final CHAP response value, and places it in @c
103 * chap->response, with a length of @c chap->response_len.
104 */
105void chap_respond ( struct chap_response *chap ) {
106 assert ( chap->digest != NULL );
107 assert ( chap->digest_context != NULL );
108 assert ( chap->response != NULL );
109
110 DBG ( "CHAP %p responding to challenge\n", chap );
111
112 if ( ! chap->digest )
113 return;
114
115 digest_final ( chap->digest, chap->digest_context, chap->response );
116}
117
118/**
119 * Free resources used by a CHAP response
120 *
121 * @v chap CHAP response
122 */
123void chap_finish ( struct chap_response *chap ) {
124 void *state = chap->digest_context;
125
126 DBG ( "CHAP %p finished\n", chap );
127
128 free ( state );
129 memset ( chap, 0, sizeof ( *chap ) );
130}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
Assertions.
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
void chap_respond(struct chap_response *chap)
Respond to the CHAP challenge.
Definition chap.c:105
void chap_update(struct chap_response *chap, const void *data, size_t len)
Add data to the CHAP challenge.
Definition chap.c:86
void chap_finish(struct chap_response *chap)
Free resources used by a CHAP response.
Definition chap.c:123
int chap_init(struct chap_response *chap, struct digest_algorithm *digest)
Initialise CHAP challenge/response.
Definition chap.c:52
CHAP protocol.
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
uint8_t state
State.
Definition eth_slow.h:36
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ENOMEM
Not enough space.
Definition errno.h:535
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
Cryptographic API.
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition crypto.h:219
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition crypto.h:230
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition crypto.h:224
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
A CHAP response.
Definition chap.h:19
uint8_t * digest_context
Context used by the digest algorithm.
Definition chap.h:23
uint8_t * response
CHAP response.
Definition chap.h:25
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition chap.h:21
size_t response_len
Length of CHAP response.
Definition chap.h:27
A message digest algorithm.
Definition crypto.h:19
size_t digestsize
Digest size.
Definition crypto.h:27
size_t ctxsize
Context size.
Definition crypto.h:23
const char * name
Algorithm name.
Definition crypto.h:21