d5f6892013-11-24Martin Nilsson /* -*- 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>
2b1c922013-12-24Henrik Grubbström (Grubba) #include "bignum.h"
d5f6892013-11-24Martin Nilsson 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(); }
f6a6ad2013-12-08Henrik Grubbström (Grubba) /*! @decl array(object(Gmp.mpz)) @ *! dsa_generate_keypair(int p_bits, int q_bits, @ *! function(int:string(0..255)) rnd)
6668dd2013-12-06Henrik Grubbström (Grubba)  *! *! Generates a DSA key pair with @[p_bits] number of bits (sometimes
d5f6892013-11-24Martin Nilsson  *! 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
230ae02013-11-25Martin Nilsson  *! 2048 224 (rejected by some versions of Hogweed)
d5f6892013-11-24Martin Nilsson  *! 2048 256 *! 3072 256 *! @} *! *! @returns *! @array
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 0
d5f6892013-11-24Martin Nilsson  *! The value p, the modulo.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 1
d5f6892013-11-24Martin Nilsson  *! The value q, the group order.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 2
d5f6892013-11-24Martin Nilsson  *! The value g, the generator.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 3
d5f6892013-11-24Martin Nilsson  *! The value y, the public value.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 4
d5f6892013-11-24Martin Nilsson  *! The value x, the private value. *! @endarray */
f6a6ad2013-12-08Henrik Grubbström (Grubba) PIKEFUN array(object(Gmp.mpz)) dsa_generate_keypair(int p_bits, int q_bits, function(int:string(0..255)) rnd)
d5f6892013-11-24Martin Nilsson { struct dsa_public_key pub; struct dsa_private_key key;
b6a4092013-12-25Henrik Grubbström (Grubba)  dsa_public_key_init(&pub); dsa_private_key_init(&key);
d5f6892013-11-24Martin Nilsson  if( !nettle_dsa_generate_keypair(&pub, &key, rnd, random_func_wrapper, NULL, NULL, p_bits, q_bits) ) {
b6a4092013-12-25Henrik Grubbström (Grubba)  dsa_private_key_clear(&key); dsa_public_key_clear(&pub);
28aa062013-12-02Martin Nilsson  Pike_error("Illegal parameter value.\n");
d5f6892013-11-24Martin Nilsson  }
b6a4092013-12-25Henrik Grubbström (Grubba)  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);
d5f6892013-11-24Martin Nilsson  f_aggregate(5);
b6a4092013-12-25Henrik Grubbström (Grubba)  stack_pop_n_elems_keep_top(args); /* Remove p_bits, q_bits and rnd. */
d5f6892013-11-24Martin Nilsson }
6668dd2013-12-06Henrik Grubbström (Grubba) /*! @decl array(object(Gmp.mpz)) @
f6a6ad2013-12-08Henrik Grubbström (Grubba)  *! rsa_generate_keypair(int bits, int e, function(int:string(0..255)) rnd)
6668dd2013-12-06Henrik Grubbström (Grubba)  *! *! Generates an RSA key pair with a @[bits] sized modulus (n), using
d5f6892013-11-24Martin Nilsson  *! the provided value for @[e] and random function @[rnd]. *! *! @returns *! @array
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 0
d5f6892013-11-24Martin Nilsson  *! The value n, the modulo.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 1
d5f6892013-11-24Martin Nilsson  *! The value d, the private exponent.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 2
d5f6892013-11-24Martin Nilsson  *! The value p, a prime.
6668dd2013-12-06Henrik Grubbström (Grubba)  *! @elem Gmp.mpz 3
d5f6892013-11-24Martin Nilsson  *! The value q, a prime. *! @endarray */ PIKEFUN array(object(Gmp.mpz))
f6a6ad2013-12-08Henrik Grubbström (Grubba)  rsa_generate_keypair(int bits, int e, function(int:string(0..255)) rnd)
d5f6892013-11-24Martin Nilsson { struct rsa_public_key pub; struct rsa_private_key key;
b6a4092013-12-25Henrik Grubbström (Grubba)  rsa_public_key_init(&pub); rsa_private_key_init(&key); mpz_set_ui(&pub.e, e);
d5f6892013-11-24Martin Nilsson  if( !nettle_rsa_generate_keypair(&pub, &key, rnd, random_func_wrapper, NULL, NULL, bits, 0) ) {
b6a4092013-12-25Henrik Grubbström (Grubba)  rsa_private_key_clear(&key); rsa_public_key_clear(&pub);
28aa062013-12-02Martin Nilsson  Pike_error("Illegal parameter value.\n");
d5f6892013-11-24Martin Nilsson  }
b6a4092013-12-25Henrik Grubbström (Grubba)  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);
d5f6892013-11-24Martin Nilsson  f_aggregate(4);
b6a4092013-12-25Henrik Grubbström (Grubba)  stack_pop_n_elems_keep_top(args); /* Remove bits, e and rnd. */
d5f6892013-11-24Martin Nilsson } void hogweed_init(void) { INIT; } void hogweed_exit(void) { EXIT; } #endif