Avoid using non-portable debian.h
[yaz-moved-to-github.git] / src / wrbuf_sha1.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file wrbuf_sha1.c
8  * \brief Implements SHA1 creation over WRBUF
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <yaz/wrbuf.h>
20
21 /*
22 SHA-1 in C
23 By Steve Reid <steve@edmweb.com>
24 100% Public Domain
25 */
26
27 /* #define SHA1HANDSOFF * Copies data before messing with it. */
28
29 #define SHA1HANDSOFF
30
31 #include <stdint.h>
32
33 typedef struct {
34     uint32_t state[5];
35     uint32_t count[2];
36     unsigned char buffer[64];
37 } SHA1_CTX;
38
39 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
40
41 /* blk0() and blk() perform the initial expand. */
42 /* I got the idea of expanding during the round function from SSLeay */
43 #if WORDS_BIGENDIAN
44 #define blk0(i) block->l[i]
45 #else
46 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
47     |(rol(block->l[i],8)&0x00FF00FF))
48 #endif
49 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
50     ^block->l[(i+2)&15]^block->l[i&15],1))
51
52 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
53 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
54 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
55 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
56 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
57 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
58
59
60 /* Hash a single 512-bit block. This is the core of the algorithm. */
61
62 static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64])
63 {
64     uint32_t a, b, c, d, e;
65     typedef union {
66         unsigned char c[64];
67         uint32_t l[16];
68     } CHAR64LONG16;
69 #ifdef SHA1HANDSOFF
70     CHAR64LONG16 block[1];  /* use array to appear as a pointer */
71     memcpy(block, buffer, 64);
72 #else
73     /* The following had better never be used because it causes the
74      * pointer-to-const buffer to be cast into a pointer to non-const.
75      * And the result is written through.  I threw a "const" in, hoping
76      * this will cause a diagnostic.
77      */
78     CHAR64LONG16* block = (const CHAR64LONG16*)buffer;
79 #endif
80     /* Copy context->state[] to working vars */
81     a = state[0];
82     b = state[1];
83     c = state[2];
84     d = state[3];
85     e = state[4];
86     /* 4 rounds of 20 operations each. Loop unrolled. */
87     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
88     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
89     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
90     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
91     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
92     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
93     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
94     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
95     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
96     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
97     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
98     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
99     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
100     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
101     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
102     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
103     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
104     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
105     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
106     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
107     /* Add the working vars back into context.state[] */
108     state[0] += a;
109     state[1] += b;
110     state[2] += c;
111     state[3] += d;
112     state[4] += e;
113     /* Wipe variables */
114     a = b = c = d = e = 0;
115 #ifdef SHA1HANDSOFF
116     memset(block, '\0', sizeof(block));
117 #endif
118 }
119
120
121 /* SHA1Init - Initialize new context */
122
123 static void SHA1Init(SHA1_CTX* context)
124 {
125     /* SHA1 initialization constants */
126     context->state[0] = 0x67452301;
127     context->state[1] = 0xEFCDAB89;
128     context->state[2] = 0x98BADCFE;
129     context->state[3] = 0x10325476;
130     context->state[4] = 0xC3D2E1F0;
131     context->count[0] = context->count[1] = 0;
132 }
133
134
135 /* Run your data through this. */
136
137 static void SHA1Update(SHA1_CTX* context, const unsigned char* data, uint32_t len)
138 {
139     uint32_t i;
140     uint32_t j;
141
142     j = context->count[0];
143     if ((context->count[0] += len << 3) < j)
144         context->count[1]++;
145     context->count[1] += (len>>29);
146     j = (j >> 3) & 63;
147     if ((j + len) > 63) {
148         memcpy(&context->buffer[j], data, (i = 64-j));
149         SHA1Transform(context->state, context->buffer);
150         for ( ; i + 63 < len; i += 64) {
151             SHA1Transform(context->state, &data[i]);
152         }
153         j = 0;
154     }
155     else i = 0;
156     memcpy(&context->buffer[j], &data[i], len - i);
157 }
158
159
160 /* Add padding and return the message digest. */
161
162 static void SHA1Final(unsigned char digest[20], SHA1_CTX* context)
163 {
164     unsigned i;
165     unsigned char finalcount[8];
166     unsigned char c;
167
168     for (i = 0; i < 8; i++) {
169         finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
170          >> ((3-(i & 3)) * 8) ) & 255);  /* Endian independent */
171     }
172     c = 0200;
173     SHA1Update(context, &c, 1);
174     while ((context->count[0] & 504) != 448) {
175         c = 0000;
176         SHA1Update(context, &c, 1);
177     }
178     SHA1Update(context, finalcount, 8);  /* Should cause a SHA1Transform() */
179     for (i = 0; i < 20; i++) {
180         digest[i] = (unsigned char)
181          ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
182     }
183     /* Wipe variables */
184     memset(context, '\0', sizeof(*context));
185     memset(&finalcount, '\0', sizeof(finalcount));
186 }
187
188
189 int wrbuf_sha1_write(WRBUF b, const char *cp, size_t sz, int hexit)
190 {
191     unsigned char digest[20];
192     SHA1_CTX ctx;
193
194     SHA1Init(&ctx);
195     SHA1Update(&ctx, (unsigned char *) cp, sz);
196     SHA1Final(digest, &ctx);
197
198     if (hexit)
199     {
200         int i;
201         for (i = 0; i < 20; i++)
202             wrbuf_printf(b, "%02x", digest[i]);
203     }
204     else
205         wrbuf_write(b, (const char *) digest, 20);
206     return 0;
207 }
208
209 int wrbuf_sha1_puts(WRBUF b, const char *cp, int hexit)
210 {
211     return wrbuf_sha1_write(b, cp, strlen(cp), hexit);
212 }
213
214 /*
215  * Local variables:
216  * c-basic-offset: 4
217  * c-file-style: "Stroustrup"
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */