Skip to content

Develop support for Atmel crypto engine ATCAECC508A #6104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a57f543
Develop support for Atmel crypto engine ATCAECC508A
Jan 26, 2018
feb08db
Add function comments and restructure device factory class
Feb 17, 2018
6e39a6c
Fixups after code restructure
Feb 17, 2018
d6c2b9c
Add missing ATCAFactory destructor
Feb 17, 2018
9a46c6d
Rename ATCA.h/c to ATCAFactory.h/c
Feb 17, 2018
3004483
Method for creating transparent pk context from HW engine
Feb 18, 2018
c3e9538
Fetch ECP PK info via API
Feb 18, 2018
0a9066f
Fix README extension
Feb 19, 2018
a413ad8
First draft of README
mazimkhan Feb 19, 2018
5783fa7
Add steps to generate server certificate.
mazimkhan Feb 19, 2018
b1329cf
Complete README with SSL sample setup.
mazimkhan Feb 19, 2018
58a72c3
Minor fixes and rework
Feb 20, 2018
9e16c31
Change ATCA_ERR_NO_ERROR to ATCA_SUCCESS
Feb 20, 2018
b69e999
Incorporate code review comments
Feb 21, 2018
4fed598
Calculate configuration zone CRC with device specific data.
Feb 21, 2018
deea022
Changes after basing with mbedtls:feature-opaque-keys branch
Feb 22, 2018
a3e9ac8
Change ATCA_ERR_SMALL_BUFFER to ATCA_ERR_BUFFER_TOO_SMALL
Feb 22, 2018
34185c3
Correct names where appropriate 'asn format'->'EC octet string'
Feb 22, 2018
123061b
Initialize pk context with API and return library error codes
Feb 22, 2018
81ce174
Improve comment explaining first 16 bytes of the sample configuration
Feb 22, 2018
2c1f903
Merge branch 'feature-opaque-keys' of github.com:ARMmbed/mbed-os into…
Feb 25, 2018
04369ac
Disable ATCAECC508A commission app in mbed_lib.json
Feb 25, 2018
54d1c9c
Add check for I2C dependency
Feb 25, 2018
7052a84
Add I2C pin names for NUMAKER_PFM_NUC472
Feb 25, 2018
02aaa63
Add check for ethernet dependency
Feb 25, 2018
e52971a
Add I2C pin names for NRF 52840 and 52832
Feb 25, 2018
6510453
Add I2C pin names for Maxim targets
Feb 25, 2018
4668930
Enable feature only for K64F
Feb 25, 2018
f4efd98
Check if mbedtls is enabled
Feb 26, 2018
100399f
Fix doxygen comments
Feb 26, 2018
d55a3b1
Guard I2C use under DEVICE_I2C
Feb 26, 2018
e4c8c48
Conditionally delete I2C pointer
Feb 26, 2018
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
169 changes: 169 additions & 0 deletions features/atcryptoauth/ATCA.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ATCA.h"

ATCA * ATCA::instance = NULL;

uint8_t ATCA::sample_config[] = {
0x01, 0x23, 0x95, 0x11,
0x00, 0x00, 0x50, 0x00,
0x5D, 0xCF, 0xD6, 0xCD,
0xEE, 0xC0, 0x1D, 0x00,
0xC0, 0x00, 0x55, 0x00,
0x83, 0x20, 0x83, 0x20,
0x83, 0x20, 0x83, 0x20,
0x83, 0x20, 0x83, 0x20,
0x83, 0x20, 0x83, 0x20,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xAF, 0x8F,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x55, 0x55,
0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x13, 0x00, 0x13, 0x00,
0x13, 0x00, 0x13, 0x00,
0x13, 0x00, 0x13, 0x00,
0x13, 0x00, 0x13, 0x00,
0x1C, 0x00, 0x10, 0x00,
0x10, 0x00, 0x10, 0x00,
0x10, 0x00, 0x10, 0x00,
0x10, 0x00, 0x1C, 0x00
};

ATCA::ATCA()
:plt(I2C_SDA, I2C_SCL,
ATCA_ECC_I2C_FREQUENCY,
ATCA_ECC_508A_I2C_ADDR),
device(plt)
{
}

ATCA * ATCA::GetInstance()
{
if ( instance == NULL )
{
instance = new ATCA();
if ( instance != NULL )
instance->device.Init();
}
return instance;
}

void ATCA::Deinit()
{
if ( instance != NULL )
delete instance;
}

ATCAKey * ATCA::GetKeyToken(ATCAKeyID keyId, ATCAError & err)
{
size_t pk_len = 0;
uint8_t pk[ATCA_ECC_ECC_PK_LEN];
err = device.GenPubKey(keyId, pk, sizeof(pk), &pk_len);
if (err != ATCA_ERR_NO_ERROR )
{
return NULL;
}
if ( pk_len != ATCA_ECC_ECC_PK_LEN )
{
err = ATCA_ERR_DEVICE_ERROR;
return NULL;
}

ATCAKey * key = new ATCAKey( device, keyId, pk );
if ( key == NULL )
err = ATCA_ERR_MEM_ALLOC_FAILURE;
return key;
}

/**
* ATCA device needs one time (irreversible) locking of the configuration zone.
* This function sets up ECC private and public key slots with desired configuration. TBD explain configuration.
* For production users should come up with their own configuration that is relevant to their application.
* The configuration is only usable after locking it. This function does not lock the config. Use lock_config with
* caution.
*/
ATCAError ATCA::Commission( )
{
// check if the device is locked?
bool locked;
ATCAError err = device.CheckConfigLock( &locked );
if (err != ATCA_ERR_NO_ERROR)
{
return ( err );
}
if ( locked )
{
return( ATCA_ERR_CONFIG_LOCKED );
}
// Configure slots 0-7 as ECC Private keys
for (uint8_t i = 0; i < 8; i++)
{
err = device.ConfigPrivKey(i);
if (err != ATCA_ERR_NO_ERROR)
return( err );
}
// Configure slot 9-14 as ECC Public keys
for (uint8_t i = 9; i <= 14; i++)
{
err = device.ConfigPubKey(i);
if (err != ATCA_ERR_NO_ERROR)
return( err );
}
// Setup slot 8 for certificate storage.
err = device.ConfigCertStore();
if (err != ATCA_ERR_NO_ERROR)
return( err );

return( ATCA_ERR_NO_ERROR );
}

ATCAError ATCA::LockConfig()
{
return device.LockCommand(sample_config, sizeof(sample_config));
}

/**
* Dump device configuration zone data.
*/
void ATCA::DumpDeviceConfig()
{
device.DumpConfig();
}



/**
* Commission ATCA device for testing & demonstration with Mbed OS.
*/
ATCAError ATCA::GenPrivKey(ATCAKeyID keyIdx)
{
// Create a Private ECC Key on Key Index
uint8_t pk_temp[ATCA_ECC_ECC_PK_LEN];
size_t len = 0;
return device.GenPrivateKey(0, pk_temp, sizeof(pk_temp), &len);
}

50 changes: 50 additions & 0 deletions features/atcryptoauth/ATCA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* mbed Microcontroller Library
* Copyright (c) 2018 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef ATCA_H
#define ATCA_H


#include "ATCADevice.h"
#include "ATCAKey.h"
#include "MbedPlatform.h"

// Integrates ATCA with mbed-os and manages SE device instance
class ATCA
{
private:
MbedPlatform plt;
ATCADevice device;
static ATCA * instance;
static uint8_t sample_config[];

ATCA();
public:
~ATCA();

static ATCA * GetInstance();
static void Deinit();
ATCADevice * GetDevice(){ return &device; }
ATCAKey * GetKeyToken(ATCAKeyID keyId, ATCAError & err_out );
ATCAError Commission();
ATCAError LockConfig();
void DumpDeviceConfig();
ATCAError GenPrivKey(ATCAKeyID keyIdx);
};


#endif /* ATCA_H */

97 changes: 97 additions & 0 deletions features/atcryptoauth/ATCAConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* mbed Microcontroller Library
* Copyright (c) 2017 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ATCAConfig.h"

ATCAError SlotConfig::EnableGenKey()
{
if (!IsPrivKey())
return ATCA_ERR_SLOT_NOT_PRIV_KEY;
_register = (_register & ~SLOT_CONFIG_WRITE_CONFIG_MASK) | SLOT_CONFIG_GEN_KEY_ENABLE_FLAG;
return ATCA_ERR_NO_ERROR;
}

void SlotConfig::SetAsPrivKey()
{
/* Declare ECC Private key */
_register |= SLOT_CONFIG_IS_SECRET_MASK;
/* Disable reads */
_register &= ~SLOT_CONFIG_ENCRYPT_READ_MASK;
}

void SlotConfig::EnableExtMsgSig()
{
/* Disable ECDH operations and internal signatures */
_register &= ~SLOT_CONFIG_READ_KEY_MASK;
/* Enable signing of arbitrary external messages */
_register |= SLOT_CONFIG_INT_SIG_FLAG;
_register |= SLOT_CONFIG_EXT_SIG_FLAG;
}

bool SlotConfig::GenKeyEnabled()
{
return ( (_register & SLOT_CONFIG_WRITE_CONFIG_MASK) == SLOT_CONFIG_GEN_KEY_ENABLE_FLAG);
}

bool SlotConfig::IsPrivKey()
{
return (_register & SLOT_CONFIG_IS_SECRET_MASK) == SLOT_CONFIG_IS_SECRET_MASK &&
(_register & SLOT_CONFIG_ENCRYPT_READ_MASK) == 0;
}


ATCAError KeyConfig::SetECCKeyType()
{
_register &= ~KEY_CONFIG_KEY_TYPE_MASK;
_register |= KEY_CONFIG_P256_ECC_KEY;
return ATCA_ERR_NO_ERROR;
}

ATCAError KeyConfig::SetNonECCKeyType()
{
_register &= ~KEY_CONFIG_KEY_TYPE_MASK;
_register |= KEY_CONFIG_NON_ECC_KEY;
return ATCA_ERR_NO_ERROR;
}

ATCAError KeyConfig::EnablePubKeyGen()
{
if (!IsPrivate())
return ATCA_ERR_SLOT_NOT_PRIV_KEY;
_register |= KEY_CONFIG_EN_PUB_KEY_MASK;
return ATCA_ERR_NO_ERROR;
}

void KeyConfig::SetPrivate()
{
_register |= KEY_CONFIG_PRIVATE_KEY_MASK;
}

bool KeyConfig::IsECCKey()
{
return ( (_register & KEY_CONFIG_P256_ECC_KEY) == KEY_CONFIG_P256_ECC_KEY);
}

bool KeyConfig::PubKeyGenEnabled()
{
return ( (_register & KEY_CONFIG_EN_PUB_KEY_MASK) == KEY_CONFIG_EN_PUB_KEY_MASK);
}

bool KeyConfig::IsPrivate()
{
return ( (_register & KEY_CONFIG_PRIVATE_KEY_MASK) == KEY_CONFIG_PRIVATE_KEY_MASK);
}

Loading