diff options
author | Yicheng Li <yichengli@chromium.org> | 2021-01-04 14:49:19 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-01-12 20:55:51 +0000 |
commit | 72a3dd167c24a20a6df568d0c84c77dd45a6d2cf (patch) | |
tree | 1d1c66467f1ce5e5b0117438c1c104b51f954f5d /test/u2f.c | |
parent | ec643f0d99397eafa09ab39b2d7dd6b968bd70d8 (diff) | |
download | chrome-ec-stabilize-13729.49.B-cr50_stab.tar.gz |
cr50_stab: Add unittests for u2fstabilize-quickfix-13729.84.B-cr50_stabstabilize-quickfix-13729.73.B-cr50_stabstabilize-quickfix-13729.60.B-cr50_stabstabilize-13729.72.B-cr50_stabstabilize-13729.57.B-cr50_stabstabilize-13729.56.B-cr50_stabstabilize-13729.49.B-cr50_stabstabilize-13729.45.B-cr50_stabstabilize-13729.16.B-cr50_stabrelease-R89-13729.B-cr50_stab
The u2f functionality had no unittests at all. This change is more
of a setup (in terms of build dependencies) so that u2f tests can
be easily added in the future. This change comes with a few simple
tests for u2f_generate.
The basic idea here is to use board/host/dcrypto.h to mock the
dcrypto functionalities. Since board/host/dcrypto.h includes an
alternative to cryptoc's sha256 definitions, we need to exclude
cryptoc/sha256.h in the test builds.
BUG=b:172971998
TEST=make -j run-u2f
TEST=make CR50_DEV=1 BOARD=cr50 -j
Signed-off-by: Yicheng Li <yichengli@chromium.org>
Change-Id: Idae6f55f599a017aedcaf0fe4cdb6c0506e72712
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2610133
Reviewed-by: Mary Ruthven <mruthven@chromium.org>
Diffstat (limited to 'test/u2f.c')
-rw-r--r-- | test/u2f.c | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/test/u2f.c b/test/u2f.c new file mode 100644 index 0000000000..3d36ff1830 --- /dev/null +++ b/test/u2f.c @@ -0,0 +1,173 @@ +/* Copyright 2021 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "test_util.h" +#include "u2f_impl.h" + +/******************************************************************************/ +/* Mock implementations of cr50 board. + */ +int system_get_chip_unique_id(uint8_t **id) +{ + return P256_NBYTES; +} + +/******************************************************************************/ +/* Mock implementations of Dcrypto functionality. + */ +int DCRYPTO_ladder_random(void *output) +{ + memset(output, 0, P256_NBYTES); + /* Return 1 for success, 0 for error. */ + return 1; +} + +int DCRYPTO_x509_gen_u2f_cert_name(const p256_int *d, const p256_int *pk_x, + const p256_int *pk_y, const p256_int *serial, + const char *name, uint8_t *cert, + const int n) +{ + /* Return the size of certificate, 0 means error. */ + return 0; +} + +int dcrypto_p256_ecdsa_sign(struct drbg_ctx *drbg, const p256_int *key, + const p256_int *message, p256_int *r, p256_int *s) +{ + memset(r, 0, sizeof(p256_int)); + memset(s, 0, sizeof(p256_int)); + /* Return 1 for success, 0 for error. */ + return 1; +} + +void hmac_drbg_init_rfc6979(struct drbg_ctx *ctx, + const p256_int *key, + const p256_int *message) +{ + memset(ctx, 0, sizeof(struct drbg_ctx)); +} + +void HASH_update(struct HASH_CTX *ctx, const void *data, size_t len) +{ + if (ctx) + SHA256_update(ctx, data, len); +} + +uint8_t *HASH_final(struct HASH_CTX *ctx) +{ + return SHA256_final(ctx); +} + +void DCRYPTO_SHA256_init(LITE_SHA256_CTX *ctx, uint32_t sw_required) +{ + SHA256_init(ctx); +} + +/******************************************************************************/ +/* Mock implementations of U2F functionality. + */ +static int presence; + +enum touch_state pop_check_presence(int consume) +{ + enum touch_state ret = presence ? + POP_TOUCH_YES : POP_TOUCH_NO; + + if (consume) + presence = 0; + return ret; +} + +int u2f_origin_user_keyhandle(const uint8_t *origin, const uint8_t *user, + const uint8_t *seed, + struct u2f_key_handle *key_handle) +{ + return EC_SUCCESS; +} + +int u2f_origin_user_versioned_keyhandle( + const uint8_t *origin, const uint8_t *user, const uint8_t *seed, + uint8_t version, + struct u2f_versioned_key_handle_header *key_handle_header) +{ + return EC_SUCCESS; +} + +int u2f_origin_user_keypair(const uint8_t *key_handle, size_t key_handle_size, + p256_int *d, p256_int *pk_x, p256_int *pk_y) +{ + return EC_SUCCESS; +} + +int g2f_individual_keypair(p256_int *d, p256_int *pk_x, p256_int *pk_y) +{ + return EC_SUCCESS; +} + +int u2f_authorization_hmac(const uint8_t *authorization_salt, + const struct u2f_versioned_key_handle_header *header, + const uint8_t *auth_time_secret_hash, uint8_t *hmac) +{ + return EC_SUCCESS; +} + +/******************************************************************************/ +/* Tests begin here. + */ +static uint8_t buffer[512]; + +test_static int test_u2f_generate_no_require_presence(void) +{ + struct u2f_generate_req *req = (struct u2f_generate_req *)buffer; + size_t response_size = sizeof(struct u2f_generate_resp); + int ret; + + memset(buffer, 0, sizeof(buffer)); + req->flags = 0; + presence = 0; + ret = u2f_generate( + VENDOR_CC_U2F_GENERATE, &buffer, + sizeof(struct u2f_generate_req), + &response_size); + + TEST_ASSERT(ret == VENDOR_RC_SUCCESS); + return EC_SUCCESS; +} + +test_static int test_u2f_generate_require_presence(void) +{ + struct u2f_generate_req *req = (struct u2f_generate_req *)buffer; + size_t response_size = sizeof(struct u2f_generate_resp); + int ret; + + memset(buffer, 0, sizeof(buffer)); + req->flags = U2F_AUTH_FLAG_TUP; + presence = 0; + ret = u2f_generate( + VENDOR_CC_U2F_GENERATE, &buffer, + sizeof(struct u2f_generate_req), + &response_size); + TEST_ASSERT(ret == VENDOR_RC_NOT_ALLOWED); + + memset(buffer, 0, sizeof(buffer)); + req->flags = U2F_AUTH_FLAG_TUP; + response_size = sizeof(struct u2f_generate_resp); + presence = 1; + ret = u2f_generate( + VENDOR_CC_U2F_GENERATE, &buffer, + sizeof(struct u2f_generate_req), + &response_size); + TEST_ASSERT(ret == VENDOR_RC_SUCCESS); + + return EC_SUCCESS; +} + +void run_test(void) +{ + RUN_TEST(test_u2f_generate_no_require_presence); + RUN_TEST(test_u2f_generate_require_presence); + + test_print_result(); +} |