📄 cr_specific.c
字号:
int ranfd; int rlen,totallen=0; ranfd = open("/dev/urandom",O_RDONLY); if (ranfd >= 0 ){ do { rlen = read(ranfd,output+totallen,bytes-totallen); totallen += rlen; } while( totallen < bytes); return CKR_OK; } else { return CKR_FUNCTION_FAILED; }#endif /* if 0 */}// convert pkcs slot number to local representationinttok_slot2local(CK_SLOT_ID snum){ return 1; }CK_RVtoken_specific_init(char * Correlator,CK_SLOT_ID SlotNumber){ crfd = CR_init_lib(NULL, NULL); return CKR_OK;}CK_RVtoken_specific_final(){ CR_close_lib(); return CKR_OK;}CK_RVtoken_specific_des_key_gen(CK_BYTE *des_key,CK_ULONG len){ // Nothing different to do for DES or TDES here as this is just // random data... Validation handles the rest rng_generate(des_key,len); // we really need to validate the key for parity etc... // we should do that here... The caller validates the single des keys // against the known and suspected poor keys.. return CKR_OK;}// convert from the local PKCS11 template representation to// the underlying requirement// returns the pointer to the local key representation//void * rsa_convert_public_key( OBJECT *key_obj ){ CK_BBOOL rc; CK_ATTRIBUTE *modulus = NULL; CK_ATTRIBUTE *pub_exp = NULL; rsa_key *rsa_pub_key; token *t_modulus, *t_exponent ; 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; rsa_pub_key = (rsa_key *) create_rsa_key(modulus->ulValueLen, pub_exp->ulValueLen); if (rsa_pub_key == NULL) return NULL; memcpy(rsa_pub_key->modulus->p_data, modulus->pValue, modulus->ulValueLen) ; memcpy(rsa_pub_key->exponent->p_data, pub_exp->pValue, pub_exp->ulValueLen) ; rsa_pub_key->modulus->data_size = modulus->ulValueLen; rsa_pub_key->exponent->data_size = pub_exp->ulValueLen; return (void *)rsa_pub_key;}void * rsa_convert_private_key(OBJECT *key_obj){ 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_crt_key *privKey; rsa_key *privKey2 ; 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 ( rc == FALSE) return NULL; /* CRT operations are faster for N>1024 operations. For all other operations, perform non-CRT operations */ if (modulus->ulValueLen > 1024) { privKey = (rsa_crt_key *) create_rsa_crt_key(prime1->ulValueLen, prime2->ulValueLen, exp1->ulValueLen, exp2->ulValueLen, coeff->ulValueLen) ; if (privKey != NULL) { memcpy(privKey->prime_p->p_data, prime1->pValue, prime1->ulValueLen) ; privKey->prime_p->data_size = prime1->ulValueLen; memcpy(privKey->prime_q->p_data, prime2->pValue, prime2->ulValueLen) ; privKey->prime_q->data_size = prime2->ulValueLen; memcpy(privKey->dmp1->p_data, exp1->pValue, exp1->ulValueLen) ; privKey->dmp1->data_size = exp1->ulValueLen; memcpy(privKey->dmq1->p_data, exp2->pValue, exp2->ulValueLen) ; privKey->dmq1->data_size = exp2->ulValueLen; memcpy(privKey->iqmp->p_data, coeff->pValue, coeff->ulValueLen) ; privKey->iqmp->data_size = coeff->ulValueLen; return (void *) privKey ; } else return NULL; } else { privKey2 = (rsa_key *) create_rsa_key(modulus->ulValueLen, priv_exp->ulValueLen) ; if (privKey2 != NULL) { memcpy(privKey2->modulus->p_data, modulus->pValue, modulus->ulValueLen) ; privKey2->modulus->data_size = modulus->ulValueLen; memcpy(privKey2->exponent->p_data, priv_exp->pValue, priv_exp->ulValueLen) ; privKey2->exponent->data_size = priv_exp->ulValueLen; return (void *) privKey2 ; } else return NULL ; } return NULL;} /* end rsa_convert_private_key() */#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 char nextRandom (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);}void swapper(char *s, char *d, int size){ int i=0; int j=size; for(i=0;i<size;i++) d[i]=s[--j];}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(1, __FILE__, __LINE__); return CKR_HOST_MEMORY; } // 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); 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_PRIME_1, 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 #2: q // bignum = rsa->q; 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_PRIME_2, ssl_ptr, BNLength, &attr );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -