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.cipher;
18  
19  import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
20  
21  /**
22   * Cipher Modes. Available algorithms.
23   */
24  public enum GordianCipherMode {
25      /**
26       * ECB Mode.
27       */
28      ECB,
29  
30      /**
31       * CBC Mode.
32       */
33      CBC,
34  
35      /**
36       * SIC(CTR) Mode.
37       */
38      SIC,
39  
40      /**
41       * CFB Mode.
42       */
43      CFB,
44  
45      /**
46       * CFB8 Mode.
47       */
48      CFB8,
49  
50      /**
51       * OFB Mode.
52       */
53      OFB,
54  
55      /**
56       * OFB8 Mode.
57       */
58      OFB8,
59  
60      /**
61       * EAX Mode.
62       */
63      EAX,
64  
65      /**
66       * CCM Mode.
67       */
68      CCM,
69  
70      /**
71       * GCM Mode.
72       */
73      GCM,
74  
75      /**
76       * OCB Mode.
77       */
78      OCB,
79  
80      /**
81       * GCFB Mode.
82       */
83      GCFB,
84  
85      /**
86       * GOFB Mode.
87       */
88      GOFB,
89  
90      /**
91       * KCTR Mode.
92       */
93      KCTR,
94  
95      /**
96       * KCCM Mode.
97       */
98      KCCM,
99  
100     /**
101      * KGCM Mode.
102      */
103     KGCM,
104 
105     /**
106      * G3413CBC Mode.
107      */
108     G3413CBC,
109 
110     /**
111      * G3413CFB Mode.
112      */
113     G3413CFB,
114 
115     /**
116      * G3413OFB Mode.
117      */
118     G3413OFB,
119 
120     /**
121      * G3413CTR Mode.
122      */
123     G3413CTR,
124 
125     /**
126      * GCMSIV Mode.
127      */
128     GCMSIV;
129 
130     /**
131      * Does the mode require padding?
132      *
133      * @return true/false
134      */
135     public boolean hasPadding() {
136         switch (this) {
137             case ECB:
138             case CBC:
139             case G3413CBC:
140                 return true;
141             default:
142                 return false;
143         }
144     }
145 
146     /**
147      * Is this an AAD mode?
148      *
149      * @return true/false
150      */
151     public boolean isAAD() {
152         switch (this) {
153             case CCM:
154             case GCM:
155             case EAX:
156             case OCB:
157             case KCCM:
158             case KGCM:
159             case GCMSIV:
160                 return true;
161             default:
162                 return false;
163         }
164     }
165 
166     /**
167      * Can we work on a short block?
168      *
169      * @return true/false
170      */
171     public boolean allowShortBlock() {
172         switch (this) {
173             case CCM:
174             case GCM:
175             case OCB:
176             case SIC:
177                 return false;
178             default:
179                 return true;
180         }
181     }
182 
183     /**
184      * Does the mode require a standard block?
185      *
186      * @return true/false
187      */
188     public boolean needsStdBlock() {
189         switch (this) {
190             case CCM:
191             case GCM:
192             case GCMSIV:
193             case OCB:
194                 return true;
195             default:
196                 return false;
197         }
198     }
199 
200     /**
201      * Is this mode valid for the symKeySpec?
202      *
203      * @param pKeySpec the keySpec
204      * @return true/false
205      */
206     public boolean validForSymKey(final GordianSymKeySpec pKeySpec) {
207         final GordianSymKeyType myKeyType = pKeySpec.getSymKeyType();
208         final GordianLength myKeyLen = pKeySpec.getKeyLength();
209         switch (this) {
210             case G3413OFB:
211             case G3413CFB:
212             case G3413CBC:
213             case G3413CTR:
214                 return GordianSymKeyType.KUZNYECHIK.equals(myKeyType);
215             case GOFB:
216             case GCFB:
217                 return GordianSymKeyType.GOST.equals(myKeyType);
218             case KCTR:
219             case KGCM:
220             case KCCM:
221                 return GordianSymKeyType.KALYNA.equals(myKeyType);
222             case GCMSIV:
223                 return GordianLength.LEN_128.equals(myKeyLen)
224                         || GordianLength.LEN_256.equals(myKeyLen);
225             default:
226                 return true;
227         }
228     }
229 
230     /**
231      * Does the mode need an IV?
232      *
233      * @return true/false
234      */
235     public boolean needsIV() {
236         return this != ECB;
237     }
238 
239     /**
240      * Needs re-initialisation after final.
241      *
242      * @return true/false
243      */
244     public boolean needsReInitialisation() {
245         return this == GCM;
246     }
247 }