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

No public description #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions java/src/main/java/com/google/rcat/RcatTinkCrypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class RcatTinkCrypto {
/** A {@code Signer} that performs public key signing operation with Tink. */
public static class Signer implements RcatCrypto.Signer {

private final KeysetHandle privateKeysetHandle;
private final PublicKeySign signer;

/**
* Computes the signature for {@code data}.
Expand All @@ -46,7 +46,6 @@ public static class Signer implements RcatCrypto.Signer {
@Override
public byte[] sign(byte[] data) throws RcatSigningException {
try {
PublicKeySign signer = this.privateKeysetHandle.getPrimitive(PublicKeySign.class);
return signer.sign(data);
} catch (GeneralSecurityException e) {
throw new RcatSigningException("Unable to create signature for payload bytes.", e);
Expand All @@ -64,14 +63,18 @@ public static RcatTinkCrypto.Signer withPrivateKeysetHandle(KeysetHandle private
}

private Signer(KeysetHandle privateKeysetHandle) {
this.privateKeysetHandle = privateKeysetHandle;
try {
this.signer = privateKeysetHandle.getPrimitive(PublicKeySign.class);
} catch (GeneralSecurityException e) {
throw new IllegalStateException("Unable to create signer", e);
}
}
}

/** A {@code Verifier} that performs public key signing verification operation with Tink. */
public static class Verifier implements RcatCrypto.Verifier {

private final KeysetHandle publicKeysetHandle;
private final PublicKeyVerify verifier;

/**
* Verifies whether {@code signature} is a valid signature for {@code data}.
Expand All @@ -82,7 +85,6 @@ public static class Verifier implements RcatCrypto.Verifier {
@Override
public void verify(byte[] signature, byte[] data) throws RcatSignatureValidationException {
try {
PublicKeyVerify verifier = this.publicKeysetHandle.getPrimitive(PublicKeyVerify.class);
verifier.verify(signature, data);
} catch (GeneralSecurityException e) {
throw new RcatSignatureValidationException(
Expand All @@ -102,14 +104,19 @@ public static RcatTinkCrypto.Verifier withPublicKeysetHandle(KeysetHandle public
}

private Verifier(KeysetHandle publicKeysetHandle) {
this.publicKeysetHandle = publicKeysetHandle;
try {
this.verifier = publicKeysetHandle.getPrimitive(PublicKeyVerify.class);
} catch (GeneralSecurityException e) {
throw new IllegalStateException("Unable to create verifier", e);
}
;
}
}

/** An {@code Encrypter} that performs encryption operation with Tink. */
public static class Encrypter implements RcatCrypto.Encrypter {

private final KeysetHandle publicKeysetHandle;
private final HybridEncrypt encrypter;

/**
* Encrypts {@code plaintext} binding {@code contextInfo} to the resulting ciphertext.
Expand All @@ -120,7 +127,6 @@ public static class Encrypter implements RcatCrypto.Encrypter {
@Override
public byte[] encrypt(byte[] plaintext, byte[] contextInfo) throws RcatEncryptionException {
try {
HybridEncrypt encrypter = this.publicKeysetHandle.getPrimitive(HybridEncrypt.class);
return encrypter.encrypt(plaintext, contextInfo);
} catch (GeneralSecurityException e) {
throw new RcatEncryptionException("Unable to encrypt RCAT token envelope.", e);
Expand All @@ -138,14 +144,18 @@ public static RcatTinkCrypto.Encrypter withPublicKeysetHandle(KeysetHandle publi
}

private Encrypter(KeysetHandle publicKeysetHandle) {
this.publicKeysetHandle = publicKeysetHandle;
try {
this.encrypter = publicKeysetHandle.getPrimitive(HybridEncrypt.class);
} catch (GeneralSecurityException e) {
throw new IllegalStateException("Unable to create encrypter", e);
}
}
}

/** An {@code Decrypter} that performs decryption operation with Tink. */
public static class Decrypter implements RcatCrypto.Decrypter {

private final KeysetHandle privateKeysetHandle;
private final HybridDecrypt decrypter;

/**
* Decrypts {@code ciphertext} verifying the integrity of {@code contextInfo}.
Expand All @@ -156,7 +166,6 @@ public static class Decrypter implements RcatCrypto.Decrypter {
@Override
public byte[] decrypt(byte[] ciphertext, byte[] contextInfo) throws RcatDecryptionException {
try {
HybridDecrypt decrypter = this.privateKeysetHandle.getPrimitive(HybridDecrypt.class);
return decrypter.decrypt(ciphertext, contextInfo);
} catch (GeneralSecurityException e) {
throw new RcatDecryptionException("Unable to decrypt RCAT token envelope.", e);
Expand All @@ -175,7 +184,11 @@ public static RcatTinkCrypto.Decrypter withPrivateKeysetHandle(
}

private Decrypter(KeysetHandle privateKeysetHandle) {
this.privateKeysetHandle = privateKeysetHandle;
try {
this.decrypter = privateKeysetHandle.getPrimitive(HybridDecrypt.class);
} catch (GeneralSecurityException e) {
throw new IllegalStateException("Unable to create decrypter", e);
}
}
}

Expand Down