Skip to content

Commit 610533e

Browse files
committed
tests: Add wrap/unwrap test with RSA-OAEP
Signed-off-by: Jakub Jelen <jjelen@redhat.com>
1 parent dd80c07 commit 610533e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

cryptoki/tests/basic.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,100 @@ fn wrap_and_unwrap_key() {
13661366
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
13671367
}
13681368

1369+
#[test]
1370+
#[serial]
1371+
fn wrap_and_unwrap_key_oaep() {
1372+
let (pkcs11, slot) = init_pins();
1373+
// open a session
1374+
let session = pkcs11.open_rw_session(slot).unwrap();
1375+
1376+
// log in the session
1377+
session
1378+
.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))
1379+
.unwrap();
1380+
1381+
let key_to_be_wrapped_template = vec![
1382+
Attribute::Token(true),
1383+
Attribute::ValueLen(32.into()),
1384+
// the key needs to be extractable to be suitable for being wrapped
1385+
Attribute::Extractable(true),
1386+
Attribute::Encrypt(true),
1387+
];
1388+
1389+
// generate a secret key that will be wrapped
1390+
let key_to_be_wrapped = session
1391+
.generate_key(&Mechanism::AesKeyGen, &key_to_be_wrapped_template)
1392+
.unwrap();
1393+
1394+
// AesEcb input length must be a multiple of 16
1395+
let encrypted_with_original = session
1396+
.encrypt(
1397+
&Mechanism::AesEcb,
1398+
key_to_be_wrapped,
1399+
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1400+
)
1401+
.unwrap();
1402+
1403+
// pub key template
1404+
let pub_key_template = vec![
1405+
Attribute::Token(true),
1406+
Attribute::Private(true),
1407+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
1408+
Attribute::ModulusBits(2048.into()),
1409+
// key needs to have "wrap" attribute to wrap other keys
1410+
Attribute::Wrap(true),
1411+
];
1412+
1413+
// priv key template
1414+
let priv_key_template = vec![Attribute::Token(true), (Attribute::Unwrap(true))];
1415+
1416+
let (wrapping_key, unwrapping_key) = session
1417+
.generate_key_pair(
1418+
&Mechanism::RsaPkcsKeyPairGen,
1419+
&pub_key_template,
1420+
&priv_key_template,
1421+
)
1422+
.unwrap();
1423+
1424+
let oaep = PkcsOaepParams::new(
1425+
MechanismType::SHA1,
1426+
PkcsMgfType::MGF1_SHA1,
1427+
PkcsOaepSource::empty(),
1428+
);
1429+
let wrapped_key = session
1430+
.wrap_key(
1431+
&Mechanism::RsaPkcsOaep(oaep),
1432+
wrapping_key,
1433+
key_to_be_wrapped,
1434+
)
1435+
.unwrap();
1436+
assert_eq!(wrapped_key.len(), 256);
1437+
1438+
let unwrapped_key = session
1439+
.unwrap_key(
1440+
&Mechanism::RsaPkcsOaep(oaep),
1441+
unwrapping_key,
1442+
&wrapped_key,
1443+
&[
1444+
Attribute::Token(true),
1445+
Attribute::Private(true),
1446+
Attribute::Encrypt(true),
1447+
Attribute::Class(ObjectClass::SECRET_KEY),
1448+
Attribute::KeyType(KeyType::AES),
1449+
],
1450+
)
1451+
.unwrap();
1452+
1453+
let encrypted_with_unwrapped = session
1454+
.encrypt(
1455+
&Mechanism::AesEcb,
1456+
unwrapped_key,
1457+
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1458+
)
1459+
.unwrap();
1460+
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
1461+
}
1462+
13691463
#[test]
13701464
#[serial]
13711465
fn login_feast() {

0 commit comments

Comments
 (0)