Coverage Report

Created: 2026-01-17 06:52

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
11.5M
#define S1(X) ((X << 1) | (X >> 31))
60
14.4M
#define S5(X) ((X << 5) | (X >> 27))
61
14.4M
#define S30(X) ((X << 30) | (X >> 2))
62
63
3.60M
#define f0(B, C, D) ((B & C) | (~B & D))
64
3.60M
#define f1(B, C, D) (B ^ C ^ D)
65
3.60M
#define f2(B, C, D) ((B & C) | (B & D) | (C & D))
66
3.60M
#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
124k
{
93
124k
    uint32_t H0;
94
124k
    uint32_t H1;
95
124k
    uint32_t H2;
96
124k
    uint32_t H3;
97
124k
    uint32_t H4;
98
124k
    uint32_t W[80];
99
124k
    uint32_t A, B, C, D, E, TEMP;
100
124k
    size_t t;
101
102
    /* copy hash_value into H0, H1, H2, H3, H4 */
103
124k
    H0 = hash_value[0];
104
124k
    H1 = hash_value[1];
105
124k
    H2 = hash_value[2];
106
124k
    H3 = hash_value[3];
107
124k
    H4 = hash_value[4];
108
109
    /* copy/xor message into array */
110
111
124k
    W[0] = be32_to_cpu(M[0]);
112
124k
    W[1] = be32_to_cpu(M[1]);
113
124k
    W[2] = be32_to_cpu(M[2]);
114
124k
    W[3] = be32_to_cpu(M[3]);
115
124k
    W[4] = be32_to_cpu(M[4]);
116
124k
    W[5] = be32_to_cpu(M[5]);
117
124k
    W[6] = be32_to_cpu(M[6]);
118
124k
    W[7] = be32_to_cpu(M[7]);
119
124k
    W[8] = be32_to_cpu(M[8]);
120
124k
    W[9] = be32_to_cpu(M[9]);
121
124k
    W[10] = be32_to_cpu(M[10]);
122
124k
    W[11] = be32_to_cpu(M[11]);
123
124k
    W[12] = be32_to_cpu(M[12]);
124
124k
    W[13] = be32_to_cpu(M[13]);
125
124k
    W[14] = be32_to_cpu(M[14]);
126
124k
    W[15] = be32_to_cpu(M[15]);
127
124k
    TEMP = W[13] ^ W[8] ^ W[2] ^ W[0];
128
124k
    W[16] = S1(TEMP);
129
124k
    TEMP = W[14] ^ W[9] ^ W[3] ^ W[1];
130
124k
    W[17] = S1(TEMP);
131
124k
    TEMP = W[15] ^ W[10] ^ W[4] ^ W[2];
132
124k
    W[18] = S1(TEMP);
133
124k
    TEMP = W[16] ^ W[11] ^ W[5] ^ W[3];
134
124k
    W[19] = S1(TEMP);
135
124k
    TEMP = W[17] ^ W[12] ^ W[6] ^ W[4];
136
124k
    W[20] = S1(TEMP);
137
124k
    TEMP = W[18] ^ W[13] ^ W[7] ^ W[5];
138
124k
    W[21] = S1(TEMP);
139
124k
    TEMP = W[19] ^ W[14] ^ W[8] ^ W[6];
140
124k
    W[22] = S1(TEMP);
141
124k
    TEMP = W[20] ^ W[15] ^ W[9] ^ W[7];
142
124k
    W[23] = S1(TEMP);
143
124k
    TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];
144
124k
    W[24] = S1(TEMP);
145
124k
    TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];
146
124k
    W[25] = S1(TEMP);
147
124k
    TEMP = W[23] ^ W[18] ^ W[12] ^ W[10];
148
124k
    W[26] = S1(TEMP);
149
124k
    TEMP = W[24] ^ W[19] ^ W[13] ^ W[11];
150
124k
    W[27] = S1(TEMP);
151
124k
    TEMP = W[25] ^ W[20] ^ W[14] ^ W[12];
152
124k
    W[28] = S1(TEMP);
153
124k
    TEMP = W[26] ^ W[21] ^ W[15] ^ W[13];
154
124k
    W[29] = S1(TEMP);
155
124k
    TEMP = W[27] ^ W[22] ^ W[16] ^ W[14];
156
124k
    W[30] = S1(TEMP);
157
124k
    TEMP = W[28] ^ W[23] ^ W[17] ^ W[15];
158
124k
    W[31] = S1(TEMP);
159
160
    /* process the remainder of the array */
161
6.09M
    for (t = 32; t < 80; t++) {
162
5.97M
        TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
163
5.97M
        W[t] = S1(TEMP);
164
5.97M
    }
165
166
124k
    A = H0;
167
124k
    B = H1;
168
124k
    C = H2;
169
124k
    D = H3;
170
124k
    E = H4;
171
172
2.61M
    for (t = 0; t < 20; t++) {
173
2.48M
        TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
174
2.48M
        E = D;
175
2.48M
        D = C;
176
2.48M
        C = S30(B);
177
2.48M
        B = A;
178
2.48M
        A = TEMP;
179
2.48M
    }
180
2.61M
    for (; t < 40; t++) {
181
2.48M
        TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
182
2.48M
        E = D;
183
2.48M
        D = C;
184
2.48M
        C = S30(B);
185
2.48M
        B = A;
186
2.48M
        A = TEMP;
187
2.48M
    }
188
2.61M
    for (; t < 60; t++) {
189
2.48M
        TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
190
2.48M
        E = D;
191
2.48M
        D = C;
192
2.48M
        C = S30(B);
193
2.48M
        B = A;
194
2.48M
        A = TEMP;
195
2.48M
    }
196
2.61M
    for (; t < 80; t++) {
197
2.48M
        TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
198
2.48M
        E = D;
199
2.48M
        D = C;
200
2.48M
        C = S30(B);
201
2.48M
        B = A;
202
2.48M
        A = TEMP;
203
2.48M
    }
204
205
124k
    hash_value[0] = H0 + A;
206
124k
    hash_value[1] = H1 + B;
207
124k
    hash_value[2] = H2 + C;
208
124k
    hash_value[3] = H3 + D;
209
124k
    hash_value[4] = H4 + E;
210
211
124k
    return;
212
124k
}
213
214
void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
215
31.6k
{
216
    /* initialize state vector */
217
31.6k
    ctx->H[0] = 0x67452301;
218
31.6k
    ctx->H[1] = 0xefcdab89;
219
31.6k
    ctx->H[2] = 0x98badcfe;
220
31.6k
    ctx->H[3] = 0x10325476;
221
31.6k
    ctx->H[4] = 0xc3d2e1f0;
222
223
    /* indicate that message buffer is empty */
224
31.6k
    ctx->octets_in_buffer = 0;
225
226
    /* reset message bit-count to zero */
227
31.6k
    ctx->num_bits_in_msg = 0;
228
31.6k
}
229
230
void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
231
                      const uint8_t *msg,
232
                      size_t octets_in_msg)
233
104k
{
234
104k
    size_t i;
235
104k
    uint8_t *buf = (uint8_t *)ctx->M;
236
237
    /* update message bit-count */
238
104k
    ctx->num_bits_in_msg += (uint32_t)octets_in_msg * 8;
239
240
    /* loop over 16-word blocks of M */
241
301k
    while (octets_in_msg > 0) {
242
196k
        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
124k
            octets_in_msg -= (64 - ctx->octets_in_buffer);
248
8.07M
            for (i = ctx->octets_in_buffer; i < 64; i++) {
249
7.94M
                buf[i] = *msg++;
250
7.94M
            }
251
124k
            ctx->octets_in_buffer = 0;
252
253
            /* process a whole block */
254
255
124k
            debug_print0(srtp_mod_sha1, "(update) running srtp_sha1_core()");
256
257
124k
            srtp_sha1_core(ctx->M, ctx->H);
258
259
124k
        } else {
260
72.3k
            debug_print0(srtp_mod_sha1,
261
72.3k
                         "(update) not running srtp_sha1_core()");
262
263
72.3k
            for (i = ctx->octets_in_buffer;
264
1.27M
                 i < (ctx->octets_in_buffer + octets_in_msg); i++) {
265
1.20M
                buf[i] = *msg++;
266
1.20M
            }
267
72.3k
            ctx->octets_in_buffer += octets_in_msg;
268
72.3k
            octets_in_msg = 0;
269
72.3k
        }
270
196k
    }
271
104k
}
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
53.4k
{
280
53.4k
    uint32_t A, B, C, D, E, TEMP;
281
53.4k
    uint32_t W[80];
282
53.4k
    size_t i, t;
283
284
    /*
285
     * process the remaining octets_in_buffer, padding and terminating as
286
     * necessary
287
     */
288
53.4k
    {
289
53.4k
        size_t tail = ctx->octets_in_buffer % 4;
290
291
        /* copy/xor message into array */
292
356k
        for (i = 0; i < (ctx->octets_in_buffer + 3) / 4; i++) {
293
303k
            W[i] = be32_to_cpu(ctx->M[i]);
294
303k
        }
295
296
        /* set the high bit of the octet immediately following the message */
297
53.4k
        switch (tail) {
298
0
        case (3):
299
0
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffffff00) | 0x80;
300
0
            W[i] = 0x0;
301
0
            break;
302
11.8k
        case (2):
303
11.8k
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffff0000) | 0x8000;
304
11.8k
            W[i] = 0x0;
305
11.8k
            break;
306
0
        case (1):
307
0
            W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xff000000) | 0x800000;
308
0
            W[i] = 0x0;
309
0
            break;
310
41.6k
        case (0):
311
41.6k
            W[i] = 0x80000000;
312
41.6k
            break;
313
53.4k
        }
314
315
        /* zeroize remaining words */
316
499k
        for (i++; i < 15; i++) {
317
446k
            W[i] = 0x0;
318
446k
        }
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
53.4k
        if (ctx->octets_in_buffer < 56) {
327
51.0k
            W[15] = ctx->num_bits_in_msg;
328
51.0k
        } else if (ctx->octets_in_buffer < 60) {
329
1.63k
            W[15] = 0x0;
330
1.63k
        }
331
332
        /* process the word array */
333
3.47M
        for (t = 16; t < 80; t++) {
334
3.42M
            TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
335
3.42M
            W[t] = S1(TEMP);
336
3.42M
        }
337
338
53.4k
        A = ctx->H[0];
339
53.4k
        B = ctx->H[1];
340
53.4k
        C = ctx->H[2];
341
53.4k
        D = ctx->H[3];
342
53.4k
        E = ctx->H[4];
343
344
1.12M
        for (t = 0; t < 20; t++) {
345
1.06M
            TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
346
1.06M
            E = D;
347
1.06M
            D = C;
348
1.06M
            C = S30(B);
349
1.06M
            B = A;
350
1.06M
            A = TEMP;
351
1.06M
        }
352
1.12M
        for (; t < 40; t++) {
353
1.06M
            TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
354
1.06M
            E = D;
355
1.06M
            D = C;
356
1.06M
            C = S30(B);
357
1.06M
            B = A;
358
1.06M
            A = TEMP;
359
1.06M
        }
360
1.12M
        for (; t < 60; t++) {
361
1.06M
            TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
362
1.06M
            E = D;
363
1.06M
            D = C;
364
1.06M
            C = S30(B);
365
1.06M
            B = A;
366
1.06M
            A = TEMP;
367
1.06M
        }
368
1.12M
        for (; t < 80; t++) {
369
1.06M
            TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
370
1.06M
            E = D;
371
1.06M
            D = C;
372
1.06M
            C = S30(B);
373
1.06M
            B = A;
374
1.06M
            A = TEMP;
375
1.06M
        }
376
377
53.4k
        ctx->H[0] += A;
378
53.4k
        ctx->H[1] += B;
379
53.4k
        ctx->H[2] += C;
380
53.4k
        ctx->H[3] += D;
381
53.4k
        ctx->H[4] += E;
382
53.4k
    }
383
384
53.4k
    debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core()");
385
386
53.4k
    if (ctx->octets_in_buffer >= 56) {
387
2.37k
        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
37.9k
        for (i = 0; i < 15; i++) {
396
35.5k
            W[i] = 0x0;
397
35.5k
        }
398
2.37k
        W[15] = ctx->num_bits_in_msg;
399
400
        /* process the word array */
401
154k
        for (t = 16; t < 80; t++) {
402
151k
            TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
403
151k
            W[t] = S1(TEMP);
404
151k
        }
405
406
2.37k
        A = ctx->H[0];
407
2.37k
        B = ctx->H[1];
408
2.37k
        C = ctx->H[2];
409
2.37k
        D = ctx->H[3];
410
2.37k
        E = ctx->H[4];
411
412
49.7k
        for (t = 0; t < 20; t++) {
413
47.4k
            TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
414
47.4k
            E = D;
415
47.4k
            D = C;
416
47.4k
            C = S30(B);
417
47.4k
            B = A;
418
47.4k
            A = TEMP;
419
47.4k
        }
420
49.7k
        for (; t < 40; t++) {
421
47.4k
            TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
422
47.4k
            E = D;
423
47.4k
            D = C;
424
47.4k
            C = S30(B);
425
47.4k
            B = A;
426
47.4k
            A = TEMP;
427
47.4k
        }
428
49.7k
        for (; t < 60; t++) {
429
47.4k
            TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
430
47.4k
            E = D;
431
47.4k
            D = C;
432
47.4k
            C = S30(B);
433
47.4k
            B = A;
434
47.4k
            A = TEMP;
435
47.4k
        }
436
49.7k
        for (; t < 80; t++) {
437
47.4k
            TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
438
47.4k
            E = D;
439
47.4k
            D = C;
440
47.4k
            C = S30(B);
441
47.4k
            B = A;
442
47.4k
            A = TEMP;
443
47.4k
        }
444
445
2.37k
        ctx->H[0] += A;
446
2.37k
        ctx->H[1] += B;
447
2.37k
        ctx->H[2] += C;
448
2.37k
        ctx->H[3] += D;
449
2.37k
        ctx->H[4] += E;
450
2.37k
    }
451
452
    /* copy result into output buffer */
453
53.4k
    output[0] = be32_to_cpu(ctx->H[0]);
454
53.4k
    output[1] = be32_to_cpu(ctx->H[1]);
455
53.4k
    output[2] = be32_to_cpu(ctx->H[2]);
456
53.4k
    output[3] = be32_to_cpu(ctx->H[3]);
457
53.4k
    output[4] = be32_to_cpu(ctx->H[4]);
458
459
    /* indicate that message buffer in context is empty */
460
53.4k
    ctx->octets_in_buffer = 0;
461
462
53.4k
    return;
463
53.4k
}