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)
 
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  )

◆ 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 51 of file chap.c.

52  {
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 }
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
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:12
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:20
static void digest_init(struct digest_algorithm *digest, void *ctx)
Definition: crypto.h:172
size_t response_len
Length of CHAP response.
Definition: chap.h:26
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by 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 85 of file chap.c.

86  {
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 }
static void digest_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Definition: crypto.h:177
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:22
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct digest_algorithm * digest
Digest algorithm used for the response.
Definition: chap.h:20
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

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

Referenced by chap_set_identifier(), 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 104 of file chap.c.

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

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

Referenced by 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 122 of file chap.c.

122  {
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 }
uint8_t state
State.
Definition: eth_slow.h:47
uint8_t * digest_context
Context used by the digest algorithm.
Definition: chap.h:22
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
#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 iscsi_close_connection(), iscsi_free(), iscsi_handle_chap_i_value(), iscsi_handle_chap_r_value(), and iscsi_login_request_done().