• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright © 2007 Carl Worth
2  * Copyright © 2009 Jeremy Huddleston, Julien Cristau, and Matthieu Herrb
3  * Copyright © 2009-2010 Mikhail Gusarov
4  * Copyright © 2012 Yaakov Selkowitz and Keith Packard
5  * Copyright © 2014 Intel Corporation
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "sha1/sha1.h"
28 #include "mesa-sha1.h"
29 #include "hex.h"
30 #include <string.h>
31 #include <inttypes.h>
32 
33 void
_mesa_sha1_compute(const void * data,size_t size,unsigned char result[20])34 _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
35 {
36    struct mesa_sha1 ctx;
37 
38    _mesa_sha1_init(&ctx);
39    _mesa_sha1_update(&ctx, data, size);
40    _mesa_sha1_final(&ctx, result);
41 }
42 
43 void
_mesa_sha1_format(char * buf,const unsigned char * sha1)44 _mesa_sha1_format(char *buf, const unsigned char *sha1)
45 {
46    mesa_bytes_to_hex(buf, sha1, SHA1_DIGEST_LENGTH);
47 }
48 
49 /* Convert a hashs string hexidecimal representation into its more compact
50  * form.
51  */
52 void
_mesa_sha1_hex_to_sha1(unsigned char * buf,const char * hex)53 _mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex)
54 {
55    mesa_hex_to_bytes(buf, hex, SHA1_DIGEST_LENGTH);
56 }
57 
58 static void
sha1_to_uint32(const uint8_t sha1[SHA1_DIGEST_LENGTH],uint32_t out[SHA1_DIGEST_LENGTH32])59 sha1_to_uint32(const uint8_t sha1[SHA1_DIGEST_LENGTH],
60                uint32_t out[SHA1_DIGEST_LENGTH32])
61 {
62    memset(out, 0, SHA1_DIGEST_LENGTH);
63 
64    for (unsigned i = 0; i < SHA1_DIGEST_LENGTH; i++)
65       out[i / 4] |= (uint32_t)sha1[i] << ((i % 4) * 8);
66 }
67 
68 void
_mesa_sha1_print(FILE * f,const uint8_t sha1[SHA1_DIGEST_LENGTH])69 _mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH])
70 {
71    uint32_t u32[SHA1_DIGEST_LENGTH];
72    sha1_to_uint32(sha1, u32);
73 
74    for (unsigned i = 0; i < SHA1_DIGEST_LENGTH32; i++) {
75       fprintf(f, i ? ", 0x%08" PRIx32 : "0x%08" PRIx32, u32[i]);
76    }
77 }
78 
79 bool
_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32])80 _mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH],
81                          const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32])
82 {
83    uint32_t u32[SHA1_DIGEST_LENGTH32];
84    sha1_to_uint32(sha1, u32);
85 
86    return memcmp(u32, printed_sha1, sizeof(u32)) == 0;
87 }
88