Encryption in Assembler - Page 1
Forum section: electronics / general

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 11:43:41 am
content: Hi to all encryption experts out there,given a DSP running at 100MHz with:- SRAM/OTP (program memory): 4k x 8bit- Accumulator: 3x 48bit (ACCU A, ACCU B and ACCU C)- RAM: 44x 48bit registers + 4x 48bit special function registers- ALU: (COSZ)and a very reduced instruction set:- add, sign, sub- resetWDG, powerOnReset, nop, stop- bitC, bitS- rad, clear, load, load2exp, move- div, mult- shiftL, shiftR- jsb, jrt- jcd, jCarC, jCarS, jEQ, jNE, jNEG, jOflC, jOflS, jPOSbeing part of a sensor ASIC, with sensor frontend and interfaces such as SPI and I²C, I'd like to check out the potential to encrypt sensor data, before they are sent via interfaces. As you can see the DSP is only programmable using Assembler.As I'm not a software expert nor familiar with encryption I'd like to get your feedback on what is possible. Thank you guys for all of your feedback.-branadic-

author: DrGeoff
author_rank: Frequent Contributor
timestamp: August 14, 2017, 11:52:09 am
content: Encryption itself is not too difficult. If you can use a block cipher then AES128 would probably work as it was designed for ease of implementation (I have written versions of it in assembler for PIC16 chips) and is fast and efficient.However, the key management part of it will be where the thought needs to be applied. An encryption system needs to have strong key management to be able to handle assigning and loading of new keys, prevention of revealing the keys, handling a compromised key etc etc.

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 01:34:16 pm
content: DrGeoff, can you share, maybe via PN, an example with me how you have impementend the block cipher in assembler? Sharing the key can be done one different ways:1. I have input registers, that can be used via SPI/I²C to give the sensor a new key. Thus the master (microcontroller) gives the key to the sensor (slave)2. A fixed key is programmed into the assembler code and delivered together with the sensor unit, like a licence key on a CD.-branadic-

author: Ash
author_rank: Regular Contributor
timestamp: August 14, 2017, 01:54:53 pm
content: Hi Brandic,Like DrGeoff said, the encryption is quite possible.The real problem / question why are you encrypting? What problem are you trying to solve by adding the encryption?Are you trying to prevent others seeing data over an insecure channel?Are you trying to ensure that is isn't corrupted?Are you trying to verify that the data is authentic and/or current (not replayed)?Are you trying to ensure that only a specific controller can command the sensor?Are you trying to enforce some kind on licensing restrictions?Are you trying to hid the existence of the sensor?Some other reason?This will change how you need to approach the problem. For instance, if you have an insecure / untrusted communications channel (eg your I2C/SPI registers which I can read with an Oscilloscope), you can't just send the keys over it because then the keys have been compromised and your encryption becomes pointless. To counter this you need a much more complicated key exchange process (like Diffie-Hellman for instance).Perhaps if you could explain what you think you want the encryption to solve we could better advise you.Also, standard warning for these things - rolling your own encryption is notoriously difficult to get right.Ash.

author: Ash
author_rank: Regular Contributor
timestamp: August 14, 2017, 02:04:53 pm
content: Quote from: branadic on August 14, 2017, 11:43:41 amJust noticed a lack of bit-wise operators - AND / OR and especially XOR. Without these you'd have to resort to look up tables for each operation and that would be pretty slow and memory hungry..Xor is used extensively in a lot of crypto.Ash.

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 02:32:38 pm
content: There are different reasons to encrypt sensor informations:Security and warranty:Only trusted and no third party sensors are allowed to be used, where the sensor is a component of a superior system. You want to make sure that sensor values are from a valid sensor. Currently the sensor delivers information via digital interface to a host controller and as you said you can hack the interface simply by an oscilloscope and replace the sensor by something else for example a cheap duplication of an expensive sensor. But what if this sensor data values are already encrypted and the sensor can identify itself? We are not talking about a simple temperature sensor, but complex multi-sensors.Next point is all this IoT stuff. You want sensor data encrypted as soon as possible before they are within "THE CLOUD". Each sensor can also be used to harvest personal data but also process data. This process data on the other hand can be used to monitor wether you treat a machine out of specs, right? On the other hand companies want to benefit from all this newish IoT rubbish, so sensor data need to be encrypted to make sure that only allowed persons can see the sensor values.So yes, the first four arguments are the main reasons why I ask for encryption.Quote-branadic-

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 02:52:34 pm
content: There are different versions of the ASIC. In the first version the bitwise operations are missing. In another version bitwise operation such as not, and, or and xor are available. So as far as I understand you this is a good reason to switch over to this version for encryption?-branadic

author: T3sl4co1l
author_rank: Super Contributor
timestamp: August 14, 2017, 04:37:22 pm
content: What sample rate of data?100MHz clock speed is irrelevant to the ability to perform encryption; but to do it in real time, that's where the interesting questions lie!If it ends up taking 10k cycles to encrypt a sample, and the sample rate is, say, 44.1kHz, you have a bit of a problem there. If it's a few lazy samples per second, it seems unlikely to me that an even more aggressively simplified ALU would have difficulty doing it (maybe not a SUBLEQ machine, but other than that..).What about physical access to the chip? If the key is transmitted in the clear via I2C, that's rather counterproductive. A hard coded key provides security (the chip itself must be reverse-engineered*), but once the key is discovered, it's all over (say by brute force, cryptanalysis, or reverse engineering).*Unless a buggy command allows leaking/dumping the address space, or something like that. Which is a very common means of access to OTP and mask ROM devices, so it's worth investigating.Also, if it's OTP instead of mask ROM, an attacker could write additional bits into the program, with unpredictable (potentially insecure) results. Erasing OTP would be much harder, though, on a similar scale as decapping and reverse-engineering I think. Write attacks aside, OTP should be pretty good.What's the threat model? Are critical backend systems exposed to the sensors, so that an attacker spoofing a sensor is dangerous? Or is it a data privacy thing, where the breech of a single sensor is an individual privacy problem, but not a global one, while a breech of all sensors would be very bad?The sensors could be made with individual (non sequential!) serial numbers, and that used as a keyphrase for the encryption. The serial number could be hashed, then transmitted as a passphrase, to the backend. You'd need a record of every serial number produced, but that's not a big burden of data, these days. This would help limit the extent of privacy breech.Tim

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 05:23:23 pm
content: The sample rate is in the order of <100 samples per second, up to 8 different sensor values each 24bit long.A hard coded key is only possible by programming the key into the OTP. I have no access to the siliconn fabrication.I know, nothing is really save, it's only a matter of effort to hack almost everything, but having no security attributes at all is ignorant.Yes, it's a privacy thing, if you would like to name it like this and I'm just checking what is possible and what's impossible with the given hardware. I'm pretty sure that future sensors especially for IoT will have encryption but also identification attributes such as physical unclonable functions (PUF) included.So given I program some individual ID into the sensor, that is is provided together with the sensor to the customer, it should be possible to encrypt sensor data in AES128 with the given hardware?-branadic-

author: DrGeoff
author_rank: Frequent Contributor
timestamp: August 14, 2017, 09:22:36 pm
content: Quote from: branadic on August 14, 2017, 05:23:23 pmAES128 is a block cipher, and encrypts in 128 bit blocks. You need to take this into account with your interface protocol. If only one block per message is required then you should probably include a unique changing number or some other bits that are unique to the message so that each encrypted block that carries the encrypted data is different. Otherwise an attacker could simply obtain a set of values for each bit of sensor stimulus and send these, effectively bypassing your encryption.As some have suggested, it is more advantageous to use a diversified key rather than a master key for all crypto operations. Diversification uses a common key that is present in all devices and this key is used to encrypt something unique within the device (such as serial or ID number) which is then subsequently used as a unique key for that device. This means that no two devices will ever use the same key, however the sender and receiver must know the deviceID in order to decode the message. The inclusion of deviceID in the message may already be part of the interface protocol.

author: nctnico
author_rank: Super Contributor
timestamp: August 14, 2017, 09:43:43 pm
content: Quote from: DrGeoff on August 14, 2017, 09:22:36 pmMy gut feeling says this is a bad idea because the device ID is (usually) something you can read from the device. If you use that to create a key then part of the key is known to an attacker.

author: DrGeoff
author_rank: Frequent Contributor
timestamp: August 14, 2017, 09:48:55 pm
content: Quote from: nctnico on August 14, 2017, 09:43:43 pmYes, most probably. However the deviceID is encrypted with a master key to produce a device specific key. The master key is unknown and it makes messages from each device unique. Without diversification you have a risk that breaking one set of messages will allow all sensors to be replaced with clones. Just as the master key can be burned into the device, so too can a 128 bit unique ID that is not readable externally but is known to both ends. That is what I would be suggesting.

author: ejeffrey
author_rank: Super Contributor
timestamp: August 14, 2017, 10:19:58 pm
content: Quote from: DrGeoff on August 14, 2017, 09:22:36 pmBlock ciphers are usually implemented in a mode that fixes this by manipulating the cipher initial value. Usually this is done either via block chaining or counter mode. Note that you may have to be careful to avoid counter reuse and similar problems with chaining. In embedded applications this may be quite difficult, since someone can power cycle your device at will. It may be better to bypass this issue by using single use AES keys and diffie hellman key exchange. But then you need to implement some public key algorithms which are more complicated, and key distribution is still a problem.If you have to be tamperproof your problem is much harder. Power channel attacks are often very easy to implement and very difficult to defend against.

author: branadic
author_rank: Super Contributor
timestamp: August 14, 2017, 10:29:38 pm
content: Thanks for the reply. I call myself a "theopractitioner", a good mix of theory and practice, so lets get to practice, shall we? I don't want a complete solution, but a hint on how I can implement it into my environment.-branadic-BTW:The sensor values are stored in result registers that can be accessed via SPI or I²C.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 14, 2017, 10:41:05 pm
content: Quote from: branadic on August 14, 2017, 02:32:38 pmIf someone breaks into a sensor, this will be enough to sniff the code and the key and then to be able to impersonate any malicious device as the sensor. Then you can build any number of copies of this malicious device which can pose as valid sensors all around the world.If you want to prevent this scenario, you need to issue a cryptographic certificate to each server. The certificates should expires unless renewed and you should be able to revoke compromised certificates.And do not store master/system key or anything of that sort inside sensors.

author: Ash
author_rank: Regular Contributor
timestamp: August 15, 2017, 12:02:03 am
content: Quote from: branadic on August 14, 2017, 02:52:34 pmDefinitely, you will find it difficult / very inefficient to implement any crypto without XOR.Quote from: branadic on August 14, 2017, 02:32:38 pmOk, thank you that helps a lot. I don't consider myself and expert in this stuff, but I've done a bit..One thing that no one has mentioned yet is that many countries consider encryption to be controlled in some way. You should work out what that means for your situation. For instance, in Australia we have to seek export approval / registration for cryptographic hardware and the definition is very broad. Whether you agree with the restrictions or not, they still apply to you so ignore them at your peril.Encryption and Authentication are two different things. A block cipher (AES for instance) will not prevent me from modifying data on the wire. I may not know what the data is, but I can flip bits and your consumer won't know the difference unless you have an additional layer of authentication.HMAC is a good way to do authentication, it is a hashing process that can use a number of different hashes (most commonly one of the newer SHAs) and an authentication key. This key should not be the same as the encryption key (at least that's my understanding).Also, as Blueskull mentioned, random numbers are important. Block ciphers take initialisation vectors and it is very poor form to reuse one. You will need a source of random. This can be a pseudo random number generator (make sure it is crypto grade stuff) and periodically seed it from a true random source (external event timing if nothing else)You will have to format your data into a packet, this packet should contain a header with the block cipher IV, then you add your encrypted sensor payload, then you HMAC the entire thing (header/IV/cipher text) and add the hash.You will need to work our how you can implement keys, you will probably need unique random keys programmed at the factory. Then you will probably want (as Blueskull suggested) a key exchange process that uses this data to generate session keys that are used for the actual encryption / authentication. Regenerate these keys periodically. This is not as easy as it sounds to do securely.As for actual implementation, this is a bit more tricky. I can only point you to implementations that are commonly available on the Internet. You will have to work our how to map this into your assembly / system.I would suggest that you try to implement everything on a PC or similar first and then compare the outputs against know good implementations with a large set of test vectors. This will tell you that your implementation is "correctly" manipulating the bits. Then move to assembling the pieces and port to your sensors. Do you have an emulator? Given the simple instruction set, perhaps you could hack one up that is sufficient to do crypto testing. This will also give you a good handle on cycle-counts needed to do the work, and this might be a problem for you. Some algorithms are more efficient in software than others. But I'm no expert on that.Hope that helps.Ash.

author: DrGeoff
author_rank: Frequent Contributor
timestamp: August 15, 2017, 12:45:30 am
content: Quote from: blueskull on August 14, 2017, 11:25:31 pmThe OP's question was simply about implementing encryption in assembler. There are really no details about the system, protocols, key management or other architecture that can be considered at this time. This is left to the OP to describe or solve.From the details so far, encryption is not being used for security of message contents, but authorisation of device.Whether this is a good approach would require a lot more system architecture and interface protocol details. There may be other mechanisms that could achieve the outcome.It should also be noted that encryption algorithms have a shelf life. This is defined as the amount of time that the algorithm is expected to be secure against brute force attack. With computing technologies improving rapidly the shelf life for algorithms such as aes128 might be significantly shortened.

author: nctnico
author_rank: Super Contributor
timestamp: August 15, 2017, 12:50:22 am
content: For authentification a hashing algorithm combined with a challenge-response system might be a good idea.