1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
492
  
493
  
494
  
495
  
496
  
497
  
498
  
499
  
500
  
501
  
502
  
503
  
504
  
505
  
506
  
507
  
508
  
509
  
510
  
511
  
512
  
513
  
514
  
515
  
516
  
517
  
518
  
519
  
520
  
521
  
522
  
523
  
524
  
525
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
598
  
599
  
600
  
601
  
602
  
603
  
604
  
605
  
606
  
607
  
608
  
609
  
610
  
611
  
612
  
613
  
614
  
615
  
616
  
617
  
618
  
619
  
620
  
/* hash.cmod -*- c -*- */ 
/* $Id: hash.cmod,v 1.26 2004/10/16 07:27:29 agehall Exp $ */ 
 
#include "global.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
 
/* For this_object() */ 
#include "object.h" 
#include "module_support.h" 
 
#include "nettle_config.h" 
 
#ifdef HAVE_LIBNETTLE 
 
#include "nettle.h" 
 
#include <nettle/md5.h> 
#ifdef HAVE_NETTLE_MD4_INIT 
#include <nettle/md4.h> 
#include <nettle/md2.h> 
#endif 
#include <nettle/sha.h> 
#include <nettle/nettle-meta.h> 
 
#include <assert.h> 
#include <stdio.h> 
#include <stdarg.h> 
#include "fdlib.h" 
 
#if 0 
static void 
werror(const char *format, ...) 
{ 
  va_list args; 
 
  va_start(args, format); 
  vfprintf(stderr, format, args); 
  va_end(args); 
} 
#else 
#define werror(x) 
#endif 
 
static struct program *Fd_ref_program = NULL; 
 
/*! @module Nettle */ 
 
/*! @class HashInfo 
 *! 
 *! Represents information about a hash algorithm, such as 
 *! name, digest size, and internal block size. 
 */ 
PIKECLASS HashInfo 
{ 
  CVAR const struct nettle_hash *meta; 
 
  /*! @decl string name(void) 
   *! 
   *! Returns a human readable name for the algorithm. 
   */ 
  PIKEFUN string name() 
    optflags OPT_TRY_OPTIMIZE; 
    { 
      if (!THIS->meta) 
        Pike_error("HashInfo not properly initialized.\n"); 
 
      push_text(THIS->meta->name); 
    } 
 
  /*! @decl string digest_size(void) 
   *! 
   *! Returns the size of a hash digests. 
   */ 
  PIKEFUN int digest_size() 
    optflags OPT_TRY_OPTIMIZE; 
    { 
      if (!THIS->meta) 
        Pike_error("HashInfo not properly initialized.\n"); 
 
      push_int(THIS->meta->digest_size); 
    } 
 
  /*! @decl string block_size(void) 
   *! 
   *! Returns the internal block size of the hash algorithm. 
   */ 
  PIKEFUN int block_size() 
    optflags OPT_TRY_OPTIMIZE; 
    { 
      if (!THIS->meta) 
        Pike_error("HashInfo not properly initialized.\n"); 
 
      push_int(THIS->meta->block_size); 
    } 
 
 
  /*! @decl string hash(string data) 
   *! 
   *!  Works as a (faster) shortcut for 
   *!  @expr{obj->update(data)->digest()@}. 
   *! 
   *! @seealso 
   *!   @[HashState()->update()] and @[HashState()->digest()]. 
   */ 
  PIKEFUN string hash(string in) 
    optflags OPT_TRY_OPTIMIZE; 
  { 
    void *ctx; 
    struct pike_string *out; 
    unsigned digest_length; 
    const struct nettle_hash *meta = THIS->meta; 
 
    if (!meta) 
      Pike_error("HashInfo not properly initialized.\n"); 
    NO_WIDE_STRING(in); 
 
    ctx = (void *)alloca(meta->context_size); 
    if(!ctx) 
      SIMPLE_OUT_OF_MEMORY_ERROR("hash", meta->context_size); 
 
    THREADS_ALLOW(); 
    meta->init(ctx); 
    meta->update(ctx, in->len, in->str); 
 
    digest_length = meta->digest_size; 
    out = begin_shared_string(digest_length); 
    meta->digest(ctx, digest_length, out->str); 
    THREADS_DISALLOW(); 
 
    pop_n_elems(args); 
    push_string(end_shared_string(out)); 
  } 
 
  /*! @decl string hash(Stdio.File file, void|int bytes) 
   *! 
   *!  Works as a (faster) shortcut for 
   *!  @expr{obj->update(Stdio.read_file(file))->digest()@}. 
   *! 
   *! @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], @[HashState()->update()] and 
   *!   @[HashState()->digest()]. 
   */ 
  PIKEFUN string hash(object in, void|int bytes) 
    optflags OPT_EXTERNAL_DEPEND; 
    { 
    void *ctx; 
    int len, fd; 
    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"); 
 
    /* Verify that the input is a Stdio.Fd_ref */ 
    if (!Fd_ref_program) 
      { 
        push_text("files.Fd_ref"); 
        SAFE_APPLY_MASTER("resolv",1); 
        Fd_ref_program = program_from_svalue(Pike_sp-1); 
        if (!Fd_ref_program) { 
          pop_stack(); 
          Pike_error("Unable to resolv files.Fd_ref.\n"); 
        } 
        add_ref(Fd_ref_program); 
        pop_stack( ); 
      } 
 
    if (!get_storage(in, Fd_ref_program ) ) 
      Pike_error("Object not Fd_ref or subclass.\n"); 
 
    safe_apply(in, "query_fd", 0); 
    fd = Pike_sp[-1].u.integer; 
    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"); 
 
    ctx = (void *)alloca(meta->context_size); 
    if (!ctx) 
      SIMPLE_OUT_OF_MEMORY_ERROR("hash", meta->context_size); 
 
    read_buffer=(char *)malloc(8192); 
    if (!read_buffer) 
      SIMPLE_OUT_OF_MEMORY_ERROR("hash", 8192); 
 
    THREADS_ALLOW(); 
    meta->init(ctx); 
    if(args==2 && bytes->u.integer>-1) { 
      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) { 
        meta->update(ctx, len, read_buffer); 
        bytes_left -= read_bytes; 
        read_bytes = MINIMUM(8192, bytes_left); 
      } 
    } 
    else 
      while((len=fd_read(fd, read_buffer, 8192))>0) 
        meta->update(ctx, len, read_buffer); 
 
    free(read_buffer); 
 
    out = begin_shared_string(meta->digest_size); 
    meta->digest(ctx, meta->digest_size, out->str); 
    THREADS_DISALLOW(); 
 
    pop_n_elems(args); 
    push_string(end_shared_string(out)); 
  } 
 
  INIT 
    { 
      werror("HashInfo->INIT\n"); 
      THIS->meta = NULL; 
    } 
} 
 
/*! @endclass HashInfo */ 
 
#define GET_META(o) \ 
( ((struct HashInfo_struct *) get_storage((o), HashInfo_program)) \ 
  ->meta) 
 
/* The algorithm objects have to be implemented in pike. */ 
 
/*! @class HashState 
 *! 
 *! Base class for hashing contexts. 
 */ 
PIKECLASS HashState 
{ 
  INHERIT HashInfo; 
  CVAR void *ctx; 
 
  /* FIXME: Create should copy state from the other object, if 
   * provided. */ 
 
  /*! @decl HashState update(string data) 
   *! 
   *! Hashes more data. 
   */ 
  PIKEFUN object update(string data) 
    optflags OPT_SIDE_EFFECT; 
    { 
      void *ctx = THIS->ctx; 
      const struct nettle_hash *meta = 
        GET_META(Pike_fp->current_object); 
 
      if (!ctx || !meta) 
        Pike_error("HashState not properly initialized.\n"); 
 
      NO_WIDE_STRING(data); 
      THREADS_ALLOW(); 
      meta->update(ctx, data->len, data->str); 
      THREADS_DISALLOW(); 
 
      push_object(this_object()); 
    } 
 
  /*! @decl string digest(int|void length) 
   *! 
   *! Generates a digests, 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 digest(int|void arg) 
    { 
      const struct nettle_hash *meta; 
      struct pike_string *digest; 
      unsigned length; 
 
      if (! THIS->ctx) 
        Pike_error("HashState not properly initialized.\n"); 
 
      meta = GET_META(Pike_fp->current_object); 
      assert(meta); 
       
      if (!arg) 
        length = meta->digest_size; 
      else 
        { 
          if (arg->type != 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, digest->str); 
      push_string(end_shared_string(digest)); 
    } 
 
  INIT 
    { 
      werror("HashState->INIT\n"); 
      THIS->ctx = NULL; 
    } 
  EXIT 
    { 
      werror("HashState->EXIT\n"); 
      if (THIS->ctx && Pike_fp->current_object->prog) 
      { 
        const struct nettle_hash *meta = 
          GET_META(Pike_fp->current_object); 
        assert(meta); 
        memset(THIS->ctx, 0, meta->context_size); 
      } 
    } 
} 
 
/*! @endclass HashState */ 
 
/*! @class MD5_Info 
 *! 
 *! Internal mixin class, intended to be multiply inherited 
 *! together with HashInfo. */ 
 
PIKECLASS MD5_Info 
{ 
  INIT 
    { 
      struct HashInfo_struct *HashInfo 
        = (struct HashInfo_struct *) get_storage(Pike_fp->current_object, 
                                                 HashInfo_program); 
 
      werror("MD5_Info->INIT\n"); 
       
      if (HashInfo && !HashInfo->meta) 
        HashInfo->meta = &nettle_md5; 
      else { 
        /* Can't call Pike_error here.  
         * Pike_error("Can't initialize this object.\n"); */ 
        werror("MD5_Info->INIT failed\n"); 
      } 
    } 
} 
 
/*! @endclass MD5_Info */ 
 
/*! @class MD5_State 
 *! 
 *! State for MD5 hashing. 
 */ 
PIKECLASS MD5_State 
{ 
  INHERIT MD5_Info; 
  INHERIT HashState; 
  CVAR struct md5_ctx md5; 
 
  INIT 
    { 
      struct HashState_struct *instance 
        = (struct HashState_struct *) get_storage(Pike_fp->current_object, 
                                                  HashState_program); 
      werror("MD5_State->INIT\n"); 
 
      assert(instance); 
 
      md5_init(&THIS->md5);       
      instance->ctx = &THIS->md5; 
    } 
} 
/*! @endclass MD5_State */ 
 
#ifdef HAVE_NETTLE_MD4_INIT 
 
/*! @class MD4_Info 
 *! 
 *! Internal mixin class, intended to be multiply inherited 
 *! together with HashInfo. */ 
 
PIKECLASS MD4_Info 
{ 
  INIT 
    { 
      struct HashInfo_struct *HashInfo 
        = (struct HashInfo_struct *) get_storage(Pike_fp->current_object, 
                                                 HashInfo_program); 
 
      werror("MD4_Info->INIT\n"); 
       
      if (HashInfo && !HashInfo->meta) 
        HashInfo->meta = &nettle_md4; 
      else { 
        /* Can't call Pike_error here.  
         * Pike_error("Can't initialize this object.\n"); */ 
        werror("MD4_Info->INIT failed\n"); 
      } 
    } 
} 
 
/*! @endclass MD4_Info */ 
 
/*! @class MD4_State 
 *! 
 *! State for MD4 hashing. 
 */ 
PIKECLASS MD4_State 
{ 
  INHERIT MD4_Info; 
  INHERIT HashState; 
  CVAR struct md4_ctx md4; 
 
  INIT 
    { 
      struct HashState_struct *instance 
        = (struct HashState_struct *) get_storage(Pike_fp->current_object, 
                                                  HashState_program); 
      werror("MD4_State->INIT\n"); 
 
      assert(instance); 
 
      md4_init(&THIS->md4); 
      instance->ctx = &THIS->md4; 
    } 
} 
/*! @endclass MD4_State */ 
 
/*! @class MD2_Info 
 *! 
 *! Internal mixin class, intended to be multiply inherited 
 *! together with HashInfo. */ 
 
PIKECLASS MD2_Info 
{ 
  INIT 
    { 
      struct HashInfo_struct *HashInfo 
        = (struct HashInfo_struct *) get_storage(Pike_fp->current_object, 
                                                 HashInfo_program); 
 
      werror("MD2_Info->INIT\n"); 
       
      if (HashInfo && !HashInfo->meta) 
        HashInfo->meta = &nettle_md2; 
      else { 
        /* Can't call Pike_error here.  
         * Pike_error("Can't initialize this object.\n"); */ 
        werror("MD2_Info->INIT failed\n"); 
      } 
    } 
} 
 
/*! @endclass MD2_Info */ 
 
/*! @class MD2_State 
 *! 
 *! State for MD2 hashing. 
 */ 
PIKECLASS MD2_State 
{ 
  INHERIT MD2_Info; 
  INHERIT HashState; 
  CVAR struct md2_ctx md2; 
 
  INIT 
    { 
      struct HashState_struct *instance 
        = (struct HashState_struct *) get_storage(Pike_fp->current_object, 
                                                  HashState_program); 
      werror("MD2_State->INIT\n"); 
 
      assert(instance); 
 
      md2_init(&THIS->md2); 
      instance->ctx = &THIS->md2; 
    } 
} 
/*! @endclass MD2_State */ 
 
#endif /* HAVE_NETTLE_MD4_INIT */ 
 
/*! @class SHA1_Info 
 *! 
 *! Internal mixin class, intended to be multiply inherited 
 *! together with HashInfo. */ 
 
PIKECLASS SHA1_Info 
{ 
  INIT 
    { 
      struct HashInfo_struct *HashInfo 
        = (struct HashInfo_struct *) get_storage(Pike_fp->current_object, 
                                                 HashInfo_program); 
 
      werror("SHA1_Info->INIT\n"); 
       
      if (HashInfo && !HashInfo->meta) 
        HashInfo->meta = &nettle_sha1; 
      else { 
        /* Can't call Pike_error here.  
         * Pike_error("Can't initialize this object.\n"); */ 
        werror("SHA1_Info->INIT failed\n"); 
      } 
    } 
} 
 
/*! @endclass SHA1_Info */ 
 
/*! @class SHA1_State 
 *! 
 *! State for SHA1 hashing. 
 */ 
 
PIKECLASS SHA1_State 
{ 
  INHERIT SHA1_Info; 
  INHERIT HashState; 
  CVAR struct sha1_ctx sha1; 
 
  INIT 
    { 
      struct HashState_struct *instance 
        = (struct HashState_struct *) get_storage(Pike_fp->current_object, 
                                                  HashState_program); 
      werror("SHA1_State->INIT\n"); 
 
      assert(instance); 
 
      sha1_init(&THIS->sha1);       
      instance->ctx = &THIS->sha1; 
    } 
} 
/*! @endclass SHA1_State */ 
 
/*! @class SHA256_Info 
 *! 
 *! Internal mixin class, intended to be multiply inherited 
 *! together with HashInfo. */ 
 
PIKECLASS SHA256_Info 
{ 
  INIT 
    { 
      struct HashInfo_struct *HashInfo 
        = (struct HashInfo_struct *) get_storage(Pike_fp->current_object, 
                                                 HashInfo_program); 
 
      werror("SHA256_Info->INIT\n"); 
       
      if (HashInfo && !HashInfo->meta) 
        HashInfo->meta = &nettle_sha256; 
      else { 
        /* Can't call Pike_error here.  
         * Pike_error("Can't initialize this object.\n"); */ 
        werror("SHA256_Info->INIT failed\n"); 
      } 
    } 
} 
 
/*! @endclass SHA256_Info */ 
 
/*! @class SHA256_State 
 *! 
 *! State for SHA256 hashing. 
 */ 
 
PIKECLASS SHA256_State 
{ 
  INHERIT SHA256_Info; 
  INHERIT HashState; 
  CVAR struct sha256_ctx sha256; 
 
  INIT 
    { 
      struct HashState_struct *instance 
        = (struct HashState_struct *) get_storage(Pike_fp->current_object, 
                                                  HashState_program); 
      werror("SHA256_State->INIT\n"); 
 
      assert(instance); 
 
      sha256_init(&THIS->sha256); 
      instance->ctx = &THIS->sha256; 
    } 
} 
/*! @endclass SHA256_State */ 
 
/*! @endmodule Nettle */ 
 
void 
hash_init(void) 
{ 
  werror("Nettle, hash init\n"); 
  INIT; 
} 
 
void 
hash_exit(void) 
{ 
  werror("Nettle, hash exit\n"); 
  if (Fd_ref_program) { 
    free_program( Fd_ref_program ); 
  } 
  EXIT; 
} 
 
#endif /* HAVE_LIBNETTLE */