-
Notifications
You must be signed in to change notification settings - Fork 4
Hmac support #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Hmac support #27
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
661d319
HMAC: detect MSA 11
jschmidb a8170b4
HMAC: introduce new error constants
jschmidb fdfaeb1
HMAC: introduce new API for hmac keys
jschmidb ab45b20
HMAC: introduce new API for hmac context
jschmidb e30465b
HMAC: update testlib routines
jschmidb ab64f1d
HMAC: consolidate some test routines
jschmidb b336e5e
HMAC: add tests for HMAC support
jschmidb 589ab6e
HMAC: update README.md
jschmidb b171959
Make rc more informative
jschmidb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
/* | ||
* Copyright IBM Corp. 2025 | ||
* | ||
* libzpc is free software; you can redistribute it and/or modify | ||
* it under the terms of the MIT license. See LICENSE for details. | ||
*/ | ||
|
||
#ifndef ZPC_HMAC_H | ||
# define ZPC_HMAC_H | ||
# ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
extern "C" { | ||
/* *INDENT-ON* */ | ||
# endif | ||
|
||
/** | ||
* \file zpc/hmac.h | ||
* | ||
* \brief HMAC API | ||
* | ||
* Message authentication API for the Hash-based Message Authentication | ||
* Code (HMAC). | ||
*/ | ||
|
||
# include <zpc/hmac_key.h> | ||
# include <stddef.h> | ||
|
||
struct zpc_hmac; | ||
|
||
/** | ||
* Allocate a new context for an HMAC operation. | ||
* \param[in,out] ctx HMAC context | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_alloc(struct zpc_hmac **ctx); | ||
/** | ||
* Set the key to be used in the context of an HMAC operation. | ||
* \param[in,out] ctx HMAC context | ||
* \param[in] key HMAC key | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_set_key(struct zpc_hmac *ctx, struct zpc_hmac_key *key); | ||
/** | ||
* Do an HMAC signing operation. | ||
* \param[in,out] ctx HMAC context | ||
* \param[in,out] mac message authentication code when set to NULL, this | ||
* indicates that an internal intermediate MAC is calculated and further | ||
* intermediate calls with additional msg data may follow. If the mac parm is | ||
* not NULL and the maclen is a valid MAC length (dependent on the underlying | ||
* hash function of the key) the final MAC is computed. | ||
* \param[in] maclen message authentication code length [bytes] | ||
* \param[in] msg message | ||
* \param[in] msglen message length [bytes] | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_sign(struct zpc_hmac *ctx, unsigned char *mac, | ||
size_t maclen, const unsigned char *msg, size_t msglen); | ||
/** | ||
* Do an HMAC verify operation. | ||
* \param[in,out] ctx HMAC context | ||
* \param[in,out] mac message authentication code if the mac parm is NULL, then | ||
* an intermediate verify op is performed. If the mac parm is not NULL and the | ||
* maclen is a valid MAC length (dependent on the underlying hash function of | ||
* the key), then the given MAC is checked for correctness. | ||
* \param[in] maclen message authentication code length [bytes] | ||
* \param[in] msg message | ||
* \param[in] msglen message length [bytes] | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_verify(struct zpc_hmac *ctx, const unsigned char *mac, | ||
size_t maclen, const unsigned char *msg, size_t msglen); | ||
/** | ||
* Free an HMAC context. | ||
* \param[in,out] ctx HMAC context | ||
*/ | ||
__attribute__((visibility("default"))) | ||
void zpc_hmac_free(struct zpc_hmac **ctx); | ||
|
||
# ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
} | ||
/* *INDENT-ON* */ | ||
# endif | ||
#endif |
This file contains hidden or 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,126 @@ | ||
/* | ||
* Copyright IBM Corp. 2025 | ||
* | ||
* libzpc is free software; you can redistribute it and/or modify | ||
* it under the terms of the MIT license. See LICENSE for details. | ||
*/ | ||
|
||
#ifndef ZPC_HMAC_KEY_H | ||
# define ZPC_HMAC_KEY_H | ||
# ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
extern "C" { | ||
/* *INDENT-ON* */ | ||
# endif | ||
|
||
/** | ||
* \file zpc/hmac_key.h | ||
* \brief HMAC key API. | ||
* | ||
* Manage | ||
* \cite HMAC keys. | ||
*/ | ||
|
||
# include <stddef.h> | ||
|
||
/* | ||
* These constants match with kernel's pkey.h, enum pkey_key_type. | ||
*/ | ||
# define ZPC_HMAC_KEY_TYPE_PVSECRET 9 | ||
|
||
typedef enum { | ||
ZPC_HMAC_SECRET_TYPE_NOT_SET = -2, | ||
ZPC_HMAC_SECRET_TYPE_INVALID = -1, | ||
ZPC_HMAC_SECRET_HMAC_SHA_256 = 0x09, /* architected key types, also below */ | ||
ZPC_HMAC_SECRET_HMAC_SHA_512 = 0x0a, | ||
} zpc_hmacsecret_type_t; | ||
|
||
typedef enum { | ||
ZPC_HMAC_HASHFUNC_NOT_SET = -2, | ||
ZPC_HMAC_HASHFUNC_INVALID = -1, | ||
ZPC_HMAC_HASHFUNC_SHA_224 = 0, | ||
ZPC_HMAC_HASHFUNC_SHA_256, | ||
ZPC_HMAC_HASHFUNC_SHA_384, | ||
ZPC_HMAC_HASHFUNC_SHA_512, | ||
} zpc_hmac_hashfunc_t; | ||
|
||
struct zpc_hmac_key; | ||
|
||
/** | ||
* Allocate a new HMAC key object with reference count 1. | ||
* \param[in,out] key HMAC key | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_alloc(struct zpc_hmac_key **key); | ||
/** | ||
* Set the HMAC key type. | ||
* \param[in,out] key HMAC key | ||
* \param[in] type currently only one type ZPC_HMAC_KEY_TYPE_PVSECRET is | ||
* supported. | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_set_type(struct zpc_hmac_key *key, int type); | ||
/** | ||
* Set the hash function to be used in the context of an HMAC operation. | ||
* \param[in,out] ctx HMAC context | ||
* \param[in] func HMAC hash function | ||
* The size of the HMAC key (64 bytes or 128 bytes) is given by the block size | ||
* of the hash function: for sha224 and sha256, the key size is set to 64 bytes, | ||
* for sha384 and sha512, the key size is set to 128 bytes. | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_set_hash_function(struct zpc_hmac_key *key, zpc_hmac_hashfunc_t func); | ||
/** | ||
* Import an HMAC protected key origin (secure key or retrievable secret ID). | ||
* \param[in,out] key HMAC key | ||
* \param[in] seckey HMAC protected key origin | ||
* \param[in] seckeylen HMAC key origin length [bytes] | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_import(struct zpc_hmac_key *key, const unsigned char *origin, | ||
size_t originlen); | ||
/** | ||
* Import an HMAC clear-key. | ||
* \param[in,out] key HMAC key | ||
* \param[in] clearkey HMAC clear-key | ||
* \param[in] keylen HMAC clear-key size [bytes] | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_import_clear(struct zpc_hmac_key *key, | ||
const unsigned char *clrkey, size_t keylen); | ||
/** | ||
* Export an HMAC protected key origin (secure key or retrievable secret ID). | ||
* \param[in,out] key HMAC key | ||
* \param[out] seckey HMAC protected key origin | ||
* \param[in,out] seckeylen origin length [bytes] | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_export(struct zpc_hmac_key *key, unsigned char *origin, | ||
size_t *originlen); | ||
/** | ||
* Generate a random HMAC protected-key. | ||
* \param[in,out] key HMAC key | ||
* \return 0 on success. Otherwise, a non-zero error code is returned. | ||
*/ | ||
__attribute__((visibility("default"))) | ||
int zpc_hmac_key_generate(struct zpc_hmac_key *key); | ||
/** | ||
* Decrease the reference count of an HMAC key object | ||
* and free it the count reaches 0. | ||
* \param[in,out] key HMAC key | ||
*/ | ||
__attribute__((visibility("default"))) | ||
void zpc_hmac_key_free(struct zpc_hmac_key **key); | ||
|
||
# ifdef __cplusplus | ||
/* *INDENT-OFF* */ | ||
} | ||
/* *INDENT-ON* */ | ||
# endif | ||
#endif |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we skip 1.3.0? If this related to other features, I would recommend to do the PRs in the right sequence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.3.0 (and even 1.3.1) is already upstream. This was the fips 140-3 update in 2024 and caused the API behavior to change, but there were no new API functions. Therefore we go to 1.4.0 now.