Skip to content

Commit 9bd0b9c

Browse files
authored
Merge pull request #4 from IBM/dev_Robin
Dynamic Key Pair Creation and Hex Calculation
2 parents 4f8f926 + bb08f01 commit 9bd0b9c

File tree

5 files changed

+78
-54
lines changed

5 files changed

+78
-54
lines changed

.github/workflows/.workflowTest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
run: |
2929
cd icc
3030
make OPSYS=AMD64_LINUX CONFIG=debug create_all
31+
export LD_LIBRARY_PATH=/home/runner/work/OpenCryptographyKitC/OpenCryptographyKitC/openssl-1.1.1/
3132
make -k OPSYS=AMD64_LINUX CONFIG=debug all
3233
make -k OPSYS=AMD64_LINUX CONFIG=debug tests
3334
make -k OPSYS=AMD64_LINUX CONFIG=debug show_config
@@ -43,6 +44,7 @@ jobs:
4344
run: |
4445
cd icc
4546
make -k OPSYS=AMD64_LINUX CONFIG=release create_all
47+
export LD_LIBRARY_PATH=/home/runner/work/OpenCryptographyKitC/OpenCryptographyKitC/openssl-1.1.1/
4648
make -k OPSYS=AMD64_LINUX CONFIG=release all
4749
make -k OPSYS=AMD64_LINUX CONFIG=release iccpkg
4850
make -k OPSYS=AMD64_LINUX CONFIG=release show_config

icc/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ tmp/tmp/dummyfile: Build_OSSL_Complete tmp/dummyfile
429429
#- Split this into 2 phases for the OS/X fat binaries work - resolves circular dependencies
430430
# this is target for icclib085 shared library
431431

432-
$(ICCDLL_NAME): icclib$(OBJSUFX) $(LIBOBJS) $(STLPRFX)zlib$(STLSUFX) tmp/tmp/dummyfile signer$(EXESUFX) tracer.h extsig.h
432+
$(ICCDLL_NAME): privkey.rsa icclib$(OBJSUFX) $(LIBOBJS) $(STLPRFX)zlib$(STLSUFX) tmp/tmp/dummyfile signer$(EXESUFX) tracer.h extsig.h
433433
$(SLD) $(SLDFLAGS) $(ICCLIB_LNK) $(EXPORT_FLAG)$(ICCLIB_EXPFILE) icclib$(OBJSUFX) $(LIBOBJS) $(STLPRFX)zlib$(STLSUFX) \
434434
tmp/tmp/*$(OBJSUFX) $(LDLIBS)
435435
#- Unstripped goes into NOSHIP
@@ -590,6 +590,15 @@ nist_algs$(EXESUFX): nist_algs1$(OBJSUFX)
590590
#=============================== Code sign/verify ==============================
591591

592592
#- stand alone signing tool
593+
privkey.rsa:
594+
$(ICC_ROOT)/openssl-1.1.1/apps/openssl genrsa -out privkey.rsa 2048
595+
596+
pubkey.h: privkey.rsa
597+
$(ICC_ROOT)/openssl-1.1.1/apps/openssl rsa -in privkey.rsa -outform DER -RSAPublicKey_out > rsa_pub_key.der
598+
echo "/*This is an auto generated code please DO NOT modify*/" > pubkey.h
599+
perl bin2hex.pl rsa_pub_key.der temp.h
600+
cat temp.h >> pubkey.h
601+
rm temp.h rsa_pub_key.der
593602

594603
signer$(OBJSUFX): extsig.c
595604
$(CC) -DSTANDALONE -DOPSYS=\"$(OPSYS)\" $(CFLAGS) -I$(OSSLINC_DIR) extsig.c $(OUT)$@
@@ -612,7 +621,7 @@ status$(OBJSUFX): status.c status.h icclib.h
612621
$(CC) $(CFLAGS) -I$(SDK_DIR) -I$(OSSLINC_DIR) -I$(OSSL_DIR) -I$(API_DIR) status.c
613622

614623
#- Build ICC FIPS code
615-
fips$(OBJSUFX): fips.c fips.h icclib.h iccerr.h $(PRNG_DIR)/fips-prng-RAND.h tracer.h
624+
fips$(OBJSUFX): pubkey.h fips.c fips.h icclib.h iccerr.h $(PRNG_DIR)/fips-prng-RAND.h tracer.h
616625
$(CC) $(CFLAGS) -I./ -I$(SDK_DIR) -I$(OSSLINC_DIR) -I$(OSSL_DIR) -I$(API_DIR) fips.c
617626

618627
#- Compile the FIPS prng code

icc/bin2hex.pl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#/*****Note- This Script is coping the effect of xxd -i command**********/
2+
#!/usr/bin/perl
3+
use strict;
4+
use warnings;
5+
6+
if (@ARGV != 2) {
7+
die "Usage: $0 input_binary_file output_text_file\n";
8+
}
9+
10+
my ($input_file, $output_file) = @ARGV;
11+
my ($input_file_name) = $input_file =~ /^(.*?)\.[^.]*$/;
12+
13+
open my $input_fh, '<:raw', $input_file or die "Cannot open input file '$input_file': $!\n";
14+
open my $output_fh, '>', $output_file or die "Cannot open output file '$output_file': $!\n";
15+
16+
print $output_fh "static const unsigned char $input_file_name\[] = {\n\t";
17+
18+
my $n = 0;
19+
while (read $input_fh, my $buffer, 1) {
20+
my $hex_byte = unpack 'H2', $buffer;
21+
if ($n > 0 ) {
22+
print $output_fh ",";
23+
}
24+
if ($n > 0 && ($n % 16) == 0) {
25+
print $output_fh "\n\t";
26+
}
27+
print $output_fh "0x", $hex_byte;
28+
$n = $n + 1;
29+
}
30+
print $output_fh "\n};\n";
31+
print $output_fh "unsigned $input_file_name\_size = ", $n, ";\n";
32+
33+
close $input_fh;
34+
close $output_fh;

icc/extsig.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
151151
const EVP_MD *md) {
152152
size_t len = 0;
153153
long amt = 0;
154+
int rc = 0;
154155

155156
if (NULL != fin) {
156157
if (0 == pos) {
@@ -159,7 +160,10 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
159160
}
160161
fseek(fin, 0, SEEK_SET);
161162
EVP_MD_CTX_cleanup(md_ctx);
162-
EVP_DigestInit(md_ctx, md);
163+
rc = EVP_DigestInit(md_ctx, md);
164+
if (1 != rc) {
165+
printf("HashCore:EVP_DigestInit failed %d\n", rc);
166+
}
163167
/* Work out how much to read */
164168
while (pos > 0) {
165169
amt = sizeof(fbuf);
@@ -168,10 +172,14 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
168172
}
169173
len = fread(fbuf, 1, amt, fin);
170174
if (len > 0) {
171-
EVP_DigestUpdate(md_ctx, fbuf, len);
175+
rc = EVP_DigestUpdate(md_ctx, fbuf, len);
176+
if (1 != rc) {
177+
printf("HashCore:EVP_DigestUpdate failed %d\n", rc);
178+
}
172179
pos -= (long)len;
173180
} else {
174-
break;
181+
printf("HashCore:fread failed\n");
182+
break;
175183
}
176184
}
177185
}
@@ -622,12 +630,19 @@ static int GenSig(FILE *fin, unsigned char *sigout, EVP_PKEY *key, long pos) {
622630
HashCore(fin, pos, md_ctx, md);
623631
evpRC = EVP_SignFinal(md_ctx, sigout, &signL, key);
624632
if (1 != evpRC) {
625-
signL = 0;
633+
printf("GenSig: EVP_SignFinal error %d\n", evpRC);
634+
signL = 0;
626635
}
627636
EVP_MD_CTX_free(md_ctx);
628637
}
638+
else {
639+
printf("GenSig: EVP error\n");
640+
}
629641
fseek(fin, pos, SEEK_SET);
630642
}
643+
else {
644+
printf("GenSig: fin error\n");
645+
}
631646
return (int)signL;
632647
}
633648
static void usage(char *pname, char *str) {

icc/fips.c

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
#include "TRNG/nist_algs.h"
2323
#include "tracer.h"
2424

25+
/** @brief this is the public key that complements the private key */
26+
/* used to sign the modules within the cryptographic */
27+
/* boundary at build time */
28+
/* The private key at this time is stored in a file */
29+
/* named icc/privkey.rsa */
30+
/* \known Data: (rsa_pub_key) RSA public key used to verify
31+
the ICC shared library signatures
32+
*/
33+
#include "pubkey.h"
34+
2535

2636
#if defined(_WIN32)
2737
#include <fcntl.h>
@@ -826,52 +836,6 @@ static const unsigned char cmac_ka[] = {
826836
0xbd,0x4b,0xf2,0x8d,0x8c,0x37,0xc3,0x5c
827837
};
828838

829-
/** @brief this is the public key that complements the private key */
830-
/* used to sign the modules within the cryptographic */
831-
/* boundary at build time */
832-
/* The private key at this time is stored in a file */
833-
/* named icc/privkey.rsa */
834-
/** \known Data: (rsa_pub_key_DER) RSA public key used to verify
835-
the ICC shared library signatures
836-
*/
837-
static const unsigned char rsa_pub_key_DER[] =
838-
{
839-
0x30,0x82,0x01,0x0A,0x02,0x82,0x01,0x01,
840-
0x00,0xD5,0x61,0x38,0x36,0x15,0x97,0xAD,
841-
0xDC,0x6D,0x4D,0x44,0x8E,0x06,0x45,0x36,
842-
0xB4,0x23,0x1A,0x69,0x82,0xD8,0xFE,0x9E,
843-
0xF0,0x74,0x45,0x87,0xE1,0xE3,0x7F,0x58,
844-
0x6D,0xA2,0x42,0xBB,0xAF,0xC6,0xC8,0x41,
845-
0xBE,0xCA,0x50,0x1D,0x02,0x96,0xFC,0xAB,
846-
0x66,0xDB,0xD5,0xE9,0x78,0x48,0x87,0x55,
847-
0x68,0xF6,0x2B,0x7A,0xD3,0x18,0xDA,0xC7,
848-
0x36,0xD0,0xD2,0x14,0xF8,0x93,0x2B,0x21,
849-
0x5E,0x7A,0x95,0xAC,0xB6,0x76,0x9A,0xFE,
850-
0x6C,0x31,0x4C,0x6D,0xC8,0xC7,0xDE,0x53,
851-
0x53,0xB5,0x98,0x87,0x2F,0x9D,0x96,0x6B,
852-
0x21,0xC0,0x83,0x6E,0xFC,0x13,0x39,0x28,
853-
0x9B,0xBB,0x76,0xC7,0xF6,0x4E,0xDE,0x69,
854-
0xCE,0xE0,0x4B,0x35,0x4D,0xC5,0xB9,0xE6,
855-
0x96,0xDD,0xD1,0xB4,0x1C,0xE9,0xDC,0xE3,
856-
0x7A,0x9C,0x74,0x19,0x71,0xCD,0xFE,0xF7,
857-
0xEA,0x4E,0xC5,0x5E,0x7C,0xE7,0xA7,0xE7,
858-
0x09,0x8C,0xB6,0xA8,0xC2,0x9F,0x1B,0xAE,
859-
0x8C,0x22,0x08,0x05,0xF2,0xA4,0x53,0xA3,
860-
0x03,0x83,0x4E,0x36,0x99,0x0D,0x86,0xC2,
861-
0x00,0xE4,0xDF,0x82,0x29,0x88,0xA6,0x99,
862-
0x1C,0x36,0x80,0xF5,0xD7,0x88,0x99,0x0A,
863-
0x6F,0xB6,0x37,0xFF,0x99,0x87,0xF1,0x6C,
864-
0x7B,0x3F,0x63,0x94,0x5A,0x42,0x56,0xF6,
865-
0x34,0x80,0xC5,0x22,0x04,0x89,0x50,0x9F,
866-
0x36,0x6A,0x58,0x49,0xB4,0xF0,0x65,0x01,
867-
0xF8,0x69,0xD9,0x76,0x0E,0x9D,0x5F,0xAF,
868-
0xB5,0xFE,0x0D,0xE0,0xF8,0x41,0xB6,0x80,
869-
0xE5,0xA2,0x02,0xD0,0x8D,0xC6,0xAB,0xEE,
870-
0xAE,0x53,0x8C,0xBC,0x1E,0x97,0x4F,0xEE,
871-
0x9C,0xF3,0x47,0x08,0xDE,0x8C,0xB1,0xD8,
872-
0x1B,0x02,0x03,0x01,0x00,0x01
873-
};
874-
875839
#if defined(KNOWN)
876840
/** \known Data. OpenSSL curve name corresponding to NIST B-233
877841
for binary field KAT
@@ -5099,14 +5063,14 @@ EVP_PKEY *get_pubkey(ICC_STATUS *stat) {
50995063
SetStatusMem(NULL, stat, __FILE__, __LINE__);
51005064
} else {
51015065
const unsigned char *p1 = NULL;
5102-
p1 = (unsigned char *)rsa_pub_key_DER;
5066+
p1 = (unsigned char *)rsa_pub_key;
51035067
/** \induced 157. Signature test, corrupt DER encoding
51045068
*/
51055069
if (157 == icc_failure) {
51065070
p1++;
51075071
}
51085072
rsaPKey = d2i_PublicKey(EVP_PKEY_RSA, &rsaPKey, (const unsigned char **)&p1,
5109-
sizeof(rsa_pub_key_DER));
5073+
sizeof(rsa_pub_key));
51105074

51115075
/** \induced 153. iccSignature test. Couldn't convert embedded key to
51125076
binary representation. This should never happen as if OpenSSL isn't

0 commit comments

Comments
 (0)