Example: ILE C program for loading a master key into your Cryptographic Coprocessor

Change this program example to suit your needs for loading a new master key into your Cryptographic Coprocessor.

Note: Read the Code license and disclaimer information for important legal information.

If you choose to use this program example, change it to suit your specific needs. For security reasons, IBM® recommends that you individualize these program examples rather than using the default values provided.

/*-------------------------------------------------------------------*/
/* Load a new master key on the  card.                           */
/*                                                                   */
/*                                                                   */
/*  COPYRIGHT 5769-SS1, 5722-SS1 (C) IBM CORP. 1999, 2000            */
/*                                                                   */
/*  This material contains programming source code for your          */
/*  consideration.  These examples have not been thoroughly          */
/*  tested under all conditions.  IBM, therefore, cannot             */
/*  guarantee or imply reliability, serviceability, or function      */
/*  of these program.  All programs contained herein are             */
/*  provided to you "AS IS".  THE IMPLIED WARRANTIES OF              */
/*  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE         */
/*  ARE EXPRESSLY DISCLAIMED.  IBM provides no program services for  */
/*  these programs and files.                                        */
/*                                                                   */
/*                                                                   */
/*                                                                   */
/* Parameters:                                                       */
/*   OPTION     (FIRST, MIDDLE, LAST, CLEAR, SET)                    */
/*   KEYPART    (24 bytes entered in hex ->  X'01F7C4....')          */
/*              Required for FIRST, MIDDLE, and LAST                 */
/*                                                                   */
/* Example:                                                          */
/*   CALL PGM(LOAD_KM)                                               */
/*     (FIRST X'0123456789ABCDEFFEDCBA98765432100123456789ABCDEF')   */
/*                                                                   */
/* Note: This program assumes the device to use is                   */
/*       already identified either by defaulting to the CRP01        */
/*       device or by being explicitly named using the               */
/*       Cryptographic_Resource_Allocate verb. Also this             */
/*       device must be varied on and you must be authorized         */
/*       to use this device description.                             */
/*                                                                   */
/*                                                                   */
/* Use these commands to compile this program on the system:         */
/* ADDLIBLE LIB(QCCA)                                                */
/* CRTCMOD MODULE(LOAD_KM) SRCFILE(SAMPLE)                           */
/* CRTPGM  PGM(LOAD_KM) MODULE(LOAD_KM)                              */
/*         BNDSRVPGM(QCCA/CSNBMKP QCCA/CSNBRNG)                      */
/*                                                                   */
/* Note: Authority to the CSNBMKP and CSNBRNG service programs       */
/*       in the QCCA library is assumed.                             */
/*                                                                   */
/* The main Common Cryptographic Architecture (CCA) verb used        */
/* is Master_Key_Process (CSNBMKP).                                  */
/*                                                                   */
/*-------------------------------------------------------------------*/


#include "csucincl.h"      /* header file for CCA Cryptographic      */
                           /* Service Provider                       */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*-------------------------------------------------------------------*/
/* standard return codes                                             */
/*-------------------------------------------------------------------*/

#define ERROR    -1
#define OK        0
#define WARNING   4

int main(int argc, char *argv[])

{
    /*-------------------------------------------------------------------*/
    /* standard CCA parameters                                           */
    /*-------------------------------------------------------------------*/
    long return_code = 0;
    long reason_code = 0;
    long exit_data_length = 2;
    char exit_data[4];
    char rule_array[2][8];
    long rule_array_count = 1;

    /*-------------------------------------------------------------------*/
    /* parameters unique to this program                                 */
    /*-------------------------------------------------------------------*/
    char keypart[24];              /* Dummy parm for SET and CLEAR */

    /*-------------------------------------------------------------------*/
    /* Process the parameters                                            */
    /*-------------------------------------------------------------------*/
    if (argc < 2)
     {
      printf("Option parameter must be specified.\n");
      return(ERROR);
     }

    if (argc < 3 && memcmp(argv[1],"CLEAR",5) != 0 &&
        memcmp(argv[1],"SET",3) != 0)
     {
      printf("KeyPart parameter must be specified.\n");
      return(ERROR);
     }


    /*-------------------------------------------------------------------*/
    /* Set the keywords in the rule array                                */
    /*-------------------------------------------------------------------*/
    memset(rule_array,' ',8);
    memcpy(rule_array,argv[1],
           (strlen(argv[1]) > 8) ? 8 : strlen(argv[1]));

    /*-------------------------------------------------------------------*/
    /* Call Master Key Process SAPI                                      */
    /*-------------------------------------------------------------------*/
    CSNBMKP( &return_code,
	     &reason_code,
	     &exit_data_length,
	     exit_data,
	     &rule_array_count,
	     (unsigned char *)rule_array,
	     (argc == 3) ? argv[2] : keypart);

    /*-------------------------------------------------------------------*/
    /* Check the return code and display the results                     */
    /*-------------------------------------------------------------------*/
    if ( (return_code == OK) | (return_code == WARNING) )
    {
    	printf("Request was successful with return/reason codes: %d/%d \n",
             return_code, reason_code);
    	return(OK);
    }
    else
    {
    	printf("Request failed with return/reason codes: %d/%d \n",
             return_code, reason_code);
    	return(ERROR);
    }

}
Related concepts
Setting a master key