nonce.go 639 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
package attestation // import "github.com/opencontainers/runc/libenclave/attestation"

import (
	"encoding/binary"
	"github.com/opencontainers/runc/libenclave/intelsgx"
	"math/rand"
	"time"
)

// FIXME: how to make seed non-global?
type Nonce struct {
	seed    uint64
	timeout uint64
	// FIXME: use sync.mutex
}

func (n *Nonce) Generate() []byte {
	timestamp := uint64(time.Now().UnixNano())
	if n.seed+n.timeout >= timestamp {
		n.seed = timestamp
	}

	buf := make([]byte, intelsgx.NonceLength)
	binary.LittleEndian.PutUint64(buf, rand.Uint64())
	binary.LittleEndian.PutUint64(buf[intelsgx.NonceLength/2:], rand.Uint64())

	return buf
}