Add challenge tests.

Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
Pol Henarejos 2023-02-14 21:38:06 +01:00
parent 5508e531a0
commit 319000df1a
No known key found for this signature in database
GPG key ID: C0095B7870A4CCD3
2 changed files with 62 additions and 0 deletions

View file

@ -81,6 +81,8 @@ class Device:
if (data): if (data):
lc = [0x00] + list(len(data).to_bytes(2, 'big')) lc = [0x00] + list(len(data).to_bytes(2, 'big'))
dataf = list(data) dataf = list(data)
else:
lc = [0x00*3]
if (ne is None): if (ne is None):
le = [0x00, 0x00] le = [0x00, 0x00]
else: else:
@ -398,6 +400,9 @@ class Device:
def delete_key_domain(self, key_domain=0): def delete_key_domain(self, key_domain=0):
self.send(cla=0x80, command=0x52, p1=0x3, p2=key_domain, codes=[0x6A88]) 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") @pytest.fixture(scope="session")
def device(): def device():

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
"""
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)