• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     0,      /* debugging is off by default */
55     "sha-1" /* printable module name       */
56 };
57 
58 /* SN == Rotate left N bits */
59 #define S1(X) ((X << 1) | (X >> 31))
60 #define S5(X) ((X << 5) | (X >> 27))
61 #define S30(X) ((X << 30) | (X >> 2))
62 
63 #define f0(B, C, D) ((B & C) | (~B & D))
64 #define f1(B, C, D) (B ^ C ^ D)
65 #define f2(B, C, D) ((B & C) | (B & D) | (C & D))
66 #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 
srtp_sha1(const uint8_t * msg,int octets_in_msg,uint32_t hash_value[5])79 void srtp_sha1(const uint8_t *msg, int octets_in_msg, uint32_t hash_value[5])
80 {
81     srtp_sha1_ctx_t ctx;
82 
83     srtp_sha1_init(&ctx);
84     srtp_sha1_update(&ctx, msg, octets_in_msg);
85     srtp_sha1_final(&ctx, hash_value);
86 }
87 
88 /*
89  *  srtp_sha1_core(M, H) computes the core compression function, where M is
90  *  the next part of the message (in network byte order) and H is the
91  *  intermediate state { H0, H1, ...} (in host byte order)
92  *
93  *  this function does not do any of the padding required in the
94  *  complete SHA1 function
95  *
96  *  this function is used in the SEAL 3.0 key setup routines
97  *  (crypto/cipher/seal.c)
98  */
99 
srtp_sha1_core(const uint32_t M[16],uint32_t hash_value[5])100 void srtp_sha1_core(const uint32_t M[16], uint32_t hash_value[5])
101 {
102     uint32_t H0;
103     uint32_t H1;
104     uint32_t H2;
105     uint32_t H3;
106     uint32_t H4;
107     uint32_t W[80];
108     uint32_t A, B, C, D, E, TEMP;
109     int t;
110 
111     /* copy hash_value into H0, H1, H2, H3, H4 */
112     H0 = hash_value[0];
113     H1 = hash_value[1];
114     H2 = hash_value[2];
115     H3 = hash_value[3];
116     H4 = hash_value[4];
117 
118     /* copy/xor message into array */
119 
120     W[0] = be32_to_cpu(M[0]);
121     W[1] = be32_to_cpu(M[1]);
122     W[2] = be32_to_cpu(M[2]);
123     W[3] = be32_to_cpu(M[3]);
124     W[4] = be32_to_cpu(M[4]);
125     W[5] = be32_to_cpu(M[5]);
126     W[6] = be32_to_cpu(M[6]);
127     W[7] = be32_to_cpu(M[7]);
128     W[8] = be32_to_cpu(M[8]);
129     W[9] = be32_to_cpu(M[9]);
130     W[10] = be32_to_cpu(M[10]);
131     W[11] = be32_to_cpu(M[11]);
132     W[12] = be32_to_cpu(M[12]);
133     W[13] = be32_to_cpu(M[13]);
134     W[14] = be32_to_cpu(M[14]);
135     W[15] = be32_to_cpu(M[15]);
136     TEMP = W[13] ^ W[8] ^ W[2] ^ W[0];
137     W[16] = S1(TEMP);
138     TEMP = W[14] ^ W[9] ^ W[3] ^ W[1];
139     W[17] = S1(TEMP);
140     TEMP = W[15] ^ W[10] ^ W[4] ^ W[2];
141     W[18] = S1(TEMP);
142     TEMP = W[16] ^ W[11] ^ W[5] ^ W[3];
143     W[19] = S1(TEMP);
144     TEMP = W[17] ^ W[12] ^ W[6] ^ W[4];
145     W[20] = S1(TEMP);
146     TEMP = W[18] ^ W[13] ^ W[7] ^ W[5];
147     W[21] = S1(TEMP);
148     TEMP = W[19] ^ W[14] ^ W[8] ^ W[6];
149     W[22] = S1(TEMP);
150     TEMP = W[20] ^ W[15] ^ W[9] ^ W[7];
151     W[23] = S1(TEMP);
152     TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];
153     W[24] = S1(TEMP);
154     TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];
155     W[25] = S1(TEMP);
156     TEMP = W[23] ^ W[18] ^ W[12] ^ W[10];
157     W[26] = S1(TEMP);
158     TEMP = W[24] ^ W[19] ^ W[13] ^ W[11];
159     W[27] = S1(TEMP);
160     TEMP = W[25] ^ W[20] ^ W[14] ^ W[12];
161     W[28] = S1(TEMP);
162     TEMP = W[26] ^ W[21] ^ W[15] ^ W[13];
163     W[29] = S1(TEMP);
164     TEMP = W[27] ^ W[22] ^ W[16] ^ W[14];
165     W[30] = S1(TEMP);
166     TEMP = W[28] ^ W[23] ^ W[17] ^ W[15];
167     W[31] = S1(TEMP);
168 
169     /* process the remainder of the array */
170     for (t = 32; t < 80; t++) {
171         TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
172         W[t] = S1(TEMP);
173     }
174 
175     A = H0;
176     B = H1;
177     C = H2;
178     D = H3;
179     E = H4;
180 
181     for (t = 0; t < 20; t++) {
182         TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
183         E = D;
184         D = C;
185         C = S30(B);
186         B = A;
187         A = TEMP;
188     }
189     for (; t < 40; t++) {
190         TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
191         E = D;
192         D = C;
193         C = S30(B);
194         B = A;
195         A = TEMP;
196     }
197     for (; t < 60; t++) {
198         TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
199         E = D;
200         D = C;
201         C = S30(B);
202         B = A;
203         A = TEMP;
204     }
205     for (; t < 80; t++) {
206         TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
207         E = D;
208         D = C;
209         C = S30(B);
210         B = A;
211         A = TEMP;
212     }
213 
214     hash_value[0] = H0 + A;
215     hash_value[1] = H1 + B;
216     hash_value[2] = H2 + C;
217     hash_value[3] = H3 + D;
218     hash_value[4] = H4 + E;
219 
220     return;
221 }
222 
srtp_sha1_init(srtp_sha1_ctx_t * ctx)223 void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
224 {
225     /* initialize state vector */
226     ctx->H[0] = 0x67452301;
227     ctx->H[1] = 0xefcdab89;
228     ctx->H[2] = 0x98badcfe;
229     ctx->H[3] = 0x10325476;
230     ctx->H[4] = 0xc3d2e1f0;
231 
232     /* indicate that message buffer is empty */
233     ctx->octets_in_buffer = 0;
234 
235     /* reset message bit-count to zero */
236     ctx->num_bits_in_msg = 0;
237 }
238 
srtp_sha1_update(srtp_sha1_ctx_t * ctx,const uint8_t * msg,int octets_in_msg)239 void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
240                       const uint8_t *msg,
241                       int octets_in_msg)
242 {
243     int i;
244     uint8_t *buf = (uint8_t *)ctx->M;
245 
246     /* update message bit-count */
247     ctx->num_bits_in_msg += octets_in_msg * 8;
248 
249     /* loop over 16-word blocks of M */
250     while (octets_in_msg > 0) {
251         if (octets_in_msg + ctx->octets_in_buffer >= 64) {
252             /*
253              * copy words of M into msg buffer until that buffer is full,
254              * converting them into host byte order as needed
255              */
256             octets_in_msg -= (64 - ctx->octets_in_buffer);
257             for (i = ctx->octets_in_buffer; i < 64; i++) {
258                 buf[i] = *msg++;
259             }
260             ctx->octets_in_buffer = 0;
261 
262             /* process a whole block */
263 
264             debug_print(srtp_mod_sha1, "(update) running srtp_sha1_core()",
265                         NULL);
266 
267             srtp_sha1_core(ctx->M, ctx->H);
268 
269         } else {
270             debug_print(srtp_mod_sha1, "(update) not running srtp_sha1_core()",
271                         NULL);
272 
273             for (i = ctx->octets_in_buffer;
274                  i < (ctx->octets_in_buffer + octets_in_msg); i++) {
275                 buf[i] = *msg++;
276             }
277             ctx->octets_in_buffer += octets_in_msg;
278             octets_in_msg = 0;
279         }
280     }
281 }
282 
283 /*
284  * srtp_sha1_final(ctx, output) computes the result for ctx and copies it
285  * into the twenty octets located at *output
286  */
287 
srtp_sha1_final(srtp_sha1_ctx_t * ctx,uint32_t * output)288 void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t *output)
289 {
290     uint32_t A, B, C, D, E, TEMP;
291     uint32_t W[80];
292     int i, t;
293 
294     /*
295      * process the remaining octets_in_buffer, padding and terminating as
296      * necessary
297      */
298     {
299         int tail = ctx->octets_in_buffer % 4;
300 
301         /* copy/xor message into array */
302         for (i = 0; i < (ctx->octets_in_buffer + 3) / 4; i++) {
303             W[i] = be32_to_cpu(ctx->M[i]);
304         }
305 
306         /* set the high bit of the octet immediately following the message */
307         switch (tail) {
308         case (3):
309             W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffffff00) | 0x80;
310             W[i] = 0x0;
311             break;
312         case (2):
313             W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffff0000) | 0x8000;
314             W[i] = 0x0;
315             break;
316         case (1):
317             W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xff000000) | 0x800000;
318             W[i] = 0x0;
319             break;
320         case (0):
321             W[i] = 0x80000000;
322             break;
323         }
324 
325         /* zeroize remaining words */
326         for (i++; i < 15; i++) {
327             W[i] = 0x0;
328         }
329 
330         /*
331          * if there is room at the end of the word array, then set the
332          * last word to the bit-length of the message; otherwise, set that
333          * word to zero and then we need to do one more run of the
334          * compression algo.
335          */
336         if (ctx->octets_in_buffer < 56) {
337             W[15] = ctx->num_bits_in_msg;
338         } else if (ctx->octets_in_buffer < 60) {
339             W[15] = 0x0;
340         }
341 
342         /* process the word array */
343         for (t = 16; t < 80; t++) {
344             TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
345             W[t] = S1(TEMP);
346         }
347 
348         A = ctx->H[0];
349         B = ctx->H[1];
350         C = ctx->H[2];
351         D = ctx->H[3];
352         E = ctx->H[4];
353 
354         for (t = 0; t < 20; t++) {
355             TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
356             E = D;
357             D = C;
358             C = S30(B);
359             B = A;
360             A = TEMP;
361         }
362         for (; t < 40; t++) {
363             TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
364             E = D;
365             D = C;
366             C = S30(B);
367             B = A;
368             A = TEMP;
369         }
370         for (; t < 60; t++) {
371             TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
372             E = D;
373             D = C;
374             C = S30(B);
375             B = A;
376             A = TEMP;
377         }
378         for (; t < 80; t++) {
379             TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
380             E = D;
381             D = C;
382             C = S30(B);
383             B = A;
384             A = TEMP;
385         }
386 
387         ctx->H[0] += A;
388         ctx->H[1] += B;
389         ctx->H[2] += C;
390         ctx->H[3] += D;
391         ctx->H[4] += E;
392     }
393 
394     debug_print(srtp_mod_sha1, "(final) running srtp_sha1_core()", NULL);
395 
396     if (ctx->octets_in_buffer >= 56) {
397         debug_print(srtp_mod_sha1, "(final) running srtp_sha1_core() again",
398                     NULL);
399 
400         /* we need to do one final run of the compression algo */
401 
402         /*
403          * set initial part of word array to zeros, and set the
404          * final part to the number of bits in the message
405          */
406         for (i = 0; i < 15; i++) {
407             W[i] = 0x0;
408         }
409         W[15] = ctx->num_bits_in_msg;
410 
411         /* process the word array */
412         for (t = 16; t < 80; t++) {
413             TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
414             W[t] = S1(TEMP);
415         }
416 
417         A = ctx->H[0];
418         B = ctx->H[1];
419         C = ctx->H[2];
420         D = ctx->H[3];
421         E = ctx->H[4];
422 
423         for (t = 0; t < 20; t++) {
424             TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
425             E = D;
426             D = C;
427             C = S30(B);
428             B = A;
429             A = TEMP;
430         }
431         for (; t < 40; t++) {
432             TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
433             E = D;
434             D = C;
435             C = S30(B);
436             B = A;
437             A = TEMP;
438         }
439         for (; t < 60; t++) {
440             TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
441             E = D;
442             D = C;
443             C = S30(B);
444             B = A;
445             A = TEMP;
446         }
447         for (; t < 80; t++) {
448             TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
449             E = D;
450             D = C;
451             C = S30(B);
452             B = A;
453             A = TEMP;
454         }
455 
456         ctx->H[0] += A;
457         ctx->H[1] += B;
458         ctx->H[2] += C;
459         ctx->H[3] += D;
460         ctx->H[4] += E;
461     }
462 
463     /* copy result into output buffer */
464     output[0] = be32_to_cpu(ctx->H[0]);
465     output[1] = be32_to_cpu(ctx->H[1]);
466     output[2] = be32_to_cpu(ctx->H[2]);
467     output[3] = be32_to_cpu(ctx->H[3]);
468     output[4] = be32_to_cpu(ctx->H[4]);
469 
470     /* indicate that message buffer in context is empty */
471     ctx->octets_in_buffer = 0;
472 
473     return;
474 }
475