001// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package org.pgpainless.algorithm;
006
007import java.util.Map;
008import java.util.concurrent.ConcurrentHashMap;
009
010import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
011
012/**
013 * Enumeration of possible symmetric encryption algorithms.
014 *
015 * @see <a href="https://tools.ietf.org/html/rfc4880#section-9.2">RFC4880: Symmetric-Key Algorithms</a>
016 */
017public enum SymmetricKeyAlgorithm {
018
019    /**
020     * Plaintext or unencrypted data.
021     */
022    NULL            (SymmetricKeyAlgorithmTags.NULL),
023
024    /**
025     * IDEA is deprecated.
026     * @deprecated use a different algorithm.
027     */
028    @Deprecated
029    IDEA            (SymmetricKeyAlgorithmTags.IDEA),
030
031    /**
032     * TripleDES (DES-EDE - 168 bit key derived from 192).
033     */
034    TRIPLE_DES      (SymmetricKeyAlgorithmTags.TRIPLE_DES),
035
036    /**
037     * CAST5 (128-bit key, as per RFC2144).
038     */
039    CAST5           (SymmetricKeyAlgorithmTags.CAST5),
040
041    /**
042     * Blowfish (128-bit key, 16 rounds).
043     */
044    BLOWFISH        (SymmetricKeyAlgorithmTags.BLOWFISH),
045
046    /**
047     * Reserved in RFC4880.
048     * SAFER-SK128 (13 rounds)
049     */
050    SAFER           (SymmetricKeyAlgorithmTags.SAFER),
051
052    /**
053     * Reserved in RFC4880.
054     * Reserved for DES/SK
055     */
056    DES             (SymmetricKeyAlgorithmTags.DES),
057
058    /**
059     * AES with 128-bit key.
060     */
061    AES_128         (SymmetricKeyAlgorithmTags.AES_128),
062
063    /**
064     * AES with 192-bit key.
065     */
066    AES_192         (SymmetricKeyAlgorithmTags.AES_192),
067
068    /**
069     * AES with 256-bit key.
070     */
071    AES_256         (SymmetricKeyAlgorithmTags.AES_256),
072
073    /**
074     * Twofish with 256-bit key.
075     */
076    TWOFISH         (SymmetricKeyAlgorithmTags.TWOFISH),
077
078    /**
079     * Reserved for Camellia with 128-bit key.
080     */
081    CAMELLIA_128    (SymmetricKeyAlgorithmTags.CAMELLIA_128),
082
083    /**
084     * Reserved for Camellia with 192-bit key.
085     */
086    CAMELLIA_192    (SymmetricKeyAlgorithmTags.CAMELLIA_192),
087
088    /**
089     * Reserved for Camellia with 256-bit key.
090     */
091    CAMELLIA_256    (SymmetricKeyAlgorithmTags.CAMELLIA_256),
092    ;
093
094    private static final Map<Integer, SymmetricKeyAlgorithm> MAP = new ConcurrentHashMap<>();
095
096    static {
097        for (SymmetricKeyAlgorithm s : SymmetricKeyAlgorithm.values()) {
098            MAP.put(s.algorithmId, s);
099        }
100    }
101
102    /**
103     * Return the {@link SymmetricKeyAlgorithm} enum that corresponds to the provided numeric id.
104     * If an invalid id is provided, null is returned.
105     *
106     * @param id numeric algorithm id
107     * @return symmetric key algorithm enum
108     */
109    public static SymmetricKeyAlgorithm fromId(int id) {
110        return MAP.get(id);
111    }
112
113    private final int algorithmId;
114
115    SymmetricKeyAlgorithm(int algorithmId) {
116        this.algorithmId = algorithmId;
117    }
118
119    /**
120     * Return the numeric algorithm id of the enum.
121     *
122     * @return numeric id
123     */
124    public int getAlgorithmId() {
125        return algorithmId;
126    }
127}