Replace SHA-1 library with one that is clearly licensed.
[cascardo/ovs.git] / lib / sha1.c
1 /*
2  * This file is from the Apache Portable Runtime Library.
3  * The full upstream copyright and license statement is included below.
4  * Modifications copyright (c) 2009 Nicira Networks.
5  */
6
7 /* Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements.  See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License.  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 /* This software also makes use of the following component:
24  *
25  * NIST Secure Hash Algorithm
26  *      heavily modified by Uwe Hollerbach uh@alumni.caltech edu
27  *      from Peter C. Gutmann's implementation as found in
28  *      Applied Cryptography by Bruce Schneier
29  *      This code is hereby placed in the public domain
30  */
31
32 #include <config.h>
33 #include "sha1.h"
34 #include <string.h>
35
36 /* a bit faster & bigger, if defined */
37 #define UNROLL_LOOPS
38
39 /* SHA f()-functions */
40 static inline uint32_t
41 f1(uint32_t x, uint32_t y, uint32_t z)
42 {
43     return (x & y) | (~x & z);
44 }
45
46 static inline uint32_t
47 f2(uint32_t x, uint32_t y, uint32_t z)
48 {
49     return x ^ y ^ z;
50 }
51
52 static inline uint32_t
53 f3(uint32_t x, uint32_t y, uint32_t z)
54 {
55     return (x & y) | (x & z) | (y & z);
56 }
57
58 static inline uint32_t
59 f4(uint32_t x, uint32_t y, uint32_t z)
60 {
61     return x ^ y ^ z;
62 }
63
64 /* SHA constants */
65 #define CONST1          0x5a827999L
66 #define CONST2          0x6ed9eba1L
67 #define CONST3          0x8f1bbcdcL
68 #define CONST4          0xca62c1d6L
69
70 /* 32-bit rotate */
71 static inline uint32_t
72 rotate32(uint32_t x, int n)
73 {
74     return ((x << n) | (x >> (32 - n)));
75 }
76
77 #define FUNC(n, i)                                                      \
78     do {                                                                \
79         temp = rotate32(A, 5) + f##n(B, C, D) + E + W[i] + CONST##n;    \
80         E = D;                                                          \
81         D = C;                                                          \
82         C = rotate32(B, 30);                                            \
83         B = A;                                                          \
84         A = temp;                                                       \
85     } while (0)
86
87 #define SHA_BLOCK_SIZE           64
88
89 /* Do SHA transformation. */
90 static void
91 sha_transform(struct sha1_ctx *sha_info)
92 {
93     int i;
94     uint32_t temp, A, B, C, D, E, W[80];
95
96     for (i = 0; i < 16; ++i) {
97         W[i] = sha_info->data[i];
98     }
99     for (i = 16; i < 80; ++i) {
100         W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
101         W[i] = rotate32(W[i], 1);
102     }
103     A = sha_info->digest[0];
104     B = sha_info->digest[1];
105     C = sha_info->digest[2];
106     D = sha_info->digest[3];
107     E = sha_info->digest[4];
108 #ifdef UNROLL_LOOPS
109     FUNC(1, 0);  FUNC(1, 1);  FUNC(1, 2);  FUNC(1, 3);  FUNC(1, 4);
110     FUNC(1, 5);  FUNC(1, 6);  FUNC(1, 7);  FUNC(1, 8);  FUNC(1, 9);
111     FUNC(1,10);  FUNC(1,11);  FUNC(1,12);  FUNC(1,13);  FUNC(1,14);
112     FUNC(1,15);  FUNC(1,16);  FUNC(1,17);  FUNC(1,18);  FUNC(1,19);
113
114     FUNC(2,20);  FUNC(2,21);  FUNC(2,22);  FUNC(2,23);  FUNC(2,24);
115     FUNC(2,25);  FUNC(2,26);  FUNC(2,27);  FUNC(2,28);  FUNC(2,29);
116     FUNC(2,30);  FUNC(2,31);  FUNC(2,32);  FUNC(2,33);  FUNC(2,34);
117     FUNC(2,35);  FUNC(2,36);  FUNC(2,37);  FUNC(2,38);  FUNC(2,39);
118
119     FUNC(3,40);  FUNC(3,41);  FUNC(3,42);  FUNC(3,43);  FUNC(3,44);
120     FUNC(3,45);  FUNC(3,46);  FUNC(3,47);  FUNC(3,48);  FUNC(3,49);
121     FUNC(3,50);  FUNC(3,51);  FUNC(3,52);  FUNC(3,53);  FUNC(3,54);
122     FUNC(3,55);  FUNC(3,56);  FUNC(3,57);  FUNC(3,58);  FUNC(3,59);
123
124     FUNC(4,60);  FUNC(4,61);  FUNC(4,62);  FUNC(4,63);  FUNC(4,64);
125     FUNC(4,65);  FUNC(4,66);  FUNC(4,67);  FUNC(4,68);  FUNC(4,69);
126     FUNC(4,70);  FUNC(4,71);  FUNC(4,72);  FUNC(4,73);  FUNC(4,74);
127     FUNC(4,75);  FUNC(4,76);  FUNC(4,77);  FUNC(4,78);  FUNC(4,79);
128 #else /* !UNROLL_LOOPS */
129     for (i = 0; i < 20; ++i) {
130         FUNC(1,i);
131     }
132     for (i = 20; i < 40; ++i) {
133         FUNC(2,i);
134     }
135     for (i = 40; i < 60; ++i) {
136         FUNC(3,i);
137     }
138     for (i = 60; i < 80; ++i) {
139         FUNC(4,i);
140     }
141 #endif /* !UNROLL_LOOPS */
142     sha_info->digest[0] += A;
143     sha_info->digest[1] += B;
144     sha_info->digest[2] += C;
145     sha_info->digest[3] += D;
146     sha_info->digest[4] += E;
147 }
148
149 /* 'count' is the number of bytes to do an endian flip. */
150 static void
151 maybe_byte_reverse(uint32_t *buffer, int count)
152 {
153     int i;
154     uint8_t ct[4], *cp;
155
156 #if !WORDS_BIGENDIAN
157         count /= sizeof(uint32_t);
158         cp = (uint8_t *) buffer;
159         for (i = 0; i < count; i++) {
160             ct[0] = cp[0];
161             ct[1] = cp[1];
162             ct[2] = cp[2];
163             ct[3] = cp[3];
164             cp[0] = ct[3];
165             cp[1] = ct[2];
166             cp[2] = ct[1];
167             cp[3] = ct[0];
168             cp += sizeof(uint32_t);
169         }
170 #endif
171 }
172
173 /*
174  * Initialize the SHA digest.
175  * context: The SHA context to initialize
176  */
177 void
178 sha1_init(struct sha1_ctx *sha_info)
179 {
180     sha_info->digest[0] = 0x67452301L;
181     sha_info->digest[1] = 0xefcdab89L;
182     sha_info->digest[2] = 0x98badcfeL;
183     sha_info->digest[3] = 0x10325476L;
184     sha_info->digest[4] = 0xc3d2e1f0L;
185     sha_info->count_lo = 0L;
186     sha_info->count_hi = 0L;
187     sha_info->local = 0;
188 }
189
190 /*
191  * Update the SHA digest.
192  * context: The SHA1 context to update.
193  * input: The buffer to add to the SHA digest.
194  * inputLen: The length of the input buffer.
195  */
196 void
197 sha1_update(struct sha1_ctx *ctx, const void *buffer_, size_t count)
198 {
199     const uint8_t *buffer = buffer_;
200     unsigned int i;
201
202     if ((ctx->count_lo + (count << 3)) < ctx->count_lo) {
203         ctx->count_hi++;
204     }
205     ctx->count_lo += count << 3;
206     ctx->count_hi += count >> 29;
207     if (ctx->local) {
208         i = SHA_BLOCK_SIZE - ctx->local;
209         if (i > count) {
210             i = count;
211         }
212         memcpy(((uint8_t *) ctx->data) + ctx->local, buffer, i);
213         count -= i;
214         buffer += i;
215         ctx->local += i;
216         if (ctx->local == SHA_BLOCK_SIZE) {
217             maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
218             sha_transform(ctx);
219         } else {
220             return;
221         }
222     }
223     while (count >= SHA_BLOCK_SIZE) {
224         memcpy(ctx->data, buffer, SHA_BLOCK_SIZE);
225         buffer += SHA_BLOCK_SIZE;
226         count -= SHA_BLOCK_SIZE;
227         maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
228         sha_transform(ctx);
229     }
230     memcpy(ctx->data, buffer, count);
231     ctx->local = count;
232 }
233
234 /*
235  * Finish computing the SHA digest.
236  * digest: the output buffer in which to store the digest.
237  * context: The context to finalize.
238  */
239 void
240 sha1_final(struct sha1_ctx *ctx, uint8_t digest[SHA1_DIGEST_SIZE])
241 {
242     int count, i, j;
243     uint32_t lo_bit_count, hi_bit_count, k;
244
245     lo_bit_count = ctx->count_lo;
246     hi_bit_count = ctx->count_hi;
247     count = (int) ((lo_bit_count >> 3) & 0x3f);
248     ((uint8_t *) ctx->data)[count++] = 0x80;
249     if (count > SHA_BLOCK_SIZE - 8) {
250         memset(((uint8_t *) ctx->data) + count, 0, SHA_BLOCK_SIZE - count);
251         maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
252         sha_transform(ctx);
253         memset((uint8_t *) ctx->data, 0, SHA_BLOCK_SIZE - 8);
254     } else {
255         memset(((uint8_t *) ctx->data) + count, 0,
256                SHA_BLOCK_SIZE - 8 - count);
257     }
258     maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
259     ctx->data[14] = hi_bit_count;
260     ctx->data[15] = lo_bit_count;
261     sha_transform(ctx);
262
263     for (i = j = 0; j < SHA1_DIGEST_SIZE; i++) {
264         k = ctx->digest[i];
265         digest[j++] = k >> 24;
266         digest[j++] = k >> 16;
267         digest[j++] = k >> 8;
268         digest[j++] = k;
269     }
270 }
271
272 /* Computes the hash of 'n' bytes in 'data' into 'digest'. */
273 void
274 sha1_bytes(const void *data, size_t n, uint8_t digest[SHA1_DIGEST_SIZE])
275 {
276     struct sha1_ctx ctx;
277
278     sha1_init(&ctx);
279     sha1_update(&ctx, data, n);
280     sha1_final(&ctx, digest);
281 }