Coverage Report

Created: 2025-07-12 06:07

/src/usrsctp/usrsctplib/netinet/sctp_sha1.c
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 *
4
 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5
 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6
 * Copyright (c) 2008-2013, by Michael Tuexen. All rights reserved.
7
 * Copyright (c) 2013,      by Lally Singh. All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * a) Redistributions of source code must retain the above copyright notice,
13
 *   this list of conditions and the following disclaimer.
14
 *
15
 * b) Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *   the documentation and/or other materials provided with the distribution.
18
 *
19
 * c) Neither the name of Cisco Systems, Inc. nor the names of its
20
 *    contributors may be used to endorse or promote products derived
21
 *    from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33
 * THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
36
#include <netinet/sctp_sha1.h>
37
38
#if defined(SCTP_USE_NSS_SHA1)
39
/* A SHA-1 Digest is 160 bits, or 20 bytes */
40
#define SHA_DIGEST_LENGTH (20)
41
42
void
43
sctp_sha1_init(struct sctp_sha1_context *ctx)
44
{
45
  ctx->pk11_ctx = PK11_CreateDigestContext(SEC_OID_SHA1);
46
  PK11_DigestBegin(ctx->pk11_ctx);
47
}
48
49
void
50
sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
51
{
52
  PK11_DigestOp(ctx->pk11_ctx, ptr, siz);
53
}
54
55
void
56
sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
57
{
58
  unsigned int output_len = 0;
59
60
  PK11_DigestFinal(ctx->pk11_ctx, digest, &output_len, SHA_DIGEST_LENGTH);
61
  PK11_DestroyContext(ctx->pk11_ctx, PR_TRUE);
62
}
63
64
#elif defined(SCTP_USE_OPENSSL_SHA1)
65
66
void
67
sctp_sha1_init(struct sctp_sha1_context *ctx)
68
{
69
  SHA1_Init(&ctx->sha_ctx);
70
}
71
72
void
73
sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
74
{
75
  SHA1_Update(&ctx->sha_ctx, ptr, (unsigned long)siz);
76
}
77
78
void
79
sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
80
{
81
  SHA1_Final(digest, &ctx->sha_ctx);
82
}
83
84
#else
85
86
#include <string.h>
87
#if defined(_WIN32) && defined(__Userspace__)
88
#include <winsock2.h>
89
#elif !(defined(_WIN32) && !defined(__Userspace__))
90
#include <arpa/inet.h>
91
#endif
92
93
2.15M
#define F1(B,C,D) (((B & C) | ((~B) & D)))  /* 0  <= t <= 19 */
94
2.15M
#define F2(B,C,D) (B ^ C ^ D)  /* 20 <= t <= 39 */
95
2.15M
#define F3(B,C,D) ((B & C) | (B & D) | (C & D))  /* 40 <= t <= 59 */
96
2.15M
#define F4(B,C,D) (B ^ C ^ D)  /* 600 <= t <= 79 */
97
98
/* circular shift */
99
24.1M
#define CSHIFT(A,B) ((B << A) | (B >> (32-A)))
100
101
2.15M
#define K1 0x5a827999    /* 0  <= t <= 19 */
102
2.15M
#define K2 0x6ed9eba1    /* 20 <= t <= 39 */
103
2.15M
#define K3 0x8f1bbcdc    /* 40 <= t <= 59 */
104
2.15M
#define K4 0xca62c1d6    /* 60 <= t <= 79 */
105
106
23.8k
#define H0INIT 0x67452301
107
23.8k
#define H1INIT 0xefcdab89
108
23.8k
#define H2INIT 0x98badcfe
109
23.8k
#define H3INIT 0x10325476
110
23.8k
#define H4INIT 0xc3d2e1f0
111
112
void
113
sctp_sha1_init(struct sctp_sha1_context *ctx)
114
23.8k
{
115
  /* Init the SHA-1 context structure */
116
23.8k
  ctx->A = 0;
117
23.8k
  ctx->B = 0;
118
23.8k
  ctx->C = 0;
119
23.8k
  ctx->D = 0;
120
23.8k
  ctx->E = 0;
121
23.8k
  ctx->H0 = H0INIT;
122
23.8k
  ctx->H1 = H1INIT;
123
23.8k
  ctx->H2 = H2INIT;
124
23.8k
  ctx->H3 = H3INIT;
125
23.8k
  ctx->H4 = H4INIT;
126
23.8k
  ctx->TEMP = 0;
127
23.8k
  memset(ctx->words, 0, sizeof(ctx->words));
128
23.8k
  ctx->how_many_in_block = 0;
129
23.8k
  ctx->running_total = 0;
130
23.8k
}
131
132
static void
133
sctp_sha1_process_a_block(struct sctp_sha1_context *ctx, unsigned int *block)
134
107k
{
135
107k
  int i;
136
137
  /* init the W0-W15 to the block of words being hashed. */
138
  /* step a) */
139
1.83M
  for (i = 0; i < 16; i++) {
140
1.72M
    ctx->words[i] = ntohl(block[i]);
141
1.72M
  }
142
  /* now init the rest based on the SHA-1 formula, step b) */
143
6.99M
  for (i = 16; i < 80; i++) {
144
6.89M
    ctx->words[i] = CSHIFT(1, ((ctx->words[(i - 3)]) ^
145
6.89M
        (ctx->words[(i - 8)]) ^
146
6.89M
        (ctx->words[(i - 14)]) ^
147
6.89M
        (ctx->words[(i - 16)])));
148
6.89M
  }
149
  /* step c) */
150
107k
  ctx->A = ctx->H0;
151
107k
  ctx->B = ctx->H1;
152
107k
  ctx->C = ctx->H2;
153
107k
  ctx->D = ctx->H3;
154
107k
  ctx->E = ctx->H4;
155
156
  /* step d) */
157
8.72M
  for (i = 0; i < 80; i++) {
158
8.61M
    if (i < 20) {
159
2.15M
      ctx->TEMP = ((CSHIFT(5, ctx->A)) +
160
2.15M
          (F1(ctx->B, ctx->C, ctx->D)) +
161
2.15M
          (ctx->E) +
162
2.15M
          ctx->words[i] +
163
2.15M
          K1);
164
6.46M
    } else if (i < 40) {
165
2.15M
      ctx->TEMP = ((CSHIFT(5, ctx->A)) +
166
2.15M
          (F2(ctx->B, ctx->C, ctx->D)) +
167
2.15M
          (ctx->E) +
168
2.15M
          (ctx->words[i]) +
169
2.15M
          K2);
170
4.30M
    } else if (i < 60) {
171
2.15M
      ctx->TEMP = ((CSHIFT(5, ctx->A)) +
172
2.15M
          (F3(ctx->B, ctx->C, ctx->D)) +
173
2.15M
          (ctx->E) +
174
2.15M
          (ctx->words[i]) +
175
2.15M
          K3);
176
2.15M
    } else {
177
2.15M
      ctx->TEMP = ((CSHIFT(5, ctx->A)) +
178
2.15M
          (F4(ctx->B, ctx->C, ctx->D)) +
179
2.15M
          (ctx->E) +
180
2.15M
          (ctx->words[i]) +
181
2.15M
          K4);
182
2.15M
    }
183
8.61M
    ctx->E = ctx->D;
184
8.61M
    ctx->D = ctx->C;
185
8.61M
    ctx->C = CSHIFT(30, ctx->B);
186
8.61M
    ctx->B = ctx->A;
187
8.61M
    ctx->A = ctx->TEMP;
188
8.61M
  }
189
  /* step e) */
190
107k
  ctx->H0 = (ctx->H0) + (ctx->A);
191
107k
  ctx->H1 = (ctx->H1) + (ctx->B);
192
107k
  ctx->H2 = (ctx->H2) + (ctx->C);
193
107k
  ctx->H3 = (ctx->H3) + (ctx->D);
194
107k
  ctx->H4 = (ctx->H4) + (ctx->E);
195
107k
}
196
197
void
198
sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
199
52.7k
{
200
52.7k
  unsigned int number_left, left_to_fill;
201
202
52.7k
  number_left = siz;
203
135k
  while (number_left > 0) {
204
111k
    left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block;
205
111k
    if (left_to_fill > number_left) {
206
      /* can only partially fill up this one */
207
28.0k
      memcpy(&ctx->sha_block[ctx->how_many_in_block],
208
28.0k
          ptr, number_left);
209
28.0k
      ctx->how_many_in_block += number_left;
210
28.0k
      ctx->running_total += number_left;
211
28.0k
      break;
212
83.1k
    } else {
213
      /* block is now full, process it */
214
83.1k
      memcpy(&ctx->sha_block[ctx->how_many_in_block],
215
83.1k
          ptr, left_to_fill);
216
83.1k
      sctp_sha1_process_a_block(ctx,
217
83.1k
          (unsigned int *)ctx->sha_block);
218
83.1k
      number_left -= left_to_fill;
219
83.1k
      ctx->running_total += left_to_fill;
220
83.1k
      ctx->how_many_in_block = 0;
221
83.1k
      ptr = (const unsigned char *)(ptr + left_to_fill);
222
83.1k
    }
223
111k
  }
224
52.7k
}
225
226
void
227
sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
228
23.8k
{
229
  /*
230
   * if any left in block fill with padding and process. Then transfer
231
   * the digest to the pointer. At the last block some special rules
232
   * need to apply. We must add a 1 bit following the message, then we
233
   * pad with 0's. The total size is encoded as a 64 bit number at the
234
   * end. Now if the last buffer has more than 55 octets in it we
235
   * cannot fit the 64 bit number + 10000000 pad on the end and must
236
   * add the 10000000 pad, pad the rest of the message with 0's and
237
   * then create an all 0 message with just the 64 bit size at the end
238
   * and run this block through by itself.  Also the 64 bit int must
239
   * be in network byte order.
240
   */
241
23.8k
  int left_to_fill;
242
23.8k
  unsigned int i, *ptr;
243
244
23.8k
  if (ctx->how_many_in_block > 55) {
245
    /*
246
     * special case, we need to process two blocks here. One for
247
     * the current stuff plus possibly the pad. The other for
248
     * the size.
249
     */
250
686
    left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block;
251
686
    if (left_to_fill == 0) {
252
      /* Should not really happen but I am paranoid */
253
0
      sctp_sha1_process_a_block(ctx,
254
0
          (unsigned int *)ctx->sha_block);
255
      /* init last block, a bit different than the rest */
256
0
      ctx->sha_block[0] = '\x80';
257
0
      for (i = 1; i < sizeof(ctx->sha_block); i++) {
258
0
        ctx->sha_block[i] = 0x0;
259
0
      }
260
686
    } else if (left_to_fill == 1) {
261
252
      ctx->sha_block[ctx->how_many_in_block] = '\x80';
262
252
      sctp_sha1_process_a_block(ctx,
263
252
          (unsigned int *)ctx->sha_block);
264
      /* init last block */
265
252
      memset(ctx->sha_block, 0, sizeof(ctx->sha_block));
266
434
    } else {
267
434
      ctx->sha_block[ctx->how_many_in_block] = '\x80';
268
434
      for (i = (ctx->how_many_in_block + 1);
269
2.95k
          i < sizeof(ctx->sha_block);
270
2.52k
          i++) {
271
2.52k
        ctx->sha_block[i] = 0x0;
272
2.52k
      }
273
434
      sctp_sha1_process_a_block(ctx,
274
434
          (unsigned int *)ctx->sha_block);
275
      /* init last block */
276
434
      memset(ctx->sha_block, 0, sizeof(ctx->sha_block));
277
434
    }
278
    /* This is in bits so multiply by 8 */
279
686
    ctx->running_total *= 8;
280
686
    ptr = (unsigned int *)&ctx->sha_block[60];
281
686
    *ptr = htonl(ctx->running_total);
282
686
    sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block);
283
23.1k
  } else {
284
    /*
285
     * easy case, we just pad this message to size - end with 0
286
     * add the magic 0x80 to the next word and then put the
287
     * network byte order size in the last spot and process the
288
     * block.
289
     */
290
23.1k
    ctx->sha_block[ctx->how_many_in_block] = '\x80';
291
23.1k
    for (i = (ctx->how_many_in_block + 1);
292
934k
        i < sizeof(ctx->sha_block);
293
911k
        i++) {
294
911k
      ctx->sha_block[i] = 0x0;
295
911k
    }
296
    /* get last int spot */
297
23.1k
    ctx->running_total *= 8;
298
23.1k
    ptr = (unsigned int *)&ctx->sha_block[60];
299
23.1k
    *ptr = htonl(ctx->running_total);
300
23.1k
    sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block);
301
23.1k
  }
302
  /* transfer the digest back to the user */
303
23.8k
  digest[3] = (ctx->H0 & 0xff);
304
23.8k
  digest[2] = ((ctx->H0 >> 8) & 0xff);
305
23.8k
  digest[1] = ((ctx->H0 >> 16) & 0xff);
306
23.8k
  digest[0] = ((ctx->H0 >> 24) & 0xff);
307
308
23.8k
  digest[7] = (ctx->H1 & 0xff);
309
23.8k
  digest[6] = ((ctx->H1 >> 8) & 0xff);
310
23.8k
  digest[5] = ((ctx->H1 >> 16) & 0xff);
311
23.8k
  digest[4] = ((ctx->H1 >> 24) & 0xff);
312
313
23.8k
  digest[11] = (ctx->H2 & 0xff);
314
23.8k
  digest[10] = ((ctx->H2 >> 8) & 0xff);
315
23.8k
  digest[9] = ((ctx->H2 >> 16) & 0xff);
316
23.8k
  digest[8] = ((ctx->H2 >> 24) & 0xff);
317
318
23.8k
  digest[15] = (ctx->H3 & 0xff);
319
23.8k
  digest[14] = ((ctx->H3 >> 8) & 0xff);
320
23.8k
  digest[13] = ((ctx->H3 >> 16) & 0xff);
321
23.8k
  digest[12] = ((ctx->H3 >> 24) & 0xff);
322
323
23.8k
  digest[19] = (ctx->H4 & 0xff);
324
23.8k
  digest[18] = ((ctx->H4 >> 8) & 0xff);
325
23.8k
  digest[17] = ((ctx->H4 >> 16) & 0xff);
326
23.8k
  digest[16] = ((ctx->H4 >> 24) & 0xff);
327
23.8k
}
328
329
#endif