001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017package com.nimbusds.jose.crypto.opts;
018
019
020import com.nimbusds.jose.JWEDecrypterOption;
021import com.nimbusds.jose.JWEEncrypterOption;
022import net.jcip.annotations.Immutable;
023
024import javax.crypto.Cipher;
025
026
027/**
028 * JCA cipher mode.
029 *
030 * @author Vladimir Dzhuvinov
031 * @version 2025-07-19
032 */
033@Immutable
034public final class CipherMode implements JWEEncrypterOption, JWEDecrypterOption {
035
036
037        /**
038         * {@link Cipher#UNWRAP_MODE} / {@link Cipher#UNWRAP_MODE}. The default
039         * for {@link com.nimbusds.jose.JWEAlgorithm#RSA_OAEP},
040         * {@link com.nimbusds.jose.JWEAlgorithm#RSA_OAEP_256} and
041         * {@link com.nimbusds.jose.JWEAlgorithm#RSA_OAEP_512}.
042         */
043        public static final CipherMode WRAP_UNWRAP = new CipherMode(Cipher.WRAP_MODE, Cipher.UNWRAP_MODE);
044
045
046        /**
047         * {@link Cipher#ENCRYPT_MODE} / {@link Cipher#DECRYPT_MODE}.
048         */
049        public static final CipherMode ENCRYPT_DECRYPT = new CipherMode(Cipher.ENCRYPT_MODE, Cipher.DECRYPT_MODE);
050
051
052
053        private final int modeForEncryption;
054
055
056        private final int modeForDecryption;
057
058
059        private CipherMode(final int modeForEncryption, final int modeForDecryption) {
060                this.modeForEncryption = modeForEncryption;
061                this.modeForDecryption = modeForDecryption;
062        }
063
064
065        /**
066         * Returns the cipher mode for a JWE encrypter.
067         *
068         * @return The cipher mode.
069         */
070        public int getForJWEEncrypter() {
071                return modeForEncryption;
072        }
073
074
075        /**
076         * Returns the cipher mode for a JWE decrypter.
077         *
078         * @return The cipher mode.
079         */
080        public int getForJWEDecrypter() {
081                return modeForDecryption;
082        }
083
084
085        @Override
086        public String toString() {
087                return "CipherMode [forEncryption=" + modeForEncryption + ", forDecryption=" + modeForDecryption + "]";
088        }
089}