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

📄 qca-ossl.cpp

📁 QCA的OPENSSL模块
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	EVPKey()	{		pkey = 0;		raw_type = false;		state = Idle;	}	EVPKey(const EVPKey &from)	{		pkey = from.pkey;		CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);		state = Idle;	}	~EVPKey()	{		reset();	}	void reset()	{		if(pkey)			EVP_PKEY_free(pkey);		pkey = 0;		raw.clear ();		raw_type = false;	}	void startSign(const EVP_MD *type)	{		state = SignActive;		if(!type)		{			raw_type = true;			raw.clear ();		}		else		{			raw_type = false;			EVP_MD_CTX_init(&mdctx);			if(!EVP_SignInit_ex(&mdctx, type, NULL))				state = SignError;		}	}	void startVerify(const EVP_MD *type)	{		state = VerifyActive;		if(!type)		{			raw_type = true;			raw.clear ();		}		else		{			EVP_MD_CTX_init(&mdctx);			if(!EVP_VerifyInit_ex(&mdctx, type, NULL))				state = VerifyError;		}	}	void update(const MemoryRegion &in)	{		if(state == SignActive)		{			if (raw_type)				raw += in;			else				if(!EVP_SignUpdate(&mdctx, in.data(), (unsigned int)in.size()))					state = SignError;		}		else if(state == VerifyActive)		{			if (raw_type)				raw += in;			else				if(!EVP_VerifyUpdate(&mdctx, in.data(), (unsigned int)in.size()))					state = VerifyError;		}	}	SecureArray endSign()	{		if(state == SignActive)		{			SecureArray out(EVP_PKEY_size(pkey));			unsigned int len = out.size();			if (raw_type)			{				if (pkey->type == EVP_PKEY_RSA)				{					if(RSA_private_encrypt (raw.size(), (unsigned char *)raw.data(),						(unsigned char *)out.data(), pkey->pkey.rsa,						RSA_PKCS1_PADDING) == -1) {						state = SignError;						return SecureArray ();					}				}				else if (pkey->type == EVP_PKEY_DSA)				{					state = SignError;					return SecureArray ();				}				else				{					state = SignError;					return SecureArray ();				}			}			else {				if(!EVP_SignFinal(&mdctx, (unsigned char *)out.data(), &len, pkey))				{					state = SignError;					return SecureArray();				}			}			out.resize(len);			state = Idle;			return out;		}		else			return SecureArray();	}	bool endVerify(const SecureArray &sig)	{		if(state == VerifyActive)		{			if (raw_type)			{				SecureArray out(EVP_PKEY_size(pkey));				int len = 0;				if (pkey->type == EVP_PKEY_RSA) {					if((len = RSA_public_decrypt (sig.size(), (unsigned char *)sig.data(),						(unsigned char *)out.data (), pkey->pkey.rsa,						RSA_PKCS1_PADDING)) == -1) {						state = VerifyError;						return false;					}				}				else if (pkey->type == EVP_PKEY_DSA)				{					state = VerifyError;					return false;				}				else				{					state = VerifyError;					return false;				}				out.resize (len);				if (out != raw) {					state = VerifyError;					return false;				}			}			else			{				if(EVP_VerifyFinal(&mdctx, (unsigned char *)sig.data(), (unsigned int)sig.size(), pkey) != 1)				{					state = VerifyError;					return false;				}			}			state = Idle;			return true;		}		else			return false;	}};//----------------------------------------------------------------------------// MyDLGroup//----------------------------------------------------------------------------// IETF primes from Botanconst char* IETF_1024_PRIME =	"FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1"	"29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD"	"EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245"	"E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED"	"EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE65381"	"FFFFFFFF FFFFFFFF";const char* IETF_2048_PRIME =	"FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1"	"29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD"	"EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245"	"E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED"	"EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D"	"C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F"	"83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D"	"670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B"	"E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9"	"DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510"	"15728E5A 8AACAA68 FFFFFFFF FFFFFFFF";const char* IETF_4096_PRIME =	"FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1"	"29024E08 8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD"	"EF9519B3 CD3A431B 302B0A6D F25F1437 4FE1356D 6D51C245"	"E485B576 625E7EC6 F44C42E9 A637ED6B 0BFF5CB6 F406B7ED"	"EE386BFB 5A899FA5 AE9F2411 7C4B1FE6 49286651 ECE45B3D"	"C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8 FD24CF5F"	"83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D"	"670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B"	"E39E772C 180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9"	"DE2BCBF6 95581718 3995497C EA956AE5 15D22618 98FA0510"	"15728E5A 8AAAC42D AD33170D 04507A33 A85521AB DF1CBA64"	"ECFB8504 58DBEF0A 8AEA7157 5D060C7D B3970F85 A6E1E4C7"	"ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226 1AD2EE6B"	"F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C"	"BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31"	"43DB5BFC E0FD108E 4B82D120 A9210801 1A723C12 A787E6D7"	"88719A10 BDBA5B26 99C32718 6AF4E23C 1A946834 B6150BDA"	"2583E9CA 2AD44CE8 DBBBC2DB 04DE8EF9 2E8EFC14 1FBECAA6"	"287C5947 4E6BC05D 99B2964F A090C3A2 233BA186 515BE7ED"	"1F612970 CEE2D7AF B81BDD76 2170481C D0069127 D5B05AA9"	"93B4EA98 8D8FDDC1 86FFB7DC 90A6C08F 4DF435C9 34063199"	"FFFFFFFF FFFFFFFF";// JCE seeds from Botanconst char* JCE_512_SEED = "B869C82B 35D70E1B 1FF91B28 E37A62EC DC34409B";const int JCE_512_COUNTER = 123;const char* JCE_768_SEED = "77D0F8C4 DAD15EB8 C4F2F8D6 726CEFD9 6D5BB399";const int JCE_768_COUNTER = 263;const char* JCE_1024_SEED = "8D515589 4229D5E6 89EE01E6 018A237E 2CAE64CD";const int JCE_1024_COUNTER = 92;static QByteArray dehex(const QString &hex){	QString str;	for(int n = 0; n < hex.length(); ++n)	{		if(hex[n] != ' ')			str += hex[n];	}	return hexToArray(str);}static BigInteger decode(const QString &prime){	QByteArray a(1, 0); // 1 byte of zero padding	a.append(dehex(prime));	return BigInteger(SecureArray(a));}static QByteArray decode_seed(const QString &hex_seed){	return dehex(hex_seed);}class DLParams{public:	BigInteger p, q, g;};static bool make_dlgroup(const QByteArray &seed, int bits, int counter, DLParams *params){	int ret_counter;	DSA *dsa = DSA_generate_parameters(bits, (unsigned char *)seed.data(), seed.size(), &ret_counter, NULL, NULL, NULL);	if(!dsa)		return false;	if(ret_counter != counter)		return false;	params->p = bn2bi(dsa->p);	params->q = bn2bi(dsa->q);	params->g = bn2bi(dsa->g);	DSA_free(dsa);	return true;}static bool get_dlgroup(const BigInteger &p, const BigInteger &g, DLParams *params){	params->p = p;	params->q = BigInteger(0);	params->g = g;	return true;}class DLGroupMaker : public QThread{	Q_OBJECTpublic:	DLGroupSet set;	bool ok;	DLParams params;	DLGroupMaker(DLGroupSet _set)	{		set = _set;	}	~DLGroupMaker()	{		wait();	}	virtual void run()	{		if(set == DSA_512)			ok = make_dlgroup(decode_seed(JCE_512_SEED), 512, JCE_512_COUNTER, &params);		else if(set == DSA_768)			ok = make_dlgroup(decode_seed(JCE_768_SEED), 768, JCE_768_COUNTER, &params);		else if(set == DSA_1024)			ok = make_dlgroup(decode_seed(JCE_1024_SEED), 1024, JCE_1024_COUNTER, &params);		else if(set == IETF_1024)			ok = get_dlgroup(decode(IETF_1024_PRIME), 2, &params);		else if(set == IETF_2048)			ok = get_dlgroup(decode(IETF_2048_PRIME), 2, &params);		else if(set == IETF_4096)			ok = get_dlgroup(decode(IETF_4096_PRIME), 2, &params);		else			ok = false;	}};class MyDLGroup : public DLGroupContext{	Q_OBJECTpublic:	DLGroupMaker *gm;	bool wasBlocking;	DLParams params;	bool empty;	MyDLGroup(Provider *p) : DLGroupContext(p)	{		gm = 0;		empty = true;	}	MyDLGroup(const MyDLGroup &from) : DLGroupContext(from.provider())	{		gm = 0;		empty = true;	}	~MyDLGroup()	{		delete gm;	}	virtual Provider::Context *clone() const	{		return new MyDLGroup(*this);	}	virtual QList<DLGroupSet> supportedGroupSets() const	{		QList<DLGroupSet> list;		list += DSA_512;		list += DSA_768;		list += DSA_1024;		list += IETF_1024;		list += IETF_2048;		list += IETF_4096;		return list;	}	virtual bool isNull() const	{		return empty;	}	virtual void fetchGroup(DLGroupSet set, bool block)	{		params = DLParams();		empty = true;		gm = new DLGroupMaker(set);		wasBlocking = block;		if(block)		{			gm->run();			gm_finished();		}		else		{			connect(gm, SIGNAL(finished()), SLOT(gm_finished()));			gm->start();		}	}	virtual void getResult(BigInteger *p, BigInteger *q, BigInteger *g) const	{		*p = params.p;		*q = params.q;		*g = params.g;	}private slots:	void gm_finished()	{		bool ok = gm->ok;		if(ok)		{			params = gm->params;			empty = false;		}		if(wasBlocking)			delete gm;		else			gm->deleteLater();		gm = 0;		if(!wasBlocking)			emit finished();	}};//----------------------------------------------------------------------------// RSAKey//----------------------------------------------------------------------------class RSAKeyMaker : public QThread{	Q_OBJECTpublic:	RSA *result;	int bits, exp;	RSAKeyMaker(int _bits, int _exp, QObject *parent = 0) : QThread(parent), result(0), bits(_bits), exp(_exp)	{	}	~RSAKeyMaker()	{		wait();		if(result)			RSA_free(result);	}	virtual void run()	{		RSA *rsa = RSA_generate_key(bits, exp, NULL, NULL);		if(!rsa)			return;		result = rsa;	}	RSA *takeResult()	{		RSA *rsa = result;		result = 0;		return rsa;	}};class RSAKey : public RSAContext{	Q_OBJECTpublic:	EVPKey evp;	RSAKeyMaker *keymaker;	bool wasBlocking;	bool sec;	RSAKey(Provider *p) : RSAContext(p)	{		keymaker = 0;		sec = false;	}	RSAKey(const RSAKey &from) : RSAContext(from.provider()), evp(from.evp)	{		keymaker = 0;		sec = from.sec;	}	~RSAKey()	{		delete keymaker;	}	virtual Provider::Context *clone() const	{		return new RSAKey(*this);	}	virtual bool isNull() const	{		return (evp.pkey ? false: true);	}	virtual PKey::Type type() const	{		return PKey::RSA;	}	virtual bool isPrivate() const	{		return sec;	}	virtual bool canExport() const	{		return true;	}	virtual void convertToPublic()	{		if(!sec)			return;		// extract the public key into DER format		int len = i2d_RSAPublicKey(evp.pkey->pkey.rsa, NULL);		SecureArray result(len);		unsigned char *p = (unsigned char *)result.data();		i2d_RSAPublicKey(evp.pkey->pkey.rsa, &p);		p = (unsigned char *)result.data();		// put the DER public key back into openssl		evp.reset();		RSA *rsa;#ifdef OSSL_097		rsa = d2i_RSAPublicKey(NULL, (const unsigned char **)&p, result.size());#else		rsa = d2i_RSAPublicKey(NULL, (unsigned char **)&p, result.size());#endif		evp.pkey = EVP_PKEY_new();		EVP_PKEY_assign_RSA(evp.pkey, rsa);		sec = false;	}	virtual int bits() const	{		return EVP_PKEY_bits(evp.pkey);	}	virtual int maximumEncryptSize(EncryptionAlgorithm alg) const	{		RSA *rsa = evp.pkey->pkey.rsa;		if(alg == EME_PKCS1v15)			return RSA_size(rsa) - 11 - 1;		else // oaep			return RSA_size(rsa) - 41 - 1;	}	virtual SecureArray encrypt(const SecureArray &in, EncryptionAlgorithm alg)	{		RSA *rsa = evp.pkey->pkey.rsa;		SecureArray buf = in;		int max = maximumEncryptSize(alg);		if(buf.size() > max)			buf.resize(max);		SecureArray result(RSA_size(rsa));		int pad;		if(alg == EME_PKCS1v15)			pad = RSA_PKCS1_PADDING;		else // oaep			pad = RSA_PKCS1_OAEP_PADDING;		int ret = RSA_public_encrypt(buf.size(), (unsigned char *)buf.data(), (unsigned char *)result.data(), rsa, pad);		if(ret < 0)			return SecureArray();		result.resize(ret);		return result;	}	virtual bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)	{		RSA *rsa = evp.pkey->pkey.rsa;		SecureArray result(RSA_size(rsa));		int pad;		if(alg == EME_PKCS1v15)			pad = RSA_PKCS1_PADDING;		else // oaep			pad = RSA_PKCS1_OAEP_PADDING;		int ret = RSA_private_decrypt(in.size(), (unsigned char *)in.data(), (unsigned char *)result.data(), rsa, pad);

⌨️ 快捷键说明

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