1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00
RIOT/pkg/tinydtls/patches/0005-crypto-remove-unnecessary-usage-of-malloc-dtls_hmac_.patch
2022-04-01 09:10:35 +02:00

106 lines
3.3 KiB
Diff

From a98c493857d27f25e6ab142fe893ffcc9e6a6474 Mon Sep 17 00:00:00 2001
From: "Martine S. Lenders" <m.lenders@fu-berlin.de>
Date: Mon, 28 Mar 2022 15:00:02 +0200
Subject: [PATCH] crypto: remove unnecessary usage of malloc()/dtls_hmac_new()
---
crypto.c | 57 ++++++++++++++++++++++++++------------------------------
1 file changed, 26 insertions(+), 31 deletions(-)
diff --git a/crypto.c b/crypto.c
index 43d3079198..6475eb81d0 100644
--- a/crypto.c
+++ b/crypto.c
@@ -207,7 +207,7 @@ dtls_p_hash(dtls_hashfunc_t h,
const unsigned char *random1, size_t random1len,
const unsigned char *random2, size_t random2len,
unsigned char *buf, size_t buflen) {
- dtls_hmac_context_t *hmac_a, *hmac_p;
+ dtls_hmac_context_t hmac_a, hmac_p;
unsigned char A[DTLS_HMAC_DIGEST_SIZE];
unsigned char tmp[DTLS_HMAC_DIGEST_SIZE];
@@ -215,54 +215,49 @@ dtls_p_hash(dtls_hashfunc_t h,
size_t len = 0; /* result length */
(void)h;
- hmac_a = dtls_hmac_new(key, keylen);
- if (!hmac_a)
- return 0;
+ dtls_hmac_init(&hmac_a, key, keylen);
/* calculate A(1) from A(0) == seed */
- HMAC_UPDATE_SEED(hmac_a, label, labellen);
- HMAC_UPDATE_SEED(hmac_a, random1, random1len);
- HMAC_UPDATE_SEED(hmac_a, random2, random2len);
+ HMAC_UPDATE_SEED(&hmac_a, label, labellen);
+ HMAC_UPDATE_SEED(&hmac_a, random1, random1len);
+ HMAC_UPDATE_SEED(&hmac_a, random2, random2len);
- dlen = dtls_hmac_finalize(hmac_a, A);
-
- hmac_p = dtls_hmac_new(key, keylen);
- if (!hmac_p)
- goto error;
+ dlen = dtls_hmac_finalize(&hmac_a, A);
while (len + dlen < buflen) {
- /* FIXME: rewrite loop to avoid superflous call to dtls_hmac_init() */
- dtls_hmac_init(hmac_p, key, keylen);
- dtls_hmac_update(hmac_p, A, dlen);
+ dtls_hmac_init(&hmac_p, key, keylen);
+ dtls_hmac_update(&hmac_p, A, dlen);
- HMAC_UPDATE_SEED(hmac_p, label, labellen);
- HMAC_UPDATE_SEED(hmac_p, random1, random1len);
- HMAC_UPDATE_SEED(hmac_p, random2, random2len);
+ HMAC_UPDATE_SEED(&hmac_p, label, labellen);
+ HMAC_UPDATE_SEED(&hmac_p, random1, random1len);
+ HMAC_UPDATE_SEED(&hmac_p, random2, random2len);
- len += dtls_hmac_finalize(hmac_p, tmp);
+ len += dtls_hmac_finalize(&hmac_p, tmp);
memcpy(buf, tmp, dlen);
buf += dlen;
/* calculate A(i+1) */
- dtls_hmac_init(hmac_a, key, keylen);
- dtls_hmac_update(hmac_a, A, dlen);
- dtls_hmac_finalize(hmac_a, A);
+ dtls_hmac_init(&hmac_a, key, keylen);
+ dtls_hmac_update(&hmac_a, A, dlen);
+ dtls_hmac_finalize(&hmac_a, A);
}
- dtls_hmac_init(hmac_p, key, keylen);
- dtls_hmac_update(hmac_p, A, dlen);
+ dtls_hmac_init(&hmac_p, key, keylen);
+ dtls_hmac_update(&hmac_p, A, dlen);
- HMAC_UPDATE_SEED(hmac_p, label, labellen);
- HMAC_UPDATE_SEED(hmac_p, random1, random1len);
- HMAC_UPDATE_SEED(hmac_p, random2, random2len);
+ HMAC_UPDATE_SEED(&hmac_p, label, labellen);
+ HMAC_UPDATE_SEED(&hmac_p, random1, random1len);
+ HMAC_UPDATE_SEED(&hmac_p, random2, random2len);
- dtls_hmac_finalize(hmac_p, tmp);
+ dtls_hmac_finalize(&hmac_p, tmp);
memcpy(buf, tmp, buflen - len);
- error:
- dtls_hmac_free(hmac_a);
- dtls_hmac_free(hmac_p);
+ /* prevent exposure of sensible data */
+ memset(&hmac_a, 0, sizeof(hmac_a));
+ memset(&hmac_p, 0, sizeof(hmac_p));
+ memset(tmp, 0, sizeof(tmp));
+ memset(A, 0, sizeof(A));
return buflen;
}
--
2.25.1