Skip to content

Commit 27e1114

Browse files
author
Andreas Auernhammer
committed
make error vars constants
This commit makes the error variables (ErrAuth and ErrExceeded) constants. In general, constants are "better" than 'effectively immutable' variables since the compiler can do a better job during optimisation (e.g. inlining, ...). It's also simpler since we can use the `const` as a language construct and don't have to accomplish something similar with a type construction. Further, this commit renames the `ErrAuth` to `NotAuthentic` b/c that's more explicit. Calling code while now read like this: ``` if _, err := io.ReadAll(decReader); err != nil { if err == sio.NotAuthentic { // error handling } } ``` This is a major breaking API change: - changing the error types - `ErrAuth` -> `NotAuthentic`
1 parent 38ecd15 commit 27e1114

File tree

4 files changed

+24
-38
lines changed

4 files changed

+24
-38
lines changed

examples_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func ExampleDecReader() {
115115

116116
// Reading from r returns the original plaintext (or an error).
117117
if _, err := ioutil.ReadAll(r); err != nil {
118-
if err == sio.ErrAuth {
118+
if err == sio.NotAuthentic {
119119
// Read data is not authentic -> ciphertext has been modified.
120120
// TODO: error handling
121121
panic(err)
@@ -149,7 +149,7 @@ func ExampleDecReaderAt() {
149149

150150
// Reading from section returns the original plaintext (or an error).
151151
if _, err := ioutil.ReadAll(section); err != nil {
152-
if err == sio.ErrAuth {
152+
if err == sio.NotAuthentic {
153153
// Read data is not authentic -> ciphertext has been modified.
154154
// TODO: error handling
155155
panic(err)
@@ -220,7 +220,7 @@ func ExampleDecWriter() {
220220
w := stream.DecryptWriter(plaintext, nonce, associatedData)
221221
defer func() {
222222
if err := w.Close(); err != nil {
223-
if err == sio.ErrAuth { // During Close() the DecWriter may detect unauthentic data -> decryption error.
223+
if err == sio.NotAuthentic { // During Close() the DecWriter may detect unauthentic data -> decryption error.
224224
panic(err) // TODO: error handling
225225
}
226226
panic(err) // TODO: error handling
@@ -233,7 +233,7 @@ func ExampleDecWriter() {
233233
// the underlying io.Writer (i.e. the plaintext *bytes.Buffer) or
234234
// returns an error.
235235
if _, err := w.Write(ciphertext); err != nil {
236-
if err == sio.ErrAuth {
236+
if err == sio.NotAuthentic {
237237
// Read data is not authentic -> ciphertext has been modified.
238238
// TODO: error handling
239239
panic(err)

reader.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -363,35 +363,35 @@ func (r *DecReader) readFragment(p []byte, firstReadOffset int) (int, error) {
363363
if len(p) < r.bufSize {
364364
r.plaintextBuffer, err = r.cipher.Open(r.buffer[:0], r.nonce, r.buffer[:ciphertextLen], r.associatedData)
365365
if err != nil {
366-
r.err = ErrAuth
366+
r.err = NotAuthentic
367367
return 0, r.err
368368
}
369369
r.offset = copy(p, r.plaintextBuffer)
370370
return r.offset, nil
371371
}
372372
if _, err = r.cipher.Open(p[:0], r.nonce, r.buffer[:ciphertextLen], r.associatedData); err != nil {
373-
r.err = ErrAuth
373+
r.err = NotAuthentic
374374
return 0, r.err
375375
}
376376
return r.bufSize, nil
377377
case err == io.EOF:
378378
if firstReadOffset+n < r.cipher.Overhead() {
379-
r.err = ErrAuth
379+
r.err = NotAuthentic
380380
return 0, r.err
381381
}
382382
r.closed = true
383383
r.associatedData[0] = 0x80
384384
if len(p) < firstReadOffset+n-r.cipher.Overhead() {
385385
r.plaintextBuffer, err = r.cipher.Open(r.buffer[:0], r.nonce, r.buffer[:firstReadOffset+n], r.associatedData)
386386
if err != nil {
387-
r.err = ErrAuth
387+
r.err = NotAuthentic
388388
return 0, r.err
389389
}
390390
r.offset = copy(p, r.plaintextBuffer)
391391
return r.offset, nil
392392
}
393393
if _, err = r.cipher.Open(p[:0], r.nonce, r.buffer[:firstReadOffset+n], r.associatedData); err != nil {
394-
r.err = ErrAuth
394+
r.err = NotAuthentic
395395
return 0, r.err
396396

397397
}

sio.go

+9-23
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,23 @@ const (
2121
BufSize = 1 << 14
2222
)
2323

24-
var (
25-
// ErrAuth is returned when the decryption of a data stream fails.
26-
// It indicates that the data is not authentic (e.g. malisously
27-
// modified).
28-
ErrAuth = errAuth{}
24+
const (
25+
// NotAuthentic is returned when the decryption of a data stream fails.
26+
// It indicates that the data is not authentic - e.g. malisously modified.
27+
NotAuthentic errorType = "data is not authentic"
2928

3029
// ErrExceeded is returned when no more data can be encrypted /
3130
// decrypted securely. It indicates that the data stream is too
3231
// large to be encrypted / decrypted with a single key-nonce
3332
// combination.
34-
ErrExceeded = errExceeded{}
33+
//
34+
// For BufSize this will happen after processing ~64 TiB.
35+
ErrExceeded errorType = "data limit exceeded"
3536
)
3637

37-
// The following error type construction prevents assigning new values to
38-
// ErrAuth or ErrExceeded. In particular it prevents client code from doing
39-
// shady things like:
40-
//
41-
// `sio.ErrAuth = nil` or `sio.ErrAuth = sio.ErrExceeded`
42-
//
43-
// In contrast you could write e.g. `io.EOF = nil` which will most likely break
44-
// any I/O code in horrible ways.
45-
46-
type errType struct{ _ int }
47-
48-
type errAuth errType
49-
50-
func (errAuth) Error() string { return "sio: authentication failed" }
51-
52-
type errExceeded errType
38+
type errorType string
5339

54-
func (errExceeded) Error() string { return "sio: data limit exceeded" }
40+
func (e errorType) Error() string { return string(e) }
5541

5642
// NewStream creates a new Stream that encrypts or decrypts data
5743
// streams with the cipher. If you don't have special requirements

writer.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (w *DecWriter) Write(p []byte) (n int, err error) {
268268
}
269269
plaintext, err := w.cipher.Open(w.buffer[:0], nonce, w.buffer, w.associatedData)
270270
if err != nil {
271-
w.err = ErrAuth
271+
w.err = NotAuthentic
272272
return n, w.err
273273
}
274274
if _, err = writeTo(w.w, plaintext); err != nil {
@@ -285,7 +285,7 @@ func (w *DecWriter) Write(p []byte) (n int, err error) {
285285
}
286286
plaintext, err := w.cipher.Open(w.buffer[:0], nonce, p[:ciphertextLen], w.associatedData)
287287
if err != nil {
288-
w.err = ErrAuth
288+
w.err = NotAuthentic
289289
return n, w.err
290290
}
291291
if _, err = writeTo(w.w, plaintext); err != nil {
@@ -327,7 +327,7 @@ func (w *DecWriter) WriteByte(b byte) error {
327327
}
328328
plaintext, err := w.cipher.Open(w.buffer[:0], nonce, w.buffer, w.associatedData)
329329
if err != nil {
330-
w.err = ErrAuth
330+
w.err = NotAuthentic
331331
return w.err
332332
}
333333
if _, err = writeTo(w.w, plaintext); err != nil {
@@ -355,7 +355,7 @@ func (w *DecWriter) Close() error {
355355
binary.LittleEndian.PutUint32(w.nonce[w.cipher.NonceSize()-4:], w.seqNum)
356356
plaintext, err := w.cipher.Open(w.buffer[:0], w.nonce, w.buffer[:w.offset], w.associatedData)
357357
if err != nil {
358-
w.err = ErrAuth
358+
w.err = NotAuthentic
359359
return w.err
360360
}
361361
if _, w.err = writeTo(w.w, plaintext); w.err != nil {
@@ -409,7 +409,7 @@ func (w *DecWriter) ReadFrom(r io.Reader) (int64, error) {
409409
}
410410
plaintext, err := w.cipher.Open(buffer[:0], nonce, buffer[:ciphertextLen], w.associatedData)
411411
if err != nil {
412-
w.err = ErrAuth
412+
w.err = NotAuthentic
413413
return n, w.err
414414
}
415415
if _, err = writeTo(w.w, plaintext); err != nil {
@@ -438,7 +438,7 @@ func (w *DecWriter) ReadFrom(r io.Reader) (int64, error) {
438438
}
439439
plaintext, err = w.cipher.Open(buffer[:0], nonce, buffer[:ciphertextLen], w.associatedData)
440440
if err != nil {
441-
w.err = ErrAuth
441+
w.err = NotAuthentic
442442
return n, w.err
443443
}
444444
if _, err = writeTo(w.w, plaintext); err != nil {

0 commit comments

Comments
 (0)