Skip to content

Commit bb12e05

Browse files
Merge pull request #32369 from karel-harjono/fipsLtpaCrypto
update CryptoUtils generateRandomBytes to support hardware provider
2 parents 754eacd + a3bfff2 commit bb12e05

File tree

3 files changed

+20
-342
lines changed

3 files changed

+20
-342
lines changed

dev/com.ibm.ws.crypto.ltpakeyutil/src/com/ibm/ws/crypto/ltpakeyutil/LTPACrypto.java

Lines changed: 2 additions & 318 deletions
Original file line numberDiff line numberDiff line change
@@ -731,323 +731,6 @@ private static final synchronized void setIVS16(byte[] key) {
731731
ivs16 = new IvParameterSpec(iv16);
732732
}
733733

734-
@Trivial
735-
static final int lsbf(byte[] data, int i, int n) {
736-
int v = 0;
737-
do {
738-
v |= (data[i + (--n)] & 0xFF) << n * 8;
739-
} while (n > 0);
740-
return v;
741-
}
742-
743-
@Trivial
744-
static final int lsbf4(byte[] data, int i) {
745-
return (data[i] & 0xFF) | ((data[i + 1] & 0xFF) << 8) | ((data[i + 2] & 0xFF) << 16) | (data[i + 3] << 24);
746-
}
747-
748-
@Trivial
749-
static final void lsbf4(int v, byte[] data, int i) {
750-
data[i] = (byte) v;
751-
data[i + 1] = (byte) (v >>> 8);
752-
data[i + 2] = (byte) (v >>> 16);
753-
data[i + 3] = (byte) (v >>> 24);
754-
}
755-
756-
@Trivial
757-
static void lsbf2(int v, byte[] data, int i) {
758-
data[i] = (byte) v;
759-
data[i + 1] = (byte) (v >>> 8);
760-
}
761-
762-
@Trivial
763-
private static final int FF(int a, int b, int c, int d, int x, int l, int r, int ac) {
764-
return (((a += ((b & c) | (~b & d)) + x + ac) << l) | (a >>> r)) + b;
765-
}
766-
767-
@Trivial
768-
private static final int GG(int a, int b, int c, int d, int x, int l, int r, int ac) {
769-
return (((a += ((b & d) | (c & ~d)) + x + ac) << l) | (a >>> r)) + b;
770-
}
771-
772-
@Trivial
773-
private static final int HH(int a, int b, int c, int d, int x, int l, int r, int ac) {
774-
return (((a += (b ^ c ^ d) + x + ac) << l) | (a >>> r)) + b;
775-
}
776-
777-
@Trivial
778-
private static final int II(int a, int b, int c, int d, int x, int l, int r, int ac) {
779-
return (((a += (c ^ (b | ~d)) + x + ac) << l) | (a >>> r)) + b;
780-
}
781-
782-
@Trivial
783-
static final void md5(int[] state, byte[] data, int off, int len, byte[] to, int pos) {
784-
int a, b, c, d;
785-
{
786-
a = 0x67452301;
787-
b = 0xefcdab89;
788-
c = 0x98badcfe;
789-
d = 0x10325476;
790-
}
791-
792-
int W0, W1, W2, W3, W4, W5, W6, W7, W8, W9, W10, W11, W12, W13, W14, W15;
793-
int i, n = len / 4, a0, b0, c0, d0;
794-
final int[] W = new int[16];
795-
796-
boolean done = false;
797-
boolean padded = false;
798-
799-
do {
800-
801-
for (i = 0; (i < 16) && (n > 0); n--, off += 4)
802-
W[i++] = lsbf4(data, off);
803-
804-
if (i < 16) {
805-
806-
if (!padded) {
807-
W[i++] = ((a0 = len % 4) != 0) ? (lsbf(data, off, a0) | (0x80 << (a0 * 8))) : 0x80;
808-
if (i == 15)
809-
W[15] = 0;
810-
padded = true;
811-
}
812-
if (i <= 14) {
813-
while (i < 14)
814-
W[i++] = 0;
815-
816-
if (state != null)
817-
len += state[5];
818-
W[14] = len << 3;
819-
W[15] = len >>> 29;
820-
done = true;
821-
}
822-
}
823-
824-
a = FF((a0 = a), b, c, d, (W0 = W[0]), 7, 25, 0xd76aa478);
825-
d = FF((d0 = d), a, b, c, (W1 = W[1]), 12, 20, 0xe8c7b756);
826-
c = FF((c0 = c), d, a, b, (W2 = W[2]), 17, 15, 0x242070db);
827-
b = FF((b0 = b), c, d, a, (W3 = W[3]), 22, 10, 0xc1bdceee);
828-
a = FF(a, b, c, d, (W4 = W[4]), 7, 25, 0xf57c0faf);
829-
d = FF(d, a, b, c, (W5 = W[5]), 12, 20, 0x4787c62a);
830-
c = FF(c, d, a, b, (W6 = W[6]), 17, 15, 0xa8304613);
831-
b = FF(b, c, d, a, (W7 = W[7]), 22, 10, 0xfd469501);
832-
a = FF(a, b, c, d, (W8 = W[8]), 7, 25, 0x698098d8);
833-
d = FF(d, a, b, c, (W9 = W[9]), 12, 20, 0x8b44f7af);
834-
c = FF(c, d, a, b, (W10 = W[10]), 17, 15, 0xffff5bb1);
835-
b = FF(b, c, d, a, (W11 = W[11]), 22, 10, 0x895cd7be);
836-
a = FF(a, b, c, d, (W12 = W[12]), 7, 25, 0x6b901122);
837-
d = FF(d, a, b, c, (W13 = W[13]), 12, 20, 0xfd987193);
838-
c = FF(c, d, a, b, (W14 = W[14]), 17, 15, 0xa679438e);
839-
b = FF(b, c, d, a, (W15 = W[15]), 22, 10, 0x49b40821);
840-
841-
a = GG(a, b, c, d, W1, 5, 27, 0xf61e2562);
842-
d = GG(d, a, b, c, W6, 9, 23, 0xc040b340);
843-
c = GG(c, d, a, b, W11, 14, 18, 0x265e5a51);
844-
b = GG(b, c, d, a, W0, 20, 12, 0xe9b6c7aa);
845-
a = GG(a, b, c, d, W5, 5, 27, 0xd62f105d);
846-
d = GG(d, a, b, c, W10, 9, 23, 0x2441453);
847-
c = GG(c, d, a, b, W15, 14, 18, 0xd8a1e681);
848-
b = GG(b, c, d, a, W4, 20, 12, 0xe7d3fbc8);
849-
a = GG(a, b, c, d, W9, 5, 27, 0x21e1cde6);
850-
d = GG(d, a, b, c, W14, 9, 23, 0xc33707d6);
851-
c = GG(c, d, a, b, W3, 14, 18, 0xf4d50d87);
852-
b = GG(b, c, d, a, W8, 20, 12, 0x455a14ed);
853-
a = GG(a, b, c, d, W13, 5, 27, 0xa9e3e905);
854-
d = GG(d, a, b, c, W2, 9, 23, 0xfcefa3f8);
855-
c = GG(c, d, a, b, W7, 14, 18, 0x676f02d9);
856-
b = GG(b, c, d, a, W12, 20, 12, 0x8d2a4c8a);
857-
858-
a = HH(a, b, c, d, W5, 4, 28, 0xfffa3942);
859-
d = HH(d, a, b, c, W8, 11, 21, 0x8771f681);
860-
c = HH(c, d, a, b, W11, 16, 16, 0x6d9d6122);
861-
b = HH(b, c, d, a, W14, 23, 9, 0xfde5380c);
862-
a = HH(a, b, c, d, W1, 4, 28, 0xa4beea44);
863-
d = HH(d, a, b, c, W4, 11, 21, 0x4bdecfa9);
864-
c = HH(c, d, a, b, W7, 16, 16, 0xf6bb4b60);
865-
b = HH(b, c, d, a, W10, 23, 9, 0xbebfbc70);
866-
a = HH(a, b, c, d, W13, 4, 28, 0x289b7ec6);
867-
d = HH(d, a, b, c, W0, 11, 21, 0xeaa127fa);
868-
c = HH(c, d, a, b, W3, 16, 16, 0xd4ef3085);
869-
b = HH(b, c, d, a, W6, 23, 9, 0x4881d05);
870-
a = HH(a, b, c, d, W9, 4, 28, 0xd9d4d039);
871-
d = HH(d, a, b, c, W12, 11, 21, 0xe6db99e5);
872-
c = HH(c, d, a, b, W15, 16, 16, 0x1fa27cf8);
873-
b = HH(b, c, d, a, W2, 23, 9, 0xc4ac5665);
874-
875-
a = II(a, b, c, d, W0, 6, 26, 0xf4292244);
876-
d = II(d, a, b, c, W7, 10, 22, 0x432aff97);
877-
c = II(c, d, a, b, W14, 15, 17, 0xab9423a7);
878-
b = II(b, c, d, a, W5, 21, 11, 0xfc93a039);
879-
a = II(a, b, c, d, W12, 6, 26, 0x655b59c3);
880-
d = II(d, a, b, c, W3, 10, 22, 0x8f0ccc92);
881-
c = II(c, d, a, b, W10, 15, 17, 0xffeff47d);
882-
b = II(b, c, d, a, W1, 21, 11, 0x85845dd1);
883-
a = II(a, b, c, d, W8, 6, 26, 0x6fa87e4f);
884-
d = II(d, a, b, c, W15, 10, 22, 0xfe2ce6e0);
885-
c = II(c, d, a, b, W6, 15, 17, 0xa3014314);
886-
b = II(b, c, d, a, W13, 21, 11, 0x4e0811a1);
887-
a = II(a, b, c, d, W4, 6, 26, 0xf7537e82);
888-
d = II(d, a, b, c, W11, 10, 22, 0xbd3af235);
889-
c = II(c, d, a, b, W2, 15, 17, 0x2ad7d2bb);
890-
b = II(b, c, d, a, W9, 21, 11, 0xeb86d391) + b0;
891-
892-
a += a0;
893-
c += c0;
894-
d += d0;
895-
} while (!done);
896-
897-
{
898-
lsbf4(a, to, pos);
899-
lsbf4(b, to, pos + 4);
900-
lsbf4(c, to, pos + 8);
901-
lsbf4(d, to, pos + 12);
902-
}
903-
}
904-
905-
private static double[] ETB = new double[16];
906-
907-
static {
908-
double d = ETB[0] = 0.001;
909-
double log2d = Math.log(2 * d);
910-
int i = 1;
911-
do {
912-
ETB[i] = Math.exp(log2d / ++i) / 2;
913-
} while (i < ETB.length);
914-
}
915-
916-
private static int slot, channels;
917-
private static int[] samples = new int[56];
918-
private static int[] ones = new int[16];
919-
private static int[] block = new int[16];
920-
921-
@Trivial
922-
static final void trng(byte[] to, int off, int len) {
923-
long accu = 0;
924-
int bits = 0, i, m, j;
925-
926-
while (len-- > 0) {
927-
while (bits < 8) {
928-
929-
int s = 0;
930-
do {
931-
long t = System.currentTimeMillis();
932-
while (System.currentTimeMillis() == t)
933-
s++;
934-
} while (s == 0);
935-
936-
int xor = samples[slot] ^ s;
937-
samples[slot] = s;
938-
939-
i = 0;
940-
m = 1;
941-
do {
942-
943-
if ((xor & m) != 0) {
944-
ones[i] += (((s & m) != 0) ? 1 : -1);
945-
channels ^= m;
946-
}
947-
948-
if (--block[i] == 0) {
949-
accu = (accu << 1) | (((channels & m) != 0) ? 1 : 0);
950-
bits++;
951-
}
952-
if (block[i] <= 0) {
953-
954-
for (j = 0; j < 16; j++) {
955-
if (Math.abs(0.5 - (double) ones[i] / (double) 56) <= ETB[j])
956-
break;
957-
}
958-
959-
block[i] = (j == 16) ? -1 : j + 1;
960-
}
961-
m <<= 1;
962-
} while (++i < 16);
963-
slot = (slot + 1) % 56;
964-
}
965-
to[off++] = (byte) (accu >>> (bits -= 8));
966-
}
967-
}
968-
969-
private static byte[] seed = new byte[32];
970-
private static int ri;
971-
private static boolean seedInitialized = false;
972-
973-
static int trMix = 128;
974-
975-
static String[][] rsaKeyMaterial = {
976-
977-
{ "4svq2jqtxo3zn2njenso9vwyg2bynvo08ekktj4d7sqwk9s3oz",
978-
"4se994le3trmoep5f74ytxfupr2o0oi9dem4nzailb4k4g5e7j", "1ekh" },
979-
980-
{ "uk5febz1u9c5x7knn185refnb02syox36xqwae0lm30z9j9p03"
981-
+ "hyu175dyxbiczds3k1n6jiwqdeyetwgsy1qrvje8a7o40cmb5",
982-
"ujsuw3e4k53dtzgbsm3tjpytf5h25i71r8cs8ijbigo607ceo5"
983-
+ "zy5toem0kp4oeb77tt86h7gkix5fjdq13sa7puya61b2ep82n",
984-
"3" } };
985-
986-
static String[][] dsaKeyMaterial = {
987-
988-
{ "otj4bi3e6pxy54h5tkjwpuzycvm3ta6jg9f6lj52mvygb9l72y1tkrs0ppuldns6kem6vzw3fbwhinhdhpqjvn284fc0dsaz39h",
989-
"jpdh5mk2p667os7al4gmvbdfmar3bsv",
990-
"cdybrmm4x665tomdaiedafq3d2wiajhlkbeql7iui72eeayleaa3ppn7lhfdbrh508kum7havwgb7otsnme3pc8r7kipf55hvio",
991-
"lpb2xrb2yivmklm6i6pyzvagsu9qhdz",
992-
"6d3ng23juhszoxet3kkzw2ei7y3hxo67c9oqvuf5d1dpev7qzwhzy11tcaikknfxtr62zyk96d9vvhli6zw2b2sxbrnlc3xkuzy" },
993-
994-
{ "10uj5jh4khn7t93eh41c1d7sfptfuqiycpiimudbj62leu8fwnnt3k5cdkzynrvbhlflm3qe6sfwsjs3bbvjm8j8ctzaljlothj"
995-
+ "tbujclhafng31uzf4zmj11qjni0z9ou77rap19wl7ps7v52fbuoycrgu6xohwoobiwfanlkh4t18wtw3kf1nsdxz7mwpu9ddu4cz",
996-
"s6zmy3zi8dumvm43ofheresn52f9trj",
997-
"z4asx4yhsha3vd0d0uhhnahzmtj1qg572k3frvtq46x9lrawlm4x70oc99d4qsplci9e8qjtaqt3sqf719tfojrwjnonkqbxm9o"
998-
+ "p3ck61fcxx2q6l4vg1rizk9kn74pi9859nqqctvn9174smwqzosvdrnd89eykgocc09ph343gpen9lgo0h6dk32a35gut5wb6w1",
999-
"f8xedoxwqju60mngerxyt5jv7rl8wbg",
1000-
"egc8c7ptmx0hr5i4x2bzgeumx8kcmc9jokca88r8e4k1ih802bnz9flr08topo1v7kodqg9yab3xpf2j0lv9zmg8jhh38okgjfe"
1001-
+ "ou1fb7xn6blo4t1m8fb64p849eaqa66f1c0ar7m1uwdwc9k57vr58frxezjd1w4sc4zp8s6wn89lmbzem0brt6phtukhg2qfgrn" } };
1002-
1003-
static byte[][][] rsaKeys;
1004-
static byte[][][] dsaKeys;
1005-
1006-
@Trivial
1007-
static final void random(byte[] to, int off, int n) {
1008-
if (!seedInitialized) {
1009-
trng(seed, 0, 32);
1010-
md5(null, seed, 0, 32, seed, 0);
1011-
1012-
rsaKeys = new byte[4][][];
1013-
for (int i = 0, j = 0; i < rsaKeyMaterial.length; i++, j += 2) {
1014-
rsaKeys[j] = new byte[8][];
1015-
rsaKeys[j][2] = new BigInteger(rsaKeyMaterial[i][2], 36).toByteArray();
1016-
rsaKeys[j][3] = new BigInteger(rsaKeyMaterial[i][0], 36).toByteArray();
1017-
rsaKeys[j][4] = new BigInteger(rsaKeyMaterial[i][1], 36).toByteArray();
1018-
setRSAKey(rsaKeys[j]);
1019-
rsaKeys[j + 1] = new byte[][] { rsaKeys[j][0], rsaKeys[j][2] };
1020-
}
1021-
1022-
dsaKeys = new byte[4][4][];
1023-
for (int i = 0, j = 0; i < dsaKeyMaterial.length; i++, j += 2) {
1024-
1025-
for (int k = 0; k < 3; k++)
1026-
dsaKeys[j][k] = dsaKeys[j + 1][k] = new BigInteger(dsaKeyMaterial[i][k], 36).toByteArray();
1027-
dsaKeys[j][3] = new BigInteger(dsaKeyMaterial[i][3], 36).toByteArray();
1028-
dsaKeys[j + 1][3] = new BigInteger(dsaKeyMaterial[i][4], 36).toByteArray();
1029-
}
1030-
1031-
seedInitialized = true;
1032-
}
1033-
1034-
synchronized (seed) {
1035-
for (int i = 0; i < n; i++) {
1036-
int ri8 = ++ri % 8;
1037-
1038-
if (ri % trMix == 0) {
1039-
byte b = seed[ri8];
1040-
trng(seed, ri8, 1);
1041-
seed[ri8] ^= b;
1042-
}
1043-
1044-
if (ri8 == 0)
1045-
md5(null, seed, 0, 32, seed, 0);
1046-
1047-
to[off++] = seed[ri8];
1048-
}
1049-
}
1050-
}
1051734

1052735
@Trivial
1053736
static final byte[] generateSharedKey() {
@@ -1106,7 +789,8 @@ static final byte[][] rsaKey(int len, boolean crt, boolean f4) {
1106789
for (p = null;;) {
1107790
for (q = null;;) {
1108791
if (q == null) {
1109-
random(b, 1, len);
792+
byte[] seed = CryptoUtils.generateRandomBytes(len);
793+
System.arraycopy(seed, 0, b, 1, len);
1110794
b[1] |= 0xC0;
1111795
b[len] |= 1;
1112796
q = new BigInteger(b);

dev/com.ibm.ws.crypto.passwordutil/src/com/ibm/ws/crypto/util/PasswordCipherUtil.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ public class PasswordCipherUtil {
9191
static final String KEY_ENCRYPTION_SERVICE = "customPasswordEncryption";
9292
private static AtomicServiceReference<CustomPasswordEncryption> customPasswordEncryption = new AtomicServiceReference<CustomPasswordEncryption>(KEY_ENCRYPTION_SERVICE);
9393

94-
private static final String HW_PROVIDER = "IBMJCECCA";
95-
9694
private static CustomPasswordEncryption cpeImpl = null;
9795
private static List<CustomManifest> cms = null;
9896

@@ -544,20 +542,12 @@ else if (!!!alreadyLoggedHASHWeakPasswordAlgoWarning && usingSHA1) {
544542
*/
545543
private static EncryptedInfo aesEncipherV0(byte[] decrypted_bytes, String cryptoKey, EncryptedInfo info,
546544
byte[] encrypted_bytes) throws InvalidKeySpecException, InvalidPasswordCipherException, NoSuchAlgorithmException, UnsupportedCryptoAlgorithmException {
547-
byte[] seed = null;
548-
SecureRandom rand = new SecureRandom();
549-
Provider provider = rand.getProvider();
550-
String providerName = provider.getName();
551-
if (providerName.equals(HW_PROVIDER)) {
552-
seed = new byte[20];
553-
rand.nextBytes(seed);
554-
} else {
555-
seed = rand.generateSeed(20);
556-
}
557-
byte[] preEncrypted = new byte[decrypted_bytes.length + 21];
558-
preEncrypted[0] = 20; // how many seed bytes there are.
559-
System.arraycopy(seed, 0, preEncrypted, 1, 20);
560-
System.arraycopy(decrypted_bytes, 0, preEncrypted, 21, decrypted_bytes.length);
545+
byte seedSize = 20;
546+
byte[] seed = CryptoUtils.generateRandomBytes(seedSize);
547+
byte[] preEncrypted = new byte[decrypted_bytes.length + seedSize + 1];
548+
preEncrypted[0] = seedSize; // how many seed bytes there are.
549+
System.arraycopy(seed, 0, preEncrypted, 1, seedSize);
550+
System.arraycopy(decrypted_bytes, 0, preEncrypted, seedSize + 1, decrypted_bytes.length);
561551
try {
562552
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
563553
c.init(Cipher.ENCRYPT_MODE, AESKeyManager.getKey(cryptoKey), AESKeyManager.getIV(cryptoKey));
@@ -627,12 +617,7 @@ private static EncryptedInfo aesEncipherV1(byte[] decrypted_bytes,
627617

628618
byte seedSize = 64;
629619

630-
if (providerName.equals(HW_PROVIDER)) {
631-
seed = new byte[seedSize];
632-
rand.nextBytes(seed);
633-
} else {
634-
seed = rand.generateSeed(seedSize);
635-
}
620+
seed = CryptoUtils.generateRandomBytes(seedSize);
636621
byte[] preEncrypted = new byte[decrypted_bytes.length + seedSize + 1];
637622
preEncrypted[0] = seedSize; // how many seed bytes there are.
638623
System.arraycopy(seed, 0, preEncrypted, 1, seedSize);

0 commit comments

Comments
 (0)