View Javadoc
1   package org.metricshub.ipmi.core.coding.security;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import javax.crypto.NoSuchPaddingException;
26  import java.security.InvalidKeyException;
27  import java.security.NoSuchAlgorithmException;
28  
29  /**
30   * Interface for Confidentiality Algorithms. All classes extending this one must
31   * implement constructor(byte[]).
32   */
33  public abstract class ConfidentialityAlgorithm {
34  
35  	private byte[] sik;
36  
37  	/**
38  	 * Initializes Confidentiality Algorithm
39  	 *
40  	 * @param key
41  	 *        - Session Integrity Key calculated during the opening of the
42  	 *        session or user password if 'one-key' logins are enabled.
43  	 * @param authenticationAlgorithm
44  	 *        - Algorithm used for authentication.
45  	 * @throws InvalidKeyException
46  	 *         - when initiation of the algorithm fails
47  	 * @throws NoSuchAlgorithmException
48  	 *         - when initiation of the algorithm fails
49  	 * @throws NoSuchPaddingException
50  	 *         - when initiation of the algorithm fails
51  	 */
52  	public void initialize(byte[] key, AuthenticationAlgorithm authenticationAlgorithm)
53  			throws InvalidKeyException,
54  			NoSuchAlgorithmException,
55  			NoSuchPaddingException {
56  		this.sik = key;
57  	}
58  
59  	/**
60  	 * Returns the Session Integrity Key passed to {@link #initialize(byte[], AuthenticationAlgorithm)}.
61  	 *
62  	 * @return the Session Integrity Key, or null if not initialized
63  	 */
64  	protected byte[] getSik() {
65  		return sik;
66  	}
67  
68  	/**
69  	 * Returns the algorithm's ID.
70  	 */
71  	public abstract byte getCode();
72  
73  	/**
74  	 * Encrypts the data.
75  	 *
76  	 * @param data
77  	 *        - payload to be encrypted
78  	 * @return encrypted data encapsulated in COnfidentiality Header and
79  	 *         Trailer.
80  	 * @throws InvalidKeyException
81  	 *         - when initiation of the algorithm fails
82  	 */
83  	public abstract byte[] encrypt(byte[] data) throws InvalidKeyException;
84  
85  	/**
86  	 * Decrypts the data.
87  	 *
88  	 * @param data
89  	 *        - encrypted data encapsulated in COnfidentiality Header and
90  	 *        Trailer.
91  	 * @return decrypted data.
92  	 * @throws IllegalArgumentException
93  	 *         - when initiation of the algorithm fails
94  	 */
95  	public abstract byte[] decrypt(byte[] data);
96  
97  	/**
98  	 * Calculates size of the confidentiality header and trailer specific for
99  	 * the algorithm.
100 	 *
101 	 * @param payloadSize
102 	 *        - size of the data that will be encrypted
103 	 */
104 	public abstract int getConfidentialityOverheadSize(int payloadSize);
105 }