Debian Codegraph @

crt/s2n/tests/unit/s2n_3des_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "api/s2n.h"
20 #include "crypto/s2n_cipher.h"
21 #include "crypto/s2n_hmac.h"
22 #include "s2n_test.h"
23 #include "stuffer/s2n_stuffer.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/s2n_cipher_suites.h"
26 #include "tls/s2n_prf.h"
27 #include "tls/s2n_record.h"
28 #include "utils/s2n_random.h"
29
30 int main(int argc, char **argv)
31 {
32 struct s2n_connection *conn = NULL;
33 uint8_t mac_key[] = "sample mac key";
34 uint8_t des3_key[] = "12345678901234567890123";
35 struct s2n_blob des3 = { 0 };
36 EXPECT_SUCCESS(s2n_blob_init(&des3, des3_key, sizeof(des3_key)));
37 uint8_t random_data[S2N_DEFAULT_FRAGMENT_LENGTH + 1];
38 struct s2n_blob r = { 0 };
39 EXPECT_SUCCESS(s2n_blob_init(&r, random_data, sizeof(random_data)));
40
41 BEGIN_TEST();
42 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
43
44 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
45 EXPECT_OK(s2n_get_public_random_data(&r));
46
47 /* Peer and we are in sync */
48 conn->server = conn->secure;
49 conn->client = conn->secure;
50
51 /* test the 3des cipher with a SHA1 hash */
52 conn->secure->cipher_suite->record_alg = &s2n_record_alg_3des_sha;
53 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->server_key));
54 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->client_key));
55 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &des3));
56 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &des3));
57
58 EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->client_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key)));
59 EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->server_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key)));
60 conn->actual_protocol_version = S2N_TLS11;
61
62 for (int i = 0; i <= S2N_DEFAULT_FRAGMENT_LENGTH + 1; i++) {
63 struct s2n_blob in = { 0 };
64 EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i));
65 int bytes_written = 0;
66
67 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out));
68
69 s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in);
70 if (i <= S2N_DEFAULT_FRAGMENT_LENGTH) {
71 EXPECT_OK(result);
72 bytes_written = i;
73 } else {
74 EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
75 bytes_written = S2N_DEFAULT_FRAGMENT_LENGTH;
76 }
77
78 uint16_t predicted_length = bytes_written + 1 + 20 + 8;
79 if (predicted_length % 8) {
80 predicted_length += (8 - (predicted_length % 8));
81 }
82 EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA);
83 EXPECT_EQUAL(conn->out.blob.data[1], 3);
84 EXPECT_EQUAL(conn->out.blob.data[2], 2);
85 EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff);
86 EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff);
87
88 /* The data should be encrypted */
89 if (bytes_written > 10) {
90 EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + 5, random_data, bytes_written), 0);
91 }
92
93 /* Copy the encrypted out data to the in data */
94 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
95 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
96 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5));
97 EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out)));
98
99 /* Let's decrypt it */
100 uint8_t content_type = 0;
101 uint16_t fragment_length = 0;
102 EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length));
103 EXPECT_SUCCESS(s2n_record_parse(conn));
104 EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA);
105 EXPECT_EQUAL(fragment_length, predicted_length);
106
107 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in));
108 EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in));
109 }
110
111 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->server_key));
112 EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->client_key));
113 EXPECT_SUCCESS(s2n_connection_free(conn));
114
115 END_TEST();
116 }