summaryrefslogtreecommitdiff
path: root/lib/sha1.c
blob: 871ff55eded77e5cfc8daf3f12def80266c988a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * This file is from the Apache Portable Runtime Library.
 * The full upstream copyright and license statement is included below.
 * Modifications copyright (c) 2009, 2010 Nicira, Inc.
 */

/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* This software also makes use of the following component:
 *
 * NIST Secure Hash Algorithm
 *      heavily modified by Uwe Hollerbach uh@alumni.caltech edu
 *  from Peter C. Gutmann's implementation as found in
 *  Applied Cryptography by Bruce Schneier
 *  This code is hereby placed in the public domain
 */

#include <config.h>
#include "sha1.h"

#ifdef HAVE_OPENSSL
#include <openssl/err.h>
#include <openssl/evp.h>
#endif

#include <ctype.h>
#include <string.h>
#include "compiler.h"
#include "openvswitch/vlog.h"
#include "util.h"

VLOG_DEFINE_THIS_MODULE(sha1);

#ifdef HAVE_OPENSSL
static void
log_openssl_err(const char *func)
{
    char buf[1024];

    ERR_error_string_n(ERR_get_error(), buf, 1024);
    VLOG_FATAL("%s failed: %s", func, buf);
}
#endif

/*
 * Initialize the SHA digest.
 * context: The SHA context to initialize
 */
void
sha1_init(struct sha1_ctx *sha_info)
{
#ifdef HAVE_OPENSSL
    sha_info->ctx = EVP_MD_CTX_create();
    if (!EVP_DigestInit_ex(sha_info->ctx, EVP_sha1(), NULL)) {
        log_openssl_err("EVP_DigestInit_ex");
    }
#else
    ovs_sha1_init(sha_info);
#endif
}

/*
 * Update the SHA digest.
 * context: The SHA1 context to update.
 * input: The buffer to add to the SHA digest.
 * inputLen: The length of the input buffer.
 */
void
sha1_update(struct sha1_ctx *ctx, const void *buffer_, uint32_t count)
{
#ifdef HAVE_OPENSSL
    if (!EVP_DigestUpdate(ctx->ctx, buffer_, count)) {
        log_openssl_err("EVP_DigestUpdate");
    }
#else
    ovs_sha1_update(ctx, buffer_, count);
#endif
}

/*
 * Finish computing the SHA digest.
 * digest: the output buffer in which to store the digest.
 * context: The context to finalize.
 */
void
sha1_final(struct sha1_ctx *ctx, uint8_t digest[SHA1_DIGEST_SIZE])
{
#ifdef HAVE_OPENSSL
    unsigned int len;

    if (!EVP_DigestFinal_ex(ctx->ctx, digest, &len)) {
        log_openssl_err("EVP_DigestFinal_ex");
    }
    ovs_assert(len == SHA1_DIGEST_SIZE);
    EVP_MD_CTX_destroy(ctx->ctx);
#else
    ovs_sha1_final(ctx, digest);
#endif
}

/* Computes the hash of 'n' bytes in 'data' into 'digest'. */
void
sha1_bytes(const void *data, uint32_t n, uint8_t digest[SHA1_DIGEST_SIZE])
{
    struct sha1_ctx ctx;

    sha1_init(&ctx);
    sha1_update(&ctx, data, n);
    sha1_final(&ctx, digest);
}

void
sha1_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE],
            char hex[SHA1_HEX_DIGEST_LEN + 1])
{
    int i;

    for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
        *hex++ = "0123456789abcdef"[digest[i] >> 4];
        *hex++ = "0123456789abcdef"[digest[i] & 15];
    }
    *hex = '\0';
}

bool
sha1_from_hex(uint8_t digest[SHA1_DIGEST_SIZE], const char *hex)
{
    int i;

    for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
        bool ok;

        digest[i] = hexits_value(hex, 2, &ok);
        if (!ok) {
            return false;
        }
        hex += 2;
    }
    return true;
}

/* Generic implementation for the case where OpenSSL is not available. */

/* A bit faster & bigger, if defined */
#define UNROLL_LOOPS

/* SHA f()-functions */
static inline uint32_t
f1(uint32_t x, uint32_t y, uint32_t z)
{
    return (x & y) | (~x & z);
}

static inline uint32_t
f2(uint32_t x, uint32_t y, uint32_t z)
{
    return x ^ y ^ z;
}

static inline uint32_t
f3(uint32_t x, uint32_t y, uint32_t z)
{
    return (x & y) | (x & z) | (y & z);
}

static inline uint32_t
f4(uint32_t x, uint32_t y, uint32_t z)
{
    return x ^ y ^ z;
}

/* SHA constants */
#define CONST1      0x5a827999L
#define CONST2      0x6ed9eba1L
#define CONST3      0x8f1bbcdcL
#define CONST4      0xca62c1d6L

/* 32-bit rotate */
static inline uint32_t
rotate32(uint32_t x, int n)
{
    return ((x << n) | (x >> (32 - n)));
}

#define FUNC(n, i)                                                      \
    do {                                                                \
        temp = rotate32(A, 5) + f##n(B, C, D) + E + W[i] + CONST##n;    \
        E = D;                                                          \
        D = C;                                                          \
        C = rotate32(B, 30);                                            \
        B = A;                                                          \
        A = temp;                                                       \
    } while (0)

#define SHA_BLOCK_SIZE           64

/* Do SHA transformation. */
static void
sha_transform(struct sha1_ctx *sha_info)
{
    int i;
    uint32_t temp, A, B, C, D, E, W[80];

    for (i = 0; i < 16; ++i) {
        W[i] = sha_info->data[i];
    }
    for (i = 16; i < 80; ++i) {
        W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
        W[i] = rotate32(W[i], 1);
    }
    A = sha_info->digest[0];
    B = sha_info->digest[1];
    C = sha_info->digest[2];
    D = sha_info->digest[3];
    E = sha_info->digest[4];
#ifdef UNROLL_LOOPS
    FUNC(1, 0);  FUNC(1, 1);  FUNC(1, 2);  FUNC(1, 3);  FUNC(1, 4);
    FUNC(1, 5);  FUNC(1, 6);  FUNC(1, 7);  FUNC(1, 8);  FUNC(1, 9);
    FUNC(1,10);  FUNC(1,11);  FUNC(1,12);  FUNC(1,13);  FUNC(1,14);
    FUNC(1,15);  FUNC(1,16);  FUNC(1,17);  FUNC(1,18);  FUNC(1,19);

    FUNC(2,20);  FUNC(2,21);  FUNC(2,22);  FUNC(2,23);  FUNC(2,24);
    FUNC(2,25);  FUNC(2,26);  FUNC(2,27);  FUNC(2,28);  FUNC(2,29);
    FUNC(2,30);  FUNC(2,31);  FUNC(2,32);  FUNC(2,33);  FUNC(2,34);
    FUNC(2,35);  FUNC(2,36);  FUNC(2,37);  FUNC(2,38);  FUNC(2,39);

    FUNC(3,40);  FUNC(3,41);  FUNC(3,42);  FUNC(3,43);  FUNC(3,44);
    FUNC(3,45);  FUNC(3,46);  FUNC(3,47);  FUNC(3,48);  FUNC(3,49);
    FUNC(3,50);  FUNC(3,51);  FUNC(3,52);  FUNC(3,53);  FUNC(3,54);
    FUNC(3,55);  FUNC(3,56);  FUNC(3,57);  FUNC(3,58);  FUNC(3,59);

    FUNC(4,60);  FUNC(4,61);  FUNC(4,62);  FUNC(4,63);  FUNC(4,64);
    FUNC(4,65);  FUNC(4,66);  FUNC(4,67);  FUNC(4,68);  FUNC(4,69);
    FUNC(4,70);  FUNC(4,71);  FUNC(4,72);  FUNC(4,73);  FUNC(4,74);
    FUNC(4,75);  FUNC(4,76);  FUNC(4,77);  FUNC(4,78);  FUNC(4,79);
#else /* !UNROLL_LOOPS */
    for (i = 0; i < 20; ++i) {
        FUNC(1,i);
    }
    for (i = 20; i < 40; ++i) {
        FUNC(2,i);
    }
    for (i = 40; i < 60; ++i) {
        FUNC(3,i);
    }
    for (i = 60; i < 80; ++i) {
        FUNC(4,i);
    }
#endif /* !UNROLL_LOOPS */
    sha_info->digest[0] += A;
    sha_info->digest[1] += B;
    sha_info->digest[2] += C;
    sha_info->digest[3] += D;
    sha_info->digest[4] += E;
}

/* 'count' is the number of bytes to do an endian flip. */
static void
maybe_byte_reverse(uint32_t *buffer OVS_UNUSED, int count OVS_UNUSED)
{
#if !WORDS_BIGENDIAN
    int i;
    uint8_t ct[4], *cp;

    count /= sizeof(uint32_t);
    cp = (uint8_t *) buffer;
    for (i = 0; i < count; i++) {
        ct[0] = cp[0];
        ct[1] = cp[1];
        ct[2] = cp[2];
        ct[3] = cp[3];
        cp[0] = ct[3];
        cp[1] = ct[2];
        cp[2] = ct[1];
        cp[3] = ct[0];
        cp += sizeof(uint32_t);
    }
#endif
}

/*
 * Initialize the SHA digest.
 * context: The SHA context to initialize
 */
void
ovs_sha1_init(struct sha1_ctx *sha_info)
{
    sha_info->digest[0] = 0x67452301L;
    sha_info->digest[1] = 0xefcdab89L;
    sha_info->digest[2] = 0x98badcfeL;
    sha_info->digest[3] = 0x10325476L;
    sha_info->digest[4] = 0xc3d2e1f0L;
    sha_info->count_lo = 0L;
    sha_info->count_hi = 0L;
    sha_info->local = 0;
}

/*
 * Update the SHA digest.
 * context: The SHA1 context to update.
 * input: The buffer to add to the SHA digest.
 * inputLen: The length of the input buffer.
 */
void
ovs_sha1_update(struct sha1_ctx *ctx, const void *buffer_, uint32_t count)
{
    const uint8_t *buffer = buffer_;
    unsigned int i;

    if ((ctx->count_lo + (count << 3)) < ctx->count_lo) {
        ctx->count_hi++;
    }
    ctx->count_lo += count << 3;
    ctx->count_hi += count >> 29;
    if (ctx->local) {
        i = SHA_BLOCK_SIZE - ctx->local;
        if (i > count) {
            i = count;
        }
        memcpy(((uint8_t *) ctx->data) + ctx->local, buffer, i);
        count -= i;
        buffer += i;
        ctx->local += i;
        if (ctx->local == SHA_BLOCK_SIZE) {
            maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
            sha_transform(ctx);
        } else {
            return;
        }
    }
    while (count >= SHA_BLOCK_SIZE) {
        memcpy(ctx->data, buffer, SHA_BLOCK_SIZE);
        buffer += SHA_BLOCK_SIZE;
        count -= SHA_BLOCK_SIZE;
        maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
        sha_transform(ctx);
    }
    memcpy(ctx->data, buffer, count);
    ctx->local = count;
}

/*
 * Finish computing the SHA digest.
 * digest: the output buffer in which to store the digest.
 * context: The context to finalize.
 */
void
ovs_sha1_final(struct sha1_ctx *ctx, uint8_t digest[SHA1_DIGEST_SIZE])
{
    int count, i, j;
    uint32_t lo_bit_count, hi_bit_count, k;

    lo_bit_count = ctx->count_lo;
    hi_bit_count = ctx->count_hi;
    count = (int) ((lo_bit_count >> 3) & 0x3f);
    ((uint8_t *) ctx->data)[count++] = 0x80;
    if (count > SHA_BLOCK_SIZE - 8) {
        memset(((uint8_t *) ctx->data) + count, 0, SHA_BLOCK_SIZE - count);
        maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
        sha_transform(ctx);
        memset((uint8_t *) ctx->data, 0, SHA_BLOCK_SIZE - 8);
    } else {
        memset(((uint8_t *) ctx->data) + count, 0,
               SHA_BLOCK_SIZE - 8 - count);
    }
    maybe_byte_reverse(ctx->data, SHA_BLOCK_SIZE);
    ctx->data[14] = hi_bit_count;
    ctx->data[15] = lo_bit_count;
    sha_transform(ctx);

    for (i = j = 0; j < SHA1_DIGEST_SIZE; i++) {
        k = ctx->digest[i];
        digest[j++] = k >> 24;
        digest[j++] = k >> 16;
        digest[j++] = k >> 8;
        digest[j++] = k;
    }
}

/* Computes the hash of 'n' bytes in 'data' into 'digest'. */
void
ovs_sha1_bytes(const void *data, uint32_t n, uint8_t digest[SHA1_DIGEST_SIZE])
{
    struct sha1_ctx ctx;

    ovs_sha1_init(&ctx);
    ovs_sha1_update(&ctx, data, n);
    ovs_sha1_final(&ctx, digest);
}