Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/security/manager/ssl/md4.c
Line
Count
Source (jump to first uncovered line)
1
/* vim:set ts=2 sw=2 et cindent: */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
/*
7
 * "clean room" MD4 implementation (see RFC 1320)
8
 */
9
10
#include <string.h>
11
#include "md4.h"
12
13
/* the "conditional" function */
14
0
#define F(x,y,z) (((x) & (y)) | (~(x) & (z)))
15
16
/* the "majority" function */
17
0
#define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
18
19
/* the "parity" function */
20
0
#define H(x,y,z) ((x) ^ (y) ^ (z))
21
22
/* rotate n-bits to the left */
23
0
#define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n)))
24
25
/* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */
26
0
#define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s)
27
28
/* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */
29
0
#define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)
30
31
/* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */
32
0
#define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)
33
34
/* converts from word array to byte array, len is number of bytes */
35
static void w2b(uint8_t *out, const uint32_t *in, uint32_t len)
36
0
{
37
0
  uint8_t *bp; const uint32_t *wp, *wpend;
38
0
39
0
  bp = out;
40
0
  wp = in;
41
0
  wpend = wp + (len >> 2);
42
0
43
0
  for (; wp != wpend; ++wp, bp += 4)
44
0
  {
45
0
    bp[0] = (uint8_t) ((*wp      ) & 0xFF);
46
0
    bp[1] = (uint8_t) ((*wp >>  8) & 0xFF);
47
0
    bp[2] = (uint8_t) ((*wp >> 16) & 0xFF);
48
0
    bp[3] = (uint8_t) ((*wp >> 24) & 0xFF);
49
0
  }
50
0
}
51
52
/* converts from byte array to word array, len is number of bytes */
53
static void b2w(uint32_t *out, const uint8_t *in, uint32_t len)
54
0
{
55
0
  uint32_t *wp; const uint8_t *bp, *bpend;
56
0
57
0
  wp = out;
58
0
  bp = in;
59
0
  bpend = in + len;
60
0
61
0
  for (; bp != bpend; bp += 4, ++wp)
62
0
  {
63
0
    *wp = (uint32_t) (bp[0]      ) |
64
0
          (uint32_t) (bp[1] <<  8) |
65
0
          (uint32_t) (bp[2] << 16) |
66
0
          (uint32_t) (bp[3] << 24);
67
0
  }
68
0
}
69
70
/* update state: data is 64 bytes in length */
71
static void md4step(uint32_t state[4], const uint8_t *data)
72
0
{
73
0
  uint32_t A, B, C, D, X[16];
74
0
75
0
  b2w(X, data, 64);
76
0
77
0
  A = state[0];
78
0
  B = state[1];
79
0
  C = state[2];
80
0
  D = state[3];
81
0
82
0
  RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19);
83
0
  RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19);
84
0
  RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19);
85
0
  RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19);
86
0
87
0
  RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13);
88
0
  RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13);
89
0
  RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13);
90
0
  RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13);
91
0
92
0
  RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15);
93
0
  RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15);
94
0
  RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15);
95
0
  RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15);
96
0
97
0
  state[0] += A;
98
0
  state[1] += B;
99
0
  state[2] += C;
100
0
  state[3] += D;
101
0
}
102
103
void md4sum(const uint8_t *input, uint32_t inputLen, uint8_t *result)
104
0
{
105
0
  uint8_t final[128];
106
0
  uint32_t i, n, m, state[4];
107
0
  uint64_t inputLenBits;
108
0
  uint32_t inputLenBitsLow;
109
0
  uint32_t inputLenBitsHigh;
110
0
111
0
  /* magic initial states */
112
0
  state[0] = 0x67452301;
113
0
  state[1] = 0xEFCDAB89;
114
0
  state[2] = 0x98BADCFE;
115
0
  state[3] = 0x10325476;
116
0
117
0
  /* compute number of complete 64-byte segments contained in input */
118
0
  m = inputLen >> 6;
119
0
120
0
  /* digest first m segments */
121
0
  for (i=0; i<m; ++i)
122
0
    md4step(state, (input + (i << 6)));
123
0
124
0
  /* build final buffer */
125
0
  n = inputLen % 64;
126
0
  memcpy(final, input + (m << 6), n);
127
0
  final[n] = 0x80;
128
0
  memset(final + n + 1, 0, 120 - (n + 1));
129
0
130
0
  /* Append the original input length in bits as a 64-bit number. This is done
131
0
   * in two 32-bit chunks, with the least-significant 32 bits first.
132
0
   * w2b will handle endianness. */
133
0
  inputLenBits = inputLen << 3;
134
0
  inputLenBitsLow = (uint32_t)(inputLenBits & 0xFFFFFFFF);
135
0
  w2b(final + (n >= 56 ? 120 : 56), &inputLenBitsLow, 4);
136
0
  inputLenBitsHigh = (uint32_t)((inputLenBits >> 32) & 0xFFFFFFFF);
137
0
  w2b(final + (n >= 56 ? 124 : 60), &inputLenBitsHigh, 4);
138
0
139
0
  md4step(state, final);
140
0
  if (n >= 56)
141
0
    md4step(state, final + 64);
142
0
143
0
  /* copy state to result */
144
0
  w2b(result, state, 16);
145
0
}