home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / srp-tconf.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  9KB  |  281 lines

  1. /*
  2.  * Copyright (c) 1997 Stanford University
  3.  *
  4.  * The use of this software for revenue-generating purposes may require a
  5.  * license from the owners of the underlying intellectual property.
  6.  *
  7.  * Within that constraint, permission to use, copy, modify, and distribute
  8.  * this software and its documentation for any purpose is hereby granted
  9.  * without fee, provided that the above copyright notices and this permission
  10.  * notice appear in all copies of the software and related documentation.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  *
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  19.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  20.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. #ifndef OS2
  24. #ifndef _WIN32
  25. #include <unistd.h>     /* close getlogin */
  26. #endif
  27. #endif
  28. #include <stdlib.h>     /* atexit exit */
  29. #include <stdio.h>
  30. #ifdef OS2
  31. #ifndef _WIN32
  32. #include <io.h>
  33. #endif /* _WIN32 */
  34. #endif /* OS2 */
  35. #include <string.h>
  36. #include "getopt.h"
  37.  
  38. #include <t_pwd.h>
  39.  
  40.  
  41. #define MIN_BASIS_BITS 257
  42. #define BASIS_BITS 2048
  43.  
  44. #ifndef _WIN32
  45. extern int errno;
  46. #endif
  47.  
  48. struct pre_struct {
  49.   char * pre_mod;
  50.   char * pre_gen;
  51.   char * comment;
  52. } pre_params[] = {
  53.   { "HMujfBWu4LfBFA0j3PpN7UbgUYfv.rMoMNuVRMoekpZ",
  54.     "2",
  55.     NULL },
  56.   { "W2KsCfRxb3/ELBvnVWufMA0gbdBlLXbJihgZkgp3xLTKwtPCUhSOHNZ5VLb9pBGR",
  57.     "2",
  58.     NULL },
  59.   { "3Kn/YYiomHkFkfM1x4kayR125MGkzpLUDy3y14FlTMwYnhZkjrMXnoC2TcFAecNlU5kFzgcpKYUbBOPZFRtyf3",
  60.     "2",
  61.     NULL },
  62.   { "CbDP.jR6YD6wAj2ByQWxQxQZ7.9J9xkn2.Uqb3zVm16vQyizprhBw9hi80psatZ8k54vwZfiIeEHZVsDnyqeWSSIpWso.wh5GD4OFgdhVI3",
  63.     "2",
  64.     NULL },
  65.   { "iqJ7nFZ4bGCRjE1F.FXEwL085Zb0kLM2TdHDaVVCdq0cKxvnH/0FLskJTKlDtt6sDl89dc//aEULTVFGtcbA/tDzc.bnFE.DWthQOu2n2JwKjgKfgCR2lZFWXdnWmoOh",
  66.     "2",
  67.     NULL },
  68.   { "///////////93zgY8MZ2DCJ6Oek0t1pHAG9E28fdp7G22xwcEnER8b5A27cED0JTxvKPiyqwGnimAmfjybyKDq/XDMrjKS95v8MrTc9UViRqJ4BffZes8F//////////",
  69.     "7",
  70.     "oakley prime 1" },
  71.   { "Ewl2hcjiutMd3Fu2lgFnUXWSc67TVyy2vwYCKoS9MLsrdJVT9RgWTCuEqWJrfB6uE3LsE9GkOlaZabS7M29sj5TnzUqOLJMjiwEzArfiLr9WbMRANlF68N5AVLcPWvNx6Zjl3m5Scp0BzJBz9TkgfhzKJZ.WtP3Mv/67I/0wmRZ",
  72.     "2",
  73.     NULL },
  74.   { "F//////////oG/QeY5emZJ4ncABWDmSqIa2JWYAPynq0Wk.fZiJco9HIWXvZZG4tU.L6RFDEaCRC2iARV9V53TFuJLjRL72HUI5jNPYNdx6z4n2wQOtxMiB/rosz0QtxUuuQ/jQYP.bhfya4NnB7.P9A6PHxEPJWV//////////",
  75.     "5",
  76.     "oakley prime 2" },
  77.   { "3NUKQ2Re4P5BEK0TLg2dX3gETNNNECPoe92h4OVMaDn3Xo/0QdjgG/EvM.hiVV1BdIGklSI14HA38Mpe5k04juR5/EXMU0r1WtsLhNXwKBlf2zEfoOh0zVmDvqInpU695f29Iy7sNW3U5RIogcs740oUp2Kdv5wuITwnIx84cnO.e467/IV1lPnvMCr0pd1dgS0a.RV5eBJr03Q65Xy61R",
  78.     "2",
  79.     NULL },
  80.   { "dUyyhxav9tgnyIg65wHxkzkb7VIPh4o0lkwfOKiPp4rVJrzLRYVBtb76gKlaO7ef5LYGEw3G.4E0jbMxcYBetDy2YdpiP/3GWJInoBbvYHIRO9uBuxgsFKTKWu7RnR7yTau/IrFTdQ4LY/q.AvoCzMxV0PKvD9Odso/LFIItn8PbTov3VMn/ZEH2SqhtpBUkWtmcIkEflhX/YY/fkBKfBbe27/zUaKUUZEUYZ2H2nlCL60.JIPeZJSzsu/xHDVcx",
  81.     "2",
  82.     NULL },
  83.   { "2iQzj1CagQc/5ctbuJYLWlhtAsPHc7xWVyCPAKFRLWKADpASkqe9djWPFWTNTdeJtL8nAhImCn3Sr/IAdQ1FrGw0WvQUstPx3FO9KNcXOwisOQ1VlL.gheAHYfbYyBaxXL.NcJx9TUwgWDT0hRzFzqSrdGGTN3FgSTA1v4QnHtEygNj3eZ.u0MThqWUaDiP87nqha7XnT66bkTCkQ8.7T8L4KZjIImrNrUftedTTBi.WCi.zlrBxDuOM0da0JbUkQlXqvp0yvJAPpC11nxmmZOAbQOywZGmu9nhZNuwTlxjfIro0FOdthaDTuZRL9VL7MRPUDo/DQEyW.d4H.UIlzp",
  84.     "2",
  85.     NULL }
  86. };
  87.  
  88. #define NPARAMS (sizeof(pre_params) / sizeof(struct pre_struct))
  89.  
  90. char *progName;
  91.  
  92. int  debug   = 0;
  93. int  verbose = 0;
  94. int  composite = 0;
  95.  
  96. int
  97. main(argc, argv)
  98.      int argc;
  99.      char *argv[];
  100. {
  101.   char *chp;
  102.   char *configFile = NULL;
  103.   char cbuf[256];
  104.   char b64buf[MAXB64PARAMLEN];
  105.   int c, ch, i, lastidx, keylen, yesno, fsize, status;
  106.   FILE *efp;
  107.  
  108.   struct t_conf * tc = NULL;
  109.   struct t_confent * tcent;
  110.  
  111.   progName = *argv;
  112.   if ((chp = strrchr(progName, '/')) != (char *) 0) progName = chp + 1;
  113.  
  114.   while ((ch = getopt(argc, argv, "dv2c:")) != EOF)
  115.     switch(ch) {
  116.     case 'c':
  117.       configFile = optarg;
  118.       break;
  119.     case 'v':
  120.       verbose++;
  121.       break;
  122.     case 'd':
  123.       debug++;
  124.       break;
  125.     case '2':
  126.       composite++;
  127.       break;
  128.     default:
  129.       fprintf(stderr, "usage: %s [-dv2] [-c configfile]\n", progName);
  130.       exit(1);
  131.     }
  132.  
  133.     if ( !ck_crypto_loaddll() ) {
  134.         fprintf(stderr, "%s: unable to load crypto dll\n",progName);
  135.         exit(1);
  136.     }
  137.  
  138.     ck_OPENSSL_add_all_algorithms_noconf();
  139.   argc -= optind;
  140.   argv += optind;
  141.  
  142.     if (configFile == NULL)
  143.         configFile = (char *)t_defaultconf();
  144.  
  145.     efp = fopen(configFile, "a+");
  146.     if(efp == NULL)
  147.         if(creat(configFile, 0644) < 0 || (efp = fopen(configFile, "a+")) == NULL) {
  148.             fprintf(stderr, "%s: unable to create %s (errno = %d)\n",
  149.                      progName, configFile, errno);
  150.             exit(2);
  151.         }
  152.         else
  153.             printf("%s: Creating new configuration file %s\n", progName, configFile);
  154.  
  155.   tc = t_openconf(efp);
  156.   if(tc == NULL) {
  157.     fprintf(stderr, "%s: unable to open configuration file %s\n",
  158.             progName, configFile);
  159.     exit(2);
  160.   }
  161.  
  162.   tcent = t_getconflast(tc);
  163.   if(tcent == NULL)
  164.     lastidx = 0;
  165.   else
  166.     lastidx = tcent->index;
  167.  
  168.   if(lastidx > 0) {
  169.     keylen = 8 * tcent->modulus.len;
  170.     printf("Current field size is %d bits.\n", keylen);
  171.     printf("\nIncrease the default field size? [y] ");
  172.     yesno = 0;
  173.     while((c = getchar()) != '\n' && c != EOF)
  174.       if(yesno == 0)
  175.         if(c == 'n' || c == 'N')
  176.           yesno = -1;
  177.         else if(c == 'y' || c == 'Y')
  178.           yesno = 1;
  179.     if(c == EOF || yesno < 0)
  180.       exit(0);
  181.   }
  182.   else {
  183.     lastidx = 0;
  184.     keylen = 0;
  185.   }
  186.  
  187.   tcent = t_newconfent(tc);
  188.  
  189.   printf("\nGenerate a (n)ew field or use a (p)redefined field? [nP] ");
  190.   fgets(cbuf, sizeof(cbuf), stdin);
  191.   if(*cbuf != 'n' && *cbuf != 'N') {
  192.     for(i = 0; i < NPARAMS; ++i) {
  193.       tcent->modulus.len = t_fromb64(tcent->modulus.data,
  194.         pre_params[i].pre_mod);
  195.       printf("(%d) [%d bits]  %s\n    Modulus = %s\n  Generator = %s\n",
  196.              i + 1, 8 * tcent->modulus.len,
  197.              pre_params[i].comment ? pre_params[i].comment : "",
  198.              pre_params[i].pre_mod, pre_params[i].pre_gen);
  199.     }
  200.     printf("\nSelect a field (1-%d): ", NPARAMS);
  201.     fgets(cbuf, sizeof(cbuf), stdin);
  202.     i = atoi(cbuf);
  203.     if(i <= 0 || i > NPARAMS) {
  204.       fprintf(stderr, "Index not in range\n");
  205.       exit(1);
  206.     }
  207.     tcent->index = lastidx + 1;
  208.     tcent->modulus.len = t_fromb64(tcent->modulus.data,
  209.       pre_params[i - 1].pre_mod);
  210.     tcent->generator.len = t_fromb64(tcent->generator.data,
  211.       pre_params[i - 1].pre_gen);
  212.     t_putconfent(tcent, efp);
  213.     t_closeconf(tc);
  214.     fclose(efp);
  215.     printf("Configuration file updated.\n");
  216.     exit(0);
  217.   }
  218.  
  219.   printf("\nEnter the new field size, in bits.  Suggested sizes:\n\n");
  220.   printf(" %3d (minimum, testing only)\n", MIN_BASIS_BITS);
  221.   printf(" 384 (low security, but fast)\n");
  222.   printf(" 512 (reasonable default)\n");
  223.   printf(" 768 (better security)\n");
  224.   printf("1024 (PGP-level security)\n");
  225.   printf("1536 (extremely secure, possibly slow)\n");
  226.   printf("2048 (maximum supported security level)\n");
  227.   printf("\nField size (%d to %d): ", MIN_BASIS_BITS, BASIS_BITS);
  228.  
  229.   fgets(cbuf, sizeof(cbuf), stdin);
  230.   fsize = atoi(cbuf);
  231.   if(fsize < MIN_BASIS_BITS || fsize > BASIS_BITS) {
  232.     fprintf(stderr, "%s: field size must be between %d and %d\n",
  233.             progName, MIN_BASIS_BITS, BASIS_BITS);
  234.     exit(1);
  235.   }
  236.  
  237.   if(fsize <= keylen)
  238.     fprintf(stderr, "Warning: new field size is not larger than old field size\n");
  239.  
  240.   printf("\nInitializing random number generator...");
  241.   fflush(stdout);
  242.   t_stronginitrand();
  243.  
  244.   if(composite)
  245.     printf("done.\n\nGenerating a %d-bit composite with safe prime factors.  This may take a while.\n", fsize);
  246.   else
  247.     printf("done.\n\nGenerating a %d-bit safe prime.  This may take a while.\n", fsize);
  248.  
  249.   while(1) {
  250.     while((tcent = (composite ? t_makeconfent_c(tc, fsize) :
  251.                                 t_makeconfent(tc, fsize))) == NULL)
  252.       printf("Parameter generation failed, retrying...\n");
  253.     tcent->index = lastidx + 1;
  254.  
  255.     printf("\nParameters successfully generated.\n");
  256.     printf("N = [%s]\n", t_tob64(b64buf,
  257.                                  tcent->modulus.data, tcent->modulus.len));
  258.     printf("g = [%s]\n", t_tob64(b64buf,
  259.                                  tcent->generator.data, tcent->generator.len));
  260.     printf("\nUpdate the configuration file with these parameters? [Ynq] ");
  261.  
  262.     fgets(cbuf, sizeof(cbuf), stdin);
  263.     switch(*cbuf) {
  264.     case 'q':
  265.     case 'Q':
  266.       fclose(efp);
  267.       exit(0);
  268.     case 'n':
  269.     case 'N':
  270.       printf("\nGenerating another set of parameters, please wait...\n");
  271.       break;
  272.     default:
  273.       t_putconfent(tcent, efp);
  274.       t_closeconf(tc);
  275.       fclose(efp);
  276.       printf("Configuration file updated.\n");
  277.       exit(0);
  278.     }
  279.   }
  280. }
  281.