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 tunable configuration parameters for jitterentropy.
10#
11# The parameters in question are LFSR loop count (kernel.jitterentropy.ll), memory access loop count
12# (kernel.jitterentropy.ml), memory block size (kernel.jitterentropy.bs), and memory block count
13# (kernel.jitterentropy.bc). See docs/jitterentropy/config-tuning.md for more information.
14
15set -e -u
16CDPATH=
17ZIRCONDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../../.." && pwd )"
18
19# Print help message and exit
20function HELP {
21    echo "$0 [options] <output-dir>" >&2
22    echo >&2
23    echo "Jitterentropy-specific options:" >&2
24    echo "    -i <iters>  : times to repeat tests [default: ]" >&2
25    echo "    --ll-seq '<N1> <N2> ... <Nk>'" >&2
26    echo "    --ml-seq '<N1> <N2> ... <Nk>'" >&2
27    echo "    --bs-seq '<N1> <N2> ... <Nk>'" >&2
28    echo "    --bc-seq '<N1> <N2> ... <Nk>'" >&2
29    echo "                : sequence of values to use for jitterentropy parameters" >&2
30    echo "                : Default (for each one): '1'" >&2
31    echo "                : Note: the argument should be a single string, with several" >&2
32    echo "                : space-separated numbers in it. In bash, use quotes, e.g." >&2
33    echo "                :     $0 \\" >&2
34    echo "                :          --ll-seq '1 2 3 4' --ml-seq '1 4 16 256' \\" >&2
35    echo "                :          --bs-seq '64' --bc-seq '512'" >&2
36    echo "                : That command will test 16 configurations, with 'll' and" >&2
37    echo "                : 'ml' varying, and 'bs' and 'bc' help constant." >&2
38    echo >&2
39    "$ZIRCONDIR"/scripts/entropy-test/repeat-boot-test -h -h
40    exit 1
41}
42
43function VERIFY_SEQ {
44    if [[ $# -eq 0 ]]; then
45        echo "empty sequence not allowed" >&2
46        HELP
47    fi
48    for val in "$@"; do
49        if ! grep '^[0-9]\+$' >/dev/null 2>&1 <<<"$val"; then
50            echo "invalid value '$val' in sequence" >&2
51            HELP
52        fi
53    done
54}
55
56ITERS=10
57LL_SEQ=(1)
58ML_SEQ=(1)
59BS_SEQ=(1)
60BC_SEQ=(1)
61PASSTHROUGH_ARGS=()
62
63# separate out our options from the repeat-boot-test options
64while [[ $# -gt 0 ]]; do
65    case "$1" in
66        -h)
67            HELP
68            ;;
69        -i)
70            if [[ $# -lt 2 ]]; then echo "-i missing iters" >&2; HELP; fi
71            ITERS="$2"
72            shift 2
73            ;;
74        --ll-seq)
75            if [[ $# -lt 2 ]]; then echo "--ll-seq missing seq" >&2; HELP; fi
76            read -ra LL_SEQ <<<"$2"
77            VERIFY_SEQ "${LL_SEQ[@]+"${LL_SEQ[@]}"}"
78            shift 2
79            ;;
80        --ml-seq)
81            if [[ $# -lt 2 ]]; then echo "--ml-seq missing seq" >&2; HELP; fi
82            read -ra ML_SEQ <<<"$2"
83            VERIFY_SEQ "${ML_SEQ[@]+"${ML_SEQ[@]}"}"
84            shift 2
85            ;;
86        --bs-seq)
87            if [[ $# -lt 2 ]]; then echo "--bs-seq missing seq" >&2; HELP; fi
88            read -ra BS_SEQ <<<"$2"
89            VERIFY_SEQ "${BS_SEQ[@]+"${BS_SEQ[@]}"}"
90            shift 2
91            ;;
92        --bc-seq)
93            if [[ $# -lt 2 ]]; then echo "--bc-seq missing seq" >&2; HELP; fi
94            read -ra BC_SEQ <<<"$2"
95            VERIFY_SEQ "${BC_SEQ[@]+"${BC_SEQ[@]}"}"
96            shift 2
97            ;;
98        *)
99            PASSTHROUGH_ARGS+=("$1")
100            shift
101            ;;
102    esac
103done
104
105BASE_CMDLINE="kernel.jitterentropy.raw=true"
106
107# build the cmdlines
108readarray -t CMDLINES < <(
109    for ((i = 0; i < ITERS; i++)); do
110        for ll in "${LL_SEQ[@]}"; do for ml in "${ML_SEQ[@]}"; do
111        for bs in "${BS_SEQ[@]}"; do for bc in "${BC_SEQ[@]}"; do
112            CMDLINE="$BASE_CMDLINE"
113            CMDLINE+=" kernel.jitterentropy.ll=$ll kernel.jitterentropy.ml=$ml"
114            CMDLINE+=" kernel.jitterentropy.bs=$bs kernel.jitterentropy.bc=$bc"
115            echo "$CMDLINE"
116        done; done
117    done
118)
119
120# run the tests
121
122# The unholy incantation around PASSTHOROUGH_ARGS comes from here:
123#     https://stackoverflow.com/a/7577209
124# TL;DR: In bash, an array is only considered 'set' if it has at least one item. Without the
125# nonsense below, if PASSTHROUGH_ARGS is empty (which is legitimate!), then `set -u` will throw.
126"$ZIRCONDIR"/scripts/entropy-test/repeat-boot-test \
127    "${PASSTHROUGH_ARGS[@]+"${PASSTHROUGH_ARGS[@]}"}" \
128    -s "jitterentropy" -- "${CMDLINES[@]}"
129