Coverage Report

Created: 2024-09-08 06:43

/src/fftw3/kernel/md5.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2003, 2007-14 Matteo Frigo
3
 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
 *
19
 */
20
21
/* 
22
   independent implementation of Ron Rivest's MD5 message-digest
23
   algorithm, based on rfc 1321.
24
25
   Optimized for small code size, not speed.  Works as long as
26
   sizeof(md5uint) >= 4.
27
*/
28
29
#include "kernel/ifftw.h"
30
31
/* sintab[i] = 4294967296.0 * abs(sin((double)(i + 1))) */
32
static const md5uint sintab[64] = {
33
     0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
34
     0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
35
     0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
36
     0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
37
     0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
38
     0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
39
     0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
40
     0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
41
     0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
42
     0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
43
     0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
44
     0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
45
     0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
46
     0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
47
     0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
48
     0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
49
}; 
50
51
/* see rfc 1321 section 3.4 */
52
static const struct roundtab {
53
     char k; 
54
     char s;
55
} roundtab[64] = {
56
     {  0,  7}, {  1, 12}, {  2, 17}, {  3, 22},
57
     {  4,  7}, {  5, 12}, {  6, 17}, {  7, 22},
58
     {  8,  7}, {  9, 12}, { 10, 17}, { 11, 22},
59
     { 12,  7}, { 13, 12}, { 14, 17}, { 15, 22},
60
     {  1,  5}, {  6,  9}, { 11, 14}, {  0, 20},
61
     {  5,  5}, { 10,  9}, { 15, 14}, {  4, 20},
62
     {  9,  5}, { 14,  9}, {  3, 14}, {  8, 20},
63
     { 13,  5}, {  2,  9}, {  7, 14}, { 12, 20},
64
     {  5,  4}, {  8, 11}, { 11, 16}, { 14, 23},
65
     {  1,  4}, {  4, 11}, {  7, 16}, { 10, 23},
66
     { 13,  4}, {  0, 11}, {  3, 16}, {  6, 23},
67
     {  9,  4}, { 12, 11}, { 15, 16}, {  2, 23},
68
     {  0,  6}, {  7, 10}, { 14, 15}, {  5, 21},
69
     { 12,  6}, {  3, 10}, { 10, 15}, {  1, 21},
70
     {  8,  6}, { 15, 10}, {  6, 15}, { 13, 21},
71
     {  4,  6}, { 11, 10}, {  2, 15}, {  9, 21}
72
};
73
74
478k
#define rol(a, s) ((a << (int)(s)) | (a >> (32 - (int)(s))))
75
76
static void doblock(md5sig state, const unsigned char *data)
77
7.48k
{
78
7.48k
     md5uint a, b, c, d, t, x[16];
79
7.48k
     const md5uint msk = (md5uint)0xffffffffUL;
80
7.48k
     int i;
81
82
     /* encode input bytes into md5uint */
83
127k
     for (i = 0; i < 16; ++i) {
84
119k
    const unsigned char *p = data + 4 * i;
85
119k
    x[i] = (unsigned)p[0] | ((unsigned)p[1] << 8) | ((unsigned)p[2] << 16) | ((unsigned)p[3] << 24);
86
119k
     }
87
88
7.48k
     a = state[0]; b = state[1]; c = state[2]; d = state[3];
89
486k
     for (i = 0; i < 64; ++i) {
90
478k
    const struct roundtab *p = roundtab + i;
91
478k
    switch (i >> 4) {
92
119k
        case 0: a += (b & c) | (~b & d); break;
93
119k
        case 1: a += (b & d) | (c & ~d); break;
94
119k
        case 2: a += b ^ c ^ d; break;
95
119k
        case 3: a += c ^ (b | ~d); break;
96
478k
    }
97
478k
    a += sintab[i];
98
478k
    a += x[(int)(p->k)];
99
478k
    a &= msk;
100
478k
    t = b + rol(a, p->s);
101
478k
    a = d; d = c; c = b; b = t;
102
478k
     }
103
7.48k
     state[0] = (state[0] + a) & msk;
104
7.48k
     state[1] = (state[1] + b) & msk;
105
7.48k
     state[2] = (state[2] + c) & msk;
106
7.48k
     state[3] = (state[3] + d) & msk;
107
7.48k
}
108
109
110
void X(md5begin)(md5 *p)
111
3.70k
{
112
3.70k
     p->s[0] = 0x67452301;
113
3.70k
     p->s[1] = 0xefcdab89;
114
3.70k
     p->s[2] = 0x98badcfe;
115
3.70k
     p->s[3] = 0x10325476;
116
3.70k
     p->l = 0;
117
3.70k
}
118
119
void X(md5putc)(md5 *p, unsigned char c)
120
478k
{
121
478k
     p->c[p->l % 64] = c;
122
478k
     if (((++p->l) % 64) == 0) doblock(p->s, p->c);
123
478k
}
124
125
void X(md5end)(md5 *p)
126
3.70k
{
127
3.70k
     unsigned l, i;
128
129
3.70k
     l = 8 * p->l; /* length before padding, in bits */
130
131
     /* rfc 1321 section 3.1: padding */
132
3.70k
     X(md5putc)(p, 0x80);
133
103k
     while ((p->l % 64) != 56) X(md5putc)(p, 0x00);
134
135
     /* rfc 1321 section 3.2: length (little endian) */
136
33.3k
     for (i = 0; i < 8; ++i) {
137
29.6k
    X(md5putc)(p, (unsigned char)(l & 0xFF));
138
29.6k
    l = l >> 8;
139
29.6k
     }
140
141
     /* Now p->l % 64 == 0 and signature is in p->s */
142
3.70k
}