ike_eap_mschapv2.c
Go to the documentation of this file.
1 /**
2  * @file ike_eap_mschapv2.c
3  * @brief EAP MS-CHAP-V2 authentication method
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneIPSEC Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL IKE_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ike/ike.h"
36 #include "ike/ike_eap.h"
37 #include "ike/ike_eap_mschapv2.h"
38 #include "debug.h"
39 
40 //Check IKEv2 library configuration
41 #if (IKE_EAP_AUTH_SUPPORT == ENABLED && IKE_EAP_MSCHAPV2_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Process EAP MS-CHAP-V2 request
46  * @param[in] sa Pointer to the IKE SA
47  * @param[in] request Pointer to the EAP MS-CHAP-V2 request
48  * @param[in] length Length of the EAP MS-CHAP-V2 request, in bytes
49  * @return Error code
50  **/
51 
53  const IkeEapMschapv2Packet *request, size_t length)
54 {
55  error_t error;
56 
57  //Malformed MS-CHAP-V2 packet?
58  if(length < sizeof(IkeEapMschapv2Packet))
59  {
60  return ERROR_INVALID_LENGTH;
61  }
62 
63  //The OpCode field identifies the type of MS-CHAP-V2 packet
64  if(request->opcode == IKE_EAP_MSCHAPV2_CODE_CHALLENGE)
65  {
66  //Process EAP MS-CHAP-V2 Challenge packet
68  (IkeEapMschapv2Challenge *) request, length);
69  }
70  else if(request->opcode == IKE_EAP_MSCHAPV2_CODE_SUCCESS)
71  {
72  //Process EAP MS-CHAP-V2 Success Request packet
75  }
76  else if(request->opcode == IKE_EAP_MSCHAPV2_CODE_FAILURE)
77  {
78  //Process EAP MS-CHAP-V2 Failure Request packet
81  }
82  else
83  {
84  //Invalid opcode
85  error = ERROR_INVALID_TYPE;
86  }
87 
88  //Return status code
89  return error;
90 }
91 
92 
93 /**
94  * @brief Process EAP MS-CHAP-V2 Challenge packet
95  * @param[in] sa Pointer to the IKE SA
96  * @param[in] challenge Pointer to the EAP MS-CHAP-V2 Challenge packet
97  * @param[in] length Length of the EAP MS-CHAP-V2 Challenge packet, in bytes
98  * @return Error code
99  **/
100 
103 {
104  error_t error;
105  IkeContext *context;
106 
107  //Point to the IKE context
108  context = sa->context;
109 
110  //Check current state
111  if(sa->eapState != IKE_EAP_STATE_INIT &&
112  sa->eapState != IKE_EAP_STATE_IDENTITY)
113  {
114  return ERROR_WRONG_STATE;
115  }
116 
117  //Malformed MS-CHAP-V2 packet?
118  if(length < sizeof(IkeEapMschapv2Challenge))
119  {
120  return ERROR_INVALID_LENGTH;
121  }
122 
123  //The MS-Length field is two octets and must be set to the value of the
124  //Length field minus 5
125  if(ntohs(challenge->msLength) != (length - sizeof(IkeEapRequest)))
126  {
127  return ERROR_INVALID_LENGTH;
128  }
129 
130  //The Value-Size field is one octet and indicates the length of the
131  //Challenge field. Since EAP MS-CHAP-V2 utilizes a 16 octet Challenge
132  //field, it is set to 16
133  if(challenge->valueSize != 16)
134  {
135  return ERROR_INVALID_LENGTH;
136  }
137 
138  //The MS-CHAPv2-ID field is one octet and aids in matching MS-CHAP-V2
139  //responses with requests
140  sa->mschapv2Id = challenge->mschapv2Id;
141 
142  //The Challenge field is 16 octets
143  osMemcpy(sa->authChallenge, challenge->challenge, 16);
144 
145  //The Peer-Challenge field is generated by the peer and is used in the
146  //calculation of the NT-Response field
147  error = context->prngAlgo->generate(context->prngContext, sa->peerChallenge,
148  16);
149  //Check status code
150  if(error)
151  {
152  return error;
153  }
154 
155  //The NT-Response field is an encoded function of the password, the user
156  //name, the contents of the Peer-Challenge field and the received challenge
157  //as output by the routine GenerateNTResponse()
158  ikeEapMschapv2GenerateNtResponse(sa->authChallenge, sa->peerChallenge,
159  context->username, context->password, sa->ntResponse);
160 
161  //Send an EAP MS-CHAP-V2 Response packet
162  sa->eapState = IKE_EAP_STATE_MSCHAPV2_CHALLENGE;
163 
164  //Sucessful processing
165  return NO_ERROR;
166 }
167 
168 
169 /**
170  * @brief Process EAP MS-CHAP-V2 Success Request packet
171  * @param[in] sa Pointer to the IKE SA
172  * @param[in] request Pointer to the EAP MS-CHAP-V2 Success Request packet
173  * @param[in] length Length of the EAP MS-CHAP-V2 Success Request packet, in bytes
174  * @return Error code
175  **/
176 
178  const IkeEapMschapv2SuccessRequest *request, size_t length)
179 {
180  bool_t valid;
181  IkeContext *context;
182 
183  //Point to the IKE context
184  context = sa->context;
185 
186  //Check current state
187  if(sa->eapState != IKE_EAP_STATE_MSCHAPV2_CHALLENGE)
188  {
189  return ERROR_WRONG_STATE;
190  }
191 
192  //Malformed MS-CHAP-V2 packet?
194  {
195  return ERROR_INVALID_LENGTH;
196  }
197 
198  //The MS-Length field is two octets and must be set to the value of the
199  //Length field minus 5
200  if(ntohs(request->msLength) != (length - sizeof(IkeEapRequest)))
201  {
202  return ERROR_INVALID_LENGTH;
203  }
204 
205  //The Message field contains a 42-octet authenticator response string and a
206  //printable message (refer to RFC 2759, section 5)
207  if(length < (sizeof(IkeEapMschapv2SuccessRequest) + 42))
208  {
209  return ERROR_INVALID_LENGTH;
210  }
211 
212  //The authenticating peer must verify the authenticator response when a
213  //Success packet is received (refer to RFC 2759, section 5)
214  valid = ikeEapMschapv2CheckAuthResponse(context->password, sa->ntResponse,
215  sa->peerChallenge, sa->authChallenge, context->username,
216  request->message);
217 
218  //Valid authenticator response?
219  if(valid)
220  {
221  //Upon successful authentication, generate the master session key
222  ikeEapMschapv2GenerateMasterSessionKey(context->password, sa->ntResponse,
223  sa->msk);
224 
225  //An EAP method supporting key derivation must export a master session key
226  //of at least 64 octet (refer to RFC 3748, section 7.10)
227  sa->mskLen = 64;
228 
229  //If the peer successfully validates the EAP MS-CHAP-V2 Success Request
230  //packet sent by the authenticator, then it must respond with an EAP
231  //MS-CHAP-V2 Success Response packet
232  sa->eapState = IKE_EAP_STATE_MSCHAPV2_SUCCESS;
233  }
234  else
235  {
236  //If the authenticator response is either missing or incorrect, the peer
237  //must end the session (refer to RFC 2759, section 5)
239  }
240 
241  //Sucessful processing
242  return NO_ERROR;
243 }
244 
245 
246 /**
247  * @brief Process EAP MS-CHAP-V2 Failure Request packet
248  * @param[in] sa Pointer to the IKE SA
249  * @param[in] request Pointer to the EAP MS-CHAP-V2 Failure Request packet
250  * @param[in] length Length of the EAP MS-CHAP-V2 Failure Request packet, in bytes
251  * @return Error code
252  **/
253 
255  const IkeEapMschapv2FailureRequest *request, size_t length)
256 {
257  //Check current state
258  if(sa->eapState != IKE_EAP_STATE_MSCHAPV2_CHALLENGE)
259  {
260  return ERROR_WRONG_STATE;
261  }
262 
263  //Malformed MS-CHAP-V2 packet?
265  {
266  return ERROR_INVALID_LENGTH;
267  }
268 
269  //The MS-Length field is two octets and must be set to the value of the
270  //Length field minus 5
271  if(ntohs(request->msLength) != (length - sizeof(IkeEapRequest)))
272  {
273  return ERROR_INVALID_LENGTH;
274  }
275 
276  //Send an EAP MS-CHAP-V2 Failure Response packet
277  sa->eapState = IKE_EAP_STATE_MSCHAPV2_FAILURE;
278 
279  //Sucessful processing
280  return NO_ERROR;
281 }
282 
283 
284 /**
285  * @brief Format EAP MS-CHAP-V2 Response packet
286  * @param[in] sa Pointer to the IKE SA
287  * @param[out] p Buffer where to format the EAP MS-CHAP-V2 packet
288  * @param[out] written Length of the resulting EAP MS-CHAP-V2 packet
289  * @return Error code
290  **/
291 
293  size_t *written)
294 {
295  size_t n;
296  IkeContext *context;
297  IkeEapMschapv2Response *response;
298 
299  //Point to the IKE context
300  context = sa->context;
301 
302  //Point to the buffer where to format the EAP packet
303  response = (IkeEapMschapv2Response *) p;
304 
305  //Format EAP header
306  response->code = IKE_EAP_CODE_RESPONSE;
307  response->identifier = sa->eapRequestId;
308  response->type = IKE_EAP_METHOD_TYPE_MSCHAPV2;
309 
310  //The OpCode field is one octet and identifies the type of MS-CHAP-V2 packet
311  response->opcode = IKE_EAP_MSCHAPV2_CODE_RESPONSE;
312 
313  //The MS-CHAPv2-ID field is one octet and aids in matching MS-CHAP-V2
314  //responses with requests
315  response->mschapv2Id = sa->mschapv2Id;
316 
317  //The Value-Size field is one octet and indicates the length of the Response
318  //field. It is set to 49
319  response->valueSize = 49;
320 
321  //The Peer-Challenge field is a 16-octet random number
322  osMemcpy(response->peerChallenge, sa->peerChallenge, 16);
323 
324  //The Reserved field must be zero
325  osMemset(response->reserved, 0, 8);
326 
327  //The NT-Response field is an encoded function of the password, the user
328  //name, the contents of the Peer-Challenge field and the received
329  //challenge as output by the routine GenerateNTResponse()
330  osMemcpy(response->ntResponse, sa->ntResponse, 24);
331 
332  //The Flags field is reserved for future use and MUST be zero
333  response->flags = 0;
334 
335  //Determine the length of the user name
336  n = osStrlen(context->username);
337 
338  //The Name field is a string of 0 to (theoretically) 256 case-sensitive
339  //ASCII characters which identifies the peer's user account name
340  osMemcpy(response->name, context->username, n);
341 
342  //Length of the EAP MS-CHAP-V2 Response packet
343  n += sizeof(IkeEapMschapv2Response);
344 
345  //The Length field is two octets and indicates the length of the EAP
346  //packet including the Code, Identifier, Length, Type, OpCode,
347  //MS-CHAPv2-ID, MS-Length, Value-Size, Challenge, and Name fields
348  response->length = htons(n);
349 
350  //The MS-Length field is two octets and must be set to the value of the
351  //Length field minus 5
352  response->msLength = htons(n - sizeof(IkeEapResponse));
353 
354  //Total length of the EAP packet
355  *written = n;
356 
357  //Sucessful processing
358  return NO_ERROR;
359 }
360 
361 
362 /**
363  * @brief Format EAP MS-CHAP-V2 Success Response packet
364  * @param[in] sa Pointer to the IKE SA
365  * @param[out] p Buffer where to format the EAP MS-CHAP-V2 packet
366  * @param[out] written Length of the resulting EAP MS-CHAP-V2 packet
367  * @return Error code
368  **/
369 
371  size_t *written)
372 {
374 
375  //Point to the buffer where to format the EAP packet
376  response = (IkeEapMschapv2SuccessResponse *) p;
377 
378  //Format EAP header
379  response->code = IKE_EAP_CODE_RESPONSE;
380  response->identifier = sa->eapRequestId;
381  response->type = IKE_EAP_METHOD_TYPE_MSCHAPV2;
382 
383  //The OpCode field is one octet and identifies the type of MS-CHAP-V2 packet
384  response->opcode = IKE_EAP_MSCHAPV2_CODE_SUCCESS;
385 
386  //Total length of the EAP packet
387  *written = sizeof(IkeEapMschapv2SuccessResponse);
388  //Convert the length field to network byte order
389  response->length = htons(*written);
390 
391  //Sucessful processing
392  return NO_ERROR;
393 }
394 
395 
396 /**
397  * @brief Format EAP MS-CHAP-V2 Failure Response packet
398  * @param[in] sa Pointer to the IKE SA
399  * @param[out] p Buffer where to format the EAP MS-CHAP-V2 packet
400  * @param[out] written Length of the resulting EAP MS-CHAP-V2 packet
401  * @return Error code
402  **/
403 
405  size_t *written)
406 {
408 
409  //Point to the buffer where to format the EAP packet
410  response = (IkeEapMschapv2FailureResponse *) p;
411 
412  //Format EAP header
413  response->code = IKE_EAP_CODE_RESPONSE;
414  response->identifier = sa->eapRequestId;
415  response->type = IKE_EAP_METHOD_TYPE_MSCHAPV2;
416 
417  //The OpCode field is one octet and identifies the type of MS-CHAP-V2 packet
418  response->opcode = IKE_EAP_MSCHAPV2_CODE_FAILURE;
419 
420  //Total length of the EAP packet
421  *written = sizeof(IkeEapMschapv2FailureResponse);
422  //Convert the length field to network byte order
423  response->length = htons(*written);
424 
425  //Sucessful processing
426  return NO_ERROR;
427 }
428 
429 
430 /**
431  * @brief GenerateNTResponse routine
432  * @param[in] authChallenge Authenticator challenge (16 bytes)
433  * @param[in] peerChallenge Peer challenge (16 bytes)
434  * @param[in] username NULL-terminated string containing the user name
435  * @param[in] password NULL-terminated string containing the password
436  * @param[out] response Resulting NT-Response field (24 bytes)
437  **/
438 
439 void ikeEapMschapv2GenerateNtResponse(const uint8_t *authChallenge,
440  const uint8_t *peerChallenge, const char_t *username,
441  const char_t *password, uint8_t *response)
442 {
443  uint8_t challenge[8];
444  uint8_t passwordHash[16];
445 
446  //Compute ChallengeHash(PeerChallenge, AuthenticatorChallenge, UserName)
447  ikeEapMschapv2ChallengeHash(peerChallenge, authChallenge, username, challenge);
448 
449  //Compute NtPasswordHash(Password)
450  ikeEapMschapv2NtPasswordHash(password, passwordHash);
451 
452  //Compute ChallengeResponse(Challenge, PasswordHash)
453  ikeEapMschapv2ChallengeResponse(challenge, passwordHash, response);
454 
455  //Erase working state
456  cryptoZeroizeMem(passwordHash, sizeof(passwordHash));
457 }
458 
459 
460 /**
461  * @brief ChallengeHash routine
462  * @param[in] peerChallenge Peer challenge (16 bytes)
463  * @param[in] authChallenge Authenticator challenge (16 bytes)
464  * @param[in] username NULL-terminated string containing the user name
465  * @param[out] challenge Resulting challenge (8 bytes)
466  **/
467 
469  const uint8_t *authChallenge, const char_t *username, uint8_t *challenge)
470 {
471  Sha1Context sha1Context;
472  uint8_t digest[SHA1_DIGEST_SIZE];
473 
474  //Compute ChallengeHash(PeerChallenge, AuthenticatorChallenge, UserName)
475  sha1Init(&sha1Context);
476  sha1Update(&sha1Context, peerChallenge, 16);
477  sha1Update(&sha1Context, authChallenge, 16);
478  sha1Update(&sha1Context, username, osStrlen(username));
479  sha1Final(&sha1Context, digest);
480  sha1Deinit(&sha1Context);
481 
482  //Return the first 8 bytes of the SHA-1 digest
483  osMemcpy(challenge, digest, 8);
484 }
485 
486 
487 /**
488  * @brief NtPasswordHash routine
489  * @param[in] password NULL-terminated string containing the password
490  * @param[out] passwordHash Password hash (16 bytes)
491  **/
492 
493 void ikeEapMschapv2NtPasswordHash(const char_t *password, uint8_t *passwordHash)
494 {
495  size_t i;
496  size_t n;
497  char_t zero;
498  Md4Context md4Context;
499 
500  //Initialize variable
501  zero = 0;
502 
503  //Determine the length of the password
504  n = osStrlen(password);
505 
506  //Use the MD4 algorithm to irreversibly hash Password into PasswordHash
507  md4Init(&md4Context);
508 
509  //Only the password is hashed without including any terminating 0
510  for(i = 0; i < n; i++)
511  {
512  //The password is a string of Unicode characters (refer to RFC 2759,
513  //section 8.3)
514  md4Update(&md4Context, &password[i], sizeof(char_t));
515  md4Update(&md4Context, &zero, sizeof(char_t));
516  }
517 
518  //Finalize MD4 computation
519  md4Final(&md4Context, passwordHash);
520  md4Deinit(&md4Context);
521 }
522 
523 
524 /**
525  * @brief HashNtPasswordHash routine
526  * @param[in] passwordHash Password hash (16 bytes)
527  * @param[out] passwordHashHash Resulting hash (16 bytes)
528  **/
529 
530 void ikeEapMschapv2HashNtPasswordHash(const uint8_t *passwordHash,
531  uint8_t *passwordHashHash)
532 {
533  Md4Context md4Context;
534 
535  //Use the MD4 algorithm to irreversibly hash PasswordHash into
536  //PasswordHashHash
537  md4Init(&md4Context);
538  md4Update(&md4Context, passwordHash, 16);
539  md4Final(&md4Context, passwordHashHash);
540  md4Deinit(&md4Context);
541 }
542 
543 
544 /**
545  * @brief ChallengeResponse routine
546  * @param[in] challenge Challenge (8 bytes)
547  * @param[in] passwordHash Password hash (16 bytes)
548  * @param[out] response Resulting response (24 bytes)
549  **/
550 
552  const uint8_t *passwordHash, uint8_t *response)
553 {
554  DesContext desContext;
555  uint8_t zPasswordHash[21];
556  uint8_t key[8];
557 
558  //Set ZPasswordHash to PasswordHash zero-padded to 21 octets (refer to
559  //RFC 2759, section 8.5)
560  osMemcpy(zPasswordHash, passwordHash, 16);
561  osMemset(zPasswordHash + 16, 0, 5);
562 
563  //Compute DesEncrypt(Challenge, 1st 7-octets of ZPasswordHash)
564  desComputeKeyParity(zPasswordHash, key);
565  desInit(&desContext, key, 8);
566  desEncryptBlock(&desContext, challenge, response);
567 
568  //Compute DesEncrypt(Challenge, 2nd 7-octets of ZPasswordHash)
569  desComputeKeyParity(zPasswordHash + 7, key);
570  desInit(&desContext, key, 8);
571  desEncryptBlock(&desContext, challenge, response + 8);
572 
573  //Compute DesEncrypt(Challenge, 3rd 7-octets of ZPasswordHash)
574  desComputeKeyParity(zPasswordHash + 14, key);
575  desInit(&desContext, key, 8);
576  desEncryptBlock(&desContext, challenge, response + 16);
577  desDeinit(&desContext);
578 
579  //Erase working state
580  cryptoZeroizeMem(zPasswordHash, sizeof(zPasswordHash));
581  cryptoZeroizeMem(key, sizeof(key));
582 }
583 
584 
585 /**
586  * @brief GenerateAuthenticatorResponse routine
587  * @param[in] password NULL-terminated string containing the password
588  * @param[out] ntResponse NT-Response field (24 bytes)
589  * @param[in] peerChallenge Peer challenge (16 bytes)
590  * @param[in] authChallenge Authenticator challenge (16 bytes)
591  * @param[in] username NULL-terminated string containing the user name
592  * @param[out] authResponse Resulting authenticator response (42 bytes)
593  **/
594 
596  const uint8_t *ntResponse, const uint8_t *peerChallenge,
597  const uint8_t *authChallenge, const char_t *username, uint8_t *authResponse)
598 {
599  uint_t i;
600  Sha1Context sha1Context;
601  uint8_t challenge[8];
602  uint8_t passwordHash[16];
603  uint8_t passwordHashHash[16];
604  uint8_t digest[SHA1_DIGEST_SIZE];
605 
606  //Magic1 constant
607  static const uint8_t magic1[39] =
608  {
609  0x4D, 0x61, 0x67, 0x69, 0x63, 0x20, 0x73, 0x65, 0x72, 0x76,
610  0x65, 0x72, 0x20, 0x74, 0x6F, 0x20, 0x63, 0x6C, 0x69, 0x65,
611  0x6E, 0x74, 0x20, 0x73, 0x69, 0x67, 0x6E, 0x69, 0x6E, 0x67,
612  0x20, 0x63, 0x6F, 0x6E, 0x73, 0x74, 0x61, 0x6E, 0x74
613  };
614 
615  //Magic2 constant
616  static const uint8_t magic2[41] =
617  {
618  0x50, 0x61, 0x64, 0x20, 0x74, 0x6F, 0x20, 0x6D, 0x61, 0x6B,
619  0x65, 0x20, 0x69, 0x74, 0x20, 0x64, 0x6F, 0x20, 0x6D, 0x6F,
620  0x72, 0x65, 0x20, 0x74, 0x68, 0x61, 0x6E, 0x20, 0x6F, 0x6E,
621  0x65, 0x20, 0x69, 0x74, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6F,
622  0x6E
623  };
624 
625  //Hex conversion table
626  static const char_t hexDigit[16] =
627  {
628  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
629  };
630 
631  //Hash the password with MD4
632  ikeEapMschapv2NtPasswordHash(password, passwordHash);
633 
634  //Use the MD4 algorithm to irreversibly hash PasswordHash into
635  //PasswordHashHash
636  ikeEapMschapv2HashNtPasswordHash(passwordHash, passwordHashHash);
637 
638  //Compute SHA1(PasswordHashHash || NT-Response || Magic1)
639  sha1Init(&sha1Context);
640  sha1Update(&sha1Context, passwordHashHash, 16);
641  sha1Update(&sha1Context, ntResponse, 24);
642  sha1Update(&sha1Context, magic1, 39);
643  sha1Final(&sha1Context, digest);
644  sha1Deinit(&sha1Context);
645 
646  //Compute ChallengeHash(PeerChallenge, AuthenticatorChallenge, UserName)
647  ikeEapMschapv2ChallengeHash(peerChallenge, authChallenge, username, challenge);
648 
649  //Compute SHA1(Digest || Challenge || Magic2)
650  sha1Init(&sha1Context);
651  sha1Update(&sha1Context, digest, 20);
652  sha1Update(&sha1Context, challenge, 8);
653  sha1Update(&sha1Context, magic2, 41);
654  sha1Final(&sha1Context, digest);
655  sha1Deinit(&sha1Context);
656 
657  //Encode the value of Digest as "S=" followed by the auth_string
658  authResponse[0] = 'S';
659  authResponse[1] = '=';
660 
661  //The auth_string quantity is a 20 octet number encoded in ASCII as 40
662  //hexadecimal digits. The hexadecimal digits A-F (if present) must be
663  //uppercase (refer to RFC 2759, section 5)
664  for(i = 0; i < SHA1_DIGEST_SIZE; i++)
665  {
666  //Convert upper nibble
667  authResponse[i * 2 + 2] = hexDigit[(digest[i] >> 4) & 0x0F];
668  //Then convert lower nibble
669  authResponse[i * 2 + 3] = hexDigit[digest[i] & 0x0F];
670  }
671 
672  //Erase working state
673  cryptoZeroizeMem(passwordHash, sizeof(passwordHash));
674  cryptoZeroizeMem(passwordHashHash, sizeof(passwordHashHash));
675  cryptoZeroizeMem(digest, sizeof(digest));
676 }
677 
678 
679 /**
680  * @brief CheckAuthenticatorResponse routine
681  * @param[in] password NULL-terminated string containing the password
682  * @param[out] ntResponse NT-Response field (24 bytes)
683  * @param[in] peerChallenge Peer challenge (16 bytes)
684  * @param[in] authChallenge Authenticator challenge (16 bytes)
685  * @param[in] username NULL-terminated string containing the user name
686  * @param[in] receivedResponse Received response (42 bytes)
687  * @return TRUE if the authenticator response is correct, else FASE
688  **/
689 
691  const uint8_t *ntResponse, const uint8_t *peerChallenge,
692  const uint8_t *authChallenge, const char_t *username,
693  const uint8_t *receivedResponse)
694 {
695  size_t i;
696  uint8_t mask;
697  uint8_t calcResponse[42];
698 
699  //Compute GenerateAuthenticatorResponse(Password, NtResponse, PeerChallenge,
700  //AuthenticatorChallenge, UserName)
702  authChallenge, username, calcResponse);
703 
704  //Verify the authenticator response
705  for(mask = 0, i = 0; i < sizeof(calcResponse); i++)
706  {
707  mask |= receivedResponse[i] ^ calcResponse[i];
708  }
709 
710  //Erase working state
711  cryptoZeroizeMem(calcResponse, sizeof(calcResponse));
712 
713  //Return TRUE if the authenticator response is correct
714  return (mask == 0) ? TRUE : FALSE;
715 }
716 
717 
718 /**
719  * @brief Generate master session key
720  * @param[in] password NULL-terminated string containing the password
721  * @param[in] ntResponse NT-Response field (24 bytes)
722  * @param[out] masterSessionKey Resulting master session key (64 bytes)
723  **/
724 
726  const uint8_t *ntResponse, uint8_t *masterSessionKey)
727 {
728  uint8_t passwordHash[16];
729  uint8_t masterKey[16];
730  uint8_t masterSendKey[16];
731  uint8_t masterReceiveKey[16];
732 
733  //The first step is to obfuscate the peer's password using NtPasswordHash()
734  //function (refer to RFC 3079, section 3.3)
735  ikeEapMschapv2NtPasswordHash(password, passwordHash);
736 
737  //The first 16 octets of the result are then hashed again using the MD4
738  //algorithm
739  ikeEapMschapv2HashNtPasswordHash(passwordHash, passwordHash);
740 
741  //The first 16 octets of this second hash are used together with the
742  //NT-Response field from the MS-CHAP-2 Response packet as the basis for
743  //the master session key
744  ikeEapMschapv2GetMasterKey(passwordHash, ntResponse, masterKey);
745 
746  //Once the master key has been generated, it is used to derive two 128-bit
747  //master session keys, one for sending and one for receiving
748  ikeEapMschapv2GetAsymmetricStartKey(masterKey, masterSendKey, 16, TRUE);
749  ikeEapMschapv2GetAsymmetricStartKey(masterKey, masterReceiveKey, 16, FALSE);
750 
751  //Compute MSK = MasterReceiveKey || MasterSendKey || 32 bytes zeroes (padding)
752  osMemcpy(masterSessionKey, masterReceiveKey, 16);
753  osMemcpy(masterSessionKey + 16, masterSendKey, 16);
754  osMemset(masterSessionKey + 32, 0, 32);
755 
756  //Erase working state
757  cryptoZeroizeMem(passwordHash, sizeof(passwordHash));
758  cryptoZeroizeMem(masterKey, sizeof(masterKey));
759  cryptoZeroizeMem(masterSendKey, sizeof(masterSendKey));
760  cryptoZeroizeMem(masterReceiveKey, sizeof(masterReceiveKey));
761 }
762 
763 
764 /**
765  * @brief GetMasterKey routine
766  * @param[in] passwordHashHash Hash value (16 bytes)
767  * @param[in] ntResponse NT-Response field (24 bytes)
768  * @param[out] masterKey Resulting master key (16 bytes)
769  **/
770 
771 void ikeEapMschapv2GetMasterKey(const uint8_t *passwordHashHash,
772  const uint8_t *ntResponse, uint8_t *masterKey)
773 {
774  Sha1Context sha1Context;
775  uint8_t digest[SHA1_DIGEST_SIZE];
776 
777  //Magic1 constant
778  static const uint8_t magic1[27] =
779  {
780  0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
781  0x65, 0x20, 0x4D, 0x50, 0x50, 0x45, 0x20, 0x4D, 0x61, 0x73,
782  0x74, 0x65, 0x72, 0x20, 0x4B, 0x65, 0x79
783  };
784 
785  //Compute SHA1(PasswordHashHash || NT-Response || Magic1)
786  sha1Init(&sha1Context);
787  sha1Update(&sha1Context, passwordHashHash, 16);
788  sha1Update(&sha1Context, ntResponse, 24);
789  sha1Update(&sha1Context, magic1, 27);
790  sha1Final(&sha1Context, digest);
791  sha1Deinit(&sha1Context);
792 
793  //Return the first 16 bytes of the SHA-1 digest
794  osMemcpy(masterKey, digest, 16);
795 
796  //Erase working state
797  cryptoZeroizeMem(digest, sizeof(digest));
798 }
799 
800 
801 /**
802  * @brief GetAsymmetricStartKey routine
803  * @param[in] masterKey Master key (16 bytes)
804  * @param[out] sessionKey Session key
805  * @param[in] sessionKeyLen Length of the session key, in bytes
806  * @param[in] isSend Send or receive direction
807  **/
808 
809 void ikeEapMschapv2GetAsymmetricStartKey(const uint8_t *masterKey,
810  uint8_t *sessionKey, size_t sessionKeyLen, bool_t isSend)
811 {
812  const uint8_t *s;
813  Sha1Context sha1Context;
814  uint8_t digest[SHA1_DIGEST_SIZE];
815 
816  //SHSpad1 constant
817  static const uint8_t shspad1[40] =
818  {
819  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
820  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
821  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
822  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
823  };
824 
825  //SHSpad2 constant
826  static const uint8_t shspad2[40] =
827  {
828  0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
829  0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
830  0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2,
831  0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2, 0xF2
832  };
833 
834  //Magic2 constant
835  static const uint8_t magic2[84] =
836  {
837  0x4F, 0x6E, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6C, 0x69,
838  0x65, 0x6E, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2C, 0x20,
839  0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
840  0x65, 0x20, 0x73, 0x65, 0x6E, 0x64, 0x20, 0x6B, 0x65, 0x79,
841  0x3B, 0x20, 0x6F, 0x6E, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
842  0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x64, 0x65,
843  0x2C, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
844  0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
845  0x6B, 0x65, 0x79, 0x2E
846  };
847 
848  //Magic3 constant
849  static const uint8_t magic3[84] =
850  {
851  0x4F, 0x6E, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6C, 0x69,
852  0x65, 0x6E, 0x74, 0x20, 0x73, 0x69, 0x64, 0x65, 0x2C, 0x20,
853  0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68,
854  0x65, 0x20, 0x72, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65, 0x20,
855  0x6B, 0x65, 0x79, 0x3B, 0x20, 0x6F, 0x6E, 0x20, 0x74, 0x68,
856  0x65, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x73,
857  0x69, 0x64, 0x65, 0x2C, 0x20, 0x69, 0x74, 0x20, 0x69, 0x73,
858  0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x65, 0x6E, 0x64, 0x20,
859  0x6B, 0x65, 0x79, 0x2E
860  };
861 
862  //Select the relevant magic constant
863  if(isSend)
864  {
865  s = magic3;
866  }
867  else
868  {
869  s = magic2;
870  }
871 
872  //Compute SHA1(MasterKey || SHSpad1 || s || SHSpad2)
873  sha1Init(&sha1Context);
874  sha1Update(&sha1Context, masterKey, 16);
875  sha1Update(&sha1Context, shspad1, 40);
876  sha1Update(&sha1Context, s, 84);
877  sha1Update(&sha1Context, shspad2, 40);
878  sha1Final(&sha1Context, digest);
879  sha1Deinit(&sha1Context);
880 
881  //Return the calculated session key
882  osMemcpy(sessionKey, digest, sessionKeyLen);
883 
884  //Erase working state
885  cryptoZeroizeMem(digest, sizeof(digest));
886 }
887 
888 #endif
#define htons(value)
Definition: cpu_endian.h:413
error_t ikeProcessEapMschapv2Request(IkeSaEntry *sa, const IkeEapMschapv2Packet *request, size_t length)
Process EAP MS-CHAP-V2 request.
int bool_t
Definition: compiler_port.h:63
bool_t ikeEapMschapv2CheckAuthResponse(const char_t *password, const uint8_t *ntResponse, const uint8_t *peerChallenge, const uint8_t *authChallenge, const char_t *username, const uint8_t *receivedResponse)
CheckAuthenticatorResponse routine.
@ IKE_EAP_MSCHAPV2_CODE_SUCCESS
Success.
void sha1Update(Sha1Context *context, const void *data, size_t length)
Update the SHA-1 context with a portion of the message being hashed.
void ikeEapMschapv2GetMasterKey(const uint8_t *passwordHashHash, const uint8_t *ntResponse, uint8_t *masterKey)
GetMasterKey routine.
void md4Deinit(Md4Context *context)
Release MD4 context.
Definition: md4.c:253
@ IKE_EAP_STATE_MSCHAPV2_CHALLENGE
Definition: ike.h:1496
void md4Update(Md4Context *context, const void *data, size_t length)
Update the MD4 context with a portion of the message being hashed.
Definition: md4.c:169
uint8_t p
Definition: ndp.h:300
@ IKE_EAP_CODE_RESPONSE
Response.
Definition: ike_eap.h:50
#define TRUE
Definition: os_port.h:50
EAP MS-CHAP-V2 authentication method.
void md4Init(Md4Context *context)
Initialize MD4 message digest context.
Definition: md4.c:147
void ikeEapMschapv2GenerateAuthResponse(const char_t *password, const uint8_t *ntResponse, const uint8_t *peerChallenge, const uint8_t *authChallenge, const char_t *username, uint8_t *authResponse)
GenerateAuthenticatorResponse routine.
uint8_t challenge[16]
IkeEapMschapv2FailureRequest
IkeEapMschapv2Challenge
#define osStrlen(s)
Definition: os_port.h:171
void desComputeKeyParity(const uint8_t *input, uint8_t *output)
Convert a 56-bit key to a 64-bit key (with odd parity)
Definition: des.c:447
@ ERROR_WRONG_STATE
Definition: error.h:211
@ IKE_EAP_STATE_MSCHAPV2_FAILURE
Definition: ike.h:1498
error_t ikeProcessEapMschapv2SuccessRequest(IkeSaEntry *sa, const IkeEapMschapv2SuccessRequest *request, size_t length)
Process EAP MS-CHAP-V2 Success Request packet.
#define IkeContext
Definition: ike.h:883
void ikeEapMschapv2GetAsymmetricStartKey(const uint8_t *masterKey, uint8_t *sessionKey, size_t sessionKeyLen, bool_t isSend)
GetAsymmetricStartKey routine.
IkeEapRequest
Definition: ike_eap.h:109
#define FALSE
Definition: os_port.h:46
DES algorithm context.
Definition: des.h:58
void sha1Init(Sha1Context *context)
Initialize SHA-1 message digest context.
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
EAP authentication.
error_t
Error codes.
Definition: error.h:43
void ikeEapMschapv2NtPasswordHash(const char_t *password, uint8_t *passwordHash)
NtPasswordHash routine.
void sha1Deinit(Sha1Context *context)
Release SHA-1 context.
Definition: sha1.c:269
error_t ikeFormatEapMschapv2FailureResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Failure Response packet.
void ikeEapMschapv2ChallengeResponse(const uint8_t *challenge, const uint8_t *passwordHash, uint8_t *response)
ChallengeResponse routine.
void ikeEapMschapv2HashNtPasswordHash(const uint8_t *passwordHash, uint8_t *passwordHashHash)
HashNtPasswordHash routine.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
__weak_func error_t desInit(DesContext *context, const uint8_t *key, size_t keyLen)
Initialize a DES context using the supplied key.
Definition: des.c:297
@ IKE_EAP_MSCHAPV2_CODE_CHALLENGE
Challenge.
void ikeEapMschapv2ChallengeHash(const uint8_t *peerChallenge, const uint8_t *authChallenge, const char_t *username, uint8_t *challenge)
ChallengeHash routine.
MD4 algorithm context.
Definition: md4.h:57
uint8_t mask
Definition: web_socket.h:319
@ ERROR_INVALID_TYPE
Definition: error.h:115
void ikeEapMschapv2GenerateMasterSessionKey(const char_t *password, const uint8_t *ntResponse, uint8_t *masterSessionKey)
Generate master session key.
uint8_t length
Definition: tcp.h:375
IkeEapMschapv2Response
@ IKE_EAP_METHOD_TYPE_MSCHAPV2
MS-CHAP-V2.
Definition: ike_eap.h:72
IKEv2 (Internet Key Exchange Protocol)
#define ntohs(value)
Definition: cpu_endian.h:421
uint8_t peerChallenge[16]
char char_t
Definition: compiler_port.h:55
#define SHA1_DIGEST_SIZE
Definition: sha1.h:45
#define IkeSaEntry
Definition: ike.h:887
uint8_t n
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
__weak_func void cryptoZeroizeMem(void *p, size_t n)
Securely erase a memory block.
Definition: crypto_mem.c:45
IkeEapMschapv2SuccessResponse
IkeEapMschapv2Packet
error_t ikeProcessEapMschapv2FailureRequest(IkeSaEntry *sa, const IkeEapMschapv2FailureRequest *request, size_t length)
Process EAP MS-CHAP-V2 Failure Request packet.
uint8_t ntResponse[24]
IkeEapMschapv2FailureResponse
@ IKE_EAP_MSCHAPV2_CODE_RESPONSE
Response.
__weak_func void desEncryptBlock(DesContext *context, const uint8_t *input, uint8_t *output)
Encrypt a 8-byte block using DES algorithm.
Definition: des.c:356
error_t ikeFormatEapMschapv2Response(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Response packet.
SHA-1 algorithm context.
Definition: sha1.h:62
uint8_t s
Definition: igmp_common.h:234
error_t ikeFormatEapMschapv2SuccessResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Success Response packet.
__weak_func void desDeinit(DesContext *context)
Release DES context.
Definition: des.c:434
void ikeEapMschapv2GenerateNtResponse(const uint8_t *authChallenge, const uint8_t *peerChallenge, const char_t *username, const char_t *password, uint8_t *response)
GenerateNTResponse routine.
@ IKE_EAP_STATE_IDENTITY
Definition: ike.h:1493
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:141
error_t ikeProcessEapMschapv2Challenge(IkeSaEntry *sa, const IkeEapMschapv2Challenge *challenge, size_t length)
Process EAP MS-CHAP-V2 Challenge packet.
void sha1Final(Sha1Context *context, uint8_t *digest)
Finish the SHA-1 message digest.
IkeEapMschapv2SuccessRequest
@ IKE_EAP_MSCHAPV2_CODE_FAILURE
Failure.
IkeEapResponse
Definition: ike_eap.h:123
void md4Final(Md4Context *context, uint8_t *digest)
Finish the MD4 message digest.
Definition: md4.c:208
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ IKE_EAP_STATE_MSCHAPV2_SUCCESS
Definition: ike.h:1497
@ IKE_EAP_STATE_INIT
Definition: ike.h:1492