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 */ 017 018package com.nimbusds.jose.crypto.impl; 019 020 021import com.nimbusds.jose.JOSEException; 022import net.jcip.annotations.ThreadSafe; 023 024import javax.crypto.Cipher; 025import javax.crypto.IllegalBlockSizeException; 026import javax.crypto.SecretKey; 027import javax.crypto.spec.OAEPParameterSpec; 028import javax.crypto.spec.PSource; 029import javax.crypto.spec.SecretKeySpec; 030import java.security.AlgorithmParameters; 031import java.security.PrivateKey; 032import java.security.Provider; 033import java.security.interfaces.RSAPublicKey; 034import java.security.spec.AlgorithmParameterSpec; 035import java.security.spec.MGF1ParameterSpec; 036 037 038/** 039 * RSAES OAEP (SHA-512) methods for Content Encryption Key (CEK) encryption and 040 * decryption. Uses the BouncyCastle.org provider. This class is thread-safe 041 * 042 * @author Vladimir Dzhuvinov 043 * @author Justin Richer 044 * @author Peter Laurina (adapted from RSA_OAEP_256) 045 * @version 2021-09-01 046 */ 047@ThreadSafe 048public class RSA_OAEP_512 { 049 050 private static MGF1ParameterSpec mgf1ParameterSpec = MGF1ParameterSpec.SHA512; 051 /** 052 * The JCA algorithm name for RSA-OAEP-512. 053 */ 054 private static final String RSA_OEAP_512_JCA_ALG = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding"; 055 056 057 /** 058 * Encrypts the specified Content Encryption Key (CEK). 059 * 060 * @param pub The public RSA key. Must not be {@code null}. 061 * @param cek The Content Encryption Key (CEK) to encrypt. Must 062 * not be {@code null}. 063 * @param provider The JCA provider, or {@code null} to use the default 064 * one. 065 * 066 * @return The encrypted Content Encryption Key (CEK). 067 * 068 * @throws JOSEException If encryption failed. 069 */ 070 public static byte[] encryptCEK(final RSAPublicKey pub, final SecretKey cek, final Provider provider) 071 throws JOSEException { 072 073 try { 074 AlgorithmParameters algp = AlgorithmParametersHelper.getInstance("OAEP", provider); 075 AlgorithmParameterSpec paramSpec = new OAEPParameterSpec("SHA-512", "MGF1", mgf1ParameterSpec, PSource.PSpecified.DEFAULT); 076 algp.init(paramSpec); 077 Cipher cipher = CipherHelper.getInstance(RSA_OEAP_512_JCA_ALG, provider); 078 cipher.init(Cipher.ENCRYPT_MODE, pub, algp); 079 return cipher.doFinal(cek.getEncoded()); 080 081 } catch (IllegalBlockSizeException e) { 082 throw new JOSEException("RSA block size exception: The RSA key is too short, try a longer one", e); 083 } catch (Exception e) { 084 // java.security.NoSuchAlgorithmException 085 // java.security.NoSuchPaddingException 086 // java.security.InvalidKeyException 087 // javax.crypto.BadPaddingException 088 throw new JOSEException(e.getMessage(), e); 089 } 090 } 091 092 093 /** 094 * Decrypts the specified encrypted Content Encryption Key (CEK). 095 * 096 * @param priv The private RSA key. Must not be {@code null}. 097 * @param encryptedCEK The encrypted Content Encryption Key (CEK) to 098 * decrypt. Must not be {@code null}. 099 * @param provider The JCA provider, or {@code null} to use the 100 * default one. 101 * 102 * @return The decrypted Content Encryption Key (CEK). 103 * 104 * @throws JOSEException If decryption failed. 105 */ 106 public static SecretKey decryptCEK(final PrivateKey priv, 107 final byte[] encryptedCEK, final Provider provider) 108 throws JOSEException { 109 110 try { 111 AlgorithmParameters algp = AlgorithmParametersHelper.getInstance("OAEP", provider); 112 AlgorithmParameterSpec paramSpec = new OAEPParameterSpec("SHA-512", "MGF1", mgf1ParameterSpec, PSource.PSpecified.DEFAULT); 113 algp.init(paramSpec); 114 Cipher cipher = CipherHelper.getInstance(RSA_OEAP_512_JCA_ALG, provider); 115 cipher.init(Cipher.DECRYPT_MODE, priv, algp); 116 return new SecretKeySpec(cipher.doFinal(encryptedCEK), "AES"); 117 118 } catch (Exception e) { 119 // java.security.NoSuchAlgorithmException 120 // java.security.NoSuchPaddingException 121 // java.security.InvalidKeyException 122 // javax.crypto.IllegalBlockSizeException 123 // javax.crypto.BadPaddingException 124 throw new JOSEException(e.getMessage(), e); 125 } 126 } 127 128 129 /** 130 * Prevents public instantiation. 131 */ 132 private RSA_OAEP_512() { } 133}