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

version» Context lines:

pike.git/src/post_modules/Nettle/hogweed.cmod:1257:       /*! @decl string(7bit) name()    *!    *! Returns the name of the curve.    */    PIKEFUN string(7bit) name()    {    ref_push_string(MK_STRING("Curve25519"));    }    +  /*! @decl string(7bit) jose_name() +  *! +  *! Returns the name of the curve according to JOSE +  *! (@rfc{8037:3.1@}). +  *! +  *! @returns +  *! Returns the string @expr{"X25519"@}. +  *! +  *! @seealso +  *! @[name()] +  */ +  PIKEFUN string(7bit) jose_name() +  { +  ref_push_string(MK_STRING("X25519")); +  } +     /*! @decl int size()    *!    *! @returns    *! Returns the size in bits for a single coordinate on the curve.    */    PIKEFUN int size()    {    push_int(255);    }   
pike.git/src/post_modules/Nettle/hogweed.cmod:1439:    /*! @endclass Point    */       /*! @class EdDSA    *!    *! Edwards Curve Digital Signing Algorithm    */    PIKECLASS EdDSA    program_flags PROGRAM_USES_PARENT|PROGRAM_NEEDS_PARENT|PROGRAM_CLEAR_STORAGE;    { +  /*! @decl inherit Point +  *! +  *! This point represents the public key. +  */ +  EXTRA +  { +  /* Perform an inherit of the Point class that our parent contains. +  */ +  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); +  + #ifdef PIKE_DEBUG +  if (parent_Point_prog != Nettle_Curve25519_Point_program) { +  Pike_fatal("Unexpected Point program.\n"); +  } + #endif +  +  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); +  } +  } +  } +     /*! @decl inherit __builtin.Nettle.Sign    */    INHERIT "__builtin.Nettle.Sign";       PIKEVAR string(8bit) private_key flags ID_PRIVATE|ID_HIDDEN;    -  PIKEVAR string(8bit) public_key flags ID_PRIVATE|ID_HIDDEN; -  +     PIKEVAR function(int(0..):string(0..255)) random    flags ID_PROTECTED;       INIT    {    struct svalue *random =    simple_mapping_string_lookup(get_builtin_constants(), "random_string");    if(!random || (TYPEOF(*random) != T_FUNCTION))    Pike_error("Unable to resolve random function.\n");    assign_svalue(&THIS->random, random);
pike.git/src/post_modules/Nettle/hogweed.cmod:1468:       /*! @decl string(7bit) name()    *!    *! Returns the string @expr{"EdDSA"@}.    */    PIKEFUN string(7bit) name()    {    ref_push_string(MK_STRING("EdDSA"));    }    +  /*! @decl string(7bit) jose_name() +  *! +  *! Returns the string @expr{"Ed25519"@}. +  */ +  PIKEFUN string(7bit) jose_name() +  { +  ref_push_string(MK_STRING("Ed25519")); +  } +     /*! @decl Curve25519 get_curve()    *!    *! Get the elliptic curve that is in use.    */    PIKEFUN object(Nettle_Curve25519) get_curve()    {    struct external_variable_context loc;    loc.o = Pike_fp->current_object;    loc.inherit = Pike_fp->context;    find_external_context(&loc, 1);
pike.git/src/post_modules/Nettle/hogweed.cmod:1504:    /*! @decl void set_private_key(string(8bit) k)    *!    *! Set the private key (and corresponding public key).    *!    *! @note    *! Throws errors if the key isn't valid for the curve.    */    PIKEFUN void set_private_key(string(8bit) k)    {    struct pike_string *pub; +  struct Nettle_Curve25519_Point_struct *point;       if (k->len != ED25519_KEY_SIZE) Pike_error("Invalid private key.\n");       if (THIS->private_key) {    free_string(THIS->private_key);    }    copy_shared_string(THIS->private_key, k);       /* Set the corresponding public key, */    pub = begin_shared_string(ED25519_KEY_SIZE);       ed25519_sha512_public_key(STR0(pub), STR0(k));    -  if (THIS->public_key) { -  free_string(THIS->public_key); +  point = get_inherited_storage(1, Nettle_Curve25519_Point_program); +  if (point->point) { +  free_string(point->point);    } -  THIS->public_key = end_shared_string(pub); +  point->point = end_shared_string(pub);    }       /*! @decl string(8bit) get_x()    *!    *! Get the x coordinate of the public key.    */    PIKEFUN string(8bit) get_x()    { -  if (THIS->public_key) { -  ref_push_string(THIS->public_key); +  struct Nettle_Curve25519_Point_struct *point; +  point = get_inherited_storage(1, Nettle_Curve25519_Point_program); +  if (point->point) { +  ref_push_string(point->point);    } else {    push_undefined();    }    }       /*! @decl void set_public_key(string(8bit) x)    *!    *! Change to the selected point on the curve as public key.    *!    *! @note    *! Throws errors if the point isn't on the curve.    */    PIKEFUN void set_public_key(string(8bit) x)    { -  if (THIS->public_key == x) return; +  struct Nettle_Curve25519_Point_struct *point; +  point = get_inherited_storage(1, Nettle_Curve25519_Point_program); +  if (point->point == x) return;    if (x->len != ED25519_KEY_SIZE) Pike_error("Invalid key.\n"); -  if (THIS->public_key) { -  free_string(THIS->public_key); +  if (point->point) { +  free_string(point->point);    }    if (THIS->private_key) {    free_string(THIS->private_key);    THIS->private_key = NULL;    } -  copy_shared_string(THIS->public_key, x); +  copy_shared_string(point->point, x);    }       /*! @decl void set_random(function(int(0..):string(8bit)) r)    *!    *! Set the random function, used to generate keys and parameters,    *! to the function @[r].    */    PIKEFUN void set_random(function(int(0..):string(8bit)) r)    {    assign_svalue(&THIS->random, r);    }       /*! @decl int(0..1) raw_verify(string(8bit) message, string(8bit) signature)    *!    *! Verify the @[signature] against the @[message].    */    PIKEFUN int(0..1) raw_verify(string(8bit) message, string(8bit) signature)    { -  if (!THIS->public_key) Pike_error("No public key.\n"); +  struct Nettle_Curve25519_Point_struct *point; +  point = get_inherited_storage(1, Nettle_Curve25519_Point_program); +  if (!point->point) Pike_error("No public key.\n");       if (signature->len != ED25519_SIGNATURE_SIZE) {    push_int(0);    return;    }    -  push_int(ed25519_sha512_verify(STR0(THIS->public_key), +  push_int(ed25519_sha512_verify(STR0(point->point),    message->len, STR0(message),    STR0(signature)));    }       /*! @decl string(8bit) raw_sign(string(8bit) message)    *!    *! Sign the @[message].    */    PIKEFUN string(8bit) raw_sign(string(8bit) message)    {    struct pike_string *res; -  +  struct Nettle_Curve25519_Point_struct *point; +  point = get_inherited_storage(1, Nettle_Curve25519_Point_program);       if (!THIS->private_key) Pike_error("No private key.\n"); -  if (!THIS->public_key) Pike_error("No public key.\n"); +  if (!point->point) Pike_error("No public key.\n");       res = begin_shared_string(ED25519_SIGNATURE_SIZE);    -  ed25519_sha512_sign(STR0(THIS->public_key), STR0(THIS->private_key), +  ed25519_sha512_sign(STR0(point->point), STR0(THIS->private_key),    message->len, STR0(message),    STR0(res));       push_string(end_shared_string(res));    }       /*! @decl void generate_key()    *!    *! Generate a new set of private and public keys on the current curve.    */