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

📄 soft_specific.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 3 页
字号:
   	const_des_cblock key_val_SSL, in_key_data;	des_cblock out_key_data;	// Create the key schedule	memcpy(&key_val_SSL, key_value, 8);   	des_set_key_unchecked(&key_val_SSL, des_key2);   	memcpy(&ivec, init_v, 8);	// the des decrypt will only fail if the data length is not evenly divisible	// by 8	if (in_data_len % 8 ){		st_err_log(11, __FILE__, __LINE__);		return CKR_DATA_LEN_RANGE;	}	if ( encrypt){		des_ncbc_encrypt(in_data, out_data, in_data_len, des_key2, &ivec, DES_ENCRYPT);		*out_data_len = in_data_len;		rc = CKR_OK;	} else {		des_ncbc_encrypt(in_data, out_data, in_data_len, des_key2, &ivec, DES_DECRYPT);		*out_data_len = in_data_len;		rc = CKR_OK;	}	return rc;}CK_RVtoken_specific_tdes_ecb(CK_BYTE * in_data,                       CK_ULONG in_data_len,                       CK_BYTE *out_data,                       CK_ULONG *out_data_len,                       CK_BYTE  *key_value,                       CK_BYTE  encrypt){	CK_RV  rc;	int k,j, ret;	des_cblock out_temp;	des_key_schedule des_key1;	des_key_schedule des_key2;	des_key_schedule des_key3;   	const_des_cblock key_SSL1, key_SSL2, key_SSL3, in_key_data;	des_cblock out_key_data;	// The key as passed is a 24 byte long string containing three des keys	// pick them apart and create the 3 corresponding key schedules	memcpy(&key_SSL1, key_value, 8);	memcpy(&key_SSL2, key_value+8, 8);	memcpy(&key_SSL3, key_value+16, 8);	des_set_key_unchecked(&key_SSL1, des_key1);	des_set_key_unchecked(&key_SSL2, des_key2);	des_set_key_unchecked(&key_SSL3, des_key3);	// the des decrypt will only fail if the data length is not evenly divisible	// by 8	if (in_data_len % 8 ){		st_err_log(11, __FILE__, __LINE__);		return CKR_DATA_LEN_RANGE;	}	// the encrypt and decrypt are done 8 bytes at a time	if (encrypt) {		for(k=0;k<in_data_len;k=k+8){		memcpy(in_key_data, in_data+k, 8);		des_ecb3_encrypt(&in_key_data, 				&out_key_data, 				des_key1, 				des_key2,				des_key3,				DES_ENCRYPT);		memcpy(out_data+k, out_key_data, 8);	}	*out_data_len = in_data_len;	rc = CKR_OK;	} else {		for (j=0;j<in_data_len;j=j+8){		memcpy(in_key_data, in_data+j, 8);		des_ecb3_encrypt(&in_key_data,				&out_key_data, 				des_key1,				des_key2,				des_key3, 				DES_DECRYPT);		memcpy(out_data+j, out_key_data, 8);	}      *out_data_len = in_data_len;      rc = CKR_OK;   }   return rc;}CK_RVtoken_specific_tdes_cbc(CK_BYTE * in_data,                       CK_ULONG in_data_len,                       CK_BYTE *out_data,                       CK_ULONG *out_data_len,                       CK_BYTE  *key_value,                        CK_BYTE *init_v,                       CK_BYTE  encrypt){	CK_RV rc = CKR_OK;	des_key_schedule des_key1;	des_key_schedule des_key2;	des_key_schedule des_key3;   	const_des_cblock key_SSL1, key_SSL2, key_SSL3, in_key_data;	des_cblock ivec;	// The key as passed in is a 24 byte string containing 3 keys	// pick it apart and create the key schedules	memcpy(&key_SSL1, key_value, 8);	memcpy(&key_SSL2, key_value+8, 8);	memcpy(&key_SSL3, key_value+16, 8);	des_set_key_unchecked(&key_SSL1, des_key1);	des_set_key_unchecked(&key_SSL2, des_key2);	des_set_key_unchecked(&key_SSL3, des_key3);	memcpy(ivec, init_v, sizeof(ivec));	// the des decrypt will only fail if the data length is not evenly divisible	// by 8	if (in_data_len % 8 ){		st_err_log(11, __FILE__, __LINE__);		return CKR_DATA_LEN_RANGE;	}	// Encrypt or decrypt the data	if (encrypt){		des_ede3_cbc_encrypt(in_data,			     out_data,			     in_data_len,			     des_key1,			     des_key2,			     des_key3,			     &ivec,			     DES_ENCRYPT);	*out_data_len = in_data_len;	rc = CKR_OK;	}else {		des_ede3_cbc_encrypt(in_data,					out_data,					in_data_len,					des_key1,					des_key2,					des_key3,					&ivec,					DES_DECRYPT);	*out_data_len = in_data_len;	rc = CKR_OK;	}	return rc;}// convert from the local PKCS11 template representation to// the underlying requirement// returns the pointer to the local key representationvoid *rsa_convert_public_key( OBJECT    * key_obj ){	CK_BBOOL           rc;	CK_ATTRIBUTE      * modulus = NULL;	CK_ATTRIBUTE      * pub_exp = NULL;	RSA *rsa;	BIGNUM *bn_mod, *bn_exp;	rc  = template_attribute_find( key_obj->template, CKA_MODULUS,         &modulus );	rc &= template_attribute_find( key_obj->template, CKA_PUBLIC_EXPONENT, &pub_exp );	if (rc == FALSE) {		return NULL;	}	// Create an RSA key struct to return	rsa = RSA_new();	if (rsa == NULL)		return NULL;	RSA_blinding_off(rsa);	// Create and init BIGNUM structs to stick in the RSA struct	bn_mod = BN_new();	bn_exp = BN_new();	if (bn_exp == NULL || bn_mod == NULL) {		if (bn_mod) free(bn_mod);		if (bn_exp) free(bn_exp);		RSA_free(rsa);		return NULL;	}	BN_init(bn_mod);	BN_init(bn_exp);	// Convert from strings to BIGNUMs and stick them in the RSA struct	BN_bin2bn((char *)modulus->pValue, modulus->ulValueLen, bn_mod);	rsa->n = bn_mod;	BN_bin2bn((char *)pub_exp->pValue, pub_exp->ulValueLen, bn_exp);	rsa->e = bn_exp;   	return (void *)rsa;}void *rsa_convert_private_key(OBJECT *key_obj){	CK_ATTRIBUTE      * attr     = NULL;	CK_ATTRIBUTE      * modulus  = NULL;	CK_ATTRIBUTE      * priv_exp = NULL;	CK_ATTRIBUTE      * prime1   = NULL;	CK_ATTRIBUTE      * prime2   = NULL;	CK_ATTRIBUTE      * exp1     = NULL;	CK_ATTRIBUTE      * exp2     = NULL;	CK_ATTRIBUTE      * coeff    = NULL;	CK_BBOOL          rc;	RSA *rsa;	BIGNUM *bn_mod, *bn_priv_exp, *bn_p1, *bn_p2, *bn_e1, *bn_e2, *bn_cf;	rc  = template_attribute_find( key_obj->template, CKA_MODULUS,          &modulus );	rc &= template_attribute_find( key_obj->template, CKA_PRIVATE_EXPONENT, &priv_exp );	rc &= template_attribute_find( key_obj->template, CKA_PRIME_1,          &prime1 );	rc &= template_attribute_find( key_obj->template, CKA_PRIME_2,          &prime2 );	rc &= template_attribute_find( key_obj->template, CKA_EXPONENT_1,       &exp1 );	rc &= template_attribute_find( key_obj->template, CKA_EXPONENT_2,       &exp2 );	rc &= template_attribute_find( key_obj->template, CKA_COEFFICIENT,      &coeff );	if ( !prime2 && !modulus ){        	return NULL;	}	// Create and init all the RSA and BIGNUM structs we need.	rsa = RSA_new();	if (rsa == NULL)		return NULL;	RSA_blinding_off(rsa);	bn_mod = BN_new();	bn_priv_exp = BN_new();	bn_p1 = BN_new();	bn_p2 = BN_new();	bn_e1 = BN_new();	bn_e2 = BN_new();	bn_cf = BN_new();	if ((bn_cf == NULL) || (bn_e2 == NULL) || (bn_e1 == NULL) ||	    (bn_p2 == NULL) || (bn_p1 == NULL) || (bn_priv_exp == NULL) ||	    (bn_mod == NULL))	{		if (rsa)         RSA_free(rsa);		if (bn_mod)      BN_free(bn_mod);		if (bn_priv_exp) BN_free(bn_priv_exp);		if (bn_p1)       BN_free(bn_p1);		if (bn_p2)       BN_free(bn_p2);		if (bn_e1)       BN_free(bn_e1);		if (bn_e2)       BN_free(bn_e2);		if (bn_cf)       BN_free(bn_cf);		return NULL;	}		// CRT key?	if ( prime1){		if (!prime2 || !exp1 ||!exp2 || !coeff) {			return NULL;		}		// Even though this is CRT key, OpenSSL requires the		// modulus and exponents filled in or encrypt and decrypt will		// not work		BN_bin2bn((char *)modulus->pValue, modulus->ulValueLen, bn_mod);		rsa->n = bn_mod;		BN_bin2bn((char *)priv_exp->pValue, priv_exp->ulValueLen, bn_priv_exp);		rsa->d = bn_priv_exp;		BN_bin2bn((char *)prime1->pValue, prime1->ulValueLen, bn_p1);		rsa->p = bn_p1;		BN_bin2bn((char *)prime2->pValue, prime2->ulValueLen, bn_p2);		rsa->q = bn_p2;		BN_bin2bn((char *)exp1->pValue, exp1->ulValueLen, bn_e1);		rsa->dmp1 = bn_e1;		BN_bin2bn((char *)exp2->pValue, exp2->ulValueLen, bn_e2);		rsa->dmq1 = bn_e2;		BN_bin2bn((char *)coeff->pValue, coeff->ulValueLen, bn_cf);		rsa->iqmp = bn_cf;		return rsa;	} else {   // must be a non-CRT key		if (!priv_exp) {			return NULL;		}		BN_bin2bn((char *)modulus->pValue, modulus->ulValueLen, bn_mod);		rsa->n = bn_mod;		BN_bin2bn((char *)priv_exp->pValue, priv_exp->ulValueLen, bn_priv_exp);		rsa->d = bn_priv_exp;	}	return (void *)rsa;}#define RNG_BUF_SIZE 100// This function is only required if public key cryptography// has been selected in your variant set up.// Set a mutex in this function and get a cache;// using the ICA device to get random numbers a byte at a//  time is VERY slow..  Keygen is gated by this function.unsigned charnextRandom (void) {  static unsigned char  buffer[RNG_BUF_SIZE];  unsigned char  byte;  static int used = (RNG_BUF_SIZE); // protected access by the mutex  pthread_mutex_lock(&nextmutex);  if (used >= RNG_BUF_SIZE){    rng_generate(buffer,sizeof(buffer));    used = 0;  }  byte = buffer[used++];  pthread_mutex_unlock(&nextmutex);    return((unsigned char)byte);}CK_RVos_specific_rsa_keygen(TEMPLATE *publ_tmpl,  TEMPLATE *priv_tmpl){	CK_ATTRIBUTE       * publ_exp = NULL;	CK_ATTRIBUTE       * attr     = NULL;	CK_ULONG             mod_bits;	CK_BBOOL             flag;	CK_RV                rc;	CK_ULONG             BNLength;	RSA *rsa;	BIGNUM *bignum;	CK_BYTE *ssl_ptr;	unsigned long three = 3;	unsigned char *exp_str;	unsigned long exponent;	flag = template_attribute_find( publ_tmpl, CKA_MODULUS_BITS, &attr );	if (!flag){		st_err_log(48, __FILE__, __LINE__);		return CKR_TEMPLATE_INCOMPLETE;  // should never happen        }	mod_bits = *(CK_ULONG *)attr->pValue;	flag = template_attribute_find( publ_tmpl, CKA_PUBLIC_EXPONENT, &publ_exp );	if (!flag){		st_err_log(48, __FILE__, __LINE__);		return CKR_TEMPLATE_INCOMPLETE;	}	// we don't support less than 1024 bit keys in the sw	if (mod_bits < 512 || mod_bits > 2048) {		st_err_log(19, __FILE__, __LINE__);		return CKR_KEY_SIZE_RANGE;	}	// Because of a limition of OpenSSL, this token only supports	// 3 as an exponent in RSA key generation	rsa = RSA_new();	if (rsa == NULL) {                st_err_log(1, __FILE__, __LINE__);                return CKR_HOST_MEMORY;        }	RSA_blinding_off(rsa);	rsa = RSA_generate_key(mod_bits, three, NULL, NULL);	if (rsa == NULL) {                st_err_log(4, __FILE__, __LINE__);                return CKR_FUNCTION_FAILED;        }	// Now fill in the objects..	//	// modulus: n	//	bignum = rsa->n;	BNLength = BN_num_bytes(bignum);	ssl_ptr = malloc(BNLength);	if (ssl_ptr == NULL) {                st_err_log(1, __FILE__, __LINE__);                rc = CKR_HOST_MEMORY;                goto done;        }	BNLength = BN_bn2bin(bignum, ssl_ptr);	rc = build_attribute( CKA_MODULUS, ssl_ptr, BNLength, &attr ); // in bytes	if (rc != CKR_OK){		st_err_log(84, __FILE__, __LINE__);		goto done;        }	template_update_attribute( publ_tmpl, attr );	free(ssl_ptr);	// Public Exponent        bignum = rsa->e;        BNLength = BN_num_bytes(bignum);        ssl_ptr = malloc(BNLength);	if (ssl_ptr == NULL) {                st_err_log(1, __FILE__, __LINE__);                rc = CKR_HOST_MEMORY;                goto done;        }        BNLength = BN_bn2bin(bignum, ssl_ptr);        rc = build_attribute( CKA_PUBLIC_EXPONENT, ssl_ptr, BNLength, &attr ); // in bytes        if (rc != CKR_OK){                st_err_log(84, __FILE__, __LINE__);                goto done;        }        template_update_attribute( publ_tmpl, attr );        free(ssl_ptr);	// local = TRUE	//	flag = TRUE;	rc = build_attribute( CKA_LOCAL, &flag, sizeof(CK_BBOOL), &attr );	if (rc != CKR_OK){		st_err_log(84, __FILE__, __LINE__);		goto done;	}	template_update_attribute( publ_tmpl, attr );	//	// now, do the private key	//	// Cheat here and put the whole original key into the CKA_VALUE... remember	// to force the system to not return this for RSA keys..	// Add the modulus to the private key information	bignum = rsa->n;	BNLength = BN_num_bytes(bignum);	ssl_ptr = malloc(BNLength);	if (ssl_ptr == NULL) {                st_err_log(1, __FILE__, __LINE__);                rc = CKR_HOST_MEMORY;                goto done;        }	BNLength = BN_bn2bin(bignum, ssl_ptr);	rc = build_attribute( CKA_MODULUS, ssl_ptr, BNLength ,&attr ); // in bytes	if (rc != CKR_OK){		st_err_log(84, __FILE__, __LINE__);		goto done;	}	template_update_attribute( priv_tmpl, attr );	free(ssl_ptr);	// Private Exponent        bignum = rsa->d;        BNLength = BN_num_bytes(bignum);        ssl_ptr = malloc( BNLength);	if (ssl_ptr == NULL) {                st_err_log(1, __FILE__, __LINE__);                rc = CKR_HOST_MEMORY;                goto done;        }        BNLength = BN_bn2bin(bignum, ssl_ptr);        rc = build_attribute( CKA_PRIVATE_EXPONENT, ssl_ptr, BNLength, &attr );        if (rc != CKR_OK){                st_err_log(84, __FILE__, __LINE__);                goto done;        }        template_update_attribute( priv_tmpl, attr );        free(ssl_ptr);	// prime #1: p	//	bignum = rsa->p;	BNLength = BN_num_bytes(bignum);

⌨️ 快捷键说明

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