# Copyright 2016-2018 Yubico AB # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from yubihsm.defs import ALGORITHM, CAPABILITY, ERROR from yubihsm.objects import OtpAeadKey, OtpData from yubihsm.exceptions import YubiHsmDeviceError from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers.aead import AESCCM from cryptography.hazmat.backends import default_backend from dataclasses import dataclass import os import struct import pytest from typing import Mapping @dataclass class OtpTestVector: key: bytes id: bytes otps: Mapping[str, OtpData] # From: https://developers.yubico.com/OTP/Specifications/Test_vectors.html TEST_VECTORS = [ OtpTestVector( key=b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", id=b"\x01\x02\x03\x04\x05\x06", otps={ "dvgtiblfkbgturecfllberrvkinnctnn": OtpData(1, 1, 1, 1), "rnibcnfhdninbrdebccrndfhjgnhftee": OtpData(1, 2, 1, 1), "iikkijbdknrrdhfdrjltvgrbkkjblcbh": OtpData(0xFFF, 1, 1, 1), }, ), OtpTestVector( key=b"\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88\x88", id=b"\x88\x88\x88\x88\x88\x88", otps={"dcihgvrhjeucvrinhdfddbjhfjftjdei": OtpData(0x8888, 0x88, 0x88, 0x8888)}, ), OtpTestVector( key=b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", id=b"\x00\x00\x00\x00\x00\x00", otps={"kkkncjnvcnenkjvjgncjihljiibgbhbh": OtpData(0, 0, 0, 0)}, ), OtpTestVector( key=b"\xc4\x42\x28\x90\x65\x30\x76\xcd\xe7\x3d\x44\x9b\x19\x1b\x41\x6a", id=b"\x33\xc6\x9e\x7f\x24\x9e", otps={"iucvrkjiegbhidrcicvlgrcgkgurhjnj": OtpData(1, 0, 0x24, 0x13A7)}, ), ] def _crc16(data): crc = 0xFFFF for b in bytearray(data): crc ^= b for _ in range(8): j = crc & 1 crc >>= 1 if j: crc ^= 0x8408 return struct.pack("