88064b2011-12-20Henrik Grubbström (Grubba) /* -*- C -*- * * Cmod header-file acting as a template for the hash classes. */
86a7272013-10-05Henrik Grubbström (Grubba) DOCSTART() @class PIKE_NAME *! *! Implementation of the PIKE_NAME hash algorithm.
88064b2011-12-20Henrik Grubbström (Grubba)  *! DOCEND()
86a7272013-10-05Henrik Grubbström (Grubba) PIKECLASS PIKE_NAME
88064b2011-12-20Henrik Grubbström (Grubba) {
86a7272013-10-05Henrik Grubbström (Grubba)  DOCSTART() @decl inherit Hash DOCEND()
2194f92018-11-04Marcus Comstedt  INHERIT Hash;
86a7272013-10-05Henrik Grubbström (Grubba) 
88064b2011-12-20Henrik Grubbström (Grubba)  INIT {
895c722014-03-29Henrik Grubbström (Grubba)  struct Nettle_Hash_struct *hash; ASSIGN_CURRENT_STORAGE(hash, struct Nettle_Hash_struct, 1, Nettle_Hash_program);
86a7272013-10-05Henrik Grubbström (Grubba)  werror(cmod_STRFY_EVAL(PIKE_NAME) "->INIT\n");
69e2632013-10-20Henrik Grubbström (Grubba)  hash->meta = &cmod_CONCAT_EVAL(nettle_, NETTLE_NAME);
88064b2011-12-20Henrik Grubbström (Grubba)  }
5a1e2e2017-07-19Martin Nilsson #ifdef SPECIAL_PBKDF2
38a7eb2017-12-11Martin Nilsson  PIKEFUN string(8bit) pbkdf2(string(8bit) password, string(8bit) salt, int rounds, int bytes)
5a1e2e2017-07-19Martin Nilsson  { struct pike_string *dst = begin_shared_string(bytes); NO_WIDE_STRING(password); NO_WIDE_STRING(salt); cmod_CONCAT_EVAL(pbkdf2_hmac_,NETTLE_NAME)(password->len, (const uint8_t *)password->str, rounds, salt->len, (const uint8_t *)salt->str, bytes, (uint8_t *)dst->str); push_string(end_shared_string(dst)); } #endif
d19e9c2017-08-01Martin Nilsson #ifdef HAVE_NETTLE_HMAC_H
1739ba2017-12-09Henrik Grubbström (Grubba)  DOCSTART() @module HMAC *! *! Accellerated implementation of HMAC (Hashing for Message Authenticity *! Control) with the PIKE_NAME hash algorithm. *! *! @seealso *! @[Crypto.HMAC] DOCEND()
d19e9c2017-08-01Martin Nilsson  PIKECLASS _HMAC flags ID_PROTECTED; { EXTRA {
f35ab02017-08-02Martin Nilsson  lexical_inherit(1, MK_STRING("_HMAC"), 0, REPORT_ERROR);
d19e9c2017-08-01Martin Nilsson  }
1739ba2017-12-09Henrik Grubbström (Grubba)  DOCSTART() @class PIKE_NAME *! The HMAC hash state. DOCEND()
d19e9c2017-08-01Martin Nilsson  PIKECLASS State { CVAR struct HMAC_CTX(struct cmod_CONCAT_EVAL(NETTLE_NAME, _ctx)) ctx; CVAR const struct nettle_hash *meta;
c47cec2017-09-21Henrik Grubbström (Grubba)  static int cmod_CONCAT_EVAL(f_Hash__HMAC_, NETTLE_NAME, _create_fun_num) = -1;
d19e9c2017-08-01Martin Nilsson  EXTRA {
c47cec2017-09-21Henrik Grubbström (Grubba)  int hmac_create_fun_num = -1;
f35ab02017-08-02Martin Nilsson  lexical_inherit(1, MK_STRING("State"), 0, REPORT_ERROR);
c47cec2017-09-21Henrik Grubbström (Grubba)  hmac_create_fun_num = FIND_LFUN(Pike_compiler->new_program->inherits[1].prog, LFUN_CREATE); if (hmac_create_fun_num >= 0) { cmod_CONCAT_EVAL(f_Hash__HMAC_, NETTLE_NAME, _create_fun_num) = really_low_reference_inherited_identifier(NULL, 1, hmac_create_fun_num); }
d19e9c2017-08-01Martin Nilsson  }
1739ba2017-12-09Henrik Grubbström (Grubba)  DOCSTART() @decl void create(string(8bit) passwd, void|int b) *! @param passwd *! The secret password (K). *! *! @param b *! Block size. Must @expr{0@} (zero) or equal to the @[block_size()]. DOCEND()
d19e9c2017-08-01Martin Nilsson  PIKEFUN void create(string(8bit) passwd, void|int b) { const struct nettle_hash *meta = THIS->meta = ((struct Nettle_Hash_struct *)parent_storage(2, Nettle_Hash_program))->meta; NO_WIDE_STRING(passwd); HMAC_SET_KEY(&THIS->ctx, meta, passwd->len, (const uint8_t *)passwd->str);
c47cec2017-09-21Henrik Grubbström (Grubba)  if (cmod_CONCAT_EVAL(f_Hash__HMAC_, NETTLE_NAME, _create_fun_num) != -1) { /* Pass along the password to the inherited pike-level code. */ apply_current(cmod_CONCAT_EVAL(f_Hash__HMAC_, NETTLE_NAME, _create_fun_num), args); }
d19e9c2017-08-01Martin Nilsson  } PIKEFUN string(8bit) ()(string(8bit) text) { struct cmod_CONCAT_EVAL(NETTLE_NAME, _ctx) state; int bytes = THIS->meta->digest_size; struct pike_string *dst = begin_shared_string(bytes);
4c15af2017-08-01Martin Nilsson  NO_WIDE_STRING(text);
d19e9c2017-08-01Martin Nilsson  memcpy(&state, &THIS->ctx.inner, THIS->meta->context_size); THIS->meta->update(&state, text->len, (const uint8_t *)text->str); hmac_digest(&THIS->ctx.outer, &THIS->ctx.inner, &state, THIS->meta, THIS->meta->digest_size, (uint8_t *)dst->str); push_string(end_shared_string(dst)); } PIKEFUN object update(string(8bit) data) optflags OPT_SIDE_EFFECT; rawtype tFunc(tStr8, tObjImpl_NETTLE_HASH_STATE); {
4c15af2017-08-01Martin Nilsson  NO_WIDE_STRING(data);
d19e9c2017-08-01Martin Nilsson  THIS->meta->update(&THIS->ctx.state, data->len, (const uint8_t *)data->str); push_object(this_object()); } PIKEFUN object init(string(8bit)|void data) optflags OPT_SIDE_EFFECT; rawtype tFunc(tOr(tVoid, tStr8), tObjImpl_NETTLE_HASH_STATE); { memcpy(&THIS->ctx.state, &THIS->ctx.inner, THIS->meta->context_size); if(data)
4c15af2017-08-01Martin Nilsson  { NO_WIDE_STRING(data);
d19e9c2017-08-01Martin Nilsson  THIS->meta->update(&THIS->ctx.state, data->len, (const uint8_t *)data->str);
4c15af2017-08-01Martin Nilsson  }
d19e9c2017-08-01Martin Nilsson  push_object(this_object()); } PIKEFUN string(8bit) digest(int(0..)|void length) { int bytes = THIS->meta->digest_size; struct pike_string *dst; if(length) bytes = MINIMUM(bytes, length->u.integer); dst = begin_shared_string(bytes); hmac_digest(&THIS->ctx.outer, &THIS->ctx.inner, &THIS->ctx.state, THIS->meta, bytes, (uint8_t *)dst->str); push_string(end_shared_string(dst)); }
4c15af2017-08-01Martin Nilsson  PIKEFUN string(8bit) digest_info(string(8bit) text) { struct external_variable_context loc; apply_current(cmod_CONCAT_EVAL(f_Nettle_,PIKE_NAME,_cq__HMAC_State_update_fun_num),1); /* Basically apply_external() with find_identifier() added. */ loc.o = Pike_fp->current_object; loc.parent_identifier = Pike_fp->fun; if (loc.o->prog) {
e780fb2017-09-07Henrik Grubbström (Grubba)  int id;
4c15af2017-08-01Martin Nilsson  loc.inherit = INHERIT_FROM_INT(loc.o->prog, loc.parent_identifier); find_external_context(&loc, 2);
e780fb2017-09-07Henrik Grubbström (Grubba)  id = find_identifier("pkcs_digest", loc.o->prog);
4c15af2017-08-01Martin Nilsson  if( id<0 ) Pike_error("Could not find pkcs_digest.\n"); apply_low(loc.o, id + loc.inherit->identifier_level, 1); } else Pike_error("Apply on parent of destructed object.\n"); }
d19e9c2017-08-01Martin Nilsson  }
f7cea12017-12-13Stephen R. van den Berg  DOCSTART() @endclass PIKE_NAME
1739ba2017-12-09Henrik Grubbström (Grubba)  DOCEND()
d19e9c2017-08-01Martin Nilsson  } #endif
1739ba2017-12-09Henrik Grubbström (Grubba)  DOCSTART() @endmodule HMAC DOCEND()
d19e9c2017-08-01Martin Nilsson 
86a7272013-10-05Henrik Grubbström (Grubba)  DOCSTART() @class State *! *! State for PIKE_NAME hashing. *! DOCEND() PIKECLASS State program_flags PROGRAM_NEEDS_PARENT|PROGRAM_USES_PARENT; {
4561b62013-10-05Henrik Grubbström (Grubba)  DOCSTART() @decl inherit Hash::State DOCEND()
86a7272013-10-05Henrik Grubbström (Grubba)  EXTRA {
0bb0ef2013-10-19Henrik Grubbström (Grubba)  /* Perform an inherit of the Hash.State class that our parent * contains via its inherit of Hash.
86a7272013-10-05Henrik Grubbström (Grubba)  */
f35ab02017-08-02Martin Nilsson  lexical_inherit(1, MK_STRING("State"), 0, REPORT_ERROR);
86a7272013-10-05Henrik Grubbström (Grubba)  }
88064b2011-12-20Henrik Grubbström (Grubba)  CVAR struct cmod_CONCAT_EVAL(NETTLE_NAME, _ctx) NETTLE_NAME; INIT {
895c722014-03-29Henrik Grubbström (Grubba)  struct Nettle_Hash_State_struct *instance; ASSIGN_CURRENT_STORAGE(instance, struct Nettle_Hash_State_struct, 1, Nettle_Hash_State_program);
88064b2011-12-20Henrik Grubbström (Grubba) 
e48f762017-09-07Henrik Grubbström (Grubba)  cmod_CONCAT_EVAL(NETTLE_NAME, _init)(&THIS->NETTLE_NAME);
88064b2011-12-20Henrik Grubbström (Grubba)  instance->ctx = &THIS->NETTLE_NAME; }
86a7272013-10-05Henrik Grubbström (Grubba)  } DOCSTART() @endclass State DOCEND()
88064b2011-12-20Henrik Grubbström (Grubba) }
86a7272013-10-05Henrik Grubbström (Grubba) DOCSTART() @endclass PIKE_NAME
88064b2011-12-20Henrik Grubbström (Grubba) DOCEND()
86a7272013-10-05Henrik Grubbström (Grubba)