Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/crypto/mbedtls.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <mbedtls/aes.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/platform_util.h>

#include <osdp.h>

Expand Down Expand Up @@ -88,6 +89,11 @@ void osdp_fill_random(uint8_t *buf, int len)
assert(rc == 0);
}

void osdp_fill_zeros(void *buf, int len)
{
mbedtls_platform_zeroize(buf, (size_t)len);
}

void osdp_crypt_teardown()
{
mbedtls_ctr_drbg_free(&ctr_drbg_ctx);
Expand Down
5 changes: 5 additions & 0 deletions src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void osdp_fill_random(uint8_t *buf, int len)
}
}

void osdp_fill_zeros(void *buf, int len)
{
OPENSSL_cleanse(buf, (size_t)len);
}

void osdp_crypt_teardown()
{
}
6 changes: 6 additions & 0 deletions src/crypto/tinyaes.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Copy link
Copy Markdown
Member

@sidcha sidcha Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the suggestion below above, you should drop this too.

#include <assert.h>

#include "tinyaes_src.h"
Expand Down Expand Up @@ -56,6 +57,11 @@ void osdp_fill_random(uint8_t *buf, int len)
}
}

void osdp_fill_zeros(void *buf, int len)
{
explicit_bzero(buf, (size_t)len);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicit_bzero() is a glibc/BSD extension, AVR libc does not provide it. Please replace this with a more portable version:

Suggested change
explicit_bzero(buf, (size_t)len);
volatile uint8_t *p = (volatile uint8_t *)buf;
while (len--)
*p++ = 0;

}

void osdp_crypt_teardown()
{
}
1 change: 1 addition & 0 deletions src/osdp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ void osdp_crypt_setup();
void osdp_encrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len);
void osdp_decrypt(uint8_t *key, uint8_t *iv, uint8_t *data, int len);
void osdp_fill_random(uint8_t *buf, int len);
void osdp_fill_zeros(void *buf, int len);
void osdp_crypt_teardown();

/* --- from osdp_sc.c --- */
Expand Down
1 change: 1 addition & 0 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,7 @@ void osdp_cp_teardown(osdp_t *ctx)
if (is_capture_enabled(pd)) {
osdp_packet_capture_finish(pd);
}
osdp_fill_zeros(&pd->sc, sizeof(struct osdp_secure_channel));

#ifndef OPT_OSDP_STATIC
safe_free(pd->file);
Expand Down
3 changes: 3 additions & 0 deletions src/osdp_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ static void osdp_pd_update(struct osdp_pd *pd)
}
if (pd->cmd_id == CMD_KEYSET && pd->reply_id == REPLY_ACK) {
memcpy(pd->sc.scbk, pd->keyset_pending, 16);
osdp_fill_zeros(pd->keyset_pending, 16);
CLEAR_FLAG(pd, PD_FLAG_SC_USE_SCBKD);
CLEAR_FLAG(pd, PD_FLAG_INSTALL_MODE);
sc_deactivate(pd);
Expand Down Expand Up @@ -1340,6 +1341,8 @@ void osdp_pd_teardown(osdp_t *ctx)
osdp_packet_capture_finish(pd);
}

osdp_fill_zeros(&pd->sc, sizeof(struct osdp_secure_channel));

if (pd_ctx->channel.close) {
pd_ctx->channel.close(pd_ctx->channel.data);
}
Expand Down
15 changes: 7 additions & 8 deletions src/osdp_sc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void osdp_compute_session_keys(struct osdp_pd *pd)
osdp_encrypt(scbk, NULL, pd->sc.s_enc, 16);
osdp_encrypt(scbk, NULL, pd->sc.s_mac1, 16);
osdp_encrypt(scbk, NULL, pd->sc.s_mac2, 16);
osdp_fill_zeros(scbk, sizeof(scbk));
}

void osdp_compute_cp_cryptogram(struct osdp_pd *pd)
Expand Down Expand Up @@ -93,10 +94,9 @@ int osdp_verify_cp_cryptogram(struct osdp_pd *pd)
memcpy(cp_crypto + 8, pd->sc.cp_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, cp_crypto, 16);

if (osdp_ct_compare(pd->sc.cp_cryptogram, cp_crypto, 16) != 0) {
return -1;
}
return 0;
int ret = osdp_ct_compare(pd->sc.cp_cryptogram, cp_crypto, 16) == 0 ? 0 : -1;
osdp_fill_zeros(cp_crypto, sizeof(cp_crypto));
return ret;
}

void osdp_compute_pd_cryptogram(struct osdp_pd *pd)
Expand All @@ -116,10 +116,9 @@ int osdp_verify_pd_cryptogram(struct osdp_pd *pd)
memcpy(pd_crypto + 8, pd->sc.pd_random, 8);
osdp_encrypt(pd->sc.s_enc, NULL, pd_crypto, 16);

if (osdp_ct_compare(pd->sc.pd_cryptogram, pd_crypto, 16) != 0) {
return -1;
}
return 0;
int ret = osdp_ct_compare(pd->sc.pd_cryptogram, pd_crypto, 16) == 0 ? 0 : -1;
osdp_fill_zeros(pd_crypto, sizeof(pd_crypto));
return ret;
}

void osdp_compute_rmac_i(struct osdp_pd *pd)
Expand Down
Loading