~ubuntu-branches/ubuntu/trusty/libesmtp/trusty-proposed

« back to all changes in this revision

Viewing changes to crammd5/hmacmd5.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
3
 *  formatted electronic mail messages using the SMTP protocol described
 
4
 *  in RFC 2821.
 
5
 *
 
6
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 
7
 *
 
8
 *  This library is free software; you can redistribute it and/or
 
9
 *  modify it under the terms of the GNU Lesser General Public
 
10
 *  License as published by the Free Software Foundation; either
 
11
 *  version 2.1 of the License, or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 *  Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public
 
19
 *  License along with this library; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
 */
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <string.h>
 
27
#include <sys/types.h>
 
28
#include "hmacmd5.h"
 
29
 
 
30
#define PAD_SIZE        64
 
31
 
 
32
/*
 
33
 * the HMAC_MD5 transform looks like:
 
34
 *
 
35
 * MD5(K XOR opad, MD5(K XOR ipad, challenge))
 
36
 *
 
37
 * where K is an n byte secret
 
38
 * ipad is the byte 0x36 repeated 64 times
 
39
 * opad is the byte 0x5c repeated 64 times
 
40
 * and challenge is the data being protected
 
41
 */
 
42
 
 
43
/* Precompute HMAC-MD5 contexts from a secret
 
44
 */
 
45
void
 
46
hmac_md5_pre (const void *secret, size_t secret_len,
 
47
              MD5_CTX *inner, MD5_CTX *outer)
 
48
{
 
49
  unsigned char ipad[PAD_SIZE];
 
50
  unsigned char opad[PAD_SIZE];
 
51
  unsigned char tk[16];
 
52
  int i;
 
53
 
 
54
  /* If secret is longer than 64 bytes reset it to secret = MD5 (secret)
 
55
   */
 
56
  if (secret_len > PAD_SIZE)
 
57
    {
 
58
      MD5_CTX tctx;
 
59
 
 
60
      MD5_Init (&tctx);
 
61
      MD5_Update (&tctx, secret, secret_len);
 
62
      MD5_Final (tk, &tctx);
 
63
      secret = tk;
 
64
      secret_len = sizeof tk;
 
65
    }
 
66
 
 
67
  /* start out by storing secret in pads */
 
68
  memcpy (ipad, secret, secret_len);
 
69
  if (secret_len < sizeof ipad)
 
70
    memset (ipad + secret_len, 0, sizeof ipad - secret_len);
 
71
  memcpy (opad, secret, secret_len);
 
72
  if (secret_len < sizeof opad)
 
73
    memset (opad + secret_len, 0, sizeof opad - secret_len);
 
74
 
 
75
  /* XOR secret with ipad and opad values */
 
76
  for (i = 0; i < PAD_SIZE; i++)
 
77
    {
 
78
      ipad[i] ^= 0x36;
 
79
      opad[i] ^= 0x5c;
 
80
    }
 
81
 
 
82
  /* perform inner MD5 */
 
83
  MD5_Init (inner);
 
84
  MD5_Update (inner, ipad, sizeof ipad);
 
85
 
 
86
  /* perform outer MD5 */
 
87
  MD5_Init (outer);
 
88
  MD5_Update (outer, opad, sizeof opad);
 
89
}
 
90
 
 
91
/* Finalise HMAC-MD5 contexts from a challenge
 
92
 */
 
93
void
 
94
hmac_md5_post (const void *challenge, size_t challenge_len,
 
95
               MD5_CTX *inner, MD5_CTX *outer, unsigned char digest[16])
 
96
{
 
97
  unsigned char id[16];
 
98
 
 
99
  /* perform inner MD5 */
 
100
  MD5_Update (inner, challenge, challenge_len);
 
101
  MD5_Final (id, inner);
 
102
 
 
103
  /* perform outer MD5 */
 
104
  MD5_Update (outer, id, sizeof id);
 
105
  MD5_Final (digest, outer);
 
106
}
 
107
 
 
108
/* Digest a challenge and a secret.
 
109
 */
 
110
void
 
111
hmac_md5 (const void *challenge, size_t challenge_len,
 
112
          const void *secret, size_t secret_len,
 
113
          unsigned char digest[16])
 
114
{
 
115
  MD5_CTX inner, outer;
 
116
 
 
117
  hmac_md5_pre (secret, secret_len, &inner, &outer);
 
118
  hmac_md5_post (challenge, challenge_len, &inner, &outer, digest);
 
119
}
 
120