Skip to content

Commit

Permalink
feat(unikraft): Add ukrandom library parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <[email protected]>
  • Loading branch information
craciunoiuc committed Sep 23, 2024
1 parent 1ad1054 commit b768d75
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions unikraft/export/v0/ukrandom/library.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package ukrandom

const LibraryName = "ukrandom"
54 changes: 54 additions & 0 deletions unikraft/export/v0/ukrandom/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package ukrandom

import (
"crypto/rand"
"fmt"
"strings"

"kraftkit.sh/unikraft/export/v0/ukargparse"
)

var ParamRandomSeed = ukargparse.NewParamStrSlice("random", "seed", nil)

// ExportedParams returns the parameters available by this exported library.
func ExportedParams() []ukargparse.Param {
return []ukargparse.Param{
ParamRandomSeed,
}
}

// RandomSeed are the 10 bytes that are required by the ukrandom library.
type RandomSeed [10]byte

// NewRandomSeed generates a new set of true random bytes or nothing if error.
func NewRandomSeed() RandomSeed {
randomBytes := make([]byte, 10)
_, err := rand.Read(randomBytes)
if err != nil {
return RandomSeed{}
}

var random RandomSeed
for i := 0; i < 10; i++ {
random[i] = randomBytes[i]
}

return random
}

// String implements fmt.Stringer and returns a valid set of random bytes.
func (rng RandomSeed) String() string {
var sb strings.Builder

sb.WriteString("[ ")
for i := 0; i < 10; i++ {
sb.WriteString(fmt.Sprintf("%02x ", rng[i]))
}
sb.WriteString("]")

return sb.String()
}

0 comments on commit b768d75

Please sign in to comment.