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
  
/* nettle.cmod -*- c -*- */ 
 
#include "global.h" 
RCSID("$Id: nettle.cmod,v 1.4 2003/03/14 15:30:20 marcus Exp $"); 
#include "interpret.h" 
#include "svalue.h" 
/* For this_object() */ 
#include "object.h" 
#include "module_support.h" 
 
#include "nettle_config.h" 
 
#ifdef HAVE_LIBNETTLE 
 
#include "nettle.h" 
 
#include <nettle/aes.h> 
#include <nettle/nettle-meta.h> 
 
#include <assert.h> 
#include <stdio.h> 
#include <stdarg.h> 
 
/* #define sp Pike_sp */ 
 
DECLARATIONS 
 
/*! @module Nettle 
 */ 
 
 
PIKECLASS aes 
{ 
  CVAR struct aes_ctx ctx; 
  CVAR void (*f)(struct aes_ctx *, unsigned length, 
                 uint8_t *dst, const uint8_t *src); 
   
  PIKEFUN string name() 
    { 
      push_string(make_shared_string("aes")); 
    } 
 
  PIKEFUN int key_size() 
    { 
      /* Recommended key size. */ 
      push_int(AES_KEY_SIZE); 
    } 
 
  PIKEFUN int block_size() 
    { 
      push_int(AES_BLOCK_SIZE); 
    } 
 
  PIKEFUN object set_encrypt_key(string key) 
    { 
      NO_WIDE_STRING(key); 
 
      if ( (key->len < AES_MIN_KEY_SIZE) 
           || (key->len > AES_MAX_KEY_SIZE) ) 
        Pike_error("Invalid key size for aes."); 
 
      aes_set_encrypt_key(&THIS->ctx, key->len, key->str); 
 
      THIS->f = aes_encrypt; 
 
      push_object(this_object()); 
    } 
 
  PIKEFUN object set_decrypt_key(string key) 
    { 
      NO_WIDE_STRING(key); 
 
      if ( (key->len < AES_MIN_KEY_SIZE) 
           || (key->len > AES_MAX_KEY_SIZE) ) 
        Pike_error("Invalid key size for aes."); 
      aes_set_decrypt_key(&THIS->ctx, key->len, key->str); 
 
      THIS->f = aes_decrypt; 
 
      push_object(this_object()); 
    } 
 
  PIKEFUN string crypt(string data) 
    { 
      struct pike_string *s; 
      /* werror("aes->encrypt\n"); */ 
 
#if 0 
      { 
        struct aes_ctx test_ctx; 
        const uint8_t clear[16] = "aaaaaaaaaaaaaaaa"; 
        uint8_t cipher[16]; 
 
        werror("tfoo\n"); 
 
        aes_set_encrypt_key(&test_ctx, 16, "0123456789abcdef"); 
        werror("tbar\n"); 
 
        aes_encrypt(&test_ctx, 16, cipher, clear); 
 
        werror("tbaz\n"); 
      } 
#endif 
       
      if (!THIS->f) 
        Pike_error("Must install an encrypt or decrypt key first."); 
 
      // werror("foo\n"); 
       
      NO_WIDE_STRING(data); 
 
      // werror("bar\n"); 
 
      if (data->len % AES_BLOCK_SIZE) 
        Pike_error("Data must be an integer number of blocks."); 
 
      // werror("baz\n"); 
       
      s = begin_shared_string(data->len); 
 
      // werror("bazz\n"); 
 
// werror("ctx = %p, len = %d, dst = %p, src = %p\n", 
//           &THIS->ctx, data->len, s->str, data->str); 
       
      THIS->f(&THIS->ctx, data->len, s->str, data->str); 
      /* aes_encrypt(&THIS->ctx, data->len, s->str, data->str); */ 
 
      // werror("bazzz\n"); 
 
      push_string(end_shared_string(s)); 
 
      // werror("bazzzz\n"); 
    } 
 
  INIT 
    { 
      /* werror("aes->INIT\n"); */ 
      THIS->f = NULL; 
    } 
 
  EXIT 
    { 
      /* werror("aes->EXIT\n"); */ 
      memset(&THIS->ctx, 0, sizeof(THIS->ctx)); 
    } 
} 
 
#endif /* HAVE_LIBNETTLE */ 
 
PIKE_MODULE_INIT 
{ 
  INIT; 
#ifdef HAVE_LIBNETTLE 
  hash_init(); 
#endif /* HAVE_LIBNETTLE */ 
} 
 
PIKE_MODULE_EXIT 
{ 
#ifdef HAVE_LIBNETTLE 
  hash_exit(); 
#endif /* HAVE_LIBNETTLE */ 
  EXIT; 
}