1/***********************************************************************
2*
3* auth.c
4*
5* Code for doing CHAP-style authentication
6*
7* Copyright (C) 2002 by Roaring Penguin Software Inc.
8*
9* This software may be distributed under the terms of the GNU General
10* Public License, Version 2, or (at your option) any later version.
11*
12* LIC: GPL
13*
14***********************************************************************/
15
16static char const RCSID[] =
17"$Id: auth.c 3323 2011-09-21 18:45:48Z lly.dev $";
18
19#include "l2tp.h"
20#include "md5.h"
21#include <string.h>
22
23/**********************************************************************
24* %FUNCTION: auth_gen_response
25* %ARGUMENTS:
26*  msg_type -- message type
27*  secret -- secret to use
28*  challenge -- challenge received from peer
29*  chal_len -- length of challenge
30*  buf -- buffer in which to place 16-byte response
31* %RETURNS:
32*  Nothing
33* %DESCRIPTION:
34*  Computes a response for the challenge using "secret"
35***********************************************************************/
36void
37l2tp_auth_gen_response(uint16_t msg_type,
38		  char const *secret,
39		  unsigned char const *challenge,
40		  size_t chal_len,
41		  unsigned char buf[16])
42{
43    struct MD5Context ctx;
44    unsigned char id = (unsigned char) msg_type;
45
46    MD5Init(&ctx);
47    MD5Update(&ctx, &id, 1);
48    MD5Update(&ctx, (unsigned char *) secret, strlen(secret));
49    MD5Update(&ctx, challenge, chal_len);
50    MD5Final(buf, &ctx);
51    DBG(l2tp_db(DBG_AUTH, "auth_gen_response(secret=%s) -> %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x\n", secret,
52	   buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
53	   buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]));
54}
55