2 * Copyright (C) 2012,2015 Robin Gareus <robin@gareus.org>
4 * This code is inspired by libcrypt, which was placed
5 * in the public domain by Wei Dai and other contributors.
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.
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.
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.
23 #define EXPORT_SHA static
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
39 #elif !defined __LITTLE_ENDIAN__
40 # include <endian.h> // machine/endian.h
41 # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
42 # define SHA_BIG_ENDIAN
54 static inline uint32_t sha1_rol32 (uint32_t number, uint8_t bits) {
55 return ((number << bits) | (number >> (32 - bits)));
58 static void sha1_hashBlock (Sha1Digest *s) {
60 uint32_t a, b, c, d, e, t;
68 for (i = 0; i < 80; ++i) {
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);
74 t = (d ^ (b & (c ^ d))) + 0x5a827999;
76 t = (b ^ c ^ d) + 0x6ed9eba1;
78 t = ((b & c) | (d & (b | c))) + 0x8f1bbcdc;
80 t = (b ^ c ^ d) + 0xca62c1d6;
82 t += sha1_rol32 (a, 5) + e + s->buffer[i & 15];
85 c = sha1_rol32 (b, 30);
97 static void sha1_addUncounted (Sha1Digest *s, const uint8_t data) {
98 uint8_t * const b = (uint8_t*) s->buffer;
100 b[s->bufferOffset] = data;
102 b[s->bufferOffset ^ 3] = data;
105 if (s->bufferOffset == 64) {
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);
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);
129 /*** public functions ***/
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;
141 EXPORT_SHA void sha1_writebyte (Sha1Digest *s, const uint8_t data) {
143 sha1_addUncounted (s, data);
146 EXPORT_SHA void sha1_write (Sha1Digest *s, const uint8_t *data, size_t len) {
147 for (;len--;) sha1_writebyte (s, (uint8_t) *data++);
150 EXPORT_SHA uint8_t* sha1_result (Sha1Digest *s) {
151 // Pad to complete the last block
154 #ifndef SHA_BIG_ENDIAN
155 // Swap byte order back
157 for (i = 0; i < 5; ++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);
165 // Return pointer to hash (20 characters)
166 return (uint8_t*) s->state;
169 EXPORT_SHA void sha1_result_hash (Sha1Digest *s, char *rv) {
171 uint8_t* hash = sha1_result (s);
172 for (i = 0; i < 20; ++i) {
173 sprintf (&rv[2*i], "%02x", hash[i]);