annotate src/c-client/auth_md5.c @ 4:d741b3ecc917 draft

imapext-2007f
author HIROSE Yuuji <yuuji@gentei.org>
date Thu, 30 Oct 2014 00:03:05 +0900
parents 2366b362676d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
1 /* ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
2 * Copyright 1988-2007 University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
3 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
6 * You may obtain a copy of the License at
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
7 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
9 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
10 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
11 * ========================================================================
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
12 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
13
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
14 /*
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
15 * Program: CRAM-MD5 authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
16 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
17 * Author: Mark Crispin
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
18 * Networks and Distributed Computing
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
19 * Computing & Communications
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
20 * University of Washington
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
21 * Administration Building, AG-44
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
22 * Seattle, WA 98195
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
23 * Internet: MRC@CAC.Washington.EDU
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
24 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
25 * Date: 21 October 1998
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
26 * Last Edited: 30 January 2007
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
27 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
28
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
29 /* MD5 context */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
30
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
31 #define MD5BLKLEN 64 /* MD5 block length */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
32 #define MD5DIGLEN 16 /* MD5 digest length */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
33
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
34 typedef struct {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
35 unsigned long chigh; /* high 32bits of byte count */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
36 unsigned long clow; /* low 32bits of byte count */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
37 unsigned long state[4]; /* state (ABCD) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
38 unsigned char buf[MD5BLKLEN]; /* input buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
39 unsigned char *ptr; /* buffer position */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
40 } MD5CONTEXT;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
41
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
42
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
43 /* Prototypes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
44
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
45 long auth_md5_valid (void);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
46 long auth_md5_client (authchallenge_t challenger,authrespond_t responder,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
47 char *service,NETMBX *mb,void *stream,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
48 unsigned long *trial,char *user);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
49 char *auth_md5_server (authresponse_t responder,int argc,char *argv[]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
50 char *auth_md5_pwd (char *user);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
51 char *apop_login (char *chal,char *user,char *md5,int argc,char *argv[]);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
52 char *hmac_md5 (char *text,unsigned long tl,char *key,unsigned long kl);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
53 void md5_init (MD5CONTEXT *ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
54 void md5_update (MD5CONTEXT *ctx,unsigned char *data,unsigned long len);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
55 void md5_final (unsigned char *digest,MD5CONTEXT *ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
56 static void md5_transform (unsigned long *state,unsigned char *block);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
57 static void md5_encode (unsigned char *dst,unsigned long *src,int len);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
58 static void md5_decode (unsigned long *dst,unsigned char *src,int len);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
59
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
60
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
61 /* Authenticator linkage */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
62
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
63 AUTHENTICATOR auth_md5 = {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
64 AU_SECURE, /* secure authenticator */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
65 "CRAM-MD5", /* authenticator name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
66 auth_md5_valid, /* check if valid */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
67 auth_md5_client, /* client method */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
68 auth_md5_server, /* server method */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
69 NIL /* next authenticator */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
70 };
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
71
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
72 /* Check if CRAM-MD5 valid on this system
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
73 * Returns: T, always
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
74 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
75
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
76 long auth_md5_valid (void)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
77 {
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
78 #ifndef QMAIL /* qmail extension can always handle MD5 */
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
79 struct stat sbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
80 /* server forbids MD5 if no MD5 enable file */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
81 if (stat (MD5ENABLE,&sbuf)) auth_md5.server = NIL;
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
82 #endif
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
83 return T; /* MD5 is otherwise valid */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
84 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
85
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
86
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
87 /* Client authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
88 * Accepts: challenger function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
89 * responder function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
90 * SASL service name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
91 * parsed network mailbox structure
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
92 * stream argument for functions
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
93 * pointer to current trial count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
94 * returned user name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
95 * Returns: T if success, NIL otherwise, number of trials incremented if retry
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
96 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
97
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
98 long auth_md5_client (authchallenge_t challenger,authrespond_t responder,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
99 char *service,NETMBX *mb,void *stream,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
100 unsigned long *trial,char *user)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
101 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
102 char pwd[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
103 void *challenge;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
104 unsigned long clen;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
105 long ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
106 /* get challenge */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
107 if (challenge = (*challenger) (stream,&clen)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
108 pwd[0] = NIL; /* prompt user */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
109 mm_login (mb,user,pwd,*trial);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
110 if (!pwd[0]) { /* user requested abort */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
111 fs_give ((void **) &challenge);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
112 (*responder) (stream,NIL,0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
113 *trial = 0; /* cancel subsequent attempts */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
114 ret = LONGT; /* will get a BAD response back */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
115 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
116 else { /* got password, build response */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
117 sprintf (pwd,"%.65s %.33s",user,hmac_md5 (challenge,clen,
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
118 pwd,strlen (pwd)));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
119 fs_give ((void **) &challenge);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
120 /* send credentials, allow retry if OK */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
121 if ((*responder) (stream,pwd,strlen (pwd))) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
122 if (challenge = (*challenger) (stream,&clen))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
123 fs_give ((void **) &challenge);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
124 else {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
125 ++*trial; /* can try again if necessary */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
126 ret = LONGT; /* check the authentication */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
127 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
128 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
129 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
130 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
131 memset (pwd,0,MAILTMPLEN); /* erase password in case not overwritten */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
132 if (!ret) *trial = 65535; /* don't retry if bad protocol */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
133 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
134 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
135
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
136 /* Server authenticator
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
137 * Accepts: responder function
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
138 * argument count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
139 * argument vector
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
140 * Returns: authenticated user name or NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
141 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
142 * This is much hairier than it needs to be due to the necessary of zapping
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
143 * the password data.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
144 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
145
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
146 static int md5try = MAXLOGINTRIALS;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
147
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
148 char *auth_md5_server (authresponse_t responder,int argc,char *argv[])
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
149 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
150 char *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
151 char *p,*u,*user,*authuser,*hash,chal[MAILTMPLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
152 unsigned long cl,pl;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
153 /* generate challenge */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
154 sprintf (chal,"<%lu.%lu@%s>",(unsigned long) getpid (),
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
155 (unsigned long) time (0),mylocalhost ());
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
156 /* send challenge, get user and hash */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
157 if (user = (*responder) (chal,cl = strlen (chal),NIL)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
158 /* got user, locate hash */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
159 if (hash = strrchr (user,' ')) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
160 *hash++ = '\0'; /* tie off user */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
161 /* see if authentication user */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
162 if (authuser = strchr (user,'*')) *authuser++ = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
163 /* get password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
164 if (p = auth_md5_pwd ((authuser && *authuser) ? authuser : user)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
165 pl = strlen (p);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
166 u = (md5try && !strcmp (hash,hmac_md5 (chal,cl,p,pl))) ? user : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
167 memset (p,0,pl); /* erase sensitive information */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
168 fs_give ((void **) &p); /* flush erased password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
169 /* now log in for real */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
170 if (u && authserver_login (u,authuser,argc,argv)) ret = myusername ();
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
171 else if (md5try) --md5try;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
172 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
173 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
174 fs_give ((void **) &user);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
175 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
176 if (!ret) sleep (3); /* slow down possible cracker */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
177 return ret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
178 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
179
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
180 /* Return MD5 password for user
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
181 * Accepts: user name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
182 * Returns: plaintext password if success, else NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
183 *
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
184 * This is much hairier than it needs to be due to the necessary of zapping
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
185 * the password data. That's why we don't use stdio here.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
186 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
187
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
188 char *auth_md5_pwd (char *user)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
189 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
190 struct stat sbuf;
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
191 #ifndef QMAIL
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
192 int fd = open (MD5ENABLE,O_RDONLY,NIL);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
193 unsigned char *s,*t,*buf,*lusr,*lret;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
194 char *r;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
195 char *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
196 if (fd >= 0) { /* found the file? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
197 fstat (fd,&sbuf); /* yes, slurp it into memory */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
198 read (fd,buf = (char *) fs_get (sbuf.st_size + 1),sbuf.st_size);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
199 /* see if any uppercase characters in user */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
200 for (s = user; *s && ((*s < 'A') || (*s > 'Z')); s++);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
201 /* yes, make lowercase copy */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
202 lusr = *s ? lcase (cpystr (user)) : NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
203 for (s = strtok_r ((char *) buf,"\015\012",&r),lret = NIL; s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
204 s = ret ? NIL : strtok_r (NIL,"\015\012",&r))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
205 /* must be valid entry line */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
206 if (*s && (*s != '#') && (t = strchr (s,'\t')) && t[1]) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
207 *t++ = '\0'; /* found tab, tie off user, point to pwd */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
208 if (!strcmp (s,user)) ret = cpystr (t);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
209 else if (lusr && !lret) if (!strcmp (s,lusr)) lret = t;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
210 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
211 /* accept case-independent name */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
212 if (!ret && lret) ret = cpystr (lret);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
213 /* don't need lowercase copy any more */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
214 if (lusr) fs_give ((void **) &lusr);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
215 /* erase sensitive information from buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
216 memset (buf,0,sbuf.st_size + 1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
217 fs_give ((void **) &buf); /* flush the buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
218 close (fd); /* don't need file any longer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
219 }
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
220 #else
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
221 # ifndef USERAPOPFILE
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
222 # define USERAPOPFILE ".apop"
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
223 # endif
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
224 # ifndef XADDR_DELIM
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
225 # ifdef POSTFIX /* Same if-condition is in maildir.c.. sorry */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
226 # define XADDR_DELIM "+"
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
227 # else
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
228 # define XADDR_DELIM "-"
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
229 # endif
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
230 # endif
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
231 extern char *myMailSuffix;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
232 char mycrypt[BUFSIZ+1];
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
233 char *ret = NIL, *tmp=NIL;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
234 FILE *apopfile = NIL;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
235 struct passwd *pw = getpwnam(user);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
236 if (!pw) pw = getpwnam(lcase(tmp=cpystr(user)));
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
237 if (!pw && strstr(user, XADDR_DELIM)) { /* Check qmail alias */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
238 char tmp[BUFSIZ];
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
239 char *s = user;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
240 memset(tmp, 0, BUFSIZ);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
241 while (*s && s && !pw) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
242 s = strstr(s, XADDR_DELIM);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
243 if (!s) break;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
244 strncpy(tmp, user, s-user);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
245 s++;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
246 pw = getpwnam(tmp);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
247 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
248 if (pw) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
249 myMailSuffix = lcase(cpystr(s-1));
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
250 user[strlen(tmp)] = '\0'; /* zap suffix */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
251 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
252 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
253 if (pw) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
254 snprintf(mycrypt, BUFSIZ, "%s/%s", pw->pw_dir, USERAPOPFILE);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
255 if (NIL != myMailSuffix) strcat(mycrypt, myMailSuffix);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
256 if (stat(mycrypt, &sbuf) < 0) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
257 /* printf("Cannot stat [%s]\015\n", mycrypt); */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
258 return ret;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
259 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
260 if (sbuf.st_mode & ~(S_IFREG | S_IREAD | S_IWRITE)) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
261 /* printf("Your apoppassword file is readable by others. Do chmod 600 %s%c\n", mycrypt, 015); */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
262 return ret;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
263 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
264 #ifdef APOPOPEN
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
265 if (stat(APOPOPEN, &sbuf) >= 0) { /* if APPOPEN exists */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
266 /* read apop password via APOPOPEN program */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
267 int bytes = 3+strlen(APOPOPEN)+strlen(mycrypt);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
268 char *aproc = (char*) fs_get(bytes);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
269 snprintf(aproc, bytes, "%s %s%c", APOPOPEN, mycrypt, '\0');
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
270 apopfile = popen(aproc, "r");
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
271 memset(aproc, 0, bytes);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
272 fs_give((void**)&aproc);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
273 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
274 #endif /* APOPOPEN */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
275 if ((NIL!=apopfile) || (apopfile = fopen( mycrypt, "r" ))) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
276 fgets(mycrypt, sizeof(mycrypt) - 1, apopfile);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
277 fclose(apopfile);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
278 if (mycrypt[0]) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
279 char p = strlen(mycrypt);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
280 while (p > 0 && (mycrypt[p-1] == '\n' || mycrypt[p-1] == '\r')) {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
281 mycrypt[--p] = '\0'; /* zap trailing newlines */
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
282 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
283 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
284 } else {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
285 return ret;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
286 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
287 ret = cpystr(mycrypt);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
288 memset(mycrypt, 0, sizeof(mycrypt));
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
289 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
290 #endif /* if QMAIL */
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
291 return ret; /* return password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
292 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
293
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
294 /* APOP server login
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
295 * Accepts: challenge
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
296 * desired user name
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
297 * purported MD5
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
298 * argument count
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
299 * argument vector
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
300 * Returns: authenticated user name or NIL
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
301 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
302
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
303 char *apop_login (char *chal,char *user,char *md5,int argc,char *argv[])
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
304 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
305 int i,j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
306 char *ret = NIL;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
307 char *s,*authuser,tmp[MAILTMPLEN];
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
308 #ifdef QMAIL
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
309 char *userback = cpystr(user);
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
310 #endif
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
311 unsigned char digest[MD5DIGLEN];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
312 MD5CONTEXT ctx;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
313 char *hex = "0123456789abcdef";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
314 /* see if authentication user */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
315 if (authuser = strchr (user,'*')) *authuser++ = '\0';
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
316 /* get password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
317 if (s = auth_md5_pwd ((authuser && *authuser) ? authuser : user)) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
318 md5_init (&ctx); /* initialize MD5 context */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
319 /* build string to get MD5 digest */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
320 sprintf (tmp,"%.128s%.128s",chal,s);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
321 memset (s,0,strlen (s)); /* erase sensitive information */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
322 fs_give ((void **) &s); /* flush erased password */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
323 md5_update (&ctx,(unsigned char *) tmp,strlen (tmp));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
324 memset (tmp,0,MAILTMPLEN); /* erase sensitive information */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
325 md5_final (digest,&ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
326 /* convert to printable hex */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
327 for (i = 0, s = tmp; i < MD5DIGLEN; i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
328 *s++ = hex[(j = digest[i]) >> 4];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
329 *s++ = hex[j & 0xf];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
330 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
331 *s = '\0'; /* tie off hash text */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
332 memset (digest,0,MD5DIGLEN);/* erase sensitive information */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
333 if (md5try && !strcmp (md5,tmp) &&
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
334 authserver_login (user,authuser,argc,argv))
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
335 ret = cpystr (myusername ());
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
336 else if (md5try) --md5try;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
337 memset (tmp,0,MAILTMPLEN); /* erase sensitive information */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
338 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
339 if (!ret) sleep (3); /* slow down possible cracker */
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
340 else {
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
341 extern void permitsmtp();
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
342 /* permitsmtp();*/
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
343 }
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
344 #ifdef QMAIL
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
345 return (ret) ? userback : ret;
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
346 #else
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
347 return ret;
4
d741b3ecc917 imapext-2007f
HIROSE Yuuji <yuuji@gentei.org>
parents: 3
diff changeset
348 #endif
0
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
349 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
350
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
351 /*
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
352 * RFC 2104 HMAC hashing
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
353 * Accepts: text to hash
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
354 * text length
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
355 * key
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
356 * key length
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
357 * Returns: hash as text, always
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
358 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
359
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
360 char *hmac_md5 (char *text,unsigned long tl,char *key,unsigned long kl)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
361 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
362 int i,j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
363 static char hshbuf[2*MD5DIGLEN + 1];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
364 char *s;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
365 MD5CONTEXT ctx;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
366 char *hex = "0123456789abcdef";
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
367 unsigned char digest[MD5DIGLEN],k_ipad[MD5BLKLEN+1],k_opad[MD5BLKLEN+1];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
368 if (kl > MD5BLKLEN) { /* key longer than pad length? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
369 md5_init (&ctx); /* yes, set key as MD5(key) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
370 md5_update (&ctx,(unsigned char *) key,kl);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
371 md5_final (digest,&ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
372 key = (char *) digest;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
373 kl = MD5DIGLEN;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
374 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
375 memcpy (k_ipad,key,kl); /* store key in pads */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
376 memset (k_ipad+kl,0,(MD5BLKLEN+1)-kl);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
377 memcpy (k_opad,k_ipad,MD5BLKLEN+1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
378 /* XOR key with ipad and opad values */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
379 for (i = 0; i < MD5BLKLEN; i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
380 k_ipad[i] ^= 0x36;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
381 k_opad[i] ^= 0x5c;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
382 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
383 md5_init (&ctx); /* inner MD5: hash ipad and text */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
384 md5_update (&ctx,k_ipad,MD5BLKLEN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
385 md5_update (&ctx,(unsigned char *) text,tl);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
386 md5_final (digest,&ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
387 md5_init (&ctx); /* outer MD5: hash opad and inner results */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
388 md5_update (&ctx,k_opad,MD5BLKLEN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
389 md5_update (&ctx,digest,MD5DIGLEN);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
390 md5_final (digest,&ctx);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
391 /* convert to printable hex */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
392 for (i = 0, s = hshbuf; i < MD5DIGLEN; i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
393 *s++ = hex[(j = digest[i]) >> 4];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
394 *s++ = hex[j & 0xf];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
395 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
396 *s = '\0'; /* tie off hash text */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
397 return hshbuf;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
398 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
399
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
400 /* Everything after this point is derived from the RSA Data Security, Inc.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
401 * MD5 Message-Digest Algorithm
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
402 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
403
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
404 /* You may wonder why these strange "a &= 0xffffffff;" statements are here.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
405 * This is to ensure correct results on machines with a unsigned long size of
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
406 * larger than 32 bits.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
407 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
408
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
409 #define RND1(a,b,c,d,x,s,ac) \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
410 a += ((b & c) | (d & ~b)) + x + (unsigned long) ac; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
411 a &= 0xffffffff; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
412 a = b + ((a << s) | (a >> (32 - s)));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
413
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
414 #define RND2(a,b,c,d,x,s,ac) \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
415 a += ((b & d) | (c & ~d)) + x + (unsigned long) ac; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
416 a &= 0xffffffff; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
417 a = b + ((a << s) | (a >> (32 - s)));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
418
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
419 #define RND3(a,b,c,d,x,s,ac) \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
420 a += (b ^ c ^ d) + x + (unsigned long) ac; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
421 a &= 0xffffffff; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
422 a = b + ((a << s) | (a >> (32 - s)));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
423
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
424 #define RND4(a,b,c,d,x,s,ac) \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
425 a += (c ^ (b | ~d)) + x + (unsigned long) ac; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
426 a &= 0xffffffff; \
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
427 a = b + ((a << s) | (a >> (32 - s)));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
428
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
429 /* Initialize MD5 context
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
430 * Accepts: context to initialize
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
431 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
432
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
433 void md5_init (MD5CONTEXT *ctx)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
434 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
435 ctx->clow = ctx->chigh = 0; /* initialize byte count to zero */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
436 /* initialization constants */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
437 ctx->state[0] = 0x67452301; ctx->state[1] = 0xefcdab89;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
438 ctx->state[2] = 0x98badcfe; ctx->state[3] = 0x10325476;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
439 ctx->ptr = ctx->buf; /* reset buffer pointer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
440 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
441
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
442
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
443 /* MD5 add data to context
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
444 * Accepts: context
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
445 * input data
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
446 * length of data
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
447 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
448
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
449 void md5_update (MD5CONTEXT *ctx,unsigned char *data,unsigned long len)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
450 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
451 unsigned long i = (ctx->buf + MD5BLKLEN) - ctx->ptr;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
452 /* update double precision number of bytes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
453 if ((ctx->clow += len) < len) ctx->chigh++;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
454 while (i <= len) { /* copy/transform data, 64 bytes at a time */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
455 memcpy (ctx->ptr,data,i); /* fill up 64 byte chunk */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
456 md5_transform (ctx->state,ctx->ptr = ctx->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
457 data += i,len -= i,i = MD5BLKLEN;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
458 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
459 memcpy (ctx->ptr,data,len); /* copy final bit of data in buffer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
460 ctx->ptr += len; /* update buffer pointer */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
461 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
462
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
463 /* MD5 Finalization
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
464 * Accepts: destination digest
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
465 * context
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
466 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
467
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
468 void md5_final (unsigned char *digest,MD5CONTEXT *ctx)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
469 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
470 unsigned long i,bits[2];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
471 bits[0] = ctx->clow << 3; /* calculate length in bits (before padding) */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
472 bits[1] = (ctx->chigh << 3) + (ctx->clow >> 29);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
473 *ctx->ptr++ = 0x80; /* padding byte */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
474 if ((i = (ctx->buf + MD5BLKLEN) - ctx->ptr) < 8) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
475 memset (ctx->ptr,0,i); /* pad out buffer with zeros */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
476 md5_transform (ctx->state,ctx->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
477 /* pad out with zeros, leaving 8 bytes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
478 memset (ctx->buf,0,MD5BLKLEN - 8);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
479 ctx->ptr = ctx->buf + MD5BLKLEN - 8;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
480 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
481 else if (i -= 8) { /* need to pad this buffer? */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
482 memset (ctx->ptr,0,i); /* yes, pad out with zeros, leaving 8 bytes */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
483 ctx->ptr += i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
484 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
485 md5_encode (ctx->ptr,bits,2); /* make LSB-first length */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
486 md5_transform (ctx->state,ctx->buf);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
487 /* store state in digest */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
488 md5_encode (digest,ctx->state,4);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
489 /* erase context */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
490 memset (ctx,0,sizeof (MD5CONTEXT));
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
491 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
492
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
493 /* MD5 basic transformation
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
494 * Accepts: state vector
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
495 * current 64-byte block
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
496 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
497
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
498 static void md5_transform (unsigned long *state,unsigned char *block)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
499 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
500 unsigned long a = state[0],b = state[1],c = state[2],d = state[3],x[16];
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
501 md5_decode (x,block,16); /* decode into 16 longs */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
502 /* round 1 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
503 RND1 (a,b,c,d,x[ 0], 7,0xd76aa478); RND1 (d,a,b,c,x[ 1],12,0xe8c7b756);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
504 RND1 (c,d,a,b,x[ 2],17,0x242070db); RND1 (b,c,d,a,x[ 3],22,0xc1bdceee);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
505 RND1 (a,b,c,d,x[ 4], 7,0xf57c0faf); RND1 (d,a,b,c,x[ 5],12,0x4787c62a);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
506 RND1 (c,d,a,b,x[ 6],17,0xa8304613); RND1 (b,c,d,a,x[ 7],22,0xfd469501);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
507 RND1 (a,b,c,d,x[ 8], 7,0x698098d8); RND1 (d,a,b,c,x[ 9],12,0x8b44f7af);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
508 RND1 (c,d,a,b,x[10],17,0xffff5bb1); RND1 (b,c,d,a,x[11],22,0x895cd7be);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
509 RND1 (a,b,c,d,x[12], 7,0x6b901122); RND1 (d,a,b,c,x[13],12,0xfd987193);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
510 RND1 (c,d,a,b,x[14],17,0xa679438e); RND1 (b,c,d,a,x[15],22,0x49b40821);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
511 /* round 2 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
512 RND2 (a,b,c,d,x[ 1], 5,0xf61e2562); RND2 (d,a,b,c,x[ 6], 9,0xc040b340);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
513 RND2 (c,d,a,b,x[11],14,0x265e5a51); RND2 (b,c,d,a,x[ 0],20,0xe9b6c7aa);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
514 RND2 (a,b,c,d,x[ 5], 5,0xd62f105d); RND2 (d,a,b,c,x[10], 9, 0x2441453);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
515 RND2 (c,d,a,b,x[15],14,0xd8a1e681); RND2 (b,c,d,a,x[ 4],20,0xe7d3fbc8);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
516 RND2 (a,b,c,d,x[ 9], 5,0x21e1cde6); RND2 (d,a,b,c,x[14], 9,0xc33707d6);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
517 RND2 (c,d,a,b,x[ 3],14,0xf4d50d87); RND2 (b,c,d,a,x[ 8],20,0x455a14ed);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
518 RND2 (a,b,c,d,x[13], 5,0xa9e3e905); RND2 (d,a,b,c,x[ 2], 9,0xfcefa3f8);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
519 RND2 (c,d,a,b,x[ 7],14,0x676f02d9); RND2 (b,c,d,a,x[12],20,0x8d2a4c8a);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
520 /* round 3 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
521 RND3 (a,b,c,d,x[ 5], 4,0xfffa3942); RND3 (d,a,b,c,x[ 8],11,0x8771f681);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
522 RND3 (c,d,a,b,x[11],16,0x6d9d6122); RND3 (b,c,d,a,x[14],23,0xfde5380c);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
523 RND3 (a,b,c,d,x[ 1], 4,0xa4beea44); RND3 (d,a,b,c,x[ 4],11,0x4bdecfa9);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
524 RND3 (c,d,a,b,x[ 7],16,0xf6bb4b60); RND3 (b,c,d,a,x[10],23,0xbebfbc70);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
525 RND3 (a,b,c,d,x[13], 4,0x289b7ec6); RND3 (d,a,b,c,x[ 0],11,0xeaa127fa);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
526 RND3 (c,d,a,b,x[ 3],16,0xd4ef3085); RND3 (b,c,d,a,x[ 6],23, 0x4881d05);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
527 RND3 (a,b,c,d,x[ 9], 4,0xd9d4d039); RND3 (d,a,b,c,x[12],11,0xe6db99e5);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
528 RND3 (c,d,a,b,x[15],16,0x1fa27cf8); RND3 (b,c,d,a,x[ 2],23,0xc4ac5665);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
529 /* round 4 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
530 RND4 (a,b,c,d,x[ 0], 6,0xf4292244); RND4 (d,a,b,c,x[ 7],10,0x432aff97);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
531 RND4 (c,d,a,b,x[14],15,0xab9423a7); RND4 (b,c,d,a,x[ 5],21,0xfc93a039);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
532 RND4 (a,b,c,d,x[12], 6,0x655b59c3); RND4 (d,a,b,c,x[ 3],10,0x8f0ccc92);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
533 RND4 (c,d,a,b,x[10],15,0xffeff47d); RND4 (b,c,d,a,x[ 1],21,0x85845dd1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
534 RND4 (a,b,c,d,x[ 8], 6,0x6fa87e4f); RND4 (d,a,b,c,x[15],10,0xfe2ce6e0);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
535 RND4 (c,d,a,b,x[ 6],15,0xa3014314); RND4 (b,c,d,a,x[13],21,0x4e0811a1);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
536 RND4 (a,b,c,d,x[ 4], 6,0xf7537e82); RND4 (d,a,b,c,x[11],10,0xbd3af235);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
537 RND4 (c,d,a,b,x[ 2],15,0x2ad7d2bb); RND4 (b,c,d,a,x[ 9],21,0xeb86d391);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
538 /* update state */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
539 state[0] += a; state[1] += b; state[2] += c; state[3] += d;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
540 memset (x,0,sizeof (x)); /* erase sensitive data */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
541 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
542
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
543 /* You may wonder why these strange "& 0xff" maskings are here. This is to
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
544 * ensure correct results on machines with a char size of larger than 8 bits.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
545 * For example, the KCC compiler on the PDP-10 uses 9-bit chars.
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
546 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
547
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
548 /* MD5 encode unsigned long into LSB-first bytes
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
549 * Accepts: destination pointer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
550 * source
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
551 * length of source
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
552 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
553
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
554 static void md5_encode (unsigned char *dst,unsigned long *src,int len)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
555 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
556 int i;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
557 for (i = 0; i < len; i++) {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
558 *dst++ = (unsigned char) (src[i] & 0xff);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
559 *dst++ = (unsigned char) ((src[i] >> 8) & 0xff);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
560 *dst++ = (unsigned char) ((src[i] >> 16) & 0xff);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
561 *dst++ = (unsigned char) ((src[i] >> 24) & 0xff);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
562 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
563 }
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
564
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
565
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
566 /* MD5 decode LSB-first bytes into unsigned long
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
567 * Accepts: destination pointer
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
568 * source
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
569 * length of destination
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
570 */
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
571
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
572 static void md5_decode (unsigned long *dst,unsigned char *src,int len)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
573 {
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
574 int i, j;
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
575 for (i = 0, j = 0; i < len; i++, j += 4)
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
576 dst[i] = ((unsigned long) (src[j] & 0xff)) |
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
577 (((unsigned long) (src[j+1] & 0xff)) << 8) |
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
578 (((unsigned long) (src[j+2] & 0xff)) << 16) |
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
579 (((unsigned long) (src[j+3] & 0xff)) << 24);
ada5e610ab86 imap-2007e
yuuji@gentei.org
parents:
diff changeset
580 }

yatex.org