encoding
encoding copied to clipboard
Implement constant time decoding
Add as a config option the ability to set constant time.
Some operations (e.g. encryption/decryption) require processing the entire contents of the input before throwing exception in order to mitigate timing attacks.
As module :library:encoding-core does not have knowledge of the higher level implementation details, this will need to be something added to them individually.
- When an
EncoderDecoderis configured for constant time and encounters an unrecognized input, it should substitute that unknown input so operations continue (e.g. characterTABLE[0]) and set an error flag. - When
Feed.doFinalProtectedis invoked, it should process the remaining contents in its buffer, and then throw an exception.- invoking
flush()should not produce an exception, only upon afterFeed.doFinalis invoked. Implementations can checkisClosedwithin theirdoFinalProtectedfunction to check whetherflushordoFinalhas been invoked.
- invoking
Think about adding a static EncoderDecoder configured specifically for this, too.
public class Base64(
config: Base64.Config
) : EncoderDecoder<Base64.Config>(config) {
// ...
public object Default {
@JvmField
public val CT: Base64 = Base64 {
lineBreakInterval = 64
constantTime = true
}
}
// ...
}
Actually, this could be implemented in core by adding another constructor with the new parameter (and deprecating the prior constructor), then handled in the Decoder.Feed abstraction.
I think the parameter would need to be of type Char?, where by if it is null, constant time is not enabled, and if it is non-null, that character will be substituted in the event of an encoding exception ocurrs.
May need to add a new exception type, like InvalidDecoderInputException that can be caught to trigger the functionality in order to substitute with the defined alternative.
EDIT: Actually, throwing an exception would defeat the purpose because that would generate a stacktrace (which takes time). Implementations need to handle substitution somehow. Need to rethink and play with implementation/abstractions to see how best to do this.