亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? crypto.c

?? 斯坦福大學密碼學家Boneh的基于身份的公鑰密碼系統
?? C
字號:
/* Crypto layer * Currently wrappers around OpenSSL functions * possibly substitute with other library in future * Ben Lynn *//*Copyright (C) 2001 Benjamin Lynn (blynn@cs.stanford.edu)See LICENSE for license*/#include <stdarg.h>#include <string.h>#include <openssl/rand.h>#include <openssl/bn.h>#include <gmp.h>#include "crypto.h"#include "byte_string.h"//OpenSSL will be changed soon, I've started preparing for this//when we upgrade to 0.9.7 remove these macros:#define do_nothing ((void) (0))#define HMAC_CTX_init(x) do_nothing#define HMAC_CTX_cleanup(x) HMAC_cleanup(x)static EVP_MD *md;static int md_length;static EVP_CIPHER* cipher;static int iv_length;void crypto_init(void)//NOT THREAD-SAFE. May add contexts here eventually...{    //OpenSSL_add_all_algorithms();    //md = EVP_get_digestbyname("SHA1");    md = EVP_sha1();    md_length = EVP_MD_size(md);    cipher = EVP_des_ede3_cbc();    iv_length =  EVP_CIPHER_iv_length(cipher);}void crypto_clear(void){    //Not needed(?):    //EVP_cleanup();}void crypto_hash(byte_string_t md_value, byte_string_t bs)//hash a byte_string to a byte_string of certain length//TODO: clarify this{    EVP_MD_CTX mdctx;    int i;    byte_string_init(md_value, md_length); //EVP_MAX_MD_SIZE    EVP_DigestInit(&mdctx, md);    EVP_DigestUpdate(&mdctx, bs->data, bs->len);    EVP_DigestFinal(&mdctx, md_value->data, &i);}void crypto_va_hash(byte_string_t md_value, int n, ...)//vararg version//hashes the concatenation of some number of byte_strings{    EVP_MD_CTX mdctx;    va_list ap;    int i;    byte_string_ptr bs;    byte_string_init(md_value, md_length);    va_start(ap, n);    EVP_DigestInit(&mdctx, md);    for (i=0; i<n; i++) {	bs = (byte_string_ptr) va_arg(ap, void *);	EVP_DigestUpdate(&mdctx, bs->data, bs->len);    }    va_end(ap);    EVP_DigestFinal(&mdctx, md_value->data, &i);}int crypto_generate_key(byte_string_t key){    byte_string_init(key, EVP_MAX_KEY_LENGTH);    if (1 != RAND_bytes(key->data, key->len)) {	byte_string_clear(key);	return 0;    }    return 1;}int crypto_generate_salt(byte_string_t salt){    int saltlen = iv_length;    byte_string_init(salt, saltlen);    if (1 != RAND_bytes(salt->data, saltlen)) {	byte_string_clear(salt);	return 0;    }    return 1;}void crypto_ctx_init(crypto_ctx_t c){    HMAC_CTX_init(&c->macctx);    EVP_CIPHER_CTX_init(&c->ctx);    c->auxbuf->len = 0;    c->ivbuf->len = 0;}void crypto_ctx_clear(crypto_ctx_t c){    HMAC_CTX_cleanup(&c->macctx);    EVP_CIPHER_CTX_cleanup(&c->ctx);    if (c->auxbuf->len) {	byte_string_clear(c->auxbuf);    }    if (c->ivbuf->len) {	byte_string_clear(c->ivbuf);    }}void crypto_make_chkeys(byte_string_t ckey, byte_string_t hkey, byte_string_t secret)//make sure ckey and hkey are independent by appending different characters//to `secret' and then hashing it{    byte_string_t bs1, bs2;    byte_string_init(bs1, secret->len + 1);    byte_string_init(bs2, secret->len + 1);    memcpy(bs1->data, secret->data, secret->len);    bs1->data[secret->len] = '1';    memcpy(bs2->data, secret->data, secret->len);    bs2->data[secret->len] = '2';    byte_string_init(ckey, EVP_MAX_KEY_LENGTH);    EVP_BytesToKey(cipher, md, NULL, bs1->data,	    bs1->len, 1, ckey->data, NULL);    crypto_hash(hkey, bs2);    byte_string_clear(bs1);    byte_string_clear(bs2);}int crypto_encrypt_init(crypto_ctx_t c, byte_string_t secret){    byte_string_t hkey, ckey;    if (1 != crypto_generate_salt(c->ivbuf)) {	//error: crypto_generate_salt failed	return 0;    }    c->state = 0;    crypto_make_chkeys(ckey, hkey, secret);    if (1 != EVP_EncryptInit(&c->ctx, cipher, ckey->data, c->ivbuf->data)) {	//error: EVP_EncryptInit failed	return 0;    }    HMAC_Init(&c->macctx, hkey->data, hkey->len, md);    byte_string_clear(hkey);    byte_string_clear(ckey);    return 1;}int crypto_encrypt_update(crypto_ctx_t c, unsigned char *out, int *outl,	unsigned char *in, int inl){    if (!c->state) {	//the first time we need to prepend the IV	c->state++;	memcpy(out, c->ivbuf->data, iv_length);	byte_string_clear(c->ivbuf);	if (1 != EVP_EncryptUpdate(&c->ctx, &out[iv_length], outl, in, inl)) {	    //error: EVP_EncryptUpdate failed	    return 0;	}	*outl += iv_length;    } else {	if (1 != EVP_EncryptUpdate(&c->ctx, out, outl, in, inl)) {	    //error: EVP_EncryptUpdate failed	    return 0;	}    }    HMAC_Update(&c->macctx, out, *outl);    return 1;}int crypto_encrypt_final(crypto_ctx_t c, unsigned char *out, int *outl){    byte_string_t mac;    if (1 != EVP_EncryptFinal(&c->ctx, out, outl)) {	//error: EVP_EncryptFinal failed	return 0;    }    //append the MAC    HMAC_Update(&c->macctx, out, *outl);    byte_string_init(mac, md_length);    HMAC_Final(&c->macctx, mac->data, &mac->len);    memcpy(&out[*outl], mac->data, md_length);    *outl += md_length;    byte_string_clear(mac);    return 1;}int crypto_encrypt(byte_string_t ctext, byte_string_t ptext,	byte_string_t secret)//encrypt short messages that fit in memory{    int i;    crypto_ctx_t c;    byte_string_init(ctext, ptext->len + crypto_block_size());    crypto_ctx_init(c);    if (1 != crypto_encrypt_init(c, secret)) {	byte_string_clear(ctext);	crypto_ctx_clear(c);	return 0;    }    if (1 != crypto_encrypt_update(c, ctext->data, &i, ptext->data, ptext->len)) {	byte_string_clear(ctext);	crypto_ctx_clear(c);	return 0;    }    if (1 != crypto_encrypt_final(c, &ctext->data[i], &ctext->len)) {	byte_string_clear(ctext);	crypto_ctx_clear(c);	return 0;    }    crypto_ctx_clear(c);    ctext->len += i;    byte_string_reinit(ctext, ctext->len);    return 1;}int crypto_decrypt_init(crypto_ctx_t c, byte_string_t secret){    byte_string_t hkey;    crypto_make_chkeys(c->auxbuf, hkey, secret);    c->state = 0;    byte_string_init(c->ivbuf, iv_length);    c->count = 0;    HMAC_Init(&c->macctx, hkey->data, hkey->len, md);    byte_string_clear(hkey);    return 1;}static int crypto_decrypt_advance(crypto_ctx_t c,	unsigned char **out, int *outl,	unsigned char *in, int inl){    int inc;    HMAC_Update(&c->macctx, in, inl);    if (1 != EVP_DecryptUpdate(&c->ctx, *out, &inc, in, inl)) {	///error: EVP_DecryptUpdate failed	return 0;    }    *out += inc;    *outl += inc;    return 1;}int crypto_decrypt_update(crypto_ctx_t c, unsigned char *out, int *outl,	unsigned char *in, int inl)//complicated by two things:////1. the IV is prepended to the ciphertext, but we may get passed the//ciphertext in very small chunks////2. the size of the ciphertext is not known in//advance, thus the only way of finding the MAC at the end is to//keep the last few bytes in a special buffer{    unsigned char *vin;    int vinl;    unsigned char *vout = out;    *outl = 0;    if (!c->state) {	//IV has not been completely read yet	//DecryptInit() has not been called yet	//auxbuf holds the key for now	int l;	if (c->count + inl >= iv_length) {	    l = iv_length - c->count;	    c->state++;	} else {	    l = inl;	}	memcpy(&c->ivbuf->data[c->count], in, l);	c->count += l;	if (!c->state) {	    //no state change, then we're done	    return 1;	} else {	    //state change means that IV has finished reading	    //can now setup context for decryption	    if (1 != EVP_DecryptInit(&c->ctx, cipher,			c->auxbuf->data, c->ivbuf->data)) {		///error: EVP_DecryptInit failed		return 0;	    }	    HMAC_Update(&c->macctx, c->ivbuf->data, c->ivbuf->len);	    byte_string_clear(c->ivbuf);	    //now auxbuf holds the last md_length bytes of ciphertext	    byte_string_reinit(c->auxbuf, md_length);	    c->count = 0;	    vin = &in[l];	    vinl = inl - l;	    if (!vinl) return 1;	}    } else {	vin = in;	vinl = inl;    }    if (1) { //assert(c->state);	//auxbuf holds the last md_length bytes received so far	int flushcount = c->count + vinl - md_length;	if (flushcount > 0) {	    if (c->count <= flushcount) {		//full buffer flush		if (c->count) {		    if (1 != crypto_decrypt_advance(c, &vout, outl, c->auxbuf->data, c->count)) return 0;		}		flushcount -= c->count;		//and we may be able to decrypt some more		if (flushcount > 0) {		    if (1 != crypto_decrypt_advance(c, &vout, outl, vin, flushcount)) return 0;		}		//lastly we refill auxbuf		memcpy(c->auxbuf->data, &vin[flushcount], md_length);		c->count = md_length;	    } else {		//partial buffer flush		if (1 != crypto_decrypt_advance(c, &vout, outl, c->auxbuf->data, flushcount)) return 0;		//move unflushed part to beginning		memmove(c->auxbuf->data, &c->auxbuf->data[flushcount],			c->count - flushcount);		c->count -= flushcount;		//keep filling auxbuf		memcpy(&c->auxbuf->data[c->count], vin, vinl);		c->count += vinl;	    }	} else {	    //keep filling auxbuf	    memcpy(&c->auxbuf->data[c->count], vin, vinl);	    c->count += vinl;	}    }    return 1;}int crypto_decrypt_final(crypto_ctx_t c, unsigned char *out, int *outl){    byte_string_t mac;    if (c->count != md_length) {	//error: ctext was too short to contain MAC!	printf("ctext was too short to contain MAC!\n");	return 0;    }    byte_string_init(mac, md_length);    HMAC_Final(&c->macctx, mac->data, &mac->len);    if (byte_string_cmp(mac, c->auxbuf)) {	//error: MAC mismatch	printf("MAC mismatch!\n");	byte_string_clear(mac);	return 0;    }    byte_string_clear(mac);    byte_string_clear(c->auxbuf);    if (1 != EVP_DecryptFinal(&c->ctx, out, outl)) {	//error: EVP_DecryptFinal failed	return 0;    }    return 1;}int crypto_decrypt(byte_string_t ptext, byte_string_t ctext,	byte_string_t secret)//decrypt short messages that fit in memory{    int i;    crypto_ctx_t c;    byte_string_init(ptext, ctext->len + crypto_block_size());    crypto_ctx_init(c);    if (1 != crypto_decrypt_init(c, secret)) {	//error: crypto_decrypt_init failed		byte_string_clear(ptext);	crypto_ctx_clear(c);	return 0;    }    if (1 != crypto_decrypt_update(c, ptext->data, &i, ctext->data, ctext->len)) {	//error: crypto_decrypt_update failed		byte_string_clear(ptext);	crypto_ctx_clear(c);	return 0;    }    if (1 != crypto_decrypt_final(c, &ptext->data[i], &ptext->len)) {	//error: crypto_decrypt_final failed		printf("crypto_decrypt_final failed\n");	byte_string_clear(ptext);	crypto_ctx_clear(c);	return 0;    }    crypto_ctx_clear(c);    ptext->len += i;    byte_string_reinit(ptext, ptext->len);    return 1;}void mympz_get_bn(mpz_t z, BIGNUM *bn)//bn = z{    int i;    BN_zero(bn);    i = mpz_sizeinbase(z, 2) - 1;    while (i>=0) {	if (mpz_tstbit(z, i)) BN_set_bit(bn, i);	i--;    }}void mympz_set_bn(mpz_t z, BIGNUM *bn)//z = bn{    int i;    mpz_set_ui(z, 0);    i = BN_num_bits(bn) - 1;    while (i>=0) {	if (BN_is_bit_set(bn, i)) mpz_setbit(z, i);	i--;    }}void mympz_randomm(mpz_t x, mpz_t limit)//x = random in {0, ..., limit - 1}{    BIGNUM *bnx, *bnl;    bnx = BN_new();    bnl = BN_new();    mympz_get_bn(limit, bnl);    BN_rand_range(bnx, bnl);    mympz_set_bn(x, bnx);    BN_free(bnx);    BN_free(bnl);}void mympz_randomb(mpz_t x, int bits){    BIGNUM *bnx;    bnx = BN_new();    BN_rand(bnx, bits, 0, 0);    mympz_set_bn(x, bnx);    BN_free(bnx);}int crypto_rand_bytes(unsigned char *r, int len){    //could read from /dev/random instead    return RAND_bytes(r, len);}int crypto_block_size(){    return EVP_CIPHER_block_size(cipher) + iv_length + md_length;}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久精品视频在线看| 成人国产电影网| 欧美日本乱大交xxxxx| 一级日本不卡的影视| 91亚洲永久精品| 亚洲视频在线一区观看| 91福利在线免费观看| 亚洲一区在线观看免费| 9191精品国产综合久久久久久| 香蕉影视欧美成人| 欧美一区二区视频观看视频| 久久精品99国产精品| 久久午夜国产精品| 91麻豆精品视频| 日韩福利视频导航| 国产三级精品视频| 欧美亚洲高清一区二区三区不卡| 亚洲va欧美va天堂v国产综合| 日韩一二三四区| 成人app软件下载大全免费| 亚洲国产美女搞黄色| 亚洲精品一区二区三区四区高清 | 日本午夜一区二区| 久久这里只有精品6| 不卡视频免费播放| 偷偷要91色婷婷| 久久久久久久久久久久久夜| 成人综合婷婷国产精品久久蜜臀| 国产精品美女久久久久aⅴ| 99re8在线精品视频免费播放| 一区二区三区不卡视频| 91精品国产入口在线| 狠狠网亚洲精品| 自拍偷拍欧美激情| 在线不卡一区二区| 午夜视频久久久久久| 欧美激情一二三区| 欧美日韩一区二区三区免费看| 免费观看在线综合色| 国产视频亚洲色图| 欧美自拍偷拍午夜视频| 久久国产精品免费| 中文字幕一区在线观看视频| 欧美日韩一区二区三区四区五区| 久久激五月天综合精品| 亚洲四区在线观看| 日韩欧美一区电影| 91一区二区在线观看| 免费在线观看一区二区三区| 精品国产1区二区| 激情亚洲综合在线| 精品日韩一区二区| 国产ts人妖一区二区| 欧美激情在线观看视频免费| 在线一区二区三区| 国产成人免费高清| 日韩精品一二三| 国产精品毛片a∨一区二区三区| 欧美日韩综合不卡| 成人高清av在线| 免费久久99精品国产| 亚洲综合免费观看高清完整版| 久久久精品国产99久久精品芒果| 91成人免费电影| 不卡的av中国片| 九色porny丨国产精品| 亚洲v中文字幕| 亚洲精品日韩一| 久久亚洲免费视频| 日韩欧美国产一区二区三区| 成人黄页毛片网站| 国产自产v一区二区三区c| 一区二区三区产品免费精品久久75| 欧美不卡一区二区三区四区| 91久久久免费一区二区| 国产a视频精品免费观看| 国产在线视视频有精品| 日韩av不卡一区二区| 一区二区三区在线观看网站| 综合色中文字幕| 中文字幕+乱码+中文字幕一区| 精品国产91亚洲一区二区三区婷婷 | 青草av.久久免费一区| 夜夜夜精品看看| 日韩美女精品在线| 亚洲国产成人自拍| 欧美国产精品v| 国产网站一区二区| 久久久亚洲精华液精华液精华液| 欧美成人一区二区三区在线观看| 欧美一区三区二区| 91精品欧美综合在线观看最新| 在线观看日韩电影| 欧美三电影在线| 欧美日韩在线观看一区二区 | 性久久久久久久久久久久| 亚洲欧美日韩久久精品| ...xxx性欧美| 亚洲免费观看高清完整版在线观看 | 亚洲国产精品一区二区久久 | 精品亚洲porn| 国产综合色视频| 国产成人免费在线| 成人av动漫网站| 成人av网址在线| 91丨porny丨首页| 在线视频你懂得一区二区三区| 日本丶国产丶欧美色综合| 色综合天天综合在线视频| 91老司机福利 在线| 色偷偷成人一区二区三区91| 日本高清不卡一区| 91精品国产综合久久福利软件| 欧美一区国产二区| 精品国产区一区| 国产精品区一区二区三区| 亚洲欧美日韩系列| 日精品一区二区三区| 日本免费新一区视频| 国产一区不卡在线| 岛国精品一区二区| 欧美亚男人的天堂| 精品免费日韩av| 中文字幕欧美国产| 亚洲国产综合人成综合网站| 免费观看日韩电影| 粉嫩av一区二区三区| 欧美日韩视频专区在线播放| 精品国产一区二区精华| 国产精品欧美综合在线| 亚洲一区二区在线观看视频| 麻豆91精品91久久久的内涵| 波多野结衣在线一区| 欧美猛男男办公室激情| 久久久午夜精品| 亚洲制服欧美中文字幕中文字幕| 久久成人免费日本黄色| 色哟哟国产精品免费观看| 欧美电影免费观看高清完整版| 国产精品久久久久久妇女6080| 亚洲成人动漫在线免费观看| 成人一区在线观看| 日韩精品中文字幕一区二区三区| 自拍偷拍亚洲激情| 韩国成人精品a∨在线观看| 欧美在线观看一区| 日韩免费看的电影| 综合久久综合久久| 成人一级视频在线观看| 欧美一区二区视频网站| 成人欧美一区二区三区白人| 精品一区二区精品| 欧美手机在线视频| 1000部国产精品成人观看| 国产99久久精品| 日韩欧美的一区| 亚洲v中文字幕| aa级大片欧美| 日韩一区欧美一区| 国产精品99久久不卡二区| 91.成人天堂一区| 曰韩精品一区二区| 成人18精品视频| 久久精品视频免费| 久草在线在线精品观看| 欧美日韩免费观看一区三区| 亚洲天堂免费看| 成人午夜激情影院| 欧美国产成人在线| 国产一区视频在线看| 欧美成人精精品一区二区频| 日韩和欧美的一区| 欧美精品欧美精品系列| 最新久久zyz资源站| 国产在线精品一区二区| 欧美怡红院视频| 亚洲一区二区三区在线播放| 99久久综合色| 国产精品福利一区| 本田岬高潮一区二区三区| 国产日韩欧美综合在线| 国产91在线观看| 国产日产欧产精品推荐色| 青青草97国产精品免费观看| 欧美一区二区三区免费大片 | 国产在线精品不卡| 精品国产不卡一区二区三区| 韩国女主播一区| 国产午夜精品久久久久久免费视| 日韩va欧美va亚洲va久久| 日本久久精品电影| 亚洲人成电影网站色mp4| 成人18视频日本| 亚洲视频网在线直播| 91老师国产黑色丝袜在线| 一区二区三区国产| 欧美久久高跟鞋激| 久久精品国产亚洲一区二区三区 | 亚洲综合色婷婷| 欧美午夜片在线观看|