gsasl  2.2.2
digest.c
Go to the documentation of this file.
1 /* digest.c --- Generate a CRAM-MD5 hex encoded HMAC-MD5 response string.
2  * Copyright (C) 2002-2025 Simon Josefsson
3  *
4  * This file is part of GNU SASL Library.
5  *
6  * GNU SASL Library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GNU SASL Library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with GNU SASL Library; if not, see
18  * <https://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <config.h>
23 
24 #include <string.h>
25 
26 /* Get prototype. */
27 #include "digest.h"
28 
29 /* Get gc_hmac_md5. */
30 #include "gc.h"
31 
32 /*
33  * From draft-ietf-sasl-crammd5-02.txt:
34  *
35  * The latter is computed by applying the keyed MD5 algorithm from
36  * [KEYED-MD5] where the key is a shared secret and the digested
37  * text is the challenge (including angle-brackets). The client
38  * MUST NOT interpret or attempt to validate the contents of the
39  * challenge in any way.
40  *
41  * This shared secret is a string known only to the client and
42  * server. The "digest" parameter itself is a 16-octet value which
43  * is sent in hexadecimal format, using lower-case US-ASCII
44  * characters.
45  * ...
46  * digest = 32(DIGIT / %x61-66)
47  * ; A hexadecimal string using only lower-case
48  * ; letters
49  *
50  */
51 
52 #if CRAM_MD5_DIGEST_LEN != 2*GC_MD5_DIGEST_SIZE
53 # error MD5 length mismatch
54 #endif
55 
56 #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F))
57 
58 void
59 cram_md5_digest (const char *challenge,
60  size_t challengelen,
61  const char *secret,
62  size_t secretlen, char response[CRAM_MD5_DIGEST_LEN])
63 {
64  char hash[GC_MD5_DIGEST_SIZE];
65  size_t i;
66 
67  gc_hmac_md5 (secret, secretlen ? secretlen : strlen (secret),
68  challenge, challengelen ? challengelen : strlen (challenge),
69  hash);
70 
71  for (i = 0; i < GC_MD5_DIGEST_SIZE; i++)
72  {
73  *response++ = HEXCHAR (hash[i] >> 4);
74  *response++ = HEXCHAR (hash[i]);
75  }
76 }
#define HEXCHAR(c)
Definition: digest.c:56
void cram_md5_digest(const char *challenge, size_t challengelen, const char *secret, size_t secretlen, char response[CRAM_MD5_DIGEST_LEN])
Definition: digest.c:59
#define CRAM_MD5_DIGEST_LEN
Definition: digest.h:28