From 319000df1aad4f9d9a1592e914ccd5f42901d763 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 14 Feb 2023 21:38:06 +0100 Subject: [PATCH] Add challenge tests. Signed-off-by: Pol Henarejos --- tests/conftest.py | 5 +++ tests/pico-hsm/test_003_challenge.py | 57 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tests/pico-hsm/test_003_challenge.py diff --git a/tests/conftest.py b/tests/conftest.py index 21b4c0e..de646e8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -81,6 +81,8 @@ class Device: if (data): lc = [0x00] + list(len(data).to_bytes(2, 'big')) dataf = list(data) + else: + lc = [0x00*3] if (ne is None): le = [0x00, 0x00] else: @@ -398,6 +400,9 @@ class Device: def delete_key_domain(self, key_domain=0): self.send(cla=0x80, command=0x52, p1=0x3, p2=key_domain, codes=[0x6A88]) + def get_challenge(self, length): + return self.send(cla=0x80, command=0x84, ne=length) + @pytest.fixture(scope="session") def device(): diff --git a/tests/pico-hsm/test_003_challenge.py b/tests/pico-hsm/test_003_challenge.py new file mode 100644 index 0000000..46a7c5e --- /dev/null +++ b/tests/pico-hsm/test_003_challenge.py @@ -0,0 +1,57 @@ +""" +/* + * This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm). + * Copyright (c) 2022 Pol Henarejos. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +""" + +import pytest +import math +from collections import Counter + +def mean(x): + sum = 0 + for i in x: + sum += i + return sum/len(x) + +def var(x): + sum = 0 + m = mean(x) + for i in x: + sum += (i-m)**2 + return sum/len(x) + +@pytest.mark.parametrize( + "length", [1, 256, 1024] +) +def test_challenge(device, length): + data = device.get_challenge(length) + assert(len(data) == length) + +def test_randomness(device): + data = [] + N = 1000 + for k2 in range(N): + data += device.get_challenge(1024) + + _, values = zip(*Counter(data).items()) + + nm = mean(values)/(N*1024/256) + sm = math.sqrt(var(values))/mean(values) + + assert(0.99 <= nm <= 1.01) + assert(sm <= 0.02) +