Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion SE050/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,29 @@ Example wolfSSL installation:

```
./configure --with-se050=/home/pi/se_mw/simw-top/ --enable-keygen --enable-cryptocb \
--disable-examples --disable-crypttests
--disable-examples --disable-crypttests \
CFLAGS="-DSE050_KEYID_START=0x0000FFFF"
make
sudo make install
```

`SE050_KEYID_START` is needed by the examples that store their own keys and
certificates in the SE050 (`wolfcrypt_key_cert_insert` and
`wolfssl_client_cert_key`). wolfSSL gives the keys it generates inside the
SE050 key IDs counting up from `SE050_KEYID_START`, and keeps that range for
itself: the functions that insert a key or certificate at a key ID chosen by
the application (`wc_se050_rsa_insert_private_key()`,
`wc_se050_ecc_insert_private_key()`, `wc_se050_insert_binary_object()` and
the matching public key functions) return `BAD_FUNC_ARG` (-173) for a key ID
at or above it. `SE050_KEYID_START` defaults to 100, and these examples use
key IDs `0x5002` to `0x5007`, so with the default every insert fails. Setting
it to `0x0000FFFF`, above the IDs the examples use, also gives the key IDs
shown in the example output in these READMEs.

The value is compiled into the wolfSSL library, so it has to be set when
building wolfSSL, either in `CFLAGS` as above or in `user_settings.h`. Defining
it only when building the examples has no effect.

Before building these demos, the "wolfssl" folder which contains these demos
should be copied into the SE05x middleware source tree at: `~/se_mw/simw-top/demos/wolfssl`.
Example: `cp -r wolfssl ~/se_mw/simw-top/demos/`
Expand Down
50 changes: 39 additions & 11 deletions SE050/wolfssl/wolfcrypt_generate_csr/wolfcrypt_generate_csr.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
{
#if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
int ret = 0;
int tmpRet = 0;
int rngInit = 0;
int eccInit = 0;
int keyStored = 0;
sss_status_t status = kStatus_SSS_Success;
sss_session_t *pSession = (sss_session_t*)&pCtx->session;
sss_key_store_t *pKeyStore = (sss_key_store_t*)&pCtx->ks;
Expand Down Expand Up @@ -87,6 +91,8 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
ret = wc_InitRng(&rng);
if (ret != 0) {
LOG_I("wc_RngInit() error, ret = %d\n", ret);
} else {
rngInit = 1;
}

/* Generate ECC key, stored in SE050 */
Expand All @@ -95,6 +101,8 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
ret = wc_ecc_init(&ecc);
if (ret != 0) {
LOG_I("wc_ecc_init error, ret = %d\n", ret);
} else {
eccInit = 1;
}
}

Expand All @@ -108,6 +116,7 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
LOG_I("wc_ecc_get_key_id error, ret = %d\n", ret);
} else {
LOG_I("Generated ECC key in SE050 ID: 0x%08x\n", keyId);
keyStored = 1;
}
}
}
Expand All @@ -128,6 +137,9 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
strncpy(req.subject.commonName, "www.example.com", CTC_NAME_SIZE);
strncpy(req.subject.email, "info@example.com", CTC_NAME_SIZE);
req.version = 0;
/* wc_InitCert() defaults to an RSA signature type, the key here is
* ECC. wolfSSL rejects a signature type that does not match the key */
req.sigType = CTC_SHA256wECDSA;

ret = wc_MakeCertReq_ex(&req, der, sizeof(der), ECC_TYPE,
(void*)&ecc);
Expand Down Expand Up @@ -163,33 +175,49 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
}
}

/* Cleanup below runs for everything that was set up, even after an
* earlier failure. A cleanup error is only kept in ret if nothing failed
* before it, so the first error is the one reported. */

/* Delete generated key in SE050 */
if (ret == 0) {
if (keyStored) {
LOG_I("Erasing ECC key stored in SE050 slot: 0x%08x\n", keyId);
ret = wc_se050_erase_object(keyId);
if (ret != 0) {
tmpRet = wc_se050_erase_object(keyId);
if (tmpRet != 0) {
LOG_I("Failed to erase ECC key in SE050\n");
if (ret == 0) {
ret = tmpRet;
}
}
}

/* Free ECC key and RNG */
if (ret == 0) {
ret = wc_ecc_free(&ecc);
if (ret != 0) {
LOG_I("wc_ecc_free error, ret = %d\n", ret);
if (eccInit) {
tmpRet = wc_ecc_free(&ecc);
if (tmpRet != 0) {
LOG_I("wc_ecc_free error, ret = %d\n", tmpRet);
if (ret == 0) {
ret = tmpRet;
}
}
}
if (ret == 0) {
ret = wc_FreeRng(&rng);
if (ret != 0) {
LOG_I("wc_FreeRng error, ret = %d\n", ret);
if (rngInit) {
tmpRet = wc_FreeRng(&rng);
if (tmpRet != 0) {
LOG_I("wc_FreeRng error, ret = %d\n", tmpRet);
if (ret == 0) {
ret = tmpRet;
}
}
}

/* Cleanup wolfSSL library resources */
wolfSSL_Cleanup();

if (ret != 0) {
status = kStatus_SSS_Fail;
}

LOG_I("Done with sample app");
return status;
#else
Expand Down
12 changes: 12 additions & 0 deletions SE050/wolfssl/wolfcrypt_key_cert_insert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ original inserted.
Before building this demo, follow initial setup instructions in the parent
[README.md](../../README.md).

The wolfSSL library linked against needs to be built with `SE050_KEYID_START`
set above the key IDs this demo uses (`0x5002` to `0x5007`), for example by
adding `-DSE050_KEYID_START=0x0000FFFF` to `CFLAGS` when running wolfSSL's
`./configure`. With the default of 100 wolfSSL refuses to insert a key or
certificate at these key IDs, and the demo fails at the first insert with:

```
App :INFO :Error in se050_rsa_insert_private_key, ret = -173
```

See the parent [README.md](../../README.md) for details.

Once this example directory has been copied into the SE05x middleware directory
tree in the correct location, compiling the middleware will also compile this
demo application:
Expand Down
113 changes: 82 additions & 31 deletions SE050/wolfssl/wolfcrypt_key_cert_insert/wolfcrypt_key_cert_insert.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static int se050_verify_ecc_sig_rs(const char* sigR, const char* sigS,
{
int ret = 0;
int res = 0;
int eccInit = 0;
int freeRet = 0;
byte eccSig[ECC_MAX_SIG_SIZE];
word32 eccSigSz;
ecc_key eccKey;
Expand All @@ -105,6 +107,8 @@ static int se050_verify_ecc_sig_rs(const char* sigR, const char* sigS,
ret = wc_ecc_init(&eccKey);
if (ret != 0) {
LOG_I("wc_ecc_init error, ret = %d\n", ret);
} else {
eccInit = 1;
}
}

Expand All @@ -127,17 +131,22 @@ static int se050_verify_ecc_sig_rs(const char* sigR, const char* sigS,

if (ret == 0) {
if (res != 1) {
/* The verify operation ran, but the signature is not valid */
LOG_I("ECDSA verify failed!\n");
ret = SIG_VERIFY_E;
} else {
LOG_I("ECDSA verify success!\n");
}
}

/* Free ECC key and RNG */
if (ret == 0) {
ret = wc_ecc_free(&eccKey);
if (ret != 0) {
LOG_I("wc_ecc_free error, ret = %d\n", ret);
/* Free ECC key if it was initialized, keeping any earlier error */
if (eccInit) {
freeRet = wc_ecc_free(&eccKey);
if (freeRet != 0) {
LOG_I("wc_ecc_free error, ret = %d\n", freeRet);
if (ret == 0) {
ret = freeRet;
}
}
}

Expand Down Expand Up @@ -179,14 +188,16 @@ static int write_certs_keys_to_se050(void)
}

/* Insert RSA certificate */
ret = wc_se050_insert_binary_object(KEYID_RSA_CERT,
server_cert_der_2048,
sizeof_server_cert_der_2048);
if (ret != 0) {
LOG_I("Error in wc_se050_insert_binary_object, ret = %d\n", ret);
} else {
LOG_I("Inserted RSA certificate into SE050 key ID: 0x%08x\n",
KEYID_RSA_CERT);
if (ret == 0) {
ret = wc_se050_insert_binary_object(KEYID_RSA_CERT,
server_cert_der_2048,
sizeof_server_cert_der_2048);
if (ret != 0) {
LOG_I("Error in wc_se050_insert_binary_object, ret = %d\n", ret);
} else {
LOG_I("Inserted RSA certificate into SE050 key ID: 0x%08x\n",
KEYID_RSA_CERT);
}
}

/* Extract RSA public key from cert DER */
Expand Down Expand Up @@ -247,23 +258,28 @@ static int write_certs_keys_to_se050(void)
}

/* Insert ECC private key */
ret = wc_se050_ecc_insert_private_key(KEYID_ECC_PRIVATE, ecc_key_der_256,
sizeof_ecc_key_der_256);
if (ret != 0) {
LOG_I("Error in se050_ecc_insert_private_key, ret = %d\n", ret);
} else {
LOG_I("Inserted ECC private key into SE050 key ID: 0x%08x\n",
KEYID_ECC_PRIVATE);
if (ret == 0) {
ret = wc_se050_ecc_insert_private_key(KEYID_ECC_PRIVATE,
ecc_key_der_256,
sizeof_ecc_key_der_256);
if (ret != 0) {
LOG_I("Error in se050_ecc_insert_private_key, ret = %d\n", ret);
} else {
LOG_I("Inserted ECC private key into SE050 key ID: 0x%08x\n",
KEYID_ECC_PRIVATE);
}
}

/* Insert ECC certificate */
ret = wc_se050_insert_binary_object(KEYID_ECC_CERT, serv_ecc_der_256,
sizeof_serv_ecc_der_256);
if (ret != 0) {
LOG_I("Error in wc_se050_insert_binary_object, ret = %d\n", ret);
} else {
LOG_I("Inserted ECC certificate into SE050 key ID: 0x%08x\n",
KEYID_ECC_CERT);
if (ret == 0) {
ret = wc_se050_insert_binary_object(KEYID_ECC_CERT, serv_ecc_der_256,
sizeof_serv_ecc_der_256);
if (ret != 0) {
LOG_I("Error in wc_se050_insert_binary_object, ret = %d\n", ret);
} else {
LOG_I("Inserted ECC certificate into SE050 key ID: 0x%08x\n",
KEYID_ECC_CERT);
}
}

/* Extract ECC public key from cert DER */
Expand Down Expand Up @@ -406,8 +422,13 @@ static int read_certs_from_se050(void)
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
}

/* Any failure above is an error, including a cert that was read back
* successfully but differs from the original in size or content. ret
* cannot be used directly, it still holds the last read result. */
if (err == 0) {
ret = 0;
} else {
ret = -1;
}

return ret;
Expand All @@ -421,50 +442,71 @@ static int read_certs_from_se050(void)
static int erase_certs_keys_from_se050(void)
{
int ret = 0;
int firstErr = 0;

ret = wc_se050_erase_object(KEYID_RSA_PRIVATE);
if (ret != 0) {
LOG_I("Failed to erase RSA private key, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased RSA private key, key ID: 0x%08x\n", KEYID_RSA_PRIVATE);
}
ret = wc_se050_erase_object(KEYID_RSA_CERT);
if (ret != 0) {
LOG_I("Failed to erase RSA certificate, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased RSA certificate, key ID: 0x%08x\n", KEYID_RSA_CERT);
}
ret = wc_se050_erase_object(KEYID_RSA_PUBLIC);
if (ret != 0) {
LOG_I("Failed to erase RSA public key, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased RSA public key, key ID: 0x%08x\n", KEYID_RSA_PUBLIC);
}
ret = wc_se050_erase_object(KEYID_ECC_PRIVATE);
if (ret != 0) {
LOG_I("Failed to erase ECC private key, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased ECC private key, key ID: 0x%08x\n", KEYID_ECC_PRIVATE);
}
ret = wc_se050_erase_object(KEYID_ECC_CERT);
if (ret != 0) {
LOG_I("Failed to erase ECC certificate, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased ECC certificate, key ID: 0x%08x\n", KEYID_ECC_CERT);
}
ret = wc_se050_erase_object(KEYID_ECC_PUBLIC);
if (ret != 0) {
LOG_I("Failed to erase ECC public key, ret = %d\n", ret);
if (firstErr == 0) {
firstErr = ret;
}
} else {
LOG_I("Erased ECC public key, key ID: 0x%08x\n", KEYID_ECC_PUBLIC);
}

return ret;
/* Every object is attempted, the first failure is the one returned */
return firstErr;
}

sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
{
int ret = 0;
int eraseRet = 0;
sss_status_t status = kStatus_SSS_Success;
sss_session_t *pSession = (sss_session_t*)&pCtx->session;
sss_key_store_t *pKeyStore = (sss_key_store_t*)&pCtx->ks;
Expand Down Expand Up @@ -516,15 +558,24 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
ret = read_certs_from_se050();
}

/* Erase sample keys/certs from SE050 */
ret = erase_certs_keys_from_se050();
if (ret != 0) {
/* Erase sample keys/certs from SE050. This is always attempted, so that
* a failed run does not leave samples behind, and its result must not
* replace an earlier failure held in ret. */
eraseRet = erase_certs_keys_from_se050();
if (eraseRet != 0) {
LOG_E("Failed to erase sample certs/keys from SE050\n");
if (ret == 0) {
ret = eraseRet;
}
}

/* Cleanup wolfSSL library resources */
wolfSSL_Cleanup();

if (ret != 0) {
status = kStatus_SSS_Fail;
}

LOG_I("Done with sample app");
return status;
}
Expand Down
4 changes: 3 additions & 1 deletion SE050/wolfssl/wolfssl_client/wolfssl_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ sss_status_t ex_sss_entry(ex_sss_boot_ctx_t *pCtx)
wolfSSL_Cleanup();
close(sockfd);

if (ret == WOLFSSL_FAILURE) {
/* Anything other than WOLFSSL_SUCCESS is a failure, wolfSSL_connect()
* for one returns WOLFSSL_FATAL_ERROR and not WOLFSSL_FAILURE. */
if (ret != WOLFSSL_SUCCESS) {
status = kStatus_SSS_Fail;
}

Expand Down
Loading
Loading