|
18 | 18 | package org.kotlincrypto.core.mac
|
19 | 19 |
|
20 | 20 | import org.kotlincrypto.core.*
|
21 |
| -import org.kotlincrypto.core.mac.internal.AndroidApi21to23MacSpiProvider |
22 |
| -import org.kotlincrypto.core.mac.internal.commonInit |
23 |
| -import org.kotlincrypto.core.mac.internal.commonToString |
| 21 | +import org.kotlincrypto.core.mac.internal.* |
24 | 22 | import java.nio.ByteBuffer
|
25 | 23 | import java.security.InvalidKeyException
|
26 | 24 | import java.security.Key
|
@@ -109,15 +107,23 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
|
109 | 107 | /**
|
110 | 108 | * Resets the [Mac] and will reinitialize it with the provided key.
|
111 | 109 | *
|
112 |
| - * This is useful if wanting to clear the key before de-referencing. |
| 110 | + * This is useful if wanting to zero out the key before de-referencing. |
113 | 111 | *
|
114 |
| - * @throws [IllegalArgumentException] if [newKey] is empty. |
| 112 | + * @see [clearKey] |
| 113 | + * @throws [IllegalArgumentException] if [newKey] is empty, or of a length |
| 114 | + * inappropriate for the [Mac] implementation. |
115 | 115 | * */
|
116 | 116 | public actual fun reset(newKey: ByteArray) {
|
117 | 117 | require(newKey.isNotEmpty()) { "newKey cannot be empty" }
|
118 | 118 | engine.reset(newKey)
|
119 | 119 | }
|
120 | 120 |
|
| 121 | + /** |
| 122 | + * Helper function that will call [reset] with a blank key in order |
| 123 | + * to zero it out. |
| 124 | + * */ |
| 125 | + public actual fun clearKey() { commonClearKey(engine::reset) } |
| 126 | + |
121 | 127 | /**
|
122 | 128 | * Core abstraction for powering a [Mac] implementation. Extends
|
123 | 129 | * Java's [MacSpi] for compatibility.
|
@@ -157,8 +163,16 @@ public actual abstract class Mac: javax.crypto.Mac, Algorithm, Copyable<Mac>, Re
|
157 | 163 | public actual override fun update(input: ByteArray) { update(input, 0, input.size) }
|
158 | 164 |
|
159 | 165 | /**
|
160 |
| - * Resets the [Engine] and will reinitialize it with the newly provided key. |
| 166 | + * Resets the [Engine] and will reinitialize it with the provided key. |
| 167 | + * |
| 168 | + * **NOTE:** [newKey] is checked to be non-empty by the [Mac] abstraction |
| 169 | + * before passing it here. Implementations should ensure any old key material |
| 170 | + * is zeroed out. |
| 171 | + * |
| 172 | + * @throws [IllegalArgumentException] if [newKey] is a length inappropriate |
| 173 | + * for the [Mac] implementation. |
161 | 174 | * */
|
| 175 | + @Throws(IllegalArgumentException::class) |
162 | 176 | public actual abstract fun reset(newKey: ByteArray)
|
163 | 177 |
|
164 | 178 | // MacSpi
|
|
0 commit comments