| 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, ERROR, ORIGIN, OBJECT |
| 16 |
from yubihsm.objects import ( |
| 17 |
AsymmetricKey, |
| 18 |
SymmetricKey, |
| 19 |
WrapKey, |
| 20 |
Opaque, |
| 21 |
PublicWrapKey, |
| 22 |
) |
| 23 |
from yubihsm.exceptions import YubiHsmDeviceError |
| 24 |
|
| 25 |
from cryptography.hazmat.primitives.asymmetric import ec, rsa, ed25519 |
| 26 |
from cryptography.hazmat.primitives import hashes, serialization |
| 27 |
from cryptography.hazmat.backends import default_backend |
| 28 |
import os |
| 29 |
import random |
| 30 |
import pytest |
| 31 |
|
| 32 |
|
| 33 |
def test_generate_wrap(session): |
| 34 |
w_id = random.randint(1, 0xFFFE) |
| 35 |
a_id = random.randint(1, 0xFFFE) |
| 36 |
|
| 37 |
wrapkey = WrapKey.generate( |
| 38 |
session, |
| 39 |
w_id, |
| 40 |
"Generate Wrap 0x%04x" % w_id, |
| 41 |
1, |
| 42 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 43 |
ALGORITHM.AES192_CCM_WRAP, |
| 44 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 45 |
) |
| 46 |
|
| 47 |
asymkey = AsymmetricKey.generate( |
| 48 |
session, |
| 49 |
a_id, |
| 50 |
"Generate Wrap 0x%04x" % a_id, |
| 51 |
0xFFFF, |
| 52 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 53 |
ALGORITHM.EC_P256, |
| 54 |
) |
| 55 |
origin = asymkey.get_info().origin |
| 56 |
assert origin == ORIGIN.GENERATED |
| 57 |
|
| 58 |
pub = asymkey.get_public_key() |
| 59 |
|
| 60 |
data = os.urandom(64) |
| 61 |
resp = asymkey.sign_ecdsa(data) |
| 62 |
|
| 63 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 64 |
|
| 65 |
wrapped = wrapkey.export_wrapped(asymkey) |
| 66 |
|
| 67 |
wrapped2 = wrapkey.export_wrapped(asymkey) |
| 68 |
|
| 69 |
assert wrapped != wrapped2 |
| 70 |
|
| 71 |
asymkey.delete() |
| 72 |
|
| 73 |
pytest.raises(YubiHsmDeviceError, asymkey.get_public_key) |
| 74 |
|
| 75 |
asymkey = wrapkey.import_wrapped(wrapped) |
| 76 |
origin = asymkey.get_info().origin |
| 77 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.GENERATED |
| 78 |
|
| 79 |
data = os.urandom(64) |
| 80 |
resp = asymkey.sign_ecdsa(data) |
| 81 |
assert resp is not None |
| 82 |
|
| 83 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 84 |
|
| 85 |
wrapkey.delete() |
| 86 |
asymkey.delete() |
| 87 |
|
| 88 |
|
| 89 |
def test_export_wrap(session): |
| 90 |
w_id = random.randint(1, 0xFFFE) |
| 91 |
wrapkey = WrapKey.put( |
| 92 |
session, |
| 93 |
w_id, |
| 94 |
"Test Export Wrap 0x%04x" % w_id, |
| 95 |
1, |
| 96 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 97 |
ALGORITHM.AES192_CCM_WRAP, |
| 98 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 99 |
os.urandom(24), |
| 100 |
) |
| 101 |
|
| 102 |
eckey = ec.generate_private_key(ec.SECP384R1(), backend=default_backend()) |
| 103 |
|
| 104 |
a_id = random.randint(1, 0xFFFE) |
| 105 |
asymkey = AsymmetricKey.put( |
| 106 |
session, |
| 107 |
a_id, |
| 108 |
"Test Export Wrap 0x%04x" % a_id, |
| 109 |
0xFFFF, |
| 110 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 111 |
eckey, |
| 112 |
) |
| 113 |
origin = asymkey.get_info().origin |
| 114 |
assert origin == ORIGIN.IMPORTED |
| 115 |
|
| 116 |
data = os.urandom(64) |
| 117 |
resp = asymkey.sign_ecdsa(data, hash=hashes.SHA384()) |
| 118 |
|
| 119 |
eckey.public_key().verify(resp, data, ec.ECDSA(hashes.SHA384())) |
| 120 |
|
| 121 |
wrapped = wrapkey.export_wrapped(asymkey) |
| 122 |
|
| 123 |
# NOTE: the code below works to decrypt a wrapped object, but relies on |
| 124 |
# understanding the internal object representation which we don't feel |
| 125 |
# like doing here. |
| 126 |
|
| 127 |
# nonce = wrapped[:13] |
| 128 |
# data = wrapped[13:-8] |
| 129 |
|
| 130 |
# nonce = '\x01' + nonce + '\x00\x01' |
| 131 |
|
| 132 |
# decryptor = Cipher(algorithms.AES(wrapkey.key), |
| 133 |
# mode=modes.CTR(nonce), |
| 134 |
# backend=default_backend()).decryptor() |
| 135 |
# dec = decryptor.update(data) |
| 136 |
|
| 137 |
# numbers = eckey.private_numbers() |
| 138 |
# serialized = int.from_bytes(numbers.private_value, 'big') |
| 139 |
# assert serialized == dec[-len(serialized):] |
| 140 |
|
| 141 |
asymkey.delete() |
| 142 |
|
| 143 |
asymkey = wrapkey.import_wrapped(wrapped) |
| 144 |
|
| 145 |
data = os.urandom(64) |
| 146 |
resp = asymkey.sign_ecdsa(data, hash=hashes.SHA384()) |
| 147 |
|
| 148 |
eckey.public_key().verify(resp, data, ec.ECDSA(hashes.SHA384())) |
| 149 |
|
| 150 |
origin = asymkey.get_info().origin |
| 151 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.IMPORTED |
| 152 |
|
| 153 |
asymkey.delete() |
| 154 |
|
| 155 |
asymkey = wrapkey.import_wrapped(wrapped) |
| 156 |
assert isinstance(asymkey, AsymmetricKey) |
| 157 |
|
| 158 |
wrapkey.delete() |
| 159 |
asymkey.delete() |
| 160 |
|
| 161 |
|
| 162 |
def test_wrap_data(session): |
| 163 |
w_id = random.randint(1, 0xFFFE) |
| 164 |
key_label = "Key in List 0x%04x" % w_id |
| 165 |
key = WrapKey.generate( |
| 166 |
session, |
| 167 |
w_id, |
| 168 |
key_label, |
| 169 |
1, |
| 170 |
CAPABILITY.WRAP_DATA | CAPABILITY.UNWRAP_DATA, |
| 171 |
ALGORITHM.AES256_CCM_WRAP, |
| 172 |
CAPABILITY.NONE, |
| 173 |
) |
| 174 |
|
| 175 |
for size in (1, 16, 128, 1024, 1989): |
| 176 |
data = os.urandom(size) |
| 177 |
wrapped = key.wrap_data(data) |
| 178 |
|
| 179 |
data2 = key.unwrap_data(wrapped) |
| 180 |
assert data == data2 |
| 181 |
|
| 182 |
key.delete() |
| 183 |
|
| 184 |
|
| 185 |
def test_more_wrap_data(session): |
| 186 |
w_id = random.randint(1, 0xFFFE) |
| 187 |
key_label = "Key in List 0x%04x" % w_id |
| 188 |
for size in (16, 24, 32): |
| 189 |
if size == 16: |
| 190 |
a = ALGORITHM.AES128_CCM_WRAP |
| 191 |
elif size == 24: |
| 192 |
a = ALGORITHM.AES192_CCM_WRAP |
| 193 |
elif size == 32: |
| 194 |
a = ALGORITHM.AES256_CCM_WRAP |
| 195 |
key = WrapKey.put( |
| 196 |
session, |
| 197 |
w_id, |
| 198 |
key_label, |
| 199 |
1, |
| 200 |
CAPABILITY.WRAP_DATA | CAPABILITY.UNWRAP_DATA, |
| 201 |
a, |
| 202 |
CAPABILITY.NONE, |
| 203 |
os.urandom(size), |
| 204 |
) |
| 205 |
|
| 206 |
data = os.urandom(size) |
| 207 |
wrap = key.wrap_data(data) |
| 208 |
plain = key.unwrap_data(wrap) |
| 209 |
assert data == plain |
| 210 |
|
| 211 |
key.delete() |
| 212 |
|
| 213 |
|
| 214 |
def test_wrap_data_many(session, info): |
| 215 |
key_label = "wrap key" |
| 216 |
raw_key = os.urandom(24) |
| 217 |
w_key = WrapKey.put( |
| 218 |
session, |
| 219 |
0, |
| 220 |
key_label, |
| 221 |
1, |
| 222 |
CAPABILITY.WRAP_DATA, |
| 223 |
ALGORITHM.AES192_CCM_WRAP, |
| 224 |
CAPABILITY.NONE, |
| 225 |
raw_key, |
| 226 |
) |
| 227 |
u_key = WrapKey.put( |
| 228 |
session, |
| 229 |
0, |
| 230 |
key_label, |
| 231 |
1, |
| 232 |
CAPABILITY.UNWRAP_DATA, |
| 233 |
ALGORITHM.AES192_CCM_WRAP, |
| 234 |
CAPABILITY.NONE, |
| 235 |
raw_key, |
| 236 |
) |
| 237 |
|
| 238 |
if info.version >= (2, 5, 0): |
| 239 |
code = ERROR.INSUFFICIENT_PERMISSIONS |
| 240 |
else: |
| 241 |
code = ERROR.INVALID_DATA |
| 242 |
|
| 243 |
for ln in range(1, 64): |
| 244 |
data = os.urandom(ln) |
| 245 |
wrap = w_key.wrap_data(data) |
| 246 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 247 |
u_key.wrap_data(data) |
| 248 |
assert context.value.code == code |
| 249 |
plain = u_key.unwrap_data(wrap) |
| 250 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 251 |
w_key.unwrap_data(wrap) |
| 252 |
assert context.value.code == code |
| 253 |
assert data == plain |
| 254 |
|
| 255 |
u_key.delete() |
| 256 |
w_key.delete() |
| 257 |
|
| 258 |
|
| 259 |
def test_import_wrap_permissions(session): |
| 260 |
key_label = "wrap key" |
| 261 |
raw_key = os.urandom(24) |
| 262 |
opaque = Opaque.put( |
| 263 |
session, |
| 264 |
0, |
| 265 |
"Test Opaque Object", |
| 266 |
0xFFFF, |
| 267 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 268 |
ALGORITHM.OPAQUE_DATA, |
| 269 |
b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", |
| 270 |
) |
| 271 |
w_key = WrapKey.put( |
| 272 |
session, |
| 273 |
0, |
| 274 |
key_label, |
| 275 |
1, |
| 276 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 277 |
ALGORITHM.AES192_CCM_WRAP, |
| 278 |
CAPABILITY.NONE, |
| 279 |
raw_key, |
| 280 |
) |
| 281 |
|
| 282 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 283 |
w_key.export_wrapped(opaque) |
| 284 |
assert context.value.code == ERROR.INSUFFICIENT_PERMISSIONS |
| 285 |
|
| 286 |
w_key.delete() |
| 287 |
w_key = WrapKey.put( |
| 288 |
session, |
| 289 |
0, |
| 290 |
key_label, |
| 291 |
1, |
| 292 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 293 |
ALGORITHM.AES192_CCM_WRAP, |
| 294 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 295 |
raw_key, |
| 296 |
) |
| 297 |
|
| 298 |
w_key.id += 1 |
| 299 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 300 |
w_key.export_wrapped(opaque) |
| 301 |
assert context.value.code == ERROR.OBJECT_NOT_FOUND |
| 302 |
|
| 303 |
w_key.id -= 1 |
| 304 |
w_key.delete() |
| 305 |
w_key = WrapKey.put( |
| 306 |
session, |
| 307 |
0, |
| 308 |
key_label, |
| 309 |
1, |
| 310 |
CAPABILITY.IMPORT_WRAPPED, |
| 311 |
ALGORITHM.AES192_CCM_WRAP, |
| 312 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 313 |
raw_key, |
| 314 |
) |
| 315 |
|
| 316 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 317 |
w_key.export_wrapped(opaque) |
| 318 |
assert context.value.code == ERROR.INSUFFICIENT_PERMISSIONS |
| 319 |
|
| 320 |
w_key.delete() |
| 321 |
w_key = WrapKey.put( |
| 322 |
session, |
| 323 |
0, |
| 324 |
key_label, |
| 325 |
1, |
| 326 |
CAPABILITY.EXPORT_WRAPPED, |
| 327 |
ALGORITHM.AES192_CCM_WRAP, |
| 328 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 329 |
raw_key, |
| 330 |
) |
| 331 |
|
| 332 |
opaque_wrapped = w_key.export_wrapped(opaque) |
| 333 |
|
| 334 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 335 |
w_key.import_wrapped(opaque_wrapped) |
| 336 |
assert context.value.code == ERROR.INSUFFICIENT_PERMISSIONS |
| 337 |
|
| 338 |
w_key.delete() |
| 339 |
w_key = WrapKey.put( |
| 340 |
session, |
| 341 |
0, |
| 342 |
key_label, |
| 343 |
1, |
| 344 |
CAPABILITY.IMPORT_WRAPPED | CAPABILITY.EXPORT_WRAPPED, |
| 345 |
ALGORITHM.AES192_CCM_WRAP, |
| 346 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 347 |
raw_key, |
| 348 |
) |
| 349 |
|
| 350 |
opaque_wrapped = w_key.export_wrapped(opaque) |
| 351 |
opaque.delete() |
| 352 |
w_key.id += 1 |
| 353 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 354 |
w_key.import_wrapped(opaque_wrapped) |
| 355 |
assert context.value.code == ERROR.OBJECT_NOT_FOUND |
| 356 |
w_key.id -= 1 |
| 357 |
opaque = w_key.import_wrapped(opaque_wrapped) |
| 358 |
|
| 359 |
assert opaque.get() == b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 360 |
|
| 361 |
opaque.delete() |
| 362 |
w_key.delete() |
| 363 |
|
| 364 |
|
| 365 |
def test_import_wrap_overwrite(session): |
| 366 |
key_label = "wrap key" |
| 367 |
raw_key = os.urandom(24) |
| 368 |
w_key = WrapKey.put( |
| 369 |
session, |
| 370 |
0, |
| 371 |
key_label, |
| 372 |
1, |
| 373 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 374 |
ALGORITHM.AES192_CCM_WRAP, |
| 375 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 376 |
raw_key, |
| 377 |
) |
| 378 |
opaque = Opaque.put( |
| 379 |
session, |
| 380 |
0, |
| 381 |
"Test Opaque Object", |
| 382 |
0xFFFF, |
| 383 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 384 |
ALGORITHM.OPAQUE_DATA, |
| 385 |
b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", |
| 386 |
) |
| 387 |
opaque_wrapped = w_key.export_wrapped(opaque) |
| 388 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 389 |
w_key.import_wrapped(opaque_wrapped) |
| 390 |
assert context.value.code == ERROR.OBJECT_EXISTS |
| 391 |
|
| 392 |
opaque.delete() |
| 393 |
|
| 394 |
opaque = w_key.import_wrapped(opaque_wrapped) |
| 395 |
|
| 396 |
assert opaque.get() == b"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa" |
| 397 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 398 |
w_key.import_wrapped(opaque_wrapped) |
| 399 |
assert context.value.code == ERROR.OBJECT_EXISTS |
| 400 |
|
| 401 |
opaque.delete() |
| 402 |
w_key.delete() |
| 403 |
|
| 404 |
|
| 405 |
def test_import_invalid_key_size(session): |
| 406 |
# Key length must match algorithm |
| 407 |
with pytest.raises(ValueError): |
| 408 |
WrapKey.put( |
| 409 |
session, |
| 410 |
0, |
| 411 |
"Test PUT invalid algorithm", |
| 412 |
0xFFFF, |
| 413 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 414 |
ALGORITHM.AES128_CCM_WRAP, |
| 415 |
CAPABILITY.NONE, |
| 416 |
os.urandom(24), |
| 417 |
) |
| 418 |
|
| 419 |
|
| 420 |
def test_import_invalid_algorithm(session): |
| 421 |
# Algorithm must be AES128_CCM_WRAP, AES192_CCM_WRAP, AES256_CCM_WRAP, |
| 422 |
# RSA_2048, RSA_3072 or RSA_4096 |
| 423 |
with pytest.raises(ValueError): |
| 424 |
WrapKey.put( |
| 425 |
session, |
| 426 |
0, |
| 427 |
"Test PUT invalid algorithm", |
| 428 |
0xFFFF, |
| 429 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 430 |
ALGORITHM.AES128_YUBICO_OTP, |
| 431 |
CAPABILITY.NONE, |
| 432 |
os.urandom(16), |
| 433 |
) |
| 434 |
|
| 435 |
|
| 436 |
class TestAsymmetricWrap: |
| 437 |
@pytest.fixture(autouse=True) |
| 438 |
def prerequisites(self, info): |
| 439 |
if info.version < (2, 4, 0): |
| 440 |
pytest.skip("Asymmetric wrap requires 2.4.0") |
| 441 |
|
| 442 |
def generate_wrap_keys( |
| 443 |
self, |
| 444 |
session, |
| 445 |
export_capabilities, |
| 446 |
export_delegated_capabilities, |
| 447 |
import_delegated_capabilities, |
| 448 |
label, |
| 449 |
): |
| 450 |
key = rsa.generate_private_key( |
| 451 |
public_exponent=0x10001, key_size=2048, backend=default_backend() |
| 452 |
) |
| 453 |
|
| 454 |
export_wrapkey = PublicWrapKey.put( |
| 455 |
session, |
| 456 |
0, |
| 457 |
label, |
| 458 |
1, |
| 459 |
CAPABILITY.EXPORT_WRAPPED | export_capabilities, |
| 460 |
CAPABILITY.EXPORTABLE_UNDER_WRAP | export_delegated_capabilities, |
| 461 |
key.public_key(), |
| 462 |
) |
| 463 |
|
| 464 |
import_wrapkey = WrapKey.put( |
| 465 |
session, |
| 466 |
0, |
| 467 |
label, |
| 468 |
1, |
| 469 |
CAPABILITY.IMPORT_WRAPPED, |
| 470 |
ALGORITHM.RSA_2048, |
| 471 |
import_delegated_capabilities, |
| 472 |
key, |
| 473 |
) |
| 474 |
return export_wrapkey, import_wrapkey |
| 475 |
|
| 476 |
def test_wrap_asymmetric_key(self, session): |
| 477 |
export_wrapkey, import_wrapkey = self.generate_wrap_keys( |
| 478 |
session, |
| 479 |
CAPABILITY.SIGN_ECDSA, |
| 480 |
CAPABILITY.SIGN_ECDSA, |
| 481 |
CAPABILITY.SIGN_ECDSA, |
| 482 |
"Test Wrap Asymmetric Key", |
| 483 |
) |
| 484 |
|
| 485 |
asymkey = AsymmetricKey.generate( |
| 486 |
session, |
| 487 |
0, |
| 488 |
"Test Wrap Asymmetric Key", |
| 489 |
0xFFFF, |
| 490 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 491 |
ALGORITHM.EC_P256, |
| 492 |
) |
| 493 |
|
| 494 |
origin = asymkey.get_info().origin |
| 495 |
assert origin == ORIGIN.GENERATED |
| 496 |
|
| 497 |
pub = asymkey.get_public_key() |
| 498 |
|
| 499 |
data = os.urandom(64) |
| 500 |
resp = asymkey.sign_ecdsa(data) |
| 501 |
|
| 502 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 503 |
|
| 504 |
wrapped = export_wrapkey.export_raw_key(asymkey) |
| 505 |
|
| 506 |
wrapped2 = export_wrapkey.export_raw_key(asymkey) |
| 507 |
|
| 508 |
assert wrapped != wrapped2 |
| 509 |
|
| 510 |
asymkey.delete() |
| 511 |
|
| 512 |
pytest.raises(YubiHsmDeviceError, asymkey.get_public_key) |
| 513 |
|
| 514 |
asymkey = import_wrapkey.import_raw_key( |
| 515 |
0, |
| 516 |
OBJECT.ASYMMETRIC_KEY, |
| 517 |
"Test Wrap Asymmetric Key", |
| 518 |
1, |
| 519 |
CAPABILITY.SIGN_ECDSA, |
| 520 |
ALGORITHM.EC_P256, |
| 521 |
wrapped, |
| 522 |
) |
| 523 |
|
| 524 |
origin = asymkey.get_info().origin |
| 525 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.IMPORTED |
| 526 |
|
| 527 |
data = os.urandom(64) |
| 528 |
resp = asymkey.sign_ecdsa(data) |
| 529 |
assert resp is not None |
| 530 |
|
| 531 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 532 |
|
| 533 |
import_wrapkey.delete() |
| 534 |
export_wrapkey.delete() |
| 535 |
|
| 536 |
def test_wrap_symmetric_key(self, session): |
| 537 |
export_wrapkey, import_wrapkey = self.generate_wrap_keys( |
| 538 |
session, |
| 539 |
CAPABILITY.ENCRYPT_ECB | CAPABILITY.DECRYPT_ECB, |
| 540 |
CAPABILITY.DECRYPT_ECB | CAPABILITY.ENCRYPT_ECB, |
| 541 |
CAPABILITY.DECRYPT_ECB, |
| 542 |
"Test Wrap Symmetric Key", |
| 543 |
) |
| 544 |
|
| 545 |
symkey = SymmetricKey.generate( |
| 546 |
session, |
| 547 |
0, |
| 548 |
"Test Wrap Symmetric Key", |
| 549 |
1, |
| 550 |
CAPABILITY.ENCRYPT_ECB |
| 551 |
| CAPABILITY.DECRYPT_ECB |
| 552 |
| CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 553 |
ALGORITHM.AES256, |
| 554 |
) |
| 555 |
|
| 556 |
origin = symkey.get_info().origin |
| 557 |
assert origin == ORIGIN.GENERATED |
| 558 |
|
| 559 |
pt = os.urandom(256) |
| 560 |
ct = symkey.encrypt_ecb(pt) |
| 561 |
|
| 562 |
assert pt == symkey.decrypt_ecb(ct) |
| 563 |
|
| 564 |
wrapped = export_wrapkey.export_raw_key(symkey) |
| 565 |
|
| 566 |
wrapped2 = export_wrapkey.export_raw_key(symkey) |
| 567 |
|
| 568 |
assert wrapped != wrapped2 |
| 569 |
|
| 570 |
symkey.delete() |
| 571 |
|
| 572 |
symkey = import_wrapkey.import_raw_key( |
| 573 |
0, |
| 574 |
OBJECT.SYMMETRIC_KEY, |
| 575 |
"Test Wrap Symmetric Key", |
| 576 |
1, |
| 577 |
CAPABILITY.DECRYPT_ECB, |
| 578 |
ALGORITHM.AES256, |
| 579 |
wrapped, |
| 580 |
) |
| 581 |
|
| 582 |
origin = symkey.get_info().origin |
| 583 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.IMPORTED |
| 584 |
|
| 585 |
assert pt == symkey.decrypt_ecb(ct) |
| 586 |
|
| 587 |
symkey.delete() |
| 588 |
import_wrapkey.delete() |
| 589 |
export_wrapkey.delete() |
| 590 |
|
| 591 |
def test_export_wrap_rsa(self, session): |
| 592 |
export_wrapkey, import_wrapkey = self.generate_wrap_keys( |
| 593 |
session, |
| 594 |
CAPABILITY.SIGN_ECDSA, |
| 595 |
CAPABILITY.SIGN_ECDSA, |
| 596 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 597 |
"Test Export RSA", |
| 598 |
) |
| 599 |
|
| 600 |
asymkey = AsymmetricKey.generate( |
| 601 |
session, |
| 602 |
0, |
| 603 |
"Test Export RSA", |
| 604 |
0xFFFF, |
| 605 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 606 |
ALGORITHM.EC_P256, |
| 607 |
) |
| 608 |
|
| 609 |
origin = asymkey.get_info().origin |
| 610 |
assert origin == ORIGIN.GENERATED |
| 611 |
|
| 612 |
pub = asymkey.get_public_key() |
| 613 |
|
| 614 |
data = os.urandom(64) |
| 615 |
resp = asymkey.sign_ecdsa(data) |
| 616 |
|
| 617 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 618 |
|
| 619 |
wrapped = export_wrapkey.export_wrapped_rsa(asymkey) |
| 620 |
|
| 621 |
wrapped2 = export_wrapkey.export_wrapped_rsa(asymkey) |
| 622 |
|
| 623 |
assert wrapped != wrapped2 |
| 624 |
|
| 625 |
asymkey.delete() |
| 626 |
|
| 627 |
pytest.raises(YubiHsmDeviceError, asymkey.get_public_key) |
| 628 |
|
| 629 |
asymkey = import_wrapkey.import_wrapped_rsa(wrapped) |
| 630 |
|
| 631 |
origin = asymkey.get_info().origin |
| 632 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.GENERATED |
| 633 |
|
| 634 |
data = os.urandom(64) |
| 635 |
resp = asymkey.sign_ecdsa(data) |
| 636 |
assert resp is not None |
| 637 |
|
| 638 |
pub.verify(resp, data, ec.ECDSA(hashes.SHA256())) |
| 639 |
|
| 640 |
asymkey.delete() |
| 641 |
import_wrapkey.delete() |
| 642 |
export_wrapkey.delete() |
| 643 |
|
| 644 |
def test_export_using_private_wrapkey(self, session): |
| 645 |
_, private_wrapkey = self.generate_wrap_keys( |
| 646 |
session, |
| 647 |
CAPABILITY.SIGN_ECDSA, |
| 648 |
CAPABILITY.SIGN_ECDSA, |
| 649 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 650 |
"Test Export Using Private Wrapkey", |
| 651 |
) |
| 652 |
|
| 653 |
asymkey = AsymmetricKey.generate( |
| 654 |
session, |
| 655 |
0, |
| 656 |
"Test Export Using Private Wrapkey", |
| 657 |
0xFFFF, |
| 658 |
CAPABILITY.SIGN_ECDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 659 |
ALGORITHM.EC_P256, |
| 660 |
) |
| 661 |
|
| 662 |
with pytest.raises(YubiHsmDeviceError) as context: |
| 663 |
# The (private) wrap key is only used for importing |
| 664 |
# wrapped objects. |
| 665 |
private_wrapkey.export_wrapped(asymkey) |
| 666 |
assert context.value.code == ERROR.INVALID_DATA |
| 667 |
|
| 668 |
asymkey.delete() |
| 669 |
private_wrapkey.delete() |
| 670 |
|
| 671 |
def test_get_public_key(self, session): |
| 672 |
import_key = rsa.generate_private_key( |
| 673 |
public_exponent=0x10001, key_size=2048, backend=default_backend() |
| 674 |
) |
| 675 |
export_key = rsa.generate_private_key( |
| 676 |
public_exponent=0x10001, key_size=2048, backend=default_backend() |
| 677 |
) |
| 678 |
|
| 679 |
export_wrapkey = PublicWrapKey.put( |
| 680 |
session, |
| 681 |
0, |
| 682 |
"Test Get Public Key (PublicWrapKey)", |
| 683 |
1, |
| 684 |
CAPABILITY.EXPORT_WRAPPED, |
| 685 |
CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 686 |
export_key.public_key(), |
| 687 |
) |
| 688 |
import_wrapkey = WrapKey.put( |
| 689 |
session, |
| 690 |
0, |
| 691 |
"Test Get Public Key (WrapKey)", |
| 692 |
1, |
| 693 |
CAPABILITY.EXPORT_WRAPPED, |
| 694 |
ALGORITHM.RSA_2048, |
| 695 |
CAPABILITY.NONE, |
| 696 |
import_key, |
| 697 |
) |
| 698 |
|
| 699 |
import_pub = import_wrapkey.get_public_key() |
| 700 |
export_pub = export_wrapkey.get_public_key() |
| 701 |
assert import_pub.public_bytes( |
| 702 |
encoding=serialization.Encoding.PEM, |
| 703 |
format=serialization.PublicFormat.SubjectPublicKeyInfo, |
| 704 |
) == import_key.public_key().public_bytes( |
| 705 |
encoding=serialization.Encoding.PEM, |
| 706 |
format=serialization.PublicFormat.SubjectPublicKeyInfo, |
| 707 |
) |
| 708 |
assert export_pub.public_bytes( |
| 709 |
encoding=serialization.Encoding.PEM, |
| 710 |
format=serialization.PublicFormat.SubjectPublicKeyInfo, |
| 711 |
) == export_key.public_key().public_bytes( |
| 712 |
encoding=serialization.Encoding.PEM, |
| 713 |
format=serialization.PublicFormat.SubjectPublicKeyInfo, |
| 714 |
) |
| 715 |
import_wrapkey.delete() |
| 716 |
export_wrapkey.delete() |
| 717 |
|
| 718 |
def test_export_ed25519(self, session): |
| 719 |
wrapkey = WrapKey.put( |
| 720 |
session, |
| 721 |
0, |
| 722 |
"Test Export ED25519", |
| 723 |
1, |
| 724 |
CAPABILITY.EXPORT_WRAPPED | CAPABILITY.IMPORT_WRAPPED, |
| 725 |
ALGORITHM.AES192_CCM_WRAP, |
| 726 |
CAPABILITY.SIGN_EDDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 727 |
os.urandom(24), |
| 728 |
) |
| 729 |
|
| 730 |
edkey = ed25519.Ed25519PrivateKey.generate() |
| 731 |
|
| 732 |
asymkey = AsymmetricKey.put( |
| 733 |
session, |
| 734 |
0, |
| 735 |
"Test Export ED25519", |
| 736 |
0xFFFF, |
| 737 |
CAPABILITY.SIGN_EDDSA | CAPABILITY.EXPORTABLE_UNDER_WRAP, |
| 738 |
edkey, |
| 739 |
) |
| 740 |
origin = asymkey.get_info().origin |
| 741 |
assert origin == ORIGIN.IMPORTED |
| 742 |
|
| 743 |
data = os.urandom(64) |
| 744 |
resp = asymkey.sign_eddsa(data) |
| 745 |
|
| 746 |
edkey.public_key().verify(resp, data) |
| 747 |
|
| 748 |
wrapped = wrapkey.export_wrapped(asymkey) |
| 749 |
wrapped_with_seed = wrapkey.export_wrapped(asymkey, True) |
| 750 |
|
| 751 |
asymkey.delete() |
| 752 |
|
| 753 |
for w in [wrapped, wrapped_with_seed]: |
| 754 |
asymkey = wrapkey.import_wrapped(w) |
| 755 |
|
| 756 |
data = os.urandom(64) |
| 757 |
resp = asymkey.sign_eddsa(data) |
| 758 |
|
| 759 |
edkey.public_key().verify(resp, data) |
| 760 |
|
| 761 |
origin = asymkey.get_info().origin |
| 762 |
assert origin == ORIGIN.IMPORTED_WRAPPED | ORIGIN.IMPORTED |
| 763 |
|
| 764 |
asymkey.delete() |
| 765 |
|
| 766 |
wrapkey.delete() |