xref: /plan9/sys/src/cmd/unix/drawterm/libsec/md5.c (revision a84536681645e23c630ce4ef2e5c3b284d4c590b)
1 #include "../lib9.h"
2 #include "../libsec/libsec.h"
3 
4 /*
5  *  rfc1321 requires that I include this.  The code is new.  The constants
6  *  all come from the rfc (hence the copyright).  We trade a table for the
7  *  macros in rfc.  The total size is a lot less. -- presotto
8  *
9  *	Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
10  *	rights reserved.
11  *
12  *	License to copy and use this software is granted provided that it
13  *	is identified as the "RSA Data Security, Inc. MD5 Message-Digest
14  *	Algorithm" in all material mentioning or referencing this software
15  *	or this function.
16  *
17  *	License is also granted to make and use derivative works provided
18  *	that such works are identified as "derived from the RSA Data
19  *	Security, Inc. MD5 Message-Digest Algorithm" in all material
20  *	mentioning or referencing the derived work.
21  *
22  *	RSA Data Security, Inc. makes no representations concerning either
23  *	the merchantability of this software or the suitability of this
24  *	software forany particular purpose. It is provided "as is"
25  *	without express or implied warranty of any kind.
26  *	These notices must be retained in any copies of any part of this
27  *	documentation and/or software.
28  */
29 
30 static void encode(uchar*, u32int*, ulong);
31 static void decode(u32int*, uchar*, ulong);
32 
33 extern void _md5block(uchar*, ulong, u32int*);
34 
35 MD5state*
36 md5(uchar *p, ulong len, uchar *digest, MD5state *s)
37 {
38 	u32int x[16];
39 	uchar buf[128];
40 	int i;
41 	uchar *e;
42 
43 	if(s == nil){
44 		s = malloc(sizeof(*s));
45 		if(s == nil)
46 			return nil;
47 		memset(s, 0, sizeof(*s));
48 		s->malloced = 1;
49 	}
50 
51 	if(s->seeded == 0){
52 		/* seed the state, these constants would look nicer big-endian */
53 		s->state[0] = 0x67452301;
54 		s->state[1] = 0xefcdab89;
55 		s->state[2] = 0x98badcfe;
56 		s->state[3] = 0x10325476;
57 		s->seeded = 1;
58 	}
59 
60 	/* fill out the partial 64 byte block from previous calls */
61 	if(s->blen){
62 		i = 64 - s->blen;
63 		if(len < i)
64 			i = len;
65 		memmove(s->buf + s->blen, p, i);
66 		len -= i;
67 		s->blen += i;
68 		p += i;
69 		if(s->blen == 64){
70 			_md5block(s->buf, s->blen, s->state);
71 			s->len += s->blen;
72 			s->blen = 0;
73 		}
74 	}
75 
76 	/* do 64 byte blocks */
77 	i = len & ~0x3f;
78 	if(i){
79 		_md5block(p, i, s->state);
80 		s->len += i;
81 		len -= i;
82 		p += i;
83 	}
84 
85 	/* save the left overs if not last call */
86 	if(digest == 0){
87 		if(len){
88 			memmove(s->buf, p, len);
89 			s->blen += len;
90 		}
91 		return s;
92 	}
93 
94 	/*
95 	 *  this is the last time through, pad what's left with 0x80,
96 	 *  0's, and the input count to create a multiple of 64 bytes
97 	 */
98 	if(s->blen){
99 		p = s->buf;
100 		len = s->blen;
101 	} else {
102 		memmove(buf, p, len);
103 		p = buf;
104 	}
105 	s->len += len;
106 	e = p + len;
107 	if(len < 56)
108 		i = 56 - len;
109 	else
110 		i = 120 - len;
111 	memset(e, 0, i);
112 	*e = 0x80;
113 	len += i;
114 
115 	/* append the count */
116 	x[0] = s->len<<3;
117 	x[1] = s->len>>29;
118 	encode(p+len, x, 8);
119 
120 	/* digest the last part */
121 	_md5block(p, len+8, s->state);
122 	s->len += len;
123 
124 	/* return result and free state */
125 	encode(digest, s->state, MD5dlen);
126 	if(s->malloced == 1)
127 		free(s);
128 	return nil;
129 }
130 
131 /*
132  *	encodes input (u32int) into output (uchar). Assumes len is
133  *	a multiple of 4.
134  */
135 static void
136 encode(uchar *output, u32int *input, ulong len)
137 {
138 	u32int x;
139 	uchar *e;
140 
141 	for(e = output + len; output < e;) {
142 		x = *input++;
143 		*output++ = x;
144 		*output++ = x >> 8;
145 		*output++ = x >> 16;
146 		*output++ = x >> 24;
147 	}
148 }
149