1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-14 00:12:06 +01:00

Start adding support for preferred AEAD algorithms

This commit is contained in:
Paul Schaub 2022-04-07 21:10:40 +02:00
parent e4bccaf58d
commit ab4f1364a2
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 113 additions and 0 deletions

View file

@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.algorithm;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
public enum AEADAlgorithm {
EAX(1, 16, 16),
OCB(2, 15, 16),
GCM(3, 12, 16),
;
private static final Map<Integer, AEADAlgorithm> MAP = new HashMap<>();
private final int id;
private final int ivLen;
private final int tagLen;
AEADAlgorithm(int id, int ivLen, int tagLen) {
this.id = id;
this.ivLen = ivLen;
this.tagLen = tagLen;
}
@Nullable
public static AEADAlgorithm fromId(int algorithmId) {
return MAP.get(algorithmId);
}
@Nonnull
public static AEADAlgorithm requireFromId(int algorithmId) {
AEADAlgorithm algorithm = fromId(algorithmId);
if (algorithm == null) {
throw new NoSuchElementException("No AEAD Algorithm found for id " + algorithmId);
}
return algorithm;
}
public int getAlgorithmId() {
return id;
}
public int getIvLength() {
return ivLen;
}
public int getTagLength() {
return tagLen;
}
}

View file

@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.algorithm;
import javax.annotation.Nonnull;
public class AEADCipherSuitePair {
private final AEADAlgorithm aeadAlgorithm;
private final SymmetricKeyAlgorithm symmetricKeyAlgorithm;
public AEADCipherSuitePair(@Nonnull SymmetricKeyAlgorithm symmetric, @Nonnull AEADAlgorithm aead) {
this.symmetricKeyAlgorithm = symmetric;
this.aeadAlgorithm = aead;
}
public SymmetricKeyAlgorithm getSymmetricKeyAlgorithm() {
return symmetricKeyAlgorithm;
}
public AEADAlgorithm getAeadAlgorithm() {
return aeadAlgorithm;
}
/**
* Mandatory-to-implement combination of AES-128 and OCB
*
* @return algorithm pair for AES-128 and OCB
*/
public static AEADCipherSuitePair aes128WithOcb() {
return new AEADCipherSuitePair(SymmetricKeyAlgorithm.AES_128, AEADAlgorithm.OCB);
}
@Override
public int hashCode() {
return symmetricKeyAlgorithm.hashCode() * 31 + aeadAlgorithm.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (!(obj instanceof AEADCipherSuitePair)) {
return false;
}
AEADCipherSuitePair other = (AEADCipherSuitePair) obj;
return getAeadAlgorithm() == other.getAeadAlgorithm()
&& getSymmetricKeyAlgorithm() == other.getSymmetricKeyAlgorithm();
}
}