Debian Codegraph @

crt/aws-c-cal/tests/rsa_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/cal/cal.h>
6 #include <aws/cal/hash.h>
7 #include <aws/cal/private/der.h>
8 #include <aws/cal/private/rsa.h>
9 #include <aws/cal/rsa.h>
10 #include <aws/common/encoding.h>
11 #include <aws/testing/aws_test_harness.h>
12
13 #include "test_case_helper.h"
14
15 #if defined(AWS_OS_LINUX)
16 # include <aws/cal/private/opensslcrypto_common.h>
17 #endif
18
19 /*
20 * Helper to figure out whether rsa signing with sha1 is supported with currently crypto lib.
21 */
22 static bool s_crypto_supports_sha1_signing(void) {
23 bool is_supported = true;
24 #if defined(AWS_OS_LINUX)
25 # if defined(OPENSSL_IS_OPENSSL)
26 # if OPENSSL_VERSION_NUMBER >= 0x30500000L
27 is_supported = false;
28 # endif
29 # endif
30 #endif
31 return is_supported;
32 }
33
34 /*
35 * TODO: Need better test vectors. NIST ones are a pain to use.
36 * For now using manually generated vectors and relying on round tripping.
37 */
38
39 static int s_byte_buf_decoded_from_base64_cur(
40 struct aws_allocator *allocator,
41 struct aws_byte_cursor cur,
42 struct aws_byte_buf *out) {
43 size_t decoded_length = 0;
44 ASSERT_SUCCESS(aws_base64_compute_decoded_len(&cur, &decoded_length));
45 ASSERT_SUCCESS(aws_byte_buf_init(out, allocator, decoded_length));
46 ASSERT_SUCCESS(aws_base64_decode(&cur, out));
47 return AWS_OP_SUCCESS;
48 }
49
50 static const char *TEST_ENCRYPTION_STRING = "The quick brown fox jumps over the lazy dog.";
51
52 static int s_rsa_encryption_roundtrip_helper(
53 struct aws_allocator *allocator,
54 struct aws_rsa_key_pair *key_pair,
55 enum aws_rsa_encryption_algorithm algo) {
56 struct aws_byte_cursor plaintext_cur = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
57
58 /*short buffer should fail*/
59 struct aws_byte_buf ciphertext_short;
60 ASSERT_SUCCESS(aws_byte_buf_init(&ciphertext_short, allocator, 5));
61 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_rsa_key_pair_encrypt(key_pair, algo, plaintext_cur, &ciphertext_short));
62
63 /*make sure not to clobber anything in existing buffer*/
64 struct aws_byte_cursor prefix = aws_byte_cursor_from_c_str("random_prefix");
65 struct aws_byte_buf ciphertext;
66 ASSERT_SUCCESS(aws_byte_buf_init(&ciphertext, allocator, prefix.len + aws_rsa_key_pair_block_length(key_pair)));
67 ASSERT_SUCCESS(aws_byte_buf_append(&ciphertext, &prefix));
68 ASSERT_SUCCESS(aws_rsa_key_pair_encrypt(key_pair, algo, plaintext_cur, &ciphertext));
69
70 struct aws_byte_cursor ciphertext_cur = aws_byte_cursor_from_buf(&ciphertext);
71 ASSERT_TRUE(aws_byte_cursor_starts_with(&ciphertext_cur, &prefix));
72
73 aws_byte_cursor_advance(&ciphertext_cur, prefix.len);
74
75 struct aws_byte_buf decrypted_short;
76 ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_short, allocator, 5));
77 ASSERT_ERROR(AWS_ERROR_SHORT_BUFFER, aws_rsa_key_pair_decrypt(key_pair, algo, ciphertext_cur, &decrypted_short));
78
79 struct aws_byte_buf decrypted;
80 ASSERT_SUCCESS(aws_byte_buf_init(&decrypted, allocator, prefix.len + aws_rsa_key_pair_block_length(key_pair)));
81 ASSERT_SUCCESS(aws_byte_buf_append(&decrypted, &prefix));
82 ASSERT_SUCCESS(aws_rsa_key_pair_decrypt(key_pair, algo, ciphertext_cur, &decrypted));
83
84 struct aws_byte_cursor decrypted_cur = aws_byte_cursor_from_buf(&decrypted);
85 ASSERT_TRUE(aws_byte_cursor_starts_with(&decrypted_cur, &prefix));
86
87 aws_byte_cursor_advance(&decrypted_cur, prefix.len);
88 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(decrypted_cur, TEST_ENCRYPTION_STRING);
89
90 aws_byte_buf_clean_up_secure(&ciphertext_short);
91 aws_byte_buf_clean_up_secure(&decrypted_short);
92 aws_byte_buf_clean_up_secure(&ciphertext);
93 aws_byte_buf_clean_up_secure(&decrypted);
94
95 return AWS_OP_SUCCESS;
96 }
97
98 static const char *TEST_PKCS1_RSA_PRIVATE_KEY_2048 = "MIIEpQIBAAKCAQEAnt/K3mvMgPQcplTgDHrItQPvXiYpUFMt5nIJbZV820Zj4n4G"
99 "7iB3Y9h5HzfBYga2Olr8Irv3OuKkIH0ydrdz2oBZuf7SOBQVpro3m1+oMKhcCtrI"
100 "GYA2MDOowaVkx6ho8pQ6K2d76pYj7GWfo0fm2p1O2jcw3JWXAPqq8dmTCMRxOw/2"
101 "1eB/6bto8vayljXy85WiCPm7WTZ2mhB9tvkSRijDVF+SEILdkVPPUT1eqox+me2Y"
102 "SM2qaXVtToscqoicOqXD8XrWFuyqeLe29CiZAA9xqmit9o/ckdNXTjiGp6cIx2qC"
103 "Svbkxwi7OK0BB4y1LOTVz021jbJRr9b+ZbP0zwIDAQABAoIBAQCU5+ort9uwDZyA"
104 "pVJtP/O3/V0v4BKez6dYsw91H0Qr/PiHg1mZfOKJuY4knUxqRSIs5bQmFgitr1jn"
105 "fpB6xo0WgXAXrOd5WhHE+ApAXVK1cEb8gXxEsm+XlAOapBsmKwlaO2Wd4ts4zsoS"
106 "ulj6X9zWj9QlIM9yH96tM0Rfc26lKoRx+jkEml24nOia6gBhnfups/Kq/sUxtnX7"
107 "qQTuCmIuwdDMWTnW/AYlX6+wsSRgl7iUhnoOpbl18AzaIJbgcw49yE0xp2XVRWew"
108 "VR86EsF7pR4hxpORgysiDmyQLLfcz85eiub3tE/A4uHUzxd37e1OwqItvrG4s/4y"
109 "YNiqEbz5AoGBAOSs57pjCbh92UPMeMKhPhGSdlxQ/GkjHIDpUuy5oU2ZB2akKg5Y"
110 "Asl51tibKsTuDR5qNtUJGEw8cMVR3A+t7p4KE7eCzRmZj/bNDBSdxnTec83Y5KcZ"
111 "Pqi4DktHju7mArlIhmnphqOrXvDuJoIjMGFMVNACk05loxPpg3WiCS+NAoGBALHb"
112 "sbP6ftkL8M9vMMRrL/Jzlz3jS0smNiJxRmW2TrpF7h0o3QeW6uzDigd1pjg40bUl"
113 "0/NilaWtlK4DRWQ+0FYuxrDQd3vHiZ38uoCJZVkSWxzSytFSaM0rpU2l28aWIwL0"
114 "ZuIk3k3l2gQBqX2VUrMFxq2MJF5ZN/OgOrUIqEDLAoGBAL7JG1TASF3qcZhFQgNw"
115 "L67NeX6v+sdlCeTrxcnHXjK1mB0kngn1l+2sf3mci+RdkAhuKW0391OzoYqfL3DN"
116 "dqXYVnbm5GOVYS1SCeAxemALMKbvbGWVhFeTqClafIAI2wDm52357eEjm0R8DRjK"
117 "bxTecGxTmb7wwUxdqNY96FgJAoGAHtLyYzzAiyE0pN6iVwg0kRJTXdhsjiObMjDr"
118 "gGkuD75a3Bbe55fSMyJYY56SJiBCx+A8cWvef44rvFS4y/zO4oDM0ovuiTc1tHm+"
119 "YNRvChbST5aAq/JaU2SDC4f5JNuUScjNo9e750g0lokrNKaSZJBVtHIbQ3a26bQV"
120 "OJa9gi0CgYEAlhk8ThPcokpqIuweY4cx34zdU1EAm/CBO1ax1Ogs/HilPFyGYXzz"
121 "tZI9TCH4Sq33MGEjf2MyW0XMXC56dA2VOPSTHGKaoKmyn7L9G4WfDFcYmCdvmLkR"
122 "7wAz2Dyxr6ImChSWD/y2ddz1U+H39uqRxwIkwJ7TbDflYNXgsAOOlUg=";
123
124 static const char *TEST_PKCS8_RSA_PRIVATE_KEY_2048 = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDN0jYhXzsI1lSA"
125 "j+3uxraUu/78uHJlCcTifQlVUftgEzJth7WNGvrJ8bDLUHwV04cTYSkydgsivjms"
126 "XFgzuTCOLiHX0ik/RT1fOvOpk0gte2gCfPIACUAkTlGXKJ+v+1kqnWFmZFNvtkz2"
127 "fmLIRq9ZrXWORVmySCeoSfvyygxkmfTUfGieFr8LMFSB6VoWKA/vOk0sMF/5PRxm"
128 "JESS4pOMjWQ3QHpLdjsgUAnxsThLVMbaA5JXdHnFHlgkTbL/OSY06EUzuZ0dJpTU"
129 "qSBbaz6qewlIO4eM9d2N+5d/mt5nn4rU7yuLGlJE/U9UpSutuvkCkqosST9yKBz7"
130 "YUhK7WyPAgMBAAECggEAYrHEZyhFJK2yA5wA2hjLgHLNiN3hbPXMRVbz3MfdJGrQ"
131 "KZmDw1AGpkORJU1I0yaFhRN4L8xO9rAE89OsL9FDqUoRzG3ofYB0N3ALW2tWlwiw"
132 "DVFgsge9jCtKEJPYTwjV7wtcoz7Ei7L9IM3mDGdoujXlQv2aT1UuPxKLEBc27h3Q"
133 "KZZL0QbA33SmO24BUx9Y741hIXsVvoce0MQM1TPwEGH18qYpffb3kCBTwFEySx+q"
134 "JFwadZiK1JPQgpELrXZT0VuARD2Ze8Z7c0b6668wYiwJ2mmf4l0NTLk67mIIfG1w"
135 "NNNzHme/UGQEjSjUU9TFWLVHU9enumPv5UeLx3wvgQKBgQD7k6pCk8kkUcjjq0sq"
136 "92Coy1DMlZg9ictcusJ5pfe1ZdW01iX/S/88ZOxk1X27jTWcHarAeA+ci4LzDWPG"
137 "I61Hy4kJFOJB6Sx3hDSnfNvuIC/7zrcWLNzvfAeDYmPhAqi1K4HYFN7Ipa/1+Uew"
138 "kWVllcUHRz5zWU+oYVXDsjARrwKBgQDRcJowibhaG4UUMzRsyyEK1l/w+z75L8A7"
139 "2ZA+wMqyUo9LPgJRYvEHHRQTHDGCzP81IUaAK2OfWrD5Jnn6r4Il7+cQ+xCHKiMD"
140 "rBhnL9dG4lVAWgdZPyWXfrdT3mqpZ3UgfWysvph8s48QdLA3ku7PpTfchwtSdXQP"
141 "cxhEItlrIQKBgQDTTB8AdCfIfXiA3+nuWH+yxbFDY5HOfeF0LNgSXDdFABcSH5si"
142 "Za4mB44U0ssbr2qLiM9VgIF8NiDyCxj13hk359dc7VFrknBqoXuoANKnmhkzIVfd"
143 "JCkca8vTqdvBrP4NzFDuL/k+BQtZSNnRjwze2X/2sPve3fBtt/LUvuBouQKBgQC9"
144 "T1Cv6uxN1m410grjA8C8MQXLpu5HAxh5gLBXaKBPCz0mv8gMlKhUy73ngCZomq9b"
145 "8NXu6ElGMw2gR10ecSHs9KohuS45XqcDnLz6GE44bkCsyDO4QdHS2+EN2A8FTNSc"
146 "J4Lhqe3fWdZJA5B8yz09R5P0q8RaJnxfsqMOg4mOwQKBgQC+N5xLCRa/n1kJ11we"
147 "rnOe2POS+zuThhi1aMn0LLPIDwVMktymV7F8JegbdYB5KjxyxzDPcpRDRKCXvAew"
148 "QiaCZLRyigBqDpDP4l3uIp1OEzWLYuEAWwnErC7fuPm0TFAy5ecegxW7eXasDy2C"
149 "dJJcK3yV8NRVOzr2voGRmr4d7w==";
150
151 static int s_rsa_encryption_roundtrip_from_user(
152 struct aws_allocator *allocator,
153 enum aws_rsa_encryption_algorithm algo) {
154 struct aws_byte_buf key_buf;
155 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
156 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_2048), &key_buf));
157
158 struct aws_rsa_key_pair *key_pair =
159 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&key_buf));
160
161 ASSERT_NOT_NULL(key_pair);
162
163 s_rsa_encryption_roundtrip_helper(allocator, key_pair, algo);
164
165 aws_rsa_key_pair_release(key_pair);
166 aws_byte_buf_clean_up_secure(&key_buf);
167
168 return AWS_OP_SUCCESS;
169 }
170
171 static int s_rsa_encryption_roundtrip_from_user_pkcs8(
172 struct aws_allocator *allocator,
173 enum aws_rsa_encryption_algorithm algo) {
174 struct aws_byte_buf key_buf;
175 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
176 allocator, aws_byte_cursor_from_c_str(TEST_PKCS8_RSA_PRIVATE_KEY_2048), &key_buf));
177
178 struct aws_rsa_key_pair *key_pair =
179 aws_rsa_key_pair_new_from_private_key_pkcs8(allocator, aws_byte_cursor_from_buf(&key_buf));
180
181 ASSERT_NOT_NULL(key_pair);
182
183 s_rsa_encryption_roundtrip_helper(allocator, key_pair, algo);
184
185 aws_rsa_key_pair_release(key_pair);
186 aws_byte_buf_clean_up_secure(&key_buf);
187
188 return AWS_OP_SUCCESS;
189 }
190
191 static int s_rsa_encryption_roundtrip_pkcs1_from_user(struct aws_allocator *allocator, void *ctx) {
192 (void)ctx;
193
194 aws_cal_library_test_init(allocator);
195
196 ASSERT_SUCCESS(s_rsa_encryption_roundtrip_from_user(allocator, AWS_CAL_RSA_ENCRYPTION_PKCS1_5));
197
198 aws_cal_library_clean_up();
199
200 return AWS_OP_SUCCESS;
201 }
202 AWS_TEST_CASE(rsa_encryption_roundtrip_pkcs1_from_user, s_rsa_encryption_roundtrip_pkcs1_from_user);
203
204 static int s_rsa_encryption_roundtrip_pkcs15_from_user_pkcs8(struct aws_allocator *allocator, void *ctx) {
205 (void)ctx;
206
207 aws_cal_library_test_init(allocator);
208
209 ASSERT_SUCCESS(s_rsa_encryption_roundtrip_from_user_pkcs8(allocator, AWS_CAL_RSA_ENCRYPTION_PKCS1_5));
210
211 aws_cal_library_clean_up();
212
213 return AWS_OP_SUCCESS;
214 }
215 AWS_TEST_CASE(rsa_encryption_roundtrip_pkcs15_from_user_pkcs8, s_rsa_encryption_roundtrip_pkcs15_from_user_pkcs8);
216
217 static int s_rsa_pkcs8_key_load_error(struct aws_allocator *allocator, void *ctx) {
218 (void)ctx;
219
220 aws_cal_library_test_init(allocator);
221
222 struct aws_byte_buf key_buf;
223 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
224 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_2048), &key_buf));
225
226 struct aws_rsa_key_pair *key_pair =
227 aws_rsa_key_pair_new_from_private_key_pkcs8(allocator, aws_byte_cursor_from_buf(&key_buf));
228
229 ASSERT_NULL(key_pair);
230 ASSERT_INT_EQUALS(AWS_ERROR_CAL_MALFORMED_ASN1_ENCOUNTERED, aws_last_error());
231
232 aws_cal_library_clean_up();
233
234 return AWS_OP_SUCCESS;
235 }
236 AWS_TEST_CASE(rsa_pkcs8_key_load_error, s_rsa_pkcs8_key_load_error);
237
238 static int s_rsa_encryption_roundtrip_oaep_sha256_from_user(struct aws_allocator *allocator, void *ctx) {
239 (void)ctx;
240
241 aws_cal_library_test_init(allocator);
242
243 ASSERT_SUCCESS(s_rsa_encryption_roundtrip_from_user(allocator, AWS_CAL_RSA_ENCRYPTION_OAEP_SHA256));
244
245 aws_cal_library_clean_up();
246
247 return AWS_OP_SUCCESS;
248 }
249 AWS_TEST_CASE(rsa_encryption_roundtrip_oaep_sha256_from_user, s_rsa_encryption_roundtrip_oaep_sha256_from_user);
250
251 static int s_rsa_encryption_roundtrip_oaep_sha512_from_user(struct aws_allocator *allocator, void *ctx) {
252 (void)ctx;
253
254 aws_cal_library_test_init(allocator);
255
256 ASSERT_SUCCESS(s_rsa_encryption_roundtrip_from_user(allocator, AWS_CAL_RSA_ENCRYPTION_OAEP_SHA512));
257
258 aws_cal_library_clean_up();
259
260 return AWS_OP_SUCCESS;
261 }
262 AWS_TEST_CASE(rsa_encryption_roundtrip_oaep_sha512_from_user, s_rsa_encryption_roundtrip_oaep_sha512_from_user);
263
264 static const char *TEST_PKCS1_RSA_PRIVATE_KEY_1024 = "MIICXAIBAAKBgQCVGG9c6uBIpv4wQMB4PJpkaUjEqa6TW5v8ebStMCnICWfpUubs"
265 "f6nr5nPxPsG2rw+HZSLJLAYVYfXOM9D+KyhTdpIJ7z4NXzXzem25x6H/N9WnRGjB"
266 "6qAOLg5Srm3uoXfulN5HVXVtncTFcJJQxgBOpT8qXLycm5k8uhm0OWP6cwIDAQAB"
267 "AoGAT0IRGU0G87hKUi5p4sEctho+A8XMNyuw7XNpd9OtslhFtARNHBX1p6D3q5xX"
268 "8Bx53dkGt/i+Nym/OOHUzPj2Uy+qprBFYK71JavQyg80h8deUsQzrKTrM45pU+U7"
269 "uGgEg24Mw4hQ53ky6HUJtRk/PG6osx8o4DMPU0EKSeoxqMECQQDVFC7qH3m+gHoE"
270 "xnz+uIR1H78NQt3sa2cnZDa0ui/Ew+UASQlDDY/xqAGYa+QAhQBMZoWJLL6AaNrj"
271 "FtxyKr+TAkEAsyDTfwJPFRTvqUNKKIsFHlNlzDclISHcIi00ST2bDKox7pS3aukE"
272 "dkytVIerIKtBMds5gjYZybAAX0cC7DHloQJAbt5VmtRN0GWhF6L/nrn7kcW27vt/"
273 "5WftAH4QSPEnscYL/Z4DB7Si1SaJzfk1ZV/Oy/H8QWfap43ndomKoozDqQJAX9lk"
274 "0kVuA53cT/oNqHwbFQsTIZ8wYvY3UKJXpAku+ivn4/3312EwXgzRgrXFwAljLUZd"
275 "E2vXiLCAwnrA+ZoJgQJBAI/P1XTqEAUro5aDD64JuwbvCpbAL8kkwGzf6wzrdF+f"
276 "0CXKkTGUEG7BGWqCr9y9nBt9KuyN1VlNbziJp+UcKVc=";
277
278 static const char *TEST_PKCS1_RSA_PUBLIC_KEY_1024 = "MIGJAoGBAJUYb1zq4Eim/jBAwHg8mmRpSMSprpNbm/x5tK0wKcgJZ+lS5ux/qevmc/"
279 "E+wbavD4dlIsksBhVh9c4z0P4rKFN2kgnvPg1fNfN6bbnHof831adEaMHqoA4uDlKu"
280 "be6hd+6U3kdVdW2dxMVwklDGAE6lPypcvJybmTy6GbQ5Y/pzAgMBAAE=";
281
282 /*
283 * pkcs1 signature generator using above private key and test encryption string.
284 */
285 static const char *TEST_RSA_SIGNATURE_PKCS1 = "Gqu9pLlPvSFIW+5ZFo9ZCxMmPR8LnAeiuYir5CfNTyraF2VPksRnCKtS6i98nwPUqzlPr"
286 "TYJ45P3c94lQIQD3SVJ3XMSAyAEWTE2pcj0F/oPzzxLcXK9cyv2Iphe4XuBjWCOVdHgFg"
287 "rD/yAA8b+B94AqE9U/B2+k9/C3Bz2YApo=";
288
289 static const char *TEST_RSA_SIGNATURE_PKCS1_SHA1 =
290 "QJxUx+4OM+v1wh0z0PJWMeGBdvjhQHxjFCdzDLeNH6zoJpgPtFXNk6i83ff75MgPpW0m33"
291 "zrrzLWfzLojZFi2KFXYu1Y39We3AfREEOyG+porTmxNQ4dJH29joXS0XNf52dJpN04Lw3WN"
292 "XUHDP6eG2K71uXlsus2Tm0uBe4TF0g=";
293
294 static const char *TEST_RSA_SIGNATURE_PSS = "j//04sVoqQVSmUgH+Id0oad7OgW+hGnIqx6hjr28VnVk75Obig+n3tJGWd0r+3S4ARxf2fK"
295 "7taVvJXISQ5aWJAYx6QRgR+25rcE96eOfi6L7ShIZIUYFzGxhc9wpUMGbqHEIhm+8QP7uNo4D"
296 "FmaPzJMgGDKL2qhedxnjtg3p8E4=";
297
298 static const char *TEST_RSA_ENCRYPTED_PKCS1 =
299 "Ng97Q53hLqC0sCNMTG6poSxXeTLVWFQJS746y1VLnDD0/IYWk/gyzhNEF0M16loaBswNLnEgL"
300 "OsTVHmBaglCiEobyWBYO16HO+hrJeXK76p1GfIQ+62hSwpnxx4abqS9N2rX59ahMNSnjXZmFiQn"
301 "yPDbvp2UYwUydSu6ArOM/H8=";
302
303 static const char *TEST_RSA_ENCRYPTED_OAEP256 =
304 "YB9CDU8z+ViRSQRvE6z3i3mFMh1NFOgKuhcYGIhZu0wqTzVV4c6Rl+x9gMQiURkLG0q1/nAF"
305 "upW5g1uo5wotJKb5GCGF8oYuMu7IemY45jBIZ3tXSz1XeZ8VHVCpBNGJBP//Pp461HI9qzaPA+mFu"
306 "jBppHZTE0GLpbZeryHRgK4qPR4J+EzojiE2JrzCST8Y1xrCwvwS6QjboeorVSr8ssO8oC3HJ89klg"
307 "uEq19eLTp0JP8WWnREJtGfbeIW6nGeu3KEjwnXD+A//Qk5fIxPFBV4+1kTDkLyO22ZOzCevXUAv9j"
308 "97f1GRuJfS2W2KL/YXQudwX1xo5ULf1UIgpeqSQ==";
309
310 static const char *TEST_RSA_ENCRYPTED_OAEP512 =
311 "Wx5SdwnG1Fc0rEIZZRibRL9iUt16NydVC4Mbok50UKWf7DnhWen4H+KZW9K6bAvXHKKZx1Sog4"
312 "RAONa/rrPTWYipFgvNWEQmCHb0erEemjabx3QTu5HqJpbnU5HKAA2l7JGrV26AvyVpezJWHa3h"
313 "2xWLnw5JWhqL49vaZeMwtEopr2Dz0+wsH9QZaedQmRcEwO1f2QRrVbnbYFB6wjo3VF1IY7k8Dk"
314 "XiLg0m9Ivb0Gwx61gRTx0DKq3zr7CNm35E+c9ujYPdGtX0MjAJfXOHeuaspzsLVAI9gdvyZ3Ca/"
315 "vdEkky9ESL7Bw4tLysuqlvc2tnVuk3LXuB3QElDC3JU+A==";
316
317 static int s_rsa_verify_signing_pkcs1_sha256(struct aws_allocator *allocator, void *ctx) {
318 (void)ctx;
319 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
320
321 aws_cal_library_test_init(allocator);
322
323 struct aws_byte_buf public_key_buf;
324 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
325 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
326 struct aws_rsa_key_pair *key_pair_public =
327 aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
328 ASSERT_NOT_NULL(key_pair_public);
329
330 uint8_t hash[AWS_SHA256_LEN];
331 AWS_ZERO_ARRAY(hash);
332 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
333 aws_sha256_compute(allocator, &message, &hash_value, 0);
334 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
335
336 struct aws_byte_buf signature_buf;
337 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
338 allocator, aws_byte_cursor_from_c_str(TEST_RSA_SIGNATURE_PKCS1), &signature_buf));
339 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf);
340
341 ASSERT_SUCCESS(aws_rsa_key_pair_verify_signature(
342 key_pair_public, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, hash_cur, signature_cur));
343
344 aws_byte_buf_clean_up(&hash_value);
345 aws_byte_buf_clean_up(&signature_buf);
346 aws_byte_buf_clean_up(&public_key_buf);
347 aws_rsa_key_pair_release(key_pair_public);
348
349 aws_cal_library_clean_up();
350
351 return AWS_OP_SUCCESS;
352 }
353 AWS_TEST_CASE(rsa_verify_signing_pkcs1_sha256, s_rsa_verify_signing_pkcs1_sha256);
354
355 static int s_rsa_verify_signing_pkcs1_sha1(struct aws_allocator *allocator, void *ctx) {
356 (void)ctx;
357 if (!s_crypto_supports_sha1_signing()) {
358 return AWS_OP_SKIP;
359 }
360
361 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
362
363 aws_cal_library_init(allocator);
364
365 struct aws_byte_buf public_key_buf;
366 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
367 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
368 struct aws_rsa_key_pair *key_pair_public =
369 aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
370 ASSERT_NOT_NULL(key_pair_public);
371
372 uint8_t hash[AWS_SHA1_LEN];
373 AWS_ZERO_ARRAY(hash);
374 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
375 aws_sha1_compute(allocator, &message, &hash_value, 0);
376 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
377
378 struct aws_byte_buf signature_buf;
379 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
380 allocator, aws_byte_cursor_from_c_str(TEST_RSA_SIGNATURE_PKCS1_SHA1), &signature_buf));
381 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf);
382
383 ASSERT_SUCCESS(aws_rsa_key_pair_verify_signature(
384 key_pair_public, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, hash_cur, signature_cur));
385
386 aws_byte_buf_clean_up(&hash_value);
387 aws_byte_buf_clean_up(&signature_buf);
388 aws_byte_buf_clean_up(&public_key_buf);
389 aws_rsa_key_pair_release(key_pair_public);
390
391 aws_cal_library_clean_up();
392
393 return AWS_OP_SUCCESS;
394 }
395 AWS_TEST_CASE(rsa_verify_signing_pkcs1_sha1, s_rsa_verify_signing_pkcs1_sha1);
396
397 static int s_rsa_verify_signing_pss_sha256(struct aws_allocator *allocator, void *ctx) {
398 (void)ctx;
399 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
400
401 aws_cal_library_test_init(allocator);
402
403 struct aws_byte_buf public_key_buf;
404 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
405 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
406 struct aws_rsa_key_pair *key_pair_public =
407 aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
408 ASSERT_NOT_NULL(key_pair_public);
409
410 uint8_t hash[AWS_SHA256_LEN];
411 AWS_ZERO_ARRAY(hash);
412 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
413 aws_sha256_compute(allocator, &message, &hash_value, 0);
414 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
415
416 struct aws_byte_buf signature_buf;
417 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
418 allocator, aws_byte_cursor_from_c_str(TEST_RSA_SIGNATURE_PSS), &signature_buf));
419 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf);
420
421 ASSERT_SUCCESS(
422 aws_rsa_key_pair_verify_signature(key_pair_public, AWS_CAL_RSA_SIGNATURE_PSS_SHA256, hash_cur, signature_cur));
423
424 aws_byte_buf_clean_up(&hash_value);
425 aws_byte_buf_clean_up(&signature_buf);
426 aws_byte_buf_clean_up(&public_key_buf);
427 aws_rsa_key_pair_release(key_pair_public);
428
429 aws_cal_library_clean_up();
430
431 return AWS_OP_SUCCESS;
432 }
433 AWS_TEST_CASE(rsa_verify_signing_pss_sha256, s_rsa_verify_signing_pss_sha256);
434
435 static int s_rsa_decrypt_pkcs1(struct aws_allocator *allocator, void *ctx) {
436 (void)ctx;
437
438 aws_cal_library_test_init(allocator);
439
440 struct aws_byte_buf private_key_buf;
441 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
442 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &private_key_buf));
443 struct aws_rsa_key_pair *key_pair_private =
444 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&private_key_buf));
445 ASSERT_NOT_NULL(key_pair_private);
446
447 struct aws_byte_buf encrypted;
448 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
449 allocator, aws_byte_cursor_from_c_str(TEST_RSA_ENCRYPTED_PKCS1), &encrypted));
450 struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted);
451
452 struct aws_byte_buf decrypted;
453 aws_byte_buf_init(&decrypted, allocator, aws_rsa_key_pair_block_length(key_pair_private));
454
455 ASSERT_SUCCESS(
456 aws_rsa_key_pair_decrypt(key_pair_private, AWS_CAL_RSA_ENCRYPTION_PKCS1_5, encrypted_cur, &decrypted));
457
458 struct aws_byte_cursor decrypted_cur = aws_byte_cursor_from_buf(&decrypted);
459
460 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(decrypted_cur, TEST_ENCRYPTION_STRING);
461
462 aws_byte_buf_clean_up(&private_key_buf);
463 aws_byte_buf_clean_up(&decrypted);
464 aws_byte_buf_clean_up(&encrypted);
465 aws_rsa_key_pair_release(key_pair_private);
466
467 aws_cal_library_clean_up();
468
469 return AWS_OP_SUCCESS;
470 }
471 AWS_TEST_CASE(rsa_decrypt_pkcs1, s_rsa_decrypt_pkcs1);
472
473 static int s_rsa_decrypt_oaep256(struct aws_allocator *allocator, void *ctx) {
474 (void)ctx;
475
476 aws_cal_library_test_init(allocator);
477
478 struct aws_byte_buf private_key_buf;
479 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
480 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_2048), &private_key_buf));
481 struct aws_rsa_key_pair *key_pair_private =
482 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&private_key_buf));
483 ASSERT_NOT_NULL(key_pair_private);
484
485 struct aws_byte_buf encrypted;
486 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
487 allocator, aws_byte_cursor_from_c_str(TEST_RSA_ENCRYPTED_OAEP256), &encrypted));
488 struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted);
489
490 struct aws_byte_buf decrypted;
491 ASSERT_SUCCESS(aws_byte_buf_init(&decrypted, allocator, aws_rsa_key_pair_block_length(key_pair_private)));
492
493 ASSERT_SUCCESS(
494 aws_rsa_key_pair_decrypt(key_pair_private, AWS_CAL_RSA_ENCRYPTION_OAEP_SHA256, encrypted_cur, &decrypted));
495
496 struct aws_byte_cursor decrypted_cur = aws_byte_cursor_from_buf(&decrypted);
497
498 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(decrypted_cur, TEST_ENCRYPTION_STRING);
499
500 aws_byte_buf_clean_up(&private_key_buf);
501 aws_byte_buf_clean_up(&decrypted);
502 aws_byte_buf_clean_up(&encrypted);
503 aws_rsa_key_pair_release(key_pair_private);
504
505 aws_cal_library_clean_up();
506
507 return AWS_OP_SUCCESS;
508 }
509 AWS_TEST_CASE(rsa_decrypt_oaep256, s_rsa_decrypt_oaep256);
510
511 static int s_rsa_decrypt_oaep512(struct aws_allocator *allocator, void *ctx) {
512 (void)ctx;
513
514 aws_cal_library_test_init(allocator);
515
516 struct aws_byte_buf private_key_buf;
517 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
518 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_2048), &private_key_buf));
519 struct aws_rsa_key_pair *key_pair_private =
520 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&private_key_buf));
521 ASSERT_NOT_NULL(key_pair_private);
522
523 struct aws_byte_buf encrypted;
524 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
525 allocator, aws_byte_cursor_from_c_str(TEST_RSA_ENCRYPTED_OAEP512), &encrypted));
526 struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted);
527
528 struct aws_byte_buf decrypted;
529 ASSERT_SUCCESS(aws_byte_buf_init(&decrypted, allocator, aws_rsa_key_pair_block_length(key_pair_private)));
530
531 ASSERT_SUCCESS(
532 aws_rsa_key_pair_decrypt(key_pair_private, AWS_CAL_RSA_ENCRYPTION_OAEP_SHA512, encrypted_cur, &decrypted));
533
534 struct aws_byte_cursor decrypted_cur = aws_byte_cursor_from_buf(&decrypted);
535
536 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(decrypted_cur, TEST_ENCRYPTION_STRING);
537
538 aws_byte_buf_clean_up(&private_key_buf);
539 aws_byte_buf_clean_up(&decrypted);
540 aws_byte_buf_clean_up(&encrypted);
541 aws_rsa_key_pair_release(key_pair_private);
542
543 aws_cal_library_clean_up();
544
545 return AWS_OP_SUCCESS;
546 }
547 AWS_TEST_CASE(rsa_decrypt_oaep512, s_rsa_decrypt_oaep512);
548
549 static int s_rsa_signing_roundtrip_helper(
550 struct aws_allocator *allocator,
551 struct aws_rsa_key_pair *key_pair_private,
552 struct aws_rsa_key_pair *key_pair_public,
553 enum aws_rsa_signature_algorithm algo,
554 const char *expected_signature) {
555 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
556
557 uint8_t hash[AWS_SHA256_LEN];
558 AWS_ZERO_ARRAY(hash);
559 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
560 if (algo == AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1) {
561 aws_sha1_compute(allocator, &message, &hash_value, 0);
562 } else {
563 aws_sha256_compute(allocator, &message, &hash_value, 0);
564 }
565
566 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
567
568 /*since our apis work by appending to buffer, lets make sure they dont
569 *clobber anything already in the buffer*/
570 struct aws_byte_cursor prefix = aws_byte_cursor_from_c_str("random_prefix");
571 struct aws_byte_buf signature;
572 ASSERT_SUCCESS(
573 aws_byte_buf_init(&signature, allocator, prefix.len + aws_rsa_key_pair_signature_length(key_pair_private)));
574 ASSERT_SUCCESS(aws_byte_buf_append(&signature, &prefix));
575 ASSERT_SUCCESS(aws_rsa_key_pair_sign_message(key_pair_private, algo, hash_cur, &signature));
576
577 /*short buffer should fail*/
578 struct aws_byte_buf signature_short;
579 ASSERT_SUCCESS(aws_byte_buf_init(&signature_short, allocator, 5));
580 ASSERT_ERROR(
581 AWS_ERROR_SHORT_BUFFER, aws_rsa_key_pair_sign_message(key_pair_private, algo, hash_cur, &signature_short));
582
583 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature);
584 ASSERT_TRUE(aws_byte_cursor_starts_with(&signature_cur, &prefix));
585 aws_byte_cursor_advance(&signature_cur, prefix.len);
586
587 if (expected_signature) {
588 struct aws_byte_buf sig_b64_buf;
589 size_t encoded_length = 0;
590 ASSERT_SUCCESS(aws_base64_compute_encoded_len(signature.len, &encoded_length));
591 ASSERT_SUCCESS(aws_byte_buf_init(&sig_b64_buf, allocator, encoded_length));
592 ASSERT_SUCCESS(aws_base64_encode(&signature_cur, &sig_b64_buf));
593 struct aws_byte_cursor sig_b64_cur = aws_byte_cursor_from_buf(&sig_b64_buf);
594 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(sig_b64_cur, expected_signature);
595 aws_byte_buf_clean_up_secure(&sig_b64_buf);
596 }
597
598 ASSERT_SUCCESS(aws_rsa_key_pair_verify_signature(key_pair_public, algo, hash_cur, signature_cur));
599
600 aws_byte_buf_clean_up_secure(&signature);
601 aws_byte_buf_clean_up_secure(&signature_short);
602
603 return AWS_OP_SUCCESS;
604 }
605
606 static int s_rsa_signing_roundtrip_from_user(
607 struct aws_allocator *allocator,
608 enum aws_rsa_signature_algorithm algo,
609 const char *expected_signature) {
610
611 struct aws_byte_buf private_key_buf;
612 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
613 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &private_key_buf));
614 struct aws_rsa_key_pair *key_pair_private =
615 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&private_key_buf));
616 ASSERT_NOT_NULL(key_pair_private);
617
618 struct aws_byte_buf public_key_buf;
619 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
620 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
621 struct aws_rsa_key_pair *key_pair_public =
622 aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
623 ASSERT_NOT_NULL(key_pair_public);
624
625 s_rsa_signing_roundtrip_helper(allocator, key_pair_private, key_pair_public, algo, expected_signature);
626
627 aws_rsa_key_pair_release(key_pair_private);
628 aws_rsa_key_pair_release(key_pair_public);
629 aws_byte_buf_clean_up_secure(&private_key_buf);
630 aws_byte_buf_clean_up_secure(&public_key_buf);
631
632 return AWS_OP_SUCCESS;
633 }
634
635 static int s_rsa_signing_roundtrip_pkcs1_sha256_from_user(struct aws_allocator *allocator, void *ctx) {
636 (void)ctx;
637
638 aws_cal_library_test_init(allocator);
639
640 ASSERT_SUCCESS(
641 s_rsa_signing_roundtrip_from_user(allocator, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, TEST_RSA_SIGNATURE_PKCS1));
642
643 aws_cal_library_clean_up();
644
645 return AWS_OP_SUCCESS;
646 }
647 AWS_TEST_CASE(rsa_signing_roundtrip_pkcs1_sha256_from_user, s_rsa_signing_roundtrip_pkcs1_sha256_from_user);
648
649 static int s_rsa_signing_roundtrip_pkcs1_sha1_from_user(struct aws_allocator *allocator, void *ctx) {
650 (void)ctx;
651
652 if (!s_crypto_supports_sha1_signing()) {
653 return AWS_OP_SKIP;
654 }
655
656 aws_cal_library_init(allocator);
657
658 ASSERT_SUCCESS(s_rsa_signing_roundtrip_from_user(
659 allocator, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, TEST_RSA_SIGNATURE_PKCS1_SHA1));
660
661 aws_cal_library_clean_up();
662
663 return AWS_OP_SUCCESS;
664 }
665 AWS_TEST_CASE(rsa_signing_roundtrip_pkcs1_sha1_from_user, s_rsa_signing_roundtrip_pkcs1_sha1_from_user);
666
667 static int s_rsa_signing_roundtrip_pss_sha256_from_user(struct aws_allocator *allocator, void *ctx) {
668 (void)ctx;
669
670 aws_cal_library_test_init(allocator);
671
672 #if defined(AWS_OS_MACOS)
673 if (__builtin_available(macOS 10.12, *)) {
674 ASSERT_SUCCESS(s_rsa_signing_roundtrip_from_user(allocator, AWS_CAL_RSA_SIGNATURE_PSS_SHA256, NULL));
675 } else {
676 ASSERT_ERROR(
677 AWS_ERROR_CAL_UNSUPPORTED_ALGORITHM,
678 s_rsa_signing_roundtrip_from_user(allocator, AWS_CAL_RSA_SIGNATURE_PSS_SHA256, NULL));
679 }
680 #else
681 ASSERT_SUCCESS(s_rsa_signing_roundtrip_from_user(allocator, AWS_CAL_RSA_SIGNATURE_PSS_SHA256, NULL));
682 #endif
683
684 aws_cal_library_clean_up();
685
686 return AWS_OP_SUCCESS;
687 }
688 AWS_TEST_CASE(rsa_signing_roundtrip_pss_sha256_from_user, s_rsa_signing_roundtrip_pss_sha256_from_user);
689
690 static int s_rsa_getters(struct aws_allocator *allocator, void *ctx) {
691 (void)ctx;
692
693 aws_cal_library_test_init(allocator);
694
695 struct aws_byte_buf private_key_buf;
696 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
697 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &private_key_buf));
698 struct aws_rsa_key_pair *key_pair_private =
699 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&private_key_buf));
700
701 ASSERT_NOT_NULL(key_pair_private);
702 ASSERT_INT_EQUALS(128, aws_rsa_key_pair_block_length(key_pair_private));
703 ASSERT_INT_EQUALS(128, aws_rsa_key_pair_signature_length(key_pair_private));
704
705 struct aws_byte_buf priv_key;
706 ASSERT_SUCCESS(aws_rsa_key_pair_get_private_key(key_pair_private, AWS_CAL_RSA_KEY_EXPORT_PKCS1, &priv_key));
707 ASSERT_TRUE(priv_key.len > 0);
708
709 struct aws_byte_buf public_key_buf;
710 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
711 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
712 struct aws_rsa_key_pair *key_pair_public =
713 aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
714
715 ASSERT_INT_EQUALS(128, aws_rsa_key_pair_block_length(key_pair_public));
716 ASSERT_INT_EQUALS(128, aws_rsa_key_pair_signature_length(key_pair_public));
717
718 struct aws_byte_buf pub_key;
719 ASSERT_SUCCESS(aws_rsa_key_pair_get_public_key(key_pair_public, AWS_CAL_RSA_KEY_EXPORT_PKCS1, &pub_key));
720 ASSERT_TRUE(pub_key.len > 0);
721
722 aws_rsa_key_pair_release(key_pair_private);
723 aws_rsa_key_pair_release(key_pair_public);
724 aws_byte_buf_clean_up_secure(&private_key_buf);
725 aws_byte_buf_clean_up_secure(&public_key_buf);
726 aws_byte_buf_clean_up_secure(&priv_key);
727 aws_byte_buf_clean_up_secure(&pub_key);
728
729 aws_cal_library_clean_up();
730
731 return AWS_OP_SUCCESS;
732 }
733 AWS_TEST_CASE(rsa_getters, s_rsa_getters);
734
735 static int s_rsa_private_pkcs1_der_parsing(struct aws_allocator *allocator, void *ctx) {
736 (void)ctx;
737
738 static uint8_t n[] = {0x95, 0x18, 0x6f, 0x5c, 0xea, 0xe0, 0x48, 0xa6, 0xfe, 0x30, 0x40, 0xc0, 0x78, 0x3c, 0x9a,
739 0x64, 0x69, 0x48, 0xc4, 0xa9, 0xae, 0x93, 0x5b, 0x9b, 0xfc, 0x79, 0xb4, 0xad, 0x30, 0x29,
740 0xc8, 0x09, 0x67, 0xe9, 0x52, 0xe6, 0xec, 0x7f, 0xa9, 0xeb, 0xe6, 0x73, 0xf1, 0x3e, 0xc1,
741 0xb6, 0xaf, 0x0f, 0x87, 0x65, 0x22, 0xc9, 0x2c, 0x06, 0x15, 0x61, 0xf5, 0xce, 0x33, 0xd0,
742 0xfe, 0x2b, 0x28, 0x53, 0x76, 0x92, 0x09, 0xef, 0x3e, 0x0d, 0x5f, 0x35, 0xf3, 0x7a, 0x6d,
743 0xb9, 0xc7, 0xa1, 0xff, 0x37, 0xd5, 0xa7, 0x44, 0x68, 0xc1, 0xea, 0xa0, 0x0e, 0x2e, 0x0e,
744 0x52, 0xae, 0x6d, 0xee, 0xa1, 0x77, 0xee, 0x94, 0xde, 0x47, 0x55, 0x75, 0x6d, 0x9d, 0xc4,
745 0xc5, 0x70, 0x92, 0x50, 0xc6, 0x00, 0x4e, 0xa5, 0x3f, 0x2a, 0x5c, 0xbc, 0x9c, 0x9b, 0x99,
746 0x3c, 0xba, 0x19, 0xb4, 0x39, 0x63, 0xfa, 0x73};
747
748 static uint8_t e[] = {0x01, 0x00, 0x01};
749
750 static uint8_t d[] = {0x4f, 0x42, 0x11, 0x19, 0x4d, 0x06, 0xf3, 0xb8, 0x4a, 0x52, 0x2e, 0x69, 0xe2, 0xc1, 0x1c,
751 0xb6, 0x1a, 0x3e, 0x03, 0xc5, 0xcc, 0x37, 0x2b, 0xb0, 0xed, 0x73, 0x69, 0x77, 0xd3, 0xad,
752 0xb2, 0x58, 0x45, 0xb4, 0x04, 0x4d, 0x1c, 0x15, 0xf5, 0xa7, 0xa0, 0xf7, 0xab, 0x9c, 0x57,
753 0xf0, 0x1c, 0x79, 0xdd, 0xd9, 0x06, 0xb7, 0xf8, 0xbe, 0x37, 0x29, 0xbf, 0x38, 0xe1, 0xd4,
754 0xcc, 0xf8, 0xf6, 0x53, 0x2f, 0xaa, 0xa6, 0xb0, 0x45, 0x60, 0xae, 0xf5, 0x25, 0xab, 0xd0,
755 0xca, 0x0f, 0x34, 0x87, 0xc7, 0x5e, 0x52, 0xc4, 0x33, 0xac, 0xa4, 0xeb, 0x33, 0x8e, 0x69,
756 0x53, 0xe5, 0x3b, 0xb8, 0x68, 0x04, 0x83, 0x6e, 0x0c, 0xc3, 0x88, 0x50, 0xe7, 0x79, 0x32,
757 0xe8, 0x75, 0x09, 0xb5, 0x19, 0x3f, 0x3c, 0x6e, 0xa8, 0xb3, 0x1f, 0x28, 0xe0, 0x33, 0x0f,
758 0x53, 0x41, 0x0a, 0x49, 0xea, 0x31, 0xa8, 0xc1};
759
760 static uint8_t p[] = {0xd5, 0x14, 0x2e, 0xea, 0x1f, 0x79, 0xbe, 0x80, 0x7a, 0x04, 0xc6, 0x7c, 0xfe,
761 0xb8, 0x84, 0x75, 0x1f, 0xbf, 0x0d, 0x42, 0xdd, 0xec, 0x6b, 0x67, 0x27, 0x64,
762 0x36, 0xb4, 0xba, 0x2f, 0xc4, 0xc3, 0xe5, 0x00, 0x49, 0x09, 0x43, 0x0d, 0x8f,
763 0xf1, 0xa8, 0x01, 0x98, 0x6b, 0xe4, 0x00, 0x85, 0x00, 0x4c, 0x66, 0x85, 0x89,
764 0x2c, 0xbe, 0x80, 0x68, 0xda, 0xe3, 0x16, 0xdc, 0x72, 0x2a, 0xbf, 0x93};
765
766 static uint8_t q[] = {0xb3, 0x20, 0xd3, 0x7f, 0x02, 0x4f, 0x15, 0x14, 0xef, 0xa9, 0x43, 0x4a, 0x28,
767 0x8b, 0x05, 0x1e, 0x53, 0x65, 0xcc, 0x37, 0x25, 0x21, 0x21, 0xdc, 0x22, 0x2d,
768 0x34, 0x49, 0x3d, 0x9b, 0x0c, 0xaa, 0x31, 0xee, 0x94, 0xb7, 0x6a, 0xe9, 0x04,
769 0x76, 0x4c, 0xad, 0x54, 0x87, 0xab, 0x20, 0xab, 0x41, 0x31, 0xdb, 0x39, 0x82,
770 0x36, 0x19, 0xc9, 0xb0, 0x00, 0x5f, 0x47, 0x02, 0xec, 0x31, 0xe5, 0xa1};
771
772 static uint8_t dmp1[] = {0x6e, 0xde, 0x55, 0x9a, 0xd4, 0x4d, 0xd0, 0x65, 0xa1, 0x17, 0xa2, 0xff, 0x9e,
773 0xb9, 0xfb, 0x91, 0xc5, 0xb6, 0xee, 0xfb, 0x7f, 0xe5, 0x67, 0xed, 0x00, 0x7e,
774 0x10, 0x48, 0xf1, 0x27, 0xb1, 0xc6, 0x0b, 0xfd, 0x9e, 0x03, 0x07, 0xb4, 0xa2,
775 0xd5, 0x26, 0x89, 0xcd, 0xf9, 0x35, 0x65, 0x5f, 0xce, 0xcb, 0xf1, 0xfc, 0x41,
776 0x67, 0xda, 0xa7, 0x8d, 0xe7, 0x76, 0x89, 0x8a, 0xa2, 0x8c, 0xc3, 0xa9};
777
778 static uint8_t dmq1[] = {0x5f, 0xd9, 0x64, 0xd2, 0x45, 0x6e, 0x03, 0x9d, 0xdc, 0x4f, 0xfa, 0x0d, 0xa8,
779 0x7c, 0x1b, 0x15, 0x0b, 0x13, 0x21, 0x9f, 0x30, 0x62, 0xf6, 0x37, 0x50, 0xa2,
780 0x57, 0xa4, 0x09, 0x2e, 0xfa, 0x2b, 0xe7, 0xe3, 0xfd, 0xf7, 0xd7, 0x61, 0x30,
781 0x5e, 0x0c, 0xd1, 0x82, 0xb5, 0xc5, 0xc0, 0x09, 0x63, 0x2d, 0x46, 0x5d, 0x13,
782 0x6b, 0xd7, 0x88, 0xb0, 0x80, 0xc2, 0x7a, 0xc0, 0xf9, 0x9a, 0x09, 0x81};
783
784 static uint8_t iqmp[] = {0x8f, 0xcf, 0xd5, 0x74, 0xea, 0x10, 0x05, 0x2b, 0xa3, 0x96, 0x83, 0x0f, 0xae,
785 0x09, 0xbb, 0x06, 0xef, 0x0a, 0x96, 0xc0, 0x2f, 0xc9, 0x24, 0xc0, 0x6c, 0xdf,
786 0xeb, 0x0c, 0xeb, 0x74, 0x5f, 0x9f, 0xd0, 0x25, 0xca, 0x91, 0x31, 0x94, 0x10,
787 0x6e, 0xc1, 0x19, 0x6a, 0x82, 0xaf, 0xdc, 0xbd, 0x9c, 0x1b, 0x7d, 0x2a, 0xec,
788 0x8d, 0xd5, 0x59, 0x4d, 0x6f, 0x38, 0x89, 0xa7, 0xe5, 0x1c, 0x29, 0x57};
789
790 aws_cal_library_test_init(allocator);
791
792 struct aws_byte_buf private_key_buf;
793 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
794 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &private_key_buf));
795
796 struct aws_byte_cursor private_key_cur = aws_byte_cursor_from_buf(&private_key_buf);
797 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, private_key_cur);
798 struct aws_rsa_private_key_pkcs1 private_key_data;
799 AWS_ZERO_STRUCT(private_key_data);
800 ASSERT_SUCCESS(aws_der_decoder_load_private_rsa_pkcs1(decoder, &private_key_data));
801
802 ASSERT_BIN_ARRAYS_EQUALS(n, AWS_ARRAY_SIZE(n), private_key_data.modulus.ptr, private_key_data.modulus.len);
803 ASSERT_BIN_ARRAYS_EQUALS(
804 e, AWS_ARRAY_SIZE(e), private_key_data.publicExponent.ptr, private_key_data.publicExponent.len);
805 ASSERT_BIN_ARRAYS_EQUALS(
806 d, AWS_ARRAY_SIZE(d), private_key_data.privateExponent.ptr, private_key_data.privateExponent.len);
807 ASSERT_BIN_ARRAYS_EQUALS(p, AWS_ARRAY_SIZE(p), private_key_data.prime1.ptr, private_key_data.prime1.len);
808 ASSERT_BIN_ARRAYS_EQUALS(q, AWS_ARRAY_SIZE(q), private_key_data.prime2.ptr, private_key_data.prime2.len);
809 ASSERT_BIN_ARRAYS_EQUALS(
810 dmp1, AWS_ARRAY_SIZE(dmp1), private_key_data.exponent1.ptr, private_key_data.exponent1.len);
811 ASSERT_BIN_ARRAYS_EQUALS(
812 dmq1, AWS_ARRAY_SIZE(dmq1), private_key_data.exponent2.ptr, private_key_data.exponent2.len);
813 ASSERT_BIN_ARRAYS_EQUALS(
814 iqmp, AWS_ARRAY_SIZE(iqmp), private_key_data.coefficient.ptr, private_key_data.coefficient.len);
815
816 aws_byte_buf_clean_up_secure(&private_key_buf);
817 aws_der_decoder_destroy(decoder);
818
819 aws_cal_library_clean_up();
820
821 return AWS_OP_SUCCESS;
822 }
823 AWS_TEST_CASE(rsa_private_pkcs1_der_parsing, s_rsa_private_pkcs1_der_parsing);
824
825 static int s_rsa_public_pkcs1_der_parsing(struct aws_allocator *allocator, void *ctx) {
826 (void)ctx;
827
828 static uint8_t n[] = {0x95, 0x18, 0x6f, 0x5c, 0xea, 0xe0, 0x48, 0xa6, 0xfe, 0x30, 0x40, 0xc0, 0x78, 0x3c, 0x9a,
829 0x64, 0x69, 0x48, 0xc4, 0xa9, 0xae, 0x93, 0x5b, 0x9b, 0xfc, 0x79, 0xb4, 0xad, 0x30, 0x29,
830 0xc8, 0x09, 0x67, 0xe9, 0x52, 0xe6, 0xec, 0x7f, 0xa9, 0xeb, 0xe6, 0x73, 0xf1, 0x3e, 0xc1,
831 0xb6, 0xaf, 0x0f, 0x87, 0x65, 0x22, 0xc9, 0x2c, 0x06, 0x15, 0x61, 0xf5, 0xce, 0x33, 0xd0,
832 0xfe, 0x2b, 0x28, 0x53, 0x76, 0x92, 0x09, 0xef, 0x3e, 0x0d, 0x5f, 0x35, 0xf3, 0x7a, 0x6d,
833 0xb9, 0xc7, 0xa1, 0xff, 0x37, 0xd5, 0xa7, 0x44, 0x68, 0xc1, 0xea, 0xa0, 0x0e, 0x2e, 0x0e,
834 0x52, 0xae, 0x6d, 0xee, 0xa1, 0x77, 0xee, 0x94, 0xde, 0x47, 0x55, 0x75, 0x6d, 0x9d, 0xc4,
835 0xc5, 0x70, 0x92, 0x50, 0xc6, 0x00, 0x4e, 0xa5, 0x3f, 0x2a, 0x5c, 0xbc, 0x9c, 0x9b, 0x99,
836 0x3c, 0xba, 0x19, 0xb4, 0x39, 0x63, 0xfa, 0x73};
837
838 static uint8_t e[] = {0x01, 0x00, 0x01};
839
840 aws_cal_library_test_init(allocator);
841
842 struct aws_byte_buf public_key_buf;
843 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
844 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PUBLIC_KEY_1024), &public_key_buf));
845
846 struct aws_byte_cursor public_key_cur = aws_byte_cursor_from_buf(&public_key_buf);
847 struct aws_der_decoder *decoder = aws_der_decoder_new(allocator, public_key_cur);
848 struct aws_rsa_public_key_pkcs1 public_key_data;
849 AWS_ZERO_STRUCT(public_key_data);
850 ASSERT_SUCCESS(aws_der_decoder_load_public_rsa_pkcs1(decoder, &public_key_data));
851
852 ASSERT_BIN_ARRAYS_EQUALS(n, AWS_ARRAY_SIZE(n), public_key_data.modulus.ptr, public_key_data.modulus.len);
853 ASSERT_BIN_ARRAYS_EQUALS(
854 e, AWS_ARRAY_SIZE(e), public_key_data.publicExponent.ptr, public_key_data.publicExponent.len);
855
856 aws_byte_buf_clean_up_secure(&public_key_buf);
857 aws_der_decoder_destroy(decoder);
858
859 aws_cal_library_clean_up();
860
861 return AWS_OP_SUCCESS;
862 }
863 AWS_TEST_CASE(rsa_public_pkcs1_der_parsing, s_rsa_public_pkcs1_der_parsing);
864
865 static int s_rsa_signing_mismatch_pkcs1_sha256(struct aws_allocator *allocator, void *ctx) {
866 (void)ctx;
867 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
868
869 aws_cal_library_test_init(allocator);
870
871 struct aws_byte_buf public_key_buf;
872 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
873 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &public_key_buf));
874 struct aws_rsa_key_pair *key_pair_private =
875 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
876 ASSERT_NOT_NULL(key_pair_private);
877
878 uint8_t hash[AWS_SHA256_LEN];
879 AWS_ZERO_ARRAY(hash);
880 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
881 aws_sha256_compute(allocator, &message, &hash_value, 0);
882 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
883
884 struct aws_byte_buf signature_buf;
885 ASSERT_SUCCESS(aws_byte_buf_init(&signature_buf, allocator, aws_rsa_key_pair_signature_length(key_pair_private)));
886 ASSERT_SUCCESS(aws_rsa_key_pair_sign_message(
887 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, hash_cur, &signature_buf));
888 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf);
889
890 hash[5] += 59; /* modify digest to force signature mismatch */
891
892 ASSERT_ERROR(
893 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
894 aws_rsa_key_pair_verify_signature(
895 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, hash_cur, signature_cur));
896
897 hash[5] -= 59; /* undo digest modification and corrupt signature */
898 signature_buf.buffer[5] += 59;
899 ASSERT_ERROR(
900 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
901 aws_rsa_key_pair_verify_signature(
902 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, hash_cur, signature_cur));
903
904 struct aws_byte_cursor short_signature_cur = aws_byte_cursor_from_c_str("bad signature");
905 ASSERT_ERROR(
906 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
907 aws_rsa_key_pair_verify_signature(
908 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA256, hash_cur, short_signature_cur));
909
910 aws_byte_buf_clean_up(&hash_value);
911 aws_byte_buf_clean_up(&signature_buf);
912 aws_byte_buf_clean_up(&public_key_buf);
913 aws_rsa_key_pair_release(key_pair_private);
914
915 aws_cal_library_clean_up();
916
917 return AWS_OP_SUCCESS;
918 }
919 AWS_TEST_CASE(rsa_signing_mismatch_pkcs1_sha256, s_rsa_signing_mismatch_pkcs1_sha256);
920
921 static int s_rsa_signing_mismatch_pkcs1_sha1(struct aws_allocator *allocator, void *ctx) {
922 (void)ctx;
923 if (!s_crypto_supports_sha1_signing()) {
924 return AWS_OP_SKIP;
925 }
926
927 struct aws_byte_cursor message = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING);
928
929 aws_cal_library_init(allocator);
930
931 struct aws_byte_buf public_key_buf;
932 ASSERT_SUCCESS(s_byte_buf_decoded_from_base64_cur(
933 allocator, aws_byte_cursor_from_c_str(TEST_PKCS1_RSA_PRIVATE_KEY_1024), &public_key_buf));
934 struct aws_rsa_key_pair *key_pair_private =
935 aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&public_key_buf));
936 ASSERT_NOT_NULL(key_pair_private);
937
938 uint8_t hash[AWS_SHA1_LEN];
939 AWS_ZERO_ARRAY(hash);
940 struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash));
941 aws_sha1_compute(allocator, &message, &hash_value, 0);
942 struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value);
943
944 struct aws_byte_buf signature_buf;
945 ASSERT_SUCCESS(aws_byte_buf_init(&signature_buf, allocator, aws_rsa_key_pair_signature_length(key_pair_private)));
946 ASSERT_SUCCESS(
947 aws_rsa_key_pair_sign_message(key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, hash_cur, &signature_buf));
948 struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf);
949
950 hash[5] += 59; /* modify digest to force signature mismatch */
951
952 ASSERT_ERROR(
953 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
954 aws_rsa_key_pair_verify_signature(
955 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, hash_cur, signature_cur));
956
957 hash[5] -= 59; /* undo digest modification and corrupt signature */
958 signature_buf.buffer[5] += 59;
959 ASSERT_ERROR(
960 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
961 aws_rsa_key_pair_verify_signature(
962 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, hash_cur, signature_cur));
963
964 struct aws_byte_cursor short_signature_cur = aws_byte_cursor_from_c_str("bad signature");
965 ASSERT_ERROR(
966 AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED,
967 aws_rsa_key_pair_verify_signature(
968 key_pair_private, AWS_CAL_RSA_SIGNATURE_PKCS1_5_SHA1, hash_cur, short_signature_cur));
969
970 aws_byte_buf_clean_up(&hash_value);
971 aws_byte_buf_clean_up(&signature_buf);
972 aws_byte_buf_clean_up(&public_key_buf);
973 aws_rsa_key_pair_release(key_pair_private);
974
975 aws_cal_library_clean_up();
976
977 return AWS_OP_SUCCESS;
978 }
979 AWS_TEST_CASE(rsa_signing_mismatch_pkcs1_sha1, s_rsa_signing_mismatch_pkcs1_sha1);