Android: Partly revert r29569 and only call the new getJavaEnvironment() when needed.
[maemo-rb.git] / utils / sbinfo / sha1.c
blob99657fb14ad95ecdb6fcc3b5baa4bb4f7adf26bb
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Amaury Pouly
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 /* Based on http://en.wikipedia.org/wiki/SHA-1 */
22 #include "crypto.h"
24 static uint32_t rot_left(uint32_t val, int rot)
26 return (val << rot) | (val >> (32 - rot));
29 static inline void byte_swapxx(byte *ptr, int size)
31 for(int i = 0; i < size / 2; i++)
33 byte c = ptr[i];
34 ptr[i] = ptr[size - i - 1];
35 ptr[size - i - 1] = c;
39 static void byte_swap32(uint32_t *v)
41 byte_swapxx((byte *)v, 4);
44 void sha_1_init(struct sha_1_params_t *params)
46 params->hash[0] = 0x67452301;
47 params->hash[1] = 0xEFCDAB89;
48 params->hash[2] = 0x98BADCFE;
49 params->hash[3] = 0x10325476;
50 params->hash[4] = 0xC3D2E1F0;
51 params->buffer_nr_bits = 0;
54 void sha_1_update(struct sha_1_params_t *params, byte *buffer, int size)
56 int buffer_nr_bytes = (params->buffer_nr_bits / 8) % 64;
57 params->buffer_nr_bits += 8 * size;
58 int pos = 0;
59 if(buffer_nr_bytes + size >= 64)
61 pos = 64 - buffer_nr_bytes;
62 memcpy((byte *)(params->w) + buffer_nr_bytes, buffer, 64 - buffer_nr_bytes);
63 sha_1_block(params, params->hash, (byte *)params->w);
64 for(; pos + 64 <= size; pos += 64)
65 sha_1_block(params, params->hash, buffer + pos);
66 buffer_nr_bytes = 0;
68 memcpy((byte *)(params->w) + buffer_nr_bytes, buffer + pos, size - pos);
71 void sha_1_finish(struct sha_1_params_t *params)
73 /* length (in bits) in big endian BEFORE preprocessing */
74 byte length_big_endian[8];
75 memcpy(length_big_endian, &params->buffer_nr_bits, 8);
76 byte_swapxx(length_big_endian, 8);
77 /* append '1' and then '0's to the message to get 448 bit length for the last block */
78 byte b = 0x80;
79 sha_1_update(params, &b, 1);
80 b = 0;
81 while((params->buffer_nr_bits % 512) != 448)
82 sha_1_update(params, &b, 1);
83 /* append length */
84 sha_1_update(params, length_big_endian, 8);
85 /* go back to big endian */
86 for(int i = 0; i < 5; i++)
87 byte_swap32(&params->hash[i]);
90 void sha_1_output(struct sha_1_params_t *params, byte *out)
92 memcpy(out, params->hash, 20);
95 void sha_1_block(struct sha_1_params_t *params, uint32_t cur_hash[5], byte *data)
97 uint32_t a, b, c, d, e;
98 a = cur_hash[0];
99 b = cur_hash[1];
100 c = cur_hash[2];
101 d = cur_hash[3];
102 e = cur_hash[4];
104 #define w params->w
106 memcpy(w, data, 64);
107 for(int i = 0; i < 16; i++)
108 byte_swap32(&w[i]);
110 for(int i = 16; i <= 79; i++)
111 w[i] = rot_left(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
113 for(int i = 0; i<= 79; i++)
115 uint32_t f, k;
116 if(i <= 19)
118 f = (b & c) | ((~b) & d);
119 k = 0x5A827999;
121 else if(i <= 39)
123 f = b ^ c ^ d;
124 k = 0x6ED9EBA1;
126 else if(i <= 59)
128 f = (b & c) | (b & d) | (c & d);
129 k = 0x8F1BBCDC;
131 else
133 f = b ^ c ^ d;
134 k = 0xCA62C1D6;
136 uint32_t temp = rot_left(a, 5) + f + e + k + w[i];
137 e = d;
138 d = c;
139 c = rot_left(b, 30);
140 b = a;
141 a = temp;
143 #undef w
145 cur_hash[0] += a;
146 cur_hash[1] += b;
147 cur_hash[2] += c;
148 cur_hash[3] += d;
149 cur_hash[4] += e;