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

📄 ecc.c

📁 这是一个用来加解密的算法库
💻 C
📖 第 1 页 / 共 2 页
字号:
/* LibTomCrypt, modular cryptographic library -- Tom St Denis * * LibTomCrypt is a library that provides various cryptographic * algorithms in a highly modular and flexible manner. * * The library is free for all purposes without any express * guarantee it works. * * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org *//* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b * * All curves taken from NIST recommendation paper of July 1999 * Available at http://csrc.nist.gov/cryptval/dss.htm */#include "tomcrypt.h"/**  @file ecc.c  ECC Crypto, Tom St Denis*/  #ifdef MECC/* size of our temp buffers for exported keys */#define ECC_BUF_SIZE 160/* max private key size */#define ECC_MAXSIZE  66/* This holds the key settings.  ***MUST*** be organized by size from smallest to largest. */static const struct {   int size;   char *name, *prime, *B, *order, *Gx, *Gy;} sets[] = {#ifdef ECC160{   20,   "ECC-160",   /* prime */   "G00000000000000000000000007",   /* B */   "1oUV2vOaSlWbxr6",   /* order */   "G0000000000004sCQUtDxaqDUN5",   /* Gx */   "jpqOf1BHus6Yd/pyhyVpP",   /* Gy */   "D/wykuuIFfr+vPyx7kQEPu8MixO",},#endif#ifdef ECC192{    24,   "ECC-192",   /* prime */   "/////////////////////l//////////",   /* B */   "P2456UMSWESFf+chSYGmIVwutkp1Hhcn",   /* order */   "////////////////cTxuDXHhoR6qqYWn",   /* Gx */   "68se3h0maFPylo3hGw680FJ/2ls2/n0I",   /* Gy */   "1nahbV/8sdXZ417jQoJDrNFvTw4UUKWH"},#endif#ifdef ECC224{   28,   "ECC-224",   /* prime */   "400000000000000000000000000000000000BV",   /* B */   "21HkWGL2CxJIp",   /* order */   "4000000000000000000Kxnixk9t8MLzMiV264/",   /* Gx */   "jpqOf1BHus6Yd/pyhyVpP",   /* Gy */   "3FCtyo2yHA5SFjkCGbYxbOvNeChwS+j6wSIwck",},#endif#ifdef ECC256{   32,   "ECC-256",   /* Prime */   "F////y000010000000000000000////////////////",   /* B */   "5h6DTYgEfFdi+kzLNQOXhnb7GQmp5EmzZlEF3udqc1B",   /* Order */   "F////y00000//////////+yvlgjfnUUXFEvoiByOoLH",   /* Gx */   "6iNqVBXB497+BpcvMEaGF9t0ts1BUipeFIXEKNOcCAM",   /* Gy */   "4/ZGkB+6d+RZkVhIdmFdXOhpZDNQp5UpiksG6Wtlr7r"},#endif#ifdef ECC384{   48,   "ECC-384",   /* prime */   "//////////////////////////////////////////x/////00000000003/"   "////",   /* B */   "ip4lf+8+v+IOZWLhu/Wj6HWTd6x+WK4I0nG8Zr0JXrh6LZcDYYxHdIg5oEtJ"   "x2hl",   /* Order */   "////////////////////////////////nsDDWVGtBTzO6WsoIB2dUkpi6MhC"   "nIbp",   /* Gx and Gy */   "geVA8hwB1JUEiSSUyo2jT6uTEsABfvkOMVT1u89KAZXL0l9TlrKfR3fKNZXo"   "TWgt",   "DXVUIfOcB6zTdfY/afBSAVZq7RqecXHywTen4xNmkC0AOB7E7Nw1dNf37NoG"   "wWvV"},#endif#ifdef ECC521{   65,   "ECC-521",   /* prime */   "V///////////////////////////////////////////////////////////"   "///////////////////////////",   /* B */   "56LFhbXZXoQ7vAQ8Q2sXK3kejfoMvcp5VEuj8cHZl49uLOPEL7iVfDx5bB0l"   "JknlmSrSz+8FImqyUz57zHhK3y0",   /* Order */   "V//////////////////////////////////////////+b66XuE/BvPhVym1I"   "FS9fT0xjScuYPn7hhjljnwHE6G9",   /* Gx and Gy */   "CQ5ZWQt10JfpPu+osOZbRH2d6I1EGK/jI7uAAzWQqqzkg5BNdVlvrae/Xt19"   "wB/gDupIBF1XMf2c/b+VZ72vRrc",   "HWvAMfucZl015oANxGiVHlPcFL4ILURH6WNhxqN9pvcB9VkSfbUz2P0nL2v0"   "J+j1s4rF726edB2G8Y+b7QVqMPG",},#endif{   0,   NULL, NULL, NULL, NULL, NULL, NULL}};#if 0/* you plug in a prime and B value and it finds a pseudo-random base point */void ecc_find_base(void){   static char *prime = "26959946667150639794667015087019630673637144422540572481103610249951";   static char *order = "26959946667150639794667015087019637467111563745054605861463538557247";   static char *b     = "9538957348957353489587";   mp_int pp, p, r, B, tmp1, tmp2, tx, ty, x, y;   char buf[4096];   int i;   mp_init_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);   mp_read_radix(&p, prime, 10);   mp_read_radix(&r, order, 10);   mp_read_radix(&B, b, 10);   /* get (p+1)/4 */   mp_add_d(&p, 1, &pp);   mp_div_2(&pp, &pp);   mp_div_2(&pp, &pp);   buf[0] = 0;   do {      printf("."); fflush(stdout);      /* make a random value of x */      for (i = 0; i < 16; i++) buf[i+1] = rand() & 255;      mp_read_raw(&x, buf, 17);      mp_copy(&x, &tx);      /* now compute x^3 - 3x + b */      mp_expt_d(&x, 3, &tmp1);      mp_mul_d(&x, 3, &tmp2);      mp_sub(&tmp1, &tmp2, &tmp1);      mp_add(&tmp1, &B, &tmp1);      mp_mod(&tmp1, &p, &tmp1);      /* now compute sqrt via x^((p+1)/4) */      mp_exptmod(&tmp1, &pp, &p, &tmp2);      mp_copy(&tmp2, &ty);      /* now square it */      mp_sqrmod(&tmp2, &p, &tmp2);      /* tmp2 should equal tmp1 */   } while (mp_cmp(&tmp1, &tmp2));   /* now output values in way that libtomcrypt wants */   mp_todecimal(&p, buf);   printf("\n\np==%s\n", buf);   mp_tohex(&B, buf);   printf("b==%s\n", buf);   mp_todecimal(&r, buf);   printf("r==%s\n", buf);   mp_tohex(&tx, buf);   printf("Gx==%s\n", buf);   mp_tohex(&ty, buf);   printf("Gy==%s\n", buf);   mp_clear_multi(&tx, &ty, &x, &y, &p, &pp, &r, &B, &tmp1, &tmp2, NULL);} #endifstatic int is_valid_idx(int n){   int x;   for (x = 0; sets[x].size != 0; x++);   if ((n < 0) || (n >= x)) {      return 0;   }   return 1;}static ecc_point *new_point(void){   ecc_point *p;   p = XMALLOC(sizeof(ecc_point));   if (p == NULL) {      return NULL;   }   if (mp_init_multi(&p->x, &p->y, NULL) != MP_OKAY) {      XFREE(p);      return NULL;   }   return p;}static void del_point(ecc_point *p){   /* prevents free'ing null arguments */   if (p != NULL) {      mp_clear_multi(&p->x, &p->y, NULL);      XFREE(p);   }}/* double a point R = 2P, R can be P*/static int dbl_point(ecc_point *P, ecc_point *R, mp_int *modulus, mp_int *mu){   mp_int s, tmp, tmpx;   int err;   if ((err = mp_init_multi(&s, &tmp, &tmpx, NULL)) != MP_OKAY) {      return mpi_to_ltc_error(err);   }   /* s = (3Xp^2 + a) / (2Yp) */   if ((err = mp_mul_2(&P->y, &tmp)) != MP_OKAY)                   { goto error; } /* tmp = 2*y */   if ((err = mp_invmod(&tmp, modulus, &tmp)) != MP_OKAY)          { goto error; } /* tmp = 1/tmp mod modulus */   if ((err = mp_sqr(&P->x, &s)) != MP_OKAY)                       { goto error; } /* s = x^2  */   if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY)              { goto error; }   if ((err = mp_mul_d(&s,(mp_digit)3, &s)) != MP_OKAY)            { goto error; } /* s = 3*(x^2) */   if ((err = mp_sub_d(&s,(mp_digit)3, &s)) != MP_OKAY)            { goto error; } /* s = 3*(x^2) - 3 */   if (mp_cmp_d(&s, 0) == MP_LT) {                                         /* if s < 0 add modulus */      if ((err = mp_add(&s, modulus, &s)) != MP_OKAY)              { goto error; }   }   if ((err = mp_mul(&s, &tmp, &s)) != MP_OKAY)                    { goto error; } /* s = tmp * s mod modulus */   if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY)              { goto error; }   /* Xr = s^2 - 2Xp */   if ((err = mp_sqr(&s,  &tmpx)) != MP_OKAY)                      { goto error; } /* tmpx = s^2  */   if ((err = mp_reduce(&tmpx, modulus, mu)) != MP_OKAY)           { goto error; } /* tmpx = tmpx mod modulus */   if ((err = mp_sub(&tmpx, &P->x, &tmpx)) != MP_OKAY)             { goto error; } /* tmpx = tmpx - x */   if ((err = mp_submod(&tmpx, &P->x, modulus, &tmpx)) != MP_OKAY) { goto error; } /* tmpx = tmpx - x mod modulus */   /* Yr = -Yp + s(Xp - Xr)  */   if ((err = mp_sub(&P->x, &tmpx, &tmp)) != MP_OKAY)              { goto error; } /* tmp = x - tmpx */   if ((err = mp_mul(&tmp, &s, &tmp)) != MP_OKAY)                  { goto error; } /* tmp = tmp * s */   if ((err = mp_submod(&tmp, &P->y, modulus, &R->y)) != MP_OKAY)  { goto error; } /* y = tmp - y mod modulus */   if ((err = mp_copy(&tmpx, &R->x)) != MP_OKAY)                   { goto error; } /* x = tmpx */   err = CRYPT_OK;   goto done;error:   err = mpi_to_ltc_error(err);done:   mp_clear_multi(&tmpx, &tmp, &s, NULL);   return err;}/* add two different points over Z/pZ, R = P + Q, note R can equal either P or Q */static int add_point(ecc_point *P, ecc_point *Q, ecc_point *R, mp_int *modulus, mp_int *mu){   mp_int s, tmp, tmpx;   int err;   if ((err = mp_init(&tmp)) != MP_OKAY) {      return mpi_to_ltc_error(err);   }   /* is P==Q or P==-Q? */   if (((err = mp_neg(&Q->y, &tmp)) != MP_OKAY) || ((err = mp_mod(&tmp, modulus, &tmp)) != MP_OKAY)) {      mp_clear(&tmp);      return mpi_to_ltc_error(err);   }   if (mp_cmp(&P->x, &Q->x) == MP_EQ)      if (mp_cmp(&P->y, &Q->y) == MP_EQ || mp_cmp(&P->y, &tmp) == MP_EQ) {         mp_clear(&tmp);         return dbl_point(P, R, modulus, mu);      }   if ((err = mp_init_multi(&tmpx, &s, NULL)) != MP_OKAY) {      mp_clear(&tmp);      return mpi_to_ltc_error(err);   }   /* get s = (Yp - Yq)/(Xp-Xq) mod p */   if ((err = mp_sub(&P->x, &Q->x, &tmp)) != MP_OKAY)                 { goto error; } /* tmp = Px - Qx mod modulus */   if (mp_cmp_d(&tmp, 0) == MP_LT) {                                          /* if tmp<0 add modulus */      if ((err = mp_add(&tmp, modulus, &tmp)) != MP_OKAY)             { goto error; }   }   if ((err = mp_invmod(&tmp, modulus, &tmp)) != MP_OKAY)             { goto error; } /* tmp = 1/tmp mod modulus */   if ((err = mp_sub(&P->y, &Q->y, &s)) != MP_OKAY)                   { goto error; } /* s = Py - Qy mod modulus */   if (mp_cmp_d(&s, 0) == MP_LT) {                                            /* if s<0 add modulus */      if ((err = mp_add(&s, modulus, &s)) != MP_OKAY)                 { goto error; }   }   if ((err = mp_mul(&s, &tmp, &s)) != MP_OKAY)                       { goto error; } /* s = s * tmp mod modulus */   if ((err = mp_reduce(&s, modulus, mu)) != MP_OKAY)                 { goto error; }   /* Xr = s^2 - Xp - Xq */   if ((err = mp_sqr(&s, &tmp)) != MP_OKAY)                           { goto error; } /* tmp = s^2 mod modulus */   if ((err = mp_reduce(&tmp, modulus, mu)) != MP_OKAY)               { goto error; }   if ((err = mp_sub(&tmp, &P->x, &tmp)) != MP_OKAY)                  { goto error; } /* tmp = tmp - Px */   if ((err = mp_sub(&tmp, &Q->x, &tmpx)) != MP_OKAY)                 { goto error; } /* tmpx = tmp - Qx */   /* Yr = -Yp + s(Xp - Xr) */   if ((err = mp_sub(&P->x, &tmpx, &tmp)) != MP_OKAY)                 { goto error; } /* tmp = Px - tmpx */   if ((err = mp_mul(&tmp, &s, &tmp)) != MP_OKAY)                     { goto error; } /* tmp = tmp * s */   if ((err = mp_submod(&tmp, &P->y, modulus, &R->y)) != MP_OKAY)     { goto error; } /* Ry = tmp - Py mod modulus */   if ((err = mp_mod(&tmpx, modulus, &R->x)) != MP_OKAY)              { goto error; } /* Rx = tmpx mod modulus */   err = CRYPT_OK;   goto done;error:   err = mpi_to_ltc_error(err);done:   mp_clear_multi(&s, &tmpx, &tmp, NULL);   return err;}/* size of sliding window, don't change this! */#define WINSIZE 4/* perform R = kG where k == integer and G == ecc_point */static int ecc_mulmod(mp_int *k, ecc_point *G, ecc_point *R, mp_int *modulus){   ecc_point *tG, *M[8];   int        i, j, err;   mp_int     mu;   mp_digit   buf;   int        first, bitbuf, bitcpy, bitcnt, mode, digidx;  /* init barrett reduction */  if ((err = mp_init(&mu)) != MP_OKAY) {      return mpi_to_ltc_error(err);  }  if ((err = mp_reduce_setup(&mu, modulus)) != MP_OKAY) {      mp_clear(&mu);      return mpi_to_ltc_error(err);  }  /* alloc ram for window temps */  for (i = 0; i < 8; i++) {      M[i] = new_point();      if (M[i] == NULL) {         for (j = 0; j < i; j++) {             del_point(M[j]);         }         mp_clear(&mu);         return CRYPT_MEM;      }  }   /* make a copy of G incase R==G */   tG = new_point();   if (tG == NULL)                                                            { err = CRYPT_MEM; goto done; }   /* tG = G */   if ((err = mp_copy(&G->x, &tG->x)) != MP_OKAY)                             { goto error; }   if ((err = mp_copy(&G->y, &tG->y)) != MP_OKAY)                             { goto error; }      /* calc the M tab, which holds kG for k==8..15 */   /* M[0] == 8G */   if ((err = dbl_point(G, M[0], modulus, &mu)) != CRYPT_OK)                  { goto done; }   if ((err = dbl_point(M[0], M[0], modulus, &mu)) != CRYPT_OK)               { goto done; }   if ((err = dbl_point(M[0], M[0], modulus, &mu)) != CRYPT_OK)               { goto done; }   /* now find (8+k)G for k=1..7 */   for (j = 9; j < 16; j++) {       if ((err = add_point(M[j-9], G, M[j-8], modulus, &mu)) != CRYPT_OK)    { goto done; }   }   /* setup sliding window */   mode   = 0;   bitcnt = 1;   buf    = 0;   digidx = k->used - 1;   bitcpy = bitbuf = 0;   first  = 1;   /* perform ops */   for (;;) {     /* grab next digit as required */     if (--bitcnt == 0) {       if (digidx == -1) {          break;       }       buf = k->dp[digidx--];       bitcnt = (int) DIGIT_BIT;     }     /* grab the next msb from the multiplicand */     i = (buf >> (DIGIT_BIT - 1)) & 1;     buf <<= 1;     /* skip leading zero bits */     if (mode == 0 && i == 0) {        continue;     }     /* if the bit is zero and mode == 1 then we double */     if (mode == 1 && i == 0) {        if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK)                { goto done; }        continue;     }     /* else we add it to the window */     bitbuf |= (i << (WINSIZE - ++bitcpy));     mode = 2;     if (bitcpy == WINSIZE) {       /* if this is the first window we do a simple copy */       if (first == 1) {          /* R = kG [k = first window] */          if ((err = mp_copy(&M[bitbuf-8]->x, &R->x)) != MP_OKAY)             { goto error; }          if ((err = mp_copy(&M[bitbuf-8]->y, &R->y)) != MP_OKAY)             { goto error; }          first = 0;       } else {         /* normal window */         /* ok window is filled so double as required and add  */         /* double first */         for (j = 0; j < WINSIZE; j++) {           if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK)             { goto done; }         }         /* then add, bitbuf will be 8..15 [8..2^WINSIZE] guaranteed */         if ((err = add_point(R, M[bitbuf-8], R, modulus, &mu)) != CRYPT_OK)  { goto done; }       }       /* empty window and reset */       bitcpy = bitbuf = 0;       mode = 1;    }  }   /* if bits remain then double/add */   if (mode == 2 && bitcpy > 0) {     /* double then add */     for (j = 0; j < bitcpy; j++) {       /* only double if we have had at least one add first */       if (first == 0) {          if ((err = dbl_point(R, R, modulus, &mu)) != CRYPT_OK)               { goto done; }       }       bitbuf <<= 1;       if ((bitbuf & (1 << WINSIZE)) != 0) {         if (first == 1){            /* first add, so copy */            if ((err = mp_copy(&tG->x, &R->x)) != MP_OKAY)                     { goto error; }            if ((err = mp_copy(&tG->y, &R->y)) != MP_OKAY)                     { goto error; }            first = 0;         } else {            /* then add */

⌨️ 快捷键说明

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