pike.git / src / post_modules / Nettle / hogweed.cmod

version» Context lines:

pike.git/src/post_modules/Nettle/hogweed.cmod:568:   #endif    }       /*! @decl Gmp.mpz new_scalar(function(int(0..):string(8bit)) rnd)    *!    *! @param rnd    *! Randomness function to use as source.    *!    *! @returns    *! Returns a random scalar suitable to use as an @[ECDSA] private key -  *! or as an ECDH exponent. +  *! or as an ECDH secret factor.    */    PIKEFUN object(Gmp.mpz) new_scalar(function(int(0..):string(8bit)) rnd)    {    struct ecc_scalar s;    struct object *ret;       if (!THIS->curve) Pike_error("No curve defined.\n");       ecc_scalar_init(&s, THIS->curve);   
pike.git/src/post_modules/Nettle/hogweed.cmod:860:    }       /*! @decl void set(object(Gmp.mpz)|int x, object(Gmp.mpz)|int y)    *!    *! Change to the selected point on the curve.    *!    *! @note    *! Throws errors if the point isn't on the curve.    */    PIKEFUN void set(object(Gmp.mpz)|int x, object(Gmp.mpz)|int y) +  flags ID_VARIANT;    {    convert_svalue_to_bignum(x);    convert_svalue_to_bignum(y);    if (!ecc_point_set(&THIS->point,    (mpz_srcptr)x->u.object->storage,    (mpz_srcptr)y->u.object->storage)) {    SIMPLE_ARG_ERROR("set", 1, "Invalid point on curve.");    }    }   
pike.git/src/post_modules/Nettle/hogweed.cmod:1220:    *!    *! @note    *! The API for this curve differs somewhat from the API    *! used by the other @[Curve]s.    *!    *! @seealso    *! @[Curve], @rfc{7748@}    */   PIKECLASS Curve25519   { +  /*! @decl inherit __builtin.Nettle.ECC_Curve +  */ +  INHERIT "__builtin.Nettle.ECC_Curve"; +     DECLARE_STORAGE;       /*! @decl string(7bit) name()    *!    *! Returns the name of the curve.    */    PIKEFUN string(7bit) name()    {    ref_push_string(MK_STRING("Curve25519"));    }
pike.git/src/post_modules/Nettle/hogweed.cmod:1256:    *! @returns    *! Returns a random scalar suitable to use as an @[ECDSA] private key    *! or as an ECDH exponent.    */    PIKEFUN string(8bit) new_scalar(function(int(0..):string(8bit)) rnd)    {    push_int(CURVE25519_SIZE);    apply_svalue(rnd, 1);    }    -  /*! @decl string(8bit) `*(string(8bit) scalar) +  /*! @decl Point `*(string(8bit) scalar)    *!    *! Multiply the curve by a scalar.    *!    *! This can be used to get the public key from a private key.    *!    *! @returns    *! Returns a new point on the curve.    */    PIKEFUN string(8bit) `*(string(8bit) scalar)    {    struct pike_string *res;       if (scalar->len != CURVE25519_SIZE) Pike_error("Invalid scalar.\n");       res = begin_shared_string(CURVE25519_SIZE);       curve25519_mul_g(STR0(res), STR0(scalar));       push_string(end_shared_string(res)); -  +  +  apply_current(Nettle_Curve25519_Point_program_fun_num, 1);    }       /*! @decl string(8bit) point_mul(string(8bit) x, string(8bit) scalar)    *!    *! Multiply a point on the curve by a scalar.    *!    *! A typical use is for Elliptic Curve Diffie Hellman (ECDH) key exchange.    *!    *! @returns    *! Returns the new point on the curve.
pike.git/src/post_modules/Nettle/hogweed.cmod:1301:    if (x->len != CURVE25519_SIZE) Pike_error("Invalid x.\n");    if (scalar->len != CURVE25519_SIZE) Pike_error("Invalid scalar.\n");       res = begin_shared_string(CURVE25519_SIZE);       curve25519_mul(STR0(res), STR0(scalar), STR0(x));       push_string(end_shared_string(res));    }    +  /*! @class Point +  *! +  *! A point on an elliptic curve. +  */ +  PIKECLASS Point +  program_flags PROGRAM_USES_PARENT|PROGRAM_NEEDS_PARENT|PROGRAM_CLEAR_STORAGE; +  { +  PIKEVAR string(8bit) point flags ID_PRIVATE|ID_PROTECTED|ID_HIDDEN; +  +  /*! @decl inherit ECC_Curve::Point +  */ +  EXTRA +  { +  /* Perform an inherit of the Point class that our parent +  * contains via its inherit of __builtin.Nettle.ECC_Curve. +  */ +  struct program *parent_prog = Pike_compiler->previous->new_program; +  struct object *parent_obj = Pike_compiler->previous->fake_object; +  int parent_Point_fun_num = +  really_low_find_shared_string_identifier(MK_STRING("Point"), +  parent_prog, +  SEE_PROTECTED|SEE_PRIVATE); +  if (parent_Point_fun_num >= 0) { +  struct program *parent_Point_prog = +  low_program_from_function(parent_obj, parent_Point_fun_num); +  if (parent_Point_prog) { +  parent_Point_fun_num = +  really_low_reference_inherited_identifier(Pike_compiler->previous, +  0, parent_Point_fun_num); +  low_inherit(parent_Point_prog, 0, +  parent_Point_fun_num, +  1 + 42, 0, NULL); +  } +  } +  } +  +  PIKEFUN void set(string(8bit) x, string(8bit)|void y) +  flags ID_VARIANT; +  { +  if (x->len != CURVE25519_SIZE) { +  Pike_error("Invalid x.\n"); +  } +  if (y && y->len) { +  Pike_error("Invalid y.\n"); +  } +  if (THIS->point) { +  free_string(THIS->point); +  } +  add_ref(THIS->point = x); +  } +  +  /* NB: Little-endian byte-order! */ +  const p_wchar0 curve25519_scalar_one[CURVE25519_SIZE] = { +  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +  }; +  +  PIKEFUN string(8bit) get_x() +  { +  if (!THIS->point) { +  /* Default to 'g'. */ +  struct pike_string *res; +  res = begin_shared_string(CURVE25519_SIZE); +  curve25519_mul_g(STR0(res), curve25519_scalar_one); +  THIS->point = end_shared_string(res); +  } +  ref_push_string(THIS->point); +  } +  +  PIKEFUN string(8bit) get_y() +  { +  push_text(""); +  } +  +  PIKEFUN void create(string(8bit)|Stdio_Buffer data) +  flags ID_PROTECTED|ID_VARIANT; +  { +  if (TYPEOF(*data) == PIKE_T_OBJECT) { +  apply(data->u.object, "read", 0); +  } +  apply_current(f_Nettle_Curve25519_Point_set_fun_num, 1); +  } +  +  PIKEFUN Nettle_Curve25519_Point `*(string(8bit) scalar) +  flags ID_PROTECTED; +  { +  struct pike_string *res; +  if (scalar->len != CURVE25519_SIZE) Pike_error("Invalid scalar.\n"); +  res = begin_shared_string(CURVE25519_SIZE); +  if (THIS->point) { +  curve25519_mul(STR0(res), STR0(scalar), STR0(THIS->point)); +  } else { +  curve25519_mul_g(STR0(res), STR0(scalar)); +  } +  push_string(end_shared_string(res)); +  +  apply_external(1, Nettle_Curve25519_Point_program_fun_num, 1); +  } +  } +  /*! @endclass Point +  */ +     /*! @class EdDSA    *!    *! Edwards Curve Digital Signing Algorithm    */    PIKECLASS EdDSA    program_flags PROGRAM_USES_PARENT|PROGRAM_NEEDS_PARENT|PROGRAM_CLEAR_STORAGE;    {    /*! @decl inherit __builtin.Nettle.Sign    */    INHERIT "__builtin.Nettle.Sign";