0

我正在为 contiki os 移植椭圆曲线密码学(基于纯 c)。我收到分段错误的错误。我使用 xxgdb 进行调试,但仍然找不到原因。而且我对c中的调试没有明确的想法。请帮助如何始终摆脱 c 中的这些分段错误(如何找出发生此分段错误的原因)。

这是我的源代码。

#include <stdio.h>
#include <stdint.h>
typedef uint32_t NN_DIGIT;
typedef uint64_t NN_DOUBLE_DIGIT;

/* Types for length */
typedef uint8_t NN_UINT;
typedef uint16_t NN_UINT2;

#if defined (SECP128R1) || defined (SECP128R2)
#define KEY_BIT_LEN 128
#else 
#if defined (SECP160K1) || defined (SECP160R1) || defined (SECP160R2) 
#define KEY_BIT_LEN 160
#else 
#if defined (SECP192K1) || defined (SECP192R1)
#define KEY_BIT_LEN 192
#else
#define KEY_BIT_LEN 128
#endif /* 192 */
#endif /* 160 */
#endif /* 128 */

/* Length of digit in bits */
#define NN_DIGIT_BITS 32

/* Length of digit in bytes */
#define NN_DIGIT_LEN (NN_DIGIT_BITS/8)

/* Maximum value of digit */
#define MAX_NN_DIGIT 0xffffffff

/* Number of digits in key
 * used by optimized mod multiplication (ModMultOpt) and optimized mod square (ModSqrOpt)
 *
 */
#define KEYDIGITS (KEY_BIT_LEN/NN_DIGIT_BITS) //5

/* Maximum length in digits */
#define MAX_NN_DIGITS (KEYDIGITS+1)

/* buffer size
 *should be large enough to hold order of base point
 */
#define NUMWORDS MAX_NN_DIGITS

/* the mask for ModSqrOpt */
#define MOD_SQR_MASK1 0x8000000000000000ll
#define MOD_SQR_MASK2 0x0000000100000000ll

typedef struct Point
{
    // point's coordinates
    NN_DIGIT x[NUMWORDS];
    NN_DIGIT y[NUMWORDS];
} Point;

Point pbkey_alice;
void 
NN_Encode(unsigned char *a, NN_UINT len, NN_DIGIT *b, NN_UINT digits)
{
  NN_DIGIT t;
  int j;
  unsigned int i, u;

  for(i = 0, j = len - 1; i < digits && j >= 0; i++) {
    t = b[i];
    for(u = 0; j >= 0 && u < NN_DIGIT_BITS; j--, u += 8) {
      a[j] = (unsigned char)(t >> u);
    }
  }

  for(; j >= 0; j--) {
    a[j] = 0;
  }
}

int ecc_point2octet(uint8_t *octet, NN_UINT octet_len, Point *P, int compress)
{
    if (compress){
        if(octet_len < KEYDIGITS*NN_DIGIT_LEN+1)
      {//too small octet
        return -1;
    }
    else
    {
          //compressed point representation
            if((1 & P->y[0]) == 0){
        octet[0] = 0x02;
            }
            else
                {
                octet[0] = 0x03;
                }
            NN_Encode(octet+1, KEYDIGITS*NN_DIGIT_LEN, P->x, KEYDIGITS);
            return KEYDIGITS*NN_DIGIT_LEN+1;
      }
    }
  else
  {//non compressed
     if(octet_len < 2*KEYDIGITS*NN_DIGIT_LEN+1)
    {

        return -1;
    }
    else
    {
            //octet[0] = 0x04;
            NN_Encode(octet+1, KEYDIGITS*NN_DIGIT_LEN, P->x, KEYDIGITS);
            NN_Encode(octet+1+KEYDIGITS*NN_DIGIT_LEN, KEYDIGITS*NN_DIGIT_LEN, P->y, KEYDIGITS);
            return 2*KEYDIGITS*NN_DIGIT_LEN+1;
        }
  }
}

int main()
{
pbkey_alice.x[5] = 0x00000000;
  pbkey_alice.x[4] = 0x21961f69;
  pbkey_alice.x[3] = 0xf02d202b;
  pbkey_alice.x[2] = 0xa4b41f1a;
  pbkey_alice.x[1] = 0x0aa08a86;
  pbkey_alice.x[0] = 0xdf27908d;

  pbkey_alice.y[5] = 0x00000000;
  pbkey_alice.y[4] = 0x378e1278;
  pbkey_alice.y[3] = 0x62836d75;
  pbkey_alice.y[2] = 0x7acb7ca4;
  pbkey_alice.y[1] = 0x0dc0ad13;
  pbkey_alice.y[0] = 0x741e287c;
uint8_t *C;
int C_len = 2*KEYDIGITS*NN_DIGIT_LEN + 1 + 20 + 20;
int oct_len = ecc_point2octet(C, C_len, &pbkey_alice, 0);

}
4

2 回答 2

5

C将其传递给ecc_point2octet(). 这似乎是您所看到的那种问题的秘诀。

于 2012-09-24T04:34:43.963 回答
0

正如卡尔所说,只需替换 : 就uint8_t *C可以uint8_t *C = malloc(sizeof(uint8_t))了。

于 2012-09-24T08:51:49.940 回答