Skip to content

Commit b3651c9

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

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
@@ -1083,6 +1083,100 @@ fn wrap_and_unwrap_key() {
10831083
assert_eq!(encrypted_with_original, encrypted_with_unwrapped);
10841084
}
10851085

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

0 commit comments

Comments
 (0)