Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/crypto/sha512-internal.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SHA-512 hash implementation and interface functions
3
 * Copyright (c) 2015, Pali Rohár <pali.rohar@gmail.com>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "includes.h"
10
11
#include "common.h"
12
#include "sha512_i.h"
13
#include "crypto.h"
14
15
16
/**
17
 * sha512_vector - SHA512 hash for data vector
18
 * @num_elem: Number of elements in the data vector
19
 * @addr: Pointers to the data areas
20
 * @len: Lengths of the data blocks
21
 * @mac: Buffer for the hash
22
 * Returns: 0 on success, -1 of failure
23
 */
24
int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
25
      u8 *mac)
26
0
{
27
0
  struct sha512_state ctx;
28
0
  size_t i;
29
30
0
  sha512_init(&ctx);
31
0
  for (i = 0; i < num_elem; i++)
32
0
    if (sha512_process(&ctx, addr[i], len[i]))
33
0
      return -1;
34
0
  if (sha512_done(&ctx, mac))
35
0
    return -1;
36
0
  return 0;
37
0
}
38
39
40
/* ===== start - public domain SHA512 implementation ===== */
41
42
/* This is based on SHA512 implementation in LibTomCrypt that was released into
43
 * public domain by Tom St Denis. */
44
45
2.61M
#define CONST64(n) n ## ULL
46
47
/* the K array */
48
static const u64 K[80] = {
49
  CONST64(0x428a2f98d728ae22), CONST64(0x7137449123ef65cd),
50
  CONST64(0xb5c0fbcfec4d3b2f), CONST64(0xe9b5dba58189dbbc),
51
  CONST64(0x3956c25bf348b538), CONST64(0x59f111f1b605d019),
52
  CONST64(0x923f82a4af194f9b), CONST64(0xab1c5ed5da6d8118),
53
  CONST64(0xd807aa98a3030242), CONST64(0x12835b0145706fbe),
54
  CONST64(0x243185be4ee4b28c), CONST64(0x550c7dc3d5ffb4e2),
55
  CONST64(0x72be5d74f27b896f), CONST64(0x80deb1fe3b1696b1),
56
  CONST64(0x9bdc06a725c71235), CONST64(0xc19bf174cf692694),
57
  CONST64(0xe49b69c19ef14ad2), CONST64(0xefbe4786384f25e3),
58
  CONST64(0x0fc19dc68b8cd5b5), CONST64(0x240ca1cc77ac9c65),
59
  CONST64(0x2de92c6f592b0275), CONST64(0x4a7484aa6ea6e483),
60
  CONST64(0x5cb0a9dcbd41fbd4), CONST64(0x76f988da831153b5),
61
  CONST64(0x983e5152ee66dfab), CONST64(0xa831c66d2db43210),
62
  CONST64(0xb00327c898fb213f), CONST64(0xbf597fc7beef0ee4),
63
  CONST64(0xc6e00bf33da88fc2), CONST64(0xd5a79147930aa725),
64
  CONST64(0x06ca6351e003826f), CONST64(0x142929670a0e6e70),
65
  CONST64(0x27b70a8546d22ffc), CONST64(0x2e1b21385c26c926),
66
  CONST64(0x4d2c6dfc5ac42aed), CONST64(0x53380d139d95b3df),
67
  CONST64(0x650a73548baf63de), CONST64(0x766a0abb3c77b2a8),
68
  CONST64(0x81c2c92e47edaee6), CONST64(0x92722c851482353b),
69
  CONST64(0xa2bfe8a14cf10364), CONST64(0xa81a664bbc423001),
70
  CONST64(0xc24b8b70d0f89791), CONST64(0xc76c51a30654be30),
71
  CONST64(0xd192e819d6ef5218), CONST64(0xd69906245565a910),
72
  CONST64(0xf40e35855771202a), CONST64(0x106aa07032bbd1b8),
73
  CONST64(0x19a4c116b8d2d0c8), CONST64(0x1e376c085141ab53),
74
  CONST64(0x2748774cdf8eeb99), CONST64(0x34b0bcb5e19b48a8),
75
  CONST64(0x391c0cb3c5c95a63), CONST64(0x4ed8aa4ae3418acb),
76
  CONST64(0x5b9cca4f7763e373), CONST64(0x682e6ff3d6b2b8a3),
77
  CONST64(0x748f82ee5defb2fc), CONST64(0x78a5636f43172f60),
78
  CONST64(0x84c87814a1f0ab72), CONST64(0x8cc702081a6439ec),
79
  CONST64(0x90befffa23631e28), CONST64(0xa4506cebde82bde9),
80
  CONST64(0xbef9a3f7b2c67915), CONST64(0xc67178f2e372532b),
81
  CONST64(0xca273eceea26619c), CONST64(0xd186b8c721c0c207),
82
  CONST64(0xeada7dd6cde0eb1e), CONST64(0xf57d4f7fee6ed178),
83
  CONST64(0x06f067aa72176fba), CONST64(0x0a637dc5a2c898a6),
84
  CONST64(0x113f9804bef90dae), CONST64(0x1b710b35131c471b),
85
  CONST64(0x28db77f523047d84), CONST64(0x32caab7b40c72493),
86
  CONST64(0x3c9ebe0a15c9bebc), CONST64(0x431d67c49c100d4c),
87
  CONST64(0x4cc5d4becb3e42b6), CONST64(0x597f299cfc657e2a),
88
  CONST64(0x5fcb6fab3ad6faec), CONST64(0x6c44198c4a475817)
89
};
90
91
/* Various logical functions */
92
68.0k
#define Ch(x,y,z)       (z ^ (x & (y ^ z)))
93
68.0k
#define Maj(x,y,z)      (((x | y) & z) | (x & y))
94
625k
#define S(x, n)         ROR64c(x, n)
95
108k
#define R(x, n)         (((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) n))
96
68.0k
#define Sigma0(x)       (S(x, 28) ^ S(x, 34) ^ S(x, 39))
97
68.0k
#define Sigma1(x)       (S(x, 14) ^ S(x, 18) ^ S(x, 41))
98
54.4k
#define Gamma0(x)       (S(x, 1) ^ S(x, 8) ^ R(x, 7))
99
54.4k
#define Gamma1(x)       (S(x, 19) ^ S(x, 61) ^ R(x, 6))
100
101
#define ROR64c(x, y) \
102
625k
    ( ((((x) & CONST64(0xFFFFFFFFFFFFFFFF)) >> ((u64) (y) & CONST64(63))) | \
103
625k
      ((x) << ((u64) (64 - ((y) & CONST64(63)))))) & \
104
625k
      CONST64(0xFFFFFFFFFFFFFFFF))
105
106
/* compress 1024-bits */
107
static int sha512_compress(struct sha512_state *md, unsigned char *buf)
108
850
{
109
850
  u64 S[8], t0, t1;
110
850
  u64 *W;
111
850
  int i;
112
113
850
  W = os_malloc(80 * sizeof(u64));
114
850
  if (!W)
115
0
    return -1;
116
117
  /* copy state into S */
118
7.65k
  for (i = 0; i < 8; i++) {
119
6.80k
    S[i] = md->state[i];
120
6.80k
  }
121
122
  /* copy the state into 1024-bits into W[0..15] */
123
14.4k
  for (i = 0; i < 16; i++)
124
13.6k
    W[i] = WPA_GET_BE64(buf + (8 * i));
125
126
  /* fill W[16..79] */
127
55.2k
  for (i = 16; i < 80; i++) {
128
54.4k
    W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
129
54.4k
      W[i - 16];
130
54.4k
  }
131
132
  /* Compress */
133
68.8k
  for (i = 0; i < 80; i++) {
134
68.0k
    t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
135
68.0k
    t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
136
68.0k
    S[7] = S[6];
137
68.0k
    S[6] = S[5];
138
68.0k
    S[5] = S[4];
139
68.0k
    S[4] = S[3] + t0;
140
68.0k
    S[3] = S[2];
141
68.0k
    S[2] = S[1];
142
68.0k
    S[1] = S[0];
143
68.0k
    S[0] = t0 + t1;
144
68.0k
  }
145
146
  /* feedback */
147
7.65k
  for (i = 0; i < 8; i++) {
148
6.80k
    md->state[i] = md->state[i] + S[i];
149
6.80k
  }
150
151
850
  os_free(W);
152
850
  return 0;
153
850
}
154
155
156
/**
157
   Initialize the hash state
158
   @param md   The hash state you wish to initialize
159
   @return CRYPT_OK if successful
160
*/
161
void sha512_init(struct sha512_state *md)
162
0
{
163
0
  md->curlen = 0;
164
0
  md->length = 0;
165
0
  md->state[0] = CONST64(0x6a09e667f3bcc908);
166
0
  md->state[1] = CONST64(0xbb67ae8584caa73b);
167
0
  md->state[2] = CONST64(0x3c6ef372fe94f82b);
168
0
  md->state[3] = CONST64(0xa54ff53a5f1d36f1);
169
0
  md->state[4] = CONST64(0x510e527fade682d1);
170
0
  md->state[5] = CONST64(0x9b05688c2b3e6c1f);
171
0
  md->state[6] = CONST64(0x1f83d9abfb41bd6b);
172
0
  md->state[7] = CONST64(0x5be0cd19137e2179);
173
0
}
174
175
176
/**
177
   Process a block of memory though the hash
178
   @param md     The hash state
179
   @param in     The data to hash
180
   @param inlen  The length of the data (octets)
181
   @return CRYPT_OK if successful
182
*/
183
int sha512_process(struct sha512_state *md, const unsigned char *in,
184
       unsigned long inlen)
185
384
{
186
384
  unsigned long n;
187
188
384
  if (md->curlen >= sizeof(md->buf))
189
0
    return -1;
190
191
1.48k
  while (inlen > 0) {
192
1.10k
    if (md->curlen == 0 && inlen >= SHA512_BLOCK_SIZE) {
193
588
      if (sha512_compress(md, (unsigned char *) in) < 0)
194
0
        return -1;
195
588
      md->length += SHA512_BLOCK_SIZE * 8;
196
588
      in += SHA512_BLOCK_SIZE;
197
588
      inlen -= SHA512_BLOCK_SIZE;
198
588
    } else {
199
512
      n = MIN(inlen, (SHA512_BLOCK_SIZE - md->curlen));
200
512
      os_memcpy(md->buf + md->curlen, in, n);
201
512
      md->curlen += n;
202
512
      in += n;
203
512
      inlen -= n;
204
512
      if (md->curlen == SHA512_BLOCK_SIZE) {
205
128
        if (sha512_compress(md, md->buf) < 0)
206
0
          return -1;
207
128
        md->length += 8 * SHA512_BLOCK_SIZE;
208
128
        md->curlen = 0;
209
128
      }
210
512
    }
211
1.10k
  }
212
213
384
  return 0;
214
384
}
215
216
217
/**
218
   Terminate the hash to get the digest
219
   @param md  The hash state
220
   @param out [out] The destination of the hash (64 bytes)
221
   @return CRYPT_OK if successful
222
*/
223
int sha512_done(struct sha512_state *md, unsigned char *out)
224
128
{
225
128
  int i;
226
227
128
  if (md->curlen >= sizeof(md->buf))
228
0
    return -1;
229
230
  /* increase the length of the message */
231
128
  md->length += md->curlen * CONST64(8);
232
233
  /* append the '1' bit */
234
128
  md->buf[md->curlen++] = (unsigned char) 0x80;
235
236
  /* if the length is currently above 112 bytes we append zeros
237
   * then compress.  Then we can fall back to padding zeros and length
238
   * encoding like normal.
239
   */
240
128
  if (md->curlen > 112) {
241
41
    while (md->curlen < 128) {
242
35
      md->buf[md->curlen++] = (unsigned char) 0;
243
35
    }
244
6
    sha512_compress(md, md->buf);
245
6
    md->curlen = 0;
246
6
  }
247
248
  /* pad up to 120 bytes of zeroes
249
   * note: that from 112 to 120 is the 64 MSB of the length.  We assume
250
   * that you won't hash > 2^64 bits of data... :-)
251
   */
252
6.71k
  while (md->curlen < 120) {
253
6.58k
    md->buf[md->curlen++] = (unsigned char) 0;
254
6.58k
  }
255
256
  /* store length */
257
128
  WPA_PUT_BE64(md->buf + 120, md->length);
258
128
  sha512_compress(md, md->buf);
259
260
  /* copy output */
261
1.15k
  for (i = 0; i < 8; i++)
262
1.02k
    WPA_PUT_BE64(out + (8 * i), md->state[i]);
263
264
128
  return 0;
265
128
}
266
267
/* ===== end - public domain SHA512 implementation ===== */