View Javadoc
1   /*
2    * GordianKnot: Security Suite
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.gordianknot.api.agree;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.keypair.GordianKeyPairType;
20  
21  /**
22   * KDF types.
23   */
24  public enum GordianAgreementKDF {
25      /**
26       * None.
27       */
28      NONE,
29  
30      /**
31       * SHA256 KDF.
32       */
33      SHA256KDF,
34  
35      /**
36       * SHA512 KDF.
37       */
38      SHA512KDF,
39  
40      /**
41       * SHA256 CKDF.
42       */
43      SHA256CKDF,
44  
45      /**
46       * SHA512 CKDF.
47       */
48      SHA512CKDF,
49  
50      /**
51       * SHA256 HKDF.
52       */
53      SHA256HKDF,
54  
55      /**
56       * SHA512 HKDF.
57       */
58      SHA512HKDF,
59  
60      /**
61       * KMAC128.
62       */
63      KMAC128,
64  
65      /**
66       * KMAC256.
67       */
68      KMAC256,
69  
70      /**
71       * SHAKE256.
72       */
73      SHAKE256;
74  
75      /**
76       * Determine whether this is a supported kdfType.
77       *
78       * @param pKeyType   pKeyType
79       * @param pAgreeType the agreement type
80       * @return true/false
81       */
82      public boolean isSupported(final GordianKeyPairType pKeyType,
83                                 final GordianAgreementType pAgreeType) {
84          /* Switch on keyType */
85          switch (pKeyType) {
86              case RSA:
87                  return !isCKDF();
88              case EC:
89              case SM2:
90              case DSTU4145:
91              case GOST2012:
92                  return isSupported4EC(pAgreeType);
93              case DH:
94                  return isSupported4DH(pAgreeType);
95              case XDH:
96                  return isSupported4XDH(pAgreeType);
97              case CMCE:
98              case FRODO:
99              case SABER:
100             case NEWHOPE:
101             case HQC:
102             case BIKE:
103             case NTRU:
104             case NTRUPRIME:
105                 return pAgreeType == GordianAgreementType.KEM && this == NONE;
106             default:
107                 return true;
108         }
109     }
110 
111     /**
112      * Determine whether this is a supported kdfType for RSA.
113      *
114      * @param pAgreeType the agreement type
115      * @return true/false
116      */
117     private boolean isSupported4DH(final GordianAgreementType pAgreeType) {
118         /* Switch on keyType */
119         switch (this) {
120             case SHA256KDF:
121             case SHA512KDF:
122                 return true;
123             case SHA256CKDF:
124             case SHA512CKDF:
125                 return pAgreeType == GordianAgreementType.UNIFIED || pAgreeType == GordianAgreementType.MQV;
126             case NONE:
127                 return pAgreeType == GordianAgreementType.BASIC || pAgreeType == GordianAgreementType.KEM;
128             default:
129                 return false;
130         }
131     }
132 
133     /**
134      * Determine whether this is a supported kdfType for XDH.
135      *
136      * @param pAgreeType the agreement type
137      * @return true/false
138      */
139     private boolean isSupported4XDH(final GordianAgreementType pAgreeType) {
140         /* Switch on keyType */
141         switch (this) {
142             case SHA512KDF:
143             case SHA256KDF:
144                 return true;
145             case SHA512CKDF:
146             case SHA256CKDF:
147             case SHA512HKDF:
148             case SHA256HKDF:
149             case NONE:
150                 return pAgreeType != GordianAgreementType.UNIFIED;
151             default:
152                 return false;
153         }
154     }
155 
156     /**
157      * Determine whether this is a supported kdfType for EC.
158      *
159      * @param pAgreeType the agreement type
160      * @return true/false
161      */
162     private boolean isSupported4EC(final GordianAgreementType pAgreeType) {
163         /* Switch on keyType */
164         switch (this) {
165             case SHA512KDF:
166             case SHA256KDF:
167             case NONE:
168                 return true;
169             case SHA512CKDF:
170             case SHA256CKDF:
171                 return pAgreeType != GordianAgreementType.KEM;
172             default:
173                 return false;
174         }
175     }
176 
177     /**
178      * Determine whether this is a CKDF.
179      *
180      * @return true/false
181      */
182     public boolean isCKDF() {
183         return this == SHA256CKDF || this == SHA512CKDF;
184     }
185 }