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

version» Context lines:

pike.git/src/post_modules/Nettle/hash.cmod:1:   /* -*- 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.   */    - #include "global.h" + #include "module.h"   #include "interpret.h" - #include "svalue.h" +    #include "threads.h" -  - /* For this_object() */ - #include "object.h" +    #include "module_support.h"   #include "pike_compiler.h" - #include "pike_memory.h" +       #include "nettle_config.h"      #ifdef HAVE_LIBNETTLE      DECLARATIONS      #include "nettle.h"      #include <nettle/md5.h>
pike.git/src/post_modules/Nettle/hash.cmod:53:    *! name, digest size, and internal block size.    */   PIKECLASS Hash   {    /*! @decl inherit __builtin.Nettle.Hash    */    INHERIT "__builtin.Nettle.Hash";       CVAR const struct nettle_hash *meta;    -  /*! @decl string(0..255) name(void) +  /*! @decl string(0..255) name()    *!    *! Returns a human readable name for the algorithm.    */    PIKEFUN string(0..255) name()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta)    Pike_error("Hash not properly initialized.\n");       push_text(THIS->meta->name);    }    -  /*! @decl int(0..) digest_size(void) +  /*! @decl int(0..) digest_size()    *!    *! Returns the size of a hash digest.    */    PIKEFUN int(0..) digest_size()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta)    Pike_error("Hash not properly initialized.\n");       push_int(THIS->meta->digest_size);    }    -  /*! @decl int(0..) block_size(void) +  /*! @decl int(0..) block_size()    *!    *! Returns the internal block size of the hash algorithm.    */    PIKEFUN int(0..) block_size()    optflags OPT_TRY_OPTIMIZE;    {    if (!THIS->meta)    Pike_error("Hash not properly initialized.\n");       push_int(THIS->meta->block_size);
pike.git/src/post_modules/Nettle/hash.cmod:150:    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;    }    -  /*! @decl string(0..255) hash(Stdio.File|Stdio.Buffer|String.Buffer|System.Memory source, void|int bytes) +  /*! @decl string(0..255) hash(Stdio.File|Stdio.Buffer|String.Buffer|System.Memory source, void|int(0..) bytes)    *!    *! Works as a (faster) shortcut for e.g.    *! @expr{State()->update(Stdio.read_file(file))->digest()@},    *! where State is the hash state class corresponding to this    *! Hash.    *!    *! @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.    *!    *! @seealso    *! @[Stdio.File], @[State()->update()] and    *! @[State()->digest()].    */ -  PIKEFUN string(0..255) hash(object in, void|int bytes) +  PIKEFUN string(0..255) hash(object in, void|int(0..) bytes)    optflags OPT_EXTERNAL_DEPEND;    {    void *ctx;    size_t len;    int fd;    char *read_buffer;    PIKE_STAT_T st;    struct pike_string *out;    const struct nettle_hash *meta = THIS->meta;   
pike.git/src/post_modules/Nettle/hash.cmod:259:    *! 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).    *!    *! The @[password] memory will be cleared before released.    *! +  *! Rounds will never be set to less than 1000. If @[rounds] is 0 +  *! it will be set to 5000. +  *!    *! @seealso    *! @[crypt_md5()]    */ -  PIKEFUN string(0..127) crypt_hash(string(0..255) password, -  string(0..255) salt, int rounds) +  PIKEFUN string(0..127) crypt_hash(string password, +  string salt, int rounds) +  /* NB: We use a weaker type above to allow us to delay +  * throwing errors on wide strings until we've had +  * time to censor the password string. +  */ +  rawtype tFunc(tStr8 tStr8 tInt, tStr7);    {    struct pike_string *res;    const struct nettle_hash *meta = THIS->meta;    void *ctx;    uint8_t *abcbuf;    uint8_t *dpbuf;    uint8_t *dsbuf;       unsigned char *p;    unsigned char *s;
pike.git/src/post_modules/Nettle/hash.cmod:290:       int a, b, c;       if (!meta)    Pike_error("HashInfo not properly initialized.\n");       if (!rounds) rounds = 5000;    if (rounds < 1000) rounds = 1000;    if (rounds > 999999999) rounds = 999999999;    +  /* Censor the password. */ +  push_string(password); +  args++; +  add_ref(Pike_sp[-args].u.string = MK_STRING("censored")); +     NO_WIDE_STRING(password);    NO_WIDE_STRING(salt);       password->flags |= STRING_CLEAR_ON_EXIT;       ctx = alloca(meta->context_size);    if (!ctx)    SIMPLE_OUT_OF_MEMORY_ERROR("crypt_hash", meta->context_size);       abcbuf = alloca(meta->digest_size * 3);
pike.git/src/post_modules/Nettle/hash.cmod:512:    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);    }       push_object(this_object());    }    -  /*! @decl string(0..255) digest(int|void length) +  /*! @decl string(0..255) digest(int(0..)|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.    */ -  PIKEFUN string(0..255) digest(int|void arg) +  PIKEFUN string(0..255) digest(int(0..)|void arg)    {    const struct nettle_hash *meta;    struct pike_string *digest;    unsigned length;       meta = GET_META(Pike_fp->current_object);       if (!THIS->ctx || !meta)    Pike_error("State not properly initialized.\n");       if (!arg)    length = meta->digest_size;    else    { -  if (TYPEOF(*arg) != PIKE_T_INT) -  Pike_error("Bad argument type.\n"); +     if (arg->u.integer < 0)    Pike_error("Invalid length, must be positive.\n");    if ((unsigned)arg->u.integer > meta->digest_size)    Pike_error("Unsupported digest length.\n");       length = arg->u.integer;    }       digest = begin_shared_string(length);    meta->digest(THIS->ctx, length, (uint8_t *)digest->str);