Codebase list libnice / ae66316
Add some new APIs to sha1 module, unused for now Youness Alaoui 13 years ago
2 changed file(s) with 75 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
1212 */
1313
1414 #include "sha1.h"
15
1516 #include <string.h>
16
1717
1818
1919 /* ===== start - public domain SHA1 implementation ===== */
261261 /* ===== end - public domain SHA1 implementation ===== */
262262
263263
264 void HMACInit(HMAC_CTX* context, const uint8_t *key, size_t key_len)
265 {
266 unsigned char ipad[64]; /* padding - key XORd with ipad */
267 unsigned char tk[20];
268 size_t i;
269
270 /* if key is longer than 64 bytes reset it to key = SHA1(key) */
271 if (key_len > 64) {
272 sha1_vector(1, &key, &key_len, tk);
273 key = tk;
274 key_len = 20;
275 }
276
277 /* start out by storing key in ipad */
278 memset(ipad, 0, sizeof(ipad));
279 memcpy(ipad, key, key_len);
280
281 /* XOR key with ipad values */
282 for (i = 0; i < 64; i++)
283 ipad[i] ^= 0x36;
284
285 /* Store the key in our context */
286 memcpy(context->key, key, key_len);
287 context->key_len = key_len;
288
289 SHA1Init (&context->context);
290 SHA1Update (&context->context, ipad, sizeof(ipad));
291 }
292
293 void HMACUpdate(HMAC_CTX *context, const void *data, uint32_t len)
294 {
295 SHA1Update (&context->context, data, len);
296 }
297
298 void HMACFinal(unsigned char digest[20], HMAC_CTX *context)
299 {
300
301 unsigned char opad[64]; /* padding - key XORd with opad */
302 unsigned char sha1_digest[SHA1_MAC_LEN];
303 const uint8_t *_addr[2];
304 size_t _len[2];
305 size_t i;
306
307 SHA1Final (sha1_digest, &context->context);
308
309 memset(opad, 0, sizeof(opad));
310 memcpy(opad, context->key, context->key_len);
311
312 /* XOR key with opad values */
313 for (i = 0; i < 64; i++)
314 opad[i] ^= 0x5c;
315
316 /* perform outer SHA1 */
317 _addr[0] = opad;
318 _len[0] = 64;
319 _addr[1] = sha1_digest;
320 _len[1] = SHA1_MAC_LEN;
321 sha1_vector(2, _addr, _len, digest);
322 }
323
264324
265325 /**
266326 * hmac_sha1_vector:
1414 #ifndef SHA1_H
1515 #define SHA1_H
1616
17 #ifdef _WIN32
18 #include "win32_common.h"
19 #else
2017 #include <stdint.h>
21 #endif
2218 #include <stddef.h>
2319
2420 #define SHA1_MAC_LEN 20
2521
26
2722 struct SHA1Context {
28 uint32_t state[5];
29 uint32_t count[2];
30 unsigned char buffer[64];
23 uint32_t state[5];
24 uint32_t count[2];
25 unsigned char buffer[64];
3126 };
3227
3328 typedef struct SHA1Context SHA1_CTX;
3530 void SHA1Init(SHA1_CTX *context);
3631 void SHA1Update(SHA1_CTX *context, const void *data, uint32_t len);
3732 void SHA1Final(unsigned char digest[20], SHA1_CTX *context);
33
34 struct HMACContext {
35 SHA1_CTX context;
36 uint8_t key[64];
37 size_t key_len;
38 };
39 typedef struct HMACContext HMAC_CTX;
40
41 void HMACInit(HMAC_CTX *context, const uint8_t *key, size_t key_len);
42 void HMACUpdate(HMAC_CTX *context, const void *data, uint32_t len);
43 void HMACFinal(unsigned char digest[20], HMAC_CTX *context);
3844
3945 void sha1_vector(size_t num_elem, const uint8_t *addr[], const size_t *len,
4046 uint8_t *mac);