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.
28 /* Get gc_hmac_md5. */
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
45 * digest = 32(DIGIT / %x61-66)
46 * ; A hexadecimal string using only lower-case
51 #if CRAM_MD5_DIGEST_LEN != 2*GC_MD5_DIGEST_SIZE
52 # error MD5 length mismatch
55 #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F))
58 cram_md5_digest (const char *challenge
,
61 size_t secretlen
, char response
[CRAM_MD5_DIGEST_LEN
])
63 char hash
[GC_MD5_DIGEST_SIZE
];
66 gc_hmac_md5 (secret
, secretlen
? secretlen
: strlen (secret
),
67 challenge
, challengelen
? challengelen
: strlen (challenge
),
70 for (i
= 0; i
< GC_MD5_DIGEST_SIZE
; i
++)
72 *response
++ = HEXCHAR (hash
[i
] >> 4);
73 *response
++ = HEXCHAR (hash
[i
]);