@@ -20,6 +20,7 @@ package org.kotlincrypto.core.mac
20
20
import org.kotlincrypto.core.*
21
21
import org.kotlincrypto.core.mac.internal.*
22
22
import org.kotlincrypto.error.InvalidKeyException
23
+ import org.kotlincrypto.error.InvalidParameterException
23
24
import org.kotlincrypto.error.ShortBufferException
24
25
import java.nio.ByteBuffer
25
26
import java.security.Key
@@ -44,7 +45,6 @@ import javax.crypto.SecretKey
44
45
* https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#mac-algorithms
45
46
*
46
47
* @see [Engine]
47
- * @throws [IllegalArgumentException] if [algorithm] is blank
48
48
* */
49
49
public actual abstract class Mac : javax.crypto.Mac , Algorithm , Copyable <Mac >, Resettable , Updatable {
50
50
@@ -55,10 +55,10 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
55
55
*
56
56
* @param [algorithm] See [Algorithm.algorithm]
57
57
* @param [engine] See [Engine]
58
- * @throws [IllegalArgumentException ] when:
58
+ * @throws [InvalidParameterException ] when:
59
59
* - [algorithm] is blank
60
60
* */
61
- @Throws(IllegalArgumentException ::class )
61
+ @Throws(InvalidParameterException ::class )
62
62
protected actual constructor (algorithm: String , engine: Engine ): super (
63
63
/* macSpi */ engine,
64
64
/* provider */ AndroidApi21to23MacSpiProvider .createOrNull(engine, algorithm),
@@ -67,9 +67,8 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
67
67
commonInit(algorithm)
68
68
this .engine = engine
69
69
70
- // Engine.engineInit is overridden as no-op, so this does
71
- // nothing other than set `javax.crypto.Mac.initialized`
72
- // to true
70
+ // Engine.engineInit is overridden to ignore EmptyKey, so this does
71
+ // nothing other than set `javax.crypto.Mac.initialized` to true.
73
72
super .init (EmptyKey )
74
73
}
75
74
@@ -129,12 +128,16 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
129
128
* This is useful if wanting to zero out the key before de-referencing.
130
129
*
131
130
* @see [clearKey]
132
- * @throws [IllegalArgumentException ] if [newKey] is empty, or of a length
131
+ * @throws [InvalidKeyException ] if [newKey] is empty, or of a length
133
132
* inappropriate for the [Mac] implementation.
134
133
* */
135
134
public actual fun reset (newKey : ByteArray ) {
136
- require(newKey.isNotEmpty()) { " newKey cannot be empty" }
137
- engine.reset(newKey)
135
+ if (newKey.isEmpty()) throw InvalidKeyException (" newKey cannot be empty" )
136
+ try {
137
+ engine.reset(newKey)
138
+ } catch (e: IllegalArgumentException ) {
139
+ throw InvalidKeyException (e)
140
+ }
138
141
}
139
142
140
143
/* *
@@ -172,21 +175,21 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
172
175
* or [Engine.doFinalInto] have been invoked).
173
176
*
174
177
* @param [key] The key that this [Engine] instance will use to apply its function to
175
- * @throws [IllegalArgumentException ] if [key] is empty
178
+ * @throws [InvalidKeyException ] if [key] is empty
176
179
* */
177
- @Throws(IllegalArgumentException ::class )
180
+ @Throws(InvalidKeyException ::class )
178
181
public actual constructor (key: ByteArray ): this (key, resetOnDoFinal = true )
179
182
180
183
/* *
181
184
* Initializes a new [Engine] with the provided [key] and [resetOnDoFinal] configuration.
182
185
*
183
186
* @param [key] the key that this [Engine] instance will use to apply its function to
184
187
* @param [resetOnDoFinal] See [Engine.resetOnDoFinal] documentation
185
- * @throws [IllegalArgumentException ] if [key] is empty
188
+ * @throws [InvalidKeyException ] if [key] is empty
186
189
* */
187
- @Throws(IllegalArgumentException ::class )
190
+ @Throws(InvalidKeyException ::class )
188
191
public actual constructor (key: ByteArray , resetOnDoFinal: Boolean ) {
189
- require (key.isNotEmpty ()) { " key cannot be empty" }
192
+ if (key.isEmpty ()) throw InvalidKeyException ( " key cannot be empty" )
190
193
this .resetOnDoFinal = resetOnDoFinal
191
194
}
192
195
@@ -239,10 +242,10 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
239
242
* before passing it here. Implementations should ensure any old key material
240
243
* is zeroed out.
241
244
*
242
- * @throws [IllegalArgumentException ] if [newKey] is a length inappropriate
243
- * for the [Mac ] implementation.
245
+ * @throws [InvalidKeyException ] if [newKey] is a length inappropriate
246
+ * for the [Engine ] implementation.
244
247
* */
245
- @Throws(IllegalArgumentException ::class )
248
+ @Throws(InvalidKeyException ::class )
246
249
public actual abstract fun reset (newKey : ByteArray )
247
250
248
251
// Gets set in engineDoFinal if resetOnDoFinal is set to false. Subsequent
0 commit comments