Giter VIP home page Giter VIP logo

Comments (17)

xlzd avatar xlzd commented on August 16, 2024 3

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.

codewinch avatar codewinch commented on August 16, 2024 1

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.

codewinch avatar codewinch commented on August 16, 2024 1

No problem!

from gotp.

codewinch avatar codewinch commented on August 16, 2024 1

By the way, thanks @mergenchik for having another look at this issue from @johncave.

from gotp.

 avatar commented on August 16, 2024

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.

mergenchik avatar mergenchik commented on August 16, 2024

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.

codewinch avatar codewinch commented on August 16, 2024

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.

codewinch avatar codewinch commented on August 16, 2024

Unsafe RNG is a very serious vulnerability in this OTP library and should be rectified asap.

from gotp.

mergenchik avatar mergenchik commented on August 16, 2024

My apologies, skipped crypto/rand. Will review you pull request shortly.

from gotp.

codewinch avatar codewinch commented on August 16, 2024

Commented here: #12 (comment)

The constrained search space (base32) and very short (16 byte) sample size should be considered another vulnerability.

from gotp.

mergenchik avatar mergenchik commented on August 16, 2024

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.

mergenchik avatar mergenchik commented on August 16, 2024

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.

mergenchik avatar mergenchik commented on August 16, 2024

swtiched from math/rand to crypto/rand in #14.

from gotp.

codewinch avatar codewinch commented on August 16, 2024

The shared secret must be at least 128 bits but RFC 4226 recommends a shared secret length of at least 160 bits.

from gotp.

codewinch avatar codewinch commented on August 16, 2024

Most other OTP packages (and other crypto packages like golang.org/x/crypto/nacl/box) utilize [32]byte for secrets.

from gotp.

codewinch avatar codewinch commented on August 16, 2024

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.

mergenchik avatar mergenchik commented on August 16, 2024

Hi @codewinch, let's move this discussion to #13 if you do not mind.

from gotp.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.