-
Notifications
You must be signed in to change notification settings - Fork 0
Hpke #5
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
Merged
Hpke #5
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c28080d
version upgrades, java version to 25
hhund c8a57e3
dependency and plugin upgrades, java to 25, version to 6.0.0-SNAPSHOT
hhund 3c968e0
javadoc typo fix
hhund 0f157ef
RFC 9180 Hybrid Public Key Encryption implementation for mode 0 and 1
hhund 9492b7e
added missing export for hpke package
hhund 1c01b21
Renamed provider interfaces, new / fixed convenience methods, new tests
hhund 6718c47
fixed implementation, added javadoc and tests
hhund fff1702
refactored / simplified code, added test
hhund 71c14b6
remove not needed code
hhund f52016c
fixed iv generation error, refactored and improved code, new tests
hhund 2f5a047
more improvements, refactorings, missing throws, renamed test
hhund 0ed6504
cleanup
hhund ef1acb1
added test based on RFC 9180 test vectors
hhund 34d2182
code cleanup
hhund f4e5590
new RsaKemWrapper test, some code cleanup
hhund 49953c1
error handling mods, new tests for DhKemWrapper and AbstractKemWrapper
hhund 0ecab9b
refactorings, redesigns and additional tests
hhund 4fa1bff
removed no longer needed parameters
hhund ca25886
added missing Override annotations
hhund 7dd5a0d
added chunk length exponent to key-schedule info
hhund 8c686b5
removed not needed test method parameters
hhund 31138dc
some refactoring, improved exception handling, new tests
hhund 2aadb55
renamed method, test to verify two kem calls result in different keys
hhund a515ed1
new workflow and dependabot config
hhund 2b1adc4
renamed build step
hhund 21bfd5c
read vs readNBytes fix
hhund 0b9d63a
improved javadoc, small fixes and refactored code
hhund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/de/hsheilbronn/mi/utils/crypto/hpke/AbstractKemWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package de.hsheilbronn.mi.utils.crypto.hpke; | ||
|
|
||
| import java.security.InvalidKeyException; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.PrivateKey; | ||
| import java.security.PublicKey; | ||
| import java.security.SecureRandom; | ||
| import java.util.Objects; | ||
|
|
||
| import javax.crypto.DecapsulateException; | ||
| import javax.crypto.KEM.Encapsulated; | ||
| import javax.crypto.SecretKey; | ||
|
|
||
| public abstract class AbstractKemWrapper implements KemWrapper | ||
| { | ||
| private final KemId kemId; | ||
|
|
||
| public AbstractKemWrapper(KemId kemId) | ||
| { | ||
| this.kemId = Objects.requireNonNull(kemId, "kemId"); | ||
| } | ||
|
|
||
| @Override | ||
| public final Encapsulated getEncapsulated(PublicKey publicKey, SecureRandom secureRandom) | ||
| throws NoSuchAlgorithmException, InvalidKeyException | ||
| { | ||
| if (!kemId.isKeySupported(publicKey)) | ||
| throw new IllegalArgumentException("publicKey not supported"); | ||
|
|
||
| Encapsulated encapsulated = doGetEncapsulated(publicKey, secureRandom, kemId.getSharedSecretLength()); | ||
|
|
||
| if (encapsulated.encapsulation().length != kemId.getEncapsulationLength()) | ||
| throw new IllegalStateException("encapsulation.length not " + kemId.getEncapsulationLength()); | ||
|
|
||
| if (encapsulated.key().getEncoded().length != kemId.getSharedSecretLength()) | ||
| throw new IllegalStateException("sharedSecret.length not " + kemId.getSharedSecretLength()); | ||
|
|
||
| return encapsulated; | ||
| } | ||
|
|
||
| protected abstract Encapsulated doGetEncapsulated(PublicKey publicKey, SecureRandom secureRandom, | ||
| int sharedSecretLength) throws NoSuchAlgorithmException, InvalidKeyException; | ||
|
|
||
| @Override | ||
| public final SecretKey getSharedSecret(PrivateKey privateKey, byte[] encapsulation) | ||
| throws NoSuchAlgorithmException, InvalidKeyException, DecapsulateException | ||
| { | ||
| if (!kemId.isKeySupported(privateKey)) | ||
| throw new IllegalArgumentException("privateKey not supported"); | ||
|
|
||
| if (encapsulation.length != kemId.getEncapsulationLength()) | ||
| throw new IllegalArgumentException("encapsulation.length not " + kemId.getEncapsulationLength()); | ||
|
|
||
| SecretKey sharedSecret = doGetSecretKey(privateKey, encapsulation, kemId.getSharedSecretLength()); | ||
|
|
||
| if (sharedSecret.getEncoded().length != kemId.getSharedSecretLength()) | ||
| throw new IllegalStateException("sharedSecret.length not " + kemId.getSharedSecretLength()); | ||
|
|
||
| return sharedSecret; | ||
| } | ||
|
|
||
| protected abstract SecretKey doGetSecretKey(PrivateKey privateKey, byte[] encapsulation, int sharedSecretLength) | ||
| throws NoSuchAlgorithmException, InvalidKeyException, DecapsulateException; | ||
| } |
138 changes: 138 additions & 0 deletions
138
src/main/java/de/hsheilbronn/mi/utils/crypto/hpke/AeadId.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| package de.hsheilbronn.mi.utils.crypto.hpke; | ||
|
|
||
| import java.security.InvalidAlgorithmParameterException; | ||
| import java.security.InvalidKeyException; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.spec.AlgorithmParameterSpec; | ||
| import java.util.Objects; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| import javax.crypto.Cipher; | ||
| import javax.crypto.NoSuchPaddingException; | ||
| import javax.crypto.SecretKey; | ||
| import javax.crypto.spec.GCMParameterSpec; | ||
| import javax.crypto.spec.IvParameterSpec; | ||
|
|
||
| public enum AeadId | ||
| { | ||
| AES_128_GCM(0x0001, 16, 12, "AES", "AES/GCM/NoPadding", 128, GCMParameterSpec::new), | ||
|
|
||
| AES_256_GCM(0x0002, 32, 12, "AES", "AES/GCM/NoPadding", 128, GCMParameterSpec::new), | ||
|
|
||
| ChaCha20Poly1305(0x0003, 32, 12, "ChaCha20", "ChaCha20-Poly1305", 128, (_, iV) -> new IvParameterSpec(iV)); | ||
|
|
||
| private final int id; | ||
| private final int keyLength; | ||
| private final int ivLength; | ||
| private final String keyAlgorithm; | ||
| private final String cipherAlgorithm; | ||
| private final int authenticationTagLengthBits; | ||
| private final BiFunction<Integer, byte[], AlgorithmParameterSpec> cipherAlgorithmParameterSpecFactory; | ||
|
|
||
| AeadId(int id, int keyLenght, int ivLength, String keyAlgorithm, String cipherAlgorithm, | ||
| int authenticationTagLengthBits, | ||
| BiFunction<Integer, byte[], AlgorithmParameterSpec> cipherAlgorithmParameterSpecFactory) | ||
| { | ||
| this.id = id; | ||
| this.keyLength = keyLenght; | ||
| this.ivLength = ivLength; | ||
| this.keyAlgorithm = keyAlgorithm; | ||
| this.cipherAlgorithm = cipherAlgorithm; | ||
| this.authenticationTagLengthBits = authenticationTagLengthBits; | ||
| this.cipherAlgorithmParameterSpecFactory = cipherAlgorithmParameterSpecFactory; | ||
| } | ||
|
|
||
| public int getId() | ||
| { | ||
| return id; | ||
| } | ||
|
|
||
| public byte[] getIdAsI2osp2Bytes() | ||
| { | ||
| return ByteEncoding.i2osp2(id); | ||
| } | ||
|
|
||
| public int getKeyLength() | ||
| { | ||
| return keyLength; | ||
| } | ||
|
|
||
| public byte[] getKeyLengthAsI2osp2Bytes() | ||
| { | ||
| return ByteEncoding.i2osp2(keyLength); | ||
| } | ||
|
|
||
| public int getIvLength() | ||
| { | ||
| return ivLength; | ||
| } | ||
|
|
||
| public byte[] getIvLengthAsI2osp2Bytes() | ||
| { | ||
| return ByteEncoding.i2osp2(ivLength); | ||
| } | ||
|
|
||
| public int getAuthenticationTagLengthBits() | ||
| { | ||
| return authenticationTagLengthBits; | ||
| } | ||
|
|
||
| public String getKeyAlgorithm() | ||
| { | ||
| return keyAlgorithm; | ||
| } | ||
|
|
||
| public void initEncryptionCipher(Cipher cipher, SecretKey key, byte[] iv) | ||
| throws NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException | ||
| { | ||
| initCipher(cipher, key, iv, Cipher.ENCRYPT_MODE); | ||
| } | ||
|
|
||
| public void initDecryptionCipher(Cipher cipher, SecretKey key, byte[] iv) | ||
| throws NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException | ||
| { | ||
| initCipher(cipher, key, iv, Cipher.DECRYPT_MODE); | ||
| } | ||
|
|
||
| private void initCipher(Cipher cipher, SecretKey key, byte[] iv, int mode) | ||
| throws InvalidKeyException, InvalidAlgorithmParameterException | ||
| { | ||
| Objects.requireNonNull(key, "key"); | ||
| Objects.requireNonNull(iv, "iv"); | ||
| if (ivLength != iv.length) | ||
| throw new IllegalArgumentException("iv.length not " + ivLength); | ||
|
|
||
| AlgorithmParameterSpec spec = cipherAlgorithmParameterSpecFactory.apply(authenticationTagLengthBits, iv); | ||
| cipher.init(mode, key, spec); | ||
| } | ||
|
|
||
| public Cipher toCipher() | ||
| { | ||
| try | ||
| { | ||
| return Cipher.getInstance(cipherAlgorithm); | ||
| } | ||
| catch (NoSuchAlgorithmException | NoSuchPaddingException e) | ||
| { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| public static AeadId from(byte[] value) throws IllegalArgumentException | ||
| { | ||
| Objects.requireNonNull(value, "value"); | ||
| if (value.length != 2) | ||
| throw new IllegalArgumentException("value.length not 2"); | ||
|
|
||
| long aeadId = ByteEncoding.os2ip(value); | ||
|
|
||
| if (AES_128_GCM.id == aeadId) | ||
| return AES_128_GCM; | ||
| else if (AES_256_GCM.id == aeadId) | ||
| return AES_256_GCM; | ||
| else if (ChaCha20Poly1305.id == aeadId) | ||
| return ChaCha20Poly1305; | ||
| else | ||
| throw new IllegalArgumentException("AeadId not supported"); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.