File tree 2 files changed +15
-8
lines changed
2 files changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -357,8 +357,7 @@ type pub inline ChaCha {
357
357
#
358
358
# # Panics
359
359
#
360
- # This method panics if the value doesn't fit in the range valid for an
361
- # unsigned 32-bits integer.
360
+ # This method panics if `value` is less than zero.
362
361
fn pub mut counter=(value: Int) {
363
362
if value < 0 { counter_size_error(value) } else { @matrix.set(12, value) }
364
363
}
@@ -385,10 +384,10 @@ type pub inline ChaCha {
385
384
i += 1
386
385
}
387
386
388
- # This in itself can't overflow, as the Int type is a 64-bits signed
389
- # integer, and below we limit it to the range that fits in a 32-bits
390
- # unsigned integer .
391
- let new_size = @matrix.get(12).wrapping_add(1)
387
+ # The chance of this overflowing is practically zero, but we use checked
388
+ # arithmetic just in case so the counter doesn't silently overflow and
389
+ # potentially mess things up .
390
+ let new_size = @matrix.get(12) + 1
392
391
393
392
@matrix.set(12, new_size)
394
393
@@ -512,8 +511,7 @@ type pub inline XChaCha {
512
511
#
513
512
# # Panics
514
513
#
515
- # This method panics if the value doesn't fit in the range valid for an
516
- # unsigned 32-bits integer.
514
+ # This method panics if `value` is less than zero.
517
515
fn pub mut counter=(value: Int) {
518
516
@chacha.counter = value
519
517
}
Original file line number Diff line number Diff line change @@ -388,6 +388,15 @@ fn pub tests(t: mut Tests) {
388
388
t.equal(secret.to_string, 'hello')
389
389
})
390
390
391
+ t.panic('ChaCha.encrypt with a counter that overflows', fn {
392
+ let key = ByteArray.filled(with: 1, times: 32)
393
+ let nonce = ByteArray.filled(with: 1, times: 12)
394
+ let cipher = ChaCha.new(key, nonce)
395
+
396
+ cipher.counter = INT_MAX
397
+ cipher.encrypt('hello'.to_byte_array)
398
+ })
399
+
391
400
t.test('chacha.hchacha20', fn (t) {
392
401
let key = ByteArray.from_array(
393
402
[
You can’t perform that action at this time.
0 commit comments