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
  
/* -*- 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 "builtin_functions.h" 
#include "interpret.h" 
#include "module.h" 
 
#include "nettle_config.h" 
 
#ifdef HAVE_LIBHOGWEED 
 
DECLARATIONS 
 
#include "nettle.h" 
#include <nettle/dsa.h> 
#include <nettle/rsa.h> 
#include <gmp.h> 
 
#include "bignum.h" 
 
void random_func_wrapper(void *f, unsigned int num, uint8_t *out) 
{ 
  push_int(num); 
  apply_svalue((struct svalue *)f, 1); 
  if(TYPEOF(Pike_sp[-1])!=T_STRING) 
    Pike_error("Random function did not return string value.\n"); 
  if(Pike_sp[-1].u.string->len != num) 
    Pike_error("Random function did not return correct number of bytes.\n"); 
  memcpy(out, Pike_sp[-1].u.string->str, num); 
  pop_stack(); 
} 
 
/*! @decl array(object(Gmp.mpz)) @ 
 *!         dsa_generate_keypair(int p_bits, int q_bits, @ 
 *!                              function(int:string(0..255)) rnd) 
 *! 
 *! Generates a DSA key pair with @[p_bits] number of bits (sometimes 
 *! referred to as L) for p, and @[q_bits] number of bits (sometimes 
 *! referred to as N) for q, using the random function @[rnd]. 
 *! 
 *! Valid combinations as per FIPS 186-3 are 
 *! @pre{ 
 *!   p_bits  q_bits 
 *!   1024    160 
 *!   2048    224 (rejected by some versions of Hogweed) 
 *!   2048    256 
 *!   3072    256 
 *! @} 
 *! 
 *! @returns 
 *!   @array 
 *!     @elem Gmp.mpz 0 
 *!       The value p, the modulo. 
 *!     @elem Gmp.mpz 1 
 *!       The value q, the group order. 
 *!     @elem Gmp.mpz 2 
 *!       The value g, the generator. 
 *!     @elem Gmp.mpz 3 
 *!       The value y, the public value. 
 *!     @elem Gmp.mpz 4 
 *!       The value x, the private value. 
 *!   @endarray 
 */ 
PIKEFUN array(object(Gmp.mpz)) 
  dsa_generate_keypair(int p_bits, int q_bits, function(int:string(0..255)) rnd) 
{ 
  struct dsa_public_key pub; 
  struct dsa_private_key key; 
 
  dsa_public_key_init(&pub); 
  dsa_private_key_init(&key); 
 
  if( !nettle_dsa_generate_keypair(&pub, &key, rnd, random_func_wrapper, 
                                   NULL, NULL, p_bits, q_bits) ) 
  { 
    dsa_private_key_clear(&key); 
    dsa_public_key_clear(&pub); 
 
    Pike_error("Illegal parameter value.\n"); 
  } 
 
  push_bignum((MP_INT *)&pub.p); 
  push_bignum((MP_INT *)&pub.q); 
  push_bignum((MP_INT *)&pub.g); 
  push_bignum((MP_INT *)&pub.y); 
  push_bignum((MP_INT *)&key.x); 
 
  dsa_private_key_clear(&key); 
  dsa_public_key_clear(&pub); 
 
  f_aggregate(5); 
  stack_pop_n_elems_keep_top(args); /* Remove p_bits, q_bits and rnd. */ 
} 
 
/*! @decl array(object(Gmp.mpz)) @ 
 *!         rsa_generate_keypair(int bits, int e, function(int:string(0..255)) rnd) 
 *! 
 *! Generates an RSA key pair with a @[bits] sized modulus (n), using 
 *! the provided value for @[e] and random function @[rnd]. 
 *! 
 *! @returns 
 *!   @array 
 *!     @elem Gmp.mpz 0 
 *!       The value n, the modulo. 
 *!     @elem Gmp.mpz 1 
 *!       The value d, the private exponent. 
 *!     @elem Gmp.mpz 2 
 *!       The value p, a prime. 
 *!     @elem Gmp.mpz 3 
 *!       The value q, a prime. 
 *!   @endarray 
 */ 
PIKEFUN array(object(Gmp.mpz)) 
  rsa_generate_keypair(int bits, int e, function(int:string(0..255)) rnd) 
{ 
  struct rsa_public_key pub; 
  struct rsa_private_key key; 
 
  rsa_public_key_init(&pub); 
  rsa_private_key_init(&key); 
 
  mpz_set_ui((MP_INT *)&pub.e, e); 
 
  if( !nettle_rsa_generate_keypair(&pub, &key, rnd, random_func_wrapper, 
                                   NULL, NULL, bits, 0) ) 
  { 
    rsa_private_key_clear(&key); 
    rsa_public_key_clear(&pub); 
 
    Pike_error("Illegal parameter value.\n"); 
  } 
 
  push_bignum((MP_INT *)&pub.n); 
  push_bignum((MP_INT *)&key.d); 
  push_bignum((MP_INT *)&key.p); 
  push_bignum((MP_INT *)&key.q); 
 
  rsa_private_key_clear(&key); 
  rsa_public_key_clear(&pub); 
 
  f_aggregate(4); 
  stack_pop_n_elems_keep_top(args); /* Remove bits, e and rnd. */ 
} 
 
#ifdef HAVE_NETTLE_ECDSA_H 
#include <nettle/ecc-curve.h> 
 
#include <nettle/ecc.h> 
 
/*! @class ECC_Curve 
 *! 
 *! Elliptic Curve Definition 
 */ 
PIKECLASS ECC_Curve 
{ 
  CVAR const struct ecc_curve *curve; 
 
  /*! @decl int size() 
   *! 
   *! @returns 
   *!   Returns the size in bits for a single coordinate on the curve. 
   */ 
  PIKEFUN int size() 
  { 
    if (THIS->curve) { 
      push_int64(ecc_size(THIS->curve) * sizeof(mp_limb_t) * 8); 
    } else { 
      push_undefined(); 
    } 
  } 
 
  /*! @decl Gmp.mpz new_scalar(function(int:string(8bit)) rnd) 
   *! 
   *! @param rnd 
   *!   Randomness function to use as source. 
   *! 
   *! @returns 
   *!   Returns a random scalar suitable to use as an @[ECDSA] private key 
   *!   or as an ECDH exponent. 
   */ 
  PIKEFUN object(Gmp.mpz) new_scalar(function(int:string(8bit)) rnd) 
  { 
    struct ecc_scalar s; 
    struct object *ret; 
 
    ecc_scalar_init(&s, THIS->curve); 
 
    ecc_scalar_random(&s, rnd, random_func_wrapper); 
 
    push_object(ret = fast_clone_object(get_auto_bignum_program())); 
    ecc_scalar_get(&s, (mpz_ptr)ret->storage); 
 
    ecc_scalar_clear(&s); 
  } 
 
  /*! @decl array(Gmp.mpz) `*(Gmp.moz|int scalar) 
   *! 
   *! Multiply the curve by a scalar. 
   *! 
   *! @returns 
   *!   Returns a new point (x, y) on the curve. 
   */ 
  PIKEFUN array(object(Gmp.mpz)) `*(object(Gmp.mpz)|int scalar) 
  { 
    struct ecc_scalar s; 
    struct ecc_point r; 
    struct object *x; 
    struct object *y; 
 
    convert_svalue_to_bignum(scalar); 
 
    ecc_scalar_init(&s, THIS->curve); 
    ecc_point_init(&r, THIS->curve); 
 
    if (!ecc_scalar_set(&s, (mpz_srcptr)scalar->u.object->storage)) { 
      ecc_scalar_clear(&s); 
      ecc_point_clear(&r); 
      SIMPLE_ARG_ERROR("`*", 1, "Invalid scalar for curve."); 
    } 
 
    ecc_point_mul_g(&r, &s); 
    push_object(x = fast_clone_object(get_auto_bignum_program())); 
    push_object(y = fast_clone_object(get_auto_bignum_program())); 
    ecc_point_get(&r, (mpz_ptr)x->storage, (mpz_ptr)y->storage); 
 
    ecc_scalar_clear(&s); 
    ecc_point_clear(&r); 
 
    f_aggregate(2); 
  } 
} 
 
/*! @endclass ECC_Curve 
 */ 
 
#endif /* HAVE_NETTLE_ECDSA_H */ 
 
void 
hogweed_init(void) 
{ 
#ifdef HAVE_NETTLE_ECDSA_H 
  struct svalue c; 
#endif 
 
  INIT; 
 
#ifdef HAVE_NETTLE_ECDSA_H 
  SET_SVAL(c, PIKE_T_OBJECT, 0, object, fast_clone_object(ECC_Curve_program)); 
  OBJ2_ECC_CURVE(c.u.object)->curve = &nettle_secp_192r1; 
  simple_add_constant("SECP_192R1", &c, 0); 
  free_svalue(&c); 
 
  SET_SVAL(c, PIKE_T_OBJECT, 0, object, fast_clone_object(ECC_Curve_program)); 
  OBJ2_ECC_CURVE(c.u.object)->curve = &nettle_secp_224r1; 
  simple_add_constant("SECP_224R1", &c, 0); 
  free_svalue(&c); 
 
  SET_SVAL(c, PIKE_T_OBJECT, 0, object, fast_clone_object(ECC_Curve_program)); 
  OBJ2_ECC_CURVE(c.u.object)->curve = &nettle_secp_256r1; 
  simple_add_constant("SECP_256R1", &c, 0); 
  free_svalue(&c); 
 
  SET_SVAL(c, PIKE_T_OBJECT, 0, object, fast_clone_object(ECC_Curve_program)); 
  OBJ2_ECC_CURVE(c.u.object)->curve = &nettle_secp_384r1; 
  simple_add_constant("SECP_384R1", &c, 0); 
  free_svalue(&c); 
 
  SET_SVAL(c, PIKE_T_OBJECT, 0, object, fast_clone_object(ECC_Curve_program)); 
  OBJ2_ECC_CURVE(c.u.object)->curve = &nettle_secp_521r1; 
  simple_add_constant("SECP_521R1", &c, 0); 
  free_svalue(&c); 
#endif 
} 
 
void 
hogweed_exit(void) 
{ 
  EXIT; 
} 
 
#endif