lsh_keygen.c 3.07 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* lsh_keygen.c
 *
Niels Möller's avatar
Niels Möller committed
3
4
5
6
 * Generic key-generation program. Writes a spki-packages private key
 * on stdout. You would usually pipe this to some other program to
 * extract the public key, encrypt the private key, and save the
 * results in two separate files.
Niels Möller's avatar
Niels Möller committed
7
 *
Niels Möller's avatar
Niels Möller committed
8
 * $Id$ */
Niels Möller's avatar
Niels Möller committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "dss_keygen.h"

Niels Möller's avatar
Niels Möller committed
31
#include "blocking_write.h"
Niels Möller's avatar
Niels Möller committed
32
33
34
35
#include "crypto.h"
#include "format.h"
#include "publickey_crypto.h"
#include "randomness.h"
Niels Möller's avatar
Niels Möller committed
36
37
#include "sexp.h"
#include "werror.h"
Niels Möller's avatar
Niels Möller committed
38
39
40
41
42

#include "getopt.h"

#include <stdio.h>

Niels Möller's avatar
Niels Möller committed
43
44
#include <unistd.h>

Niels Möller's avatar
Niels Möller committed
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
static void usage(void) NORETURN;

static void usage(void)
{
  fprintf(stderr, "Usage: lsh_keygen nist-level\n");
  exit(1);
}

int main(int argc, char **argv)
{
  int l;
  struct dss_public public;
  mpz_t x;
  
  mpz_t t;
  struct randomness *r;
  
  if (argc != 2)
    usage();
  
  l = atoi(argv[1]);

  if ( (l<0) || (l > 8))
    usage();

  mpz_init(public.p);
  mpz_init(public.q);
  mpz_init(public.g);
  mpz_init(public.y);

  mpz_init(x);
  
  mpz_init(t);

  r = make_poor_random(&sha_algorithm, NULL);
  dss_nist_gen(public.p, public.q, r, l);

Niels Möller's avatar
Niels Möller committed
82
83
84
85
  debug_mpz(public.p);
  debug("\n");
  debug_mpz(public.q);
  debug("\n");
Niels Möller's avatar
Niels Möller committed
86
87

  /* Sanity check. */
Niels Möller's avatar
Niels Möller committed
88
  if (!mpz_probab_prime_p(public.p, 10))
Niels Möller's avatar
Niels Möller committed
89
    {
Niels Möller's avatar
Niels Möller committed
90
      werror("p not a prime!\n");
Niels Möller's avatar
Niels Möller committed
91
92
93
      return 1;
    }

Niels Möller's avatar
Niels Möller committed
94
  if (!mpz_probab_prime_p(public.q, 10))
Niels Möller's avatar
Niels Möller committed
95
    {
Niels Möller's avatar
Niels Möller committed
96
      werror("q not a prime!\n");
Niels Möller's avatar
Niels Möller committed
97
98
99
      return 1;
    }

Niels Möller's avatar
Niels Möller committed
100
  mpz_fdiv_r(t, public.p, public.q);
Niels Möller's avatar
Niels Möller committed
101
102
  if (mpz_cmp_ui(t, 1))
    {
Niels Möller's avatar
Niels Möller committed
103
      werror("q doesn't divide p-1 !\n");
Niels Möller's avatar
Niels Möller committed
104
105
106
107
108
109
      return 1;
    }

  dss_find_generator(public.g, r, public.p, public.q);

  r = make_reasonably_random();
Niels Möller's avatar
Niels Möller committed
110
  mpz_set(t, public.q);
Niels Möller's avatar
Niels Möller committed
111
112
113
114
115
  mpz_sub_ui(t, t, 2);
  bignum_random(x, r, t);

  mpz_add_ui(x, x, 1);

Niels Möller's avatar
Niels Möller committed
116
  mpz_powm(public.y, public.g, x, public.p);
Niels Möller's avatar
Niels Möller committed
117
  
Niels Möller's avatar
Niels Möller committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  {
    /* Now, output a private key spki structure. */
    struct abstract_write *output = make_blocking_write(STDOUT_FILENO);
    
    struct lsh_string *key = sexp_format
      (sexp_l(2, sexp_z("private-key"),
	      sexp_l(6, sexp_z("dss"),
		     sexp_l(2, sexp_z("p"), sexp_n(public.p), -1),
		     sexp_l(2, sexp_z("q"), sexp_n(public.q), -1),
		     sexp_l(2, sexp_z("g"), sexp_n(public.g), -1),
		     sexp_l(2, sexp_z("y"), sexp_n(public.y), -1),
		     sexp_l(2, sexp_z("x"), sexp_n(x), -1), -1), -1),
       SEXP_CANONICAL);

    return LSH_FAILUREP(A_WRITE(output, key))
      ? 1 : 0;
  }
Niels Möller's avatar
Niels Möller committed
135
136
137
}