Ejemplo n.º 1
0
/* 'do it all in one go' subroutine     */
void hmac_sha(const unsigned char key[], unsigned long key_len,
          const unsigned char data[], unsigned long data_len,
          unsigned char mac[], unsigned long mac_len)
{   hmac_ctx    cx[1];

    hmac_sha_begin(cx);
    hmac_sha_key(key, key_len, cx);
    hmac_sha_data(data, data_len, cx);
    hmac_sha_end(mac, mac_len, cx);
}
Ejemplo n.º 2
0
/* compute and output the MAC value */
void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])
{   unsigned char dig[HASH_OUTPUT_SIZE];
    unsigned int i;

    /* if no data has been entered perform a null data phase        */
    if(cx->klen != HMAC_IN_DATA)
        hmac_sha_data((const unsigned char*)0, 0, cx);

    sha_end(dig, cx->ctx);         /* complete the inner hash      */

    /* set outer key value using opad and removing ipad */
    for(i = 0; i < HASH_INPUT_SIZE / sizeof(unsigned long); ++i)
        ((unsigned long*)cx->key)[i] ^= OPAD ^ IPAD;

    /* perform the outer hash operation */
    sha_begin(cx->ctx);
    sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx);
    sha_hash(dig, HASH_OUTPUT_SIZE, cx->ctx);
    sha_end(dig, cx->ctx);

    /* output the hash value            */
    for(i = 0; i < mac_len; ++i)
        mac[i] = dig[i];
}
Ejemplo n.º 3
0
int main()
{   unsigned int    i, j, k, key_len = 64;
    unsigned char   mac[HMAC_MAX_OUTPUT_SIZE];

#ifdef SHA_1
    for(i = 0; i < SHA1_TESTS; ++i)
    {
        hmac_sha(HMAC_SHA1, t_sha1[i].key, t_sha1[i].key_len, t_sha1[i].text, t_sha1[i].txt_len, mac, t_sha1[i].mac_len);

        printf("\nHMAC-SHA1 test %i, ", i + 1);
        printf("key %s", memcmp(t_sha1[i].mac, mac, t_sha1[i].mac_len) ? "is bad" : "is good");
        printf("\n");
        for(j = 0; j < t_sha1[i].mac_len; j += 4)
            printf("0x%02x%02x%02x%02x ", mac[j], mac[j + 1], mac[j + 2], mac[j + 3]);
        printf("\n");
    }

    for(i = 0; i < SHA1_TESTS; ++i)
    {   hmac_ctx    cx[1];

        hmac_sha_begin(HMAC_SHA1, cx);
        hmac_sha_key(t_sha1[i].key, t_sha1[i].key_len / 2, cx);
        hmac_sha_key(t_sha1[i].key + t_sha1[i].key_len / 2, t_sha1[i].key_len - t_sha1[i].key_len / 2, cx);

        hmac_sha_data(t_sha1[i].text, t_sha1[i].txt_len / 2, cx);
        hmac_sha_data(t_sha1[i].text + t_sha1[i].txt_len / 2, t_sha1[i].txt_len - t_sha1[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_sha1[i].mac_len, cx);

        printf("\nHMAC-SHA1 test %i, ", i + 1);
        printf("mac %s", memcmp(t_sha1[i].mac, mac, t_sha1[i].mac_len) ? "is bad" : "is good");
        printf("\n");
        for(j = 0; j < t_sha1[i].mac_len; j += 4)
            printf("0x%02x%02x%02x%02x ", mac[j], mac[j + 1], mac[j + 2], mac[j + 3]);
        printf("\n");
    }
#endif

#ifdef SHA_256
    for(i = 0; i < SHA256_TESTS; ++i)
    {
        hmac_sha(HMAC_SHA256, t_sha256[i].key, t_sha256[i].key_len, t_sha256[i].text,
                                t_sha256[i].txt_len, mac,t_sha256[i].mac_len);

        printf("\nHMAC-SHA256 test %i, ", i + 1);
        printf("mac %s", memcmp(t_sha256[i].mac, mac, t_sha256[i].mac_len)
                                                ? "is bad" : "is good");

        for(j = 0; j < t_sha256[i].mac_len; j += 4)
        {
            if(j % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[j], mac[j + 1], mac[j + 2], mac[j + 3]);
        }
        printf("\n");
    }

    for(i = 0; i < SHA256_TESTS; ++i)
    {   hmac_ctx    cx[1];

        hmac_sha_begin(HMAC_SHA256, cx);
        hmac_sha_key(t_sha256[i].key, t_sha256[i].key_len / 2, cx);
        hmac_sha_key(t_sha256[i].key + t_sha256[i].key_len / 2,
                        t_sha256[i].key_len - t_sha256[i].key_len / 2, cx);

        hmac_sha_data(t_sha256[i].text, t_sha256[i].txt_len / 2, cx);
        hmac_sha_data(t_sha256[i].text + t_sha256[i].txt_len / 2,
                        t_sha256[i].txt_len - t_sha256[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_sha256[i].mac_len, cx);

        printf("\nHMAC-SHA256 test %i, ", i + 1);
        printf("mac %s", memcmp(t_sha256[i].mac, mac, t_sha256[i].mac_len)
                                                ? "is bad" : "is good");

        for(j = 0; j < t_sha256[i].mac_len; j += 4)
        {
            if(j % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[j], mac[j + 1], mac[j + 2], mac[j + 3]);
        }
        printf("\n");
    }
#endif

#ifdef SHA_224
    for(i = 0; i < SHA2_TESTS; ++i)
    {   hmac_ctx cx[1];
        hmac_sha_begin(HMAC_SHA224, cx);
        hmac_sha_key(t_s2[i].key, t_s2[i].key_len / 2, cx);
        hmac_sha_key(t_s2[i].key + t_s2[i].key_len / 2,
                    t_s2[i].key_len - t_s2[i].key_len / 2, cx);

        hmac_sha_data(t_s2[i].text, t_s2[i].txt_len / 2, cx);
        hmac_sha_data(t_s2[i].text + t_s2[i].txt_len / 2,
                        t_s2[i].txt_len - t_s2[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_s2[i].mac_len[0], cx);

        printf("\nHMAC-SHA224 test %i, ", i + 1);
        printf("mac %s", memcmp(t_s2[i].r224, mac, t_s2[i].mac_len[0])
                                                ? "is bad" : "is good");
        for(k = 0; k < t_s2[i].mac_len[0]; k += 4)
        {
            if(k % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[k], mac[k + 1], mac[k + 2], mac[k + 3]);
        }
        printf("\n");
    }
#endif

#ifdef SHA_256
    for(i = 0; i < SHA2_TESTS; ++i)
    {   hmac_ctx cx[1];
        hmac_sha_begin(HMAC_SHA256, cx);
        hmac_sha_key(t_s2[i].key, t_s2[i].key_len / 2, cx);
        hmac_sha_key(t_s2[i].key + t_s2[i].key_len / 2,
                    t_s2[i].key_len - t_s2[i].key_len / 2, cx);

        hmac_sha_data(t_s2[i].text, t_s2[i].txt_len / 2, cx);
        hmac_sha_data(t_s2[i].text + t_s2[i].txt_len / 2,
                        t_s2[i].txt_len - t_s2[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_s2[i].mac_len[1], cx);

        printf("\nHMAC-SHA256 test %i, ", i + 1);
        printf("mac %s", memcmp(t_s2[i].r256, mac, t_s2[i].mac_len[1])
                                                ? "is bad" : "is good");
        for(k = 0; k < t_s2[i].mac_len[1]; k += 4)
        {
            if(k % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[k], mac[k + 1], mac[k + 2], mac[k + 3]);
        }
        printf("\n");
    }
#endif

#ifdef SHA_384
    for(i = 0; i < SHA2_TESTS; ++i)
    {   hmac_ctx cx[1];
        hmac_sha_begin(HMAC_SHA384, cx);
        hmac_sha_key(t_s2[i].key, t_s2[i].key_len / 2, cx);
        hmac_sha_key(t_s2[i].key + t_s2[i].key_len / 2,
                    t_s2[i].key_len - t_s2[i].key_len / 2, cx);

        hmac_sha_data(t_s2[i].text, t_s2[i].txt_len / 2, cx);
        hmac_sha_data(t_s2[i].text + t_s2[i].txt_len / 2,
                        t_s2[i].txt_len - t_s2[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_s2[i].mac_len[2], cx);

        printf("\nHMAC-SHA384 test %i, ", i + 1);
        printf("mac %s", memcmp(t_s2[i].r384, mac, t_s2[i].mac_len[2])
                                                ? "is bad" : "is good");
        for(k = 0; k < t_s2[i].mac_len[2]; k += 4)
        {
            if(k % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[k], mac[k + 1], mac[k + 2], mac[k + 3]);
        }
        printf("\n");
    }
#endif

#ifdef SHA_512
    for(i = 0; i < SHA2_TESTS; ++i)
    {   hmac_ctx cx[1];
        hmac_sha_begin(HMAC_SHA512, cx);
        hmac_sha_key(t_s2[i].key, t_s2[i].key_len / 2, cx);
        hmac_sha_key(t_s2[i].key + t_s2[i].key_len / 2,
                    t_s2[i].key_len - t_s2[i].key_len / 2, cx);

        hmac_sha_data(t_s2[i].text, t_s2[i].txt_len / 2, cx);
        hmac_sha_data(t_s2[i].text + t_s2[i].txt_len / 2,
                        t_s2[i].txt_len - t_s2[i].txt_len / 2, cx);

        hmac_sha_end(mac, t_s2[i].mac_len[3], cx);

        printf("\nHMAC-SHA512 test %i, ", i + 1);
        printf("mac %s", memcmp(t_s2[i].r512, mac, t_s2[i].mac_len[3])
                                                ? "is bad" : "is good");
        for(k = 0; k < t_s2[i].mac_len[3]; k += 4)
        {
            if(k % 16 == 0)
                printf("\n");
            printf("0x%02x%02x%02x%02x ", mac[k], mac[k + 1], mac[k + 2], mac[k + 3]);
        }
        printf("\n");
    }
#endif

    printf("\n\n");
    return 0;
}
Ejemplo n.º 4
0
void fcrypt_decrypt(unsigned char data[], unsigned int data_len, fcrypt_ctx cx[1])
{
    hmac_sha_data(data, data_len, cx->auth_ctx);
    encr_data(data, data_len, cx);
}