Bump versions.
[gsasl.git] / lib / cram-md5 / digest.c
blob48feca4d4152aa8df8d8b300e1d429130fcf6961
1 /* digest.c --- Generate a CRAM-MD5 hex encoded HMAC-MD5 response string.
2 * Copyright (C) 2002, 2003, 2004, 2005 Simon Josefsson
4 * This file is part of GNU SASL Library.
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU SASL Library; if not, write to the Free
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include <string.h>
25 /* Get prototype. */
26 #include "digest.h"
28 /* Get gc_hmac_md5. */
29 #include "gc.h"
32 * From draft-ietf-sasl-crammd5-02.txt:
34 * The latter is computed by applying the keyed MD5 algorithm from
35 * [KEYED-MD5] where the key is a shared secret and the digested
36 * text is the challenge (including angle-brackets). The client
37 * MUST NOT interpret or attempt to validate the contents of the
38 * challenge in any way.
40 * This shared secret is a string known only to the client and
41 * server. The "digest" parameter itself is a 16-octet value which
42 * is sent in hexadecimal format, using lower-case US-ASCII
43 * characters.
44 * ...
45 * digest = 32(DIGIT / %x61-66)
46 * ; A hexadecimal string using only lower-case
47 * ; letters
51 #if CRAM_MD5_DIGEST_LEN != 2*GC_MD5_DIGEST_SIZE
52 # error MD5 length mismatch
53 #endif
55 #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F))
57 void
58 cram_md5_digest (const char *challenge,
59 size_t challengelen,
60 const char *secret,
61 size_t secretlen, char response[CRAM_MD5_DIGEST_LEN])
63 char hash[GC_MD5_DIGEST_SIZE];
64 size_t i;
66 gc_hmac_md5 (secret, secretlen ? secretlen : strlen (secret),
67 challenge, challengelen ? challengelen : strlen (challenge),
68 hash);
70 for (i = 0; i < GC_MD5_DIGEST_SIZE; i++)
72 *response++ = HEXCHAR (hash[i] >> 4);
73 *response++ = HEXCHAR (hash[i]);