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

📄 mech_des.c

📁 IBM的Linux上的PKCS#11实现
💻 C
📖 第 1 页 / 共 4 页
字号:
         // 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(113, __FILE__, __LINE__);      free( clear );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;}////CK_RVdes_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 ){   DES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      = NULL;   OBJECT       * key       = NULL;   CK_BYTE      * cipher    = NULL;   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 = (DES_CONTEXT *)ctx->context;   total = context->len + in_data_len;   if (total < DES_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 % DES_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_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      // 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_des_cbc_decrypt( cipher,   out_len,                                out_data, out_data_len,                                ctx->mech.pParameter,                                attr->pValue );      if (rc == CKR_OK) {         *out_data_len = out_len;         // the new init_v is the last decrypted data block         //         memcpy( ctx->mech.pParameter, cipher + (out_len - DES_BLOCK_SIZE), DES_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(114, __FILE__, __LINE__);      free( cipher );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;}////CK_RVdes_cbc_pad_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 ){   DES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      = NULL;   OBJECT       * key       = NULL;   CK_BYTE      * clear     = NULL;   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 = (DES_CONTEXT *)ctx->context;   total = (context->len + in_data_len);   // note, this is subtly different from the other encrypt update routines   //   if (total <= DES_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 + 1 byte      //      remain  = total % DES_BLOCK_SIZE;      out_len = total - remain;      if (remain == 0) {         remain     = DES_BLOCK_SIZE;         out_len -= DES_BLOCK_SIZE;      }      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      // at this point, we should have:      //    1) remain != 0      //    2) out_len != 0      //      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_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      // 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 );      //      // we don't do padding during the update      //      rc = ckm_des_cbc_encrypt( clear,    out_len,                                out_data, out_data_len,                                ctx->mech.pParameter,                                attr->pValue );      if (rc == CKR_OK) {         // the new init_v is the last encrypted data block         //         memcpy( ctx->mech.pParameter, out_data + (*out_data_len - DES_BLOCK_SIZE), DES_BLOCK_SIZE );         // copy the remaining 'new' input data to the temporary space         //         memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }      else         st_err_log(113, __FILE__, __LINE__);      free( clear );      return rc;   }}////CK_RVdes_cbc_pad_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 ){   DES_CONTEXT  * context   = NULL;   CK_ATTRIBUTE * attr      = NULL;   OBJECT       * key       = NULL;   CK_BYTE      * cipher    = NULL;   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 = (DES_CONTEXT *)ctx->context;   total = (context->len + in_data_len);   // note, this is subtly different from the other decrypt update routines   //   if (total <= DES_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 + 1 byte      //      remain  = total % DES_BLOCK_SIZE;      out_len = total - remain;      if (remain == 0) {         remain     = DES_BLOCK_SIZE;         out_len -= DES_BLOCK_SIZE;      }      if (length_only == TRUE) {         *out_data_len = out_len;         return CKR_OK;      }      // at this point, we should have:      //    1) remain != 0      //    2) out_len != 0      //      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_VALUE, &attr );      if (rc == FALSE){         st_err_log(4, __FILE__, __LINE__, __FUNCTION__);         return CKR_FUNCTION_FAILED;      }      // 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_des_cbc_decrypt( cipher,  out_len,                                out_data, out_data_len,                                ctx->mech.pParameter,                                attr->pValue );      if (rc == CKR_OK) {         // the new init_v is the last decrypted data block         //         memcpy( ctx->mech.pParameter, cipher + (out_len - DES_BLOCK_SIZE), DES_BLOCK_SIZE );         // copy the remaining 'new' input data to the temporary space         //         memcpy( context->data, in_data + (in_data_len - remain), remain );         context->len = remain;      }            else         st_err_log(114, __FILE__, __LINE__);      free( cipher );      return rc;   }   st_err_log(4, __FILE__, __LINE__, __FUNCTION__);   return CKR_FUNCTION_FAILED;}////CK_RVdes_ecb_encrypt_final( SESSION           *sess,                       CK_BBOOL           length_only,                       ENCR_DECR_CONTEXT *ctx,                       CK_BYTE           *out_data,                       CK_ULONG          *out_data_len ){   DES_CONTEXT *context   = NULL;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   // satisfy the compiler   //   if (length_only)      context = NULL;   context = (DES_CONTEXT *)ctx->context;   // DES-ECB does no padding so there had better not be   // any data in the context buffer.  if there is it means   // that the overall data length was not a multiple of the blocksize   //   if (context->len != 0){      st_err_log(109, __FILE__, __LINE__);      return CKR_DATA_LEN_RANGE;   }   *out_data_len = 0;   return CKR_OK;}////CK_RVdes_ecb_decrypt_final( SESSION           *sess,                       CK_BBOOL           length_only,                       ENCR_DECR_CONTEXT *ctx,                       CK_BYTE           *out_data,                       CK_ULONG          *out_data_len ){   DES_CONTEXT *context   = NULL;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;    }   // satisfy the compiler   //   if (length_only)      context = NULL;   context = (DES_CONTEXT *)ctx->context;   // DES-ECB does no padding so there had better not be   // any data in the context buffer.  if there is it means   // that the overall data length was not a multiple of the blocksize   //   if (context->len != 0){      st_err_log(109, __FILE__, __LINE__);      return CKR_DATA_LEN_RANGE;   }   *out_data_len = 0;   return CKR_OK;}////CK_RVdes_cbc_encrypt_final( SESSION           *sess,                       CK_BBOOL           length_only,                       ENCR_DECR_CONTEXT *ctx,                       CK_BYTE           *out_data,                       CK_ULONG          *out_data_len ){   DES_CONTEXT *context   = NULL;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   // satisfy the compiler   //   if (length_only)      context = NULL;   context = (DES_CONTEXT *)ctx->context;   // DES-CBC does no padding so there had better not be   // any data in the context buffer.  if there is it means   // that the overall data length was not a multiple of the blocksize   //   if (context->len != 0){      st_err_log(109, __FILE__, __LINE__);      return CKR_DATA_LEN_RANGE;   }   *out_data_len = 0;   return CKR_OK;}////CK_RVdes_cbc_decrypt_final( SESSION           *sess,                       CK_BBOOL           length_only,                       ENCR_DECR_CONTEXT *ctx,                       CK_BYTE           *out_data,                       CK_ULONG          *out_data_len ){   DES_CONTEXT *context   = NULL;   if (!sess || !ctx || !out_data_len){      st_err_log(4, __FILE__, __LINE__, __FUNCTION__);      return CKR_FUNCTION_FAILED;   }   // satisfy the compiler   //   if (length_only)      context = NULL;   context = (DES_CONTEXT *)ctx->context;   // DES-CBC does no padding so there had better not be   // any data in the context buffer.  if there is it means   // that the overall data length was not a multiple of the blocksize   //

⌨️ 快捷键说明

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