iPXE
mschapv2_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * MS-CHAPv2 authentication self-tests
29  *
30  */
31 
32 /* Forcibly enable assertions */
33 #undef NDEBUG
34 
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ipxe/mschapv2.h>
38 #include <ipxe/test.h>
39 
40 /** An MS-CHAPv2 test */
41 struct mschapv2_test {
42  /** Username */
43  const char *username;
44  /** Password */
45  const char *password;
46  /** Authenticator challenge */
48  /** Peer challenge */
49  const struct mschapv2_challenge *peer;
50  /** Expected challenge response */
51  const struct mschapv2_response *response;
52  /** Expected authenticator response */
53  const struct mschapv2_auth *auth;
54 };
55 
56 /** Define inline data */
57 #define DATA(...) { __VA_ARGS__ }
58 
59 /** Define an MS-CHAPv2 test */
60 #define MSCHAPV2_TEST( name, USERNAME, PASSWORD, CHALLENGE, PEER, \
61  RESPONSE, AUTH ) \
62  static const struct mschapv2_challenge name ## _challenge = { \
63  .byte = CHALLENGE, \
64  }; \
65  static const struct mschapv2_challenge name ## _peer = { \
66  .byte = PEER, \
67  }; \
68  static const union { \
69  struct mschapv2_response response; \
70  uint8_t byte[ sizeof ( struct mschapv2_response ) ]; \
71  } name ## _response = { \
72  .byte = RESPONSE, \
73  }; \
74  static const union { \
75  struct mschapv2_auth auth; \
76  uint8_t byte[ sizeof ( struct mschapv2_auth ) ]; \
77  } name ## _auth = { \
78  .byte = AUTH, \
79  }; \
80  static struct mschapv2_test name = { \
81  .username = USERNAME, \
82  .password = PASSWORD, \
83  .challenge = &name ## _challenge, \
84  .peer = &name ## _peer, \
85  .response = &name ## _response.response, \
86  .auth = &name ## _auth.auth, \
87  };
88 
89 /** RFC 2759 section 9.2 test case */
90 MSCHAPV2_TEST ( rfc2759_test,
91  "User", "clientPass",
92  DATA ( 0x5b, 0x5d, 0x7c, 0x7d, 0x7b, 0x3f, 0x2f, 0x3e,
93  0x3c, 0x2c, 0x60, 0x21, 0x32, 0x26, 0x26, 0x28 ),
94  DATA ( 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, 0x2a,
95  0x28, 0x29, 0x5f, 0x2b, 0x3a, 0x33, 0x7c, 0x7e ),
96  DATA ( 0x21, 0x40, 0x23, 0x24, 0x25, 0x5e, 0x26, 0x2a,
97  0x28, 0x29, 0x5f, 0x2b, 0x3a, 0x33, 0x7c, 0x7e,
98  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99  0x82, 0x30, 0x9e, 0xcd, 0x8d, 0x70, 0x8b, 0x5e,
100  0xa0, 0x8f, 0xaa, 0x39, 0x81, 0xcd, 0x83, 0x54,
101  0x42, 0x33, 0x11, 0x4a, 0x3d, 0x85, 0xd6, 0xdf,
102  0x00 ),
103  "S=407A5589115FD0D6209F510FE9C04566932CDA56" );
104 
105 /**
106  * Report an MS-CHAPv2 test result
107  *
108  * @v test Authentication test
109  * @v file Test code file
110  * @v line Test code line
111  */
112 static void mschapv2_okx ( struct mschapv2_test *test,
113  const char *file, unsigned int line ) {
114  struct mschapv2_response response;
115  struct mschapv2_auth auth;
116 
117  /* Compute challenge response */
118  mschapv2_response ( test->username, test->password, test->challenge,
119  test->peer, &response );
120  okx ( memcmp ( &response, test->response, sizeof ( response ) ) == 0,
121  file, line );
122 
123  /* Compute authenticator response */
124  mschapv2_auth ( test->username, test->password, test->challenge,
125  test->response, &auth );
126  okx ( memcmp ( &auth, test->auth, sizeof ( auth ) ) == 0, file, line );
127 }
128 #define mschapv2_ok( test ) \
129  mschapv2_okx ( test, __FILE__, __LINE__ )
130 
131 /**
132  * Perform MS-CHAPv2 self-test
133  *
134  */
135 static void mschapv2_test_exec ( void ) {
136 
137  mschapv2_ok ( &rfc2759_test );
138 }
139 
140 /** MS-CHAPv2 self-test */
142  .name = "mschapv2",
143  .exec = mschapv2_test_exec,
144 };
static void mschapv2_okx(struct mschapv2_test *test, const char *file, unsigned int line)
Report an MS-CHAPv2 test result.
const char * password
Password.
Definition: mschapv2_test.c:45
#define MSCHAPV2_TEST(name, USERNAME, PASSWORD, CHALLENGE, PEER, RESPONSE, AUTH)
Define an MS-CHAPv2 test.
Definition: mschapv2_test.c:60
#define DATA(...)
Define inline data.
Definition: mschapv2_test.c:57
Self-test infrastructure.
const char * name
Test set name.
Definition: test.h:17
void mschapv2_response(const char *username, const char *password, const struct mschapv2_challenge *challenge, const struct mschapv2_challenge *peer, struct mschapv2_response *response)
Calculate MS-CHAPv2 challenge response.
Definition: mschapv2.c:269
A self-test set.
Definition: test.h:15
const struct mschapv2_auth * auth
Expected authenticator response.
Definition: mschapv2_test.c:53
An MS-CHAPv2 challenge.
Definition: mschapv2.h:15
#define okx(success, file, line)
Report test result.
Definition: test.h:44
MS-CHAPv2 authentication.
struct self_test mschapv2_test __self_test
MS-CHAPv2 self-test.
const char * username
Username.
Definition: mschapv2_test.c:43
void mschapv2_auth(const char *username, const char *password, const struct mschapv2_challenge *challenge, const struct mschapv2_response *response, struct mschapv2_auth *auth)
Calculate MS-CHAPv2 authenticator response.
Definition: mschapv2.c:308
const struct mschapv2_response * response
Expected challenge response.
Definition: mschapv2_test.c:51
An MS-CHAPv2 challenge response.
Definition: mschapv2.h:27
An MS-CHAPv2 test.
Definition: mschapv2_test.c:41
static void mschapv2_test_exec(void)
Perform MS-CHAPv2 self-test.
An MS-CHAPv2 authenticator response.
Definition: mschapv2.h:39
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const struct mschapv2_challenge * peer
Peer challenge.
Definition: mschapv2_test.c:49
#define mschapv2_ok(test)
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
String functions.
static int test
Definition: epic100.c:73
const struct mschapv2_challenge * challenge
Authenticator challenge.
Definition: mschapv2_test.c:47