1 | /* digest.c --- Generate a CRAM-MD5 hex encoded HMAC-MD5 response string. |
2 | * Copyright (C) 2002-2012 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, write to the Free |
18 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | * |
21 | */ |
22 | |
23 | #include <string.h> |
24 | |
25 | /* Get prototype. */ |
26 | #include "digest.h" |
27 | |
28 | /* Get gc_hmac_md5. */ |
29 | #include "gc.h" |
30 | |
31 | /* |
32 | * From draft-ietf-sasl-crammd5-02.txt: |
33 | * |
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. |
39 | * |
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 |
48 | * |
49 | */ |
50 | |
51 | #if CRAM_MD5_DIGEST_LEN != 2*GC_MD5_DIGEST_SIZE |
52 | #error MD5 length mismatch |
53 | #endif |
54 | |
55 | #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F)) |
56 | |
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]) |
62 | { |
63 | char hash[GC_MD5_DIGEST_SIZE]; |
64 | size_t i; |
65 | |
66 | gc_hmac_md5 (secret, secretlen ? secretlen : strlen (secret), |
67 | challenge, challengelen ? challengelen : strlen (challenge), |
68 | hash); |
69 | |
70 | for (i = 0; i < GC_MD5_DIGEST_SIZE; i++) |
71 | { |
72 | *response++ = HEXCHAR (hash[i] >> 4); |
73 | *response++ = HEXCHAR (hash[i]); |
74 | } |
75 | } |
76 | |