Skip to content

Commit 6ef91d8

Browse files
basavesht8m
authored andcommitted
Fix leakage when the cacheline is 32-bytes in CBC_MAC_ROTATE_IN_PLACE
rotated_mac is a 64-byte aligned buffer of size 64 and rotate_offset is secret. Consider a weaker leakage model(CL) where only cacheline base address is leaked, i.e address/32 for 32-byte cacheline(CL32). Previous code used to perform two loads 1. rotated_mac[rotate_offset ^ 32] and 2. rotated_mac[rotate_offset++] which would leak 2q + 1, 2q for 0 <= rotate_offset < 32 and 2q, 2q + 1 for 32 <= rotate_offset < 64 The proposed fix performs load operations which will always leak 2q, 2q + 1 and selects the appropriate value in constant-time. Reviewed-by: Matt Caswell <[email protected]> Reviewed-by: Tomas Mraz <[email protected]> (Merged from openssl#18050)
1 parent c7d6c08 commit 6ef91d8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

ssl/record/ssl3_record.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,7 @@ int ssl3_cbc_copy_mac(unsigned char *out,
15321532
#if defined(CBC_MAC_ROTATE_IN_PLACE)
15331533
unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
15341534
unsigned char *rotated_mac;
1535+
char aux1, aux2, aux3, mask;
15351536
#else
15361537
unsigned char rotated_mac[EVP_MAX_MD_SIZE];
15371538
#endif
@@ -1581,9 +1582,16 @@ int ssl3_cbc_copy_mac(unsigned char *out,
15811582
#if defined(CBC_MAC_ROTATE_IN_PLACE)
15821583
j = 0;
15831584
for (i = 0; i < md_size; i++) {
1584-
/* in case cache-line is 32 bytes, touch second line */
1585-
((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
1586-
out[j++] = rotated_mac[rotate_offset++];
1585+
/*
1586+
* in case cache-line is 32 bytes,
1587+
* load from both lines and select appropriately
1588+
*/
1589+
aux1 = rotated_mac[rotate_offset & ~32];
1590+
aux2 = rotated_mac[rotate_offset | 32];
1591+
mask = constant_time_eq_8(rotate_offset & ~32, rotate_offset);
1592+
aux3 = constant_time_select_8(mask, aux1, aux2);
1593+
out[j++] = aux3;
1594+
rotate_offset++;
15871595
rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
15881596
}
15891597
#else

0 commit comments

Comments
 (0)