fio: fix aio trim completion latencies
[fio.git] / crc / md5.c
1 /*
2  * Shamelessly lifted from the 2.6 kernel (crypto/md5.c)
3  */
4 #include <string.h>
5 #include "md5.h"
6
7 static void md5_transform(uint32_t *hash, uint32_t const *in)
8 {
9         uint32_t a, b, c, d;
10
11         a = hash[0];
12         b = hash[1];
13         c = hash[2];
14         d = hash[3];
15
16         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
17         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
18         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
19         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
20         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
21         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
22         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
23         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
24         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
25         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
26         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
27         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
28         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
29         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
30         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
31         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
32
33         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
34         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
35         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
36         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
37         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
38         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
39         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
40         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
41         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
42         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
43         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
44         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
45         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
46         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
47         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
48         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
49
50         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
51         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
52         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
53         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
54         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
55         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
56         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
57         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
58         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
59         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
60         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
61         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
62         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
63         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
64         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
65         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
66
67         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
68         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
69         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
70         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
71         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
72         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
73         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
74         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
75         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
76         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
77         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
78         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
79         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
80         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
81         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
82         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
83
84         hash[0] += a;
85         hash[1] += b;
86         hash[2] += c;
87         hash[3] += d;
88 }
89
90 void fio_md5_init(struct fio_md5_ctx *mctx)
91 {
92         mctx->hash[0] = 0x67452301;
93         mctx->hash[1] = 0xefcdab89;
94         mctx->hash[2] = 0x98badcfe;
95         mctx->hash[3] = 0x10325476;
96 }
97
98 void fio_md5_update(struct fio_md5_ctx *mctx, const uint8_t *data,
99                     unsigned int len)
100 {
101         const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
102
103         mctx->byte_count += len;
104
105         if (avail > len) {
106                 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
107                        data, len);
108                 return;
109         }
110
111         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
112                data, avail);
113
114         md5_transform(mctx->hash, mctx->block);
115         data += avail;
116         len -= avail;
117
118         while (len >= sizeof(mctx->block)) {
119                 memcpy(mctx->block, data, sizeof(mctx->block));
120                 md5_transform(mctx->hash, mctx->block);
121                 data += sizeof(mctx->block);
122                 len -= sizeof(mctx->block);
123         }
124
125         memcpy(mctx->block, data, len);
126 }
127
128 void fio_md5_final(struct fio_md5_ctx *mctx)
129 {
130         const unsigned int offset = mctx->byte_count & 0x3f;
131         char *p = (char *)mctx->block + offset;
132         int padding = 56 - (offset + 1);
133
134         *p++ = 0x80;
135         if (padding < 0) {
136                 memset(p, 0x00, padding + sizeof (uint64_t));
137                 md5_transform(mctx->hash, mctx->block);
138                 p = (char *)mctx->block;
139                 padding = 56;
140         }
141
142         memset(p, 0, padding);
143         mctx->block[14] = mctx->byte_count << 3;
144         mctx->block[15] = mctx->byte_count >> 29;
145         md5_transform(mctx->hash, mctx->block);
146 }