001package com.nimbusds.jose;
002
003
004import net.jcip.annotations.Immutable;
005
006
007/**
008 * Encryption method name, represents the {@code enc} header parameter in JSON
009 * Web Encryption (JWE) objects. This class is immutable.
010 *
011 * <p>Includes constants for the following standard encryption method names:
012 *
013 * <ul>
014 *     <li>{@link #A128CBC_HS256 A128CBC+HS256}
015 *     <li>{@link #A256CBC_HS512 A256CBC+HS512}
016 *     <li>{@link #A128GCM}
017 *     <li>{@link #A256GCM}
018 * </ul>
019 *
020 * <p>Additional encryption method names can be defined using the constructors.
021 *
022 * @author Vladimir Dzhuvinov
023 * @version $version$ (2013-01-08)
024 */
025@Immutable
026public final class EncryptionMethod extends Algorithm {
027
028
029        /**
030         * Composite Authenticated Encryption algorithm using Advanced 
031         * Encryption Standard (AES) in Cipher Block Chaining (CBC) mode with 
032         * PKCS #5 padding (NIST.800-38A) with an integrity calculation using 
033         * HMAC SHA-256, using a 256 bit CMK (and a 128 bit CEK) (required).
034         */
035        public static final EncryptionMethod A128CBC_HS256 = new EncryptionMethod("A128CBC+HS256", Requirement.REQUIRED);
036
037
038        /**
039         * Composite Authenticated Encryption algorithm using Advanced 
040         * Encryption Standard (AES) in Cipher Block Chaining (CBC) mode with 
041         * PKCS #5 padding (NIST.800-38A) with an integrity calculation using 
042         * HMAC SHA-512, using a 512 bit CMK (and a 256 bit CEK) (required).
043         */
044        public static final EncryptionMethod A256CBC_HS512 = new EncryptionMethod("A256CBC+HS512", Requirement.REQUIRED);
045
046
047        /**
048         * Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM)
049         * (NIST.800-38D) using 128 bit keys (recommended).
050         */
051        public static final EncryptionMethod A128GCM = new EncryptionMethod("A128GCM", Requirement.RECOMMENDED);
052
053
054        /**
055         * Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM)
056         * (NIST.800-38D) using 256 bit keys (recommended).
057         */
058        public static final EncryptionMethod A256GCM = new EncryptionMethod("A256GCM", Requirement.RECOMMENDED);
059
060
061        /**
062         * Creates a new encryption method.
063         *
064         * @param name The encryption method name. Must not be {@code null}.
065         * @param req  The implementation requirement, {@code null} if not 
066         *             known.
067         */
068        public EncryptionMethod(final String name, final Requirement req) {
069
070                super(name, req);
071        }
072
073
074        /**
075         * Creates a new encryption method.
076         *
077         * @param name The encryption method name. Must not be {@code null}.
078         */
079        public EncryptionMethod(final String name) {
080
081                super(name, null);
082        }
083
084
085        /**
086         * Parses an encryption method from the specified string.
087         *
088         * @param s The string to parse. Must not be {@code null}.
089         *
090         * @return The encryption method  (matching standard algorithm constant,
091         *         else a newly created algorithm).
092         */
093        public static EncryptionMethod parse(final String s) {
094
095                if (s == A128CBC_HS256.getName()) {
096                        return A128CBC_HS256;
097                } else if (s == A256CBC_HS512.getName()) {
098                        return A256CBC_HS512;
099                } else if (s == A128GCM.getName()) {
100                        return A128GCM;
101                } else if (s == A256GCM.getName()) {
102                        return A256GCM;
103                } else {
104                        return new EncryptionMethod(s);
105                }
106        }
107}