Skip to content

Commit

Permalink
feat(v4x): add experimental support to CHACHA20-BLAKE3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenithar committed Dec 20, 2024
1 parent fb9b2a5 commit 67e57bd
Show file tree
Hide file tree
Showing 7 changed files with 600 additions and 21 deletions.
42 changes: 31 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ More examples - [here](example_test.go)

## Benchmarks

> Go version 1.21 / Mac M1
> Go version 1.23.1 / Mac M1
### V3

Expand All @@ -56,12 +56,13 @@ More examples - [here](example_test.go)
goos: darwin
goarch: arm64
pkg: zntr.io/paseto/v3
Benchmark_Paseto_Encrypt-10 200414 6050 ns/op 8274 B/op 59 allocs/op
Benchmark_Paseto_Decrypt-10 214208 5558 ns/op 8050 B/op 59 allocs/op
Benchmark_Paseto_Sign-10 9921 117440 ns/op 9060 B/op 86 allocs/op
Benchmark_Paseto_Verify-10 2373 504017 ns/op 2504 B/op 36 allocs/op
cpu: Apple M1 Pro
Benchmark_Paseto_Encrypt-10 213638 5511 ns/op 7378 B/op 52 allocs/op
Benchmark_Paseto_Decrypt-10 226226 5268 ns/op 7154 B/op 52 allocs/op
Benchmark_Paseto_Sign-10 10000 124865 ns/op 9060 B/op 86 allocs/op
Benchmark_Paseto_Verify-10 2292 536478 ns/op 2504 B/op 36 allocs/op
PASS
ok zntr.io/paseto/v3 6.169s
ok zntr.io/paseto/v3 6.052s
```

### V4
Expand All @@ -71,12 +72,30 @@ ok zntr.io/paseto/v3 6.169s
goos: darwin
goarch: arm64
pkg: zntr.io/paseto/v4
Benchmark_Paseto_Encrypt-10 411024 2532 ns/op 2288 B/op 12 allocs/op
Benchmark_Paseto_Decrypt-10 567030 2092 ns/op 2064 B/op 12 allocs/op
Benchmark_Paseto_Sign-10 51487 23268 ns/op 928 B/op 4 allocs/op
Benchmark_Paseto_Verify-10 23076 51794 ns/op 704 B/op 4 allocs/op
cpu: Apple M1 Pro
Benchmark_Paseto_Encrypt-10 511538 2397 ns/op 2288 B/op 12 allocs/op
Benchmark_Paseto_Decrypt-10 528723 2260 ns/op 2064 B/op 12 allocs/op
Benchmark_Paseto_Sign-10 51476 24680 ns/op 928 B/op 4 allocs/op
Benchmark_Paseto_Verify-10 22828 52875 ns/op 704 B/op 4 allocs/op
PASS
ok zntr.io/paseto/v4 5.825s
ok zntr.io/paseto/v4 7.119s
```

### V4X (XCHACHA20-BLAKE3) - Non standard

> This is a non-standard version of PASETO using XCHACHA20-BLAKE3 instead of XCHACHA20-POLY1305.
> https://kerkour.com/chacha20-blake3/
```sh
❯ go test -bench=. -test.benchtime=1s
goos: darwin
goarch: arm64
pkg: zntr.io/paseto/v4x
cpu: Apple M1 Pro
Benchmark_Paseto_Encrypt-10 496018 2336 ns/op 1056 B/op 6 allocs/op
Benchmark_Paseto_Decrypt-10 531135 2314 ns/op 832 B/op 6 allocs/op
PASS
ok zntr.io/paseto/v4x 2.832s
```

## License
Expand All @@ -88,3 +107,4 @@ All artifacts and source code are released under [Apache 2.0 Software License](L
- <https://developer.okta.com/blog/2019/10/17/a-thorough-introduction-to-paseto>
- <https://dev.to/techschoolguru/why-paseto-is-better-than-jwt-for-token-based-authentication-1b0c>
- <https://paragonie.com/blog/2021/08/paseto-is-even-more-secure-alternative-jose-standards-jwt-etc>
- <https://kerkour.com/chacha20-blake3>
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module zntr.io/paseto

go 1.21
go 1.23

require (
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.22.0
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.31.0
lukechampine.com/blake3 v1.3.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 10 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
lukechampine.com/blake3 v1.3.0 h1:sJ3XhFINmHSrYCgl958hscfIa3bw8x4DqMP3u1YvoYE=
lukechampine.com/blake3 v1.3.0/go.mod h1:0OFRp7fBtAylGVCO40o87sbupkyIGgbpv1+M1k1LM6k=
34 changes: 34 additions & 0 deletions v4x/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Licensed to SolID under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SolID licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package v4x

const (
// KeyLength is the requested encryption key size.
KeyLength = 32
)

const (
nonceLength = 32
macLength = 32
encryptionKDFLength = 56
authenticationKeyLength = 32
LocalPrefix = "v4x.local."
)

// LocalKey represents a key for symetric encryption (local).
type LocalKey [32]byte
58 changes: 58 additions & 0 deletions v4x/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Licensed to SolID under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. SolID licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package v4x

import (
"errors"

"lukechampine.com/blake3"

"zntr.io/paseto/internal/common"
)

func kdf(key *LocalKey, n []byte) (ek, n2 []byte, err error) {
// Check arguments
if key == nil {
return nil, nil, errors.New("unable to derive keys from a nil seed")
}

// Derive encryption key
encKDF := blake3.New(encryptionKDFLength, key[:])

// Domain separation (we use the same seed for 2 different purposes)
encKDF.Write([]byte("paseto-encryption-key"))

Check failure on line 38 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `encKDF.Write` is not checked (errcheck)

Check failure on line 38 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `encKDF.Write` is not checked (errcheck)
encKDF.Write(n)

Check failure on line 39 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `encKDF.Write` is not checked (errcheck)

Check failure on line 39 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `encKDF.Write` is not checked (errcheck)
tmp := encKDF.Sum(nil)

// No error
return tmp[:KeyLength], tmp[KeyLength:], nil
}

func mac(ak, h, n, c, f, i []byte) ([]byte, error) {

Check failure on line 46 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

mac - result 1 (error) is always nil (unparam)

Check failure on line 46 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

mac - result 1 (error) is always nil (unparam)
// Compute pre-authentication message
preAuth := common.PreAuthenticationEncoding(h, n, c, f, i)

// Compute MAC
mac := blake3.New(macLength, ak)

// Hash pre-authentication content
mac.Write(preAuth)

Check failure on line 54 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `mac.Write` is not checked (errcheck)

Check failure on line 54 in v4x/helpers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `mac.Write` is not checked (errcheck)

// No error
return mac.Sum(nil), nil
}
Loading

0 comments on commit 67e57bd

Please sign in to comment.