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 <string.h> 30 31void 32_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20]) 33{ 34 struct mesa_sha1 ctx; 35 36 _mesa_sha1_init(&ctx); 37 _mesa_sha1_update(&ctx, data, size); 38 _mesa_sha1_final(&ctx, result); 39} 40 41void 42_mesa_sha1_format(char *buf, const unsigned char *sha1) 43{ 44 static const char hex_digits[] = "0123456789abcdef"; 45 int i; 46 47 for (i = 0; i < 40; i += 2) { 48 buf[i] = hex_digits[sha1[i >> 1] >> 4]; 49 buf[i + 1] = hex_digits[sha1[i >> 1] & 0x0f]; 50 } 51 buf[i] = '\0'; 52} 53 54/* Convert a hashs string hexidecimal representation into its more compact 55 * form. 56 */ 57void 58_mesa_sha1_hex_to_sha1(unsigned char *buf, const char *hex) 59{ 60 for (unsigned i = 0; i < 20; i++) { 61 char tmp[3]; 62 tmp[0] = hex[i * 2]; 63 tmp[1] = hex[(i * 2) + 1]; 64 tmp[2] = '\0'; 65 buf[i] = strtol(tmp, NULL, 16); 66 } 67} 68 69static void 70sha1_to_uint32(const uint8_t sha1[SHA1_DIGEST_LENGTH], 71 uint32_t out[SHA1_DIGEST_LENGTH32]) 72{ 73 memset(out, 0, SHA1_DIGEST_LENGTH); 74 75 for (unsigned i = 0; i < SHA1_DIGEST_LENGTH; i++) 76 out[i / 4] |= (uint32_t)sha1[i] << ((i % 4) * 8); 77} 78 79void 80_mesa_sha1_print(FILE *f, const uint8_t sha1[SHA1_DIGEST_LENGTH]) 81{ 82 uint32_t u32[SHA1_DIGEST_LENGTH]; 83 sha1_to_uint32(sha1, u32); 84 85 for (unsigned i = 0; i < SHA1_DIGEST_LENGTH32; i++) { 86 fprintf(f, "0x%08x", u32[i]); 87 if (i < SHA1_DIGEST_LENGTH32 - 1) 88 fprintf(f, ", "); 89 } 90} 91 92bool 93_mesa_printed_sha1_equal(const uint8_t sha1[SHA1_DIGEST_LENGTH], 94 const uint32_t printed_sha1[SHA1_DIGEST_LENGTH32]) 95{ 96 uint32_t u32[SHA1_DIGEST_LENGTH32]; 97 sha1_to_uint32(sha1, u32); 98 99 return memcmp(u32, printed_sha1, sizeof(u32)) == 0; 100} 101