Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import Key with even parity fails with Parity Exception #509

Open
mchhil-incomm opened this issue Jan 13, 2023 · 7 comments
Open

Import Key with even parity fails with Parity Exception #509

mchhil-incomm opened this issue Jan 13, 2023 · 7 comments

Comments

@mchhil-incomm
Copy link
Contributor

From jpos google group

https://groups.google.com/g/jpos-users/c/sa8LIIg4ZNQ/m/fFdIxt7rCwAJ

java  -cp bcprov-jdk18on-172.jar -jar /opt/jPOS/jpos/build/install/jpos/jpos-2.1.8-ST-q2.jar -c "smconsole  -jce org.bouncycastle.jce.provider.BouncyCastleProvider  -lmk  lmk.key IK 128 ZPK E61BCE2AF5508A0E8705BC05AE0BC68B 128 ZMK D48177CDD2ED9E43BB607D9BEE21B17F E7472E"

I was getting below error,

  "  <security-module-exception>

      Parity not adjusted

      org.jpos.security.jceadapter.JCEHandlerException: Parity not adjusted"

I am of the opinion, either have a parameter in IK command of the SMConsole to adjust the parity at input and log appropriate info when adjusted.

or

Change decryptDesKey to adjust parity and use it instead of throwing the exception.

public Key decryptDESKey(short keyLength, byte[] encryptedDESKey, Key encryptingKey, boolean checkParity)
throws JCEHandlerException {
byte[] clearKeyBytes = doCryptStuff(encryptedDESKey, encryptingKey, Cipher.DECRYPT_MODE);
if (checkParity && !Util.isDESParityAdjusted(clearKeyBytes)) {
throw new JCEHandlerException("Parity not adjusted");
}
return formDESKey(keyLength, clearKeyBytes);
}

encryptDeskey adjusts the parity and uses the adjusted cryptogram.

public byte[] encryptDESKey(short keyLength, Key clearDESKey, Key encryptingKey) throws JCEHandlerException {
byte[] clearKeyBytes = extractDESKeyMaterial(keyLength, clearDESKey);
// enforce correct (odd) parity before encrypting the key
Util.adjustDESParity(clearKeyBytes);
return doCryptStuff(clearKeyBytes, encryptingKey, Cipher.ENCRYPT_MODE);
}

@ar
Copy link
Member

ar commented Jan 19, 2023

The if statement in the decrypt method honors a checkParity parameter. If we always adjust, we'd loose the ability to raise an exception when the input key material is not parity adjusted, which is usually an indication that the key might be not correct.

@mchhil-incomm
Copy link
Contributor Author

Checkparity is only set by code and is always true. If you are using the console one has no control over it.

Based on my limited knowledge, a Thales hsm will allow even parity keys and adjust it to odd parity.

@ar
Copy link
Member

ar commented Jan 20, 2023

I guess we could add a --no-check-parity parameter to SSM init CLI command.
We can also create an adjustparity command.

Having the ability to early detect bad keys is something I think we should keep.

@mchhil-incomm
Copy link
Contributor Author

I agree with you.
If the parity exception gets thrown, one has the ability to try and adjust the even parity keys.

@alcarraz
Copy link
Contributor

alcarraz commented Jan 21, 2023 via email

@mchhil-incomm
Copy link
Contributor Author

Adjusting an odd parity key has no impact and returns the same key. If the key before and after adjust parity are same, then that implies it was already odd else it was even.

Logging should add more clarity to what has happened and what has been done.

@alcarraz
Copy link
Contributor

alcarraz commented Jan 22, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants