iPXE
Functions
chap.c File Reference

CHAP protocol. More...

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/chap.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
 FILE_SECBOOT (PERMITTED)
 
int chap_init (struct chap_response *chap, struct digest_algorithm *digest)
 Initialise CHAP challenge/response. More...
 
void chap_update (struct chap_response *chap, const void *data, size_t len)
 Add data to the CHAP challenge. More...
 
void chap_respond (struct chap_response *chap)
 Respond to the CHAP challenge. More...
 
void chap_finish (struct chap_response *chap)
 Free resources used by a CHAP response. More...
 

Detailed Description

CHAP protocol.

Definition in file chap.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED  )

◆ chap_init()

int chap_init ( struct chap_response chap,
struct digest_algorithm digest 
)

Initialise CHAP challenge/response.

Parameters
chapCHAP challenge/response
digestDigest algorithm to use
Return values
rcReturn status code

Initialises a CHAP challenge/response structure. This routine allocates memory, and so may fail. The allocated memory must eventually be freed by a call to chap_finish().

Definition at line 52 of file chap.c.

53  {
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 }
uint8_t * response
CHAP response.
Definition: chap.h:25
uint8_t state
State.
Definition: eth_slow.h:48
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:23
#define ENOMEM
Not enough space.
Definition: errno.h:535
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:21
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:219
size_t response_len
Length of CHAP response.
Definition: chap.h:27
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:621
size_t ctxsize
Context size.
Definition: crypto.h:23
size_t digestsize
Digest size.
Definition: crypto.h:27
const char * name
Algorithm name.
Definition: crypto.h:21
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References assert(), digest_algorithm::ctxsize, DBG, chap_response::digest, chap_response::digest_context, digest_init(), digest_algorithm::digestsize, ENOMEM, malloc(), digest_algorithm::name, NULL, chap_response::response, chap_response::response_len, and state.

Referenced by eap_rx_md5(), iscsi_handle_chap_i_value(), and iscsi_handle_chap_r_value().

◆ chap_update()

void chap_update ( struct chap_response chap,
const void *  data,
size_t  len 
)

Add data to the CHAP challenge.

Parameters
chapCHAP response
dataData to add
lenLength of data to add

Definition at line 86 of file chap.c.

87  {
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 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:224
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:23
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
ring len
Length.
Definition: dwmac.h:231
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:21
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References assert(), data, chap_response::digest, chap_response::digest_context, digest_update(), len, and NULL.

Referenced by chap_set_identifier(), eap_rx_md5(), iscsi_handle_chap_c_value(), iscsi_handle_chap_i_value(), and iscsi_handle_chap_r_value().

◆ chap_respond()

void chap_respond ( struct chap_response chap)

Respond to the CHAP challenge.

Parameters
chapCHAP response

Calculates the final CHAP response value, and places it in chap->response, with a length of chap->response_len.

Definition at line 105 of file chap.c.

105  {
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 }
uint8_t * response
CHAP response.
Definition: chap.h:25
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:23
static void digest_final(struct digest_algorithm *digest, void *ctx, void *out)
Definition: crypto.h:230
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:21
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References assert(), DBG, chap_response::digest, chap_response::digest_context, digest_final(), NULL, and chap_response::response.

Referenced by eap_rx_md5(), iscsi_handle_chap_c_value(), and iscsi_handle_chap_r_value().

◆ chap_finish()

void chap_finish ( struct chap_response chap)

Free resources used by a CHAP response.

Parameters
chapCHAP response

Definition at line 123 of file chap.c.

123  {
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 }
uint8_t state
State.
Definition: eth_slow.h:48
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:23
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
void * memset(void *dest, int character, size_t len) __nonnull

References DBG, chap_response::digest_context, free, memset(), and state.

Referenced by eap_rx_md5(), iscsi_close_connection(), iscsi_free(), iscsi_handle_chap_i_value(), iscsi_handle_chap_r_value(), and iscsi_login_request_done().