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

📄 mech_aes.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 5 页
字号:
      //      remain  = (total % AES_BLOCK_SIZE);      out_len = (total - remain);     // should always be at least 1 block      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      rc = object_mgr_find_in_map1( ctx->key, &key );      if (rc != CKR_OK){         st_err_log(110, __FILE__, __LINE__);         return rc;      }      rc = template_attribute_find( key->template, CKA_KEY_TYPE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      keytype = *(CK_KEY_TYPE *)attr->pValue;      rc = template_attribute_find( key->template, CKA_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      memcpy( key_value, attr->pValue, attr->ulValueLen );      clear = (CK_BYTE *)malloc( out_len );      if (!clear){         st_err_log(0, __FILE__, __LINE__);         return CKR_HOST_MEMORY;      }      // copy any data left over from the previous encryption operation first      //      memcpy( clear,                context->data, context->len );      memcpy( clear + context->len, in_data,       out_len - context->len );      rc = ckm_aes_ecb_encrypt( clear, out_len, out_data, 				out_data_len, key_value, attr->ulValueLen );      if (rc == CKR_OK) {         *out_data_len = out_len;         // update the context buffer.  we already used the buffer's current         // contents so we completely overwrite it         //         if (remain != 0)            memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }      free( clear );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;  // shouldn't reach this}////CK_RVaes_ecb_decrypt_update( SESSION           *sess,                        CK_BBOOL           length_only,                        ENCR_DECR_CONTEXT *ctx,                        CK_BYTE           *in_data,                        CK_ULONG           in_data_len,                        CK_BYTE           *out_data,                        CK_ULONG          *out_data_len ){   AES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      = NULL;   OBJECT       * key       = NULL;   CK_BYTE      * cipher    = NULL;   CK_BYTE        key_value[AES_KEY_SIZE_256];   CK_KEY_TYPE    keytype;   CK_ULONG       total, remain, out_len;   CK_RV          rc;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   context = (AES_CONTEXT *)ctx->context;   total = (context->len + in_data_len);   if (total < AES_BLOCK_SIZE) {      if (length_only == FALSE) {         memcpy( context->data + context->len, in_data, in_data_len );         context->len += in_data_len;      }      *out_data_len = 0;      return CKR_OK;   }   else {      // we have at least 1 block      //      remain  = (total % AES_BLOCK_SIZE);      out_len = total - remain;      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      rc = object_mgr_find_in_map1( ctx->key, &key );      if (rc != CKR_OK){         st_err_log(110, __FILE__, __LINE__);         return rc;      }      rc = template_attribute_find( key->template, CKA_KEY_TYPE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      keytype = *(CK_KEY_TYPE *)attr->pValue;      rc = template_attribute_find( key->template, CKA_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }            memcpy( key_value, attr->pValue, attr->ulValueLen );      cipher = (CK_BYTE *)malloc( out_len );      if (!cipher){         st_err_log(0, __FILE__, __LINE__);         return CKR_HOST_MEMORY;      }      // copy any data left over from the previous decryption operation first      //      memcpy( cipher,                context->data, context->len );      memcpy( cipher + context->len, in_data,       out_len - context->len );      rc = ckm_aes_ecb_decrypt( cipher, out_len, out_data, out_data_len,				key_value, attr->ulValueLen );      if (rc == CKR_OK) {         *out_data_len = out_len;         // copy the remaining 'new' input data to the context buffer         //         if (remain != 0)            memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }      else          st_err_log(105, __FILE__, __LINE__);      free( cipher );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;  // shouldn't reach this}////CK_RVaes_cbc_encrypt_update( SESSION           *sess,                        CK_BBOOL           length_only,                        ENCR_DECR_CONTEXT *ctx,                        CK_BYTE           *in_data,                        CK_ULONG           in_data_len,                        CK_BYTE           *out_data,                        CK_ULONG          *out_data_len ){   AES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      =  NULL;   OBJECT       * key       = NULL;   CK_BYTE      * clear     = NULL;   CK_BYTE        key_value[AES_KEY_SIZE_256];   CK_KEY_TYPE    keytype;   CK_ULONG       total, remain, out_len;   CK_RV          rc;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   context = (AES_CONTEXT *)ctx->context;   total = (context->len + in_data_len);   if (total < AES_BLOCK_SIZE) {      if (length_only == FALSE) {         memcpy( context->data + context->len, in_data, in_data_len );         context->len += in_data_len;      }      *out_data_len = 0;      return CKR_OK;   }   else {      // we have at least 1 block      //      remain  = (total % AES_BLOCK_SIZE);      out_len = total - remain;      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      rc = object_mgr_find_in_map1( ctx->key, &key );      if (rc != CKR_OK){         st_err_log(110, __FILE__, __LINE__);         return rc;      }      rc = template_attribute_find( key->template, CKA_KEY_TYPE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      keytype = *(CK_KEY_TYPE *)attr->pValue;      rc = template_attribute_find( key->template, CKA_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }            memcpy( key_value, attr->pValue, attr->ulValueLen );      // these buffers need to be longword aligned      //      clear  = (CK_BYTE *)malloc( out_len );      if (!clear){         st_err_log(0, __FILE__, __LINE__);         return CKR_HOST_MEMORY;      }      // copy any data left over from the previous encryption operation first      //      memcpy( clear,                context->data, context->len );      memcpy( clear + context->len, in_data,       out_len - context->len );      rc = ckm_aes_cbc_encrypt( clear,    out_len,                                out_data, out_data_len,                                ctx->mech.pParameter,                                key_value, attr->ulValueLen );      if (rc == CKR_OK) {         *out_data_len = out_len;         // the new init_v is the last encrypted data block         //         memcpy( ctx->mech.pParameter, out_data + (*out_data_len - AES_BLOCK_SIZE), AES_BLOCK_SIZE );         // copy the remaining 'new' input data to the context buffer         //         if (remain != 0)            memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }      else          st_err_log(105, __FILE__, __LINE__);      free( clear );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;}////CK_RVaes_cbc_decrypt_update( SESSION           *sess,                        CK_BBOOL           length_only,                        ENCR_DECR_CONTEXT *ctx,                        CK_BYTE           *in_data,                        CK_ULONG           in_data_len,                        CK_BYTE           *out_data,                        CK_ULONG          *out_data_len ){   AES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      = NULL;   OBJECT       * key       = NULL;   CK_BYTE      * cipher    = NULL;   CK_BYTE        key_value[AES_KEY_SIZE_256];   CK_KEY_TYPE    keytype;   CK_ULONG       total, remain, out_len;   CK_RV          rc;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   context = (AES_CONTEXT *)ctx->context;   total = context->len + in_data_len;   if (total < AES_BLOCK_SIZE) {      if (length_only == FALSE) {         memcpy( context->data + context->len, in_data, in_data_len );         context->len += in_data_len;      }      *out_data_len = 0;      return CKR_OK;   }   else {      // we have at least 1 block      //      remain  = total % AES_BLOCK_SIZE;      out_len = total - remain;      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      rc = object_mgr_find_in_map1( ctx->key, &key );      if (rc != CKR_OK){         st_err_log(110, __FILE__, __LINE__);         return rc;      }      rc = template_attribute_find( key->template, CKA_KEY_TYPE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      keytype = *(CK_KEY_TYPE *)attr->pValue;      rc = template_attribute_find( key->template, CKA_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }            memcpy( key_value, attr->pValue, attr->ulValueLen );      // these buffers need to be longword aligned      //      cipher = (CK_BYTE *)malloc( out_len );      if (!cipher){         st_err_log(0, __FILE__, __LINE__);         return CKR_HOST_MEMORY;      }      // copy any data left over from the previous decryption operation first      //      memcpy( cipher,                context->data, context->len );      memcpy( cipher + context->len, in_data,       out_len - context->len );      rc = ckm_aes_cbc_decrypt( cipher,    out_len,                                out_data,  out_data_len,                                ctx->mech.pParameter,                                key_value, attr->ulValueLen );      if (rc == CKR_OK) {         *out_data_len = out_len;         // the new init_v is the last input data block         //         memcpy( ctx->mech.pParameter, cipher + (out_len - AES_BLOCK_SIZE), AES_BLOCK_SIZE );         // copy the remaining 'new' input data to the context buffer         //         if (remain != 0)            memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }      else          st_err_log(106, __FILE__, __LINE__);      free( cipher );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;}////CK_RVaes_cbc_pad_encrypt_update( SESSION           *sess,                            CK_BBOOL           length_only,

⌨️ 快捷键说明

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