Peter Huewe | 9f7cc08 | 2019-05-02 18:16:28 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
Juergen Repp | 52ceb8c | 2018-08-22 18:30:12 +0200 | [diff] [blame] | 2 | /******************************************************************************* |
| 3 | * Copyright 2018, Fraunhofer SIT sponsored by Infineon Technologies AG |
| 4 | * All rights reserved. |
| 5 | ******************************************************************************/ |
| 6 | |
| 7 | #include <stdarg.h> |
| 8 | #include <inttypes.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #include <setjmp.h> |
| 13 | #include <cmocka.h> |
| 14 | |
| 15 | #include "tss2_esys.h" |
| 16 | #include "esys_crypto.h" |
| 17 | |
| 18 | #define LOGMODULE tests |
| 19 | #include "util/log.h" |
| 20 | |
| 21 | /** |
| 22 | * This unit tst checks several error cases of the crypto backends, which are not |
| 23 | * covered by the integration tests. |
| 24 | */ |
| 25 | |
| 26 | static void |
| 27 | check_hash_functions(void **state) |
| 28 | { |
| 29 | TSS2_RC rc; |
| 30 | IESYS_CRYPTO_CONTEXT_BLOB *context; |
| 31 | uint8_t buffer[10] = { 0 }; |
| 32 | TPM2B tpm2b; |
| 33 | size_t size = 0; |
| 34 | |
| 35 | rc = iesys_crypto_hash_start(NULL, TPM2_ALG_SHA384); |
| 36 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 37 | |
| 38 | #ifndef OSSL |
| 39 | rc = iesys_crypto_hash_start(&context, TPM2_ALG_SHA512); |
| 40 | assert_int_equal (rc, TSS2_ESYS_RC_NOT_IMPLEMENTED); |
| 41 | #endif |
| 42 | |
| 43 | rc = iesys_crypto_hash_start(&context, 0); |
| 44 | assert_int_equal (rc, TSS2_ESYS_RC_NOT_IMPLEMENTED); |
| 45 | |
| 46 | rc = iesys_crypto_hash_start(&context, TPM2_ALG_SHA384); |
| 47 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 48 | |
| 49 | rc = iesys_crypto_hash_finish(NULL, &buffer[0], &size); |
| 50 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 51 | |
| 52 | rc = iesys_crypto_hash_finish(&context, &buffer[0], &size); |
| 53 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_SIZE); |
| 54 | |
| 55 | iesys_crypto_hash_abort(NULL); |
| 56 | iesys_crypto_hash_abort(&context); |
| 57 | |
| 58 | rc = iesys_crypto_hash_update(NULL, &buffer[0], 10); |
| 59 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 60 | |
| 61 | rc = iesys_crypto_hash_update2b(NULL, &tpm2b); |
| 62 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 63 | |
| 64 | /* Create invalid context */ |
| 65 | rc = iesys_crypto_hmac_start(&context, TPM2_ALG_SHA1, &buffer[0], 10); |
| 66 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 67 | |
| 68 | iesys_crypto_hash_abort(&context); |
| 69 | |
| 70 | rc = iesys_crypto_hash_update(context, &buffer[0], 10); |
| 71 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 72 | |
| 73 | rc = iesys_crypto_hash_finish(&context, &buffer[0], &size); |
| 74 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
Peter Huewe | ce73eef | 2019-02-20 00:00:14 +0100 | [diff] [blame] | 75 | |
| 76 | /* cleanup */ |
| 77 | iesys_crypto_hmac_abort(&context); |
Juergen Repp | 52ceb8c | 2018-08-22 18:30:12 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void |
| 81 | check_hmac_functions(void **state) |
| 82 | { |
| 83 | TSS2_RC rc; |
| 84 | IESYS_CRYPTO_CONTEXT_BLOB *context; |
| 85 | uint8_t buffer[10] = { 0 }; |
| 86 | TPM2B tpm2b; |
| 87 | size_t size = 0; |
| 88 | |
| 89 | rc = iesys_crypto_hmac_start(NULL, TPM2_ALG_SHA384, &buffer[0], 10); |
| 90 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 91 | |
| 92 | #ifndef OSSL |
| 93 | rc = iesys_crypto_hmac_start(&context, TPM2_ALG_SHA512, &buffer[0], 10); |
| 94 | assert_int_equal (rc, TSS2_ESYS_RC_NOT_IMPLEMENTED); |
| 95 | #endif |
| 96 | |
| 97 | rc = iesys_crypto_hmac_start(&context, 0, &buffer[0], 10); |
| 98 | assert_int_equal (rc, TSS2_ESYS_RC_NOT_IMPLEMENTED); |
| 99 | |
| 100 | rc = iesys_crypto_hmac_start(&context, TPM2_ALG_SHA1, &buffer[0], 10); |
| 101 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 102 | |
| 103 | rc = iesys_crypto_hmac_finish(NULL, &buffer[0], &size); |
| 104 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 105 | |
| 106 | rc = iesys_crypto_hmac_finish2b(NULL, &tpm2b); |
| 107 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 108 | |
| 109 | rc = iesys_crypto_hmac_finish(&context, &buffer[0], &size); |
| 110 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_SIZE); |
| 111 | |
| 112 | iesys_crypto_hmac_abort(NULL); |
| 113 | iesys_crypto_hmac_abort(&context); |
| 114 | |
| 115 | rc = iesys_crypto_hmac_update(NULL, &buffer[0], 10); |
| 116 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 117 | |
| 118 | rc = iesys_crypto_hmac_update2b(NULL, &tpm2b); |
| 119 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 120 | |
| 121 | /* Create invalid context */ |
| 122 | rc = iesys_crypto_hash_start(&context, TPM2_ALG_SHA1); |
| 123 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 124 | |
| 125 | iesys_crypto_hmac_abort(&context); |
| 126 | |
| 127 | rc = iesys_crypto_hmac_update(context, &buffer[0], 10); |
| 128 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 129 | |
| 130 | rc = iesys_crypto_hmac_finish(&context, &buffer[0], &size); |
| 131 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
Peter Huewe | ce73eef | 2019-02-20 00:00:14 +0100 | [diff] [blame] | 132 | |
| 133 | /* cleanup */ |
| 134 | iesys_crypto_hash_abort(&context); |
Juergen Repp | 52ceb8c | 2018-08-22 18:30:12 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
| 138 | check_random(void **state) |
| 139 | { |
| 140 | TSS2_RC rc; |
| 141 | size_t num_bytes = 0; |
| 142 | TPM2B_NONCE nonce; |
| 143 | rc = iesys_crypto_random2b(&nonce, num_bytes); |
| 144 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 145 | } |
| 146 | |
| 147 | static void |
| 148 | check_pk_encrypt(void **state) |
| 149 | { |
| 150 | TSS2_RC rc; |
| 151 | uint8_t in_buffer[5] = { 1, 2, 3, 4, 5 }; |
| 152 | size_t size = 5; |
| 153 | uint8_t out_buffer[5]; |
| 154 | TPM2B_PUBLIC inPublicRSA = { |
| 155 | .size = 0, |
| 156 | .publicArea = { |
| 157 | .type = TPM2_ALG_RSA, |
| 158 | .nameAlg = TPM2_ALG_SHA1, |
| 159 | .objectAttributes = (TPMA_OBJECT_USERWITHAUTH | |
| 160 | TPMA_OBJECT_RESTRICTED | |
| 161 | TPMA_OBJECT_DECRYPT | |
| 162 | TPMA_OBJECT_FIXEDTPM | |
| 163 | TPMA_OBJECT_FIXEDPARENT | |
| 164 | TPMA_OBJECT_SENSITIVEDATAORIGIN), |
| 165 | .authPolicy = { |
| 166 | .size = 0, |
| 167 | }, |
| 168 | .parameters.rsaDetail = { |
| 169 | .symmetric = { |
| 170 | .algorithm = TPM2_ALG_AES, |
| 171 | .keyBits.aes = 128, |
| 172 | .mode.aes = TPM2_ALG_CFB, |
| 173 | }, |
| 174 | .scheme = { |
| 175 | .scheme = |
| 176 | TPM2_ALG_NULL, |
| 177 | }, |
| 178 | .keyBits = 2048, |
| 179 | .exponent = 0, |
| 180 | }, |
| 181 | .unique.rsa = { |
| 182 | .size = 0, |
| 183 | .buffer = {} |
| 184 | , |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | inPublicRSA.publicArea.nameAlg = 0; |
| 190 | rc = iesys_crypto_pk_encrypt(&inPublicRSA, size, &in_buffer[0], size, &out_buffer[0], &size, "LABEL"); |
| 191 | assert_int_equal (rc, TSS2_ESYS_RC_NOT_IMPLEMENTED); |
| 192 | |
| 193 | inPublicRSA.publicArea.nameAlg = TPM2_ALG_SHA1; |
| 194 | inPublicRSA.publicArea.parameters.rsaDetail.scheme.scheme = 0; |
| 195 | rc = iesys_crypto_pk_encrypt(&inPublicRSA, size, &in_buffer[0], size, &out_buffer[0], &size, "LABEL"); |
| 196 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_VALUE); |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | check_aes_encrypt(void **state) |
| 201 | { |
| 202 | TSS2_RC rc; |
| 203 | uint8_t key[32] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
| 204 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; |
| 205 | uint8_t buffer[5] = { 1, 2, 3, 4, 5 }; |
| 206 | size_t size = 5; |
| 207 | |
| 208 | rc = iesys_crypto_sym_aes_encrypt(NULL, TPM2_ALG_AES, 192, TPM2_ALG_CFB, 16, |
| 209 | &buffer[0], size, &key[0]); |
| 210 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 211 | |
| 212 | rc = iesys_crypto_sym_aes_encrypt(&key[0], TPM2_ALG_AES, 192, TPM2_ALG_CFB, 16, |
| 213 | &buffer[0], size, &key[0]); |
| 214 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 215 | rc = iesys_crypto_sym_aes_encrypt(&key[0], TPM2_ALG_AES, 256, TPM2_ALG_CFB, 16, |
| 216 | &buffer[0], size, &key[0]); |
| 217 | assert_int_equal (rc, TSS2_RC_SUCCESS); |
| 218 | |
| 219 | rc = iesys_crypto_sym_aes_encrypt(&key[0], 0, 256, TPM2_ALG_CFB, 16, |
| 220 | &buffer[0], size, &key[0]); |
| 221 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_VALUE); |
| 222 | |
| 223 | rc = iesys_crypto_sym_aes_encrypt(&key[0], TPM2_ALG_AES, 256, 0, 16, |
| 224 | &buffer[0], size, &key[0]); |
| 225 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_VALUE); |
| 226 | |
| 227 | rc = iesys_crypto_sym_aes_encrypt(&key[0], TPM2_ALG_AES, 999, TPM2_ALG_CFB, 16, |
| 228 | &buffer[0], size, &key[0]); |
| 229 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_VALUE); |
| 230 | |
| 231 | rc = iesys_crypto_sym_aes_decrypt(NULL, TPM2_ALG_AES, 192, TPM2_ALG_CFB, 16, |
| 232 | &buffer[0], size, &key[0]); |
| 233 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_REFERENCE); |
| 234 | |
| 235 | rc = iesys_crypto_sym_aes_decrypt(&key[0], 0, 192, TPM2_ALG_CFB, 16, |
| 236 | &buffer[0], size, &key[0]); |
| 237 | assert_int_equal (rc, TSS2_ESYS_RC_BAD_VALUE); |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | check_free(void **state) |
| 242 | { |
| 243 | uint8_t *buffer; |
| 244 | |
| 245 | buffer = malloc(10); |
Tadeusz Struk | 795ea13 | 2018-09-14 08:54:15 -0700 | [diff] [blame] | 246 | Esys_Free(buffer); |
Juergen Repp | 52ceb8c | 2018-08-22 18:30:12 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | int |
| 252 | main(int argc, char *argv[]) |
| 253 | { |
| 254 | const struct CMUnitTest tests[] = { |
| 255 | cmocka_unit_test(check_hash_functions), |
| 256 | cmocka_unit_test(check_hmac_functions), |
| 257 | cmocka_unit_test(check_random), |
| 258 | cmocka_unit_test(check_pk_encrypt), |
| 259 | cmocka_unit_test(check_aes_encrypt), |
| 260 | cmocka_unit_test(check_free), |
| 261 | }; |
| 262 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 263 | } |