1#!/bin/sh
2
3# Measure heap usage (and performance) of ECC operations with various values of
4# the relevant tunable compile-time parameters.
5#
6# Usage (preferably on a 32-bit platform):
7# cmake -D CMAKE_BUILD_TYPE=Release .
8# scripts/ecc-heap.sh | tee ecc-heap.log
9#
10# Copyright The Mbed TLS Contributors
11# SPDX-License-Identifier: Apache-2.0
12#
13# Licensed under the Apache License, Version 2.0 (the "License"); you may
14# not use this file except in compliance with the License.
15# You may obtain a copy of the License at
16#
17# http://www.apache.org/licenses/LICENSE-2.0
18#
19# Unless required by applicable law or agreed to in writing, software
20# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22# See the License for the specific language governing permissions and
23# limitations under the License.
24
25set -eu
26
27CONFIG_H='include/mbedtls/mbedtls_config.h'
28
29if [ -r $CONFIG_H ]; then :; else
30    echo "$CONFIG_H not found" >&2
31    exit 1
32fi
33
34if grep -i cmake Makefile >/dev/null; then :; else
35    echo "Needs Cmake" >&2
36    exit 1
37fi
38
39if git status | grep -F $CONFIG_H >/dev/null 2>&1; then
40    echo "mbedtls_config.h not clean" >&2
41    exit 1
42fi
43
44CONFIG_BAK=${CONFIG_H}.bak
45cp $CONFIG_H $CONFIG_BAK
46
47cat << EOF >$CONFIG_H
48#define MBEDTLS_PLATFORM_C
49#define MBEDTLS_PLATFORM_MEMORY
50#define MBEDTLS_MEMORY_BUFFER_ALLOC_C
51#define MBEDTLS_MEMORY_DEBUG
52
53#define MBEDTLS_TIMING_C
54
55#define MBEDTLS_BIGNUM_C
56#define MBEDTLS_ECP_C
57#define MBEDTLS_ASN1_PARSE_C
58#define MBEDTLS_ASN1_WRITE_C
59#define MBEDTLS_ECDSA_C
60#define MBEDTLS_SHA256_C // ECDSA benchmark needs it
61#define MBEDTLS_SHA224_C // SHA256 requires this for now
62#define MBEDTLS_ECDH_C
63
64// NIST curves >= 256 bits
65#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
66#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
67#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
68// SECP "koblitz-like" curve >= 256 bits
69#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
70// Brainpool curves (no specialised "mod p" routine)
71#define MBEDTLS_ECP_DP_BP256R1_ENABLED
72#define MBEDTLS_ECP_DP_BP384R1_ENABLED
73#define MBEDTLS_ECP_DP_BP512R1_ENABLED
74// Montgomery curves
75#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
76#define MBEDTLS_ECP_DP_CURVE448_ENABLED
77
78#define MBEDTLS_HAVE_ASM // just make things a bit faster
79#define MBEDTLS_ECP_NIST_OPTIM // faster and less allocations
80
81//#define MBEDTLS_ECP_WINDOW_SIZE            4
82//#define MBEDTLS_ECP_FIXED_POINT_OPTIM      1
83EOF
84
85for F in 0 1; do
86    for W in 2 3 4; do
87        scripts/config.py set MBEDTLS_ECP_WINDOW_SIZE $W
88        scripts/config.py set MBEDTLS_ECP_FIXED_POINT_OPTIM $F
89        make benchmark >/dev/null 2>&1
90        echo "fixed point optim = $F, max window size = $W"
91        echo "--------------------------------------------"
92        programs/test/benchmark ecdh ecdsa
93    done
94done
95
96# cleanup
97
98mv $CONFIG_BAK $CONFIG_H
99make clean
100