Comments (17)
time.Now().UnixNano()
returns the number of nanoseconds elapsed since January 1, 1970 UTC. It's very hard to attack as a random seed although it can be attacked theoretically. I'll update implement via UUID later~
from gotp.
UnixNano()
has very few bits of entropy and would be easy to attack.
This project should use crypto/rand
in the Go standard library, which is Go's implementation of a cryptographically secure random number generator for use by cryptographic primitives, OTP generators, and other situations where high-quality, high-entropy randomness is essential. It's also cross-platform and works securely on all platforms Go is available on.
It's very easy to use. For example, https://pkg.go.dev/crypto/rand#example-Read :
package main
import (
"bytes"
"crypto/rand"
"fmt"
)
func main() {
c := 10
b := make([]byte, c)
_, err := rand.Read(b)
if err != nil {
fmt.Println("error:", err)
return
}
// The slice should now contain random bytes instead of only zeroes.
fmt.Println(bytes.Equal(b, make([]byte, c)))
}
from gotp.
No problem!
from gotp.
By the way, thanks @mergenchik for having another look at this issue from @johncave.
from gotp.
I had issue with the seed in random string generator. I ended up with this:
var rnd *rand.Rand
func init() {
rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
}
from gotp.
I think as @xlzd already noted, time.Now().UnixNano()
is difficult to be used in timing attacks. In Java there is a SecureRandom which provides cryptographically strong random number generator. But there is no such in go standard library (edit: there is one, crypto/rand
, mentioned in comments below by @codewinch).
So, maybe in your project you can use your own implementation of RandomSecret function which uses more secure random number generator.
Maybe we need to add in documentation, to not use RandomSecret, but implement their own with seeding of rand during program startup.
from gotp.
I had issue with the seed in random string generator. I ended up with this:
var rnd *rand.Rand func init() { rnd = rand.New(rand.NewSource(time.Now().UnixNano())) }
@ghost this is not much better. Better to use crypto/rand
rather than math/rand
, which is an unsafe RNG (better for gaming, compression, etc -- other non-cryptographic purposes.)
from gotp.
Unsafe RNG is a very serious vulnerability in this OTP library and should be rectified asap.
from gotp.
My apologies, skipped crypto/rand. Will review you pull request shortly.
from gotp.
Commented here: #12 (comment)
The constrained search space (base32) and very short (16 byte) sample size should be considered another vulnerability.
from gotp.
Commented here: #12 (comment)
The constrained search space (base32) and very short (16 byte) sample size should be considered another vulnerability.
we need to check with RFC. HOTP RFC 4226 and TOTP RFC 6238. I will try to check if got some time.
from gotp.
I checked documentation, there is no constraints on secret size. Future comments on secret size will be in #13.
Need to correctly switch from math/rand
to crypto/rand
from gotp.
swtiched from math/rand
to crypto/rand
in #14.
from gotp.
The shared secret must be at least 128 bits but RFC 4226 recommends a shared secret length of at least 160 bits.
from gotp.
Most other OTP packages (and other crypto packages like golang.org/x/crypto/nacl/box) utilize [32]byte for secrets.
from gotp.
Using 128 bits in 2022 is a tiny bit anachronistic. ;) Is there a practical reason why you can't spare 16 additional bytes for the secret?
from gotp.
Hi @codewinch, let's move this discussion to #13 if you do not mind.
from gotp.
Related Issues (13)
- panic when getting the key from viper. HOT 1
- use int64 for timestamp HOT 1
- Increase secret size to 32 bytes HOT 4
- refactor BuildUri HOT 1
- Provide a method for checking if secret is valid to avoid panic checking HOT 2
- request help: `NowWithExpiration` changes the `secret` HOT 2
- '@' Symbol HOT 4
- gotp.RandomSecret(16) generates a random secret of lenth of 26 instead of 16
- unable to generate OTP with more than 9 digits HOT 3
- totp.go times other than now HOT 4
- How to set step length for TOTP? HOT 2
- Permit a more full range of chars to be used in 'RandomSecrets'. HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gotp.