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

version» Context lines:

pike.git/src/post_modules/Nettle/nettle.cmod:1:   /* nettle.cmod -*- c -*- */      #include "global.h" - RCSID("$Id: nettle.cmod,v 1.24 2003/12/12 21:53:42 nilsson Exp $"); + RCSID("$Id: nettle.cmod,v 1.25 2003/12/14 00:08:57 nilsson Exp $");   #include "interpret.h"   #include "svalue.h"   /* For this_object() */   #include "object.h"   #include "module_support.h"      #include "nettle_config.h"      #ifdef HAVE_LIBNETTLE   
pike.git/src/post_modules/Nettle/nettle.cmod:150:    Pike_error("This random generator has no sources.\n");    if( source<0 || source>=THIS->ctx.nsources )    Pike_error("Invalid random source.\n");    if( entropy<0 )    Pike_error("Entropy must be positive.\n");    if( entropy>(data->len*8) )    Pike_error("Impossibly large entropy value.\n");    RETURN yarrow256_update(&THIS->ctx, source, entropy, data->len, data->str);    }    +  /*! @decl int(0..) needed_sources() +  *! The number of sources that must reach the threshold before a +  *! slow reseed will happen. +  */    PIKEFUN int(0..) needed_sources()    {    RETURN yarrow256_needed_sources(&THIS->ctx);    }       /*! @decl string random_string(int length)    *! Returns a pseudo-random string of the requested @[length].    */    PIKEFUN string random_string(int length)    {
pike.git/src/post_modules/Nettle/nettle.cmod:274:    if(missing) {    free_object(obj);    Pike_error("Object is missing identifier \"%s\"\n", missing);    }       return obj;   }         /*! @class CBC -  *! Implementation of the cipher block chaining mode (CBC). +  *! Implementation of the cipher block chaining mode (CBC). Works as +  *! a wrapper for the cipher algorithm put in create.    */   PIKECLASS CBC   {    CVAR struct object *object;    CVAR unsigned INT8 *iv;    CVAR INT32 block_size;    CVAR INT32 mode;       INIT    {
pike.git/src/post_modules/Nettle/nettle.cmod:354:       pop_stack();    MEMCPY(THIS->iv, source, block_size);    }       static void cbc_free_this_object() {    if(THIS->object) free_object(THIS->object);    }       /*! @decl void create(program|object|function cipher, mixed ... args) +  *! Initialize the CBC wrapper with a cipher algorithm. If it is a +  *! program, an object will be instantiated with @[args] as arguments. +  *! If it is an object that doesn't conform to the cipher API, but has +  *! an @[LFUN::`()], that LFUN will be called. If it is a function, +  *! that function will be called with @[args] as arguments.    */    PIKEFUN void create(program|object|function cipher, mixed ... more) {    THIS->object = make_cipher_object(args);       safe_apply(THIS->object, "block_size", 0);       if(Pike_sp[-1].type != T_INT)    Pike_error("block_size() didn't return an int.\n");    THIS->block_size = Pike_sp[-1].u.integer;   
pike.git/src/post_modules/Nettle/nettle.cmod:375:       if ((!THIS->block_size) ||    (THIS->block_size > 4096))    Pike_error("Bad block size %d.\n", THIS->block_size);       THIS->iv = (unsigned INT8 *)xalloc(THIS->block_size);    MEMSET(THIS->iv, 0, THIS->block_size);    }       /*! @decl string name() +  *! Returns the string @expr{"CBC"@}.    */    PIKEFUN string name() {    push_constant_text("CBC");    }       /*! @decl int block_size() -  +  *! Reurns the block size of the encapsulated cipher.    */    PIKEFUN int block_size() {    RETURN THIS->block_size;    }       /*! @decl int key_size() -  +  *! Returns the key size of the encapsulated cipher.    */    PIKEFUN int key_size() {    safe_apply(THIS->object, "key_size", args);    }    -  /*! @decl void set_encrypt_key(string key) +  /*! @decl this_program set_encrypt_key(string key) +  *! Prepare the cipher and the wrapper for encrypting +  *! with the given @[key].    */    PIKEFUN object set_encrypt_key(string key) {    assert(THIS->block_size); -  NO_WIDE_STRING(key); +     THIS->mode = 0;    safe_apply(THIS->object, "set_encrypt_key", args);    pop_stack();    RETURN this_object();    }    -  /*! @decl void set_decrypt_key(string key) +  /*! @decl this_program set_decrypt_key(string key) +  *! Prepare the cipher and the wrapper for decrypting +  *! with the given @[key].    */    PIKEFUN object set_decrypt_key(string key) {    f_CBC_set_encrypt_key(args);    THIS->mode = 1;    }    -  +  /*! @decl this_program set_iv(string iv) +  *! Set the initialization vector to @[iv]. +  */    PIKEFUN object set_iv(string iv) {    assert(THIS->iv);    NO_WIDE_STRING(iv);    if(iv->len != THIS->block_size)    Pike_error("Argument incompatible with cipher block size.\n");    MEMCPY(THIS->iv, iv->str, THIS->block_size);    RETURN this_object();    }    -  +  /*! @decl string crypt(string data) +  *! Encrypt/decrypt @[data] and return the result. @[data] must +  *! be an integral number of blocks. +  */    PIKEFUN string crypt(string data) {    unsigned INT8 *result;    INT32 offset = 0;       NO_WIDE_STRING(data);       if(data->len % THIS->block_size)    Pike_error("Data length not multiple of block size.\n");    if(!(result = alloca(data->len)))    Pike_error("Out of memory.\n");