2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.algorithm;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
import java.util.Map;
|
2020-01-09 20:49:12 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Enumeration of possible symmetric encryption algorithms.
|
|
|
|
*
|
|
|
|
* @see <a href="https://tools.ietf.org/html/rfc4880#section-9.2">RFC4880: Symmetric-Key Algorithms</a>
|
|
|
|
*/
|
2018-06-02 21:21:35 +02:00
|
|
|
public enum SymmetricKeyAlgorithm {
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Plaintext or unencrypted data.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
NULL (SymmetricKeyAlgorithmTags.NULL),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
2018-07-02 21:40:59 +02:00
|
|
|
/**
|
|
|
|
* IDEA is deprecated.
|
|
|
|
* @deprecated use a different algorithm.
|
|
|
|
*/
|
2021-04-25 13:28:33 +02:00
|
|
|
@Deprecated
|
2018-07-02 21:40:59 +02:00
|
|
|
IDEA (SymmetricKeyAlgorithmTags.IDEA),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TripleDES (DES-EDE - 168 bit key derived from 192).
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
TRIPLE_DES (SymmetricKeyAlgorithmTags.TRIPLE_DES),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CAST5 (128 bit key, as per RFC2144).
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
CAST5 (SymmetricKeyAlgorithmTags.CAST5),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Blowfish (128 bit key, 16 rounds).
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
BLOWFISH (SymmetricKeyAlgorithmTags.BLOWFISH),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserved in RFC4880.
|
|
|
|
* SAFER-SK128 (13 rounds)
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
SAFER (SymmetricKeyAlgorithmTags.SAFER),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserved in RFC4880.
|
|
|
|
* Reserved for DES/SK
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
DES (SymmetricKeyAlgorithmTags.DES),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AES with 128-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
AES_128 (SymmetricKeyAlgorithmTags.AES_128),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AES with 192-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
AES_192 (SymmetricKeyAlgorithmTags.AES_192),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* AES with 256-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
AES_256 (SymmetricKeyAlgorithmTags.AES_256),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Twofish with 256-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
TWOFISH (SymmetricKeyAlgorithmTags.TWOFISH),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserved for Camellia with 128-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
CAMELLIA_128 (SymmetricKeyAlgorithmTags.CAMELLIA_128),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserved for Camellia with 192-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
CAMELLIA_192 (SymmetricKeyAlgorithmTags.CAMELLIA_192),
|
2021-04-25 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reserved for Camellia with 256-bit key.
|
|
|
|
*/
|
2018-07-02 21:40:59 +02:00
|
|
|
CAMELLIA_256 (SymmetricKeyAlgorithmTags.CAMELLIA_256),
|
2018-06-02 21:21:35 +02:00
|
|
|
;
|
|
|
|
|
2020-01-09 20:49:12 +01:00
|
|
|
private static final Map<Integer, SymmetricKeyAlgorithm> MAP = new ConcurrentHashMap<>();
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
for (SymmetricKeyAlgorithm s : SymmetricKeyAlgorithm.values()) {
|
|
|
|
MAP.put(s.algorithmId, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Return the {@link SymmetricKeyAlgorithm} enum that corresponds to the provided numeric id.
|
|
|
|
* If an invalid id is provided, null is returned.
|
|
|
|
*
|
|
|
|
* @param id numeric algorithm id
|
|
|
|
* @return symmetric key algorithm enum
|
|
|
|
*/
|
2018-06-19 17:14:37 +02:00
|
|
|
public static SymmetricKeyAlgorithm fromId(int id) {
|
2018-06-02 21:21:35 +02:00
|
|
|
return MAP.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final int algorithmId;
|
|
|
|
|
|
|
|
SymmetricKeyAlgorithm(int algorithmId) {
|
|
|
|
this.algorithmId = algorithmId;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Return the numeric algorithm id of the enum.
|
|
|
|
*
|
|
|
|
* @return numeric id
|
|
|
|
*/
|
2018-06-02 21:21:35 +02:00
|
|
|
public int getAlgorithmId() {
|
|
|
|
return algorithmId;
|
|
|
|
}
|
|
|
|
}
|