1#!/bin/bash
2
3# Copyright 2017 The Fuchsia Authors
4#
5# Use of this source code is governed by a MIT-style
6# license that can be found in the LICENSE file or at
7# https://opensource.org/licenses/MIT
8
9# Test the "basic configuration" parameters for jitterentropy.
10#
11# The parameters in question are pseudorandom loop counts and data processing. For more details, see
12# docs/jitterentropy/config-basic.md.
13#
14# This script tests two axes:
15#     loop counts: { pseudorandom, max, min}
16#     data processing: { on, off}
17#
18# The loop counts axis is reflected in the values of 'kernel.jitterentropy.ml' and
19# 'kernel.jitterentropy.ll'. For pseudorandom, both are set to 0. For max, both are set to the
20# maximum values that pseudorandom can output, namely 'ml = 128' and 'll = 16'. For min, both are
21# set to the minimum values that pseudorandom can output, namely 'ml = 1' and 'll = 1'.
22#
23# The data processing axis is reflected in the value of 'kernel.jitterentropy.raw'. When data
24# processing is on, 'raw = false' and when data processing is off, 'raw = true'.
25#
26# This script requires an output directory, passed as the first (and only) non-option command line
27# argument. There are also a number of options, described in the HELP function below.
28
29set -e -u
30CDPATH=
31ZIRCONDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../../.." && pwd )"
32
33# Print help message and exit
34function HELP {
35    echo "$0 [options] <output-dir>" >&2
36    echo >&2
37    echo "Jitterentropy-specific options:" >&2
38    echo "    -c <count>  : jitterentropy block count [default: 1024]" >&2
39    echo "    -i <iters>  : times to repeat tests [default: 10]" >&2
40    echo "    -s <size>   : jitterentropy block size [default: 64]" >&2
41    echo "                : (this replaces the '-s <source>' option below; this" >&2
42    echo "                : script always uses jitterentropy as its source)" >&2
43    echo >&2
44    "$ZIRCONDIR"/scripts/entropy-test/repeat-boot-test -h -h
45    exit 1
46}
47
48BLOCK_COUNT=1024
49BLOCK_SIZE=64
50ITERS=10
51PASSTHROUGH_ARGS=()
52
53# separate out our options from the repeat-boot-test options
54while [[ $# -gt 0 ]]; do
55    case "$1" in
56        -c)
57            if [[ $# -lt 2 ]]; then echo "-c missing count" >&2; HELP; fi
58            BLOCK_COUNT="$2"
59            shift 2
60            ;;
61        -h)
62            HELP
63            ;;
64        -i)
65            if [[ $# -lt 2 ]]; then echo "-i missing iters" >&2; HELP; fi
66            ITERS="$2"
67            shift 2
68            ;;
69        -s)
70            if [[ $# -lt 2 ]]; then echo "-s missing size" >&2; HELP; fi
71            BLOCK_SIZE="$2"
72            shift 2
73            ;;
74        *)
75            PASSTHROUGH_ARGS+=("$1")
76            shift
77            ;;
78    esac
79done
80
81BASE_CMDLINE="kernel.jitterentropy.bc=$BLOCK_COUNT kernel.jitterentropy.bs=$BLOCK_SIZE"
82
83AXIS_1=(
84    "kernel.jitterentropy.ml=0 kernel.jitterentropy.ll=0"
85    "kernel.jitterentropy.ml=128 kernel.jitterentropy.ll=16"
86    "kernel.jitterentropy.ml=1 kernel.jitterentropy.ll=1"
87)
88
89AXIS_2=(
90    "kernel.jitterentropy.raw=true"
91    "kernel.jitterentropy.raw=false"
92)
93
94# build the cmdlines
95readarray -t CMDLINES < <(
96    for ((i = 0; i < ITERS; i++)); do
97        for v1 in "${AXIS_1[@]}"; do for v2 in "${AXIS_2[@]}"; do
98            CMDLINE="$BASE_CMDLINE $v1 $v2"
99            echo "$CMDLINE"
100        done; done
101    done
102)
103
104# run the tests
105
106# The unholy incantation around PASSTHOROUGH_ARGS comes from here:
107#     https://stackoverflow.com/a/7577209
108# TL;DR: In bash, an array is only considered 'set' if it has at least one item. without the
109# nonsense below, if PASSTHROUGH_ARGS is empty (which is legitimate!), then `set -u` will throw.
110"$ZIRCONDIR"/scripts/entropy-test/repeat-boot-test \
111    "${PASSTHROUGH_ARGS[@]+"${PASSTHROUGH_ARGS[@]}"}" \
112    -s "jitterentropy" -- "${CMDLINES[@]}"
113