Skip to content

Commit af5a5ce

Browse files
author
itayzafrir
committed
atecc608a ecdas sign and verify impl and tests
1 parent ca2b9f9 commit af5a5ce

16 files changed

+2162
-8
lines changed

features/atecc608a/ATCAConfig.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ATCAConfig.h"
18+
19+
ATCAError SlotConfig::EnableGenKey()
20+
{
21+
if (!IsPrivKey())
22+
return ATCA_ERR_SLOT_NOT_PRIV_KEY;
23+
_register = (_register & ~SLOT_CONFIG_WRITE_CONFIG_MASK) | SLOT_CONFIG_GEN_KEY_ENABLE_FLAG;
24+
return ATCA_SUCCESS;
25+
}
26+
27+
void SlotConfig::SetAsPrivKey()
28+
{
29+
/* Declare ECC Private key */
30+
_register |= SLOT_CONFIG_IS_SECRET_MASK;
31+
/* Disable reads */
32+
_register &= ~SLOT_CONFIG_ENCRYPT_READ_MASK;
33+
}
34+
35+
void SlotConfig::EnableExtMsgSig()
36+
{
37+
/* Disable ECDH operations and internal signatures */
38+
_register &= ~SLOT_CONFIG_READ_KEY_MASK;
39+
/* Enable signing of arbitrary external messages */
40+
_register |= SLOT_CONFIG_INT_SIG_FLAG;
41+
_register |= SLOT_CONFIG_EXT_SIG_FLAG;
42+
}
43+
44+
bool SlotConfig::GenKeyEnabled()
45+
{
46+
return ( (_register & SLOT_CONFIG_WRITE_CONFIG_MASK) == SLOT_CONFIG_GEN_KEY_ENABLE_FLAG);
47+
}
48+
49+
bool SlotConfig::IsPrivKey()
50+
{
51+
return (_register & SLOT_CONFIG_IS_SECRET_MASK) == SLOT_CONFIG_IS_SECRET_MASK &&
52+
(_register & SLOT_CONFIG_ENCRYPT_READ_MASK) == 0;
53+
}
54+
55+
56+
ATCAError KeyConfig::SetECCKeyType()
57+
{
58+
_register &= ~KEY_CONFIG_KEY_TYPE_MASK;
59+
_register |= KEY_CONFIG_P256_ECC_KEY;
60+
return ATCA_SUCCESS;
61+
}
62+
63+
ATCAError KeyConfig::SetNonECCKeyType()
64+
{
65+
_register &= ~KEY_CONFIG_KEY_TYPE_MASK;
66+
_register |= KEY_CONFIG_NON_ECC_KEY;
67+
return ATCA_SUCCESS;
68+
}
69+
70+
ATCAError KeyConfig::EnablePubKeyGen()
71+
{
72+
if (!IsPrivate())
73+
return ATCA_ERR_SLOT_NOT_PRIV_KEY;
74+
_register |= KEY_CONFIG_EN_PUB_KEY_MASK;
75+
return ATCA_SUCCESS;
76+
}
77+
78+
void KeyConfig::SetPrivate()
79+
{
80+
_register |= KEY_CONFIG_PRIVATE_KEY_MASK;
81+
}
82+
83+
bool KeyConfig::IsECCKey()
84+
{
85+
return ( (_register & KEY_CONFIG_P256_ECC_KEY) == KEY_CONFIG_P256_ECC_KEY);
86+
}
87+
88+
bool KeyConfig::PubKeyGenEnabled()
89+
{
90+
return ( (_register & KEY_CONFIG_EN_PUB_KEY_MASK) == KEY_CONFIG_EN_PUB_KEY_MASK);
91+
}
92+
93+
bool KeyConfig::IsPrivate()
94+
{
95+
return ( (_register & KEY_CONFIG_PRIVATE_KEY_MASK) == KEY_CONFIG_PRIVATE_KEY_MASK);
96+
}
97+

features/atecc608a/ATCAConfig.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ATCACONFIG_H
18+
#define ATCACONFIG_H
19+
20+
#include <stdint.h>
21+
#include "ATCAError.h"
22+
23+
/* Field masks */
24+
#define SLOT_CONFIG_WRITE_CONFIG_MASK 0xF000
25+
#define SLOT_CONFIG_IS_SECRET_MASK 0x0080
26+
#define SLOT_CONFIG_ENCRYPT_READ_MASK 0x0040
27+
#define SLOT_CONFIG_READ_KEY_MASK 0x000F
28+
29+
#define KEY_CONFIG_KEY_TYPE_MASK 0x001C
30+
#define KEY_CONFIG_PRIVATE_KEY_MASK 0x0001
31+
#define KEY_CONFIG_EN_PUB_KEY_MASK 0x0002
32+
33+
/* Configuration flags */
34+
#define SLOT_CONFIG_GEN_KEY_ENABLE_FLAG 0x2000
35+
#define SLOT_CONFIG_INT_SIG_FLAG 0x0002
36+
#define SLOT_CONFIG_EXT_SIG_FLAG 0x0001
37+
38+
#define KEY_CONFIG_P256_ECC_KEY 0x0010
39+
#define KEY_CONFIG_NON_ECC_KEY 0x001C
40+
41+
/** class for validating and preparing slot config for a data zone.
42+
*/
43+
class SlotConfig
44+
{
45+
private:
46+
uint16_t _register;
47+
public:
48+
SlotConfig(uint16_t slot_config)
49+
: _register(slot_config)
50+
{}
51+
SlotConfig()
52+
{
53+
Reset();
54+
}
55+
56+
void Reset(){ _register = 0; }
57+
ATCAError EnableGenKey();
58+
void SetAsPrivKey();
59+
void EnableExtMsgSig();
60+
61+
bool GenKeyEnabled();
62+
bool IsPrivKey();
63+
uint16_t Get()
64+
{
65+
return _register;
66+
}
67+
};
68+
69+
70+
/** class for validating and preparing key config for a data zone.
71+
*/
72+
class KeyConfig
73+
{
74+
private:
75+
uint16_t _register;
76+
77+
public:
78+
KeyConfig(uint16_t key_config)
79+
: _register(key_config)
80+
{}
81+
82+
KeyConfig()
83+
{
84+
Reset();
85+
}
86+
87+
void Reset(){ _register = 0; }
88+
ATCAError SetECCKeyType();
89+
ATCAError SetNonECCKeyType();
90+
ATCAError EnablePubKeyGen();
91+
void SetPrivate();
92+
93+
bool IsECCKey();
94+
bool PubKeyGenEnabled();
95+
bool IsPrivate();
96+
uint16_t Get()
97+
{
98+
return _register;
99+
}
100+
};
101+
102+
#endif /* ATCACONFIG_H */

features/atecc608a/ATCAConstants.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ATCAECCCONSTANTS_H
18+
#define ATCAECCCONSTANTS_H
19+
20+
/** Datasheet defined constants */
21+
#define ATCA_ECC_508A_I2C_ADDR 0xC0
22+
#define ATCA_ECC_508A_REVISION {0x00, 0x00, 0x50, 0x00}
23+
#define ATCA_ECC_WORD_SZ 4
24+
#define ATCA_ECC_STATUS_RESP_LEN 4
25+
#define ATCA_ECC_RESP_STATUS_IDX 1
26+
#define ATCA_ECC_NUM_PRIV_KEY_SLOTS 8
27+
#define ATCA_ECC_ECC_PK_LEN 64
28+
#define ATCA_ECC_CONFIG_ZONE_SZ 128
29+
#define ATCA_KEY_ID_0 0
30+
#define ATCA_ECC_HASH_256_LEN 32
31+
#define ATCA_ECC_SIG_LEN 64
32+
#define ATCA_ECC_CRC_POLYNOMIAL 0x8005
33+
#define ATCA_ECC_NUM_DEVICE_DATA_BLOCKS 4
34+
35+
/** Device functions */
36+
#define ATCA_ECC_FUNC_RST_IO_ADDR 0x00
37+
#define ATCA_ECC_FUNC_SLEEP 0x01
38+
#define ATCA_ECC_FUNC_IDLE 0x02
39+
#define ATCA_ECC_FUNC_COMMAND 0x03
40+
41+
42+
/** EEPROM Zones */
43+
enum ATCAZone
44+
{
45+
ATCA_ECC_ZONE_CONFIG = 0x0
46+
47+
/* Data and OTP zones are not defined here as this driver does not make
48+
* use of them. */
49+
};
50+
51+
/** Command opcodes */
52+
enum ATCAOpCode
53+
{
54+
ATCA_ECC_CMD_OPCODE_READ = 0x02,
55+
ATCA_ECC_CMD_OPCODE_WRITE = 0x12,
56+
ATCA_ECC_CMD_OPCODE_LOCK = 0x17,
57+
ATCA_ECC_CMD_OPCODE_GENKEY = 0x40,
58+
ATCA_ECC_CMD_OPCODE_NONCE = 0x16,
59+
ATCA_ECC_CMD_OPCODE_SIGN = 0x41,
60+
ATCA_ECC_CMD_OPCODE_VERIFY = 0x45,
61+
};
62+
63+
/** Key ID/Slot no. Type */
64+
typedef uint32_t ATCAKeyID;
65+
#define ATCA_ECC_KEY_ID_0 0
66+
#define ATCA_ECC_KEY_ID_INVALID 0xFFFFFFFF
67+
68+
/** Command and response length */
69+
#define ATCA_ECC_CMD_OFFSET 2 /* Command offset in Tx buffer */
70+
#define ATCA_ECC_RESP_OFFSET 1 /* Response offset in Rx buffer */
71+
#define ATCA_ECC_CRC_INPUT_OFFSET 1 /* CRC input data offset in Tx buffer */
72+
#define ATCA_ECC_MAX_CMD_LEN 132 /* Bytes device allocates for constructing command */
73+
#define ATCA_ECC_MAX_RESP_LEN 67 /* Bytes device allocates for constructing responce */
74+
#define ATCA_ECC_FUNCTION_LEN 1 /* Device function (Write address) */
75+
#define ATCA_ECC_CMD_IO_WRAPER_LEN 3 /* Count + CRC */
76+
#define ATCA_ECC_CMD_READ_LEN 4
77+
#define ATCA_ECC_RESP_READ_LEN 7
78+
#define ATCA_ECC_CMD_WRITE_LEN 8
79+
#define ATCA_ECC_RESP_WRITE_LEN 4
80+
#define ATCA_ECC_CMD_WRITE_LEN 8
81+
#define ATCA_ECC_RESP_WRITE_LEN 4
82+
#define ATCA_ECC_CMD_LOCK_LEN 4
83+
#define ATCA_ECC_STATUS_LEN 4
84+
#define ATCA_ECC_CMD_GENKEY_LEN 4
85+
#define ATCA_ECC_RESP_GENKEY_LEN 67
86+
#define ATCA_ECC_CMD_NONCE_LEN 36
87+
#define ATCA_ECC_CMD_SIGN_LEN 4
88+
#define ATCA_ECC_RESP_SIGN_LEN 67
89+
#define ATCA_ECC_CMD_VERIFY_LEN 132
90+
91+
/** EEPROM addresses */
92+
#define ATCA_ECC_CFG_ADDR_REVISION 4
93+
#define ATCA_ECC_CFG_ADDR_SLOT_CFG 20
94+
#define ATCA_ECC_CFG_ADDR_KEY_CFG 96
95+
#define ATCA_ECC_CFG_ADDR_LCK_CFG 87
96+
97+
/** Flags */
98+
#define ATCA_ECC_FLG_READ_SZ_32 0x80
99+
100+
/** Devices Delays */
101+
#define ATCA_ECC_DELAY_TPU_US 100
102+
#define ATCA_ECC_DELAY_TWLO_US 60
103+
#define ATCA_ECC_DELAY_TWHI_US 1500
104+
#define ATCA_ECC_DELAY_WAKE_TOKEN_RETRY_US 100
105+
106+
/** Command execution times */
107+
#define ATCA_ECC_EXEC_TIME_READ_TYP_US 100
108+
#define ATCA_ECC_EXEC_TIME_READ_MAX_US 1000
109+
#define ATCA_ECC_EXEC_TIME_WRITE_TYP_US 7000
110+
#define ATCA_ECC_EXEC_TIME_WRITE_MAX_US 26000
111+
#define ATCA_ECC_EXEC_TIME_LOCK_TYP_US 8000
112+
#define ATCA_ECC_EXEC_TIME_LOCK_MAX_US 32000
113+
#define ATCA_ECC_EXEC_TIME_GENKEY_TYP_US 11000
114+
#define ATCA_ECC_EXEC_TIME_GENKEY_MAX_US 115000
115+
#define ATCA_ECC_EXEC_TIME_NONCE_TYP_US 100
116+
#define ATCA_ECC_EXEC_TIME_NONCE_MAX_US 7000
117+
#define ATCA_ECC_EXEC_TIME_SIGN_TYP_US 42000
118+
#define ATCA_ECC_EXEC_TIME_SIGN_MAX_US 50000
119+
#define ATCA_ECC_EXEC_TIME_VERIFY_TYP_US 38000
120+
#define ATCA_ECC_EXEC_TIME_VERIFY_MAX_US 58000
121+
122+
/** Driver defined constants */
123+
#define ATCA_ECC_I2C_FREQUENCY 10000
124+
125+
#endif /* ATCAECCCONSTANTS_H */

0 commit comments

Comments
 (0)