RSA BSAFE Crypto-C

Cryptographic Components for C

Search

dhparam.c

/* $Id: dhparam.c,v 1.7 2004/12/03 02:08:37 sparki Exp $ */
/*
 * Copyright (C) 1998-2004 RSA Security Inc.
 *
 * This file shall only be used to demonstrate how to interface to an
 * RSA Security Inc. licensed development product.
 *
 * You have a royalty-free right to use, reproduce and distribute this
 * demonstration file, provided that you agree that RSA Security Inc.
 * has no warranty, implied or otherwise, or liability for this
 * demonstration file (including any modified version).  This software
 * is provided "as is" without warranties or representations of any
 * kind. RSA Security disclaims all conditions and warranties, statutory
 * and otherwise, both express and implied, with respect to the software,
 * its quality and performance, including but not limited to, all
 * implied warranties of merchantability, fitness for a particular
 * purpose, title and noninfringement of third party rights. Without
 * limiting the foregoing, RSA Security does not warrant that the
 * software is error-free or that errors in the product will be
 * corrected. You agree that RSA Security shall not be liable for any
 * direct, indirect, incidental, special, consequential, punitive or
 * other damages whatsoever resulting from your use of this software
 * or any modified version.
 *
 *
 */

/*  This program will generate Diffie-Hellman parameters needed for
 *   the key agreement (see dhagree.c).
 */

#include "bsafe.h"
#include "demoutil.h"  /* in samples/common/include */
#include "bsfutil.h"   /* in samples/common/include */
#include "surrctx.h"   /* in samples/common/include */

B_ALGORITHM_METHOD *DH_SAMPLE_CHOOSER[] = {
  &AM_DH_PARAM_GEN,
  (B_ALGORITHM_METHOD *)NULL_PTR
/* This will fix a problem that the IA64 compiler finds when *
 * seeing a short chooser list */
#ifdef IA64_FORCE_LARGE
                IA64_FORCE_LARGE
#endif
};



#ifdef CRYPTOC_APP
#define MAIN dhparamMain
#else
#define MAIN main
#endif

int MAIN(int argc, char *argv[])
{
  int status;

  B_ALGORITHM_OBJ randomAlgorithm = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ dhParamGenerator = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ dhParametersObj = (B_ALGORITHM_OBJ)NULL_PTR;

  A_DH_PARAM_GEN_PARAMS dhParams;

  A_SURRENDER_CTX generalSurrenderContext;

  ITEM *cryptocDHParametersBER;
  ITEM myDHParametersBER = {NULL_PTR, 0};

  unsigned int generalFlag;

  /* see samples/common/source/surrctx.c for RSA_GeneralSurrenderFunction */
  generalSurrenderContext.Surrender = RSA_GeneralSurrenderFunction;
  generalSurrenderContext.handle = (POINTER)&generalFlag;
  generalSurrenderContext.reserved = NULL_PTR;

  do {
    /* The RSA_* demo code utilities are described in
       common/include/demoutil.h.  This procedure simply checks the
       command-line arguments for input or output options. */
    if ((status = RSA_SetOptions (argc, argv)) != 0)
      break;

    RSA_PrintMessage ("Diffie-Hellman Algorithm\n");
    RSA_PrintMessage ("========================\n");

    if ((status = RSA_CreateRandomAlgorithmObject (&randomAlgorithm)) != 0)
      break;

    RSA_PrintMessage ("\n   Generating DH parameters -- will take awhile\n");
    RSA_PrintMessage ("   ============================================\n");

    /*  Create algorithm object */
    if ((status = B_CreateAlgorithmObject (&dhParamGenerator)) != 0)
      break;

    /*  Set algorithm object to AI_DHParamGen
     */
    dhParams.primeBits = 512;
    dhParams.exponentBits = 504;
    if ((status = B_SetAlgorithmInfo (dhParamGenerator, AI_DHParamGen,
                                      (POINTER)&dhParams)) != 0)
      break;

    /*  Init */
    if ((status = B_GenerateInit (dhParamGenerator, DH_SAMPLE_CHOOSER,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /*  Note that there is no Update used in generating DH parameters */

    /*  Generate DH parameters
     */
    if ((status = B_CreateAlgorithmObject (&dhParametersObj)) != 0)
      break;

    /*  generalFlag is for the surrender function. */
    generalFlag = 0;
    if ((status = B_GenerateParameters (dhParamGenerator, dhParametersObj,
                                        randomAlgorithm,
                                        &generalSurrenderContext)) != 0)
      break;

    RSA_PrintMessage ("\n   Distributing DH parameters\n");
    RSA_PrintMessage ("   ==========================\n");

    if ((status = B_GetAlgorithmInfo ((POINTER *)&cryptocDHParametersBER,
                                      dhParametersObj, AI_DHKeyAgreeBER)) != 0)
      break;

    myDHParametersBER.len = cryptocDHParametersBER->len;
    myDHParametersBER.data = T_malloc (myDHParametersBER.len);
    if (myDHParametersBER.data == NULL_PTR) {
      status = RSA_DEMO_E_ALLOC;
      break;
    }

    T_memcpy (myDHParametersBER.data, cryptocDHParametersBER->data,
              myDHParametersBER.len);

    RSA_PrintBuf ("DH Parameters", myDHParametersBER.data,
                  myDHParametersBER.len);
  } while (0);

  if (status != 0)
    RSA_PrintError ("dhparam", status);

  /*  Destroy the algorithm objects  */
  B_DestroyAlgorithmObject (&randomAlgorithm);
  B_DestroyAlgorithmObject (&dhParametersObj);
  B_DestroyAlgorithmObject (&dhParamGenerator);

  /*  Free up any memory allocated  */
  if (myDHParametersBER.data != NULL_PTR)
    T_free (myDHParametersBER.data);

  return (status);
} /*  end main  */

Copyright (c) 1999-2005 RSA Security Inc. All rights reserved. 068-001001-6210-001-000 - 6.2.1