Hello everyone, this is the use of a blog, this article is the ATSHA204A encryption chip configuration, mentioned above, atsha204a encryption chip internal ROM three areas, one is Config zone, one is slot zone, there is an OTP zone, This article not only explains Config zone configuration, but also explains the slot and OTP area configuration method, and unifies the official library to demonstrate the code.
First of all, the communication interface in this paper use the I2C protocol, but all the functions can be implemented on a single bus.
To begin with, I'd like to start with a few details, which is a bit of a problem for me to get in touch with this chip.
1, the chip config area and data area once locked, there is no way to unlock, the lock method can only be locked through the lock command.
2, config area in the unlocked time, you can use write command to write, but note that the 0X00-0X03 address (Word address, please refer to the manual) can not be written, 0x15word address can not use the Write command.
3, before the config lock, the data area (including the slot area and OTP area) can neither write nor read. In the config lock, the data area before the lock, the data area can only write can not read, in the data area locked, can be based on config in the configuration to read and write.
The reason why I have summed up these points is that we can understand the reading and writing properties of these areas and then get to the point. 1, the Communication package format
All packets sent by ATSHA204A are in the following format.
Word address indicates how this packet works, with the following values
The most common is the 0x03 command packet.
Count is the length of the packet, including the count itself, data, and CRC16.
Data is the content, depending on the command.
The CRC16 is the checksum code of the packet, and the checksum content is from count to data.
Let's take a look at the Configuration area Config.
All address operations for ATSHA204A are based on the word address, as shown in the previous illustration.
OK, now let's take a look at the next read command format (that is, the contents of the data field in the packet).
The first byte is the opcode, and the read command's opcode is 0x02,
The highest bit bit7 of the second byte indicates whether to read 32 bytes or 4 bytes, and 1 to read 32 bytes. Bit0 indicates which district to read.
The third byte is the address.
About the address here, the datasheet manual reads as follows
Read and Write command Param2 this address as above, high 8-bit 0, low 8-bit top 3 is offset, high 5 bit is block, a blocks of 32 bytes, so config 3 blocks (0-3), slot 16 block (0-15), The OTP is divided into 2 blocks (0-1) and the top 3 offset is ignored if 32 bytes are used for reading and writing. For example, we're going to read the Config area's word address 0x09 that 4 bytes, because the 0x09 is block1, and the offset is 1 (the Block division of Config is that word 0x00-0x07 is block, 0,0x08-0x0f for block1,0x10-0x15 is Block2, anyway, 32 bytes is 1 Block,slot and OTP is the same)
Address[4:3] = 1;
If it is 4 bytes read, then
ADDRESS[2:0] = 1;
If it is 32 bytes read
Ignores the address[2:0].
Now let me give you an example of the packet of communication.
If we were to read the 4 bytes of Config's word0 address, the packet format would be as follows:
If the communication is normal, then the chip will return the data, the returned packet format is as follows
The meaning of each data field is the same as the data sent.
Based on the examples just given, the data returned are as follows:
Because we read the chip serial number, XX is the only part of each chip serial number.
This is just a demonstration of the read commands, if other commands, the data field will be more complex, fortunately Atmel has a special library to encapsulate these operations, the official library files are listed below:
Sha204_comm_marshling.h and sha204_comm_marshling.c, these two files.
uint8_t Sha204m_execute (
uint8_t Op_code, uint8_t param1, uint16_t param2,
uint8_t Datalen1, uint8_t *data1, uint8_t datalen2,
uint8_t *data2, uint8_t Datalen3, uint8_t *data3,
uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size,
uint8_t *rx_buffer);
This function encapsulates all the commands, using very aspects, the concrete implementation can go to download the source of the library to view, and the portable files are sha204_i2c.c, sha204_physical.h, Sha_timer_utilities.h and Sha_ Timer_utilities.c These files, do a transplant simply modify the inside several about I2C communication and delay function.
This section describes the basic format of the command in a simple example, and simply introduces the structure of the official library file, which can be DMS. Config Area Configuration
This section is the focus of this blog, only the correct configuration of the config,atsha204a chip in order to act according to their own intentions.
I'm going to put another Config zone configuration table
The following address I'm talking about is all word addresses
The configuration area address 0x00-0x03 is not allowed to write and can only be read.
Starting from 0x04, the first is I2C address, you can set the I2C device addresses, if the I2C bus hanging several atsha204a, you can modify this address, and then checkmacconfig, the documentation described below
This byte is very difficult to understand, this byte only affects the read and write commands and the Checkmac command, for read and write commands, in the encryption read write to match the Tempkey in the corresponding bit of sourceflag, otherwise encrypted read write will fail, what is encrypted reading and writing. It will be introduced later, and similar to the CHECKMAC command, you can refer to the document for specific use. The
Next OTP mode
document describes the following
OTP mode = 0xAA Read-only mode, after OTP lockdown, only read content in the OTP area
OTP mode = 0x55 consumption mode, after OTP locking, write operations can only make Bit 1 to 0, can not be changed to 1, such as the original content is 0xFF, write 0xFC, it becomes 0xFC, and then write 0xFF, it is still 0xFC, write 0x00 immediately clear 0 and can not write other values (this process is a bit like flash burn write).
OTP mode = 0x00 Traditional mode, and cannot read OTP0 and OTP1 after OTP lockdown, nor can it be read in 32 bytes.
Other values are not allowed.
Selector mode, which defines the Selector update on the 0x15 address,
Selector mode = 0x00, you can update the values in Updateextra with the Selector command,
Selecto R Mode = Other value that can be updated by the Updateextra command command only if the value in Selector is 0 o'clock.
here by the way selector, the device will not enter idle mode when the Pause command command is executed and the selector specified in the Pause command command parameter is equal to the selector in the configuration.
For general users, the 4 configurations of the 0x04 address are set by default values.
Next is the address 0x05-0x0c about slot configuration, here is the focus of this section, but also need to pay attention to the most places.
Each slot config occupies 2 bytes and each bit is configured as follows
One of them said
Bit0-3: Specifies the slot area of the encrypted read, which means that if you set the Slotx to be encrypted read, you would specify that the encrypted read key be stored in the slot, in short, you have to read a slotx, and slot Config[x] has been configured to read encrypted, But need to know slot config[x]. The key for the slot area specified in the Readkey.
Bit4:0: This slot area can be used for all cryptographic commands, 1: This slot can only be used for CHECKMAC commands and Gendig commands.
bit5:0: The key stored in this slot can be used indefinitely, 1: This slot store the key is limited use, the number of times according to Useflag or Lastkeyflag specified.
Bit6:0: Can read in plaintext, 1: read must be encrypted.
Bit7:0: This slot is not a key area, you can read and write, 1: This slot as a key area, if you can read and write must be encrypted.
Speaking of which, let's look at a table first.
You can see clearly the impact of the configuration of Issecret and Encryptread bits on the slot read operation.
Issecret = 1;encryptread = 0;slot cannot be read.
Issecret = 1;encryptread = 1;slot can be read encrypted.
Issecret = 0;encryptread = 0; Read freely.
Say back to the slot config configuration, bit8-bit11, specify the slot area key for the cryptographic write operation, similar to bit0-3, except that it is a write operation that uses the Write command commands.
Bit12-15 Write config table, the above write config table looks a bit laborious, bit12-15 for the derivekey command and Write command commands have different meanings, for Derivekey Command, is to determine how the new key generated by the way to get (target?,parent?), you can see my last article in the use of the Derivekey command description. And for the write Command, define the following
bit15 = 0;bit14 = 0;bit13 = 0; Write at random
bit15 = x;bit14 = 0;bit13 = 1; Can't write
bit15 = 1;bit14 = 0;bit13 =x; Can't write
bit15 = x;bit14 = 1;bit13 =x; Encrypt write
Here is a general introduction to the configuration of the slot area, here for the simplest example, as follows
Now to set the slot0 and slot1 into the key area, no read and write, you can perform all cryptographic commands, unlimited use, the address word0x04 configuration should be 0x80 0xa0 0x80 0xa0,
According to the manual, we know that the Write command commands are as follows
We're going to write Config zone, the address is 0x04,4 byte write, so populate the following
opcode = 0x12,
Zone = 0x00,
Address = 0x04,
Value = 0x80,0xa0,0x80,0xa0
The data packet is in the following format
data_2 mac Domain in the encrypted write operation will have content, there is no encryption write operation, so the above data packet does not have this field, if you use the official encapsulated library, you can use the Sha204m_execute function
The function prototype is as follows:
uint8_t Sha204m_execute (uint8_t op_code, uint8_t param1, uint16_t param2,
uint8_t Datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t Datalen3, uint8_t *data3,
uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size);
This function has many parameters, mainly considering the different contents of various commands, when the function is called by the description above, the specific contents are:
State = Sha204m_execute (
0x12,
0x00
0x05,
4,*yourbuffer,
0,0,0,0,
Sha204_cmd_size_min,
Txbuf,
Write_rsp_size,
Rxbuf
);
Of course this is a piece of pseudocode, Yourbuffer is the Data_1:value storage place, Sha204_cmd_size_min and read_32_rsp_size in the library are defined, in fact, in this function to implement the internal sha204_cmd_size The size specified by _min and write_rsp_size does not work, and automatically adjusts the size of the packet to and from within the function, and the user only needs to be given a large enough txbuf and rxbuf.
Next is Useflag, where the value in Useflag indicates how many times the key in the corresponding slot area (only slot0-7) can be used, but only if it is slot config. SingleUse = 1,useflag is useful. In the original datasheet 78 pages inside the detailed introduction of the use of Single-use key, I cut a picture for you to see
Probably means, slot0-7 only have useflag this function, slot8-14 not (slot15 more special). If the corresponding slot Config.singleuse = 1, then the command will return an error when performing some cryptographic commands that need to use this slot useflag[slotid] = 0x00. Useflag's counting method is simply that this 8-digit digit is 1, for example, 0xFF is eight times, 0x7f is 7, 0x01 is 1 times, so on.
When some encryption commands are executed, the Useflag will start zero at the top, and the Derivekey command can update the values in Useflag. For the Derivekey command, you can look at the blog I used in the previous article, because the space problem, no longer discussed.
Updatecount is the corresponding slot use Derivekey update how many times, to 0xFF roll back to 0x00.
Then it is lastkeyuse0-15, first of all, that this "last" is not the previous meaning, but the final meaning, that is, this lastkeyuse0-15 are all for the slot15 service, really powerful, In fact, lastkeyuse use and Useflag almost, but also to slot15 Config.singleuse = 1 to be effective, and calculate the remaining use of the method is, all the number of Lastkeyuse bit 1 add up, then the total has 16*8 = 128 times , but after use, slot15 is completely finished, can not be used again, can not be Derivekey update, this is slot15 more special place.
Address 0x15 can not be written command, only through other commands, about Userextra, the official source document said this can be Updateextra command update, its use is mainly for users, other uses unknown, If anyone else knows what this byte is for, I'd like to tell you, thank you.
Selector the previous article: when executing the Pause command command, the device will not enter idle mode when the selector specified in the Pause command parameter is equal to the selector in the configuration. can also be
Updateextra command Update,
The Updateextra command commands are formatted as follows:
Parameters at a glance, no more said, here only need attention,
1, mode.bit1 = 1 o'clock, the operation of this command is to reduce the number of slotid specified in NewValue.
2, the value in Userextra is not 0, then this command will return failure if the Userextra is updated.
3, if the Selectormode is not 0, and the selector is not 0, then this command will return failure if the selector is updated.
Lock data and lock config are the two bytes that control the locked and configuration areas.
Lockdata = 0x55,data area is not locked for write operations.
Lockdata = 0x00,data area is locked and read-write needs to be configured.
Lockconfig =0x55,config area is not locked for read and write operations.
Lockconfig = 0x00,config area cannot be written.
These two bytes must be set using Lockcommand, Lockcommand as follows:
ZONE.BIT0 determines whether the lock config or the OTP area, ZONE.BIT7 = 1, does not calculate the CRC16 value that needs to be locked, otherwise you need to enter a CRC16 value to verify.
Summary if zone.bit7=0, then you need to fill in the corresponding locking area of the CRC16 value, otherwise fill 0x0000. Returns 0 if the command succeeds.
This section describes the configuration of config zone, users can configure the config according to their own needs, strongly recommend that users to read datasheet can be deeply understood. configuration of the slot area
The configuration of this area is not so complex, the main intention of slot is to store the key, of course, can also be set to use EEPROM, but the general user will not do so.
The slot area is divided into 16 parts, slot0-15, each part can store 32 bytes of key, believe enough to use.
The configuration of the slot area is no more than read-write content, and before the config area is locked, all reads and writes to the slot area return an error. After the config area is locked, the data area can only write slot area and cannot read before locking, and after both are locked, it is possible to read and write according to the slot config mentioned in the previous section.
Suppose we have configured config area, now we write slot area, very simple, in the data area before locking can read and write, but it is strongly recommended to use 32 bytes to write operations, using 4-byte write operation, there are many restrictions.
If we write to slot0 0x11,0x11,..... 0x11 (a total of 32 0x11), using the Write command, the arguments are as follows (here we will not consider all kinds of cryptographic read and write for the time being, we'll introduce the encrypted read and write at the end of this article)
opcode = 0x12,
zone = 0x80|0x02; 32 bytes Write, write slot area.
address = 0x00;
data_1 = 32 bytes 0x11;
Data_2 not fill
If the write succeeds, atsha204a will return 0. OTP Configuration
OTP and slot area configuration similar, in atsha204a, OTP 16 parts, each 4 bytes, the default value is the 0XFFFFFFFF,OTP area in the Config area before the lock can not do any read and write operation, after the Config lock, the data area locked before, can not read, Can only write, unlike the slot area, the OTP area does not have the concept of encrypted read and write, and it is strongly recommended that 32-byte write, 4-byte writes have many limitations, and that when the data area is locked, the reading and writing of the OTP area is determined by OTP mode, which is described in the config section, This is no longer discussed here.
On the role of OTP, first, users can customize the use of the second is the OTP to participate in some cryptographic command of the Mac digest calculation, if there are other uses, welcome readers to add. Encrypt read and write
I think the encryption read and write is the function of advanced users, since ATSHA204A provides this function, that presumably this function is also very useful, nonsense not to say, to get to the point.
Encrypted read only when slot config. Encryptread = 1 and slot config. Issecret = 1 o'clock to proceed.
Encrypted reading is to prevent others from sniffing the key on the bus, and here I borrow a picture from the official document Atmel-8981
This flowchart is really clear, the main steps are as follows:
1, first send nonce command commands, update the value in Tempkey, the host side according to the command returned Randout to use the SHA-256 algorithm to calculate the Tempkey value.
2, send gendig command,gen Dig command in the parameters of keyID, is slot config. Readkey, for example, we want to read the SLOT1 key, and slot config[0]. Readkey is set to 0, then we need to know that the key stored in the slot0 to read the SLOT1 key, after sending this command if executed correctly, the value in Tempkey will be recalculated, and the host needs to be based on some column parameters such as read key to calculate the value in Tempkey.
3, read the command, read the value in the slot area, this time the return of the content is a different or encrypted, the key is the 2nd step to calculate the summary, that is to say: Read back the value of the second step of the calculation of the abstract = original.
Here next gendig command, this command in my last article on the blog, not mentioned, here to say, this command functions as follows (datasheet original):
The Gendig command uses the SHA-256 algorithm to update Tempkey with the value in Tempkey, and the value in Tempkey must be legal before executing this command.
Because some cryptographic commands, such as the Mac,hmac command, need to use the Tempkey value, this command can be used to increase the use of some slot areas of authentication, so as to achieve more secure authentication. In the process of encrypting the read, this command must be executed correctly, and the corresponding zone can be read by the correct encryption.
About the nonce command and the Read command, which were mentioned in my previous configuration and above, are not discussed here.
Encryption write, encryption write a little more complex, here or put the Atmel-8981 document diagram
Similarly, cryptographic writes are designed to prevent others from acquiring keys on the bus. Similar to the encryption read process, but the host also calculates a Mac sent to atsha204a.
1, send nonce command commands, update the value in Tempkey, the host side according to the command returned Randout to use the SHA-256 algorithm to calculate the Tempkey value.
2, send Gendig Command,keyid is to write the slot Config.writekey specified in the ID, such as we want to write slot1, and slot Config[1].writekey set to 0, then this keyID is 0. The host then calculates a summary (session key) based on the key and other parameters in the Slot0, and of course atsha204a will carry out the same calculations alongside the Tempkey.
3, we want to write the key and the second step of the calculation of the summary of the different or operation until the cipher text.
4. A host MAC is computed based on the summary and clear text generated in the second step and other parameters (as indicated in the flowchart).
5, execute write command, write command parameter value is encrypted text, and parameter Mac is host Mac (about write command format back to the text).
6, ATSHA204A received this command, will decrypt the value (that is, and their own tempkey do XOR or operation), and then perform a similar 4th step of the calculation process to get the client Mac, compare host Mac and Client Mac know the written data is correct.
In this way, the entire cryptographic writing process is so, in this whole process, whether the key, or write key, there is no clear text on the bus transmission, the sniffer can not intercept our set the key. Summary
This paper introduces a series of configuration methods of the ATSHA204A chip, this is my many days to study the results, but the most complete or original datasheet, I write blog may not thoroughly end of this chip all the functions and use of the introduction of clear, developers according to so many configuration items, Can be fully configured to their own functions (such as anti-copy board, product pairs, key verification, etc.). In addition, the deficiencies in this article, I beg to correct. Appendix
Because of personal work reasons, I produced the burner is no longer sold, if any use of the problem can contact me.