STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
chap-md5.c
Go to the documentation of this file.
1 /*
2  * chap-md5.c - New CHAP/MD5 implementation.
3  *
4  * Copyright (c) 2003 Paul Mackerras. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. The name(s) of the authors of this software must not be used to
14  * endorse or promote products derived from this software without
15  * prior written permission.
16  *
17  * 3. Redistributions of any form whatsoever must retain the following
18  * acknowledgment:
19  * "This product includes software developed by Paul Mackerras
20  * <paulus@samba.org>".
21  *
22  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29  */
30 
31 #include "lwip/opt.h"
32 #if PPP_SUPPORT && CHAP_SUPPORT /* don't build if not configured for use in lwipopts.h */
33 
34 #if 0 /* UNUSED */
35 #include <stdlib.h>
36 #include <string.h>
37 #endif /* UNUSED */
38 
39 #include "netif/ppp/ppp_impl.h"
40 
41 #include "netif/ppp/chap-new.h"
42 #include "netif/ppp/chap-md5.h"
43 #include "netif/ppp/magic.h"
44 
45 #if LWIP_INCLUDED_POLARSSL_MD5
46 #include "netif/ppp/polarssl/md5.h"
47 #else
48 #include "polarssl/md5.h"
49 #endif
50 
51 #define MD5_HASH_SIZE 16
52 #define MD5_MIN_CHALLENGE 17
53 #define MD5_MAX_CHALLENGE 24
54 #define MD5_MIN_MAX_POWER_OF_TWO_CHALLENGE 3 /* 2^3-1 = 7, 17+7 = 24 */
55 
56 #if PPP_SERVER
57 static void chap_md5_generate_challenge(ppp_pcb *pcb, unsigned char *cp) {
58  int clen;
59  LWIP_UNUSED_ARG(pcb);
60 
61  clen = MD5_MIN_CHALLENGE + magic_pow(MD5_MIN_MAX_POWER_OF_TWO_CHALLENGE);
62  *cp++ = clen;
63  magic_random_bytes(cp, clen);
64 }
65 
66 static int chap_md5_verify_response(ppp_pcb *pcb, int id, const char *name,
67  const unsigned char *secret, int secret_len,
68  const unsigned char *challenge, const unsigned char *response,
69  char *message, int message_space) {
70  md5_context ctx;
71  unsigned char idbyte = id;
72  unsigned char hash[MD5_HASH_SIZE];
73  int challenge_len, response_len;
74  LWIP_UNUSED_ARG(name);
75  LWIP_UNUSED_ARG(pcb);
76 
77  challenge_len = *challenge++;
78  response_len = *response++;
79  if (response_len == MD5_HASH_SIZE) {
80  /* Generate hash of ID, secret, challenge */
81  md5_starts(&ctx);
82  md5_update(&ctx, &idbyte, 1);
83  md5_update(&ctx, secret, secret_len);
84  md5_update(&ctx, challenge, challenge_len);
85  md5_finish(&ctx, hash);
86 
87  /* Test if our hash matches the peer's response */
88  if (memcmp(hash, response, MD5_HASH_SIZE) == 0) {
89  ppp_slprintf(message, message_space, "Access granted");
90  return 1;
91  }
92  }
93  ppp_slprintf(message, message_space, "Access denied");
94  return 0;
95 }
96 #endif /* PPP_SERVER */
97 
98 static void chap_md5_make_response(ppp_pcb *pcb, unsigned char *response, int id, const char *our_name,
99  const unsigned char *challenge, const char *secret, int secret_len,
100  unsigned char *private_) {
101  md5_context ctx;
102  unsigned char idbyte = id;
103  int challenge_len = *challenge++;
104  LWIP_UNUSED_ARG(our_name);
105  LWIP_UNUSED_ARG(private_);
106  LWIP_UNUSED_ARG(pcb);
107 
108  md5_starts(&ctx);
109  md5_update(&ctx, &idbyte, 1);
110  md5_update(&ctx, (const u_char *)secret, secret_len);
111  md5_update(&ctx, challenge, challenge_len);
112  md5_finish(&ctx, &response[1]);
113  response[0] = MD5_HASH_SIZE;
114 }
115 
116 const struct chap_digest_type md5_digest = {
117  CHAP_MD5, /* code */
118 #if PPP_SERVER
119  chap_md5_generate_challenge,
120  chap_md5_verify_response,
121 #endif /* PPP_SERVER */
122  chap_md5_make_response,
123  NULL, /* check_success */
124  NULL, /* handle_failure */
125 };
126 
127 #endif /* PPP_SUPPORT && CHAP_SUPPORT */
128 
#define NULL
Definition: usbd_def.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89