Changeset 2906268


Ignore:
Timestamp:
2016-10-16T06:58:19Z (8 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
399d65a
Parents:
6e991a9
git-author:
dequis <dx@…> (16-10-16 05:51:08)
git-committer:
dequis <dx@…> (16-10-16 06:58:19)
Message:

lib/sha1: Refactor sha1_hmac into a generic b_hmac function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/sha1.c

    r6e991a9 r2906268  
    2424#define HMAC_BLOCK_SIZE 64
    2525
    26 /* BitlBee addition: */
    27 void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, guint8 digest[SHA1_HASH_SIZE])
     26void b_hmac(GChecksumType checksum_type, const char *key_, size_t key_len,
     27            const char *payload, size_t payload_len, guint8 **digest)
    2828{
    29         sha1_state_t sha1;
    30         guint8 hash[SHA1_HASH_SIZE];
     29        GChecksum *checksum;
     30        size_t hash_len;
     31        guint8 *hash;
    3132        guint8 key[HMAC_BLOCK_SIZE + 1];
    3233        int i;
     34
     35        hash_len = g_checksum_type_get_length(checksum_type);
     36
     37        if (hash_len == (size_t) -1) {
     38                return;
     39        }
     40
     41        hash = g_malloc(hash_len);
    3342
    3443        if (key_len == 0) {
     
    4352        memset(key, 0, HMAC_BLOCK_SIZE + 1);
    4453        if (key_len > HMAC_BLOCK_SIZE) {
    45                 sha1_init(&sha1);
    46                 sha1_append(&sha1, (guint8 *) key_, key_len);
    47                 sha1_finish(&sha1, key);
     54                checksum = g_checksum_new(checksum_type);
     55                g_checksum_update(checksum, (guint8 *) key_, key_len);
     56                g_checksum_get_digest(checksum, key, &hash_len);
     57                g_checksum_free(checksum);
    4858        } else {
    4959                memcpy(key, key_, key_len);
     
    5161
    5262        /* Inner part: H(K XOR 0x36, text) */
    53         sha1_init(&sha1);
     63        checksum = g_checksum_new(checksum_type);
    5464        for (i = 0; i < HMAC_BLOCK_SIZE; i++) {
    5565                key[i] ^= 0x36;
    5666        }
    57         sha1_append(&sha1, key, HMAC_BLOCK_SIZE);
    58         sha1_append(&sha1, (const guint8 *) payload, payload_len);
    59         sha1_finish(&sha1, hash);
     67        g_checksum_update(checksum, key, HMAC_BLOCK_SIZE);
     68        g_checksum_update(checksum, (const guint8 *) payload, payload_len);
     69        g_checksum_get_digest(checksum, hash, &hash_len);
     70        g_checksum_free(checksum);
    6071
    6172        /* Final result: H(K XOR 0x5C, inner stuff) */
    62         sha1_init(&sha1);
     73        checksum = g_checksum_new(checksum_type);
    6374        for (i = 0; i < HMAC_BLOCK_SIZE; i++) {
    6475                key[i] ^= 0x36 ^ 0x5c;
    6576        }
    66         sha1_append(&sha1, key, HMAC_BLOCK_SIZE);
    67         sha1_append(&sha1, hash, SHA1_HASH_SIZE);
    68         sha1_finish(&sha1, digest);
     77        g_checksum_update(checksum, key, HMAC_BLOCK_SIZE);
     78        g_checksum_update(checksum, hash, hash_len);
     79        g_checksum_get_digest(checksum, *digest, &hash_len);
     80        g_checksum_free(checksum);
     81
     82        g_free(hash);
    6983}
     84
     85void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, guint8 digest[SHA1_HASH_SIZE])
     86{
     87        b_hmac(G_CHECKSUM_SHA1, key_, key_len, payload, payload_len, &digest);
     88}
     89
    7090
    7191/* I think this follows the scheme described on:
Note: See TracChangeset for help on using the changeset viewer.