⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modetest.c

📁 AES加密算法的VS2005工程实现
💻 C
📖 第 1 页 / 共 4 页
字号:
                goto error1;
            if(memcmp(viv, iv, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        if(10 * sam_cnt > 9 * SAMPLE2)
        {
            *av /= sam_cnt;
            *sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
            if(*sig > (tol / 100.0) * *av)
                sam_cnt = 0;
        }
        else
        {
            if(lcnt++ == 10)
            {
                lcnt = 0; tol += 5;
                if(tol > 30)
                    return 0;
            }
            sam_cnt = 0;
        }
    }

    return 1;
#ifdef VALIDATE_IN_TIMING
error1:
    printf("\nCFB Decryption data error in timing");
    exit(1);
error2:
    printf("\nCFB Decryption iv error in timing");
    exit(1);
#endif
}

#endif

#ifdef TEST_OFB

int time_ofb_enc(unsigned int k_len, int blocks, double *av, double *sig)
{   int                 i, tol, lcnt, sam_cnt;
    double              cy, av1, sig1;
    unsigned char       key[2 * AES_BLOCK_SIZE];
    unsigned char       vb[10000 * AES_BLOCK_SIZE];
    unsigned char       viv[AES_BLOCK_SIZE];

    aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
    aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
    aligned_auto(f_ectx, ecx, 1, 16);

    block_rndfill(key, 2 * AES_BLOCK_SIZE);
    f_enc_key(ecx, key, k_len);
    block_rndfill(iv, AES_BLOCK_SIZE);
    memcpy(viv, iv, AES_BLOCK_SIZE);
    block_rndfill(pt, blocks * AES_BLOCK_SIZE);
    memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
    f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
    OFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
    if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
        goto error1;
    if(memcmp(viv, iv, AES_BLOCK_SIZE))
        goto error2;
#endif
    tol = 10; lcnt = sam_cnt = 0;
    while(!sam_cnt)
    {
        av1 = sig1 = 0.0;

        for(i = 0; i < SAMPLE1; ++i)
        {
            cy = (double)read_tsc();
            f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
            cy = (double)read_tsc() - cy;

            av1 += cy;
            sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
            OFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, iv, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        av1 /= SAMPLE1;
        sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
        sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);

        *av = *sig = 0.0;
        for(i = 0; i < SAMPLE2; ++i)
        {
            cy = (double)read_tsc();
            f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
            cy = (double)read_tsc() - cy;

            if(cy > av1 - sig1 && cy < av1 + sig1)
            {
                *av += cy;
                *sig += cy * cy;
                sam_cnt++;
            }
#ifdef VALIDATE_IN_TIMING
            OFBenc(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, iv, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        if(10 * sam_cnt > 9 * SAMPLE2)
        {
            *av /= sam_cnt;
            *sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
            if(*sig > (tol / 100.0) * *av)
                sam_cnt = 0;
        }
        else
        {
            if(lcnt++ == 10)
            {
                lcnt = 0; tol += 5;
                if(tol > 30)
                    return 0;
            }
            sam_cnt = 0;
        }
    }

    return 1;
#ifdef VALIDATE_IN_TIMING
error1:
    printf("\nOFB Encryption data error in timing");
    exit(1);
error2:
    printf("\nOFB Encryption iv error in timing");
    exit(1);
#endif
}

int time_ofb_dec(unsigned int k_len, int blocks, double *av, double *sig)
{   int                 i, tol, lcnt, sam_cnt;
    double              cy, av1, sig1;
    unsigned char       key[2 * AES_BLOCK_SIZE];
    unsigned char       vb[10000 * AES_BLOCK_SIZE];
    unsigned char       viv[AES_BLOCK_SIZE];

    aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
    aligned_auto(unsigned char, iv, AES_BLOCK_SIZE, 16);
    aligned_auto(f_ectx, ecx, 1, 16);

    block_rndfill(key, 2 * AES_BLOCK_SIZE);
    f_enc_key(ecx, key, k_len);
    block_rndfill(iv, AES_BLOCK_SIZE);
    memcpy(viv, iv, AES_BLOCK_SIZE);
    block_rndfill(pt, blocks * AES_BLOCK_SIZE);
    memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
    f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
#ifdef VALIDATE_IN_TIMING
    OFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
    if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
        goto error1;
    if(memcmp(viv, iv, AES_BLOCK_SIZE))
        goto error2;
#endif
    tol = 10; lcnt = sam_cnt = 0;
    while(!sam_cnt)
    {
        av1 = sig1 = 0.0;

        for(i = 0; i < SAMPLE1; ++i)
        {
            cy = (double)read_tsc();
            f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
            cy = (double)read_tsc() - cy;

            av1 += cy;
            sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
            OFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, iv, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        av1 /= SAMPLE1;
        sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
        sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);

        *av = *sig = 0.0;
        for(i = 0; i < SAMPLE2; ++i)
        {
            cy = (double)read_tsc();
            f_ofb_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, iv);
            cy = (double)read_tsc() - cy;

            if(cy > av1 - sig1 && cy < av1 + sig1)
            {
                *av += cy;
                *sig += cy * cy;
                sam_cnt++;
            }
#ifdef VALIDATE_IN_TIMING
            OFBdec(vb, blocks * AES_BLOCK_SIZE, viv, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, iv, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        if(10 * sam_cnt > 9 * SAMPLE2)
        {
            *av /= sam_cnt;
            *sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
            if(*sig > (tol / 100.0) * *av)
                sam_cnt = 0;
        }
        else
        {
            if(lcnt++ == 10)
            {
                lcnt = 0; tol += 5;
                if(tol > 30)
                    return 0;
            }
            sam_cnt = 0;
        }
    }

    return 1;
#ifdef VALIDATE_IN_TIMING
error1:
    printf("\nOFB Decryption data error in timing");
    exit(1);
error2:
    printf("\nOFB Decryption iv error in timing");
    exit(1);
#endif
}

#endif

#ifdef TEST_CTR

int time_ctr_crypt(unsigned int k_len, int blocks, cbuf_inc ctr_inc, double *av, double *sig)
{   int                 i, tol, lcnt, sam_cnt;
    double              cy, av1, sig1;
    unsigned char       key[2 * AES_BLOCK_SIZE];
    unsigned char       vb[10000 * AES_BLOCK_SIZE];
    unsigned char       viv[AES_BLOCK_SIZE];

    aligned_auto(unsigned char, pt, 10000 * AES_BLOCK_SIZE, 16);
    aligned_auto(unsigned char, cbuf, AES_BLOCK_SIZE, 16);
    aligned_auto(f_ectx, ecx, 1, 16);

    block_rndfill(key, 2 * AES_BLOCK_SIZE);
    f_enc_key(ecx, key, k_len);
    block_rndfill(cbuf, AES_BLOCK_SIZE);
    memcpy(viv, cbuf, AES_BLOCK_SIZE);
    block_rndfill(pt, blocks * AES_BLOCK_SIZE);
    memcpy(vb, pt, blocks * AES_BLOCK_SIZE);
    f_ctr_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, cbuf, ctr_inc);
#ifdef VALIDATE_IN_TIMING
    CTRcry(vb, blocks * AES_BLOCK_SIZE, viv, ctr_inc, ecx);
    if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
        goto error1;
    if(memcmp(viv, cbuf, AES_BLOCK_SIZE))
        goto error2;
#endif
    tol = 10; lcnt = sam_cnt = 0;
    while(!sam_cnt)
    {
        av1 = sig1 = 0.0;

        for(i = 0; i < SAMPLE1; ++i)
        {
            cy = (double)read_tsc();
            f_ctr_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, cbuf, ctr_inc);
            cy = (double)read_tsc() - cy;

            av1 += cy;
            sig1 += cy * cy;
#ifdef VALIDATE_IN_TIMING
            CTRcry(vb, blocks * AES_BLOCK_SIZE, viv, ctr_inc, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, cbuf, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        av1 /= SAMPLE1;
        sig1 = sqrt((sig1 - av1 * av1 * SAMPLE1) / SAMPLE1);
        sig1 = (sig1 < 0.05 * av1 ? 0.05 * av1 : sig1);

        *av = *sig = 0.0;
        for(i = 0; i < SAMPLE2; ++i)
        {
            cy = (double)read_tsc();
            f_ctr_cry(ecx, pt, pt, blocks * AES_BLOCK_SIZE, cbuf, ctr_inc);
            cy = (double)read_tsc() - cy;

            if(cy > av1 - sig1 && cy < av1 + sig1)
            {
                *av += cy;
                *sig += cy * cy;
                sam_cnt++;
            }
#ifdef VALIDATE_IN_TIMING
            CTRcry(vb, blocks * AES_BLOCK_SIZE, viv, ctr_inc, ecx);
            if(memcmp(pt, vb, blocks * AES_BLOCK_SIZE))
                goto error1;
            if(memcmp(viv, cbuf, AES_BLOCK_SIZE))
                goto error2;
#endif
        }

        if(10 * sam_cnt > 9 * SAMPLE2)
        {
            *av /= sam_cnt;
            *sig = sqrt((*sig - *av * *av * sam_cnt) / sam_cnt);
            if(*sig > (tol / 100.0) * *av)
                sam_cnt = 0;
        }
        else
        {
            if(lcnt++ == 10)
            {
                lcnt = 0; tol += 5;
                if(tol > 30)
                    return 0;
            }
            sam_cnt = 0;
        }
    }

    return 1;
#ifdef VALIDATE_IN_TIMING
error1:
    printf("\nOFB Decryption data error in timing");
    exit(1);
error2:
    printf("\nOFB Decryption cbuf error in timing");
    exit(1);
#endif
}

#endif

void ctr_inc(unsigned char x[AES_BLOCK_SIZE])
{
    if(!(++(x[0])))
        if(!(++(x[1])))
            if(!(++(x[2])))
                ++(x[3]);
}

#define BUFLEN  (1000 * AES_BLOCK_SIZE)

int main(void)
{   int     i, k, err, blocks, len, len2;
    double  a0, av, sig, td;
    unsigned char   buf1[BUFLEN];
    unsigned char   buf2[BUFLEN];
    unsigned char   iv1[AES_BLOCK_SIZE];
    unsigned char   iv2[AES_BLOCK_SIZE];
    unsigned char   key[32];
    f_ectx ecx1[1];
    f_dctx dcx1[1];
    aligned_auto(unsigned char, buf3, BUFLEN, 16);
    aligned_auto(unsigned char, iv3, AES_BLOCK_SIZE, 16);
    aligned_auto(f_ectx, ecx2, 1, 16);
    aligned_auto(f_dctx, dcx2, 1, 16);

#if defined( DLL_IMPORT ) && defined( DYNAMIC_LINK )
    HINSTANCE   h_dll;
#endif

#if defined( DUAL_CORE ) && defined( _WIN32 )
    // we need to constrain the process to one core in order to
    // obtain meaningful timing data
    HANDLE ph;
    DWORD_PTR afp;
    DWORD_PTR afs;
    ph = GetCurrentProcess();
    if(GetProcessAffinityMask(ph, &afp, &afs))
    {
        afp &= (GetCurrentProcessorNumber() + 1);
        if(!SetProcessAffinityMask(ph, afp))
        {
            printf("Couldn't set Process Affinity Mask\n\n"); return -1;
        }
    }
    else
    {
        printf("Couldn't get Process Affinity Mask\n\n"); return -1;
    }
#endif

#if defined( DLL_IMPORT ) && defined( DYNAMIC_LINK )
    if(!(h_dll = init_dll(&fn)))
        return -1;
#elif defined(STATIC_TABLES)
    aes_init();
#endif

	if(f_talign(0,16) != EXIT_SUCCESS)
		return -1;

    printf("\nRun tests for the AES algorithm");
#if defined( DLL_IMPORT )
    printf(" (DLL Version)");
#endif
#if defined( __cplusplus )
    printf(" (CPP Version)");
#endif

    for(k = 128; k <= 256; k += 64)
    {
        printf("\n\n%03i Bit Keys", k);
#ifdef TEST_ECB
        err = 0;
        for(i = 0; i < 100; ++i)
        {
            block_rndfill(key, 2 * AES_BLOCK_SIZE);
            f_enc_key(ecx1, key, k);
            f_enc_key(ecx2, key, k);
            f_dec_key(dcx1, key, k);
            f_dec_key(dcx2, key, k);

            block_rndfill(buf1, BUFLEN);
            memcpy(buf2, buf1, BUFLEN);
            memcpy(buf3, buf1, BUFLEN);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -