xref: /xnu-12377.41.6/tools/tests/perf_index/md5.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  *  md5.c
3*bbb1b6f9SApple OSS Distributions  *  Adapted for perf_index from ccmd5_ltc.c in corecrypto
4*bbb1b6f9SApple OSS Distributions  *
5*bbb1b6f9SApple OSS Distributions  *  Created by Fabrice Gautier on 12/3/10.
6*bbb1b6f9SApple OSS Distributions  *  Copyright 2010,2011 Apple Inc. All rights reserved.
7*bbb1b6f9SApple OSS Distributions  *
8*bbb1b6f9SApple OSS Distributions  */
9*bbb1b6f9SApple OSS Distributions 
10*bbb1b6f9SApple OSS Distributions #include "md5.h"
11*bbb1b6f9SApple OSS Distributions 
12*bbb1b6f9SApple OSS Distributions #include <stdint.h>
13*bbb1b6f9SApple OSS Distributions #include <string.h>
14*bbb1b6f9SApple OSS Distributions 
15*bbb1b6f9SApple OSS Distributions #define CCMD5_BLOCK_SIZE 64
16*bbb1b6f9SApple OSS Distributions 
17*bbb1b6f9SApple OSS Distributions #define F(x, y, z)  (z ^ (x & (y ^ z)))
18*bbb1b6f9SApple OSS Distributions #define G(x, y, z)  (y ^ (z & (y ^ x)))
19*bbb1b6f9SApple OSS Distributions #define H(x, y, z)  (x^y^z)
20*bbb1b6f9SApple OSS Distributions #define I(x, y, z)  (y^(x|(~z)))
21*bbb1b6f9SApple OSS Distributions 
22*bbb1b6f9SApple OSS Distributions #define CC_ROLc(X, s) (((X) << (s)) | ((X) >> (32 - (s))))
23*bbb1b6f9SApple OSS Distributions 
24*bbb1b6f9SApple OSS Distributions #define FF(a, b, c, d, M, s, t) \
25*bbb1b6f9SApple OSS Distributions a = (a + F(b,c,d) + M + t); a = CC_ROLc(a, s) + b;
26*bbb1b6f9SApple OSS Distributions 
27*bbb1b6f9SApple OSS Distributions #define GG(a, b, c, d, M, s, t) \
28*bbb1b6f9SApple OSS Distributions a = (a + G(b,c,d) + M + t); a = CC_ROLc(a, s) + b;
29*bbb1b6f9SApple OSS Distributions 
30*bbb1b6f9SApple OSS Distributions #define HH(a, b, c, d, M, s, t) \
31*bbb1b6f9SApple OSS Distributions a = (a + H(b,c,d) + M + t); a = CC_ROLc(a, s) + b;
32*bbb1b6f9SApple OSS Distributions 
33*bbb1b6f9SApple OSS Distributions #define II(a, b, c, d, M, s, t) \
34*bbb1b6f9SApple OSS Distributions a = (a + I(b,c,d) + M + t); a = CC_ROLc(a, s) + b;
35*bbb1b6f9SApple OSS Distributions 
36*bbb1b6f9SApple OSS Distributions static void
md5_compress(uint32_t * state,unsigned long nblocks,const void * in)37*bbb1b6f9SApple OSS Distributions md5_compress(uint32_t *state, unsigned long nblocks, const void *in)
38*bbb1b6f9SApple OSS Distributions {
39*bbb1b6f9SApple OSS Distributions 	uint32_t i, W[16], a, b, c, d;
40*bbb1b6f9SApple OSS Distributions 	uint32_t *s = state;
41*bbb1b6f9SApple OSS Distributions 	const unsigned char *buf = in;
42*bbb1b6f9SApple OSS Distributions 
43*bbb1b6f9SApple OSS Distributions 	while (nblocks--) {
44*bbb1b6f9SApple OSS Distributions 		/* copy the state into 512-bits into W[0..15] */
45*bbb1b6f9SApple OSS Distributions 		for (i = 0; i < 16; i++) {
46*bbb1b6f9SApple OSS Distributions 			W[i] = ((uint32_t*)buf)[i];
47*bbb1b6f9SApple OSS Distributions 		}
48*bbb1b6f9SApple OSS Distributions 
49*bbb1b6f9SApple OSS Distributions 		/* copy state */
50*bbb1b6f9SApple OSS Distributions 		a = s[0];
51*bbb1b6f9SApple OSS Distributions 		b = s[1];
52*bbb1b6f9SApple OSS Distributions 		c = s[2];
53*bbb1b6f9SApple OSS Distributions 		d = s[3];
54*bbb1b6f9SApple OSS Distributions 
55*bbb1b6f9SApple OSS Distributions 		FF(a, b, c, d, W[0], 7, 0xd76aa478)
56*bbb1b6f9SApple OSS Distributions 		FF(d, a, b, c, W[1], 12, 0xe8c7b756)
57*bbb1b6f9SApple OSS Distributions 		FF(c, d, a, b, W[2], 17, 0x242070db)
58*bbb1b6f9SApple OSS Distributions 		FF(b, c, d, a, W[3], 22, 0xc1bdceee)
59*bbb1b6f9SApple OSS Distributions 		FF(a, b, c, d, W[4], 7, 0xf57c0faf)
60*bbb1b6f9SApple OSS Distributions 		FF(d, a, b, c, W[5], 12, 0x4787c62a)
61*bbb1b6f9SApple OSS Distributions 		FF(c, d, a, b, W[6], 17, 0xa8304613)
62*bbb1b6f9SApple OSS Distributions 		FF(b, c, d, a, W[7], 22, 0xfd469501)
63*bbb1b6f9SApple OSS Distributions 		FF(a, b, c, d, W[8], 7, 0x698098d8)
64*bbb1b6f9SApple OSS Distributions 		FF(d, a, b, c, W[9], 12, 0x8b44f7af)
65*bbb1b6f9SApple OSS Distributions 		FF(c, d, a, b, W[10], 17, 0xffff5bb1)
66*bbb1b6f9SApple OSS Distributions 		FF(b, c, d, a, W[11], 22, 0x895cd7be)
67*bbb1b6f9SApple OSS Distributions 		FF(a, b, c, d, W[12], 7, 0x6b901122)
68*bbb1b6f9SApple OSS Distributions 		FF(d, a, b, c, W[13], 12, 0xfd987193)
69*bbb1b6f9SApple OSS Distributions 		FF(c, d, a, b, W[14], 17, 0xa679438e)
70*bbb1b6f9SApple OSS Distributions 		FF(b, c, d, a, W[15], 22, 0x49b40821)
71*bbb1b6f9SApple OSS Distributions 		GG(a, b, c, d, W[1], 5, 0xf61e2562)
72*bbb1b6f9SApple OSS Distributions 		GG(d, a, b, c, W[6], 9, 0xc040b340)
73*bbb1b6f9SApple OSS Distributions 		GG(c, d, a, b, W[11], 14, 0x265e5a51)
74*bbb1b6f9SApple OSS Distributions 		GG(b, c, d, a, W[0], 20, 0xe9b6c7aa)
75*bbb1b6f9SApple OSS Distributions 		GG(a, b, c, d, W[5], 5, 0xd62f105d)
76*bbb1b6f9SApple OSS Distributions 		GG(d, a, b, c, W[10], 9, 0x02441453)
77*bbb1b6f9SApple OSS Distributions 		GG(c, d, a, b, W[15], 14, 0xd8a1e681)
78*bbb1b6f9SApple OSS Distributions 		GG(b, c, d, a, W[4], 20, 0xe7d3fbc8)
79*bbb1b6f9SApple OSS Distributions 		GG(a, b, c, d, W[9], 5, 0x21e1cde6)
80*bbb1b6f9SApple OSS Distributions 		GG(d, a, b, c, W[14], 9, 0xc33707d6)
81*bbb1b6f9SApple OSS Distributions 		GG(c, d, a, b, W[3], 14, 0xf4d50d87)
82*bbb1b6f9SApple OSS Distributions 		GG(b, c, d, a, W[8], 20, 0x455a14ed)
83*bbb1b6f9SApple OSS Distributions 		GG(a, b, c, d, W[13], 5, 0xa9e3e905)
84*bbb1b6f9SApple OSS Distributions 		GG(d, a, b, c, W[2], 9, 0xfcefa3f8)
85*bbb1b6f9SApple OSS Distributions 		GG(c, d, a, b, W[7], 14, 0x676f02d9)
86*bbb1b6f9SApple OSS Distributions 		GG(b, c, d, a, W[12], 20, 0x8d2a4c8a)
87*bbb1b6f9SApple OSS Distributions 		HH(a, b, c, d, W[5], 4, 0xfffa3942)
88*bbb1b6f9SApple OSS Distributions 		HH(d, a, b, c, W[8], 11, 0x8771f681)
89*bbb1b6f9SApple OSS Distributions 		HH(c, d, a, b, W[11], 16, 0x6d9d6122)
90*bbb1b6f9SApple OSS Distributions 		HH(b, c, d, a, W[14], 23, 0xfde5380c)
91*bbb1b6f9SApple OSS Distributions 		HH(a, b, c, d, W[1], 4, 0xa4beea44)
92*bbb1b6f9SApple OSS Distributions 		HH(d, a, b, c, W[4], 11, 0x4bdecfa9)
93*bbb1b6f9SApple OSS Distributions 		HH(c, d, a, b, W[7], 16, 0xf6bb4b60)
94*bbb1b6f9SApple OSS Distributions 		HH(b, c, d, a, W[10], 23, 0xbebfbc70)
95*bbb1b6f9SApple OSS Distributions 		HH(a, b, c, d, W[13], 4, 0x289b7ec6)
96*bbb1b6f9SApple OSS Distributions 		HH(d, a, b, c, W[0], 11, 0xeaa127fa)
97*bbb1b6f9SApple OSS Distributions 		HH(c, d, a, b, W[3], 16, 0xd4ef3085)
98*bbb1b6f9SApple OSS Distributions 		HH(b, c, d, a, W[6], 23, 0x04881d05)
99*bbb1b6f9SApple OSS Distributions 		HH(a, b, c, d, W[9], 4, 0xd9d4d039)
100*bbb1b6f9SApple OSS Distributions 		HH(d, a, b, c, W[12], 11, 0xe6db99e5)
101*bbb1b6f9SApple OSS Distributions 		HH(c, d, a, b, W[15], 16, 0x1fa27cf8)
102*bbb1b6f9SApple OSS Distributions 		HH(b, c, d, a, W[2], 23, 0xc4ac5665)
103*bbb1b6f9SApple OSS Distributions 		II(a, b, c, d, W[0], 6, 0xf4292244)
104*bbb1b6f9SApple OSS Distributions 		II(d, a, b, c, W[7], 10, 0x432aff97)
105*bbb1b6f9SApple OSS Distributions 		II(c, d, a, b, W[14], 15, 0xab9423a7)
106*bbb1b6f9SApple OSS Distributions 		II(b, c, d, a, W[5], 21, 0xfc93a039)
107*bbb1b6f9SApple OSS Distributions 		II(a, b, c, d, W[12], 6, 0x655b59c3)
108*bbb1b6f9SApple OSS Distributions 		II(d, a, b, c, W[3], 10, 0x8f0ccc92)
109*bbb1b6f9SApple OSS Distributions 		II(c, d, a, b, W[10], 15, 0xffeff47d)
110*bbb1b6f9SApple OSS Distributions 		II(b, c, d, a, W[1], 21, 0x85845dd1)
111*bbb1b6f9SApple OSS Distributions 		II(a, b, c, d, W[8], 6, 0x6fa87e4f)
112*bbb1b6f9SApple OSS Distributions 		II(d, a, b, c, W[15], 10, 0xfe2ce6e0)
113*bbb1b6f9SApple OSS Distributions 		II(c, d, a, b, W[6], 15, 0xa3014314)
114*bbb1b6f9SApple OSS Distributions 		II(b, c, d, a, W[13], 21, 0x4e0811a1)
115*bbb1b6f9SApple OSS Distributions 		II(a, b, c, d, W[4], 6, 0xf7537e82)
116*bbb1b6f9SApple OSS Distributions 		II(d, a, b, c, W[11], 10, 0xbd3af235)
117*bbb1b6f9SApple OSS Distributions 		II(c, d, a, b, W[2], 15, 0x2ad7d2bb)
118*bbb1b6f9SApple OSS Distributions 		II(b, c, d, a, W[9], 21, 0xeb86d391)
119*bbb1b6f9SApple OSS Distributions 
120*bbb1b6f9SApple OSS Distributions 		/* store state */
121*bbb1b6f9SApple OSS Distributions 		s[0] += a;
122*bbb1b6f9SApple OSS Distributions 		s[1] += b;
123*bbb1b6f9SApple OSS Distributions 		s[2] += c;
124*bbb1b6f9SApple OSS Distributions 		s[3] += d;
125*bbb1b6f9SApple OSS Distributions 
126*bbb1b6f9SApple OSS Distributions 		buf += CCMD5_BLOCK_SIZE;
127*bbb1b6f9SApple OSS Distributions 	}
128*bbb1b6f9SApple OSS Distributions }
129*bbb1b6f9SApple OSS Distributions 
130*bbb1b6f9SApple OSS Distributions void
md5_hash(uint8_t * message,uint64_t len,uint32_t * hash)131*bbb1b6f9SApple OSS Distributions md5_hash(uint8_t *message, uint64_t len, uint32_t *hash)
132*bbb1b6f9SApple OSS Distributions {
133*bbb1b6f9SApple OSS Distributions 	hash[0] = 0x67452301;
134*bbb1b6f9SApple OSS Distributions 	hash[1] = 0xEFCDAB89;
135*bbb1b6f9SApple OSS Distributions 	hash[2] = 0x98BADCFE;
136*bbb1b6f9SApple OSS Distributions 	hash[3] = 0x10325476;
137*bbb1b6f9SApple OSS Distributions 
138*bbb1b6f9SApple OSS Distributions 	md5_compress(hash, len / 64, message);
139*bbb1b6f9SApple OSS Distributions 
140*bbb1b6f9SApple OSS Distributions 	uint32_t blockbuff[16];
141*bbb1b6f9SApple OSS Distributions 	uint8_t *byteptr = (uint8_t*)blockbuff;
142*bbb1b6f9SApple OSS Distributions 
143*bbb1b6f9SApple OSS Distributions 	int left = len % 64;
144*bbb1b6f9SApple OSS Distributions 	memcpy(byteptr, message + len - left, left);
145*bbb1b6f9SApple OSS Distributions 
146*bbb1b6f9SApple OSS Distributions 	byteptr[left] = 0x80;
147*bbb1b6f9SApple OSS Distributions 	left++;
148*bbb1b6f9SApple OSS Distributions 	if (64 - left >= 8) {
149*bbb1b6f9SApple OSS Distributions 		bzero(byteptr + left, 56 - left);
150*bbb1b6f9SApple OSS Distributions 	} else {
151*bbb1b6f9SApple OSS Distributions 		memset(byteptr + left, 0, 64 - left);
152*bbb1b6f9SApple OSS Distributions 		md5_compress(hash, 1, blockbuff);
153*bbb1b6f9SApple OSS Distributions 		bzero(blockbuff, 56);
154*bbb1b6f9SApple OSS Distributions 	}
155*bbb1b6f9SApple OSS Distributions 	blockbuff[14] = (uint32_t)(len << 3);
156*bbb1b6f9SApple OSS Distributions 	blockbuff[15] = (uint32_t)(len >> 29);
157*bbb1b6f9SApple OSS Distributions 	md5_compress(hash, 1, blockbuff);
158*bbb1b6f9SApple OSS Distributions }
159