78c567d0dc2fc8b9c0a5ed5497c7656d980a8d08
[tinc] / src / genauth.c
1 /*
2     genauth.c -- generate public/private keypairs
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
4                             2000 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: genauth.c,v 1.7.4.4 2000/10/20 15:34:35 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <openssl/rsa.h>
29 #include <openssl/rand.h>
30
31 #include <xalloc.h>
32
33 #include "system.h"
34
35 #define RSA_PUBLIC_EXPONENT 65535
36
37 void indicator(int a, int b, void *p)
38 {
39   switch(a)
40   {
41     case 0:
42       fprintf(stderr, ".");
43       break;
44     case 1:
45       fprintf(stderr, "+");
46       break;
47     case 2:
48       fprintf(stderr, "-");
49       break;
50     case 3:
51       switch(b)
52         {
53           case 0:
54             fprintf(stderr, " p\n");      
55             break;
56           case 1:
57             fprintf(stderr, " q\n");
58             break;
59           default:
60             fprintf(stderr, "?");
61          }
62        break;
63     default:
64       fprintf(stderr, "?");
65   }
66 }
67
68 int main(int argc, char **argv)
69 {
70   int bits;
71   RSA *key;
72
73   setlocale (LC_ALL, "");
74   bindtextdomain (PACKAGE, LOCALEDIR);
75   textdomain (PACKAGE);
76
77   if(argc > 2 || (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))))
78     {
79       fprintf(stderr, _("Usage: %s bits\n"), argv[0]);
80       return 1;
81     }
82
83   if(!argv[1])
84     argv[1] = "1024";
85     
86   bits = atol(argv[1]);
87
88   if(bits<32)
89     {
90       fprintf(stderr, _("Illegal number: %s\n"), argv[1]);
91       return 1;
92     }
93     
94   bits = ((bits - 1) | 7) + 1;          /* Align to bytes for easy mallocing and reading */
95
96   fprintf(stderr, _("Seeding the PRNG: please press some keys or move\nthe mouse if this program seems to have halted...\n"));
97
98   RAND_load_file("/dev/random", 1024);  /* OpenSSL PRNG state apparently uses 1024 bytes */
99
100   fprintf(stderr, _("Generating %d bits keys:\n"), bits);
101
102   key = RSA_generate_key(bits, RSA_PUBLIC_EXPONENT, indicator, NULL);
103
104   fprintf(stderr, _("Done.\n"));
105
106   printf(_("Public key:  %s\n"), BN_bn2hex(key->n));
107   printf(_("Private key: %s\n"), BN_bn2hex(key->d));
108   printf(_("Public exp:  %s\n"), BN_bn2hex(key->e));
109
110   fflush(stdin);        /* Flush any input caused by random keypresses */
111
112   return 0;
113 }