source: lib/sha1.c @ 0b9daac

Last change on this file since 0b9daac was 0b9daac, checked in by dequis <dx@…>, at 2015-02-20T23:16:08Z

Reorganize include files to avoid conflicts with other libs

  • Change all header includes to be relative to the project root
  • Remove -I${includedir} from bitlbee.pc Cflags
  • Install lib and protocols headers to their own directories. So now it is:

/usr/include/bitlbee/*.h
/usr/include/bitlbee/lib/*.h
/usr/include/bitlbee/protocols/*.h

This breaks backwards compatibility of third party plugins, but now
they don't have to do ambiguous includes like #include <proxy.h>

This also fixes trac ticket 1170 - conflicts when libproxy and liboauth
are installed at the same time bitlbee is built, which the macports
project ran into several times.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include "lib/sha1.h"
2#include <string.h>
3#include <stdio.h>
4
5
6
7void sha1_init(sha1_state_t *ctx)
8{
9        *ctx = g_checksum_new(G_CHECKSUM_SHA1);
10}
11
12void sha1_append(sha1_state_t *ctx, const guint8 * message_array, guint len)
13{
14        g_checksum_update(*ctx, message_array, len);
15}
16
17void sha1_finish(sha1_state_t *ctx, guint8 digest[SHA1_HASH_SIZE])
18{
19        gsize digest_len = SHA1_HASH_SIZE;
20
21        g_checksum_get_digest(*ctx, digest, &digest_len);
22        g_checksum_free(*ctx);
23}
24
25#define HMAC_BLOCK_SIZE 64
26
27/* BitlBee addition: */
28void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, guint8 digest[SHA1_HASH_SIZE])
29{
30        sha1_state_t sha1;
31        guint8 hash[SHA1_HASH_SIZE];
32        guint8 key[HMAC_BLOCK_SIZE + 1];
33        int i;
34
35        if (key_len == 0) {
36                key_len = strlen(key_);
37        }
38        if (payload_len == 0) {
39                payload_len = strlen(payload);
40        }
41
42        /* Create K. If our current key is >64 chars we have to hash it,
43           otherwise just pad. */
44        memset(key, 0, HMAC_BLOCK_SIZE + 1);
45        if (key_len > HMAC_BLOCK_SIZE) {
46                sha1_init(&sha1);
47                sha1_append(&sha1, (guint8 *) key_, key_len);
48                sha1_finish(&sha1, key);
49        } else {
50                memcpy(key, key_, key_len);
51        }
52
53        /* Inner part: H(K XOR 0x36, text) */
54        sha1_init(&sha1);
55        for (i = 0; i < HMAC_BLOCK_SIZE; i++) {
56                key[i] ^= 0x36;
57        }
58        sha1_append(&sha1, key, HMAC_BLOCK_SIZE);
59        sha1_append(&sha1, (const guint8 *) payload, payload_len);
60        sha1_finish(&sha1, hash);
61
62        /* Final result: H(K XOR 0x5C, inner stuff) */
63        sha1_init(&sha1);
64        for (i = 0; i < HMAC_BLOCK_SIZE; i++) {
65                key[i] ^= 0x36 ^ 0x5c;
66        }
67        sha1_append(&sha1, key, HMAC_BLOCK_SIZE);
68        sha1_append(&sha1, hash, SHA1_HASH_SIZE);
69        sha1_finish(&sha1, digest);
70}
71
72/* I think this follows the scheme described on:
73   http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
74   My random data comes from a SHA1 generator but hey, it's random enough for
75   me, and RFC 4122 looks way more complicated than I need this to be.
76
77   Returns a value that must be free()d. */
78char *sha1_random_uuid(sha1_state_t * context)
79{
80        guint8 dig[SHA1_HASH_SIZE];
81        char *ret = g_new0(char, 40);   /* 36 chars + \0 */
82        int i, p;
83
84        sha1_finish(context, dig);
85        for (p = i = 0; i < 16; i++) {
86                if (i == 4 || i == 6 || i == 8 || i == 10) {
87                        ret[p++] = '-';
88                }
89                if (i == 6) {
90                        dig[i] = (dig[i] & 0x0f) | 0x40;
91                }
92                if (i == 8) {
93                        dig[i] = (dig[i] & 0x30) | 0x80;
94                }
95
96                sprintf(ret + p, "%02x", dig[i]);
97                p += 2;
98        }
99        ret[p] = '\0';
100
101        return ret;
102}
Note: See TracBrowser for help on using the repository browser.