(no commit message provided)

This commit is contained in:
Prad Nukala
2024-07-05 22:20:13 -04:00
committed by Prad Nukala (aider)
commit 5fd43dfd6b
457 changed files with 115535 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
package subtle
import (
"errors"
"fmt"
"io"
"golang.org/x/crypto/hkdf"
)
const (
// Minimum tag size in bytes. This provides minimum 80-bit security strength.
minTagSizeInBytes = uint32(10)
)
var errHKDFInvalidInput = errors.New("HKDF: invalid input")
// validateHKDFParams validates parameters of HKDF constructor.
func validateHKDFParams(hash string, _ uint32, tagSize uint32) error {
// validate tag size
digestSize, err := GetHashDigestSize(hash)
if err != nil {
return err
}
if tagSize > 255*digestSize {
return fmt.Errorf("tag size too big")
}
if tagSize < minTagSizeInBytes {
return fmt.Errorf("tag size too small")
}
return nil
}
// ComputeHKDF extracts a pseudorandom key.
func ComputeHKDF(hashAlg string, key []byte, salt []byte, info []byte, tagSize uint32) ([]byte, error) {
keySize := uint32(len(key))
if err := validateHKDFParams(hashAlg, keySize, tagSize); err != nil {
return nil, fmt.Errorf("hkdf: %s", err)
}
hashFunc := GetHashFunc(hashAlg)
if hashFunc == nil {
return nil, fmt.Errorf("hkdf: invalid hash algorithm")
}
if len(salt) == 0 {
salt = make([]byte, hashFunc().Size())
}
result := make([]byte, tagSize)
kdf := hkdf.New(hashFunc, key, salt, info)
n, err := io.ReadFull(kdf, result)
if n != len(result) || err != nil {
return nil, fmt.Errorf("compute of hkdf failed")
}
return result, nil
}
+22
View File
@@ -0,0 +1,22 @@
package random
import (
"crypto/rand"
"encoding/binary"
)
// GetRandomBytes randomly generates n bytes.
func GetRandomBytes(n uint32) []byte {
buf := make([]byte, n)
_, err := rand.Read(buf)
if err != nil {
panic(err) // out of randomness, should never happen
}
return buf
}
// GetRandomUint32 randomly generates an unsigned 32-bit integer.
func GetRandomUint32() uint32 {
b := GetRandomBytes(4)
return binary.BigEndian.Uint32(b)
}
+16
View File
@@ -0,0 +1,16 @@
package random_test
import (
"testing"
"github.com/onsonr/hway/crypto/subtle/random"
)
func TestGetRandomBytes(t *testing.T) {
for i := 0; i <= 32; i++ {
buf := random.GetRandomBytes(uint32(i))
if len(buf) != i {
t.Errorf("length of the output doesn't match the input")
}
}
}
+130
View File
@@ -0,0 +1,130 @@
package subtle
import (
"crypto/elliptic"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"hash"
"math/big"
)
var errNilHashFunc = errors.New("nil hash function")
// hashDigestSize maps hash algorithms to their digest size in bytes.
var hashDigestSize = map[string]uint32{
"SHA1": uint32(20),
"SHA224": uint32(28),
"SHA256": uint32(32),
"SHA384": uint32(48),
"SHA512": uint32(64),
}
// GetHashDigestSize returns the digest size of the specified hash algorithm.
func GetHashDigestSize(hash string) (uint32, error) {
digestSize, ok := hashDigestSize[hash]
if !ok {
return 0, errors.New("invalid hash algorithm")
}
return digestSize, nil
}
// TODO(ckl): Perhaps return an explicit error instead of ""/nil for the
// following functions.
// ConvertHashName converts different forms of a hash name to the
// hash name that tink recognizes.
func ConvertHashName(name string) string {
switch name {
case "SHA-224":
return "SHA224"
case "SHA-256":
return "SHA256"
case "SHA-384":
return "SHA384"
case "SHA-512":
return "SHA512"
case "SHA-1":
return "SHA1"
default:
return ""
}
}
// ConvertCurveName converts different forms of a curve name to the
// name that tink recognizes.
func ConvertCurveName(name string) string {
switch name {
case "secp256r1", "P-256":
return "NIST_P256"
case "secp384r1", "P-384":
return "NIST_P384"
case "secp521r1", "P-521":
return "NIST_P521"
default:
return ""
}
}
// GetHashFunc returns the corresponding hash function of the given hash name.
func GetHashFunc(hash string) func() hash.Hash {
switch hash {
case "SHA1":
return sha1.New
case "SHA224":
return sha256.New224
case "SHA256":
return sha256.New
case "SHA384":
return sha512.New384
case "SHA512":
return sha512.New
default:
return nil
}
}
// GetCurve returns the curve object that corresponds to the given curve type.
// It returns null if the curve type is not supported.
func GetCurve(curve string) elliptic.Curve {
switch curve {
case "NIST_P256":
return elliptic.P256()
case "NIST_P384":
return elliptic.P384()
case "NIST_P521":
return elliptic.P521()
default:
return nil
}
}
// ComputeHash calculates a hash of the given data using the given hash function.
func ComputeHash(hashFunc func() hash.Hash, data []byte) ([]byte, error) {
if hashFunc == nil {
return nil, errNilHashFunc
}
h := hashFunc()
_, err := h.Write(data)
if err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// NewBigIntFromHex returns a big integer from a hex string.
func NewBigIntFromHex(s string) (*big.Int, error) {
if len(s)%2 == 1 {
s = "0" + s
}
b, err := hex.DecodeString(s)
if err != nil {
return nil, err
}
ret := new(big.Int).SetBytes(b)
return ret, nil
}
+25
View File
@@ -0,0 +1,25 @@
package subtle
import (
"crypto/rand"
"golang.org/x/crypto/curve25519"
)
// GeneratePrivateKeyX25519 generates a new 32-byte private key.
func GeneratePrivateKeyX25519() ([]byte, error) {
privKey := make([]byte, curve25519.ScalarSize)
_, err := rand.Read(privKey)
return privKey, err
}
// ComputeSharedSecretX25519 returns the 32-byte shared key, i.e.
// privKey * pubValue on the curve.
func ComputeSharedSecretX25519(privKey, pubValue []byte) ([]byte, error) {
return curve25519.X25519(privKey, pubValue)
}
// PublicFromPrivateX25519 computes privKey's corresponding public key.
func PublicFromPrivateX25519(privKey []byte) ([]byte, error) {
return ComputeSharedSecretX25519(privKey, curve25519.Basepoint)
}