📄 ica_specific.c
字号:
keysize /= 2; // exponent 1: d mod(p-1) // ptr = (CK_BYTE *)(privKey->keyRecord); rc = build_attribute( CKA_EXPONENT_1, ptr, keysize + 8, &attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto privkey_cleanup; } template_update_attribute( priv_tmpl, attr ); // exponent 2: d mod(q-1) // ptr += keysize + 8; rc = build_attribute( CKA_EXPONENT_2, ptr, keysize, &attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto privkey_cleanup; } template_update_attribute( priv_tmpl, attr ); // prime #1: p // ptr += keysize; rc = build_attribute( CKA_PRIME_1, ptr, keysize+8, &attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto privkey_cleanup; } template_update_attribute( priv_tmpl, attr ); // prime #2: q // ptr += keysize + 8; rc = build_attribute( CKA_PRIME_2, ptr, keysize, &attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto privkey_cleanup; } template_update_attribute( priv_tmpl, attr ); // CRT coefficient: q_inverse mod(p) // ptr += keysize; rc = build_attribute( CKA_COEFFICIENT, ptr, keysize + 8, &attr ); if (rc != CKR_OK){ st_err_log(84, __FILE__, __LINE__); goto privkey_cleanup; } template_update_attribute( priv_tmpl, attr ); privkey_cleanup: free(privKey); pubkey_cleanup: free(publKey); return rc;}#endif// SAB FIXME this keygen stuff needs to be reworked..////CK_RVtoken_specific_rsa_generate_keypair( TEMPLATE * publ_tmpl, TEMPLATE * priv_tmpl ){ CK_ATTRIBUTE * publ_exp = NULL; CK_ATTRIBUTE * attr = NULL; CK_BYTE * ptr = NULL; CK_BYTE repl_buf[5500]; CK_ULONG req_len, repl_len; CK_ULONG mod_bits; CK_BBOOL flag; CK_RV rc; rc = os_specific_rsa_keygen(publ_tmpl,priv_tmpl); if (rc != CKR_OK) st_err_log(91, __FILE__, __LINE__); return rc;}////CK_RVtoken_specific_rsa_encrypt( CK_BYTE * in_data, CK_ULONG in_data_len, CK_BYTE * out_data, OBJECT * key_obj ){ CK_ATTRIBUTE * attr = NULL; CK_ATTRIBUTE * modulus = NULL; CK_ATTRIBUTE * pub_exp = NULL; CK_BYTE * ptr = NULL; CK_ULONG buffer[80]; // plenty of room... CK_OBJECT_CLASS keyclass; CK_ULONG req_len, repl_len, key_len; CK_RV rc; unsigned int out_data_len; ICA_KEY_RSA_MODEXPO *publKey; publKey = (ICA_KEY_RSA_MODEXPO *) rsa_convert_public_key(key_obj); if (publKey == NULL) { st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED; goto done; } out_data_len = (unsigned int)in_data_len; rc = icaRsaModExpo((int)adapter_handle, (unsigned int)in_data_len, in_data, publKey, &out_data_len, out_data); if (rc != 0) { st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED; } else { rc = CKR_OK; } free(publKey); goto done;done: return rc;}////CK_RVtoken_specific_rsa_decrypt( CK_BYTE * in_data, CK_ULONG in_data_len, CK_BYTE * out_data, OBJECT * key_obj ){ CK_ATTRIBUTE * attr = NULL; CK_ATTRIBUTE * modulus = NULL; CK_ATTRIBUTE * pub_exp = NULL; CK_ATTRIBUTE * prime1 = NULL; CK_ATTRIBUTE * prime2 = NULL; CK_ATTRIBUTE * exp1 = NULL; CK_ATTRIBUTE * exp2 = NULL; CK_ATTRIBUTE * coeff = NULL; CK_BYTE * ptr = NULL; CK_ULONG buffer[80]; // plenty of room... CK_OBJECT_CLASS keyclass; CK_ULONG key_size; CK_ULONG req_len, repl_len; CK_RV rc; unsigned int out_data_len; ICA_KEY_RSA_CRT *privKey; privKey = (ICA_KEY_RSA_CRT *) rsa_convert_private_key(key_obj); if (privKey == NULL) { st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED; goto done; } out_data_len = (unsigned int)in_data_len; if (privKey->keyType == KEYTYPE_PKCSCRT) { rc = icaRsaCrt((int)adapter_handle, (unsigned int)in_data_len, in_data, privKey, &out_data_len, out_data); } else if (privKey->keyType == KEYTYPE_MODEXPO) { rc = icaRsaModExpo((int)adapter_handle, (unsigned int)in_data_len, in_data, (ICA_KEY_RSA_MODEXPO *) privKey, &out_data_len, out_data); } else { st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED; free(privKey); goto done; } if (rc != 0) { st_err_log(4, __FILE__, __LINE__, __FUNCTION__); rc = CKR_FUNCTION_FAILED; } else { rc = CKR_OK; } free(privKey); goto done;done: return rc;}CK_RVtoken_specific_session(CK_SLOT_ID slotid){ return CKR_OK;}CK_RVtoken_specific_sha_init( DIGEST_CONTEXT * ctx ){ oc_sha1_ctx *sc; /* For the C_DigestInit, C_Digest case, we may have already * created ctx->context... - KEY */ if(ctx->context) { sc = (oc_sha1_ctx *)ctx->context; if(sc->dev_ctx) free(sc->dev_ctx); free(ctx->context); } /* The caller will check to see if ctx->context == NULL */ ctx->context_len = sizeof(oc_sha1_ctx); ctx->context = malloc(sizeof(oc_sha1_ctx)); if(ctx->context == NULL) return CKR_HOST_MEMORY; memset(ctx->context, 0, ctx->context_len); sc = (oc_sha1_ctx *)ctx->context; sc->hash_len = SHA1_HASH_SIZE; sc->message_part = SHA_MSG_PART_ONLY; /* This is libica's LENGTH_SHA_CONTEXT */ sc->dev_ctx = malloc(LENGTH_SHA_CONTEXT); if(sc->dev_ctx == NULL){ free(ctx->context); return CKR_HOST_MEMORY; } memset(sc->dev_ctx, 0, LENGTH_SHA_CONTEXT); return CKR_OK;}CK_RVtoken_specific_sha_update( DIGEST_CONTEXT *ctx, CK_BYTE *in_data, CK_ULONG in_data_len ){ unsigned int rc, i, fill_size = 0; oc_sha1_ctx *oc_sha_ctx = (oc_sha1_ctx *)ctx->context; SHA_CONTEXT *ica_sha_ctx = (SHA_CONTEXT *)oc_sha_ctx->dev_ctx; if( !ctx ) return CKR_OPERATION_NOT_INITIALIZED; if( !in_data ) return CKR_FUNCTION_FAILED; if( ctx->multi == TRUE ){ if (oc_sha_ctx->tail_len == 64) { /* Submit the filled out save buffer */ if( icaSha1( adapter_handle, ica_sha_ctx->runningLength == 0 ? SHA_MSG_PART_FIRST : SHA_MSG_PART_MIDDLE, 64, oc_sha_ctx->tail, LENGTH_SHA_CONTEXT, ica_sha_ctx, &oc_sha_ctx->hash_len, oc_sha_ctx->hash)) return CKR_FUNCTION_FAILED; oc_sha_ctx->tail_len = 0; } /* libICA (and SHA1) demands that if this is a PART_FIRST or a * PART_MIDDLE operation, the amount of data passed in * must be a multiple of 64 bytes. - KEY */ if( ica_sha_ctx->runningLength == 0 && oc_sha_ctx->tail_len == 0) { oc_sha_ctx->message_part = SHA_MSG_PART_FIRST; /* Just copying the last <64 bytes will not work in the case * of a user who SHA's a large chunk of data in 64 byte * pieces because we need to cache the last 64 bytes so that * we're not stuck with 0 bytes when the MSG_PART_FINAL * comes in. - KEY */ if (!(in_data_len % 64)) { oc_sha_ctx->tail_len = 64; memcpy(oc_sha_ctx->tail, in_data + in_data_len - 64, 64); in_data_len -= 64; } else oc_sha_ctx->tail_len = in_data_len & 0x3f; if(oc_sha_ctx->tail_len < 64) { in_data_len &= ~0x3f; memcpy(oc_sha_ctx->tail, in_data + in_data_len, oc_sha_ctx->tail_len); } } else if( ica_sha_ctx->runningLength == 0 && oc_sha_ctx->tail_len > 0 ) { /* Here we need to fill out the temporary tail buffer until * it has 64 bytes in it, then call icaSha1 on that buffer. * If there weren't enough bytes passed in to fill it out, * just copy in what we can and return success without calling * icaSha1. - KEY */ fill_size = 64 - oc_sha_ctx->tail_len; if(fill_size < in_data_len) { memcpy(oc_sha_ctx->tail + oc_sha_ctx->tail_len, in_data, fill_size); /* Submit the filled out save buffer */ if( icaSha1( adapter_handle, (unsigned int)SHA_MSG_PART_FIRST, (unsigned int)64, oc_sha_ctx->tail, (unsigned int)LENGTH_SHA_CONTEXT, ica_sha_ctx, &oc_sha_ctx->hash_len, oc_sha_ctx->hash)) return CKR_FUNCTION_FAILED; } else { memcpy(oc_sha_ctx->tail + oc_sha_ctx->tail_len, in_data, in_data_len); oc_sha_ctx->tail_len += in_data_len; return CKR_OK; } /* We had to use 'fill_size' bytes from in_data to fill out the * empty part of save data, so adjust in_data_len */ in_data_len -= fill_size; oc_sha_ctx->tail_len = in_data_len & 0x3f; if(oc_sha_ctx->tail_len) { memcpy(oc_sha_ctx->tail, in_data + fill_size, oc_sha_ctx->tail_len); in_data_len &= ~0x3f; } } else if( ica_sha_ctx->runningLength > 0 ) { oc_sha_ctx->message_part = SHA_MSG_PART_MIDDLE; if(oc_sha_ctx->tail_len) { fill_size = 64 - oc_sha_ctx->tail_len; if(fill_size < in_data_len) { memcpy(oc_sha_ctx->tail + oc_sha_ctx->tail_len, in_data, fill_size); /* Submit the filled out save buffer */ if( icaSha1( adapter_handle, (unsigned int)oc_sha_ctx->message_part, (unsigned int)64, oc_sha_ctx->tail, (unsigned int)LENGTH_SHA_CONTEXT, ica_sha_ctx, &oc_sha_ctx->hash_len, oc_sha_ctx->hash)) return CKR_FUNCTION_FAILED; } else { memcpy(oc_sha_ctx->tail + oc_sha_ctx->tail_len, in_data, in_data_len); oc_sha_ctx->tail_len += in_data_len; return CKR_OK; } /* We had to use some of the data from in_data to fill out the * empty part of save data, so adjust in_data_len */ in_data_len -= fill_size; oc_sha_ctx->tail_len = in_data_len & 0x3f; if(oc_sha_ctx->tail_len) { memcpy(oc_sha_ctx->tail, in_data + fill_size, oc_sha_ctx->tail_len); in_data_len &= ~0x3f; } } else { /* This is the odd case, where we need to go ahead and * send the first X * 64 byte chunks in to be processed * and copy the last <64 byte area into the tail. -KEY */ /* Just copying the last <64 bytes will not work in the case * of a user who SHA's a large chunk of data in 64 byte
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -