Encryption and Decryption on PIC18F1320 - encryption

I have a pair of transceivers connected to the micro-controller from Port A, and a MM232R connected to Port B on a separate PCB. Each transceiver will send encrypted data, while MM232R will receive a decrypted data. I need write encryption algorithm and decryption code. Can anyone give me idea on how to go about it? I am new to programming and encryption algorithm.

I'm not sure about the ROM/RAM limits of the PIC18F1320, that is the main constraint.
This page contains an implementation of AES on a PIC18F4620. It uses 2K words of ROM and 240 bytes of RAM.
This other page seems to have an implementation of RSA on a PIC18F4550, but it warns that it is very slow (which does't surprise me, since RSA encryption requires modular arithmetic on large integers).

Related

Which public-key algorithm is suitable for videos and images?

I am planning to create an application that has to be able to securely (that is, encrypted) send messages between clients. These messages may include images and videos (up to 50MB in size). Due to how public-key cryptography works, the encryption process has to be run once for each public key.
For these reasons, I am looking for an algorithm that can encrypt the media fairly quickly on modern devices (eg. mid-level smartphones).
I am asking the question because all algorithms I came across (Blowfish and RSA for example) have a relatively small payload limit, which means that even an image will not fit into it.
Don't roll your own protocol. You're thinking about this at a far too low level. People who know exactly what they're doing have a hard time writing secure code. You don't even know where to start, so you don't stand a chance of getting it right.
If you can establish a direct connection between the two endpoints, use TLS. If you can't establish a direct connection, consider using TLS and relaying the encrypted packets; if that's impractical, use Signal. TLS is ubiquitous; your operating system(s) probably come with an implementation in their default installation. Signal is less ubiquitous, so you'll probably have to embed a library. Make sure to keep up with updates to this library. And once again, don't implement your own library.
Under the hood, all systems that use public-key cryptography to store or transmit more than a few bytes of data use hybrid cryptography. Public-key cryptography is used to establish a symmetric key and authenticate the data, and symmetric cryptography does the heavy lifting.
For example, RSA can be used to encrypt a symmetric key. It's pretty difficult to get it right in practice, however. It's also slower than more modern methods that use elliptic-curve cryptography. TLS can use RSA encryption, but this is deprecated. TLS preferably uses an (elliptic curve) Diffie-Hellman key exchange and an RSA or (EC)DSA signature to set up the secure connection. If you'd like more explanations about how TLS works, read an overview of the protocol, a history of problems and how they were resolved, and an explanation of why RSA encryption is problematic.
Note that Blowfish has been obsolete for a while. If you ever need to choose a specific symmetric cryptosystem — which usually means you're doing it wrong — pick AES-GCM, AES-CCM or ChaCha20-Poly1305.
You are correct about the input size for RSA. In general, for any public key cryptography, the input size of limited and as opposed to your needs, it's quite a bit slower than what you need. Public key cryptography is usually used to share small pieces of data.
For example, in the TLS implementation using RSA (TLS_RSA_WITH_AES_128_GCM_SHA256), the RSA step is used to share a symmetric key, generally, AES, between 2 parties and then all the heavy lifting is done by AES.
What you need is a symmetric encryption algorithm. You can use AES (key sizes of 128, 192 or even 256) to encrypt your images which can be of any size. AES is a block cipher and, using a suitable block cipher mode, theoretically does not have an input limit to it. AES-128 is a fairly standard NIST approved (FIPS 197) symmetric encryption scheme, so it should be safe, but you can always go for a higher key size, say 256.
Since, you have the security of the algorithm defined, we can now talk about the speed.
RSA:
$ openssl speed rsa2048
Doing 2048 bit private rsa's for 10s: 296 2048 bit private RSA's in 10.00s
Doing 2048 bit public rsa's for 10s: 6171 2048 bit public RSA's in 9.99s
LibreSSL 2.6.5
built on: date not available
options:bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) aes(partial) blowfish(idx)
compiler: information not available
sign verify sign/s verify/s
rsa 2048 bits 0.033784s 0.001619s 29.6 617.7
AES-128 in GCM mode
$ openssl speed aes-128-gcm
Doing aes-128 gcm for 3s on 16 size blocks: 3778792 aes-128 gcm's in 2.99s
Doing aes-128 gcm for 3s on 64 size blocks: 1611239 aes-128 gcm's in 3.00s
Doing aes-128 gcm for 3s on 256 size blocks: 485243 aes-128 gcm's in 2.99s
Doing aes-128 gcm for 3s on 1024 size blocks: 125054 aes-128 gcm's in 2.99s
Doing aes-128 gcm for 3s on 8192 size blocks: 15366 aes-128 gcm's in 2.96s
LibreSSL 2.6.5
built on: date not available
options:bn(64,64) rc4(ptr,int) des(idx,cisc,16,int) aes(partial) blowfish(idx)
compiler: information not available
The 'numbers' are in 1000s of bytes per second processed.
type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes
aes-128 gcm 20220.96k 34373.10k 41545.89k 42827.86k 42526.44k
You can see the difference, how AES can blow away RSA in the dust with respect to speed. Symmetric Encryption algorithms are inherently faster due to its design. Asymmetric algorithms use math operations on huge numbers and primes which are slower.
Since AES is so popular, CPU manufacturers started implementing special instructions to make AES computations more and more native (AES-NI). So AES will be faster on almost all popular CPUs.
A small note on security between RSA and AES, although it's like comparing apples to oranges.
RSA2048 offers only 112 bits of security whereas AES128 offers 128 bits of security (ref: aes-vs-rsa)
Another challenge in the above problem would be key management. How would you safely manage the keys? If the keys are to be ephemeral, i.e. session based or one key per use, then you can use RSA to exchange symmetric keys and then perform the encryption. This would guarantee perfect forward secrecy. If you want persistent keys, then obviously there are much more elaborate methods to do that which is out of scope of this question.

Length of public key vs encryption algorithm

I'm trying to understand the mechanism of encryption used on the web.
One thing is not clear to me at this point:
If I check, for example, google.com certificate, I can see that the public key is 4320 bits long, but Chrome shows that the connection is encrypted using AES_128_GCM, that I would expect to work with 128 bit keys.
What am I missing here?
The 4320 bits in the public key algorithm are used to encrypt the 128 bits in the symmetric key algorithm. The public key establishes a secure channel of communication between two parties who initially don't have any shared key. Think of this as a low bandwidth channel, which isn't very useful due to the computationally expensive nature of the public key algorithm. In practice -- the sole use of this public-key is to communicate a shared key to establish a high bandwidth channel which uses something like AES (which requires both parties to have the same key). The overall process is an example of a hybrid cryptosystem.
The whole process is more involved but here are the basis to answer the question.
The data is encrypted with a symmetric algorythm such as AES, it is fast, secure and can handle any length of data.
The symmetric key is encrypted by an asymetric algorythm sucb as RSA or EC, they are slow and the data length is limited by the key length.
There are many articles on the Internet about HTTPS, it can get quite complicated.

What is advantage of implementing RSA cryptography algorithm on FPGAs?

RSA , no doubt, has several applications and is very famous. And today we use 2048-bit RSA key for security purposes. Yet, I see several research papers implementing RSA algorithm with 32-bit key (or more) on FPGAs.
http://ac.els-cdn.com/S1877050914009478/1-s2.0-S1877050914009478-main.pdf?_tid=a6e0e2de-edcd-11e5-9656-00000aab0f26&acdnat=1458390502_b193a360ee3dad04b27a38b33d3e113a
http://research.ijcaonline.org/volume92/number8/pxc3894226.pdf
What is the purpose of it? 32-bit algorithm is easily crackable.
Only Montgomery method is effective approach to RSA implementation in FPGA.
The advantage is ratio of the: technology cost, obtained bandwidth and power consumption.
This ratio for PC computer with OS is relatively weak.
Sorry for My English.
Why on FPGA?
Because a hardware implementation is more efficient and offloads the CPU. That is more relevant for small devices, think IoT.
Even if we do, then we end up with 32 or 64 bit key
That seems to be an artifact of the fact that this is research. One of those papers mentions "It is verified that this architecture support multiple key of 128bits, 256bits, and 512 bits"
Still not something I would like to use for my bank transactions but for data packets of low value, or only valuable or a short time period, even 128 bits might be acceptable.

Can Elliptic Curve Cryptography be used as a block ciper?

I am trying to use asymmetric encryption to encrypt firmware. The bootloader will decrypt and update the flash. This is on a embedded device with 32 bit CPU executing at 60MHz.
I want to use ECC due to its varies advantages. I am new to encryption and my understanding os ECC as implemented in ECIES is to use ECC for the key generation and use AES for actual data encryption. Due to code and ram size, I cannot support multiple encryption algorithms.
Is there a implementation of ECC that can be used just like AES. All I am looking for is to use a "Private key" to encrypt firmware and the bootloader uses "Public Key" to decrypt it.
Thanks.
I'm not sure that you completely understand what ECIES consists of:
http://en.wikipedia.org/wiki/Integrated_Encryption_Scheme
That's quite a bit of work, and it requires a whole lot of primitives, including at least one symmetric primitive, it seems to me. That might as well be AES.
Let's start from the last sentence of the question:
All I am looking for is to use a "Private key" to encrypt firmware and the bootloader uses "Public Key" to decrypt it.
There's some confusion in terminology here. Private keys are used to decrypt (or sign) and public keys are used to encrypt (or verify). If I understand correctly, what you want is for the bootloader to verify a signature on the firmware so that only a firmware that was properly signed by yourself will be accepted by the bootloader.
There are various asymmetric signature schemes which can be used for this purpose, including some which are based on eliptic curve cryptography. For example you could use the OpenSSL implementation of ECDSA (see http://www.openssl.org/docs/crypto/ecdsa.html).
I'm afraid there's not enough information in the question to properly choose the best signature scheme (and possibly an encryption scheme as well if there is a need to keep the firmware secret). In any case, good cryptography is not enough to make a system secure and other considerations such as secure implementation are no less important.
If this is is something that is important for you to protect and that you are worried that hackers may try to break, I would strongly advise procuring the services of a security professional. Using cryptography correctly is a very tricky business that requires a full understanding of the system - otherwise you may find yourself in a situation like this
If you look for "authentication" you have to use asymmetric algorithm like EC, this usually done because if the user or process want to update the "firmware" he should identify him self to the bootloader by his "signature" to check who request this update.
After that is done, the bootloader will load the symmetric key from a secure memory to decrypt what you want to do.
So, you have a symmetric key for encryption (AES), and asymmetric two keys for authentication (=Who are you?).
Note: there is no advantages of EC on 32 bit CPU executing at 60MHz for Encryption, unless your application need asymmetric for Encryption NOT authentication, this happen due to line between the user and bootloader is not secure.
Therefore, you could use bootloader's "public key" to encrypt firmware and the bootloader uses its "private Key" to decrypt it, however, the implementation cost a lot due to the high computing for asymmetric algorithm.
Look for "lightweight cryptography", it is typical for your application.

SSL Certificate encryption vs cypher encryption

I just installed a SSL certificate. This certificate is encrypted with 2048 bit encryption.
However, the cypher is 128 bit encryption(or 40, or some other variation depending on the browser.)
It seems that there are two different types of encryption here. The "handshake" encryption of 2048 and the "over the wire" encryption of some magnitude smaller.
Do I have this right in theory? Can anyone explain it better?
I have been all over the Google and cannot find a clear explanation of the difference between the two.
There is a good entry in Wikipedia.
You are right, there are two kinds of encryption going on. The first one is asymmetric encryption or public key encryption - this is the one with the larger key. The second type is symmetric encryption with the smaller key.
The first type of encryption (asymmetric - larger key) is used to negotiate what type of symmetric encryption the client and the server will use. They'll also exchange the session key that they'll use. This is the handshake process and this is encrpyted using the asymmetric encryption
The session key is basically the key that they'll use when sending the real data, encrypted by whatever type they've decided on the handshake process. This is the symmetric encryption part.
It is true that symmetric encryption typically uses much fewer bits for its key length. The reason is because symmetric encryption is much stronger at a given number of bits.
Asymmetric encryption (where each side has a different key) is much harder to pull off. It is more computationally intensive and therefore only used for the handshake portion or for encrypting a symmetric key that the rest of the message uses.

Resources