Skip to content

Commit

Permalink
Fix revirew comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hwupathum committed Mar 20, 2024
1 parent 934432b commit 89417b5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
16 changes: 4 additions & 12 deletions ballerina/hpke.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@
// specific language governing permissions and limitations
// under the License.

# Represents the supported HPKE algorithms.
public type HpkeAlgorithm KYBER768|RSA_KYBER768;

# Represent the supported symmetric key sizes for AES algorithm.
public type AesKeySize 16|24|32;

# Represents the encapsulated secret and the ciphertext used in Hybrid Public Key Encryption (HPKE).
#
# + algorithm - The hybrid public key encryption algorithm used
# + encapsulatedSecret - The encapsulated secret
# + cipherText - The encrypted data
public type HybridEncryptionResult record {|
HpkeAlgorithm algorithm;
byte[] encapsulatedSecret;
byte[] cipherText;
|};
Expand All @@ -46,13 +41,12 @@ public type HybridEncryptionResult record {|
# + publicKey - Public key used for encryption
# + symmetricKeySize - The length of the symmetric key (in bytes)
# + return - Encrypted data or else a `crypto:Error` if an error occurs
public isolated function encryptKyber768Hpke(byte[] input, PublicKey publicKey, AesKeySize symmetricKeySize = 32) returns HybridEncryptionResult|error {
public isolated function encryptKyber768Hpke(byte[] input, PublicKey publicKey, AesKeySize symmetricKeySize = 32) returns HybridEncryptionResult|Error {
EncapsulationResult encapsulationResult = check encapsulateKyber768Kem(publicKey);
byte[] sharedSecret = check hkdfSha256(encapsulationResult.sharedSecret, symmetricKeySize);
byte[] encapsulatedSecret = encapsulationResult.encapsulatedSecret;
byte[] ciphertext = check encryptAesEcb(input, sharedSecret);
return {
algorithm: KYBER768,
encapsulatedSecret: encapsulatedSecret,
cipherText: ciphertext
};
Expand All @@ -78,13 +72,12 @@ public isolated function encryptKyber768Hpke(byte[] input, PublicKey publicKey,
# + privateKey - The Kyber private key used for decryption
# + length - The length of the output (in bytes)
# + return - Decrypted data or else a `crypto:Error` if error occurs
public isolated function decryptKyber768Hpke(byte[] input, byte[] encapsulatedKey, PrivateKey privateKey, int length = 32) returns byte[]|error {
public isolated function decryptKyber768Hpke(byte[] input, byte[] encapsulatedKey, PrivateKey privateKey, int length = 32) returns byte[]|Error {
byte[] key = check decapsulateKyber768Kem(encapsulatedKey, privateKey);
key = check hkdfSha256(key, length);
return check decryptAesEcb(input, key);
}


# Returns the RsaKyber768-HPKE-encrypted value for the given data.
# ```ballerina
# string input = "Hello Ballerina";
Expand All @@ -106,13 +99,12 @@ public isolated function decryptKyber768Hpke(byte[] input, byte[] encapsulatedKe
# + kyberPublicKey - The Kyber public key used for encryption
# + symmetricKeySize - The length of the symmetric key (in bytes)
# + return - Encrypted data or else a `crypto:Error` if an error occurs
public isolated function encryptRsaKyber768Hpke(byte[] input, PublicKey rsaPublicKey, PublicKey kyberPublicKey, AesKeySize symmetricKeySize = 32) returns HybridEncryptionResult|error {
public isolated function encryptRsaKyber768Hpke(byte[] input, PublicKey rsaPublicKey, PublicKey kyberPublicKey, AesKeySize symmetricKeySize = 32) returns HybridEncryptionResult|Error {
EncapsulationResult hybridEncapsulationResult = check encapsulateRsaKyber768Kem(rsaPublicKey, kyberPublicKey);
byte[] sharedSecret = check hkdfSha256(hybridEncapsulationResult.sharedSecret, symmetricKeySize);
byte[] encapsulatedSecret = hybridEncapsulationResult.encapsulatedSecret;
byte[] ciphertext = check encryptAesEcb(input, sharedSecret);
return {
algorithm: RSA_KYBER768,
encapsulatedSecret: encapsulatedSecret,
cipherText: ciphertext
};
Expand Down Expand Up @@ -145,7 +137,7 @@ public isolated function encryptRsaKyber768Hpke(byte[] input, PublicKey rsaPubli
# + kyberPrivateKey - The Kyber private key used for decryption
# + length - The length of the output (in bytes)
# + return - Decrypted data or else a `crypto:Error` if error occurs
public isolated function decryptRsaKyber768Hpke(byte[] input, byte[] encapsulatedKey, PrivateKey rsaPrivateKey, PrivateKey kyberPrivateKey, int length = 32) returns byte[]|error {
public isolated function decryptRsaKyber768Hpke(byte[] input, byte[] encapsulatedKey, PrivateKey rsaPrivateKey, PrivateKey kyberPrivateKey, int length = 32) returns byte[]|Error {
byte[] key = check decapsulateRsaKyber768Kem(encapsulatedKey, rsaPrivateKey, kyberPrivateKey);
key = check hkdfSha256(key, length);
return check decryptAesEcb(input, key);
Expand Down
2 changes: 2 additions & 0 deletions ballerina/kem.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import ballerina/jballerina.java;
# Represents the supported KEM algorithms.
public type KemAlgorithm RSA|KYBER768|RSA_KYBER768;

# The `Kyber768` KEM algorithm.
public const KYBER768 = "KYBER768";
# The `RSA-Kyber768` KEM algorithm.
public const RSA_KYBER768 = "RSA_KYBER768";

Expand Down
11 changes: 3 additions & 8 deletions ballerina/private_public_key.bal
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/jballerina.java;
import ballerina/time;

# Represents the supported public key algorithms.
public type KeyAlgorithm RSA|KYBER768|DILITHIUM3;
# Represents the supported key algorithms.
public type KeyAlgorithm RSA;

# The `RSA` algorithm.
public const RSA = "RSA";

# The `Kyber768` algorithm.
public const KYBER768 = "KYBER768";

# The `Dilithium3` algorithm.
public const DILITHIUM3 = "DILITHIUM3";

# Represents the KeyStore-related configurations.
#
# + path - Path to the KeyStore file
Expand Down

0 comments on commit 89417b5

Please sign in to comment.