Browse Source

Merge pull request #402 from libtom/pr/fix-ecc_set_key

fix ecc_set_key - no check of private key input buffer size
karel-m 7 years ago
parent
commit
d11a1a7f06
1 changed files with 3 additions and 4 deletions
  1. 3 4
      src/pk/ecc/ecc_set_key.c

+ 3 - 4
src/pk/ecc/ecc_set_key.c

@@ -24,24 +24,22 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key
    a     = key->dp.A;
    b     = key->dp.B;
 
-   if (type == PK_PRIVATE && inlen <= (unsigned long)key->dp.size) {
+   if (type == PK_PRIVATE) {
       /* load private key */
       if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)in, inlen)) != CRYPT_OK) {
          goto error;
       }
-      if (mp_iszero(key->k)) {
+      if (mp_iszero(key->k) || (mp_cmp(key->k, key->dp.order) != LTC_MP_LT)) {
          err = CRYPT_INVALID_PACKET;
          goto error;
       }
       /* compute public key */
       if ((err = ltc_mp.ecc_ptmul(key->k, &key->dp.base, &key->pubkey, a, prime, 1)) != CRYPT_OK)         { goto error; }
-      key->type = type;
    }
    else if (type == PK_PUBLIC) {
       /* load public key */
       if ((err = ltc_ecc_import_point(in, inlen, prime, a, b, key->pubkey.x, key->pubkey.y)) != CRYPT_OK) { goto error; }
       if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK)                                                   { goto error; }
-      key->type = type;
    }
    else {
       err = CRYPT_INVALID_PACKET;
@@ -53,6 +51,7 @@ int ecc_set_key(const unsigned char *in, unsigned long inlen, int type, ecc_key
       goto error;
    }
 
+   key->type = type;
    return CRYPT_OK;
 
 error: