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      protected byte[] sik;
35  
36      /**
37       * Initializes Confidentiality Algorithm
38       *
39       * @param sik
40       *            - Session Integrity Key calculated during the opening of the
41       *            session or user password if 'one-key' logins are enabled.
42       * @param authenticationAlgorithm
43       *           - Algorithm used for authentication.
44       * @throws InvalidKeyException
45       *             - when initiation of the algorithm fails
46       * @throws NoSuchAlgorithmException
47       *             - when initiation of the algorithm fails
48       * @throws NoSuchPaddingException
49       *             - when initiation of the algorithm fails
50       */
51      public void initialize(byte[] sik, AuthenticationAlgorithm authenticationAlgorithm) throws InvalidKeyException,
52              NoSuchAlgorithmException, NoSuchPaddingException {
53          this.sik = sik;
54      }
55  
56      /**
57       * Returns the algorithm's ID.
58       */
59      public abstract byte getCode();
60  
61      /**
62       * Encrypts the data.
63       *
64       * @param data
65       *            - payload to be encrypted
66       * @return encrypted data encapsulated in COnfidentiality Header and
67       *         Trailer.
68       * @throws InvalidKeyException
69       *             - when initiation of the algorithm fails
70       */
71      public abstract byte[] encrypt(byte[] data) throws InvalidKeyException;
72  
73      /**
74       * Decrypts the data.
75       *
76       * @param data
77       *            - encrypted data encapsulated in COnfidentiality Header and
78       *            Trailer.
79       * @return decrypted data.
80       * @throws IllegalArgumentException
81       *             - when initiation of the algorithm fails
82       */
83      public abstract byte[] decrypt(byte[] data);
84  
85      /**
86       * Calculates size of the confidentiality header and trailer specific for
87       * the algorithm.
88       *
89       * @param payloadSize
90       *            - size of the data that will be encrypted
91       */
92      public abstract int getConfidentialityOverheadSize(int payloadSize);
93  }