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

📄 encryption.c

📁 一个开源的sip源代码
💻 C
📖 第 1 页 / 共 2 页
字号:

    /* Verify that test vectors are valid */
    for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
	PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].input) == 
			    rfc2202_test_vector[i].input_len, -50);
	PJ_ASSERT_RETURN(pj_ansi_strlen(rfc2202_test_vector[i].key) == 
			    rfc2202_test_vector[i].key_len, -52);
	PJ_ASSERT_RETURN(rfc2202_test_vector[i].md5_digest==NULL ||
			    pj_ansi_strlen(rfc2202_test_vector[i].md5_digest)<=16,
			    -54);
	PJ_ASSERT_RETURN(rfc2202_test_vector[i].sha1_digest==NULL ||
			    pj_ansi_strlen(rfc2202_test_vector[i].sha1_digest)<=20,
			    -56);
    }

    /* Test HMAC-MD5 */
    PJ_LOG(3, (THIS_FILE, "  HMAC-MD5 test vectors from rfc 2202.."));
    for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
	pj_uint8_t digest_buf[18], *digest;

	if (rfc2202_test_vector[i].md5_digest == NULL)
	    continue;

	digest_buf[0] = '\0';
	digest_buf[17] = '\0';

	digest = digest_buf+1;

	pj_hmac_md5((pj_uint8_t*)rfc2202_test_vector[i].input, 
		    rfc2202_test_vector[i].input_len,
		    (pj_uint8_t*)rfc2202_test_vector[i].key, 
		    rfc2202_test_vector[i].key_len,
		    digest);

	/* Check for overwrites */
	if (digest_buf[0] != '\0' || digest_buf[17] != '\0') {
	    PJ_LOG(3, (THIS_FILE, "    error: overwriting outside buffer on test %d", i));
	    return -60;
	}

	/* Compare digest */
	if (pj_memcmp(rfc2202_test_vector[i].md5_digest, digest, 16)) {
	    PJ_LOG(3, (THIS_FILE, "    error: digest mismatch on test %d", i));
	    return -65;
	}
    }

    /* Test HMAC-SHA1 */
    PJ_LOG(3, (THIS_FILE, "  HMAC-SHA1 test vectors from rfc 2202.."));
    for (i=0; i<PJ_ARRAY_SIZE(rfc2202_test_vector); ++i) {
	pj_uint8_t digest_buf[22], *digest;

	if (rfc2202_test_vector[i].sha1_digest == NULL)
	    continue;

	digest_buf[0] = '\0';
	digest_buf[21] = '\0';

	digest = digest_buf+1;

	pj_hmac_sha1((pj_uint8_t*)rfc2202_test_vector[i].input, 
		     rfc2202_test_vector[i].input_len,
		     (pj_uint8_t*)rfc2202_test_vector[i].key, 
		     rfc2202_test_vector[i].key_len,
		     digest);

	/* Check for overwrites */
	if (digest_buf[0] != '\0' || digest_buf[21] != '\0') {
	    PJ_LOG(3, (THIS_FILE, "    error: overwriting outside buffer on test %d", i));
	    return -70;
	}

	/* Compare digest */
	if (pj_memcmp(rfc2202_test_vector[i].sha1_digest, digest, 20)) {
	    PJ_LOG(3, (THIS_FILE, "    error: digest mismatch on test %d", i));
	    return -75;
	}
    }


    /* Success */
    return 0;
}

/* CRC32 test data, generated from crc32 test on a Linux box */
struct crc32_test_t
{
    char	    *input;
    pj_uint32_t	     crc;
} crc32_test_data[] = 
{
    {
	"",
	0x0
    },
    {
	"Hello World",
	0x4a17b156
    },
    {
	/* Something read from /dev/random */
	"\x21\x21\x98\x10\x62\x59\xbc\x58\x42\x24\xe5\xf3\x92\x0a\x68\x3c\xa7\x67\x73\xc3",
	0x506693be
    },
    {
	"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
	0xcab11777
    },
    {
	"123456789",
	0xCBF43926
    }
};

/*
 * CRC32 test
 */
static int crc32_test(void)
{
    unsigned i;

    PJ_LOG(3, (THIS_FILE, "  crc32 test.."));

    /* testing pj_crc32_calc */
    for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
	pj_uint32_t crc;

	crc = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input,
			    pj_ansi_strlen(crc32_test_data[i].input));
	if (crc != crc32_test_data[i].crc) {
	    PJ_LOG(3,(THIS_FILE, "    error: crc mismatch on test %d", i));
	    return -80;
	}
    }

    /* testing incremental CRC32 calculation */
    for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
	pj_crc32_context ctx;
	pj_uint32_t crc0, crc1;
	unsigned len;

	len = pj_ansi_strlen(crc32_test_data[i].input);
	crc0 = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input, len);

	pj_crc32_init(&ctx);
	pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input,
			len / 2);

	if (len/2 > 0) {
	    pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input + len/2,
			    len - len/2);
	}

	crc1 = pj_crc32_final(&ctx);

	if (crc0 != crc1) {
	    PJ_LOG(3,(THIS_FILE, 
		      "    error: crc algorithm error on test %d", i));
	    return -85;
	}

    }
    return 0;
}


int encryption_test()
{
    int rc;

    rc = sha1_test1();
    if (rc != 0)
	return rc;

    rc = sha1_test2();
    if (rc != 0)
	return rc;

    rc = rfc2202_test();
    if (rc != 0)
	return rc;

    rc = crc32_test();
    if (rc != 0)
	return rc;

    return 0;
}

static void crc32_update(pj_crc32_context *c, const pj_uint8_t *data,
			 pj_size_t nbytes)
{
    pj_crc32_update(c, data, nbytes);
}

static void crc32_final(pj_crc32_context *ctx, pj_uint32_t *digest)
{
    *digest = pj_crc32_final(ctx);
}

int encryption_benchmark()
{
    pj_pool_t *pool;
    pj_uint8_t *input;
    union {
	pj_md5_context md5_context;
	pj_sha1_context sha1_context;
    } context;
    pj_uint8_t digest[32];
    pj_size_t input_len;
    struct algorithm
    {
	const char *name;
	void (*init_context)(void*);
	void (*update)(void*, const pj_uint8_t*, unsigned);
	void (*final)(void*, void*);
	pj_uint32_t t;
    } algorithms[] = 
    {
	{
	    "MD5  ",
	    (void (*)(void*))&pj_md5_init,
	    (void (*)(void*, const pj_uint8_t*, unsigned))&pj_md5_update,
	    (void (*)(void*, void*))&pj_md5_final
	},
	{
	    "SHA1 ",
	    (void (*)(void*))&pj_sha1_init,
	    (void (*)(void*, const pj_uint8_t*, unsigned))&pj_sha1_update,
	    (void (*)(void*, void*))&pj_sha1_final
	},
	{
	    "CRC32",
	    (void (*)(void*))&pj_crc32_init,
	    (void (*)(void*, const pj_uint8_t*, unsigned))&crc32_update,
	    (void (*)(void*, void*))&crc32_final
	}
    };
#if defined(PJ_DEBUG) && PJ_DEBUG!=0
    enum { LOOP = 1000 };
#else
    enum { LOOP = 10000 };
#endif
    unsigned i;
    double total_len;

    input_len = 2048;
    total_len = input_len * LOOP;
    pool = pj_pool_create(mem, "enc", input_len+256, 0, NULL);
    if (!pool)
	return PJ_ENOMEM;

    input = (pj_uint8_t*)pj_pool_alloc(pool, input_len);
    pj_memset(input, '\xaa', input_len);
    
    PJ_LOG(3, (THIS_FILE, "  feeding %d Mbytes of data",
	       (unsigned)(total_len/1024/1024)));

    /* Dry run */
    for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
	algorithms[i].init_context(&context);
	algorithms[i].update(&context, input, input_len);
	algorithms[i].final(&context, digest);
    }

    /* Run */
    for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
	int j;
	pj_timestamp t1, t2;

	pj_get_timestamp(&t1);
	algorithms[i].init_context(&context);
	for (j=0; j<LOOP; ++j) {
	    algorithms[i].update(&context, input, input_len);
	}
	algorithms[i].final(&context, digest);
	pj_get_timestamp(&t2);

	algorithms[i].t = pj_elapsed_usec(&t1, &t2);
    }

    /* Results */
    for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
	double bytes;

	bytes = (total_len * 1000000 / algorithms[i].t);
	PJ_LOG(3, (THIS_FILE, "    %s:%8d usec (%3d.%03d Mbytes/sec)",
		   algorithms[i].name, algorithms[i].t,
		   (unsigned)(bytes / 1024 / 1024),
		   ((unsigned)(bytes) % (1024 * 1024)) / 1024));
    }

    return 0;
}



#endif /* INCLUDE_ENCRYPTION_TEST */

⌨️ 快捷键说明

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