|
|
@ -70,38 +70,127 @@ __RCSID("$NetBSD$"); |
|
|
|
|
|
|
|
#define TRAILER "","","","",0,NULL,NULL |
|
|
|
|
|
|
|
static void |
|
|
|
cryptoki_init(pgp_crypt_t *crypt, CK_KEY_TYPE keyType, CK_MECHANISM *mechanism ) |
|
|
|
{ |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
CK_OBJECT_HANDLE key; |
|
|
|
CK_OBJECT_CLASS keyClass = CKO_SECRET_KEY; |
|
|
|
CK_BBOOL true = TRUE, false = FALSE; |
|
|
|
CK_UTF8CHAR label[] = "test"; |
|
|
|
CK_ULONG len = crypt->keysize; |
|
|
|
|
|
|
|
CK_ATTRIBUTE template[] = { |
|
|
|
{CKA_CLASS, &keyClass, sizeof(keyClass) }, |
|
|
|
{CKA_KEY_TYPE, &keyType, sizeof(keyType) }, |
|
|
|
{CKA_LABEL, label, sizeof(label) }, |
|
|
|
{CKA_ENCRYPT, &true, sizeof(true) }, |
|
|
|
{CKA_VALUE, crypt->key, len } |
|
|
|
}; |
|
|
|
|
|
|
|
crypt->session = malloc(sizeof(CK_SESSION_HANDLE)); |
|
|
|
memset(crypt->session, 0, sizeof(CK_SESSION_HANDLE)); |
|
|
|
|
|
|
|
rv = getSession( crypt->session ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "getSession: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
|
|
|
|
rv = funcs->C_CreateObject( *crypt->session, template, sizeof(template)/sizeof(CK_ATTRIBUTE), &key); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_CreateObject: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
|
|
|
|
rv = funcs->C_EncryptInit( *crypt->session, mechanism, key); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_EncryptInit: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
|
|
|
|
static int |
|
|
|
cast5_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
cast5_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
cryptoki_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
printf("%s:%d\n",__FILE__,__LINE__); |
|
|
|
CK_ULONG len = crypt->blocksize; |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
hexdump(stdout, "cryptoki_block_encrypt input", in, crypt->blocksize); |
|
|
|
rv = funcs->C_Encrypt( *crypt->session, in, crypt->blocksize, out, &len ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_Encrypt: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
hexdump(stdout, "cryptoki_block_encrypt output", out, len); |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
cast5_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
cryptoki_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
printf("%s:%d\n",__FILE__,__LINE__); |
|
|
|
CK_ULONG len; |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
rv = funcs->C_Encrypt( *crypt->session, (CK_BYTE_PTR)in, crypt->num, out, &len ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_Encrypt: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
cast5_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
cryptoki_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
printf("%s:%d\n",__FILE__,__LINE__); |
|
|
|
CK_ULONG len; |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
hexdump(stdout, "cryptoki_cfb_encrypt input", in, crypt->blocksize); |
|
|
|
rv = funcs->C_Encrypt( *crypt->session, (CK_BYTE_PTR)in, count, out, &len ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_Encrypt: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
hexdump(stdout, "cryptoki_cfb_encrypt output", out, len); |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
cast5_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
cryptoki_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
printf("%s:%d\n",__FILE__,__LINE__); |
|
|
|
CK_ULONG len; |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
rv = funcs->C_Encrypt( *crypt->session, (CK_BYTE_PTR)in, count, out, &len ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_Encrypt: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
cryptoki_finish(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
CK_RV rv = CKR_OK; |
|
|
|
|
|
|
|
rv = funcs->C_CloseSession( *crypt->session ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_CloseSession: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
|
|
|
|
rv = funcs->C_Finalize( NULL ); |
|
|
|
if( rv != CKR_OK) { |
|
|
|
fprintf(stderr, "C_Finalize: rv = 0x%.8lX\n", rv); |
|
|
|
} |
|
|
|
|
|
|
|
free( crypt->session ); |
|
|
|
} |
|
|
|
|
|
|
|
static int |
|
|
|
cast5_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
CK_MECHANISM mechanism = { |
|
|
|
CKM_CAST5_ECB, |
|
|
|
NULL, |
|
|
|
0 |
|
|
|
}; |
|
|
|
cryptoki_init( crypt, CKK_CAST5, &mechanism ); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static pgp_crypt_t cast5 = |
|
|
|
{ |
|
|
|
PGP_SA_CAST5, |
|
|
@ -111,40 +200,26 @@ static pgp_crypt_t cast5 = |
|
|
|
std_set_key, |
|
|
|
cast5_init, |
|
|
|
std_resync, |
|
|
|
cast5_block_encrypt, |
|
|
|
cast5_block_decrypt, |
|
|
|
cast5_cfb_encrypt, |
|
|
|
cast5_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
|
static int |
|
|
|
idea_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
CK_MECHANISM mechanism = { |
|
|
|
CKM_IDEA_ECB, |
|
|
|
NULL, |
|
|
|
0 |
|
|
|
}; |
|
|
|
cryptoki_init( crypt, CKK_IDEA, &mechanism ); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
idea_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
idea_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
idea_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
idea_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static const pgp_crypt_t idea = |
|
|
|
{ |
|
|
|
PGP_SA_IDEA, |
|
|
@ -154,11 +229,11 @@ static const pgp_crypt_t idea = |
|
|
|
std_set_key, |
|
|
|
idea_init, |
|
|
|
std_resync, |
|
|
|
idea_block_encrypt, |
|
|
|
idea_block_decrypt, |
|
|
|
idea_cfb_encrypt, |
|
|
|
idea_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
@ -169,29 +244,12 @@ static const pgp_crypt_t idea = |
|
|
|
static int |
|
|
|
aes128_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
CK_BYTE_PTR pData; |
|
|
|
CK_MECHANISM mechanism = { CKM_AES_ECB, 0, 0 }; |
|
|
|
cryptoki_init( crypt, CKK_AES, &mechanism ); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
aes_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
aes_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
aes_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
aes_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static const pgp_crypt_t aes128 = |
|
|
|
{ |
|
|
|
PGP_SA_AES_128, |
|
|
@ -201,11 +259,11 @@ static const pgp_crypt_t aes128 = |
|
|
|
std_set_key, |
|
|
|
aes128_init, |
|
|
|
std_resync, |
|
|
|
aes_block_encrypt, |
|
|
|
aes_block_decrypt, |
|
|
|
aes_cfb_encrypt, |
|
|
|
aes_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
@ -216,6 +274,7 @@ static const pgp_crypt_t aes128 = |
|
|
|
static int |
|
|
|
aes256_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
@ -228,11 +287,11 @@ static const pgp_crypt_t aes256 = |
|
|
|
std_set_key, |
|
|
|
aes256_init, |
|
|
|
std_resync, |
|
|
|
aes_block_encrypt, |
|
|
|
aes_block_decrypt, |
|
|
|
aes_cfb_encrypt, |
|
|
|
aes_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
@ -241,31 +300,10 @@ static const pgp_crypt_t aes256 = |
|
|
|
static int |
|
|
|
tripledes_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
tripledes_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
tripledes_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
tripledes_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, |
|
|
|
size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
tripledes_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, |
|
|
|
size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static const pgp_crypt_t tripledes = |
|
|
|
{ |
|
|
|
PGP_SA_TRIPLEDES, |
|
|
@ -275,11 +313,11 @@ static const pgp_crypt_t tripledes = |
|
|
|
std_set_key, |
|
|
|
tripledes_init, |
|
|
|
std_resync, |
|
|
|
tripledes_block_encrypt, |
|
|
|
tripledes_block_decrypt, |
|
|
|
tripledes_cfb_encrypt, |
|
|
|
tripledes_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
@ -288,29 +326,10 @@ static const pgp_crypt_t tripledes = |
|
|
|
static int |
|
|
|
camellia128_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
camellia_block_encrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
camellia_block_decrypt(pgp_crypt_t *crypt, void *out, const void *in) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
camellia_cfb_encrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static void |
|
|
|
camellia_cfb_decrypt(pgp_crypt_t *crypt, void *out, const void *in, size_t count) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
static const pgp_crypt_t camellia128 = |
|
|
|
{ |
|
|
|
PGP_SA_CAMELLIA_128, |
|
|
@ -320,11 +339,11 @@ static const pgp_crypt_t camellia128 = |
|
|
|
std_set_key, |
|
|
|
camellia128_init, |
|
|
|
std_resync, |
|
|
|
camellia_block_encrypt, |
|
|
|
camellia_block_decrypt, |
|
|
|
camellia_cfb_encrypt, |
|
|
|
camellia_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
@ -335,6 +354,7 @@ static const pgp_crypt_t camellia128 = |
|
|
|
static int |
|
|
|
camellia256_init(pgp_crypt_t *crypt) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
@ -347,21 +367,22 @@ static const pgp_crypt_t camellia256 = |
|
|
|
std_set_key, |
|
|
|
camellia256_init, |
|
|
|
std_resync, |
|
|
|
camellia_block_encrypt, |
|
|
|
camellia_block_decrypt, |
|
|
|
camellia_cfb_encrypt, |
|
|
|
camellia_cfb_decrypt, |
|
|
|
std_finish, |
|
|
|
cryptoki_block_encrypt, |
|
|
|
cryptoki_block_decrypt, |
|
|
|
cryptoki_cfb_encrypt, |
|
|
|
cryptoki_cfb_decrypt, |
|
|
|
cryptoki_finish, |
|
|
|
TRAILER |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
The list of algorithms for z/OS can be seen here: |
|
|
|
https://www.ibm.com/docs/en/zos/2.1.0?topic=api-key-types-mechanisms-supported |
|
|
|
*/ |
|
|
|
const pgp_crypt_t * |
|
|
|
get_proto(pgp_symm_alg_t alg) |
|
|
|
{ |
|
|
|
switch (alg) { |
|
|
|
case PGP_SA_CAST5: |
|
|
|
return &cast5; |
|
|
|
case PGP_SA_IDEA: |
|
|
|
return &idea; |
|
|
|
case PGP_SA_AES_128: |
|
|
@ -374,9 +395,10 @@ get_proto(pgp_symm_alg_t alg) |
|
|
|
return &camellia256; |
|
|
|
case PGP_SA_TRIPLEDES: |
|
|
|
return &tripledes; |
|
|
|
case PGP_SA_CAST5: |
|
|
|
return &cast5; |
|
|
|
default: |
|
|
|
(void) fprintf(stderr, "get_proto: Unknown algorithm: %d (%s)\n", |
|
|
|
alg, pgp_show_symm_alg(alg)); |
|
|
|
(void) fprintf(stderr, "get_proto: Unknown algorithm: %d (%s)\n", alg, pgp_show_symm_alg(alg)); |
|
|
|
} |
|
|
|
return NULL; |
|
|
|
} |
|
|
@ -387,17 +409,25 @@ get_proto(pgp_symm_alg_t alg) |
|
|
|
\param alg Symmetric Algorithm to check |
|
|
|
\return 1 if supported; else 0 |
|
|
|
*/ |
|
|
|
/* |
|
|
|
IBM z/OS is the worst platform for such stuff ever, half |
|
|
|
the algorithms aren't implemented. |
|
|
|
Here's a list of the few which are, in case you've got a crash |
|
|
|
here and wanna have a look at the desert of the real worl yoursel: |
|
|
|
https://www.ibm.com/docs/en/zos/2.1.0?topic=api-key-types-mechanisms-supported |
|
|
|
*/ |
|
|
|
unsigned |
|
|
|
pgp_is_sa_supported(pgp_symm_alg_t alg) |
|
|
|
{ |
|
|
|
printf("%s:%d, %s\n",__FILE__,__LINE__,__FUNCTION__); |
|
|
|
switch (alg) { |
|
|
|
case PGP_SA_AES_128: |
|
|
|
case PGP_SA_AES_256: |
|
|
|
case PGP_SA_CAST5: |
|
|
|
case PGP_SA_TRIPLEDES: |
|
|
|
case PGP_SA_CAMELLIA_128: |
|
|
|
case PGP_SA_CAMELLIA_256: |
|
|
|
case PGP_SA_IDEA: |
|
|
|
//case PGP_SA_IDEA: |
|
|
|
//case PGP_SA_CAST5: |
|
|
|
return 1; |
|
|
|
|
|
|
|
default: |
|
|
|