digest.c

Go to the documentation of this file.
00001 /* digest.c --- Generate a CRAM-MD5 hex encoded HMAC-MD5 response string.
00002  * Copyright (C) 2002, 2003, 2004, 2005  Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include <string.h>
00024 
00025 /* Get prototype. */
00026 #include "digest.h"
00027 
00028 /* Get gc_hmac_md5. */
00029 #include "gc.h"
00030 
00031 /*
00032  * From draft-ietf-sasl-crammd5-02.txt:
00033  *
00034  *   The latter is computed by applying the keyed MD5 algorithm from
00035  *   [KEYED-MD5] where the key is a shared secret and the digested
00036  *   text is the challenge (including angle-brackets). The client
00037  *   MUST NOT interpret or attempt to validate the contents of the
00038  *   challenge in any way.
00039  *
00040  *   This shared secret is a string known only to the client and
00041  *   server.  The "digest" parameter itself is a 16-octet value which
00042  *   is sent in hexadecimal format, using lower-case US-ASCII
00043  *   characters.
00044  * ...
00045  *   digest     = 32(DIGIT / %x61-66)
00046  *   ; A hexadecimal string using only lower-case
00047  *   ; letters
00048  *
00049  */
00050 
00051 #if CRAM_MD5_DIGEST_LEN != 2*GC_MD5_DIGEST_SIZE
00052 # error MD5 length mismatch
00053 #endif
00054 
00055 #define HEXCHAR(c) ((c & 0x0F) > 9 ? 'a' + (c & 0x0F) - 10 : '0' + (c & 0x0F))
00056 
00057 void
00058 cram_md5_digest (const char *challenge,
00059                  size_t challengelen,
00060                  const char *secret,
00061                  size_t secretlen, char response[CRAM_MD5_DIGEST_LEN])
00062 {
00063   char hash[GC_MD5_DIGEST_SIZE];
00064   size_t i;
00065 
00066   gc_hmac_md5 (secret, secretlen ? secretlen : strlen (secret),
00067                challenge, challengelen ? challengelen : strlen (challenge),
00068                hash);
00069 
00070   for (i = 0; i < GC_MD5_DIGEST_SIZE; i++)
00071     {
00072       *response++ = HEXCHAR (hash[i] >> 4);
00073       *response++ = HEXCHAR (hash[i]);
00074     }
00075 }

Generated on Tue Aug 22 12:06:06 2006 for gsasl by  doxygen 1.4.7