File: cipher.c

package info (click to toggle)
nessus-libraries 1.0.10-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 9,536 kB
  • ctags: 12,585
  • sloc: ansic: 72,626; asm: 25,921; sh: 19,570; makefile: 1,974; cpp: 560; pascal: 536; yacc: 234; lex: 203; lisp: 186; perl: 76; fortran: 24
file content (953 lines) | stat: -rw-r--r-- 22,836 bytes parent folder | download
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
 *          Copyright (c) mjh-EDV Beratung, 1996-1999
 *     mjh-EDV Beratung - 63263 Neu-Isenburg - Rosenstrasse 12
 *          Tel +49 6102 328279 - Fax +49 6102 328278
 *                Email info@mjh.teddy-net.com
 *
 *   Author: Jordan Hrycaj <jordan@mjh.teddy-net.com>
 *
 *   $Id: cipher.c,v 1.2 2000/08/28 12:08:16 jordan Exp $
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License for more details.
 */

#include "common-stuff.h"

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#ifdef HAVE_CTYPE_H
# include <ctype.h>
#endif

#ifdef HAVE_ASSERT_H
#include <assert.h>
#else
#define assert(x)
#endif

#ifdef USE_PTHREADS
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#endif

#include "us_export_wizard.h"
#include "cipher/cipher.h"
#include "cipher/raw-port.h"

#ifdef CIPHER_3DES
#include "cipher/des.h"
#endif

#ifdef CIPHER_BLOWFISH
#include "cipher/blowfish.h"
#endif

#ifdef CIPHER_TWOFISH
#include "cipher/twofish.h"
#endif

#ifdef MD5_FRAME
#include "cipher/md5.h"
#endif

#ifdef RMD_FRAME
#include "cipher/rmd.h"
#endif

#ifdef SHA_FRAME
#include "cipher/sha1.h"
#endif

/* ------------------------------------------------------------------------- *
 *                    private definitions                                    *
 * ------------------------------------------------------------------------- */

#define DUMP_MUTEX

/* some code optimazation */
#ifndef CIPHER_3DES
# undef EMULATE_DES24KEY_BY_DES16KEY
#endif
#ifndef CIPHER_BLOWFISH
# undef EMULATE_BF20KEY_BY_BF16KEY
#endif
#if !defined (EMULATE_DES24KEY_BY_DES16KEY) &&\
    !defined (EMULATE_BF20KEY_BY_BF16KEY)
# undef EMULATE_LONG_KEY_HASH
#endif

#ifdef EMULATE_LONG_KEY_HASH 
# if KEYLENGTH_MAX < 16 /* sanity check */
#  error Somebody set KEYLENGTH_MAX < 16, this is not supported
# endif
# if KEYLENGTH_MAX > 40 /* sanity check */
#  error Somebody set KEYLENGTH_MAX > 40, this is not supported
# endif
#else
# undef EMULATE_DES24KEY_BY_DES16KEY
# undef EMULATE_BF20KEY_BY_BF16KEY
#endif

/* ------------------------------------------------------------------------- *
 *                 private class descriptors                                 *
 * ------------------------------------------------------------------------- */

typedef struct _cipher_class { 
  const char *name ;

  unsigned keybits ;		/* key length in bits */
  unsigned keylen ;		/* key length in bytes */
  unsigned blocklen ;		/* block length n bytes */
  unsigned contextlen ;		/* size of internal decripror */
  char  map16to_long_key ;	/* for 3des and bf160 */

  /* cipher functions working on a context: encryption */
  int (*set_enkey) (void *, unsigned char *key, unsigned len);
  void  (*encrypt) (void *, unsigned char *out, unsigned char *in);

  /* cipher functions working on a context: decryption */
  int (*set_dekey) (void *, unsigned char *key, unsigned len);
  void  (*decrypt) (void *, unsigned char *out, unsigned char *in);

} cipher_class ;



typedef struct _frame_class { 

  const char *name ;
  unsigned contextlen ;		/* size of internal decripror */

  crc_fns crc ;			/* crc prototypes */

  /* functions library returns that length of a message digest */
  unsigned mdlen ;		/* length of digest */

  /* functions library to be used by crc () */
  void           (*init) (void *);
  void          (*write) (void *, unsigned char *buf, unsigned n);
  void          (*final) (void *);
  unsigned char* (*read) (void *);
  
} frame_class ;


/* ------------------------------------------------------------------------- *
 *                 private class variables                                   *
 * ------------------------------------------------------------------------- */
static  frame_class  *frame_list;
static     unsigned   frame_list_dim = 0 ;

static cipher_class *cipher_list;
static     unsigned  cipher_list_dim = 0 ;

/* ------------------------------------------------------------------------- *
 *                    private helpers                                        *
 * ------------------------------------------------------------------------- */

/* 
 * The inverse of context:
 * the (cipher_desc*) relative to its sub-entry context
 */

#define cipher_txetnoc(d)        \
  ((cipher_desc*)(((char*)(d)) - \
     ((char*)(((cipher_desc*)0)->context) - (char*)((cipher_desc*)0))))

/* 
 * The inverse of context:
 * the (frame_desc*) relative to its sub-entry context
 */
#define frame_txetnoc(d)        \
  ((frame_desc*)(((char*)(d)) - \
     ((char*)(((frame_desc*)0)->context) - (char*)((frame_desc*)0))))

/* ------------------------------------------------------------------------ *
 *                private functions: pthreads support                       *
 * ------------------------------------------------------------------------ */

#define SYSNAME "cipher"
#include "peks-sema.h"

/* ------------------------------------------------------------------------- *
 *                    private functions: crc/md wrappers                     *
 * ------------------------------------------------------------------------- */

static void
md_first
  (void       *vp,
   const char *buf,
   unsigned    len)
{
  (*((frame_class*)frame_txetnoc (vp)->class)->init) (vp) ;
  if (len) {
# ifdef __ALIGNMENT_FIX_NEEDED
#   if defined (HAVE_32ALIGN) || defined (HAVE_64ALIGN)
    char *cpy ;
#   endif
#   ifdef HAVE_32ALIGN
    if (((long)buf & 3) != 0) {
      cpy = ALLOCA (len) ;
      memcpy (cpy, buf, len); 
      buf = cpy;
    }
#   endif
#   ifdef HAVE_64ALIGN
    if (((long)buf & 7) != 0) {
      cpy = ALLOCA (len) ;
      memcpy (cpy, buf, len); 
      buf = cpy;
    }
#   endif
# endif /* __ALIGNMENT_FIX_NEEDED */
    (*((frame_class*)frame_txetnoc (vp)->class)->write) (vp, (char*)buf, len) ;
# ifdef __ALIGNMENT_FIX_NEEDED
#   if defined (HAVE_32ALIGN) || defined (HAVE_64ALIGN)
    DEALLOCA (cpy);
#   endif
# endif /* __ALIGNMENT_FIX_NEEDED */
  }
}

static void
md_next
  (void       *vp,
   const char *buf,
   unsigned    len)
{
# ifdef __ALIGNMENT_FIX_NEEDED
# if defined (HAVE_32ALIGN) || defined (HAVE_64ALIGN)
  char *cpy ;
# endif
# ifdef HAVE_32ALIGN
  if (((long)buf & 3) != 0) {
    cpy = ALLOCA (len); 
    memcpy (cpy, buf, len); 
    buf = cpy;
  }
# endif
# ifdef HAVE_64ALIGN
  if (((long)buf & 7) != 0) {
    cpy = ALLOCA (len); 
    memcpy (cpy, buf, len); 
    buf = cpy;
  }
# endif
# endif /* __ALIGNMENT_FIX_NEEDED */
  (*((frame_class*)frame_txetnoc (vp)->class)->write) (vp, (char*)buf, len) ;
# ifdef __ALIGNMENT_FIX_NEEDED
# if defined (HAVE_32ALIGN) || defined (HAVE_64ALIGN)
  DEALLOCA (cpy);
# endif
# endif /* __ALIGNMENT_FIX_NEEDED */
}

static unsigned char *
md_result
  (void *vp)
{
  frame_class *c = (frame_class*)frame_txetnoc (vp)->class ;

  (*c->final) (vp) ;
  
  /* Get a digest of length mdlen returned in an array (part of vp) */
  return (*c->read) (vp) ;
}


/* ------------------------------------------------------------------------- *
 *                 private functions: add classes                            *
 * ------------------------------------------------------------------------- */

static void *
new_class
  (void    **List,
   unsigned  size,
   unsigned  *Dim)
{
  /* make a new table entry in the list []; note that there is always 
     one more terminating NULL entry at the end of the list, which is not 
     counted by the dimension */

  if (*Dim) {
    *List = XREALLOC (*List, (2 + (*Dim)) * size) ;
#   ifndef XREALLOC_DOES_INITIALIZE
    memset ((char *)(*List) + size * (*Dim), 0, size << 1) ;
#   endif
  } else {
    *List = XMALLOC (size << 1) ;
  }

  /* return next free entry and set to the new dimension */
  return (char *)(*List) + size * ((*Dim) ++) ;
}


static cipher_class *
new_cipher (void)
{
  cipher_class *p = new_class 
    ((void**)&cipher_list, sizeof (cipher_class), &cipher_list_dim) ;

  return p ;
}


static frame_class *
new_frame (void)
{
  frame_class *p = new_class 
    ((void**)&frame_list, sizeof (frame_class), &frame_list_dim) ;

  p->crc.first  = md_first ;
  p->crc.next   = md_next ;
  p->crc.result = md_result ;

  return p ;
}


/* ------------------------------------------------------------------------- *
 *                private functions: add some ciphers and frames             *
 * ------------------------------------------------------------------------- */

#ifdef EMULATE_LONG_KEY_HASH
static char *
genkey_from16key
  (unsigned       char *new,
   unsigned             len, /* between 17 and 40 */
   const unsigned char *old,
   unsigned             oln)
{
  static frame_desc *md ;
  unsigned chunk = (len+1)>>1 ;

  __enter_lock_semaphore ();

  if (md == 0) {
    md = create_frame (find_frame_class (EMULATE_LONG_KEY_HASH), 0);
    assert (md != 0);
  }

  /* initialization, only ? */
  if (new == 0) return 0 ;

  XCRCFIRST (md, old, oln>>1); /* first half (size truncated) */
  memcpy (new, XCRCRESULT0 (md), chunk) ;

  XCRCFIRST (md, old + (oln>>1), (oln+1)>>1); /* all the rest */
  memcpy (new + chunk, XCRCRESULT0 (md), chunk) ;

  __release_lock_semaphore ();

  return new;
}
#endif


static void
link_ciphers (void) 
{
  cipher_class *p = 0 ;

  if (cipher_list_dim != 0) 
    return ;

# ifdef CIPHER_BLOWFISH
  p = new_cipher () ;
  p->name = blowfish_get_info			/* 128 bit key length */
    (4, &p->keybits, &p->blocklen, &p->contextlen, 
     &p->set_enkey, &p->encrypt, &p->decrypt) ;
  p->set_dekey = p->set_enkey ;
  if ((p->keylen = (p->keybits + CHAR_BIT - 1) / CHAR_BIT) > KEYLENGTH_MAX)
    p->name = "" ; /* unusable */

  if (p->name != 0) p = new_cipher () ;
  p->name = blowfish_get_info			/* 160 bit key length */
    (42, &p->keybits, &p->blocklen, &p->contextlen, 
     &p->set_enkey, &p->encrypt, &p->decrypt) ;
  p->set_dekey = p->set_enkey ;
  if ((p->keylen = (p->keybits + CHAR_BIT - 1) / CHAR_BIT) > KEYLENGTH_MAX)
# ifdef EMULATE_BF20KEY_BY_BF16KEY
    if (p->keylen < 41) {
      p->map16to_long_key = 20 ;
      p->keylen = KEYLENGTH_MAX ;
    } else
# endif
      p->name = "" ; /* unusable */
# endif /* CIPHER_BLOWFISH */

# ifdef CIPHER_TWOFISH
  if (p->name != 0) p = new_cipher () ;
  p->name = twofish_get_info			/* 128 bit key length */
    (102, &p->keybits, &p->blocklen, &p->contextlen, 
     &p->set_enkey, &p->encrypt, &p->decrypt) ;
  p->set_dekey = p->set_enkey ;
  if ((p->keylen = (p->keybits + CHAR_BIT - 1) / CHAR_BIT) > KEYLENGTH_MAX)
    p->name = "" ; /* unusable */
  /* compat mode: twofish128 -> twofish */
  if (strcmp (p->name, "TWOFISH128") == 0)
    p->name = "TWOFISH" ;
# endif /* CIPHER_TWOFISH */

# ifdef CIPHER_3DES
  if (p->name != 0) p = new_cipher () ;
  p->name = des_get_info			/* 128 bit key length */
    (2, &p->keybits, &p->blocklen, &p->contextlen, 
     &p->set_enkey, &p->encrypt, &p->decrypt) ;
  p->set_dekey = p->set_enkey ;
  if ((p->keylen = (p->keybits + CHAR_BIT - 1) / CHAR_BIT) > KEYLENGTH_MAX)
# ifdef EMULATE_DES24KEY_BY_DES16KEY
    if (p->keylen < 41) {
      p->map16to_long_key = p->keylen ;
      p->keylen =  KEYLENGTH_MAX ; 
    } else
# endif
      p->name = "" ; /* unusable */
# endif /* CIPHER_3DES */

#ifdef EMULATE_LONG_KEY_HASH
  /* initialize ... */
  genkey_from16key (0, 0, 0, 0);
# endif
}


static void
link_frames (void) 
{
  frame_class *p = 0 ;
  unsigned char *c_dummy ;
  int i_dummy;

  if (frame_list_dim != 0) 
    return ;

  p = new_frame () ;

# ifdef MD5_FRAME
  p->name = md5_get_info 
    (1, &p->contextlen, &c_dummy, &i_dummy, &p->mdlen,
     &p->init, &p->write, &p->final, &p->read) ;
  if (p->name != 0 && p->mdlen >= 8) p = new_frame () ;
# endif
  
# ifdef RMD_FRAME
  p->name = rmd160_get_info 
    (3, &p->contextlen, &c_dummy, &i_dummy, &p->mdlen,
     &p->init, &p->write, &p->final, &p->read) ;
  if (p->name != 0 && p->mdlen >= 8) p = new_frame () ;
# endif

# ifdef SHA_FRAME
  p->name = sha1_get_info 
    (2, &p->contextlen, &c_dummy, &i_dummy, &p->mdlen,
     &p->init, &p->write, &p->final, &p->read) ;
  if (p->name != 0 && p->mdlen >= 8) p = new_frame () ;
# endif

  p->name = "" ;
}

/* ------------------------------------------------------------------------- *
 *                 private functions: instance functions                     *
 * ------------------------------------------------------------------------- */

static cipher_desc * 
_create_cipher_desc
   (cipher_class *p)
{
  /* use secure memory for the encryption state */
  cipher_desc *q = SMALLOC (sizeof (frame_desc) + p->contextlen -1) ;

  /* back link to the proper class */
  q->class    = p ;

  /* lnk default params */
  q->keylen   = p->keylen ;
  q->blocklen = p->blocklen ;

  return q ;
}

/* ------------------------------------------------------------------------- *
 *                private functions: find classes                            *
 * ------------------------------------------------------------------------- */

static cipher_class * /* assuming name != 0 */
_find_cipher_class
   (const char *name,
    unsigned     len)
{
  cipher_class *cl = cipher_list ;
  
  if (*name == 0 || len == 0)
    return 0 ;

  /* find cipher name in the list */
  while (cl->name != 0) {
    if (strncasecmp (name, cl->name, len) == 0 && 
	cl->name [len] == '\0')
      return cl ;
    cl ++ ;
  }
  return 0;
}


static frame_class * /* assuming name != 0 */
_find_frame_class
   (const char *name,
    unsigned     len)
{
  frame_class *fl = frame_list ;

  if (*name == 0 || len == 0)
    return 0 ;

  /* find frame name in the list */
  while (fl->name != 0) {
    if (strncasecmp (name, fl->name, len) == 0 && 
	fl->name [len] == '\0')
      return fl ;
    fl ++  ;
  }
  return 0;
}


/* ------------------------------------------------------------------------- *
 *                 public functions: find classes                            *
 * ------------------------------------------------------------------------- */

void *
find_cipher_class
  (const char *name)
{
  link_ciphers () ;
  
  return name == 0 
    ? 0 
    : _find_cipher_class (name, strlen (name)) ;
}

void *
find_frame_class
  (const char *name)
{
  link_frames () ;

  return name == 0 
    ? 0 
    : _find_frame_class (name, strlen (name)) ;
}

int
find_classes_by_name
  (void    **cipher,
   void       **crc,
   const char *name)
{
  cipher_class *cl ;
  frame_class  *fl ;
  const char *p, *q;
  int len ;

  link_ciphers () ;
  link_frames () ;

  /* if called just for initialization */
  if (name == 0) 
    return -1 ;

  /* cipher splits into <cipher-name>/<crc-name>[:<digit>] */
  if ((p = strchr (name, '/')) == 0)
    return -1 ;

  if ((cl = _find_cipher_class (name, p - name)) == 0)
    return -1 ;

# ifdef USE_ZLIB_COMPRESSION
  if ((q = strchr (p+1, ':')) != 0) {
    /* must be :<digit> */
    if (! isdigit (q [1]) || q [2])
      return -1;
    len = q - p - 1 ;
  } else
# endif
    len =  strlen (p+1) ;

  if ((fl = _find_frame_class (p+1, len)) == 0)
    return -1 ;
  
  if (cipher != 0) *cipher = cl ;
  if (crc    != 0) *crc    = fl ;
  return 0;
}

unsigned
cipher_keylen
  (const char *cipher)
{
  unsigned len ;
  char *p;
  cipher_class *cl ;

  link_ciphers () ;

  if (cipher == 0)
    return 0 ;

  /* text can come as <cipher-name>/<hash-name> */
  if ((p = strchr (cipher, '/')) != 0)
    len = p - cipher ;
  else
    len = strlen (cipher) ;

  return (cl = _find_cipher_class (cipher, len)) != 0 ? cl->keylen : 0 ;
}

 
/* ------------------------------------------------------------------------- *
 *                 public functions: initialize                              *
 * ------------------------------------------------------------------------- */

cipher_desc *
create_encryption
  (void    *cipher,
   const char *key,
   unsigned    len)
{
  cipher_desc *p;
  char *k = 0;
#ifdef US_EXPORT_MAGICIAN
  char *y;
#endif
  int n ;

# define c ((cipher_class *)cipher)

  if (cipher == 0 || len < c->keylen)
    return 0 ;
  p = _create_cipher_desc (c) ;

  len = c->keylen ;
#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    k   = ALLOCA (len = c->map16to_long_key);
    key = genkey_from16key (k, len, key, c->keylen) ;
  }
#endif

#ifdef US_EXPORT_MAGICIAN
# ifdef HAVE_PRAGMA_WARNING
#  pragma message Restricted key lengths: create_encryption
# else
#  warning Restricted key lengths: create_encryption
#endif
  y = ALLOCA (len) ;
  memcpy (y, key, len);
  US_EXPORT_MAGICIAN (y, len) ;
  key = y ;
#endif /* US_EXPORT_MAGICIAN */

  /* returns non zero if there are some problems with the key */
  n = (*c->set_enkey) (p->context, (char *)key, len) ;

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    memset (k, -1, c->map16to_long_key) ;
    DEALLOCA (k);
  }
#endif

#ifdef US_EXPORT_MAGICIAN
  memset (y, -1, len);
  DEALLOCA (y) ;
#endif

  if (n != 0) { XFREE (p) ; return 0 ; }
  
  /* cast to get rid of the (const char*) vs. (char*) warning */
  p->crypt = (crypto_fn)c->encrypt ;
  return p ;
# undef c
}


cipher_desc *
create_decryption
  (void    *cipher,
   const char *key,
   unsigned    len)
{
  cipher_desc *p ;
  int n ; 
  char *k = 0;
#ifdef US_EXPORT_MAGICIAN
  char *y;
#endif

# define c ((cipher_class *)cipher)

  if (cipher == 0 || len < c->keylen)
    return 0 ;
  p = _create_cipher_desc (c) ;

  len = c->keylen ;
#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    k   = ALLOCA (len = c->map16to_long_key);
    key = genkey_from16key (k, len, key, c->keylen) ;
  }
#endif

#ifdef US_EXPORT_MAGICIAN
# ifdef HAVE_PRAGMA_WARNING
#  pragma message Restricted key lengths: create_decryption
# else
#  warning Restricted key lengths: create_decryption
#endif
  y = ALLOCA (len) ;
  memcpy (y, key, len);
  US_EXPORT_MAGICIAN (y, len) ;
  key = y ;
#endif /* US_EXPORT_MAGICIAN */

  /* returns non zero if there are some problems with the key */
  n = (*c->set_dekey) (p->context, (char *)key, len) ;

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    memset (k, -1, c->map16to_long_key) ;
    DEALLOCA (k);
  }
#endif

#ifdef US_EXPORT_MAGICIAN
  memset (y, -1, len);
  DEALLOCA (y) ;
#endif

  if (n != 0) { XFREE (p) ; return 0 ; }
  
  /* cast to get rid of the (const char*) vs. (char*) warning */
  p->crypt = (crypto_fn)c->decrypt ;
  return p ;
# undef c
}


cipher_desc *
duplicate_cipher
  (cipher_desc *cipher)
{
  unsigned size ;

  if (cipher == 0)
    return 0 ;

  /* duplicate current state with cipher descriptor */
  size = sizeof (*cipher) + ((cipher_class *)(cipher->class))->contextlen - 1;

  /* copy to secure memory */
  return memcpy (SMALLOC (size), cipher, size) ;
}


int
change_encryption_key
  (cipher_desc *cipher,
   const char     *key)
{
# define c ((cipher_class *)(cipher->class))
  char *k = 0;
  int n, len = cipher->keylen ;
#ifdef US_EXPORT_MAGICIAN
  char *y;
#endif

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    k   = ALLOCA (len = c->map16to_long_key) ;
    key = genkey_from16key (k, len, key, c->keylen) ;
  }
#endif

#ifdef US_EXPORT_MAGICIAN
# ifdef HAVE_PRAGMA_WARNING
#  pragma message Restricted key lengths: change_encryption_key
# else
#  warning Restricted key lengths: change_encryption_key
# endif
  y = ALLOCA (len) ;
  memcpy (y, key, len);
  US_EXPORT_MAGICIAN (y, len) ;
  key = y ;
#endif /* US_EXPORT_MAGICIAN */

  /* returns non zero if there are some problems with the key */
  n =(*c->set_enkey) (cipher->context, (char *)key, len) ;

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    memset (k, -1, c->map16to_long_key) ;
    DEALLOCA (k);
  }
#endif

#ifdef US_EXPORT_MAGICIAN
  memset (y, -1, len);
  DEALLOCA (y) ;
#endif

  return n;
# undef c
}


int
change_decryption_key
  (cipher_desc *cipher,
   const char     *key)
{
# define c ((cipher_class *)(cipher->class))
  char *k = 0;
  int n, len = cipher->keylen ;
#ifdef US_EXPORT_MAGICIAN
  char *y;
#endif

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    k   = ALLOCA (len = c->map16to_long_key) ;
    key = genkey_from16key (k, len, key, c->keylen) ;
  }
#endif

#ifdef US_EXPORT_MAGICIAN
# ifdef HAVE_PRAGMA_WARNING
#  pragma message Restricted key lengths: change_decryption_key
# else
#  warning Restricted key lengths: change_decryption_key
# endif
  y = ALLOCA (len) ;
  memcpy (y, key, len);
  US_EXPORT_MAGICIAN (y, len) ;
  key = y ;
#endif /* US_EXPORT_MAGICIAN */

  /* returns non zero if there are some problems with the key */
  n = (*c->set_dekey) (cipher->context, (char *)key, len) ;

#ifdef EMULATE_LONG_KEY_HASH
  if (c->map16to_long_key) {
    memset (k, -1, c->map16to_long_key) ;
    DEALLOCA (k);
  }
#endif

#ifdef US_EXPORT_MAGICIAN
  memset (y, -1, len);
  DEALLOCA (y) ;
#endif

  return n;
# undef c
}


frame_desc *
create_frame
  (void     *frame,
   unsigned offset)
{
  frame_desc *p ;
# define f ((frame_class *)frame)

  if (frame == 0)
    return 0;

  /* create desriptor on secure memory */
  p = SMALLOC (sizeof (frame_desc) + f->contextlen - 1) ;

  /* back link to the proper class */
  p->class = frame ;

  /* link default methods */
  p->crc   = f->crc ;

  /* copy digest length */
  p->mdlen = f->mdlen ;

  if (f->mdlen > 4)
    /* hash values usually have 16 bytes - use this
       to extract a 4 byte window, from */
    p->offset = (offset % (f->mdlen - 4));

  return p;
# undef f
}


frame_desc *
duplicate_frame (frame)
     frame_desc *frame ;
{
  unsigned size ;

  if (frame == 0)
    return 0 ;

  /* duplicate current state with frame descriptor */
  size = sizeof (*frame) + ((frame_class *)(frame->class))->contextlen - 1;

  /* copy to secure memory */
  return memcpy (SMALLOC (size), frame, size) ;
}

void
destroy_cipher (desc)
     cipher_desc *desc ;
{
  cipher_class *class ;
  if (desc == 0 || (class = desc->class) == 0)
    return ;
  XFREE (desc) ; 
}

void
destroy_frame (desc)
     frame_desc *desc ;
{
  frame_class *class ;
  if (desc == 0 || (class = desc->class) == 0)
    return ;
  XFREE (desc) ; 
}

/* ------------------------------------------------------------------------- *
 *                    public functions: misc                                 *
 * ------------------------------------------------------------------------- */

#ifdef USE_PTHREADS
void cipher_sema_create (void *attr) { __sema_create (attr); }
void cipher_sema_destroy (void)      { __sema_destroy    (); }
#endif