31#include "netif/ppp/ppp_opts.h"
32#if PPP_SUPPORT && CHAP_SUPPORT
39#include "netif/ppp/ppp_impl.h"
41#include "netif/ppp/chap-new.h"
42#include "netif/ppp/chap-md5.h"
43#include "netif/ppp/magic.h"
44#include "netif/ppp/pppcrypt.h"
46#define MD5_HASH_SIZE 16
47#define MD5_MIN_CHALLENGE 17
48#define MD5_MAX_CHALLENGE 24
49#define MD5_MIN_MAX_POWER_OF_TWO_CHALLENGE 3
52static void chap_md5_generate_challenge(ppp_pcb *pcb,
unsigned char *cp) {
56 clen = MD5_MIN_CHALLENGE + magic_pow(MD5_MIN_MAX_POWER_OF_TWO_CHALLENGE);
58 magic_random_bytes(cp, clen);
61static int chap_md5_verify_response(ppp_pcb *pcb,
int id,
const char *name,
62 const unsigned char *secret,
int secret_len,
63 const unsigned char *challenge,
const unsigned char *response,
64 char *
message,
int message_space) {
66 unsigned char idbyte = id;
67 unsigned char hash[MD5_HASH_SIZE];
68 int challenge_len, response_len;
72 challenge_len = *challenge++;
73 response_len = *response++;
74 if (response_len == MD5_HASH_SIZE) {
77 lwip_md5_starts(&ctx);
78 lwip_md5_update(&ctx, &idbyte, 1);
79 lwip_md5_update(&ctx, secret, secret_len);
80 lwip_md5_update(&ctx, challenge, challenge_len);
81 lwip_md5_finish(&ctx, hash);
85 if (memcmp(hash, response, MD5_HASH_SIZE) == 0) {
86 ppp_slprintf(
message, message_space,
"Access granted");
90 ppp_slprintf(
message, message_space,
"Access denied");
95static void chap_md5_make_response(ppp_pcb *pcb,
unsigned char *response,
int id,
const char *our_name,
96 const unsigned char *challenge,
const char *secret,
int secret_len,
97 unsigned char *private_) {
99 unsigned char idbyte = id;
100 int challenge_len = *challenge++;
106 lwip_md5_starts(&ctx);
107 lwip_md5_update(&ctx, &idbyte, 1);
108 lwip_md5_update(&ctx, (
const u_char *)secret, secret_len);
109 lwip_md5_update(&ctx, challenge, challenge_len);
110 lwip_md5_finish(&ctx, &response[1]);
112 response[0] = MD5_HASH_SIZE;
115const struct chap_digest_type md5_digest = {
118 chap_md5_generate_challenge,
119 chap_md5_verify_response,
121 chap_md5_make_response,
#define LWIP_UNUSED_ARG(x)