1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.gordianknot.api.cipher;
18
19 import io.github.tonywasher.joceanus.gordianknot.api.base.GordianLength;
20
21 import java.util.Objects;
22
23
24
25
26 public class GordianSymCipherSpec
27 extends GordianCipherSpec<GordianSymKeySpec> {
28
29
30
31 public static final int AADIVLEN = 12;
32
33
34
35
36 private static final String SEP = "-";
37
38
39
40
41 private final GordianCipherMode theMode;
42
43
44
45
46 private final GordianPadding thePadding;
47
48
49
50
51 private final boolean isValid;
52
53
54
55
56 private String theName;
57
58
59
60
61
62
63
64
65 public GordianSymCipherSpec(final GordianSymKeySpec pKeySpec,
66 final GordianCipherMode pMode,
67 final GordianPadding pPadding) {
68 super(pKeySpec);
69 theMode = pMode;
70 thePadding = pPadding;
71 isValid = checkValidity();
72 }
73
74
75
76
77
78
79 public GordianCipherMode getCipherMode() {
80 return theMode;
81 }
82
83
84
85
86
87
88 public GordianPadding getPadding() {
89 return thePadding;
90 }
91
92 @Override
93 public boolean isValid() {
94 return isValid;
95 }
96
97 @Override
98 public boolean needsIV() {
99 return theMode.needsIV();
100 }
101
102
103
104
105
106
107 public GordianLength getBlockLength() {
108 return getKeyType().getBlockLength();
109 }
110
111 @Override
112 public int getIVLength() {
113 if (getCipherMode().isAAD()) {
114 return AADIVLEN;
115 }
116 final int myBlockLen = getKeyType().getBlockLength().getByteLength();
117 return GordianCipherMode.G3413CTR.equals(theMode)
118 ? myBlockLen >> 1
119 : myBlockLen;
120 }
121
122
123
124
125
126
127 public boolean isAAD() {
128 return theMode != null && theMode.isAAD();
129 }
130
131 @Override
132 public String toString() {
133
134 if (theName == null) {
135
136 if (isValid) {
137
138 theName = super.toString();
139 theName += SEP + theMode;
140 if (!GordianPadding.NONE.equals(thePadding)) {
141 theName += SEP + thePadding;
142 }
143 } else {
144
145 theName = "InvalidSymCipherSpec: " + super.toString() + ":" + theMode + ":" + thePadding;
146 }
147 }
148
149
150 return theName;
151 }
152
153
154
155
156
157
158 private boolean checkValidity() {
159 final GordianSymKeySpec mySpec = getKeyType();
160 return mySpec != null && mySpec.isValid()
161 && theMode != null && thePadding != null;
162 }
163
164 @Override
165 public boolean equals(final Object pThat) {
166
167 if (this == pThat) {
168 return true;
169 }
170 if (pThat == null) {
171 return false;
172 }
173
174
175 return pThat instanceof GordianSymCipherSpec myThat
176 && getKeyType().equals(myThat.getKeyType())
177 && theMode == myThat.getCipherMode()
178 && thePadding == myThat.getPadding();
179 }
180
181 @Override
182 public int hashCode() {
183 return Objects.hash(getKeyType(), theMode, thePadding);
184 }
185 }