ibm-information-center/dist/eclipse/plugins/i5OS.ic.apis_5.4.0.1/qc3SetupCusILEC.htm

313 lines
17 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
<title>Example in ILE C: Setting up keys</title>
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<!-- Change History: -->
<!-- 050629 HAG New -->
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
</head>
<body>
<!-- Java sync-link-->
<script type="text/javascript" language="Javascript" src="../rzahg/synch.js">
</script>
<h2><img src="delta.gif" alt="Start of change">
Example in ILE C: Setting up keys</h2>
<p>See <a href="../apiref/aboutapis.htm #codedisclaimer">Code disclaimer information</a>
for information pertaining to code examples.</p>
<p>Refer to <a href="qc3Scenario.htm">Scenario: Key Management and File Encryption Using the Cryptographic Services APIs</a> for a description of this scenario.</p>
<pre>
/*-------------------------------------------------------------------*/
/* */
/* Sample C program: Setup_Cus */
/* */
/* COPYRIGHT 5722-SS1 (c) IBM Corp 2006 */
/* */
/* 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 programs. All programs contained herein are */
/* provided to you &quot;AS IS&quot;. THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* EXPRESSLY DISCLAIMED. IBM provides no program services for */
/* these programs and files. */
/* */
/* Description: */
/* This is a sample program to demonstrate use of the Cryptographic */
/* Services APIs. APIs demonstrated in this program are: */
/* Create Key Store */
/* Generate Key Record */
/* Create Key Context */
/* Create Algorithm Context */
/* Generate Symmetric Key */
/* Destroy Key Context */
/* Destroy Algorithm Context */
/* */
/* Function: */
/* Create CUSDTA file for storing customer information */
/* Create CUSPI file for storing information needed to process */
/* CUSDTA file. */
/* Create key store file, CUSKEYFILE. */
/* Create a KEK in CUSKEYFILE with label CUSDTAKEK. */
/* Generate a key encrypted under CUSDTAKEK and store in CUSPI. */
/* */
/* Refer to the iSeries (TM) Information Center for a full */
/* description of this scenario. */
/* */
/* Use the following commands to compile this program: */
/* CRTCMOD MODULE(MY_LIB/SETUP_CUS) SRCFILE(MY_LIB/MY_SRC) */
/* CRTPGM PGM(MY_LIB/SETUP_CUS) MODULE(MY_LIB/SETUP_CUS) + */
/* BNDSRVPGM(QC3KSCRT QC3KRGEN QC3KEYGN QC3CTX) */
/* */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* Retrieve various structures/utilities. */
/*-------------------------------------------------------------------*/
#include &lt;stdio.h&gt; /* Standard I/O header */
#include &lt;stdlib.h&gt; /* General utilities */
#include &lt;stddef.h&gt; /* Standard definitions */
#include &lt;string.h&gt; /* String handling utilities */
#include &lt;recio.h&gt; /* Record I/O routines */
#include &lt;qusec.h&gt; /* Error code structure */
#include &lt;qc3kscrt.h&gt; /* Hdr for Create Key Store API*/
#include &lt;qc3krgen.h&gt; /* Hdr for Gen Key Record API */
#include &lt;qc3keygn.h&gt; /* Hdr for Generate Key APIs */
#include &lt;qc3ctx.h&gt; /* Hdr for Context APIs */
/*-------------------------------------------------------------------*/
/* The following structure was generated with GENCSRC. */
/*-------------------------------------------------------------------*/
#ifdef __cplusplus
#include &lt;bcd.h&gt;
#else
#include &lt;decimal.h&gt;
#endif
/* ------------------------------------------------------- *
// PHYSICAL FILE : MY_LIB/CUSPI
// FILE LAST CHANGE DATE : 2004/02/11
// RECORD FORMAT : CUSPIREC
// FORMAT LEVEL IDENTIFIER : 248C15A88E09C
* ------------------------------------------------------- */
typedef _Packed struct {
char KEY[16]; /* ENCRYPTION KEY */
#ifndef __cplusplus
decimal( 8, 0) LASTCUS;
#else
_DecimalT&lt; 8, 0&gt; LASTCUS; /* LAST CUSTOMER NUMBER */
/* BCD class SPECIFIED IN DDS */
#endif
} CUSPIREC_both_t;
/*-------------------------------------------------------------------*/
/* Start of mainline code. */
/*-------------------------------------------------------------------*/
int main()
{
/*-------------------------------------------------------------------*/
/* Return codes */
/*-------------------------------------------------------------------*/
int rtn; /* Return code */
#define ERROR -1
#define OK 0
/*-------------------------------------------------------------------*/
/* File handling variables */
/*-------------------------------------------------------------------*/
_RFILE *cuspiPtr; /* Pointer to CUSPI file */
CUSPIREC_both_t cuspi; /* CUSPI record */
_RIOFB_T *db_fdbk; /* I/O Feedback */
/*-------------------------------------------------------------------*/
/* Parameters needed by the Cryptographic Services APIs */
/*-------------------------------------------------------------------*/
Qus_EC_t errCode; /* Error code structure */
char csp; /* Crypto service provider */
char ksauth[10]; /* Key store authority */
char ksdesc[50]; /* Key store description */
int mkid; /* Master key ID */
int disFunc; /* Disallowed function value */
int pubExp; /* Public key exponent */
int keyType; /* Key type */
int keySize; /* Key size */
char keyFormat; /* Key format */
char keyForm; /* Key form */
int keyStringLen; /* Length of key string */
Qc3_Format_KEYD0400_T kskey; /* Key store key name structure*/
char KEKctx[8]; /* KEK key context token */
Qc3_Format_ALGD0200_T algD; /* Block cipher alg description*/
char AESctx[8]; /* AES alg context token */
int keyLen; /* Length of generated key */
/*-------------------------------------------------------------------*/
/* Initializations */
/*-------------------------------------------------------------------*/
/* Init to good return */
rtn = OK;
/* Set to generate exceptions */
memset(&amp;errCode, 0, sizeof(errCode));
/* Use any crypto provider */
csp = Qc3_Any_CSP;
/*-------------------------------------------------------------------*/
/* Create file CUSDTA, used for storing customer information. */
/*-------------------------------------------------------------------*/
system(&quot;CRTPF FILE(MY_LIB/CUSDTA) &quot;
&quot;SRCFILE(MY_LIB/QDDSSRC) SRCMBR(CUSDTA) AUT(*EXCLUDE)&quot;);
/*-------------------------------------------------------------------*/
/* Create file CUSPI, used for processing file CUSDTA. */
/*-------------------------------------------------------------------*/
system(&quot;CRTPF FILE(MY_LIB/CUSPI) &quot;
&quot;SRCFILE(MY_LIB/QDDSSRC) SRCMBR(CUSPI) AUT(*EXCLUDE)&quot;);
/*-------------------------------------------------------------------*/
/* Create key store file, CUSKEYFILE, and generate a key record */
/* with label CUSDTAKEK. */
/*-------------------------------------------------------------------*/
memset(&amp;kskey, 0, sizeof(kskey)); /* Init name structure to null */
/* Create key store file CUSKEYFILE */
/* Init file name to blank */
memset(kskey.Key_Store, 0x40, sizeof(kskey.Key_Store));
/* Set file name */
memcpy(kskey.Key_Store,&quot;CUSKEYFILEMY_LIB&quot;, 16);
memcpy(ksauth, &quot;*EXCLUDE &quot;, 10); /* Set public auth to *EXCLUDE */
/* Set file text description */
memcpy(ksdesc, &quot;Key store for Customer data files, CUSDTA &amp; CUSPI &quot;, 50);
/* Set to use master key 3 to */
mkid = Qc3_Master_Key_3; /* encrypt key store keys */
/* Create key store file */
Qc3CreateKeyStore(kskey.Key_Store, &amp;mkid, ksauth, ksdesc, &amp;errCode);
/* Generate AES key record CUSDTAKEK */
/* Init record label to blank */
memset(kskey.Record_Label, 0x40, sizeof(kskey.Record_Label));
/* Set the record label */
memcpy(kskey.Record_Label, &quot;CUSDTAKEK&quot;, 9);
keyType = Qc3_AES; /* Key type is AES */
keySize = 16; /* Key size is 16 (128 bit) */
pubExp = 0; /* Ignore public key exponent */
disFunc=0; /* No disallowed functions */
/* Generate an AES key record */
Qc3GenKeyRecord(kskey.Key_Store, kskey.Record_Label, &amp;keyType, &amp;keySize,
&amp;pubExp, &amp;disFunc, &amp;csp, NULL, &amp;errCode);
/*-------------------------------------------------------------------*/
/* Create a key context for CUSDTAKEK. */
/*-------------------------------------------------------------------*/
keyStringLen = sizeof(kskey); /* Set length of key string */
keyFormat = Qc3_KSLabel_Struct; /* Key format is keystore label*/
keyForm = Qc3_Clear; /* Key string is clear */
/* Key type already set to AES */
/* Create key context */
Qc3CreateKeyContext((char*)&amp;kskey, &amp;keyStringLen, &amp;keyFormat,
&amp;keyType, &amp;keyForm, NULL, NULL, KEKctx, &amp;errCode);
/*-------------------------------------------------------------------*/
/* Create an AES algorithm context for CUSDTAKEK. */
/*-------------------------------------------------------------------*/
memset(&amp;algD, 0, sizeof(algD)); /* Init alg description to null*/
algD.Block_Cipher_Alg = Qc3_AES; /* Set AES algorithm */
algD.Block_Length = 16; /* Block size is 16 */
algD.Mode = Qc3_CBC; /* Use cipher block chaining */
algD.Pad_Option = Qc3_No_Pad; /* Do not pad */
/* Create algorithm context */
Qc3CreateAlgorithmContext((unsigned char*)&amp;algD, Qc3_Alg_Block_Cipher,
AESctx, &amp;errCode);
/*-------------------------------------------------------------------*/
/* Generate a file key encrypted under CUSDTAKEK. */
/*-------------------------------------------------------------------*/
keyFormat = Qc3_Bin_String; /* Return a binary string */
keyForm = Qc3_Encrypted; /* Encrypt generated key */
keyLen = 16; /* Receiver is 16 bytes long */
/* Key type already set to AES */
/* Key size already set to 16 */
/* Generate encrypted key */
Qc3GenSymmetricKey(&amp;keyType, &amp;keySize, &amp;keyFormat, &amp;keyForm,
(unsigned char*)&amp;KEKctx, (unsigned char*)&amp;AESctx,
&amp;csp, NULL, cuspi.KEY, &amp;keyLen, &amp;keyLen, &amp;errCode);
/*-------------------------------------------------------------------*/
/* Write record with encrypted file key to CUSPI. */
/*-------------------------------------------------------------------*/
cuspi.LASTCUS = 0; /* Set last customer num to 0 */
/* Open CUSDTA file */
if ((cuspiPtr = _Ropen(&quot;MY_LIB/CUSPI&quot;, &quot;wr, riofb=N&quot;))
== NULL)
{ /* If null ptr returned */
/* Send error message */
printf(&quot;Open of CUSPI file failed.&quot;);
return ERROR;
}
/* Write encrypted key and */
/* last customer number to file*/
db_fdbk = _Rwrite(cuspiPtr, &amp;cuspi, sizeof(cuspi));
if (db_fdbk->num_bytes &lt; sizeof(cuspi))
{ /* If data not written */
/* Send error message */
printf(&quot;Error writing to file CUSPI.&quot;);
_Rclose(cuspiPtr); /* Close CUSPI file */
return ERROR; /* Return error */
}
/*-------------------------------------------------------------------*/
/* Cleanup. */
/*-------------------------------------------------------------------*/
/* Wipe out encrypted file key */
memset(cuspi.KEY, 0, sizeof(cuspi.KEY));
/* Destroy KEK context */
Qc3DestroyKeyContext(KEKctx, &amp;errCode);
/* Destroy AES alg context */
Qc3DestroyAlgorithmContext(AESctx, &amp;errCode);
/* Close CUSPI file */
_Rclose(cuspiPtr);
/* Return */
return rtn;
}
</pre>
<p><img src="deltaend.gif" alt="End of change"></p>
<br>
<hr>
<center>
<table cellpadding="2" cellspacing="2">
<tr align="center">
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> |
<a href= "catcrypt.htm">Cryptographic Services APIs</a> |<a href=
"aplist.htm">APIs by category</a></td>
</tr>
</table>
</center>
</body>
</html>