Debian Codegraph @

crt/s2n/tests/sidetrail/working/patches/cbc.patch

raw
1 --- a/tests/sidetrail/working/s2n-cbc/tls/s2n_cbc.c
2 +++ b/tests/sidetrail/working/s2n-cbc/tls/s2n_cbc.c
3 @@ -22,6 +22,28 @@
4 #include "utils/s2n_mem.h"
5 #include "utils/s2n_safety.h"
6
7 +#include <smack.h>
8 +#include <smack-contracts.h>
9 +#include "ct-verif.h"
10 +#include "sidetrail.h"
11 +
12 +/* Because of a bug in SMACK, when we try to specify the invarients for this loop in the main function,
13 + * SMACK crashes. Moving the code (unmodified) to this function sidesteps the bug. Michael Emmi has promissed
14 + * a fix soon
15 + */
16 +int double_loop(int old_mismatches, struct s2n_blob *decrypted, int check, int cutoff, int padding_length) {
17 + __VERIFIER_assert(decrypted->size >= 0);
18 + __VERIFIER_assert(decrypted->size <= MAX_SIZE);
19 + int mismatches = old_mismatches;
20 + for (uint32_t i = 0, j = decrypted->size - 1 - check; i < check && j < decrypted->size; i++, j++) {
21 + invariant(i <= check);
22 + invariant(j == i + decrypted->size - check - 1);
23 + uint8_t mask = ~(0xff << ((i >= cutoff) * 8));
24 + mismatches |= (decrypted->data[j] ^ padding_length) & mask;
25 + }
26 + return mismatches;
27 +}
28 +
29 /* A TLS CBC record looks like ..
30 *
31 * [ Payload data ] [ HMAC ] [ Padding ] [ Padding length byte ]
32 @@ -44,23 +66,28 @@
33 */
34 int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted)
35 {
36 - uint8_t mac_digest_size = 0;
37 - POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));
38 + uint8_t mac_digest_size = DIGEST_SIZE;
39 + //POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));
40
41 /* The record has to be at least big enough to contain the MAC,
42 * plus the padding length byte */
43 POSIX_ENSURE_GT(decrypted->size, mac_digest_size);
44
45 int payload_and_padding_size = decrypted->size - mac_digest_size;
46 + __VERIFIER_assert(payload_and_padding_size <= MAX_SIZE - 20);
47
48 /* Determine what the padding length is */
49 uint8_t padding_length = decrypted->data[decrypted->size - 1];
50 + __VERIFIER_assume(padding_length >= 0);
51 + __VERIFIER_assume(padding_length < 256);
52
53 int payload_length = S2N_MAX(payload_and_padding_size - padding_length - 1, 0);
54
55 /* Update the MAC */
56 POSIX_GUARD(s2n_hmac_update(hmac, decrypted->data, payload_length));
57 int currently_in_hash_block = hmac->currently_in_hash_block;
58 + __VERIFIER_assume(currently_in_hash_block >= 0);
59 + __VERIFIER_assume(currently_in_hash_block < BLOCK_SIZE);
60
61 /* Check the MAC */
62 uint8_t check_digest[S2N_MAX_DIGEST_LEN];
63 @@ -82,9 +109,9 @@
64 * recommended. If SSLv3 must be used, the blinding feature (enabled
65 * by default) helps mitigates this issue as well.
66 */
67 - if (conn->actual_protocol_version == S2N_SSLv3) {
68 - return 0 - mismatches;
69 - }
70 + //if (conn->actual_protocol_version == S2N_SSLv3) {
71 + // return 0 - mismatches;
72 + //}
73
74 /* Check the maximum amount that could theoretically be padding */
75 uint32_t check = S2N_MIN(255, (payload_and_padding_size - 1));
76 @@ -92,10 +119,11 @@
77 POSIX_ENSURE_GTE(check, padding_length);
78
79 uint32_t cutoff = check - padding_length;
80 - for (size_t i = 0, j = decrypted->size - 1 - check; i < check && j < decrypted->size; i++, j++) {
81 - uint8_t mask = ~(0xff << ((i >= cutoff) * 8));
82 - mismatches |= (decrypted->data[j] ^ padding_length) & mask;
83 - }
84 + mismatches = double_loop(mismatches, decrypted, check, cutoff, padding_length);
85 + //for (size_t i = 0, j = decrypted->size - 1 - check; i < check && j < decrypted->size; i++, j++) {
86 + // uint8_t mask = ~(0xff << ((i >= cutoff) * 8));
87 + // mismatches |= (decrypted->data[j] ^ padding_length) & mask;
88 + //}
89
90 S2N_ERROR_IF(mismatches, S2N_ERR_CBC_VERIFY);
91