Skip to content

Commit da7ab4a

Browse files
committed
Don’t allocate extra memory in add_hmac_digest, use hmac_v functions
1 parent 48d0f11 commit da7ab4a

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

ssl/tls1.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static const cipher_info_t cipher_info[NUM_PROTOCOLS] =
8585
16, /* block padding size */
8686
SHA1_SIZE, /* digest size */
8787
2*(SHA1_SIZE+16+16), /* key block size */
88-
hmac_sha1, /* hmac algorithm */
88+
hmac_sha1_v, /* hmac algorithm */
8989
(crypt_func)AES_cbc_encrypt, /* encrypt */
9090
(crypt_func)AES_cbc_decrypt /* decrypt */
9191
},
@@ -96,7 +96,7 @@ static const cipher_info_t cipher_info[NUM_PROTOCOLS] =
9696
16, /* block padding size */
9797
SHA1_SIZE, /* digest size */
9898
2*(SHA1_SIZE+32+16), /* key block size */
99-
hmac_sha1, /* hmac algorithm */
99+
hmac_sha1_v, /* hmac algorithm */
100100
(crypt_func)AES_cbc_encrypt, /* encrypt */
101101
(crypt_func)AES_cbc_decrypt /* decrypt */
102102
},
@@ -107,7 +107,7 @@ static const cipher_info_t cipher_info[NUM_PROTOCOLS] =
107107
16, /* block padding size */
108108
SHA256_SIZE, /* digest size */
109109
2*(SHA256_SIZE+32+16), /* key block size */
110-
hmac_sha256, /* hmac algorithm */
110+
hmac_sha256_v, /* hmac algorithm */
111111
(crypt_func)AES_cbc_encrypt, /* encrypt */
112112
(crypt_func)AES_cbc_decrypt /* decrypt */
113113
},
@@ -118,7 +118,7 @@ static const cipher_info_t cipher_info[NUM_PROTOCOLS] =
118118
16, /* block padding size */
119119
SHA256_SIZE, /* digest size */
120120
2*(SHA256_SIZE+32+16), /* key block size */
121-
hmac_sha256, /* hmac algorithm */
121+
hmac_sha256_v, /* hmac algorithm */
122122
(crypt_func)AES_cbc_encrypt, /* encrypt */
123123
(crypt_func)AES_cbc_decrypt /* decrypt */
124124
}
@@ -746,21 +746,24 @@ static void increment_write_sequence(SSL *ssl)
746746
static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
747747
const uint8_t *buf, int buf_len, uint8_t *hmac_buf)
748748
{
749-
int hmac_len = buf_len + 8 + SSL_RECORD_SIZE;
750-
uint8_t *t_buf = (uint8_t *)malloc(hmac_len);
749+
const uint8_t* bufs[] = {
750+
(mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ?
751+
ssl->write_sequence : ssl->read_sequence,
752+
hmac_header,
753+
buf
754+
};
751755

752-
memcpy(t_buf, (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ?
753-
ssl->write_sequence : ssl->read_sequence, 8);
754-
memcpy(&t_buf[8], hmac_header, SSL_RECORD_SIZE);
755-
memcpy(&t_buf[8+SSL_RECORD_SIZE], buf, buf_len);
756+
int lengths[] = {
757+
8,
758+
SSL_RECORD_SIZE,
759+
buf_len
760+
};
756761

757-
ssl->cipher_info->hmac(t_buf, hmac_len,
762+
ssl->cipher_info->hmac_v(bufs, lengths, 3,
758763
(mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ) ?
759764
ssl->server_mac : ssl->client_mac,
760765
ssl->cipher_info->digest_size, hmac_buf);
761766

762-
free(t_buf);
763-
764767
#if 0
765768
print_blob("record", hmac_header, SSL_RECORD_SIZE);
766769
print_blob("buf", buf, buf_len);

ssl/tls1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ typedef struct
132132
uint8_t padding_size;
133133
uint8_t digest_size;
134134
uint8_t key_block_size;
135-
hmac_func hmac;
135+
hmac_func_v hmac_v;
136136
crypt_func encrypt;
137137
crypt_func decrypt;
138138
} cipher_info_t;

0 commit comments

Comments
 (0)