Coverage Report

Created: 2026-08-30 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libsrtp/crypto/hash/sha1.c
Line
Count
Source
1
/*
2
 * sha1.c
3
 *
4
 * an implementation of the Secure Hash Algorithm v.1 (SHA-1),
5
 * specified in FIPS 180-1
6
 *
7
 * David A. McGrew
8
 * Cisco Systems, Inc.
9
 */
10
11
/*
12
 *
13
 * Copyright (c) 2001-2017, Cisco Systems, Inc.
14
 * All rights reserved.
15
 *
16
 * Redistribution and use in source and binary forms, with or without
17
 * modification, are permitted provided that the following conditions
18
 * are met:
19
 *
20
 *   Redistributions of source code must retain the above copyright
21
 *   notice, this list of conditions and the following disclaimer.
22
 *
23
 *   Redistributions in binary form must reproduce the above
24
 *   copyright notice, this list of conditions and the following
25
 *   disclaimer in the documentation and/or other materials provided
26
 *   with the distribution.
27
 *
28
 *   Neither the name of the Cisco Systems, Inc. nor the names of its
29
 *   contributors may be used to endorse or promote products derived
30
 *   from this software without specific prior written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
36
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43
 * OF THE POSSIBILITY OF SUCH DAMAGE.
44
 *
45
 */
46
47
#ifdef HAVE_CONFIG_H
48
#include <config.h>
49
#endif
50
51
#include "sha1.h"
52
53
srtp_debug_module_t srtp_mod_sha1 = {
54
    false,  /* debugging is off by default */
55
    "sha-1" /* printable module name       */
56
};
57
58
/* SN == Rotate left N bits */
59
8.20M
#define S1(X) ((X << 1) | (X >> 31))
60
10.2M
#define S5(X) ((X << 5) | (X >> 27))
61
10.2M
#define S30(X) ((X << 30) | (X >> 2))
62
63
2.56M
#define f0(B, C, D) ((B & C) | (~B & D))
64
2.56M
#define f1(B, C, D) (B ^ C ^ D)
65
2.56M
#define f2(B, C, D) ((B & C) | (B & D) | (C & D))
66
2.56M
#define f3(B, C, D) (B ^ C ^ D)
67
68
/*
69
 * nota bene: the variable K0 appears in the curses library, so we
70
 * give longer names to these variables to avoid spurious warnings
71
 * on systems that uses curses
72
 */
73
74
uint32_t SHA_K0 = 0x5A827999; /* Kt for 0  <= t <= 19 */
75
uint32_t SHA_K1 = 0x6ED9EBA1; /* Kt for 20 <= t <= 39 */
76
uint32_t SHA_K2 = 0x8F1BBCDC; /* Kt for 40 <= t <= 59 */
77
uint32_t SHA_K3 = 0xCA62C1D6; /* Kt for 60 <= t <= 79 */
78
79
/*
80
 *  srtp_sha1_core(M, H) computes the core compression function, where M is
81
 *  the next part of the message (in network byte order) and H is the
82
 *  intermediate state { H0, H1, ...} (in host byte order)
83
 *
84
 *  this function does not do any of the padding required in the
85
 *  complete SHA1 function
86
 *
87
 *  this function is used in the SEAL 3.0 key setup routines
88
 *  (crypto/cipher/seal.c)
89
 */
90
91
void srtp_sha1_core(const uint32_t M[16], uint32_t hash_value[5])
92
96.5k
{
93
96.5k
    uint32_t H0;
94
96.5k
    uint32_t H1;
95
96.5k
    uint32_t H2;
96
96.5k
    uint32_t H3;
97
96.5k
    uint32_t H4;
98
96.5k
    uint32_t W[80];
99
96.5k
    uint32_t A, B, C, D, E, TEMP;
100
96.5k
    size_t t;
101
102
    /* copy hash_value into H0, H1, H2, H3, H4 */
103
96.5k
    H0 = hash_value[0];
104
96.5k
    H1 = hash_value[1];
105
96.5k
    H2 = hash_value[2];
106
96.5k
    H3 = hash_value[3];
107
96.5k
    H4 = hash_value[4];
108
109
    /* copy/xor message into array */
110
111
96.5k
    W[0] = be32_to_cpu(M[0]);
112
96.5k
    W[1] = be32_to_cpu(M[1]);
113
96.5k
    W[2] = be32_to_cpu(M[2]);
114
96.5k
    W[3] = be32_to_cpu(M[3]);
115
96.5k
    W[4] = be32_to_cpu(M[4]);
116
96.5k
    W[5] = be32_to_cpu(M[5]);
117
96.5k
    W[6] = be32_to_cpu(M[6]);
118
96.5k
    W[7] = be32_to_cpu(M[7]);
119
96.5k
    W[8] = be32_to_cpu(M[8]);
120
96.5k
    W[9] = be32_to_cpu(M[9]);
121
96.5k
    W[10] = be32_to_cpu(M[10]);
122
96.5k
    W[11] = be32_to_cpu(M[11]);
123
96.5k
    W[12] = be32_to_cpu(M[12]);
124
96.5k
    W[13] = be32_to_cpu(M[13]);
125
96.5k
    W[14] = be32_to_cpu(M[14]);
126
96.5k
    W[15] = be32_to_cpu(M[15]);
127
96.5k
    TEMP = W[13] ^ W[8] ^ W[2] ^ W[0];
128
96.5k
    W[16] = S1(TEMP);
129
96.5k
    TEMP = W[14] ^ W[9] ^ W[3] ^ W[1];
130
96.5k
    W[17] = S1(TEMP);
131
96.5k
    TEMP = W[15] ^ W[10] ^ W[4] ^ W[2];
132
96.5k
    W[18] = S1(TEMP);
133
96.5k
    TEMP = W[16] ^ W[11] ^ W[5] ^ W[3];
134
96.5k
    W[19] = S1(TEMP);
135
96.5k
    TEMP = W[17] ^ W[12] ^ W[6] ^ W[4];
136
96.5k
    W[20] = S1(TEMP);
137
96.5k
    TEMP = W[18] ^ W[13] ^ W[7] ^ W[5];
138
96.5k
    W[21] = S1(TEMP);
139
96.5k
    TEMP = W[19] ^ W[14] ^ W[8] ^ W[6];
140
96.5k
    W[22] = S1(TEMP);
141
96.5k
    TEMP = W[20] ^ W[15] ^ W[9] ^ W[7];
142
96.5k
    W[23] = S1(TEMP);
143
96.5k
    TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];
144
96.5k
    W[24] = S1(TEMP);
145
96.5k
    TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];
146
96.5k
    W[25] = S1(TEMP);
147
96.5k
    TEMP = W[23] ^ W[18] ^ W[12] ^ W[10];
148
96.5k
    W[26] = S1(TEMP);
149
96.5k
    TEMP = W[24] ^ W[19] ^ W[13] ^ W[11];
150
96.5k
    W[27] = S1(TEMP);
151
96.5k
    TEMP = W[25] ^ W[20] ^ W[14] ^ W[12];
152
96.5k
    W[28] = S1(TEMP);
153
96.5k
    TEMP = W[26] ^ W[21] ^ W[15] ^ W[13];
154
96.5k
    W[29] = S1(TEMP);
155
96.5k
    TEMP = W[27] ^ W[22] ^ W[16] ^ W[14];
156
96.5k
    W[30] = S1(TEMP);
157
96.5k
    TEMP = W[28] ^ W[23] ^ W[17] ^ W[15];
158
96.5k
    W[31] = S1(TEMP);
159
160
    /* process the remainder of the array */
161
4.73M
    for (t = 32; t < 80; t++) {
162
4.63M
        TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
163
4.63M
        W[t] = S1(TEMP);
164
4.63M
    }
165
166
96.5k
    A = H0;
167
96.5k
    B = H1;
168
96.5k
    C = H2;
169
96.5k
    D = H3;
170
96.5k
    E = H4;
171
172
2.02M
    for (t = 0; t < 20; t++) {
173
1.93M
        TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
174
1.93M
        E = D;
175
1.93M
        D = C;
176
1.93M
        C = S30(B);
177
1.93M
        B = A;
178
1.93M
        A = TEMP;
179
1.93M
    }
180
2.02M
    for (; t < 40; t++) {
181
1.93M
        TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
182
1.93M
        E = D;
183
1.93M
        D = C;
184
1.93M
        C = S30(B);
185
1.93M
        B = A;
186
1.93M
        A = TEMP;
187
1.93M
    }
188
2.02M
    for (; t < 60; t++) {
189
1.93M
        TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
190
1.93M
        E = D;
191
1.93M
        D = C;
192
1.93M
        C = S30(B);
193
1.93M
        B = A;
194
1.93M
        A = TEMP;
195
1.93M
    }
196
2.02M
    for (; t < 80; t++) {
197
1.93M
        TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
198
1.93M
        E = D;
199
1.93M
        D = C;
200
1.93M
        C = S30(B);
201
1.93M
        B = A;
202
1.93M
        A = TEMP;
203
1.93M
    }
204
205
96.5k
    hash_value[0] = H0 + A;
206
96.5k
    hash_value[1] = H1 + B;
207
96.5k
    hash_value[2] = H2 + C;
208
96.5k
    hash_value[3] = H3 + D;
209
96.5k
    hash_value[4] = H4 + E;
210
211
96.5k
    return;
212
96.5k
}
213
214
void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
215
36.2k
{
216
    /* initialize state vector */
217
36.2k
    ctx->H[0] = 0x67452301;
218
36.2k
    ctx->H[1] = 0xefcdab89;
219
36.2k
    ctx->H[2] = 0x98badcfe;
220
36.2k
    ctx->H[3] = 0x10325476;
221
36.2k
    ctx->H[4] = 0xc3d2e1f0;
222
223
    /* indicate that message buffer is empty */
224
36.2k
    ctx->octets_in_buffer = 0;
225
226
    /* reset message bit-count to zero */
227
36.2k
    ctx->num_bits_in_msg = 0;
228
36.2k
}
229
230
void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
231
                      const uint8_t *msg,
232
                      size_t octets_in_msg)
233
67.9k
{
234
67.9k
    size_t i;
235
67.9k
    uint8_t *buf = (uint8_t *)ctx->M;
236
237
    /* update message bit-count */
238
67.9k
    ctx->num_bits_in_msg += (uint32_t)octets_in_msg * 8;
239
240
    /* loop over 16-word blocks of M */
241
195k
    while (octets_in_msg > 0) {
242
127k
        if (octets_in_msg + ctx->octets_in_buffer >= 64) {
243
            /*
244
             * copy words of M into msg buffer until that buffer is full,
245
             * converting them into host byte order as needed
246
             */
247
96.5k
            octets_in_msg -= (64 - ctx->octets_in_buffer);
248
6.26M
            for (i = ctx->octets_in_buffer; i < 64; i++) {
249
6.16M
                buf[i] = *msg++;
250
6.16M
            }
251
96.5k
            ctx->octets_in_buffer = 0;
252
253
            /* process a whole block */
254
255
96.5k
            debug_print0(srtp_mod_sha1, "(update) running srtp_sha1_core()");
256
257
96.5k
            srtp_sha1_core(ctx->M, ctx->H);
258
259
96.5k
        } else {
260
31.2k
            debug_print0(srtp_mod_sha1,
261
31.2k
                         "(update) not running srtp_sha1_core()");
262
263
31.2k
            for (i = ctx->octets_in_buffer;
264
708k
                 i < (ctx->octets_in_buffer + octets_in_msg); i++) {
265
676k
                buf[i] = *msg++;
266
676k
            }
267
31.2k
            ctx->octets_in_buffer += octets_in_msg;
268
31.2k
            octets_in_msg = 0;
269
31.2k
        }
270
127k
    }
271
67.9k
}
272
273
/*
274
 * srtp_sha1_final(ctx, output) computes the result for ctx and copies it
275
 * into the twenty octets located at *output
276
 */
277
278
void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t output[5])
279
28.2k
{
280
28.2k
    uint32_t A, B, C, D, E, TEMP;
281
28.2k
    uint32_t W[80];
282
28.2k
    size_t i, t;
283
284
    /*
285
     * process the remaining octets_in_buffer, padding and terminating as
286
     * necessary
287
     */
288
28.2k
    {
289
28.2k
        size_t tail = ctx->octets_in_buffer % 4;
290
291
        /* copy/xor message into array */
292
196k
        for (i = 0; i < (ctx->octets_in_buffer + 3) / 4; i++) {
293
168k
            W[i] = be32_to_cpu(ctx->M[i]);
294
168k
        }
295
296
        /* set the high bit of the octet immediately following the message */
297
28.2k
        switch (tail) {
298
3.04k
        case (3):
299
3.04k
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffffff00) | 0x80;
300
3.04k
            W[i] = 0x0;
301
3.04k
            break;
302
2.24k
        case (2):
303
2.24k
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffff0000) | 0x8000;
304
2.24k
            W[i] = 0x0;
305
2.24k
            break;
306
964
        case (1):
307
964
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xff000000) | 0x800000;
308
964
            W[i] = 0x0;
309
964
            break;
310
22.0k
        case (0):
311
22.0k
            W[i] = 0x80000000;
312
22.0k
            break;
313
28.2k
        }
314
315
        /* zeroize remaining words */
316
258k
        for (i++; i < 15; i++) {
317
230k
            W[i] = 0x0;
318
230k
        }
319
320
        /*
321
         * if there is room at the end of the word array, then set the
322
         * last word to the bit-length of the message; otherwise, set that
323
         * word to zero and then we need to do one more run of the
324
         * compression algo.
325
         */
326
28.2k
        if (ctx->octets_in_buffer < 56) {
327
24.9k
            W[15] = ctx->num_bits_in_msg;
328
24.9k
        } else if (ctx->octets_in_buffer < 60) {
329
2.87k
            W[15] = 0x0;
330
2.87k
        }
331
332
        /* process the word array */
333
1.83M
        for (t = 16; t < 80; t++) {
334
1.80M
            TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
335
1.80M
            W[t] = S1(TEMP);
336
1.80M
        }
337
338
28.2k
        A = ctx->H[0];
339
28.2k
        B = ctx->H[1];
340
28.2k
        C = ctx->H[2];
341
28.2k
        D = ctx->H[3];
342
28.2k
        E = ctx->H[4];
343
344
593k
        for (t = 0; t < 20; t++) {
345
565k
            TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
346
565k
            E = D;
347
565k
            D = C;
348
565k
            C = S30(B);
349
565k
            B = A;
350
565k
            A = TEMP;
351
565k
        }
352
593k
        for (; t < 40; t++) {
353
565k
            TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
354
565k
            E = D;
355
565k
            D = C;
356
565k
            C = S30(B);
357
565k
            B = A;
358
565k
            A = TEMP;
359
565k
        }
360
593k
        for (; t < 60; t++) {
361
565k
            TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
362
565k
            E = D;
363
565k
            D = C;
364
565k
            C = S30(B);
365
565k
            B = A;
366
565k
            A = TEMP;
367
565k
        }
368
593k
        for (; t < 80; t++) {
369
565k
            TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
370
565k
            E = D;
371
565k
            D = C;
372
565k
            C = S30(B);
373
565k
            B = A;
374
565k
            A = TEMP;
375
565k
        }
376
377
28.2k
        ctx->H[0] += A;
378
28.2k
        ctx->H[1] += B;
379
28.2k
        ctx->H[2] += C;
380
28.2k
        ctx->H[3] += D;
381
28.2k
        ctx->H[4] += E;
382
28.2k
    }
383
384
28.2k
    debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core()");
385
386
28.2k
    if (ctx->octets_in_buffer >= 56) {
387
3.29k
        debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core() again");
388
389
        /* we need to do one final run of the compression algo */
390
391
        /*
392
         * set initial part of word array to zeros, and set the
393
         * final part to the number of bits in the message
394
         */
395
52.6k
        for (i = 0; i < 15; i++) {
396
49.3k
            W[i] = 0x0;
397
49.3k
        }
398
3.29k
        W[15] = ctx->num_bits_in_msg;
399
400
        /* process the word array */
401
213k
        for (t = 16; t < 80; t++) {
402
210k
            TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
403
210k
            W[t] = S1(TEMP);
404
210k
        }
405
406
3.29k
        A = ctx->H[0];
407
3.29k
        B = ctx->H[1];
408
3.29k
        C = ctx->H[2];
409
3.29k
        D = ctx->H[3];
410
3.29k
        E = ctx->H[4];
411
412
69.1k
        for (t = 0; t < 20; t++) {
413
65.8k
            TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
414
65.8k
            E = D;
415
65.8k
            D = C;
416
65.8k
            C = S30(B);
417
65.8k
            B = A;
418
65.8k
            A = TEMP;
419
65.8k
        }
420
69.1k
        for (; t < 40; t++) {
421
65.8k
            TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
422
65.8k
            E = D;
423
65.8k
            D = C;
424
65.8k
            C = S30(B);
425
65.8k
            B = A;
426
65.8k
            A = TEMP;
427
65.8k
        }
428
69.1k
        for (; t < 60; t++) {
429
65.8k
            TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
430
65.8k
            E = D;
431
65.8k
            D = C;
432
65.8k
            C = S30(B);
433
65.8k
            B = A;
434
65.8k
            A = TEMP;
435
65.8k
        }
436
69.1k
        for (; t < 80; t++) {
437
65.8k
            TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
438
65.8k
            E = D;
439
65.8k
            D = C;
440
65.8k
            C = S30(B);
441
65.8k
            B = A;
442
65.8k
            A = TEMP;
443
65.8k
        }
444
445
3.29k
        ctx->H[0] += A;
446
3.29k
        ctx->H[1] += B;
447
3.29k
        ctx->H[2] += C;
448
3.29k
        ctx->H[3] += D;
449
3.29k
        ctx->H[4] += E;
450
3.29k
    }
451
452
    /* copy result into output buffer */
453
28.2k
    output[0] = be32_to_cpu(ctx->H[0]);
454
28.2k
    output[1] = be32_to_cpu(ctx->H[1]);
455
28.2k
    output[2] = be32_to_cpu(ctx->H[2]);
456
28.2k
    output[3] = be32_to_cpu(ctx->H[3]);
457
28.2k
    output[4] = be32_to_cpu(ctx->H[4]);
458
459
    /* indicate that message buffer in context is empty */
460
28.2k
    ctx->octets_in_buffer = 0;
461
462
28.2k
    return;
463
28.2k
}