示例#1
0
int fcrypt_end(unsigned char mac[], fcrypt_ctx cx[1])
{
    /*unsigned int res = cx->mode;*/

    hmac_sha_end(mac, MAC_LENGTH(cx->mode), cx->auth_ctx);
    memset(cx, 0, sizeof(fcrypt_ctx));	/* clear the encryption context	*/
    return MAC_LENGTH(res);		/* return MAC length in bytes   */
}
示例#2
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);
}
示例#3
0
文件: hmac_test.c 项目: HydAu/SHA
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;
}