Crypt-Cryptoki

 view release on metacpan or  search on metacpan

t/cryptoki.t  view on Meta::CPAN


my $t_private = Crypt::Cryptoki::Template::RSAPrivateKey->new(
	token => 1,
	decrypt => 1,
	sign => 1,
	unwrap => 1,
	label => 'test',
	id => pack('C*', 0x01, 0x02, 0x03)
);

my ( $public_key, $private_key ) = $session->generate_key_pair($t_public,$t_private);

try {
	my $plain_text = 'plain text';
	my ( $encrypted_text_ref, $len ) = $public_key->encrypt(\$plain_text, length($plain_text));

	( $encrypted_text_ref ) = $private_key->decrypt($encrypted_text_ref, $len);
	diag $$encrypted_text_ref;

	diag explain $public_key->get_attributes(1);
	diag $public_key->export_as_string;

	diag explain $private_key->get_attributes(1);

} catch { diag $_; 0 };

ok $public_key->destroy;
ok $private_key->destroy;

done_testing();

t/openssl.t  view on Meta::CPAN

    [ CKA_TOKEN,           pack('C',TRUE) ],
    [ CKA_ENCRYPT,         pack('C',TRUE) ],
    [ CKA_VERIFY,          pack('C',TRUE) ],
    [ CKA_WRAP,            pack('C',TRUE) ],
    [ CKA_MODULUS_BITS,    pack('Q',4096) ],
    [ CKA_PUBLIC_EXPONENT, pack('C*', 0x01, 0x00, 0x01) ],
    [ CKA_LABEL,    	   'test_pub' ],
	[ CKA_ID, 			   pack('C*', 0x01, 0x02, 0x03) ],
];

my $private_key_template = [
    [ CKA_CLASS,      pack('Q',CKO_PRIVATE_KEY) ],
    [ CKA_KEY_TYPE,   pack('Q',CKK_RSA) ],
    [ CKA_TOKEN,      pack('C',TRUE) ],
    [ CKA_PRIVATE,    pack('C',TRUE) ],
    [ CKA_SENSITIVE,  pack('C',TRUE) ],
    [ CKA_DECRYPT,    pack('C',TRUE) ],
    [ CKA_SIGN,       pack('C',TRUE) ],
    [ CKA_UNWRAP,     pack('C',TRUE) ],
    [ CKA_LABEL,      'test' ],
	[ CKA_ID, 		  pack('C*', 0x04, 0x05, 0x06) ],
];

my $private_key = -1;
my $public_key = -1;

is rv_to_str($f->C_GenerateKeyPair(
	$session, 
	[ CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 ], 
	$public_key_template,
    $private_key_template,
	$public_key, 
	$private_key
)), 'CKR_OK', 'C_GenerateKeyPair';

my $get_attributes_template = [
    [ CKA_MODULUS, '' ],
    [ CKA_PUBLIC_EXPONENT, '' ],
];

is rv_to_str($f->C_GetAttributeValue(
	$session,
	$public_key,

t/openssl.t  view on Meta::CPAN

diag $rsa_pub->get_public_key_string;
my $encrypted_text = $rsa_pub->encrypt($plain_text);
my $encrypted_text_len = length($encrypted_text);

#diag 'enc: ', unpack('H*', $encrypted_text);
#diag 'len: ', $encrypted_text_len;

is rv_to_str($f->C_DecryptInit(
	$session, 
	[ CKM_RSA_PKCS, NULL_PTR, 0 ], 
	$private_key, 
)), 'CKR_OK', 'C_DecryptInit';

my $decrypted_text = '';
my $decrypted_text_len = 0;
is rv_to_str($f->C_Decrypt(
	$session, 
	$encrypted_text,
	$encrypted_text_len,
	$decrypted_text,
	$decrypted_text_len,
)), 'CKR_OK', 'C_Decrypt';

is $decrypted_text, $plain_text, 'decryption OK';

is $f->C_DestroyObject($session, $public_key), CKR_OK, 'destroy public key';
is $f->C_DestroyObject($session, $private_key), CKR_OK, 'destroy private key';

done_testing();








t/softhsm.t  view on Meta::CPAN

    [ CKA_TOKEN,           pack('C',1) ],
    [ CKA_ENCRYPT,         pack('C',1) ],
    [ CKA_VERIFY,          pack('C',1) ],
    [ CKA_WRAP,            pack('C',1) ],
    [ CKA_MODULUS_BITS,    pack('Q',4096) ],
    [ CKA_PUBLIC_EXPONENT, pack('C*', 0x01, 0x00, 0x01) ],
    [ CKA_LABEL,    	   'test_pub' ],
	[ CKA_ID, 			   pack('C*', 0x01, 0x02, 0x03) ],
];

my $private_key_template = [
    [ CKA_CLASS,      pack('Q',CKO_PRIVATE_KEY) ],
    [ CKA_KEY_TYPE,   pack('Q',CKK_RSA) ],
    [ CKA_TOKEN,      pack('C',1) ],
    [ CKA_PRIVATE,    pack('C',1) ],
    [ CKA_SENSITIVE,  pack('C',1) ],
    [ CKA_DECRYPT,    pack('C',1) ],
    [ CKA_SIGN,       pack('C',1) ],
    [ CKA_UNWRAP,     pack('C',1) ],
    [ CKA_LABEL,      'test' ],
	[ CKA_ID, 		  pack('C*', 0x04, 0x05, 0x06) ],
];

my $private_key = -1;
my $public_key = -1;

is rv_to_str($f->C_GenerateKeyPair(
	$session, 
	[ CKM_RSA_PKCS_KEY_PAIR_GEN, undef, 0 ], 
	$public_key_template,
    $private_key_template,
	$public_key, 
	$private_key
)), 'CKR_OK', 'C_GenerateKeyPair';

diag $public_key;
diag $private_key;


is rv_to_str($f->C_EncryptInit(
	$session, 
	[ CKM_RSA_PKCS, undef, 0 ], 
	$public_key, 
)), 'CKR_OK', 'C_EncryptInit';

my $plain_text = 'plain text';
my $encrypted_text = '';

t/softhsm.t  view on Meta::CPAN

	$plain_text,
	length($plain_text),
	$encrypted_text,
	$encrypted_text_len
)), 'CKR_OK', 'C_Encrypt';
diag unpack('H*',$encrypted_text);

is rv_to_str($f->C_DecryptInit(
	$session, 
	[ CKM_RSA_PKCS, undef, 0 ], 
	$private_key, 
)), 'CKR_OK', 'C_DecryptInit';


my $decrypted_text = '';
my $decrypted_text_len = 0;
is rv_to_str($f->C_Decrypt(
	$session, 
	$encrypted_text,
	$encrypted_text_len,
	$decrypted_text,
	$decrypted_text_len,
)), 'CKR_OK', 'C_Decrypt';
diag $decrypted_text;

is $decrypted_text, $plain_text, 'decrypt: "plain text"';


is rv_to_str($f->C_SignInit(
	$session, 
	[ CKM_SHA256_RSA_PKCS, undef, 0 ], 
	$private_key, 
)), 'CKR_OK', 'C_SignInit';

my $signature = '';
my $signature_len = 0;
is rv_to_str($f->C_Sign(
	$session, 
	$plain_text,
	length($plain_text),
	$signature,
	$signature_len,

t/softhsm.t  view on Meta::CPAN

is rv_to_str($f->C_GetAttributeValue(
	$session,
	$public_key,
	$get_attributes_template
)), 'CKR_OK', 'C_GetAttributeValue';
	
diag 'modulus: ', unpack('H*', $get_attributes_template->[0][1]);
diag 'exponent: ', unpack('H*', $get_attributes_template->[1][1]);

is $f->C_DestroyObject($session, $public_key), CKR_OK, 'destroy public key';
is $f->C_DestroyObject($session, $private_key), CKR_OK, 'destroy private key';

done_testing();



( run in 0.262 second using v1.01-cache-2.11-cpan-4d50c553e7e )