Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/fwupd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
fwupd_ref: [ 'main', '1.9.26', '2.0.12' ]
fwupd_ref: [ '1.9.26', '2.0.12' ]
fail-fast: false
runs-on: ${{ matrix.os }}
container:
Expand Down
4 changes: 2 additions & 2 deletions wolfssl-gnutls-wrapper/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ endif
UNAME_S := $(shell uname -s)

ifeq ($(UNAME_S),Linux)
CFLAGS += -DENABLE_WOLFSSL -fPIC -Wall -Wextra
CFLAGS += -DENABLE_WOLFSSL -fPIC -Wall -Wextra -pthread
CFLAGS_DEBUG += -g
# Use rpath to wherever we actually link from
LDFLAGS += -Wl,--no-as-needed -Wl,-z,now \
-Wl,-rpath,$(GNUTLS_LIBDIR) -Wl,-rpath,$(WOLFSSL_LIBDIR)
SOFLAG := -shared
DL_LIBS := -ldl
DL_LIBS := -ldl -pthread
endif

ifeq ($(UNAME_S),Darwin)
Expand Down
126 changes: 48 additions & 78 deletions wolfssl-gnutls-wrapper/src/cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ struct wolfssl_cipher_ctx {
size_t data_size;
/** Tag has been set. */
unsigned int tag_set:1;
/** Tag has been set from external source. */
unsigned int tag_set_ext:1;
};

/** Array of supported ciphers. */
Expand Down Expand Up @@ -654,7 +652,6 @@ int wolfssl_cipher_setiv(void *_ctx, const void *iv, size_t iv_size)
/* IV stored and used in encrypt/decrypt/tag. */
/* No tag set, auth data or plaintext now we have a new IV. */
ctx->tag_set = 0;
ctx->tag_set_ext = 0;
ctx->auth_data_size = 0;
ctx->data_size = 0;
break;
Expand Down Expand Up @@ -1032,7 +1029,7 @@ int wolfssl_cipher_decrypt(void *_ctx, const void *src, size_t src_size,

/* Allocate a decrypted data buffer to decrypt all ciphertext into. */
decr = gnutls_malloc(ctx->data_size + src_size);
if (ptr == NULL) {
if (decr == NULL) {
WGW_ERROR("realloc of decrypted data failed");
return GNUTLS_E_INVALID_REQUEST;
}
Expand All @@ -1042,31 +1039,26 @@ int wolfssl_cipher_decrypt(void *_ctx, const void *src, size_t src_size,
unsigned char *aad = ctx->auth_data_heap ?
ctx->auth_data_heap : ctx->auth_data_static;

/* If caller hasn't set tag then we are creating it. */
if (!ctx->tag_set_ext) {
/* Encrypt the ciphertext to get the plaintext.
* Tag will have been created on plaintext which is of no use.
*/
ret = wc_AesGcmEncrypt(&ctx->cipher.aes_ctx, decr, ctx->data,
ctx->data_size, ctx->iv, ctx->iv_size,
ctx->tag, ctx->tag_size, aad, ctx->auth_data_size);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_AesGcmEncrypt", ret);
gnutls_free(decr);
return GNUTLS_E_ENCRYPTION_FAILED;
}
/* Encrypt the plaintext to create the tag. */
ret = wc_AesGcmEncrypt(&ctx->cipher.aes_ctx, decr, decr,
ctx->data_size, ctx->iv, ctx->iv_size,
ctx->tag, ctx->tag_size, aad, ctx->auth_data_size);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_AesGcmEncrypt", ret);
gnutls_free(decr);
return GNUTLS_E_ENCRYPTION_FAILED;
}
/* A tag is now available. */
ctx->tag_set = 1;
/* Encrypt the ciphertext to get the plaintext. */
ret = wc_AesGcmEncrypt(&ctx->cipher.aes_ctx, decr, ctx->data,
ctx->data_size, ctx->iv, ctx->iv_size,
ctx->tag, ctx->tag_size, aad, ctx->auth_data_size);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_AesGcmEncrypt", ret);
gnutls_free(decr);
return GNUTLS_E_ENCRYPTION_FAILED;
}
/* Encrypt the plaintext to create the tag. */
ret = wc_AesGcmEncrypt(&ctx->cipher.aes_ctx, decr, decr,
ctx->data_size, ctx->iv, ctx->iv_size,
ctx->tag, ctx->tag_size, aad, ctx->auth_data_size);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_AesGcmEncrypt", ret);
gnutls_free(decr);
return GNUTLS_E_ENCRYPTION_FAILED;
}
/* A tag is now available. */
ctx->tag_set = 1;
/* Do decryption with cipehtext, IV, authentication data and tag. */
ret = wc_AesGcmDecrypt(&ctx->cipher.aes_ctx, decr,
ctx->data, ctx->data_size, ctx->iv, ctx->iv_size,
Expand Down Expand Up @@ -1118,82 +1110,60 @@ int wolfssl_cipher_decrypt(void *_ctx, const void *src, size_t src_size,
}

/**
* Get or store the tag;
* Output the tag, or random bytes when it cannot be computed.
*
* When tag is all zeros then copy out the stored authentication tag.
*
* @param [in] _ctx Cipher context.
* @param [in, out] tag Authentication tag.
* @param [in] tag_size Size of authentication tag in bytes.
* @param [in] _ctx Cipher context.
* @param [out] tag Authentication tag.
* @param [in] tag_size Size of authentication tag in bytes.
*/
void wolfssl_cipher_tag(void *_ctx, void *tag, size_t tag_size)
{
WGW_FUNC_ENTER();
WGW_LOG("tag_size %zu", tag_size);

struct wolfssl_cipher_ctx *ctx = _ctx;
int ret = -1;

if (!ctx->initialized) {
WGW_LOG("cipher context not initialized");
return;
}

/* Make sure copied tag size is no larger than that generated. */
if (tag_size > ctx->tag_size) {
tag_size = ctx->tag_size;
}

/* Check if tag available. */
if (ctx->tag_set) {
if (ctx->mode == GCM) {
XMEMCPY(tag, ctx->tag, tag_size);
/* Authentication data used - reset count. */
ctx->auth_data_size = 0;
/* Dispose of cached data. */
gnutls_free(ctx->data);
ctx->data = NULL;
ctx->data_size = 0;
WGW_LOG("tag returned successfully");
} else {
WGW_LOG("AES mode not supported: %d", ctx->mode);
} else if (ctx->mode != GCM) {
WGW_LOG("AES mode not supported: %d", ctx->mode);
} else {
/* Make sure copied tag size is no larger than that generated. */
if (tag_size > ctx->tag_size) {
tag_size = ctx->tag_size;
}
} else if (ctx->enc) {
int ret = -1;

/* Encrypting and no tag set means we don't have plaintext. */
if (ctx->mode == GCM) {
WGW_LOG("wc_AesGcmEncrypt");

if (ctx->tag_set) {
ret = 0;
} else {
unsigned char *aad = ctx->auth_data_heap ?
ctx->auth_data_heap : ctx->auth_data_static;

/* Do authentication with no plaintext. */
WGW_LOG("wc_AesGcmEncrypt");
ret = wc_AesGcmEncrypt(&ctx->cipher.aes_ctx, NULL, NULL, 0, ctx->iv,
ctx->iv_size, ctx->tag, ctx->tag_size, aad,
ctx->auth_data_size);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_AesGcmEncrypt", ret);
} else {
/* Copy out tag. */
ctx->tag_set = 1;
XMEMCPY(tag, ctx->tag, tag_size);
WGW_LOG("tag stored successfully");
}
/* Authentication data used - reset count. */
ctx->auth_data_size = 0;
/* Dispose of cached plaintext. */
gnutls_free(ctx->data);
ctx->data = NULL;
ctx->data_size = 0;
} else {
WGW_LOG("AES mode not supported: %d", ctx->mode);
}
} else {
/* Decrypting and we need to set tag for decrypt operation. */
XMEMCPY(ctx->tag, tag, tag_size);
ctx->tag_set = 1;
ctx->tag_set_ext = 1;
WGW_LOG("tag provided successfully");
/* Authentication data used, so we reset count. */
ctx->auth_data_size = 0;
/* Dispose of cached data. */
gnutls_free(ctx->data);
ctx->data = NULL;
ctx->data_size = 0;
}

if (ret == 0) {
XMEMCPY(tag, ctx->tag, tag_size);
WGW_LOG("tag returned successfully");
} else if (gnutls_rnd(GNUTLS_RND_NONCE, tag, tag_size) < 0) {
WGW_ERROR("no random bytes for the tag");
Comment thread
gasbytes marked this conversation as resolved.
Resolved
}
}

Expand Down
9 changes: 0 additions & 9 deletions wolfssl-gnutls-wrapper/src/kdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ static int wolfssl_hkdf_extract(gnutls_mac_algorithm_t mac, const void *key,

if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_HKDF_Extract", ret);
#if defined(HAVE_FIPS)
return GNUTLS_FIPS140_OP_NOT_APPROVED;
#else
return GNUTLS_E_INTERNAL_ERROR;
#endif
}

return 0;
Expand Down Expand Up @@ -147,11 +143,6 @@ static int wolfssl_pbkdf2(gnutls_mac_algorithm_t mac, const void *key,

if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_PBKDF2_ex", ret);
#if defined(HAVE_FIPS)
if (ret == HMAC_MIN_KEYLEN_E) {
return GNUTLS_FIPS140_OP_NOT_APPROVED;
}
#endif
return GNUTLS_E_INTERNAL_ERROR;
}

Expand Down
14 changes: 5 additions & 9 deletions wolfssl-gnutls-wrapper/src/mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,7 @@ static int wolfssl_hmac_setkey(void *_ctx, const void *key, size_t keysize)
(word32)keysize);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_HmacSetKey", ret);
#if defined(HAVE_FIPS)
return GNUTLS_FIPS140_OP_NOT_APPROVED;
#else
return GNUTLS_E_HASH_FAILED;
#endif
}

WGW_LOG("hmac key set successfully");
Expand Down Expand Up @@ -362,6 +358,7 @@ static int wolfssl_hmac_fast(gnutls_mac_algorithm_t algorithm,
/* Set key into HMAC context. */
ret = wolfssl_hmac_setkey(ctx, key, keysize);
if (ret != 0) {
wolfssl_hmac_deinit(ctx);
return ret;
}

Expand Down Expand Up @@ -507,7 +504,6 @@ static int wolfssl_cmac_setkey(void *_ctx, const void *key, size_t keysize)
WC_CMAC_AES, NULL);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_InitCmac", ret);
gnutls_free(ctx);
return GNUTLS_E_HASH_FAILED;
}

Expand Down Expand Up @@ -548,7 +544,6 @@ static int wolfssl_cmac_hash(void *_ctx, const void *text, size_t textsize)
(word32)ctx->key_size, WC_CMAC_AES, NULL);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_InitCmac", ret);
gnutls_free(ctx);
return GNUTLS_E_HASH_FAILED;
}
ctx->set_key = 0;
Expand Down Expand Up @@ -617,7 +612,6 @@ static int wolfssl_cmac_output(void *_ctx, void *digest, size_t digestsize)
(word32)ctx->key_size, WC_CMAC_AES, NULL);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_InitCmac", ret);
gnutls_free(ctx);
return GNUTLS_E_HASH_FAILED;
}
ctx->set_key = 0;
Expand Down Expand Up @@ -690,6 +684,7 @@ static int wolfssl_cmac_fast(gnutls_mac_algorithm_t algorithm,
/* Set key into CMAC context. */
ret = wolfssl_cmac_setkey(ctx, key, keysize);
if (ret != 0) {
wolfssl_cmac_deinit(ctx);
return ret;
}

Expand Down Expand Up @@ -865,7 +860,6 @@ static int wolfssl_gmac_setkey(void *_ctx, const void *key, size_t keysize)
ret = wc_GmacSetKey(&ctx->gmac_ctx, (const byte*)key, (word32)keysize);
if (ret != 0) {
WGW_WOLFSSL_ERROR("wc_GmacSetKey", ret);
gnutls_free(ctx);
return GNUTLS_E_HASH_FAILED;
}

Expand Down Expand Up @@ -1051,12 +1045,14 @@ static int wolfssl_gmac_fast(gnutls_mac_algorithm_t algorithm,
/* Set key into GMAC context. */
ret = wolfssl_gmac_setkey(ctx, key, keysize);
if (ret != 0) {
wolfssl_gmac_deinit(ctx);
return ret;
}

/* Set key into GMAC context. */
/* Set nonce into GMAC context. */
ret = wolfssl_gmac_setnonce(ctx, nonce, nonce_size);
if (ret != 0) {
wolfssl_gmac_deinit(ctx);
return ret;
}

Expand Down
Loading
Loading