Example #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);
}
Example #2
0
int fcrypt_init(
    int mode,                               /* the mode to be used (input)          */
    const unsigned char pwd[],              /* the user specified password (input)  */
    unsigned int pwd_len,                   /* the length of the password (input)   */
    const unsigned char salt[],             /* the salt (input)                     */
#ifdef PASSWORD_VERIFIER
    unsigned char pwd_ver[PWD_VER_LENGTH],  /* 2 byte password verifier (output)    */
#endif
    fcrypt_ctx      cx[1])                  /* the file encryption context (output) */
{
    unsigned char kbuf[2 * MAX_KEY_LENGTH + PWD_VER_LENGTH];

    if(pwd_len > MAX_PWD_LENGTH)
        return PASSWORD_TOO_LONG;

    if(mode < 1 || mode > 3)
        return BAD_MODE;

    cx->mode = mode;
    cx->pwd_len = pwd_len;
    /* initialise the encryption nonce and buffer pos   */
    cx->encr_pos = BLOCK_SIZE;

    /* if we need a random component in the encryption  */
    /* nonce, this is where it would have to be set     */
    memset(cx->nonce, 0, BLOCK_SIZE * sizeof(unsigned char));
    /* initialise for authentication			        */
    hmac_sha_begin(cx->auth_ctx);

    /* derive the encryption and authetication keys and the password verifier   */
    derive_key(pwd, pwd_len, salt, SALT_LENGTH(mode), KEYING_ITERATIONS,
                        kbuf, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);
    /* set the encryption key							*/
    aes_encrypt_key(kbuf, KEY_LENGTH(mode), cx->encr_ctx);
    /* set the authentication key						*/
    hmac_sha_key(kbuf + KEY_LENGTH(mode), KEY_LENGTH(mode), cx->auth_ctx);
#ifdef PASSWORD_VERIFIER
    memcpy(pwd_ver, kbuf + 2 * KEY_LENGTH(mode), PWD_VER_LENGTH);
#endif
    /* clear the buffer holding the derived key values	*/
    memset(kbuf, 0, 2 * KEY_LENGTH(mode) + PWD_VER_LENGTH);

    return GOOD_RETURN;
}
Example #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;
}