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

📄 ecc.c

📁 NIST推荐的素域上的椭圆曲线
💻 C
📖 第 1 页 / 共 2 页
字号:
       if (mp_read_radix(&order, (unsigned char *)sets[i].order, 10) != MP_OKAY)     { goto error; }       /* is prime actually prime? */       if (is_prime(&modulus, &primality) != CRYPT_OK)           { goto error; }       if (primality == 0) {          res = CRYPT_FAIL_TESTVECTOR;          goto done1;       }         /* is order prime ? */       if (is_prime(&order, &primality) != CRYPT_OK)             { goto error; }       if (primality == 0) {          res = CRYPT_FAIL_TESTVECTOR;          goto done1;       }       if (mp_read_radix(&G->x, (unsigned char *)sets[i].Gx, 16) != MP_OKAY) { goto error; }       if (mp_read_radix(&G->y, (unsigned char *)sets[i].Gy, 16) != MP_OKAY) { goto error; }       /* then we should have G == (order + 1)G */       if (mp_add_d(&order, 1, &order) != MP_OKAY)                  { goto error; }       if (ecc_mulmod(&order, G, GG, &modulus, i) != CRYPT_OK)      { goto error; }       if (mp_cmp(&G->x, &GG->x) || mp_cmp(&G->y, &GG->y)) {          res = CRYPT_FAIL_TESTVECTOR;          goto done1;       }   }   res = CRYPT_OK;   goto done1;error:   res = CRYPT_MEM;done1:   del_point(GG);   del_point(G);   mp_clear_multi(&order, &modulus, NULL);   return res;}void ecc_sizes(int *low, int *high){ int i; _ARGCHK(low != NULL); _ARGCHK(high != NULL); *low = INT_MAX; *high = 0; for (i = 0; sets[i].size; i++) {     if (sets[i].size < *low)  {         *low  = sets[i].size;      }     if (sets[i].size > *high) {         *high = sets[i].size;      } }}int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key){   int x, res, errno;   ecc_point *base;   mp_int prime;   unsigned char buf[4096];   _ARGCHK(key != NULL);   /* good prng? */   if ((errno = prng_is_valid(wprng)) != CRYPT_OK) {      return errno;   }   /* find key size */   for (x = 0; (keysize > sets[x].size) && (sets[x].size); x++);   keysize = sets[x].size;   if (sets[x].size == 0) {       return CRYPT_INVALID_KEYSIZE;   }   key->idx = x;   /* make up random string */   buf[0] = 0;   if (prng_descriptor[wprng].read(buf+1, keysize, prng) != (unsigned long)keysize) {      return CRYPT_ERROR_READPRNG;   }   /* setup the key variables */   if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL) != MP_OKAY) {       return CRYPT_MEM;   }   base = new_point();   if (base == NULL) {      mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, &prime, NULL);      return CRYPT_MEM;   }   /* read in the specs for this key */   if (mp_read_radix(&prime, (unsigned char *)sets[x].prime, 10) != MP_OKAY)  { goto error; }   if (mp_read_radix(&base->x, (unsigned char *)sets[x].Gx, 16) != MP_OKAY)   { goto error; }   if (mp_read_radix(&base->y, (unsigned char *)sets[x].Gy, 16) != MP_OKAY)   { goto error; }   if (mp_read_raw(&key->k, (unsigned char *)buf, keysize+1) != MP_OKAY)      { goto error; }   /* make the public key */   if (ecc_mulmod(&key->k, base, &key->pubkey, &prime, x) != CRYPT_OK) { goto error; }   key->type = PK_PRIVATE;   /* free up ram */   res = CRYPT_OK;   goto done;error:   res = CRYPT_MEM;done:   del_point(base);   mp_clear(&prime);#ifdef CLEAN_STACK   zeromem(buf, sizeof(buf));#endif   return res;}void ecc_free(ecc_key *key){   _ARGCHK(key != NULL);   mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);}static int compress_y_point(ecc_point *pt, int idx, int *result){   mp_int tmp, tmp2, p;   int res;   _ARGCHK(pt != NULL);   _ARGCHK(result != NULL);   if (mp_init_multi(&tmp, &tmp2, &p, NULL) != MP_OKAY) {      return CRYPT_MEM;   }   /* get x^3 - 3x + b */   if (mp_read_radix(&p, (unsigned char *)sets[idx].B, 16) != MP_OKAY) { goto error; } /* p = B */   if (mp_expt_d(&pt->x, 3, &tmp) != MP_OKAY)              { goto error; } /* tmp = pX^3  */   if (mp_mul_d(&pt->x, 3, &tmp2) != MP_OKAY)              { goto error; } /* tmp2 = 3*pX^3 */   if (mp_sub(&tmp, &tmp2, &tmp) != MP_OKAY)               { goto error; } /* tmp = tmp - tmp2 */   if (mp_add(&tmp, &p, &tmp) != MP_OKAY)                  { goto error; } /* tmp = tmp + p */   if (mp_read_radix(&p, (unsigned char *)sets[idx].prime, 10) != MP_OKAY)  { goto error; } /* p = prime */   if (mp_mod(&tmp, &p, &tmp) != MP_OKAY)                  { goto error; } /* tmp = tmp mod p */   /* now find square root */   if (mp_add_d(&p, 1, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = p + 1 */   if (mp_div_2(&tmp2, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = tmp2/2 */   if (mp_div_2(&tmp2, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = (p+1)/4 */   if (mp_exptmod(&tmp, &tmp2, &p, &tmp) != MP_OKAY)       { goto error; } /* tmp  = (x^3 - 3x + b)^((p+1)/4) mod p */   /* if tmp equals the y point give a 0, otherwise 1 */   if (mp_cmp(&tmp, &pt->y) == 0)      *result = 0;   else      *result = 1;      res = CRYPT_OK;   goto done;error:   res = CRYPT_MEM;done:   mp_clear_multi(&p, &tmp, &tmp2, NULL);   return res;}static int expand_y_point(ecc_point *pt, int idx, int result){   mp_int tmp, tmp2, p;   int res;   _ARGCHK(pt != NULL);    if (mp_init_multi(&tmp, &tmp2, &p, NULL) != MP_OKAY) {      return CRYPT_MEM;   }   /* get x^3 - 3x + b */   if (mp_read_radix(&p, (unsigned char *)sets[idx].B, 16) != MP_OKAY) { goto error; } /* p = B */   if (mp_expt_d(&pt->x, 3, &tmp) != MP_OKAY)              { goto error; } /* tmp = pX^3 */   if (mp_mul_d(&pt->x, 3, &tmp2) != MP_OKAY)              { goto error; } /* tmp2 = 3*pX^3 */   if (mp_sub(&tmp, &tmp2, &tmp) != MP_OKAY)               { goto error; } /* tmp = tmp - tmp2 */   if (mp_add(&tmp, &p, &tmp) != MP_OKAY)                  { goto error; } /* tmp = tmp + p */   if (mp_read_radix(&p, (unsigned char *)sets[idx].prime, 10) != MP_OKAY)  { goto error; } /* p = prime */   if (mp_mod(&tmp, &p, &tmp) != MP_OKAY)                  { goto error; } /* tmp = tmp mod p */   /* now find square root */   if (mp_add_d(&p, 1, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = p + 1 */   if (mp_div_2(&tmp2, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = tmp2/2 */   if (mp_div_2(&tmp2, &tmp2) != MP_OKAY)                  { goto error; } /* tmp2 = (p+1)/4 */   if (mp_exptmod(&tmp, &tmp2, &p, &tmp) != MP_OKAY)       { goto error; } /* tmp  = (x^3 - 3x + b)^((p+1)/4) mod p */   /* if result==0, then y==tmp, otherwise y==p-tmp */   if (result == 0) {      if (mp_copy(&tmp, &pt->y) != MP_OKAY) { goto error; }   } else {      if (mp_sub(&p, &tmp, &pt->y) != MP_OKAY) { goto error; }   }      res = CRYPT_OK;   goto done;error:   res = CRYPT_MEM;done:   mp_clear_multi(&p, &tmp, &tmp2, NULL);   return res;}#define OUTPUT_BIGNUM(num, buf2, y, z)         \{                                              \      z = mp_raw_size(num);                    \      STORE32L(z, buf2+y);                     \      y += 4;                                  \      mp_toraw(num, buf2+y);                   \      y += z;                                  \}#define INPUT_BIGNUM(num, in, x, y)                              \{                                                                \     /* load value */                                            \     LOAD32L(x, in+y);                                           \     y += 4;                                                     \                                                                 \     /* sanity check... */                                       \     if (x > 1024) {                                             \        goto error;                                              \     }                                                           \                                                                 \     /* load it */                                               \     if (mp_read_raw(num, (unsigned char *)in+y, x) != MP_OKAY) {\        goto error;                                              \     }                                                           \     y += x;                                                     \}int ecc_export(unsigned char *out, unsigned long *outlen, int type, ecc_key *key){   unsigned long y, z;   int res, errno;   unsigned char buf2[512];   _ARGCHK(out != NULL);   _ARGCHK(outlen != NULL);   _ARGCHK(key != NULL);   /* type valid? */   if (key->type != PK_PRIVATE && type == PK_PRIVATE) {       return CRYPT_PK_TYPE_MISMATCH;    }   /* output type and magic byte */   y = PACKET_SIZE;   buf2[y++] = type;   buf2[y++] = key->idx;   /* output x coordinate */   OUTPUT_BIGNUM(&(key->pubkey.x), buf2, y, z);   /* compress y and output it  */   if ((errno = compress_y_point(&key->pubkey, key->idx, &res)) != CRYPT_OK) {      return errno;   }   buf2[y++] = res;   if (type == PK_PRIVATE) {      OUTPUT_BIGNUM(&key->k, buf2, y, z);   }   /* check size */   if (*outlen < y) {       return CRYPT_BUFFER_OVERFLOW;   }   /* store header */   packet_store_header(buf2, PACKET_SECT_ECC, PACKET_SUB_KEY, y);   memcpy(out, buf2, y);   *outlen = y;   #ifdef CLEAN_STACK       zeromem(buf2, sizeof(buf2));   #endif   return CRYPT_OK;}int ecc_import(const unsigned char *in, ecc_key *key){   unsigned long x, y;   int res, errno;   _ARGCHK(in != NULL);   _ARGCHK(key != NULL);   /* check type */   if ((errno = packet_valid_header((unsigned char *)in, PACKET_SECT_ECC, PACKET_SUB_KEY)) != CRYPT_OK) {       return errno;   }   /* init key */   if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL) != MP_OKAY) {      return CRYPT_MEM;   }   y = PACKET_SIZE;   key->type = in[y++];   key->idx  = in[y++];   /* type check both values */   if ((key->type != PK_PUBLIC) && (key->type != PK_PRIVATE))  {      res = CRYPT_INVALID_PACKET;      goto error2;   }   /* is the key idx valid? */   if (!is_valid_idx(key->idx)) {      res = CRYPT_INVALID_PACKET;      goto error2;   }   /* load x coordinate */   INPUT_BIGNUM(&key->pubkey.x, in, x, y);     /* load y */   x = in[y++];   if ((errno = expand_y_point(&key->pubkey, key->idx, x)) != CRYPT_OK) { res = errno; goto error2; }   if (key->type == PK_PRIVATE) {      /* load private key */      INPUT_BIGNUM(&key->k, in, x, y);   }   res = CRYPT_OK;   goto done;error:   res = CRYPT_MEM;error2:   mp_clear_multi(&key->pubkey.x, &key->pubkey.y, &key->k, NULL);done:   return res;}int ecc_shared_secret(ecc_key *private_key, ecc_key *public_key,                       unsigned char *out, unsigned long *outlen){   unsigned long x, y;   ecc_point *result;   mp_int prime;   int res, errno;   _ARGCHK(private_key != NULL);   _ARGCHK(public_key != NULL);   _ARGCHK(out != NULL);   _ARGCHK(outlen != NULL);   /* type valid? */   if (private_key->type != PK_PRIVATE) {      return CRYPT_PK_NOT_PRIVATE;   }   if (private_key->idx != public_key->idx) {      return CRYPT_PK_TYPE_MISMATCH;   }   /* make new point */   result = new_point();   if (result == NULL) {       return CRYPT_MEM;   }   if (mp_init(&prime) != MP_OKAY) {       del_point(result);      return CRYPT_MEM;   }   if (mp_read_radix(&prime, (unsigned char *)sets[private_key->idx].prime, 10) != MP_OKAY) { goto error; }   if ((errno = ecc_mulmod(&private_key->k, &public_key->pubkey, result, &prime, private_key->idx)) != CRYPT_OK) { res = errno; goto done1; }   x = mp_raw_size(&result->x);   y = mp_raw_size(&result->y);   if (*outlen < (x+y)) {      res = CRYPT_BUFFER_OVERFLOW;      goto done1;   }   *outlen = x+y;   mp_toraw(&result->x, out);   mp_toraw(&result->y, out+x);   res = CRYPT_OK;   goto done1;error:   res = CRYPT_MEM;done1:   mp_clear(&prime);   del_point(result);   return res;}int ecc_get_size(ecc_key *key){   _ARGCHK(key != NULL);   if (is_valid_idx(key->idx))      return sets[key->idx].size;   else      return INT_MAX; /* large value known to cause it to fail when passed to ecc_make_key() */}#include "ecc_sys.c"#endif

⌨️ 快捷键说明

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