| 1 |
# Copyright 2016-2018 Yubico AB |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from yubihsm.defs import ALGORITHM, CAPABILITY |
| 16 |
from yubihsm.objects import HmacKey |
| 17 |
import random |
| 18 |
import os |
| 19 |
import pytest |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize( |
| 23 |
"algorithm, expect_len", |
| 24 |
[ |
| 25 |
(ALGORITHM.HMAC_SHA1, 20), |
| 26 |
(ALGORITHM.HMAC_SHA256, 32), |
| 27 |
(ALGORITHM.HMAC_SHA384, 48), |
| 28 |
(ALGORITHM.HMAC_SHA512, 64), |
| 29 |
], |
| 30 |
) |
| 31 |
def test_generate_hmac(session, algorithm, expect_len): |
| 32 |
caps = CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC |
| 33 |
hmackey = HmacKey.generate(session, 0, "Generate HMAC", 1, caps, algorithm) |
| 34 |
|
| 35 |
data = os.urandom(64) |
| 36 |
|
| 37 |
resp = hmackey.sign_hmac(data) |
| 38 |
assert len(resp) == expect_len |
| 39 |
assert hmackey.verify_hmac(resp, data) |
| 40 |
|
| 41 |
resp2 = hmackey.sign_hmac(data) |
| 42 |
assert len(resp2) == expect_len |
| 43 |
assert resp == resp2 |
| 44 |
|
| 45 |
data = os.urandom(64) |
| 46 |
resp2 = hmackey.sign_hmac(data) |
| 47 |
assert len(resp2) == expect_len |
| 48 |
assert resp != resp2 |
| 49 |
assert hmackey.verify_hmac(resp2, data) |
| 50 |
|
| 51 |
hmackey.delete() |
| 52 |
|
| 53 |
hmackey = HmacKey.generate(session, 0, "Generate HMAC", 1, caps, algorithm) |
| 54 |
|
| 55 |
resp = hmackey.sign_hmac(data) |
| 56 |
assert len(resp) == expect_len |
| 57 |
assert resp != resp2 |
| 58 |
assert hmackey.verify_hmac(resp, data) |
| 59 |
|
| 60 |
hmackey.delete() |
| 61 |
|
| 62 |
|
| 63 |
@pytest.mark.parametrize( |
| 64 |
"vector", |
| 65 |
[ |
| 66 |
{ |
| 67 |
"key": b"\x0b" * 20, |
| 68 |
"chal": b"Hi There", |
| 69 |
"exp_sha1": b"\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00", # noqa: E501 |
| 70 |
"exp_sha256": b"\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7", # noqa: E501 |
| 71 |
"exp_sha512": b"\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20\x3a\x12\x68\x54", # noqa: E501 |
| 72 |
"exp_sha384": b"\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2\xfa\x9c\xb6", # noqa: E501 |
| 73 |
}, |
| 74 |
{ |
| 75 |
"key": b"Jefe", |
| 76 |
"chal": b"what do ya want for nothing?", |
| 77 |
"exp_sha1": b"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79", # noqa: E501 |
| 78 |
"exp_sha256": b"\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43", # noqa: E501 |
| 79 |
"exp_sha512": b"\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a\x38\xbc\xe7\x37", # noqa: E501 |
| 80 |
"exp_sha384": b"\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa\xb2\x16\x49", # noqa: E501 |
| 81 |
}, |
| 82 |
{ |
| 83 |
"key": b"\xaa" * 20, |
| 84 |
"chal": b"\xdd" * 50, |
| 85 |
"exp_sha1": b"\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3", # noqa: E501 |
| 86 |
"exp_sha256": b"\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe", # noqa: E501 |
| 87 |
"exp_sha512": b"\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59\xe1\x32\x92\xfb", # noqa: E501 |
| 88 |
"exp_sha384": b"\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01\xa3\x4f\x27", # noqa: E501 |
| 89 |
}, |
| 90 |
{ |
| 91 |
"key": b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", # noqa: E501 |
| 92 |
"chal": b"\xcd" * 50, |
| 93 |
"exp_sha1": b"\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda", # noqa: E501 |
| 94 |
"exp_sha256": b"\x82\x55\x8a\x38\x9a\x44\x3c\x0e\xa4\xcc\x81\x98\x99\xf2\x08\x3a\x85\xf0\xfa\xa3\xe5\x78\xf8\x07\x7a\x2e\x3f\xf4\x67\x29\x66\x5b", # noqa: E501 |
| 95 |
"exp_sha512": b"\xb0\xba\x46\x56\x37\x45\x8c\x69\x90\xe5\xa8\xc5\xf6\x1d\x4a\xf7\xe5\x76\xd9\x7f\xf9\x4b\x87\x2d\xe7\x6f\x80\x50\x36\x1e\xe3\xdb\xa9\x1c\xa5\xc1\x1a\xa2\x5e\xb4\xd6\x79\x27\x5c\xc5\x78\x80\x63\xa5\xf1\x97\x41\x12\x0c\x4f\x2d\xe2\xad\xeb\xeb\x10\xa2\x98\xdd", # noqa: E501 |
| 96 |
"exp_sha384": b"\x3e\x8a\x69\xb7\x78\x3c\x25\x85\x19\x33\xab\x62\x90\xaf\x6c\xa7\x7a\x99\x81\x48\x08\x50\x00\x9c\xc5\x57\x7c\x6e\x1f\x57\x3b\x4e\x68\x01\xdd\x23\xc4\xa7\xd6\x79\xcc\xf8\xa3\x86\xc6\x74\xcf\xfb", # noqa: E501 |
| 97 |
}, |
| 98 |
], |
| 99 |
) |
| 100 |
def test_hmac_vectors(session, vector): |
| 101 |
key1_id, key2_id, key3_id, key4_id = random.sample(range(1, 0xFFFE), 4) |
| 102 |
|
| 103 |
caps = CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC |
| 104 |
|
| 105 |
key1 = HmacKey.put( |
| 106 |
session, |
| 107 |
key1_id, |
| 108 |
"Test HMAC Vectors 0x%04x" % key1_id, |
| 109 |
1, |
| 110 |
caps, |
| 111 |
vector["key"], |
| 112 |
ALGORITHM.HMAC_SHA1, |
| 113 |
) |
| 114 |
key2 = HmacKey.put( |
| 115 |
session, |
| 116 |
key2_id, |
| 117 |
"Test HMAC Vectors 0x%04x" % key2_id, |
| 118 |
1, |
| 119 |
caps, |
| 120 |
vector["key"], |
| 121 |
ALGORITHM.HMAC_SHA256, |
| 122 |
) |
| 123 |
key3 = HmacKey.put( |
| 124 |
session, |
| 125 |
key3_id, |
| 126 |
"Test HMAC Vectors 0x%04x" % key3_id, |
| 127 |
1, |
| 128 |
caps, |
| 129 |
vector["key"], |
| 130 |
ALGORITHM.HMAC_SHA384, |
| 131 |
) |
| 132 |
key4 = HmacKey.put( |
| 133 |
session, |
| 134 |
key4_id, |
| 135 |
"Test HMAC Vectors 0x%04x" % key4_id, |
| 136 |
1, |
| 137 |
caps, |
| 138 |
vector["key"], |
| 139 |
ALGORITHM.HMAC_SHA512, |
| 140 |
) |
| 141 |
|
| 142 |
assert key1.sign_hmac(vector["chal"]) == vector["exp_sha1"] |
| 143 |
assert key2.sign_hmac(vector["chal"]) == vector["exp_sha256"] |
| 144 |
assert key3.sign_hmac(vector["chal"]) == vector["exp_sha384"] |
| 145 |
assert key4.sign_hmac(vector["chal"]) == vector["exp_sha512"] |
| 146 |
assert key1.verify_hmac(vector["exp_sha1"], vector["chal"]) |
| 147 |
assert key2.verify_hmac(vector["exp_sha256"], vector["chal"]) |
| 148 |
assert key3.verify_hmac(vector["exp_sha384"], vector["chal"]) |
| 149 |
assert key4.verify_hmac(vector["exp_sha512"], vector["chal"]) |
| 150 |
|
| 151 |
key1.delete() |
| 152 |
key2.delete() |
| 153 |
key3.delete() |
| 154 |
key4.delete() |
| 155 |
|
| 156 |
|
| 157 |
@pytest.mark.parametrize( |
| 158 |
"vector", |
| 159 |
[ |
| 160 |
{ |
| 161 |
"key": b"\x0b" * 65, # Larger than SHA1 block size (64) |
| 162 |
"chal": b"\xdd" * 50, |
| 163 |
"algorithm": ALGORITHM.HMAC_SHA1, |
| 164 |
"exp_sha": b"<D\x87\x828\x8eW&\x03\xa1\xa8\x9a\x0bmy\x81\xfa\xffD\xed", # noqa: E501 |
| 165 |
}, |
| 166 |
{ |
| 167 |
"key": b"\xcd" * 65, # Larger than SHA256 block size (64), |
| 168 |
"chal": b"\xab" * 50, |
| 169 |
"algorithm": ALGORITHM.HMAC_SHA256, |
| 170 |
"exp_sha": b"T\x15\xaf\xc0\xb8\xda\x95\x97pb_\xe6$\xea\xe1rR\xfe\\\r\x11(\xbe\x02\x06\x1a&l\xc9\x94\x95\x13", # noqa: E501 |
| 171 |
}, |
| 172 |
{ |
| 173 |
"key": b"\xde" * 129, # Larger than SHA384 block size (128) |
| 174 |
"chal": b"\xbb" * 50, |
| 175 |
"algorithm": ALGORITHM.HMAC_SHA384, |
| 176 |
"exp_sha": b'`H\x9f\xfd\x99"\x86\x15\x1e\x80O\x0c\x19\xe9gH\xbck\xb57\x82\x16G42%6\xdcs\x82\xc8I\xcb|1\x1ftU\xfbb\x1c\x8b\xfb;"\xb6A\xb4', # noqa: E501 |
| 177 |
}, |
| 178 |
{ |
| 179 |
"key": b"\xcc" * 129, # Larger than SHA512 block size (128) |
| 180 |
"chal": b"\xac" * 50, |
| 181 |
"algorithm": ALGORITHM.HMAC_SHA512, |
| 182 |
"exp_sha": b"1\xe2\x94\x08\xa0\xefF\x01Y\xeb\x9e\xc7\xe5\x807\xc9\xd7~Y\xd9\x83pM\xe5\x0eU\x94\xe4\xf1/\xa9J\xb97r\xf5\x16\x0f\xe62\x88\xcd\x14\xee\xd7\x1b|I\xe7\xbd\x8a\x12*H\x9b\xeb\x00\xa0-V\x05dn\x16", # noqa: E501 |
| 183 |
}, |
| 184 |
], |
| 185 |
) |
| 186 |
def test_import_large_keys(session, vector): |
| 187 |
key_id = random.randint(1, 0xFFFE) |
| 188 |
|
| 189 |
caps = CAPABILITY.SIGN_HMAC | CAPABILITY.VERIFY_HMAC |
| 190 |
|
| 191 |
key = HmacKey.put( |
| 192 |
session, |
| 193 |
key_id, |
| 194 |
"Test import large key 0x%04x" % key_id, |
| 195 |
1, |
| 196 |
caps, |
| 197 |
vector["key"], |
| 198 |
vector["algorithm"], |
| 199 |
) |
| 200 |
|
| 201 |
assert key.sign_hmac(vector["chal"]) == vector["exp_sha"] |
| 202 |
assert key.verify_hmac(vector["exp_sha"], vector["chal"]) |
| 203 |
key.delete() |
| 204 |
|
| 205 |
|
| 206 |
def test_import_invalid_algorithm(session): |
| 207 |
# Algorithm must be HMAC_SHA1, HMAC_SHA256, HMAC_SHA384 or HMAC_SHA512 |
| 208 |
with pytest.raises(ValueError): |
| 209 |
HmacKey.put( |
| 210 |
session, |
| 211 |
0, |
| 212 |
"Test PUT invalid algorithm", |
| 213 |
0xFFFF, |
| 214 |
CAPABILITY.SIGN_HMAC, |
| 215 |
os.urandom(64), |
| 216 |
ALGORITHM.RSA_PSS_SHA256, |
| 217 |
) |