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

📄 ibecacheimpl.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "ibecache.h"
#include "ibe.h"
#include "mpint.h"
#include "ictk.h"
#include "errorctx.h"

/* The entry contains a bfCtx. Build the byte array that is the
 * element. Place the allocated byte array into the element VtItem in
 * the entry.
 *
 * @param ctx
 * @param cache
 * @param entry The entry containing the bfCtx.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltBuildCacheElementBF VOLT_PROTO_LIST ((
   VoltIBECacheCtx *ctx,
   VoltIBELocalCache *cache,
   VoltIBECacheEntry *entry,
   unsigned char *element,
   unsigned int bufferSize,
   unsigned int *elementLen
));

/* The entry contains a bbCtx. Build the byte array that is the
 * element. Place the allocated byte array into the element VtItem in
 * the entry.
 *
 * @param ctx
 * @param cache
 * @param entry The entry containing the bbCtx.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltBuildCacheElementBB VOLT_PROTO_LIST ((
   VoltIBECacheCtx *ctx,
   VoltIBELocalCache *cache,
   VoltIBECacheEntry *entry,
   unsigned char *element,
   unsigned int bufferSize,
   unsigned int *elementLen
));

/* Write an integer to the buffer.
 * <p>This function does no argument checking, so don't make any
 * mistakes in calling. For example, make sure the buffer is allocated
 * and big enough and make sure the byteCount is valid (not 0 and not
 * larger than the buffer, it should be 2 or 4, maybe in the future it
 * can be 8).
 * <p>It will write the low byteCount bytes of value.
 * <p>It will prepend 00 bytes if necessary.
 * <p>It will place the bytes in "big endian".
 * <pre>
 * <code>
 *    PlaceIntegerBE (buffer, 0x11223344, 4);
 *      places 4 bytes into buffer
 *      11 22 33 44
 *
 *    PlaceIntegerBE (buffer, 0x11223344, 2);
 *      places 2 bytes into buffer
 *      33 44
 *
 *    PlaceIntegerBE (buffer, 0x3344, 4);
 *      places 4 bytes into buffer
 *      00 00 33 44
 * </code>
 * </pre>
 */
static void VOLT_CALLING_CONV PlaceIntegerBE VOLT_PROTO_LIST ((
   unsigned char *buffer,
   unsigned int value,
   unsigned int byteCount
));

/* Get the integer in the buffer. The function will do no argument
 * checking, so make sure the buffer is valid and there are enough
 * bytes to extract an integer.
 * <p>The funtion will collect the integerSize bytes at buffer and
 * convert them into an unsigned int, returning that result.
 * <p>It will convert the number from big endian.
 * <p>NOTE! The return value is not an error code (status), but the
 * value computed.
 */
static unsigned int VOLT_CALLING_CONV GetIntegerBE VOLT_PROTO_LIST ((
   unsigned char *buffer,
   unsigned int integerSize
));

/* If zValue is not NULL, place that value. If it is NULL, place the
 * inputBuffer.
 * <p>This function does no argument checking, so don't make any
 * mistakes in calling. For example, make sure the buffer is allocated
 * and big enough and make sure the inputLen is valid (notlarger than
 * the buffer).
 * <p>The return is NOT an error code (status), it is the number of
 * bytes placed into the buffer.
 */
static unsigned int VOLT_CALLING_CONV PlaceBuffer VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   z_t *zValue,
   unsigned char *inputBuffer,
   unsigned int inputLen,
   unsigned int size,
   unsigned char *outputBuffer
));

/* Given the element (the size already checks out), build the bfCtx.
 * <p>This function creates a new bfCtx.
 */
static int VOLT_CALLING_CONV BuildBfCtxFromCacheEntry VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   unsigned int primeLen,
   unsigned int subprimeLen,
   unsigned int accelCount,
   unsigned char *element,
   bf_context_t **bfCtx
));

/* Given the element (the size already checks out), build the bbCtx.
 * <p>This function creates a new bbCtx.
 */
static int VOLT_CALLING_CONV BuildBbCtxFromCacheEntry VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   unsigned int primeLen,
   unsigned int subprimeLen,
   unsigned int accelCount,
   unsigned char *element,
   bb1_context_t **bbCtx
));

int VoltAddBfCtxToCache (
   Pointer ibeCacheCtx,
   unsigned int flag,
   Pointer theCtx
   )
{
  int status, status2;
  int lockAcquired = 0;
  unsigned int refLen;
  unsigned char *refBuf = (unsigned char *)0;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VtBFType1IBEParamInfo *bfParams = (VtBFType1IBEParamInfo *)0;
  VtBBType1IBEParamInfo *bbParams = (VtBBType1IBEParamInfo *)0;
  VoltIBECacheEntry *entry = (VoltIBECacheEntry *)0;
  VoltIBECacheEntry *nextEntry;
  VoltIBECacheEntry *newEntry = (VoltIBECacheEntry *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = ctx->LockIBECache(ibeCacheCtx);
    if (status != 0)
      break;
    
    lockAcquired = 1;
    
    /* Find the last entry. While we're looking, check to see if this
     * ctx is already in the cache. If so, we don't need to do anything.
     */
    status = 0;
    nextEntry = cache->cacheList;
    while (nextEntry != (VoltIBECacheEntry *)0)
    {
      entry = nextEntry;
      if ( (entry->ctxType == flag) && (entry->theCtx == theCtx) )
        break;

      nextEntry = (VoltIBECacheEntry *)(entry->nextEntry);
    }

    /* If nextEntry is not NULL, we broke out early because we found a
     * match. The ctx has already been added.
     */
    if (nextEntry != (VoltIBECacheEntry *)0)
      break;

    /* Get the parameter set to build the reference.
     */
    if (flag == VOLT_IBE_CTX_TYPE_BF)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltBuildIBEParamsFromBfCtx (
        libCtx, (bf_context_t *)theCtx, &bfParams);
      if (status != 0)
        break;
    }
    else
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltBuildIBEParamsFromBbCtx (
        libCtx, (bb1_context_t *)theCtx, &bbParams);
      if (status != 0)
        break;
    }

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBuildIBECacheRefAlloc (
      libCtx, bfParams, bbParams, &refBuf, &refLen);
    if (status != 0)
      break;

    /* Create a new entry.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newEntry = (VoltIBECacheEntry *)Z2Malloc (
      sizeof (VoltIBECacheEntry), 0);
    if (newEntry == (VoltIBECacheEntry *)0)
      break;
    Z2Memset (newEntry, 0, sizeof (VoltIBECacheEntry));

    newEntry->referenceCount = 1;
    newEntry->ctxType = flag;
    newEntry->reference.data = refBuf;
    newEntry->reference.len = refLen;
    newEntry->theCtx = theCtx;
    newEntry->previousEntry = (Pointer)entry;

    /* If there is a previous entry, set the newEntry as the previous
     * entry's next. If not, this newEntry is the first.
     */
    if (entry != (VoltIBECacheEntry *)0)
      entry->nextEntry = (Pointer)newEntry;
    else
      cache->cacheList = newEntry;

    cache->count++;

    status = 0;

  } while (0);

  VoltDemolishIBEParams (libCtx, &bfParams);
  VoltDemolishBBIBEParams (libCtx, &bbParams);

  if (lockAcquired)
  {
    status2 = ctx->UnlockIBECache(ibeCacheCtx);
    if (status == 0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = status2;
    }
  }
  
  if (status == 0)
    return (0);

  if (refBuf != (unsigned char *)0)
    Z2Free (refBuf);
  if (newEntry != (VoltIBECacheEntry *)0)
    Z2Free (newEntry);

  VOLT_LOG_ERROR_INFO (
    0, ibeCacheCtx, status, 0, errorType,
    (char *)0, "VoltAddBfCtxToCache", fnctLine, (char *)0)

  return (status);
}

int VoltSearchCacheForBfCtx (
   Pointer ibeCacheCtx,
   unsigned int flag,
   unsigned char *reference,
   unsigned int referenceLen,
   Pointer *theCtx
   )
{
  int status = 0;
  int status2;
  int lockAcquired = 0;
  unsigned int primeLen, offset, match;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VoltIBECacheEntry *entry;
  VoltIBECacheEntry *nextEntry;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Initialize the return to NULL, if we find something, we'll set it.
    */
    *theCtx = (Pointer)0;
    if ( (reference == (unsigned char *)0) || (referenceLen == 0) )
      break;

    /* Acquire the cache lock to syncronize access */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = ctx->LockIBECache(ibeCacheCtx);
    if (status != 0)
      break;
    
    lockAcquired = 1;
    
    /* Run through the entry link list. If we find a match with the
    * reference, return the ctx.
    */
    nextEntry = cache->cacheList;
    while (nextEntry != (VoltIBECacheEntry *)0)
    {
      match = 0;
      entry = nextEntry;
      nextEntry = (VoltIBECacheEntry *)(entry->nextEntry);
      if (entry->ctxType != flag)
        continue;
      if (entry->reference.len != referenceLen)
        continue;

      /* Make sure we get at least one coordinate from each point (one
      * coordinate from one point or two coordinates from one point is
      * not enough). Set the 1 bit of match if there's a coordinate from
      * the first two reference values. Set the 2 bit if there's a
      * coordinate from rht second two. If both bits are set after the
      * compares, we have a match.
      */
      primeLen = referenceLen >> 2;
      if ( (entry->reference.data[0] != 0) && (reference[0] != 0) )
      {
        if (Z2Memcmp (entry->reference.data, reference, primeLen) != 0)
          continue;
        match |= 1;
      }
      offset = primeLen;
      if ( (entry->reference.data[offset] != 0) && (reference[offset] != 0) )
      {
        if (Z2Memcmp (
          entry->reference.data + offset, reference + offset, primeLen) != 0)
          continue;
        match |= 1;
      }
      offset += primeLen;
      if ( (entry->reference.data[offset] != 0) && (reference[offset] != 0) )
      {
        if (Z2Memcmp (
          entry->reference.data + offset, reference + offset, primeLen) != 0)
          continue;
        match |= 2;
      }
      offset += primeLen;
      if ( (entry->reference.data[offset] != 0) && (reference[offset] != 0) )
      {
        if (Z2Memcmp (
          entry->reference.data + offset, reference + offset, primeLen) != 0)
          continue;
        match |= 2;
      }

      if (match != 3)
        continue;

      /* The reference matched, set theCtx to this entry's ctx and quit
      * looking.
      * Also, increment this entry's counter, indicating someone is
      * using this ctx.
      */
      *theCtx = entry->theCtx;
      entry->referenceCount++;
      break;
    }
  } while (0);
  
  if (lockAcquired)
  {
    status2 = ctx->UnlockIBECache(ibeCacheCtx);
    if (status == 0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = status2;
    }
  }

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, ibeCacheCtx, status, 0, 0,
    (char *)0, "VoltSearchCacheForBfCtx", fnctLine, (char *)0)

  return (status);
}

int VoltReleaseBfCtxFromCache (
   Pointer ibeCacheCtx,
   Pointer *theCtx
   )
{
  int status, status2;
  int lockAcquired = 0;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VoltIBECacheEntry *entry;
  VoltIBECacheEntry *nextEntry;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {

⌨️ 快捷键说明

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