Skip to content

Commit 83b16fb

Browse files
committed
Minor code changes
Signed-off-by: Robin Dubey <[email protected]>
1 parent fa3d63a commit 83b16fb

File tree

7 files changed

+212
-141
lines changed

7 files changed

+212
-141
lines changed

icc/extsig.c

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ Equivalent environment variable (none)
7474
#include "openssl/evp.h"
7575
#include "openssl/rsa.h"
7676

77-
7877
#include "extsig.h"
7978
#include "iccversion.h"
8079
# if !defined(STANDALONE)
@@ -150,7 +149,6 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
150149
const EVP_MD *md) {
151150
size_t len = 0;
152151
long amt = 0;
153-
int rc = 0;
154152

155153
if (NULL != fin) {
156154
if (0 == pos) {
@@ -159,10 +157,7 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
159157
}
160158
fseek(fin, 0, SEEK_SET);
161159
EVP_MD_CTX_cleanup(md_ctx);
162-
rc = EVP_DigestInit(md_ctx, md);
163-
if (1 != rc) {
164-
printf("HashCore:EVP_DigestInit failed %d\n", rc);
165-
}
160+
EVP_DigestInit(md_ctx, md);
166161
/* Work out how much to read */
167162
while (pos > 0) {
168163
amt = sizeof(fbuf);
@@ -171,14 +166,14 @@ static long HashCore(FILE *fin, long pos, EVP_MD_CTX *md_ctx,
171166
}
172167
len = fread(fbuf, 1, amt, fin);
173168
if (len > 0) {
169+
int rc = 0;
174170
rc = EVP_DigestUpdate(md_ctx, fbuf, len);
175-
if (1 != rc) {
176-
printf("HashCore:EVP_DigestUpdate failed %d\n", rc);
171+
if (rc <= 0) {
172+
return -1;
177173
}
178174
pos -= (long)len;
179175
} else {
180-
printf("HashCore:fread failed\n");
181-
break;
176+
break;
182177
}
183178
}
184179
}
@@ -210,14 +205,26 @@ static int GenHash(FILE *fin, unsigned char *hashout, long pos) {
210205
md = EVP_get_digestbyname("SHA256");
211206
if (NULL != md_ctx && NULL != md) {
212207
pos = HashCore(fin, pos, md_ctx, md);
213-
/* printf("Unread %ld\n",pos); */
208+
if (pos > 0) {
209+
printf("Error: GenHash: HashCore: Unread %ld\n", pos);
210+
return 0;
211+
}
212+
else if (pos < 0) {
213+
printf("Error: GenHash: HashCore\n");
214+
return 0;
215+
}
214216
evpRC = EVP_DigestFinal(md_ctx, hashout, &signL);
215217
if (1 != evpRC) {
216218
signL = 0;
219+
printf("Error: GenHash: failed: EVP_DigestFinal %d\n", evpRC);
217220
}
218221
EVP_MD_CTX_cleanup(md_ctx);
219222
EVP_MD_CTX_free(md_ctx);
220223
}
224+
else {
225+
const char* x = md_ctx ? "md" : "md_ctx";
226+
printf("Error: GenHash: failed: EVP_get_digestbyname %s\n", x);
227+
}
221228
}
222229
return (int)signL;
223230
}
@@ -626,24 +633,31 @@ static int GenSig(FILE *fin, unsigned char *sigout, EVP_PKEY *key, long pos) {
626633
md_ctx = EVP_MD_CTX_new();
627634
md = EVP_get_digestbyname("SHA256");
628635
if (NULL != md_ctx && NULL != md) {
629-
HashCore(fin, pos, md_ctx, md);
636+
long unread = HashCore(fin, pos, md_ctx, md);
637+
if (unread > 0) {
638+
printf("Error: GenSig: HashCore: Unread %ld\n", unread);
639+
return 0;
640+
}
641+
else if (unread < 0) {
642+
printf("Error: GenSig: HashCore\n");
643+
return 0;
644+
}
630645
evpRC = EVP_SignFinal(md_ctx, sigout, &signL, key);
631646
if (1 != evpRC) {
632-
printf("GenSig: EVP_SignFinal error %d\n", evpRC);
647+
printf("failed: GenSig: EVP_SignFinal %d\n", evpRC);
633648
signL = 0;
634649
}
635650
EVP_MD_CTX_free(md_ctx);
636651
}
637652
else {
638-
printf("GenSig: EVP error\n");
653+
const char* x = md_ctx ? "md" : "md_ctx";
654+
printf("failed: GenSig: EVP_get_digestbyname %s\n", x);
639655
}
640656
fseek(fin, pos, SEEK_SET);
641657
}
642-
else {
643-
printf("GenSig: fin error\n");
644-
}
645658
return (int)signL;
646659
}
660+
647661
static void usage(char *pname, char *str) {
648662
printf("usage:\t %s sigfile keyfile [-v(erify)] [-SELF] [-FILE file] "
649663
"[\"X=Y\"] ...[\"Z=K\"]\n",
@@ -701,9 +715,9 @@ int main(int argc, char *argv[]) {
701715
{
702716
int rc = 0;
703717
rc = OPENSSL_init_crypto(
704-
OPENSSL_INIT_NO_LOAD_CONFIG | OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
705-
OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_ADD_ALL_CIPHERS,
706-
NULL);
718+
OPENSSL_INIT_NO_LOAD_CONFIG | OPENSSL_INIT_LOAD_CRYPTO_STRINGS |
719+
OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_ADD_ALL_CIPHERS,
720+
NULL);
707721
if (rc != 1) {
708722
usage("OpenSSL", "OPENSSL_init_crypto");
709723
exit(1);
@@ -802,7 +816,7 @@ int main(int argc, char *argv[]) {
802816
/* At this point, we should have everything, start pushing it out */
803817
fprintf(sigf, "# IBM Crypto for C.%s", EOL);
804818
fprintf(sigf, "# ICC Version %d.%d.%d.%d%s", ICC_VERSION_VER,
805-
ICC_VERSION_REL, ICC_VERSION_MOD, ICC_VERSION_FIX, EOL);
819+
ICC_VERSION_REL, ICC_VERSION_MOD, ICC_VERSION_FIX, EOL);
806820
fprintf(sigf,
807821
"#%s# Note the signed library contains a copy of cryptographic "
808822
"code from OpenSSL (www.openssl.org),%s",
@@ -876,13 +890,13 @@ int main(int argc, char *argv[]) {
876890
}
877891
fflush(sigf);
878892
fprintf(sigf, "%s#Do not edit before this line%s#", EOL, EOL);
893+
fprintf(sigf, "%s# Global Settings%s", EOL, EOL);
879894
if (NULL != tweaks[0]) {
880-
fprintf(sigf, "%s# Global Settings%s", EOL, EOL);
881895
for (i = 0; NULL != tweaks[i]; i++) {
882896
fprintf(sigf, "%s%s", tweaks[i], EOL);
883897
}
884-
fprintf(sigf, "#%s", EOL);
885898
}
899+
fprintf(sigf, "#%s", EOL);
886900
}
887901
fseek(sigf, 0, SEEK_SET);
888902
fseek(bfile, 0, SEEK_SET);
@@ -906,13 +920,9 @@ int main(int argc, char *argv[]) {
906920
}
907921

908922
for (i = 0; i < MAXTWEAKS; i++) {
909-
if (NULL != tweaks[i]) {
910-
free(tweaks[i]);
911-
} else {
912-
break;
913-
}
923+
free(tweaks[i]);
914924
}
915-
printf("%d config items found\n", ReadConfigItems(sigf, tweaks, 20));
925+
printf("%d config items found\n", ReadConfigItems(sigf, tweaks, MAXTWEAKS));
916926

917927
fclose(sigf);
918928
fclose(bfile);
@@ -923,11 +933,7 @@ int main(int argc, char *argv[]) {
923933
}
924934

925935
for (i = 0; i < MAXTWEAKS; i++) {
926-
if (NULL != tweaks[i]) {
927-
free(tweaks[i]);
928-
} else {
929-
break;
930-
}
936+
free(tweaks[i]);
931937
}
932938
OPENSSL_cleanup();
933939
return 0;

icc/fips.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*************************************************************************
22
// Copyright IBM Corp. 2025
3-
//
3+
//
44
// Licensed under the Apache License 2.0 (the "License"). You may not use
55
// this file except in compliance with the License. You can obtain a copy
66
// in the file LICENSE in the source distribution.
@@ -10,7 +10,7 @@
1010
// Description:
1111
// The functions contained within implement operations to conform
1212
// to the FIPS 140-3 startup and self test for a cryptographic
13-
// module.
13+
// module.
1414
//
1515
*************************************************************************/
1616

@@ -525,21 +525,21 @@ static const unsigned char RSA_key[] =
525525
0x59, 0x33, 0xA7, 0xE9, 0x72, 0x9D, 0x7E, 0xC1};
526526

527527
static const unsigned char RSA_PKCS_sig[] = {
528-
0xAB, 0xBC, 0x2A, 0x22, 0xA1, 0xD1, 0xFC, 0x5D, 0x66, 0xB4, 0x4B, 0x42, 0xC8, 0xE2, 0x63, 0xE6,
529-
0xE8, 0x3D, 0x33, 0xB9, 0x0A, 0xDF, 0xA3, 0x38, 0x8B, 0x7C, 0x64, 0x0E, 0x34, 0x41, 0x60, 0xCB,
530-
0x37, 0xBC, 0xB0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 0x4F, 0xB3, 0x22, 0xB3, 0x89, 0xD1, 0xDE, 0x32, 0x97, 0x51, 0xAB, 0x28,
542-
0xF7, 0x6E, 0x37, 0xD1, 0xCE, 0x39, 0x53, 0xDA, 0x3D, 0x0E, 0x10, 0x56, 0x05, 0x02, 0x5B, 0xA3,
528+
0xAB,0xBC,0x2A,0x22,0xA1,0xD1,0xFC,0x5D,0x66,0xB4,0x4B,0x42,0xC8,0xE2,0x63,0xE6,
529+
0xE8,0x3D,0x33,0xB9,0x0A,0xDF,0xA3,0x38,0x8B,0x7C,0x64,0x0E,0x34,0x41,0x60,0xCB,
530+
0x37,0xBC,0xB0,0xB4,0x0D,0x15,0x2D,0x5B,0x09,0xEB,0x7F,0xD9,0x6C,0x70,0x0B,0xCE,
531+
0x62,0x13,0x3A,0xA0,0x7C,0x36,0x7C,0x48,0xC4,0x64,0x38,0xA4,0x98,0x83,0x1B,0x3C,
532+
0xA0,0x79,0x11,0xC4,0x3A,0xE1,0x54,0xD2,0xD8,0xF8,0xF7,0x95,0x2D,0x29,0xA8,0x98,
533+
0x1B,0x56,0x89,0x2E,0xAE,0x41,0x06,0x2C,0xFD,0x6F,0xA0,0x05,0xA5,0xCE,0xD5,0xC3,
534+
0xCB,0xC4,0xA1,0x4F,0x85,0xA8,0xA9,0xF3,0x45,0x1E,0x28,0xCA,0x1D,0xCA,0xFF,0x81,
535+
0xEE,0x02,0x2E,0x82,0xBD,0x8F,0x6E,0x55,0x23,0x04,0x01,0x1E,0xCA,0x86,0xC6,0x55,
536+
0x06,0xEC,0x44,0x91,0x42,0x35,0x74,0xBF,0x6E,0x95,0x25,0xEF,0x53,0xD5,0x0C,0x7A,
537+
0xC5,0x92,0x31,0xB5,0xC3,0x70,0xF8,0x55,0x91,0x29,0xA6,0xBA,0x83,0x5B,0x34,0x33,
538+
0x9E,0x26,0x2E,0x51,0x15,0x74,0x95,0x2B,0x5E,0xBF,0xDA,0x86,0x10,0xC1,0xAA,0x7B,
539+
0x8C,0xBF,0xFA,0x63,0x2D,0xFA,0x4D,0x6C,0x17,0x0C,0x13,0xCF,0x08,0xB8,0x81,0x7C,
540+
0x7C,0x5E,0x96,0xF1,0x3D,0x72,0x82,0xD8,0xB4,0x30,0xCA,0x58,0x9A,0x54,0x48,0x1E,
541+
0x2C,0x2D,0x15,0x1A,0x4F,0xB3,0x22,0xB3,0x89,0xD1,0xDE,0x32,0x97,0x51,0xAB,0x28,
542+
0xF7,0x6E,0x37,0xD1,0xCE,0x39,0x53,0xDA,0x3D,0x0E,0x10,0x56,0x05,0x02,0x5B,0xA3,
543543
0xFE,0xA1,0x0E,0xF7,0x15,0x68,0x28,0x73,0xBB,0x20,0xA0,0xA2,0x33,0x30,0x8F,0x0C,
544544
};
545545
static const unsigned char RSA_PSS_sig[] = {
@@ -4148,23 +4148,23 @@ static int DoVeryBrokenTests(ICClib *pcb, ICC_STATUS *stat)
41484148

41494149
#if defined(KNOWN)
41504150
printf("\nKnown answers with a broken RNG\n\n");
4151+
printf("RSA PKCS1.5\n");
4152+
iccGenerateRSASig(stat,RSA_key,sizeof(RSA_key),RSA_PKCS1_PADDING);
4153+
printf("RSA-PSS\n");
4154+
iccGenerateRSASig(stat,RSA_key,sizeof(RSA_key),RSA_PKCS1_PSS_PADDING);
41514155
printf("EC_key_P384\n");
41524156
iccGenerateECDSASig(stat,EC_key_P384,sizeof(EC_key_P384),0,"P-384");
41534157
printf("EC_key_B233\n");
41544158
iccGenerateECDSASig(stat,EC_key_B233,sizeof(EC_key_B233),0,"B-233");
41554159
printf("EC_key_K233\n");
41564160
iccGenerateECDSASig(stat,EC_key_K233,sizeof(EC_key_K233),0,"K-233");
4157-
printf("EC_key_X448\n");
4158-
iccGenerateECDSASig(stat,EC_key_X448,sizeof(EC_key_X448),0,"X448");
4159-
printf("EC_key_X25519\n");
4160-
iccGenerateECDSASig(stat,EC_key_X25519,sizeof(EC_key_X448),0,"X25519");
4161+
// printf("EC_key_X448\n");
4162+
// iccGenerateECDSASig(stat,EC_key_X448,sizeof(EC_key_X448),0,"X448");
4163+
// printf("EC_key_X25519\n");
4164+
// iccGenerateECDSASig(stat,EC_key_X25519,sizeof(EC_key_X448),0,"X25519");
41614165

41624166
printf("DSA_key\n");
41634167
iccGenerateDSASig(stat,DSA_key,sizeof(DSA_key));
4164-
/*
4165-
printf("RSA_key, PSS, SHA256\n");
4166-
iccGenerateRSASig(stat,RSA_key,sizeof(RSA_key),RSA_PKCS1_PSS_PADDING);
4167-
*/
41684168
printf("\nEnd known answers with a broken RNG\n\n");
41694169
#endif
41704170

icc/functions.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#;
1414
#;
1515
#;
16-
#Comments start with an # and end with an ;
17-
#In fact, all statements must end with an ;
18-
# AND NO TABS ;
16+
# Comments start with an # and end with an ;
17+
# In fact, all statements must end with an ;
18+
# And no tabs ;
1919
#;
2020
# Namespacing;
2121
#;
@@ -795,8 +795,8 @@ OPENSSLPREFIX=;
795795
#! @param callback Feedback for a progress indicator. See the OpenSSL docs. Typically, set this to NULL.;
796796
#! @param cb_arg Information to be passed to the callback method when it is called. Typically, set this to NULL;
797797
#! @return an pointer to a newly allocated RSA structure containing both the public and private RSA keys or NULL on failure;
798-
#! @note The callback and cb_arg parameters should be set to NULL for ICC consumers. ;
799-
#! While in theory these paramaters could be used we can envisage no ICC consumer scenario ;
798+
#! @note The callback and cb_arg parameters should be set to NULL in IBM applications. ;
799+
#! While in theory these paramaters could be used we can envisage no IBM application scenario ;
800800
#! where it would be useful to set these to non-NULL values ;
801801

802802
0abcdEPMC RSA * RSA_generate_key(int bits, unsigned long e,void (*callback)(int,int,void *),void *cb_arg);
@@ -2119,24 +2119,24 @@ OPENSSLPREFIX=;
21192119
#! @note All the aad wanted must be supplied before any data is supplied, but both aad and data can;
21202120
#! be supplied in segments;
21212121

2122-
0abcdE int AES_GCM_EncryptUpdate(AES_GCM_CTX *aes_gcm_ctx,unsigned char *aad, unsigned long aadlen,unsigned char *data,unsigned long datalen,unsigned char *out, unsigned long *outlen);
2122+
0abcdE int AES_GCM_EncryptUpdate(AES_GCM_CTX *aes_gcm_ctx,const unsigned char *aad, unsigned long aadlen, const unsigned char *data, unsigned long datalen, unsigned char *out, unsigned long *outlen);
21232123

21242124
#;
2125-
#! @brief Update phase of a AES_GCM encrypt operation;
2125+
#! @brief Update phase of a AES_GCM Decrypt operation;
21262126
#! @param aes_gcm_ctx a pointer to a AES_GCM_CTX;
21272127
#! @param aad a pointer to Additional Authentication Data to hash;
21282128
#! @param aadlen the length of the aad 0 <= aadlen <= 2^56 bytes TOTAL - not per call;
2129-
#! @param data a pointer to the data to encrypt and hash ;
2129+
#! @param data a pointer to the ciphertext data to decrypt and authenticate ;
21302130
#! @param datalen the length of the data 0 <= datalen <= 2^56 bytes TOTAL - not per call ;
2131-
#! @param out a pointer to a place to hold up to one block of residual data from the previous update ;
2131+
#! @param out a pointer to a buffer to receive decrypted plaintext. May contain up to one block of residual data from the previous update ;
21322132
#! @param outlen a place to store the length of any returned data ;
21332133
#! @return ICC_OSSL_SUCCESS on success, ICC_FAILURE on failure;
21342134
#! @note blocked/aligned data will be more efficient, but this will;
21352135
#! survive incorrectly blocked/misaligned aad/data;
21362136
#! @note All the aad wanted must be supplied before any data is supplied, but both aad and data can;
21372137
#! be supplied in segments;
21382138

2139-
0abcdE int AES_GCM_DecryptUpdate(AES_GCM_CTX *aes_gcm_ctx,unsigned char *aad, unsigned long aadlen,unsigned char *data,unsigned long datalen,unsigned char *out, unsigned long *outlen);
2139+
0abcdE int AES_GCM_DecryptUpdate(AES_GCM_CTX *aes_gcm_ctx, const unsigned char *aad, unsigned long aadlen, const unsigned char *data, unsigned long datalen, unsigned char *out, unsigned long *outlen);
21402140

21412141
#;
21422142
#! @brief Finish a AES_GCM encrypt operation and return any remaining ciphertext and the auth tag;
@@ -2216,7 +2216,7 @@ OPENSSLPREFIX=;
22162216
#! @note AES_CCM is (by specification and design) a one shot algorithm;
22172217
#! you have to feed everything into this one call;
22182218

2219-
0abcdEP int AES_CCM_Encrypt(unsigned char *nonce,unsigned int nlen, unsigned char *key,unsigned int keylen,unsigned char *aad, unsigned long aadlen,unsigned char *data,unsigned long datalen,unsigned char *out, unsigned long *outlen,unsigned int taglen);
2219+
0abcdEP int AES_CCM_Encrypt(const unsigned char *nonce,unsigned int nlen, const unsigned char *key,unsigned int keylen,const unsigned char *aad, unsigned long aadlen,const unsigned char *data,unsigned long datalen,unsigned char *out, unsigned long *outlen,unsigned int taglen);
22202220

22212221
#;
22222222
#! @brief Perform an AES CCM Decrypt operation,;
@@ -2246,7 +2246,7 @@ OPENSSLPREFIX=;
22462246
#! @note datalen in this call INCLUDES the length of the tag generated ;
22472247
#! by the corresponding Encrypt call;
22482248

2249-
0abcdEP int AES_CCM_Decrypt(unsigned char *nonce,unsigned int nlen,unsigned char *key, unsigned int keylen, unsigned char *aad, unsigned long aadlen, unsigned char *data, unsigned long datalen, unsigned char *out, unsigned long *outlen, unsigned int taglen);
2249+
0abcdEP int AES_CCM_Decrypt(const unsigned char *nonce,unsigned int nlen,const unsigned char *key, unsigned int keylen, const unsigned char *aad, unsigned long aadlen, const unsigned char *data, unsigned long datalen, unsigned char *out, unsigned long *outlen, unsigned int taglen);
22502250

22512251
#;
22522252
#! @brief Get an ICC RNG handle;

0 commit comments

Comments
 (0)