Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: MohamedSabthar <[email protected]>
Co-authored-by: Bhashinee <[email protected]>
  • Loading branch information
3 people authored Mar 21, 2024
1 parent a96c026 commit 194fa5a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 17 deletions.
3 changes: 1 addition & 2 deletions ballerina/hpke.bal
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ public isolated function decryptMlKem768Hpke(byte[] input, byte[] encapsulatedKe
public isolated function encryptRsaMlKem768Hpke(byte[] input, PublicKey rsaPublicKey, PublicKey mlkemPublicKey, AesKeySize symmetricKeySize = 32) returns HybridEncryptionResult|Error {

Check warning on line 102 in ballerina/hpke.bal

View check run for this annotation

Codecov / codecov/patch

ballerina/hpke.bal#L102

Added line #L102 was not covered by tests
EncapsulationResult hybridEncapsulationResult = check encapsulateRsaKemMlKem768(rsaPublicKey, mlkemPublicKey);
byte[] sharedSecret = check hkdfSha256(hybridEncapsulationResult.sharedSecret, symmetricKeySize);
byte[] encapsulatedSecret = hybridEncapsulationResult.encapsulatedSecret;
byte[] ciphertext = check encryptAesEcb(input, sharedSecret);
return {
encapsulatedSecret: encapsulatedSecret,
encapsulatedSecret: hybridEncapsulationResult.encapsulatedSecret,

Check warning on line 107 in ballerina/hpke.bal

View check run for this annotation

Codecov / codecov/patch

ballerina/hpke.bal#L106-L107

Added lines #L106 - L107 were not covered by tests
cipherText: ciphertext
};
}

Check warning on line 110 in ballerina/hpke.bal

View check run for this annotation

Codecov / codecov/patch

ballerina/hpke.bal#L110

Added line #L110 was not covered by tests
Expand Down
20 changes: 10 additions & 10 deletions ballerina/tests/private_public_key_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,12 @@ isolated function testParseMlDsa65PublicKeyFromX509CertFile() returns Error? {
test:assertEquals(publicKey.algorithm, "DILITHIUM3");
Certificate certificate = <Certificate>publicKey.certificate;

string serial = (<int>certificate.serial).toString();
string issuer = <string>certificate.issuer;
string subject = <string>certificate.subject;
string signingAlgorithm = <string>certificate.signingAlgorithm;
int serial = certificate.serial;
string issuer = certificate.issuer;
string subject = certificate.subject;
string signingAlgorithm = certificate.signingAlgorithm;

test:assertEquals(serial, "1023822328749742100");
test:assertEquals(serial, 1023822328749742100);
test:assertEquals(issuer, "CN=localhost,OU=WSO2,O=WSO2,L=Mountain View,ST=CA,C=US");
test:assertEquals(subject, "CN=localhost,OU=WSO2,O=WSO2,L=Mountain View,ST=CA,C=US");
test:assertEquals(signingAlgorithm, "DILITHIUM3");
Expand All @@ -373,12 +373,12 @@ isolated function testParseMlKem768PublicKeyFromX509CertFile() returns Error? {
test:assertEquals(publicKey.algorithm, "KYBER768");
Certificate certificate = <Certificate>publicKey.certificate;

string serial = (<int>certificate.serial).toString();
string issuer = <string>certificate.issuer;
string subject = <string>certificate.subject;
string signingAlgorithm = <string>certificate.signingAlgorithm;
int serial = certificate.serial;
string issuer = certificate.issuer;
string subject = certificate.subject;
string signingAlgorithm = certificate.signingAlgorithm;

test:assertEquals(serial, "749281432");
test:assertEquals(serial, 749281432);
test:assertEquals(issuer, "C=US,ST=CA,L=Mountain View,O=WSO2,OU=WSO2,CN=localhost");
test:assertEquals(subject, "C=US,ST=CA,L=Mountain View,O=WSO2,OU=WSO2,CN=localhost");
test:assertEquals(signingAlgorithm, "SHA256withRSA");
Expand Down
2 changes: 1 addition & 1 deletion ballerina/tests/sign_verify_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ isolated function testSignRsaSha512WithInvalidKey() {
@test:Config {}
isolated function testSignMlDsa65WithInvalidKey() {
byte[] payload = "Ballerina test".toBytes();
PrivateKey privateKey = {algorithm:"DILITHIUM3"};
PrivateKey privateKey = {algorithm: "DILITHIUM3"};
byte[]|Error result = signMlDsa65(payload, privateKey);
if result is Error {
test:assertTrue(result.message().includes("Uninitialized private key:"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static Object generateRsaEncapsulated(PublicKey publicKey) {
RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
RSAKEMGenerator keyGenerator = new RSAKEMGenerator(

Check warning on line 207 in native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java#L206-L207

Added lines #L206 - L207 were not covered by tests
32, new KDF2BytesGenerator(new SHA256Digest()), new SecureRandom());
RSAKeyParameters rsaKeyParams = new RSAKeyParameters(
RSAKeyParameters rsaKeyParams = new RSAKeyParameters(
false, rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
SecretWithEncapsulation secretWithEncapsulation = keyGenerator.generateEncapsulated(rsaKeyParams);
SecretKey secretKey = new SecretKeySpec(secretWithEncapsulation.getSecret(), Constants.RSA_ALGORITHM);
Expand All @@ -230,7 +230,7 @@ public static Object extractSecret(byte[] encapsulation, String algorithm, Priva

public static Object extractRsaSecret(byte[] encapsulation, PrivateKey privateKey) {
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
RSAKeyParameters rsaKeyParameters = new RSAKeyParameters(
RSAKeyParameters rsaKeyParameters = new RSAKeyParameters(
true, rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
RSAKEMExtractor keyExtractor = new RSAKEMExtractor(

Check warning on line 235 in native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java#L232-L235

Added lines #L232 - L235 were not covered by tests
rsaKeyParameters, 32, new KDF2BytesGenerator(new SHA256Digest()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,4 @@ public static Object decapsulateRsaKem(BArray inputValue, BMap<?, ?> privateKey)
PrivateKey key = (PrivateKey) privateKey.getNativeData(Constants.NATIVE_DATA_PRIVATE_KEY);
return CryptoUtils.extractRsaSecret(input, key);

Check warning on line 84 in native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Kem.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Kem.java#L82-L84

Added lines #L82 - L84 were not covered by tests
}


}

0 comments on commit 194fa5a

Please sign in to comment.