RSA BSAFE Crypto-C

Cryptographic Components for C

Search

dsasign.c

/* $Id: dsasign.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 a DSA keypair, sign with the private
 *  key, then verify with the public key using AI_DSAWithSHA1.
 */

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

B_ALGORITHM_METHOD *DSA_CHOOSER[] = {
  &AM_DSA_PARAM_GEN,
  &AM_DSA_KEY_GEN,
  &AM_DSA_SIGN,
  &AM_DSA_VERIFY,
  &AM_SHA,
  (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
};



#define DSA_KEY_BITS   1024
#define SIGNATURE_LEN  ((DSA_KEY_BITS + 7) / 8)
#define MAX_SIG_LEN 48

#ifdef CRYPTOC_APP
#define MAIN dsasignMain
#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 dsaParamGenerator = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ dsaKeyGenObj = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ dsaSigner = (B_ALGORITHM_OBJ)NULL_PTR;
  B_ALGORITHM_OBJ dsaVerifier = (B_ALGORITHM_OBJ)NULL_PTR;

  B_KEY_OBJ dsaPublicKey = (B_KEY_OBJ)NULL_PTR;
  B_KEY_OBJ dsaPrivateKey = (B_KEY_OBJ)NULL_PTR;

  B_DSA_PARAM_GEN_PARAMS dsaParams;

  unsigned char inputData[] = {
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  };
  unsigned int inputDataLen = sizeof(inputData);
  unsigned char signature[MAX_SIG_LEN];
  unsigned int signatureLen;

  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 ("DSA With SHA1 Digital Signature\n");
    RSA_PrintMessage ("===============================\n");

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

    RSA_PrintMessage ("\n  Generating a DSA Keypair\n");
    RSA_PrintMessage ("  ========================\n");

    /* Create the algorithm and key objects. */
    if ((status = B_CreateAlgorithmObject (&dsaParamGenerator)) != 0)
      break;
    if ((status = B_CreateAlgorithmObject (&dsaKeyGenObj)) != 0)
      break;
    if ((status = B_CreateKeyObject (&dsaPublicKey)) != 0)
      break;
    if ((status = B_CreateKeyObject (&dsaPrivateKey)) != 0)
      break;

    /* Set the algorithm info. */
    dsaParams.primeBits = DSA_KEY_BITS;
    if ((status = B_SetAlgorithmInfo (dsaParamGenerator, AI_DSAParamGen,
                                      (POINTER)&dsaParams)) != 0)
      break;

    /* Init. */
    if ((status = B_GenerateInit (dsaParamGenerator, DSA_CHOOSER,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Generate. */
    if ((status = B_GenerateParameters (dsaParamGenerator, dsaKeyGenObj,
                                        randomAlgorithm,
                                        (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Init. */
    if ((status = B_GenerateInit (dsaKeyGenObj, DSA_CHOOSER,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Generate. */
    if ((status = B_GenerateKeypair (dsaKeyGenObj, dsaPublicKey,
                                     dsaPrivateKey, randomAlgorithm,
                                     (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    RSA_PrintMessage ("\n  Signing with DSA \n");
    RSA_PrintMessage ("  ================ \n");

    RSA_PrintBuf ("Data To Sign", inputData, inputDataLen);

    /* Create. */
    if ((status = B_CreateAlgorithmObject (&dsaSigner)) != 0)
      break;

    /* Set. */
    if ((status = B_SetAlgorithmInfo (dsaSigner, AI_DSAWithSHA1,
                                      NULL_PTR)) != 0)
      break;

    /* Init. */
    if ((status = B_SignInit (dsaSigner, dsaPrivateKey, DSA_CHOOSER,
                              (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Update. */
    if ((status = B_SignUpdate (dsaSigner, inputData, inputDataLen,
                                (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Final. */
    if ((status = B_SignFinal (dsaSigner, signature, &signatureLen,
                               MAX_SIG_LEN, randomAlgorithm,
                               (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    RSA_PrintBuf ("Signature", signature, signatureLen);

    RSA_PrintMessage ("\n  Verifying with DSA\n");
    RSA_PrintMessage ("  ==================\n");

    /* Create. */
    if ((status = B_CreateAlgorithmObject (&dsaVerifier)) != 0)
      break;

    /* Set. */
    if ((status = B_SetAlgorithmInfo (dsaVerifier, AI_DSAWithSHA1,
                                      NULL_PTR)) != 0)
      break;

    /* Init. */
    if ((status = B_VerifyInit (dsaVerifier, dsaPublicKey, DSA_CHOOSER,
                                (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Update. */
    if ((status = B_VerifyUpdate (dsaVerifier, inputData, inputDataLen,
                                  (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;

    /* Final. */
    if ((status = B_VerifyFinal (dsaVerifier, signature, signatureLen,
                                 (B_ALGORITHM_OBJ)NULL_PTR,
                                 (A_SURRENDER_CTX *)NULL_PTR)) != 0)
      break;
  } while (0);

  if (status != 0)
    RSA_PrintError ("dsasign", status);
  else
    RSA_PrintMessage ("\nDSA With SHA1 Digital Signature verified.\n");

  /* Destroy all key and algorithm objects. */
  B_DestroyAlgorithmObject (&randomAlgorithm);
  B_DestroyAlgorithmObject (&dsaParamGenerator);
  B_DestroyAlgorithmObject (&dsaKeyGenObj);
  B_DestroyAlgorithmObject (&dsaSigner);
  B_DestroyAlgorithmObject (&dsaVerifier);
  B_DestroyKeyObject (&dsaPublicKey);
  B_DestroyKeyObject (&dsaPrivateKey);

  return (status);
}

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