Skip to content

Commit d32bc62

Browse files
committed
tests: Add wrap/unwrap test with RSA-OAEP
Signed-off-by: Jakub Jelen <[email protected]>
1 parent 8feb4d0 commit d32bc62

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

cryptoki/tests/basic.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,97 @@ fn wrap_and_unwrap_key() {
10811081
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
10821082
}
10831083

1084+
#[test]
1085+
#[serial]
1086+
fn wrap_and_unwrap_key_oaep() {
1087+
let (pkcs11, slot) = init_pins();
1088+
// open a session
1089+
let session = pkcs11.open_rw_session(slot).unwrap();
1090+
1091+
// log in the session
1092+
session
1093+
.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))
1094+
.unwrap();
1095+
1096+
let key_to_be_wrapped_template = vec![
1097+
Attribute::Token(true),
1098+
Attribute::ValueLen(32.into()),
1099+
// the key needs to be extractable to be suitable for being wrapped
1100+
Attribute::Extractable(true),
1101+
Attribute::Encrypt(true),
1102+
];
1103+
1104+
// generate a secret key that will be wrapped
1105+
let key_to_be_wrapped = session
1106+
.generate_key(&Mechanism::AesKeyGen, &key_to_be_wrapped_template)
1107+
.unwrap();
1108+
1109+
// AesEcb input length must be a multiple of 16
1110+
let encrypted_with_original = session
1111+
.encrypt(
1112+
&Mechanism::AesEcb,
1113+
key_to_be_wrapped,
1114+
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1115+
)
1116+
.unwrap();
1117+
1118+
// pub key template
1119+
let pub_key_template = vec![
1120+
Attribute::Token(true),
1121+
Attribute::Private(true),
1122+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
1123+
Attribute::ModulusBits(2048.into()),
1124+
// key needs to have "wrap" attribute to wrap other keys
1125+
Attribute::Wrap(true),
1126+
];
1127+
1128+
// priv key template
1129+
let priv_key_template = vec![Attribute::Token(true), (Attribute::Unwrap(true))];
1130+
1131+
let (wrapping_key, unwrapping_key) = session
1132+
.generate_key_pair(
1133+
&Mechanism::RsaPkcsKeyPairGen,
1134+
&pub_key_template,
1135+
&priv_key_template,
1136+
)
1137+
.unwrap();
1138+
1139+
let oaep = PkcsOaepParams::new(
1140+
MechanismType::SHA1,
1141+
PkcsMgfType::MGF1_SHA1,
1142+
PkcsOaepSource::empty(),
1143+
);
1144+
let wrapped_key = session
1145+
.wrap_key(&Mechanism::RsaPkcsOaep(oaep), wrapping_key, key_to_be_wrapped)
1146+
.unwrap();
1147+
assert_eq!(wrapped_key.len(), 256);
1148+
1149+
let unwrapped_key = session
1150+
.unwrap_key(
1151+
&Mechanism::RsaPkcsOaep(oaep),
1152+
unwrapping_key,
1153+
&wrapped_key,
1154+
&[
1155+
Attribute::Token(true),
1156+
Attribute::Private(true),
1157+
Attribute::Encrypt(true),
1158+
Attribute::Class(ObjectClass::SECRET_KEY),
1159+
Attribute::KeyType(KeyType::AES),
1160+
],
1161+
)
1162+
.unwrap();
1163+
1164+
let encrypted_with_unwrapped = session
1165+
.encrypt(
1166+
&Mechanism::AesEcb,
1167+
unwrapped_key,
1168+
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
1169+
)
1170+
.unwrap();
1171+
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
1172+
}
1173+
1174+
10841175
#[test]
10851176
#[serial]
10861177
fn login_feast() {

0 commit comments

Comments
 (0)