sha1: cleanup & separate unit test
[ardour.git] / libs / ardour / sha1.c
1 /*
2  * Copyright (C) 2012,2015 Robin Gareus <robin@gareus.org>
3  *
4  * This code is inspired by libcrypt, which was placed
5  * in the public domain by Wei Dai and other contributors.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SHA
23 #define EXPORT_SHA static
24 #endif
25
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #ifdef __BIG_ENDIAN__
32 # define SHA_BIG_ENDIAN
33 #elif defined _BIG_ENDIAN
34 # define SHA_BIG_ENDIAN
35 #elif defined __BYTE_ORDER__
36 # if __BYTE_ORDER__ ==  __ORDER_BIG_ENDIAN__
37 #  define SHA_BIG_ENDIAN
38 # endif
39 #elif !defined __LITTLE_ENDIAN__
40 # include <endian.h> // machine/endian.h
41 # if __BYTE_ORDER__ ==  __ORDER_BIG_ENDIAN__
42 #  define SHA_BIG_ENDIAN
43 # endif
44 #endif
45
46 typedef struct {
47         uint32_t buffer[16];
48         uint32_t state[5];
49         uint32_t byteCount;
50         uint8_t bufferOffset;
51 } Sha1Digest;
52
53
54 static inline uint32_t sha1_rol32 (uint32_t number, uint8_t bits) {
55         return ((number << bits) | (number >> (32 - bits)));
56 }
57
58 static void sha1_hashBlock (Sha1Digest *s) {
59         uint8_t i;
60         uint32_t a, b, c, d, e, t;
61
62         a = s->state[0];
63         b = s->state[1];
64         c = s->state[2];
65         d = s->state[3];
66         e = s->state[4];
67
68         for (i = 0; i < 80; ++i) {
69                 if (i >= 16) {
70                         t = s->buffer[(i + 13) & 15] ^ s->buffer[(i + 8) & 15] ^ s->buffer[(i + 2) & 15] ^ s->buffer[i & 15];
71                         s->buffer[i & 15] = sha1_rol32 (t, 1);
72                 }
73                 if (i < 20) {
74                         t = (d ^ (b & (c ^ d))) + 0x5a827999;
75                 } else if (i < 40) {
76                         t = (b ^ c ^ d) + 0x6ed9eba1;
77                 } else if (i < 60) {
78                         t = ((b & c) | (d & (b | c))) + 0x8f1bbcdc;
79                 } else {
80                         t = (b ^ c ^ d) + 0xca62c1d6;
81                 }
82                 t += sha1_rol32 (a, 5) + e + s->buffer[i & 15];
83                 e = d;
84                 d = c;
85                 c = sha1_rol32 (b, 30);
86                 b = a;
87                 a = t;
88         }
89
90         s->state[0] += a;
91         s->state[1] += b;
92         s->state[2] += c;
93         s->state[3] += d;
94         s->state[4] += e;
95 }
96
97 static void sha1_addUncounted (Sha1Digest *s, const uint8_t data) {
98         uint8_t * const b = (uint8_t*) s->buffer;
99 #ifdef SHA_BIG_ENDIAN
100         b[s->bufferOffset] = data;
101 #else
102         b[s->bufferOffset ^ 3] = data;
103 #endif
104         s->bufferOffset++;
105         if (s->bufferOffset == 64) {
106                 sha1_hashBlock (s);
107                 s->bufferOffset = 0;
108         }
109 }
110
111 static void sha1_pad (Sha1Digest *s) {
112         // Implement SHA-1 padding (fips180-2 5.1.1)
113         // Pad with 0x80 followed by 0x00 until the end of the block
114         sha1_addUncounted (s, 0x80);
115         while (s->bufferOffset != 56) sha1_addUncounted (s, 0x00);
116
117         // Append length in the last 8 bytes
118         sha1_addUncounted (s, 0); // We're only using 32 bit lengths
119         sha1_addUncounted (s, 0); // But SHA-1 supports 64 bit lengths
120         sha1_addUncounted (s, 0); // So zero pad the top bits
121         sha1_addUncounted (s, s->byteCount >> 29); // Shifting to multiply by 8
122         sha1_addUncounted (s, s->byteCount >> 21); // as SHA-1 supports bitstreams as well as
123         sha1_addUncounted (s, s->byteCount >> 13); // byte.
124         sha1_addUncounted (s, s->byteCount >> 5);
125         sha1_addUncounted (s, s->byteCount << 3);
126 }
127
128
129 /*** public functions ***/
130
131 EXPORT_SHA void sha1_init (Sha1Digest *s) {
132         s->state[0] = 0x67452301;
133         s->state[1] = 0xefcdab89;
134         s->state[2] = 0x98badcfe;
135         s->state[3] = 0x10325476;
136         s->state[4] = 0xc3d2e1f0;
137         s->byteCount = 0;
138         s->bufferOffset = 0;
139 }
140
141 EXPORT_SHA void sha1_writebyte (Sha1Digest *s, const uint8_t data) {
142         ++s->byteCount;
143         sha1_addUncounted (s, data);
144 }
145
146 EXPORT_SHA void sha1_write (Sha1Digest *s, const uint8_t *data, size_t len) {
147         for (;len--;) sha1_writebyte (s, (uint8_t) *data++);
148 }
149
150 EXPORT_SHA uint8_t* sha1_result (Sha1Digest *s) {
151         // Pad to complete the last block
152         sha1_pad (s);
153
154 #ifndef SHA_BIG_ENDIAN
155         // Swap byte order back
156         int i;
157         for (i = 0; i < 5; ++i) {
158                 s->state[i] =
159                           (((s->state[i])<<24)& 0xff000000)
160                         | (((s->state[i])<<8) & 0x00ff0000)
161                         | (((s->state[i])>>8) & 0x0000ff00)
162                         | (((s->state[i])>>24)& 0x000000ff);
163         }
164 #endif
165         // Return pointer to hash (20 characters)
166         return (uint8_t*) s->state;
167 }
168
169 EXPORT_SHA void sha1_result_hash (Sha1Digest *s, char *rv) {
170         int i;
171         uint8_t* hash = sha1_result (s);
172         for (i = 0; i < 20; ++i) {
173                 sprintf (&rv[2*i], "%02x", hash[i]);
174         }
175 }