56c0642008-07-31Martin Stjernholm /* -*- c -*- || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
6d1a542003-03-13Niels Möller  #include "global.h" #include "interpret.h" #include "svalue.h"
602fe62004-02-21Martin Nilsson #include "threads.h"
6d1a542003-03-13Niels Möller  /* For this_object() */ #include "object.h" #include "module_support.h"
0a146b2013-03-12Arne Goedeke #include "pike_memory.h"
6d1a542003-03-13Niels Möller  #include "nettle_config.h" #ifdef HAVE_LIBNETTLE
56c0642008-07-31Martin Stjernholm DECLARATIONS
6d1a542003-03-13Niels Möller #include "nettle.h" #include <nettle/md5.h>
40f88a2003-08-01Martin Nilsson #include <nettle/md4.h>
5c36c82003-08-26Martin Nilsson #include <nettle/md2.h>
6d1a542003-03-13Niels Möller #include <nettle/sha.h>
c353b02013-11-14Martin Nilsson #ifdef HAVE_NETTLE_SHA3_H #include <nettle/sha3.h> #endif
dcb2802013-11-30Martin Nilsson #ifdef HAVE_NETTLE_RIPEMD160_H #include <nettle/ripemd160.h> #endif #ifdef HAVE_NETTLE_GOSTHASH94_H #include <nettle/gosthash94.h> #endif
6d1a542003-03-13Niels Möller 
5a20f92004-04-28Martin Nilsson #include "fdlib.h"
7a38822004-04-30Martin Nilsson 
411a282003-03-18Niels Möller /*! @module Nettle */
86a7272013-10-05Henrik Grubbström (Grubba) /*! @class Hash
cd26482003-03-18Niels Möller  *! *! Represents information about a hash algorithm, such as *! name, digest size, and internal block size. */
86a7272013-10-05Henrik Grubbström (Grubba) PIKECLASS Hash
6d1a542003-03-13Niels Möller {
21ab5d2013-10-05Henrik Grubbström (Grubba)  /*! @decl inherit __builtin.Nettle.Hash */ INHERIT "__builtin.Nettle.Hash";
6d1a542003-03-13Niels Möller  CVAR const struct nettle_hash *meta;
5345c92013-05-19Martin Nilsson  /*! @decl string(0..255) name(void)
cd26482003-03-18Niels Möller  *! *! Returns a human readable name for the algorithm. */
5345c92013-05-19Martin Nilsson  PIKEFUN string(0..255) name()
1a2b712004-02-14Martin Nilsson  optflags OPT_TRY_OPTIMIZE;
6d1a542003-03-13Niels Möller  { if (!THIS->meta)
86a7272013-10-05Henrik Grubbström (Grubba)  Pike_error("Hash not properly initialized.\n");
6d1a542003-03-13Niels Möller 
3f286d2003-11-29Martin Nilsson  push_text(THIS->meta->name);
6d1a542003-03-13Niels Möller  }
5345c92013-05-19Martin Nilsson  /*! @decl int(0..) digest_size(void)
cd26482003-03-18Niels Möller  *!
0473d52014-05-11Henrik Grubbström (Grubba)  *! Returns the size of a hash digest.
cd26482003-03-18Niels Möller  */
5345c92013-05-19Martin Nilsson  PIKEFUN int(0..) digest_size()
1a2b712004-02-14Martin Nilsson  optflags OPT_TRY_OPTIMIZE;
6d1a542003-03-13Niels Möller  { if (!THIS->meta)
86a7272013-10-05Henrik Grubbström (Grubba)  Pike_error("Hash not properly initialized.\n");
6d1a542003-03-13Niels Möller  push_int(THIS->meta->digest_size); }
5345c92013-05-19Martin Nilsson  /*! @decl int(0..) block_size(void)
cd26482003-03-18Niels Möller  *!
f1c9f42003-08-05Martin Nilsson  *! Returns the internal block size of the hash algorithm.
cd26482003-03-18Niels Möller  */
5345c92013-05-19Martin Nilsson  PIKEFUN int(0..) block_size()
1a2b712004-02-14Martin Nilsson  optflags OPT_TRY_OPTIMIZE;
6d1a542003-03-13Niels Möller  { if (!THIS->meta)
86a7272013-10-05Henrik Grubbström (Grubba)  Pike_error("Hash not properly initialized.\n");
6d1a542003-03-13Niels Möller  push_int(THIS->meta->block_size); }
9512252004-04-28Martin Nilsson 
f6a6ad2013-12-08Henrik Grubbström (Grubba)  /*! @decl string(0..255) hash(string(0..255) data)
9512252004-04-28Martin Nilsson  *! *! Works as a (faster) shortcut for
86a7272013-10-05Henrik Grubbström (Grubba)  *! @expr{State()->update(data)->digest()@}, where State is *! the hash state class corresponding to this Hash.
9512252004-04-28Martin Nilsson  *! *! @seealso
86a7272013-10-05Henrik Grubbström (Grubba)  *! @[State()->update()] and @[State()->digest()].
9512252004-04-28Martin Nilsson  */
f6a6ad2013-12-08Henrik Grubbström (Grubba)  PIKEFUN string(0..255) hash(string(0..255) in)
9512252004-04-28Martin Nilsson  optflags OPT_TRY_OPTIMIZE; { void *ctx; struct pike_string *out; unsigned digest_length;
5a20f92004-04-28Martin Nilsson  const struct nettle_hash *meta = THIS->meta;
9512252004-04-28Martin Nilsson 
5a20f92004-04-28Martin Nilsson  if (!meta)
86a7272013-10-05Henrik Grubbström (Grubba)  Pike_error("Hash not properly initialized.\n");
9512252004-04-28Martin Nilsson  NO_WIDE_STRING(in);
dc8d022014-04-27Martin Nilsson  ctx = alloca(meta->context_size);
9512252004-04-28Martin Nilsson  if(!ctx)
8a3e562004-05-19Martin Nilsson  SIMPLE_OUT_OF_MEMORY_ERROR("hash", meta->context_size);
9512252004-04-28Martin Nilsson 
e8be7c2010-11-14Jonas Walldén  /* Only thread this block for significant data size */
0473d52014-05-11Henrik Grubbström (Grubba)  if (in->len > HASH_THREADS_ALLOW_THRESHOLD) {
e8be7c2010-11-14Jonas Walldén  THREADS_ALLOW(); meta->init(ctx); meta->update(ctx, in->len, (const uint8_t *)in->str); THREADS_DISALLOW(); } else { meta->init(ctx); meta->update(ctx, in->len, (const uint8_t *)in->str); }
9512252004-04-28Martin Nilsson 
5a20f92004-04-28Martin Nilsson  digest_length = meta->digest_size;
9512252004-04-28Martin Nilsson  out = begin_shared_string(digest_length);
20e2ef2005-12-12Martin Nilsson  meta->digest(ctx, digest_length, (uint8_t *)out->str);
5a20f92004-04-28Martin Nilsson  pop_n_elems(args); push_string(end_shared_string(out)); }
f0aa6f2013-06-09Martin Nilsson  int is_stdio_file(struct object *o) { struct program *p = o->prog; INT32 i = p->num_inherits; while( i-- ) { if( p->inherits[i].prog->id == PROG_STDIO_FD_ID || p->inherits[i].prog->id == PROG_STDIO_FD_REF_ID ) return 1; } return 0; }
7d0cc62014-12-02Per Hedbor  /*! @decl string(0..255) hash(Stdio.File|Stdio.Buffer|String.Buffer|System.Memory source, void|int bytes)
5a20f92004-04-28Martin Nilsson  *!
7d0cc62014-12-02Per Hedbor  *! Works as a (faster) shortcut for e.g.
86a7272013-10-05Henrik Grubbström (Grubba)  *! @expr{State()->update(Stdio.read_file(file))->digest()@}, *! where State is the hash state class corresponding to this *! Hash.
5a20f92004-04-28Martin Nilsson  *!
7a38822004-04-30Martin Nilsson  *! @param bytes *! The number of bytes of the file object @[file] that should be *! hashed. Negative numbers are ignored and the whole file is *! hashed. *!
5a20f92004-04-28Martin Nilsson  *! @seealso
86a7272013-10-05Henrik Grubbström (Grubba)  *! @[Stdio.File], @[State()->update()] and *! @[State()->digest()].
5a20f92004-04-28Martin Nilsson  */
5345c92013-05-19Martin Nilsson  PIKEFUN string(0..255) hash(object in, void|int bytes)
5a20f92004-04-28Martin Nilsson  optflags OPT_EXTERNAL_DEPEND; { void *ctx;
7d0cc62014-12-02Per Hedbor  size_t len; int fd;
5a20f92004-04-28Martin Nilsson  char *read_buffer; PIKE_STAT_T st; struct pike_string *out; const struct nettle_hash *meta = THIS->meta; if (!meta) Pike_error("HashInfo not properly initialized.\n");
7d0cc62014-12-02Per Hedbor  ctx = alloca(meta->context_size); if (!ctx) SIMPLE_OUT_OF_MEMORY_ERROR("hash", meta->context_size); meta->init(ctx); if( get_memory_object_memory(in, (void **)&read_buffer, &len, NULL) ) { if( bytes ) len = MINIMUM(len, MAXIMUM(bytes->u.integer,0) ); meta->update( ctx, len, (const uint8_t*)read_buffer); goto ret_meta; }
f0aa6f2013-06-09Martin Nilsson  if (!is_stdio_file(in))
7d0cc62014-12-02Per Hedbor  Pike_error("Object not Fd or Fd_ref, or subclass or 8bit buffer.\n");
5a20f92004-04-28Martin Nilsson 
0e235b2013-06-09Martin Nilsson  apply(in, "query_fd", 0);
fe11b92004-04-29Martin Nilsson  fd = Pike_sp[-1].u.integer;
5a20f92004-04-28Martin Nilsson  pop_stack(); if (fd_fstat(fd, &st)<0) Pike_error("File not found!\n"); if (!S_ISREG(st.st_mode)) Pike_error("Non-regular file.\n");
dc8d022014-04-27Martin Nilsson  read_buffer=xalloc(8192);
337f332004-04-28Martin Nilsson 
5a20f92004-04-28Martin Nilsson  THREADS_ALLOW();
c79cf72015-04-17Martin Nilsson  if(bytes && bytes->u.integer>-1) {
7a38822004-04-30Martin Nilsson  int bytes_left = bytes->u.integer; int read_bytes = MINIMUM(8192, bytes_left); while(read_bytes>0 && (len=fd_read(fd, read_buffer, read_bytes))>0) {
20e2ef2005-12-12Martin Nilsson  meta->update(ctx, len, (const uint8_t *)read_buffer);
7a38822004-04-30Martin Nilsson  bytes_left -= read_bytes; read_bytes = MINIMUM(8192, bytes_left); } } else while((len=fd_read(fd, read_buffer, 8192))>0)
20e2ef2005-12-12Martin Nilsson  meta->update(ctx, len, (const uint8_t *)read_buffer);
337f332004-04-28Martin Nilsson  free(read_buffer);
5a20f92004-04-28Martin Nilsson 
d51d792010-06-21Martin Stjernholm  THREADS_DISALLOW();
7d0cc62014-12-02Per Hedbor  ret_meta:
5a20f92004-04-28Martin Nilsson  out = begin_shared_string(meta->digest_size);
20e2ef2005-12-12Martin Nilsson  meta->digest(ctx, meta->digest_size, (uint8_t *)out->str);
9512252004-04-28Martin Nilsson  pop_n_elems(args); push_string(end_shared_string(out)); }
7940a82013-03-07Henrik Grubbström (Grubba)  /* NOTE: This is NOT the MIME base64 table! */
f6a6ad2013-12-08Henrik Grubbström (Grubba)  static const char b64tab[64] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
7940a82013-03-07Henrik Grubbström (Grubba)  static inline void b64enc(char *dest, int a, int b, int c, int sz) { unsigned int bitbuf = a | (b << 8) | (c << 16); while (sz--) { *(dest++) = b64tab[bitbuf & 63]; bitbuf >>= 6; } }
f6a6ad2013-12-08Henrik Grubbström (Grubba)  /*! @decl string(0..127) crypt_hash(string(0..255) password, @ *! string(0..255) salt, int rounds)
7940a82013-03-07Henrik Grubbström (Grubba)  *! *! Password hashing function in @[crypt_md5()]-style. *! *! Implements the algorithm described in *! @url{http://www.akkadia.org/drepper/SHA-crypt.txt@}. *! *! This is the algorithm used by @tt{crypt(2)@} in *! methods @tt{$5$@} (SHA256) and @tt{$6$@} (SHA512). *!
6580032013-08-15Martin Nilsson  *! The @[password] memory will be cleared before released. *!
7940a82013-03-07Henrik Grubbström (Grubba)  *! @seealso *! @[crypt_md5()] */
f6a6ad2013-12-08Henrik Grubbström (Grubba)  PIKEFUN string(0..127) crypt_hash(string(0..255) password, string(0..255) salt, int rounds)
7940a82013-03-07Henrik Grubbström (Grubba)  { struct pike_string *res; const struct nettle_hash *meta = THIS->meta; void *ctx; uint8_t *abcbuf; uint8_t *dpbuf; uint8_t *dsbuf;
5345c92013-05-19Martin Nilsson  unsigned char *p; unsigned char *s;
7940a82013-03-07Henrik Grubbström (Grubba)  int plen; int slen; int dsz = meta->digest_size; int i; int r; int a, b, c; if (!rounds) rounds = 5000; if (rounds < 1000) rounds = 1000; if (rounds > 999999999) rounds = 999999999; NO_WIDE_STRING(password); NO_WIDE_STRING(salt);
bb64932013-08-09Arne Goedeke  password->flags |= STRING_CLEAR_ON_EXIT;
dc8d022014-04-27Martin Nilsson  ctx = alloca(meta->context_size);
7940a82013-03-07Henrik Grubbström (Grubba)  if (!ctx) SIMPLE_OUT_OF_MEMORY_ERROR("crypt_hash", meta->context_size);
dc8d022014-04-27Martin Nilsson  abcbuf = alloca(meta->digest_size * 3);
7940a82013-03-07Henrik Grubbström (Grubba)  if (!abcbuf) SIMPLE_OUT_OF_MEMORY_ERROR("crypt_hash", meta->digest_size * 3); dpbuf = abcbuf + meta->digest_size; dsbuf = dpbuf + meta->digest_size; /* NB: We use these to allow the compiler to * avoid dereferencing at every step. */
5345c92013-05-19Martin Nilsson  p = (unsigned char*)password->str;
7940a82013-03-07Henrik Grubbström (Grubba)  plen = password->len;
5345c92013-05-19Martin Nilsson  s = (unsigned char*)salt->str;
7940a82013-03-07Henrik Grubbström (Grubba)  slen = salt->len; if (slen > 16) slen = 16; dsz = meta->digest_size; /* NB: We allocate the result here to avoid throwing away all the work * on out of memory at the end. */ if (dsz == 32) { /* 4 * (30/3) + 3 */ res = begin_shared_string(43); } else if (dsz == 64) { /* 4 * (63/3) + 2 */ res = begin_shared_string(86); } else { Pike_error("crypt_hash() not supported for this digest size yet (%d).\n", dsz); } THREADS_ALLOW(); /* NB: Comments refer to http://www.akkadia.org/drepper/SHA-crypt.txt */ meta->init(ctx); /* 4 */ meta->update(ctx, plen, p); /* 5 */ meta->update(ctx, slen, s); /* 6 */ meta->update(ctx, plen, p); /* 7 */ meta->digest(ctx, dsz, abcbuf); /* 8 */
00aadd2015-04-17Martin Nilsson  /* 1 */
7940a82013-03-07Henrik Grubbström (Grubba)  meta->update(ctx, plen, p); /* 2 */ meta->update(ctx, slen, s); /* 3 */ for (i = 0; i + dsz < plen; i += dsz) { /* 9 */ meta->update(ctx, dsz, abcbuf); } meta->update(ctx, plen - i, abcbuf); /* 10 */ for (i = 1; i < plen; i <<= 1) { /* 11 */ if (plen & i) { meta->update(ctx, dsz, abcbuf); } else { meta->update(ctx, plen, p); } } meta->digest(ctx, dsz, abcbuf); /* 12 */
00aadd2015-04-17Martin Nilsson  /* 13 */
7940a82013-03-07Henrik Grubbström (Grubba)  for (i = 0; i < plen; i++) { /* 14 */ meta->update(ctx, plen, p); } meta->digest(ctx, dsz, dpbuf); /* 15 */ /* Sequence P is implicit. */ /* 16 */
00aadd2015-04-17Martin Nilsson  /* 17 */
7940a82013-03-07Henrik Grubbström (Grubba)  for(i = 0; i < 16 + abcbuf[0]; i++) { /* 18 */ meta->update(ctx, slen, s); } meta->digest(ctx, dsz, dsbuf); /* 19 */ /* Sequence S is implicit. */ /* 20 */ for (r = 0; r < rounds; r++) { /* 21 */
00aadd2015-04-17Martin Nilsson  /* a */
7940a82013-03-07Henrik Grubbström (Grubba)  if (r & 1) { /* b */ for (i = 0; i + dsz < plen; i += dsz) { meta->update(ctx, dsz, dpbuf); } meta->update(ctx, plen - i, dpbuf); } else { meta->update(ctx, dsz, abcbuf); /* c */ }
6bcd952015-05-01Martin Nilsson  if (r % 3) /* d */ meta->update(ctx, slen, dsbuf);
7940a82013-03-07Henrik Grubbström (Grubba)  if (r % 7) { /* e */ for (i = 0; i + dsz < plen; i += dsz) { meta->update(ctx, dsz, dpbuf); } meta->update(ctx, plen - i, dpbuf); } if (r & 1) { /* f */ meta->update(ctx, dsz, abcbuf); } else { /* g */ for (i = 0; i + dsz < plen; i += dsz) { meta->update(ctx, dsz, dpbuf); } meta->update(ctx, plen - i, dpbuf); } meta->digest(ctx, dsz, abcbuf); /* h */ } THREADS_DISALLOW(); /* And now time for some pointless shuffling of the result. * Note that the shuffling is slightly different between * the two cases. * * This is followed by a custom base64-style encoding. */ c = 0; b = dsz/3; a = 2*b; if (dsz == 32) { for (i = 0, r = 0; i + 3 < dsz; i+=3, r+=4) { int t; b64enc(res->str + r, abcbuf[a], abcbuf[b], abcbuf[c], 4); t = a+1; a = b+1; b = c+1; c = t; } b64enc(res->str + r, abcbuf[30], abcbuf[31], 0, 3); } else { for (i = 0, r = 0; i + 3 < dsz; i+=3, r+=4) { int t; b64enc(res->str + r, abcbuf[a], abcbuf[b], abcbuf[c], 4); t = a+1; a = c+1; c = b+1; b = t; } b64enc(res->str + r, abcbuf[63], 0, 0, 2); } push_string(end_shared_string(res)); /* 22e */ /* Clean intermediate values. */
21b12a2014-09-03Martin Nilsson  memset(ctx, 0, meta->context_size); memset(abcbuf, 0, 3*dsz);
7940a82013-03-07Henrik Grubbström (Grubba)  }
6d1a542003-03-13Niels Möller  INIT {
86a7272013-10-05Henrik Grubbström (Grubba)  werror("Hash->INIT\n");
6d1a542003-03-13Niels Möller  THIS->meta = NULL; }
86a7272013-10-05Henrik Grubbström (Grubba)  /*! @class State *! *! Base class for hashing contexts. */ PIKECLASS State
9e336b2014-06-19Henrik Grubbström (Grubba)  program_flags PROGRAM_USES_PARENT|PROGRAM_NEEDS_PARENT|PROGRAM_CLEAR_STORAGE;
86a7272013-10-05Henrik Grubbström (Grubba)  {
4561b62013-10-05Henrik Grubbström (Grubba)  DOCSTART() @decl inherit Hash::State DOCEND()
411a282003-03-18Niels Möller 
86a7272013-10-05Henrik Grubbström (Grubba)  EXTRA { /* Perform an inherit of the State class (if any) that our parent * may contain via its inherit of __builtin.Nettle.Hash. */ struct program *parent_prog = Pike_compiler->previous->new_program; struct object *parent_obj = Pike_compiler->previous->fake_object; int parent_State_fun_num = really_low_find_shared_string_identifier(MK_STRING("State"), parent_prog, SEE_PROTECTED|SEE_PRIVATE); if (parent_State_fun_num >= 0) { struct program *parent_State_prog = low_program_from_function(parent_obj, parent_State_fun_num); if (parent_State_prog) {
27b78a2014-10-06Henrik Grubbström (Grubba)  parent_State_fun_num = really_low_reference_inherited_identifier(Pike_compiler->previous, 0, parent_State_fun_num);
86a7272013-10-05Henrik Grubbström (Grubba)  low_inherit(parent_State_prog, 0,
27b78a2014-10-06Henrik Grubbström (Grubba)  parent_State_fun_num,
86a7272013-10-05Henrik Grubbström (Grubba)  1 + 42, 0, NULL); } } }
6d1a542003-03-13Niels Möller 
86a7272013-10-05Henrik Grubbström (Grubba) #define GET_META(o) \
13e2ba2014-04-26Henrik Grubbström (Grubba)  ( ((struct Nettle_Hash_struct *)parent_storage(1, Nettle_Hash_program))->meta )
6d1a542003-03-13Niels Möller 
86a7272013-10-05Henrik Grubbström (Grubba)  CVAR void *ctx;
6d1a542003-03-13Niels Möller 
86a7272013-10-05Henrik Grubbström (Grubba)  /* FIXME: Create should copy state from the other object, if * provided. */
6d1a542003-03-13Niels Möller 
86a7272013-10-05Henrik Grubbström (Grubba)  /*! @decl State update(string(0..255) data) *! *! Hashes more data. *! *! @returns *! Returns @expr{this@} in order to simplify chaining *! of function calls. */ PIKEFUN object update(string(0..255) data) optflags OPT_SIDE_EFFECT;
895c722014-03-29Henrik Grubbström (Grubba)  rawtype tFunc(tStr8, tObjImpl_NETTLE_HASH_STATE);
6d1a542003-03-13Niels Möller  {
766bc82004-10-16Marcus Agehall  void *ctx = THIS->ctx;
9119632003-05-07Henrik Grubbström (Grubba)  const struct nettle_hash *meta = GET_META(Pike_fp->current_object);
6d1a542003-03-13Niels Möller 
03123d2004-02-21Martin Nilsson  if (!ctx || !meta)
86a7272013-10-05Henrik Grubbström (Grubba)  Pike_error("State not properly initialized.\n");
6d1a542003-03-13Niels Möller  NO_WIDE_STRING(data);
13670c2015-05-25Martin Nilsson 
e8be7c2010-11-14Jonas Walldén  /* Only thread this block for significant data size */
0473d52014-05-11Henrik Grubbström (Grubba)  if (data->len > HASH_THREADS_ALLOW_THRESHOLD) {
e8be7c2010-11-14Jonas Walldén  THREADS_ALLOW(); meta->update(ctx, data->len, (const uint8_t *)data->str); THREADS_DISALLOW(); } else { meta->update(ctx, data->len, (const uint8_t *)data->str); }
6d1a542003-03-13Niels Möller  push_object(this_object()); }
86a7272013-10-05Henrik Grubbström (Grubba)  /*! @decl string(0..255) digest(int|void length) *! *! Generates a digest, and resets the hashing contents. *! *! @param length *! If the length argument is provided, the digest is truncated *! to the given length. *! *! @returns *! The digest. */
5345c92013-05-19Martin Nilsson  PIKEFUN string(0..255) digest(int|void arg)
6d1a542003-03-13Niels Möller  { const struct nettle_hash *meta; struct pike_string *digest; unsigned length;
9119632003-05-07Henrik Grubbström (Grubba)  meta = GET_META(Pike_fp->current_object);
86a7272013-10-05Henrik Grubbström (Grubba)  if (!THIS->ctx || !meta) Pike_error("State not properly initialized.\n");
6d1a542003-03-13Niels Möller  if (!arg) length = meta->digest_size; else {
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(*arg) != PIKE_T_INT)
f1c9f42003-08-05Martin Nilsson  Pike_error("Bad argument type.\n"); if (arg->u.integer < 0) Pike_error("Invalid length, must be positive.\n");
61e14b2004-01-23Martin Nilsson  if ((unsigned)arg->u.integer > meta->digest_size)
f1c9f42003-08-05Martin Nilsson  Pike_error("Unsupported digest length.\n");
6d1a542003-03-13Niels Möller  length = arg->u.integer; }
13670c2015-05-25Martin Nilsson 
6d1a542003-03-13Niels Möller  digest = begin_shared_string(length);
20e2ef2005-12-12Martin Nilsson  meta->digest(THIS->ctx, length, (uint8_t *)digest->str);
6d1a542003-03-13Niels Möller  push_string(end_shared_string(digest)); }
ac14652003-07-29Martin Nilsson 
86a7272013-10-05Henrik Grubbström (Grubba)  INIT
6d1a542003-03-13Niels Möller  {
86a7272013-10-05Henrik Grubbström (Grubba)  werror("State->INIT\n");
6d1a542003-03-13Niels Möller  THIS->ctx = NULL; }
22038d2008-05-30Martin Nilsson 
86a7272013-10-05Henrik Grubbström (Grubba)  EXIT
6d1a542003-03-13Niels Möller  {
86a7272013-10-05Henrik Grubbström (Grubba)  werror("State->EXIT\n"); if (THIS->ctx)
6d1a542003-03-13Niels Möller  {
9119632003-05-07Henrik Grubbström (Grubba)  const struct nettle_hash *meta = GET_META(Pike_fp->current_object);
86a7272013-10-05Henrik Grubbström (Grubba)  if (meta) { memset(THIS->ctx, 0, meta->context_size); }
6d1a542003-03-13Niels Möller  } }
86a7272013-10-05Henrik Grubbström (Grubba)  } /*! @endclass State */
6d1a542003-03-13Niels Möller }
86a7272013-10-05Henrik Grubbström (Grubba) /*! @endclass Hash */ /* The algorithm objects can be overloaded in pike. */
411a282003-03-18Niels Möller 
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define TOSTR(DEF) #DEF
6d1a542003-03-13Niels Möller 
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define PIKE_NAME MD5 #cmod_define NETTLE_NAME md5 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
6d1a542003-03-13Niels Möller 
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define PIKE_NAME MD4 #cmod_define NETTLE_NAME md4 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
5c36c82003-08-26Martin Nilsson 
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define PIKE_NAME MD2 #cmod_define NETTLE_NAME md2 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
5c36c82003-08-26Martin Nilsson 
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define PIKE_NAME SHA1 #cmod_define NETTLE_NAME sha1 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
52e8792013-11-27Arne Goedeke #ifdef HAVE_NETTLE_SHA224_INIT
9f7d272013-11-19Martin Nilsson #cmod_define PIKE_NAME SHA224 #cmod_define NETTLE_NAME sha224 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
52e8792013-11-27Arne Goedeke #endif
88064b2011-12-20Henrik Grubbström (Grubba) #cmod_define PIKE_NAME SHA256 #cmod_define NETTLE_NAME sha256 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
0980772003-07-29Martin Nilsson 
1ebab82011-12-20Henrik Grubbström (Grubba) #ifdef SHA384_DIGEST_SIZE #cmod_define PIKE_NAME SHA384 #cmod_define NETTLE_NAME sha384 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif /* SHA384_DIGEST_SIZE */ #ifdef SHA512_DIGEST_SIZE #cmod_define PIKE_NAME SHA512 #cmod_define NETTLE_NAME sha512 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif /* SHA512_DIGEST_SIZE */
3c29c62015-07-23Martin Nilsson #ifdef SHA512_224_DIGEST_SIZE #cmod_define PIKE_NAME SHA512_224 #cmod_define NETTLE_NAME sha512_224 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif /* SHA512_224_DIGEST_SIZE */ #ifdef SHA512_256_DIGEST_SIZE #cmod_define PIKE_NAME SHA512_256 #cmod_define NETTLE_NAME sha512_256 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif /* SHA512_256_DIGEST_SIZE */
4cd0d12016-01-29Henrik Grubbström (Grubba) /* NB: SHA3 in Nettle 3.2 and later does not interoperate * with earlier versions of Nettle. */
d90a322016-01-29Martin Nilsson #ifdef NETTLE_SHA3_FIPS202
c353b02013-11-14Martin Nilsson  #cmod_define PIKE_NAME SHA3_224 #cmod_define NETTLE_NAME sha3_224 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #cmod_define PIKE_NAME SHA3_256 #cmod_define NETTLE_NAME sha3_256 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #cmod_define PIKE_NAME SHA3_384 #cmod_define NETTLE_NAME sha3_384 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #cmod_define PIKE_NAME SHA3_512 #cmod_define NETTLE_NAME sha3_512 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME
d90a322016-01-29Martin Nilsson #endif /* NETTLE_SHA3_FIPS202 */
c353b02013-11-14Martin Nilsson 
dcb2802013-11-30Martin Nilsson #ifdef HAVE_NETTLE_RIPEMD160_H #cmod_define PIKE_NAME RIPEMD160 #cmod_define NETTLE_NAME ripemd160 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif #ifdef HAVE_NETTLE_GOSTHASH94_H #cmod_define PIKE_NAME GOST94 #cmod_define NETTLE_NAME gosthash94 #cmod_include "hash.H" #cmod_undef PIKE_NAME #cmod_undef NETTLE_NAME #endif
411a282003-03-18Niels Möller /*! @endmodule Nettle */
6d1a542003-03-13Niels Möller  void hash_init(void) { werror("Nettle, hash init\n"); INIT; } void hash_exit(void) { werror("Nettle, hash exit\n"); EXIT; } #endif /* HAVE_LIBNETTLE */