Skip to content

Commit 48634f4

Browse files
author
smcmahonibm
committed
8.9.6 updates merged
Manual by merging diffs from 8.9.6 tag (branch RC_8.9.6) Signed-off-by: smcmahonibm <[email protected]>
1 parent 9c991b8 commit 48634f4

File tree

10 files changed

+180
-109
lines changed

10 files changed

+180
-109
lines changed

icc/TRNG/ICC_NRBG.c

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,29 @@ typedef struct NRBG_type_t {
6868
int initialized; /*!< Flag to say it was used */
6969
} NRBG_type;
7070

71+
72+
/* In non-FIPS, all platforms default to TRNG_OS and upgrade to TRNG_HW at runtime if available.
73+
This is done to prioritise compatibility on the unpredictable range and age of the virtualisatised systems we might run on,
74+
while still upgrading and using TRNG_HW in most cases.
75+
*/
76+
7177
#if (NON_FIPS_ICC == 1) /* Built as non-FIPS */
7278

73-
/* These definitions match the availability of OPENSSL_HW_rand */
79+
/* These definitions try mirror the availability of OPENSSL_HW_rand to avoid a mismatch (not relevant when we use TRNG_OS) */
7480
/* X86 Linux and Windows, Solaris x86 */
7581
#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
7682
defined(__INTEL__) || \
7783
defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) ) && ( !(defined(__SunOS) && !defined(__amd64)) \
7884
)
79-
80-
static TRNG_TYPE global_trng_type = TRNG_HW;
85+
static int global_trng_type_attempted_upgrade = 0;
86+
static TRNG_TYPE global_trng_type = TRNG_OS;
8187

8288
#elif defined(__s390__) || defined(__MVS__)
89+
static int global_trng_type_attempted_upgrade = 0;
90+
static TRNG_TYPE global_trng_type = TRNG_OS;
8391

84-
static TRNG_TYPE global_trng_type = TRNG_HW;
85-
86-
/* We will do a runtime check for cpu support for darn, present since ISA3.0, and update to TRNG_HW if so */
8792
#elif defined(__ppc__) || defined(__powerpc__) || defined(_AIX)
93+
/* We will do a runtime check for cpu support for darn, present since ISA3.0, and update to TRNG_HW if so */
8894
static int global_trng_type_attempted_upgrade = 0;
8995
static TRNG_TYPE global_trng_type = TRNG_OS;
9096

@@ -97,7 +103,7 @@ typedef struct NRBG_type_t {
97103
static TRNG_TYPE global_trng_type = TRNG_FIPS;
98104
#endif
99105

100-
/* If a user sets TRNG_OS on power, we don't want to upgrade even if HW is available */
106+
/* If a user explicitly sets a TRNG, we don't want to upgrade even if HW is available */
101107
int global_trng_type_user_set = 0;
102108

103109
static void TRNG_LocalCleanup(TRNG *T);
@@ -247,6 +253,7 @@ void checkTRNGAlias(char **trngname) {
247253
*trngname = "TRNG_OS";
248254
}
249255
}
256+
MARK("TRNG aliased to", *trngname);
250257
}
251258
}
252259
}
@@ -258,6 +265,12 @@ void checkTRNGAlias(char **trngname) {
258265
extern unsigned icc_failure; /*!< Trigger for induced failure tests */
259266
int SetTRNGName(char *trngname)
260267
{
268+
if (NULL != trngname) {
269+
MARK("Request to set TRNG to", trngname);
270+
}
271+
else {
272+
MARK("Request to set NULL TRNG", "");
273+
}
261274
int rv = 0;
262275
int i = 0;
263276
checkTRNGAlias(&trngname);
@@ -288,8 +301,11 @@ TRNG_TYPE SetDefaultTrng(TRNG_TYPE trng) {
288301
case TRNG_HW:
289302
case TRNG_FIPS:
290303
if(TRNG_ARRAY[trng].avail()) {
304+
MARK("TRNG set to", TRNG_ARRAY[trng].name);
291305
global_trng_type = trng;
292306
global_trng_type_user_set = 1;
307+
} else {
308+
MARK("TRNG attempted to be set to", TRNG_ARRAY[trng].name);
293309
}
294310
break;
295311
default:
@@ -397,12 +413,39 @@ static void TRNG_ESourceCleanup(E_SOURCE *es)
397413

398414
TRNG_TYPE GetDefaultTrng()
399415
{
400-
#if defined(__ppc__) || defined(__powerpc__) || defined(_AIX)
401-
if(!global_trng_type_attempted_upgrade && 0 == global_trng_type_user_set && 0 == strcasecmp("TRNG_OS", *trngname) && ALT4_Avail()) {
416+
#if (NON_FIPS_ICC == 1)
417+
418+
#if (\
419+
(( defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
420+
defined(__INTEL__) || \
421+
defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64)) && (!(defined(__SunOS) && !defined(__amd64)))) \
422+
|| \
423+
( defined(__s390__) || defined(__MVS__)) \
424+
|| \
425+
( defined(__ppc__) || defined(__powerpc__) || defined(_AIX)) \
426+
)
427+
if(!global_trng_type_attempted_upgrade) {
428+
MARK("Testing the availability of TRNG_HW", "");
429+
430+
if(0 == global_trng_type_user_set) {
431+
if (TRNG_FIPS != global_trng_type) {
432+
if (ALT4_Avail()) {
433+
MARK("Found, switching to TRNG_HW", "");
402434
global_trng_type = TRNG_HW;
435+
} else {
436+
MARK("TRNG_HW not available, remaining with", TRNG_ARRAY[global_trng_type].name);
437+
}
438+
} else {
439+
MARK("TRNG_FIPS set, remaining with", TRNG_ARRAY[global_trng_type].name);
440+
}
441+
} else {
442+
MARK("User TRNG set, remaining with", TRNG_ARRAY[global_trng_type].name);
403443
}
404444
global_trng_type_attempted_upgrade = 1;
405-
#endif
445+
}
446+
447+
#endif /*x86_64, z/architecture, power */
448+
#endif /*non-FIPS*/
406449
return global_trng_type;
407450
}
408451
/*!

icc/TRNG/TRNG_ALT.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040

4141

4242
static int fd_alt = -1;
43-
43+
#if defined(_WIN32)
44+
static BCRYPT_ALG_HANDLE hProvider = NULL;
45+
#endif
4446
/*! Pre-init function for TRNG_ALT
4547
4648
*/
@@ -89,10 +91,12 @@ static int alt_read(unsigned char *buffer,int n)
8991
break;
9092
case -3:
9193
#if defined(_WIN32)
94+
{
9295
NTSTATUS status = 0;
93-
status = BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, (PUCHAR)buffer, n, 0);
94-
if(status != STATUS_SUCCESS) {
95-
rv = TRNG_REQ_SIZE; /* One of the parameters was likely not correct */
96+
status = BCryptGenRandom(hProvider, (PUCHAR)buffer, n, 0);
97+
if(!BCRYPT_SUCCESS(status)) {
98+
rv = TRNG_REQ_SIZE; /* One of the parameters was likely not correct, or bad provider */
99+
}
96100
}
97101
#endif
98102
break;
@@ -124,18 +128,17 @@ TRNG_ERRORS ALT_Init(E_SOURCE *E, unsigned char *pers, int perl)
124128
/* Else probe for something else */
125129
if(-1 == fd_alt) {
126130
#if defined(_WIN32)
131+
{
127132
#define SIZE 8
128133
/* ON Windows ..... */
129134
/* If no HW RNG, OS RNG source */
130135
NTSTATUS status = 0;
131-
int tmpSize = SIZE; /* 64 bits, small test of availability */
132-
unsigned char tmp[SIZE];
133-
status = BCryptGenRandom(BCRYPT_RNG_ALG_HANDLE, (PUCHAR)&tmp, tmpSize, 0); /* Using a pseudo-handle */
134-
135-
if(status == STATUS_SUCCESS) {
136+
status = BCryptOpenAlgorithmProvider(&hProvider, BCRYPT_RNG_ALGORITHM, NULL, 0);
137+
if(BCRYPT_SUCCESS(status)) {
136138
fd_alt = -3;
137139
} else {
138-
rv = TRNG_INIT;
140+
rv = TRNG_INIT; /*error*/
141+
}
139142
}
140143
#else
141144
/* On Unix .... */
@@ -209,11 +212,15 @@ TRNG_ERRORS ALT_Cleanup(E_SOURCE *E)
209212

210213
void ALT_Final()
211214
{
212-
#if !defined(_WIN32)
215+
#if defined(_WIN32)
216+
if((-3 == fd_alt) && (0 != hProvider)) {
217+
BCryptCloseAlgorithmProvider(hProvider, 0);
218+
hProvider = 0;
219+
}
220+
#else
213221
if(fd_alt >= 0) {
214222
close(fd_alt);
215223
fd_alt = -1;
216224
}
217225
#endif
218-
219226
}

icc/TRNG/entropy_to_NRBG.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,13 @@ int conditioner(TRNG *T, unsigned char* outbuf, unsigned len)
6868
if( 0 != trng_raw(&(T->econd),tbuf,SHA_DIGEST_SIZE) ) {
6969
rv = SetRNGError("Insufficient entropy",__FILE__,__LINE__);
7070
if(TRNG_OK != rv) {
71-
break;
71+
HMAC_CTX_cleanup(T->cond.hctx);
72+
return rv;
7273
}
7374
}
7475
HMAC_Update(T->cond.hctx,tbuf,sizeof(tbuf));
7576
}
76-
if(TRNG_RESTART == rv) {
77-
TRNG_TRNG_Init(T,-1);
78-
rv = TRNG_OK;
79-
continue;
80-
}
77+
8178
HMAC_Final(T->cond.hctx,tbuf,&mlen);
8279

8380
for(i = 0; (i < mlen) && (n < len); ) {
@@ -120,7 +117,10 @@ TRNG_ERRORS Entropy_to_TRNG(TRNG *T, unsigned char *data, unsigned int len)
120117
{
121118
for (l = 0; l < len; l += SHA_DIGEST_SIZE)
122119
{
123-
conditioner(T, buffer, SHA_DIGEST_SIZE);
120+
rv = conditioner(T, buffer, SHA_DIGEST_SIZE);
121+
if (TRNG_OK != rv) {
122+
return rv;
123+
}
124124
e = pmax4(buffer,SHA_DIGEST_SIZE);
125125
if(e < 50) {
126126
break;
@@ -137,9 +137,8 @@ TRNG_ERRORS Entropy_to_TRNG(TRNG *T, unsigned char *data, unsigned int len)
137137
if (j >= TRNG_RETRIES)
138138
{
139139
rv = SetRNGError("Unable to obtain sufficient entropy", __FILE__, __LINE__);
140-
if(TRNG_OK == rv) {
141-
j = 0;
142-
continue;
140+
if(TRNG_OK != rv) {
141+
return rv;
143142
}
144143
}
145144
/* Final sanity check, we got out, is our overall entropy good with a compression function
@@ -149,7 +148,9 @@ TRNG_ERRORS Entropy_to_TRNG(TRNG *T, unsigned char *data, unsigned int len)
149148
if (!EntropyOK(T))
150149
{
151150
rv = SetRNGError("Long term entropy is below acceptable limits", __FILE__, __LINE__);
152-
if(TRNG_OK == rv) continue;
151+
if (TRNG_OK != rv) {
152+
return rv;
153+
}
153154
}
154155
/*!
155156
\FIPS
@@ -171,10 +172,10 @@ TRNG_ERRORS Entropy_to_TRNG(TRNG *T, unsigned char *data, unsigned int len)
171172
m++;
172173
if(m > 5) {
173174
rv = SetRNGError("Repeated duplicate seeds from TRNG", __FILE__, __LINE__);
174-
if(TRNG_OK == rv) {
175-
continue;
175+
if (TRNG_OK != rv) {
176+
EVP_MD_CTX_reset(T->md_ctx);
177+
return rv;
176178
}
177-
break;
178179
}
179180
continue;
180181
}

icc/TRNG/noise_to_entropy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ int trng_raw(E_SOURCE *E,
9090
/* 201 is a transient failure, 202 persistent */
9191
if((icc_failure == 201) || (icc_failure == 202)) {
9292
failcount = MAX_HT_FAIL +1;
93+
/* Pretend to clear the buffer so we goto error */
94+
k = 0;
95+
E->cnt = 0;
9396
}
9497
if(0 == k) {
9598
E->impl.gb(E,&(E->nbuf[0]), E_ESTB_BUFLEN);

icc/TRNG/timer_entropy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ ICC_UINT64 RdCTR_raw() {
131131
#elif defined(__sun__) && defined(__i386__) && defined(__GNUC__)
132132

133133
ICC_UINT64 RdCTR_raw() {
134-
ICC_UINT64 lo;
134+
ICC_UINT32 lo;
135135
__asm__ __volatile__("rdtsc\n" : "=a" (lo) : : "edx");
136-
return lo;
136+
return (ICC_UINT64)lo;
137137
}
138138

139139
/* End Solaris x86 */

icc/fips-prng/SP800-90.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ TRNG_ERRORS PRNG_GenerateRandomSeed(PRNG_CTX *P, unsigned int n,
332332
icc_failure = 0; /* Simulate a transient failure of a TRNG */
333333
}
334334
/* Try again, we should have changed the TRNG now */
335-
rv = TRNG_GenerateRandomSeed(prng->trng, n, buf);
335+
/* rv = TRNG_GenerateRandomSeed(prng->trng, n, buf); */
336336
if((TRNG_OK != rv) || (406 == icc_failure) ) {
337337
prng->state = SP800_90CRIT;
338338
prng->error_reason = ERRAT("TRNG failure, low entropy");
@@ -1326,6 +1326,8 @@ SP800_90STATE RNG_ReSeed(PRNG_CTX *ctx, unsigned char *adata,
13261326

13271327
/*
13281328
check that the global TRNG type hasn't changed
1329+
- And that we aren't the synthetic PRNG under TRNG_ALT2
1330+
which uses an assumed low entropy source (TRNG_MINIMAL) and an SP800_90 PRNG as a compressor
13291331
*/
13301332
type = TRNG_type(ictx->trng);
13311333
if (type != GetDefaultTrng())

icc/iccdef.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
// in the file LICENSE in the source distribution.
77
*************************************************************************/
88

9-
#if defined(WIN64)
9+
#if defined(_WIN32)
1010
#include "BaseTsd.h"
11+
#elif defined(__sun)
12+
#include <inttypes.h>
1113
#else
1214
#include <stdint.h>
1315
#endif
1416

1517

16-
#if defined(_WIN64)
18+
#if defined(_WIN32)
1719
#define ICC_INT32 INT32
1820
#define ICC_UINT32 UINT32
1921
#else
@@ -24,7 +26,7 @@
2426
/* Can't trust long, which is 4 bytes on windows, 8 on linux
2527
stdint.h should be available everywhere. */
2628

27-
#if defined(WIN64)
29+
#if defined(_WIN32)
2830
#define ICC_INT64 INT64
2931
#define ICC_UINT64 UINT64
3032
#else

0 commit comments

Comments
 (0)