forked from latchset/pkcs11-provider
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AES Ciphers and key management using EVP_SKEY
Signed-off-by: Simo Sorce <[email protected]>
- Loading branch information
Showing
9 changed files
with
698 additions
and
1 deletion.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright (C) 2024 Simo Sorce <[email protected]> | ||
SPDX-License-Identifier: Apache-2.0 */ | ||
|
||
#ifndef _CIPHER_H | ||
#define _CIPHER_H | ||
|
||
#define DISPATCH_CIPHER_FN(alg, name) \ | ||
DECL_DISPATCH_FUNC(cipher, p11prov_##alg, name) | ||
|
||
#define DISPATCH_TABLE_CIPHER_FN(cipher, size, mode, mechanism) \ | ||
static void *p11prov_##cipher##size##mode##_newctx(void *provctx) \ | ||
{ \ | ||
return p11prov_##cipher##_newctx(provctx, size, mechanism); \ | ||
} \ | ||
static int p11prov_##cipher##size##mode##_get_params(OSSL_PARAM params[]) \ | ||
{ \ | ||
return p11prov_##cipher##_get_params(params, size, mechanism); \ | ||
} \ | ||
static const OSSL_PARAM *p11prov_##cipher##size##mode##_gettable_params(\ | ||
void *provctx) \ | ||
{ \ | ||
return p11prov_aes_gettable_params(provctx, size, mechanism); \ | ||
} \ | ||
const OSSL_DISPATCH ossl_##cipher##size##mode##_functions[] = { \ | ||
{ OSSL_FUNC_CIPHER_NEWCTX, \ | ||
(void (*)(void)) p11prov_##cipher##size##mode##_newctx }, \ | ||
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) p11prov_cipher_freectx }, \ | ||
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) p11prov_##cipher##_dupctx }, \ | ||
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, \ | ||
(void (*)(void))p11prov_cipher_encrypt_init }, \ | ||
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, \ | ||
(void (*)(void))p11prov_cipher_decrypt_init }, \ | ||
{ OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))p11prov_cipher_update }, \ | ||
{ OSSL_FUNC_CIPHER_FINAL, (void (*)(void))p11prov_cipher_final }, \ | ||
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))p11prov_##cipher##_cipher }, \ | ||
{ OSSL_FUNC_CIPHER_GET_PARAMS, \ | ||
(void (*)(void)) p11prov_##cipher##size##mode##_get_params }, \ | ||
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \ | ||
(void (*)(void))p11prov_##cipher##_get_ctx_params }, \ | ||
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \ | ||
(void (*)(void))p11prov_##cipher##_set_ctx_params }, \ | ||
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \ | ||
(void (*)(void))p11prov_##cipher##size##mode##_gettable_params }, \ | ||
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \ | ||
(void (*)(void))p11prov_##cipher##_gettable_ctx_params }, \ | ||
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \ | ||
(void (*)(void))p11prov_##cipher##_settable_ctx_params }, \ | ||
{ OSSL_FUNC_CIPHER_ENCRYPT_SKEY_INIT, \ | ||
(void (*)(void))p11prov_cipher_encrypt_skey_init }, \ | ||
{ OSSL_FUNC_CIPHER_DECRYPT_SKEY_INIT, \ | ||
(void (*)(void))p11prov_cipher_decrypt_skey_init }, \ | ||
OSSL_DISPATCH_END \ | ||
}; | ||
|
||
extern const OSSL_DISPATCH p11prov_aes128ecb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192ecb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256ecb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128cbc_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192cbc_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256cbc_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128ofb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192ofb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256ofb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128cfb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192cfb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256cfb_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128cfb1_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192cfb1_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256cfb1_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128cfb8_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192cfb8_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256cfb8_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes128ctr_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes192ctr_functions[]; | ||
extern const OSSL_DISPATCH p11prov_aes256ctr_functions[]; | ||
|
||
#endif /* _CIPHER_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* Copyright (C) 2024 Simo Sorce <[email protected]> | ||
SPDX-License-Identifier: Apache-2.0 */ | ||
|
||
#include "provider.h" | ||
#include "platform/endian.h" | ||
#include <string.h> | ||
|
||
DISPATCH_SKEYMGMT_FN(aes, free); | ||
DISPATCH_SKEYMGMT_FN(aes, import); | ||
DISPATCH_SKEYMGMT_FN(aes, export); | ||
|
||
static void p11prov_aes_free(void *key) | ||
{ | ||
P11PROV_debug("aes free %p", key); | ||
p11prov_obj_free((P11PROV_OBJ *)key); | ||
} | ||
|
||
static void *p11prov_aes_import(void *provctx, int selection, | ||
const OSSL_PARAM params[]) | ||
{ | ||
P11PROV_CTX *ctx = (P11PROV_CTX *)provctx; | ||
|
||
P11PROV_debug("aes import"); | ||
|
||
if (!ctx) { | ||
return NULL; | ||
} | ||
|
||
if (!(selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY)) { | ||
/* TODO: check for hack import uri */ | ||
return NULL; | ||
} | ||
|
||
return p11prov_obj_import_secret_key(ctx, CKK_AES, params); | ||
} | ||
|
||
static int p11prov_aes_export(void *keydata, int selection, | ||
OSSL_CALLBACK *param_cb, void *cbarg) | ||
{ | ||
P11PROV_OBJ *key = (P11PROV_OBJ *)keydata; | ||
|
||
P11PROV_raise(p11prov_obj_get_prov_ctx(key), | ||
CKR_KEY_FUNCTION_NOT_PERMITTED, | ||
"Not exportable"); | ||
|
||
return RET_OSSL_ERR; | ||
} | ||
|
||
const OSSL_DISPATCH p11prov_aes_skeymgmt_functions[] = { | ||
DISPATCH_SKEYMGMT_ELEM(aes, FREE, free), | ||
DISPATCH_SKEYMGMT_ELEM(aes, IMPORT, import), | ||
DISPATCH_SKEYMGMT_ELEM(aes, EXPORT, export), | ||
{ 0, NULL }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* Copyright (C) 2024 Simo Sorce <[email protected]> | ||
SPDX-License-Identifier: Apache-2.0 */ | ||
|
||
#ifndef _SKEYMGMT_H | ||
#define _SKEYMGMT_H | ||
|
||
/* keymgmt */ | ||
#define DISPATCH_SKEYMGMT_FN(type, name) \ | ||
DECL_DISPATCH_FUNC(skeymgmt, p11prov_##type, name) | ||
#define DISPATCH_SKEYMGMT_ELEM(type, NAME, name) \ | ||
{ \ | ||
OSSL_FUNC_SKEYMGMT_##NAME, (void (*)(void))p11prov_##type##_##name \ | ||
} | ||
extern const OSSL_DISPATCH p11prov_aes_skeymgmt_functions[]; | ||
|
||
#endif /* _SKEYMGMT_H */ | ||
|