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

📄 deskey.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "keyobj.h"
#include "des.h"
#include "errorctx.h"

/* Convert 0 to 1
 *         1 to 1
 *         2 to 2
 *         3 to 2
 *          ...
 */
#define DES_PARITY_TABLE \
{ \
0x01,0x01,0x02,0x02,0x04,0x04,0x07,0x07,0x08,0x08,0x0b,0x0b,0x0d,0x0d,0x0e,0x0e, \
0x10,0x10,0x13,0x13,0x15,0x15,0x16,0x16,0x19,0x19,0x1a,0x1a,0x1c,0x1c,0x1f,0x1f, \
0x20,0x20,0x23,0x23,0x25,0x25,0x26,0x26,0x29,0x29,0x2a,0x2a,0x2c,0x2c,0x2f,0x2f, \
0x31,0x31,0x32,0x32,0x34,0x34,0x37,0x37,0x38,0x38,0x3b,0x3b,0x3d,0x3d,0x3e,0x3e, \
0x40,0x40,0x43,0x43,0x45,0x45,0x46,0x46,0x49,0x49,0x4a,0x4a,0x4c,0x4c,0x4f,0x4f, \
0x51,0x51,0x52,0x52,0x54,0x54,0x57,0x57,0x58,0x58,0x5b,0x5b,0x5d,0x5d,0x5e,0x5e, \
0x61,0x61,0x62,0x62,0x64,0x64,0x67,0x67,0x68,0x68,0x6b,0x6b,0x6d,0x6d,0x6e,0x6e, \
0x70,0x70,0x73,0x73,0x75,0x75,0x76,0x76,0x79,0x79,0x7a,0x7a,0x7c,0x7c,0x7f,0x7f, \
0x80,0x80,0x83,0x83,0x85,0x85,0x86,0x86,0x89,0x89,0x8a,0x8a,0x8c,0x8c,0x8f,0x8f, \
0x91,0x91,0x92,0x92,0x94,0x94,0x97,0x97,0x98,0x98,0x9b,0x9b,0x9d,0x9d,0x9e,0x9e, \
0xa1,0xa1,0xa2,0xa2,0xa4,0xa4,0xa7,0xa7,0xa8,0xa8,0xab,0xab,0xad,0xad,0xae,0xae, \
0xb0,0xb0,0xb3,0xb3,0xb5,0xb5,0xb6,0xb6,0xb9,0xb9,0xba,0xba,0xbc,0xbc,0xbf,0xbf, \
0xc1,0xc1,0xc2,0xc2,0xc4,0xc4,0xc7,0xc7,0xc8,0xc8,0xcb,0xcb,0xcd,0xcd,0xce,0xce, \
0xd0,0xd0,0xd3,0xd3,0xd5,0xd5,0xd6,0xd6,0xd9,0xd9,0xda,0xda,0xdc,0xdc,0xdf,0xdf, \
0xe0,0xe0,0xe3,0xe3,0xe5,0xe5,0xe6,0xe6,0xe9,0xe9,0xea,0xea,0xec,0xec,0xef,0xef, \
0xf1,0xf1,0xf2,0xf2,0xf4,0xf4,0xf7,0xf7,0xf8,0xf8,0xfb,0xfb,0xfd,0xfd,0xfe,0xfe  \
}

/* Implements VCheckKeyLen.
 */
int VOLT_CALLING_CONV DESCheckKeyLen VOLT_PROTO_LIST ((
   VtLibCtx libraryCtx,
   unsigned int keyLen
));

int VtKeyParamDES (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    if (flag == VOLT_KEY_GET_TYPE_FLAG)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetSymmetricKey (
        object, (Pointer *)info, VOLT_KEY_ALG_DES);
      break;
    }

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltSetSymmetricKey (
      object, info, flag, DESCheckKeyLen,
      VOLT_KEY_ALG_DES | VOLT_KEY_TYPE_DATA);
    if (status != 0)
      break;

    VoltSetDESParity ((VtItem *)(obj->keyData));

    /* If successful, set the FIPS bit in the object type, this object
     * is a FIPS object.
     */
    ((VoltObject *)object)->objectType |= VOLT_OBJECT_TYPE_FIPS;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, ((VoltObject *)object)->libraryCtx, status, 0, fnctLine,
    "VtKeyParamDES", (char *)0)

  return (status);
}

int DESCheckKeyLen (
   VtLibCtx libraryCtx,
   unsigned int keyLen
   )
{
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* A DES key can only be 8 bytes long.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  if (keyLen == 8)
    return (0);

  VOLT_LOG_ERROR (
    libraryCtx, VT_ERROR_INVALID_KEY_LENGTH, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "DESCheckKeyLen", (char *)0)

  return (VT_ERROR_INVALID_KEY_LENGTH);
}

void VoltSetDESParity (
   VtItem *keyData
   )
{
  unsigned int index;
  unsigned char parityTable[256] = DES_PARITY_TABLE;

  /* Set the parity bits. How many of the first 7 bits of a byte are
   * set? If the number of bits set is odd, set the parity bit to 0,
   * if even, set the parity bit to 1.
   * For example,
   *   1011 100?   --->   1011 1001
   *   0001 000?   --->   0001 0000
   */
  for (index = 0; index < keyData->len; ++index)
    keyData->data[index] = parityTable[keyData->data[index]];
}

⌨️ 快捷键说明

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