Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:20 23 Dec 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : AES software solution

     Page 1 of 2    
Author Message
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 03:25pm 17 Dec 2024
Copy link to clipboard 
Print this post

Hi dear community,

As a loyal reader, I have a question today.
I encountered a problem.
Maybe you have a tip.

Initial situation:

I would like to build a small measuring device/flash tool for our company.
My concern is to have a cool menu design and a stable system.

The prerequisite is AES128 De/Encryption

I started with:
Bascom - good compiler - AES hardware available - 4" touch TFT not optimal
Annex32 - AES hardware available - 4" touch TFT good - somewhat unstable - private use only
MMBasic - AES hardware/software not available - 4" touch TFT very good - very stable - my favorite!

Has anyone seen an AES software solution on the Picomite?
Like "Tiny AES in C" and has it implemented somehow?
I also only need a slow speed to encrypt and decrypt.

OK, I'm probably thinking too simple....and it's much more complicated than that.

I'm about to take the Picomite and connect it serially to an Xmega32A... to have a nice interface and hardware AES.

That would make it expensive and complicated again.
A software solution would be better.  


Cheers Bishop
Edited 2024-12-18 01:25 by BishopXXL
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 03:46pm 17 Dec 2024
Copy link to clipboard 
Print this post

How long are the strings you need to encrypt/decrypt?
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4066
Posted: 05:07pm 17 Dec 2024
Copy link to clipboard 
Print this post

Looks plausible, there is a Python version of the algorithm here which shouldn't be impossible to port to MMBasic:

   https://github.com/rafael2903/AES-128-cipher/blob/main/AES.py

Not what you asked for, but I did  XXTEA/CBC/PKCS#7 in MMBasic a couple of years ago:

   https://github.com/thwill1000/mmbasic-sptools/blob/master/src/splib/crypt.inc

Best wishes,

Tom
Edited 2024-12-18 03:10 by thwill
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 05:14pm 17 Dec 2024
Copy link to clipboard 
Print this post

Happy to include it as a new MATH command

e.g.
Math aes128 encrypt cbc key(),in(),out()

The question is should key, in and out be strings or integers?
Strings are more logical but getting binary data into and out of strings is a bit of a pain
Integers are easier
dim key%(16)=(&H..   )

Perhaps the key should be an integer array and the data a string?

Need input from the OP as to a real world use case
Edited 2024-12-18 03:15 by matherp
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 06:02pm 17 Dec 2024
Copy link to clipboard 
Print this post

Hi everyone,
WOW that was quick!!!!!

Thank you for your feedback.

I can say the following about encryption.
The key is 16 bytes (AES128).
It would be that the key can also contain a &H00.

The data record to be encrypted can be a maximum of 32 bytes long.
If the length of the data is shorter, I have to fill the rest with &H04.
I'm not sure how this works with PKCS7 and whether you can bypass it manually.

Since &H00 can also occur in the data stream, a string would certainly not be optimal.
An Array() would be just great here!

Since I've already done the whole thing successfully in Bascom, I can do that
Post the command from the help here if it is allowed.

This statement of function uses the Xmega AES encryption engine to encrypt a block of data.

Syntax
AESENCRYPT  key, var , size
targ = AESENCRYPT ( key, var , size)


key -
The name of a label that contains 16 bytes of key data. Or an array holding 16 bytes of key data.

var -
A variable or array containing the data to be encrypted. When you use the statement, this variable will contain the encrypted data after the conversion.


size -
The number of bytes to encrypt. Encryption is done with blocks of 16 bytes. So the size should be a multiple of 16. If you supply only 14 bytes this is ok too, but the result will still be 16 bytes. It is important that your array is big enough to hold the result.
Without the full 16 byte result, you can not decrypt the data.
 
targ -
In case you use the function, this variable will hold the result.

https://avrhelp.mcselec.com/index.html?aesencrypt.htm

Thank you very much !!!!!  
Bishop
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 06:28pm 17 Dec 2024
Copy link to clipboard 
Print this post

Does this encryption map onto any of the tiny aes types? It would be tiny AES code that I would include
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 06:47pm 17 Dec 2024
Copy link to clipboard 
Print this post

Hello Peter,

Unfortunately, I didn't fully understand your question.
My English is very basic and I have to use Google Translate.
what do you mean by “tiny aes types”?
Do you mean my target - that would be a PIC24FJ128GA306-I/PT that receives the data.
I transfer a data set there ,that looks something like this.

' STX STX / CMD/Length/ Address 4 Bytes / Serial Number / CRC /
' 55 - 55 - 04 - 10 - 00 - 00 - ff - 00 - 41 - 36 - 32 - 33 - 34 - 34 - 32 - 20 - 20 - 20 - CRC - CRC - 04 - 04 - 04 to 32 bytes
'            \______________________________________________________________________________________________________/  AES verschlüsset
'



Cheers Bishop
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 07:34pm 17 Dec 2024
Copy link to clipboard 
Print this post

Tiny AES supports three types of encryption and decryption: CBC, EBC and CTR

Is one of these the same as AESENCRYPT?

If not, and I implement Tiny AES then it won't be compatible with your application.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6904
Posted: 08:40pm 17 Dec 2024
Copy link to clipboard 
Print this post

I found this, Peter:
  Quote  
The aesencrypt statement in Bascom-AVR supports three modes of operation: ECB (Electronic Codebook), CBC (Cipher Block Chaining), and CTR (Counter). Here’s a brief overview of each mode and their characteristics:

ECB (Electronic Codebook):
Each block of plaintext is encrypted independently using the same key.
Does not provide confidentiality or integrity protection for data.
Not recommended for use due to its insecurity.
CBC (Cipher Block Chaining):
Each block of plaintext is XORed with the previous ciphertext block before encryption.
Provides confidentiality and integrity protection for data.
Suitable for most encryption applications, but may not be suitable for random access encryption.
CTR (Counter):
An incrementing counter is encrypted to produce a sequence of blocks, which are then XORed with the plaintext.
Provides confidentiality and integrity protection for data.
Suitable for random access encryption, as it allows for efficient decryption of arbitrary blocks.
In the provided Bascom-AVR code snippets, you can see examples of each mode being used:

ECB: aes_encrypt(keydata, Ar(1), 32)
CBC: aes_encrypt(keydata, Ar(1) ^ ct[i], 32)
CTR: input[0]++; (incrementing counter)

When choosing an AES encryption mode, consider the specific requirements of your application. If you need confidentiality and integrity protection for sequential data, CBC might be a good choice. For random access encryption, CTR is more suitable. Avoid using ECB due to its insecurity.

Keep in mind that these modes are implemented in software using the Xmega’s AES hardware engine, and the specific implementation details may vary depending on the Bascom-AVR version and Xmega device used.


Looks like ECB.

Bascom AVR

.
Edited 2024-12-18 06:46 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 07:55am 18 Dec 2024
Copy link to clipboard 
Print this post

Hello,

thank you for your help!

@Peter
@Mixtel90

yes mick, you're right, it's "ECB"
I tried this again with an online tool!

I'll add a screenshot.

If it's not too complicated, I would be very happy to use it at some point
is accepted into the MATH family - THANKS to EVERYONE!!!





Cheers Bishop
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 08:17am 18 Dec 2024
Copy link to clipboard 
Print this post

Bishop

See V6.00.01RC9 for a full implementation. Please try it and report if works for you
Edited 2024-12-18 20:24 by matherp
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1301
Posted: 01:52pm 18 Dec 2024
Copy link to clipboard 
Print this post

I modified the demo program from here:
https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=17466&LastEntry=Y#231262
for ASCII input. Works perfectly on V6.00.01RC9/Pico 2.  

' Peters aes demo program for Picomites
' modified for ascii input by twofingers@TBS

Option explicit
CLS
Dim float key(15)=(&H2b,&H7e,&H15,&H16,&H28,&Hae,&Hd2,&Ha6,&Hab,&Hf7,&H15,&H88,
&H09,&Hcf,&H4f,&H3c)

Dim string in$="",out$="",comp$=""
Dim integer iv(15),i

'convert the key array to a string version of the key
For i=0 To 15
  iv(i)=15-i 'create an initialisation vector different from the default
Next
'convert the message to a string version of the message
Print
Input "Your message (16 min char, max 128): ", in$
Inc in$,String$(16-Len(in$) Mod 16,0)

' encrypt the message
Math aes128 encrypt ctr key(),in$,out$,iv()
Print in$
Print out$
'Math v_print out(),hex
'decrypt the message
Math aes128 decrypt ctr key(),out$,comp$,iv()
'Math v_print comp(),hex
Print out$
Print comp$
'check for errors
If in$<>comp$ Then Print "WTF" Else Print "Okay"

End

Regards
Michael
causality ≠ correlation ≠ coincidence
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4343
Posted: 02:16pm 18 Dec 2024
Copy link to clipboard 
Print this post

Hi Michael,

Why is the key in a float array. The values are integer (bytes).

Volhout
PicomiteVGA PETSCII ROBOTS
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 02:18pm 18 Dec 2024
Copy link to clipboard 
Print this post

It was done just to test you can use float, integer, or string
 
twofingers
Guru

Joined: 02/06/2014
Location: Germany
Posts: 1301
Posted: 02:32pm 18 Dec 2024
Copy link to clipboard 
Print this post

@Harm
I was also wondering, but didn't want to change it.
Regards
Michael
causality ≠ correlation ≠ coincidence
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 02:42pm 18 Dec 2024
Copy link to clipboard 
Print this post

Hi Peter,

Sorry , I answered here because you posted a new version there.

PicoMite V6.00.01 release candidates - please test thoroughly

Bishop
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 02:55pm 18 Dec 2024
Copy link to clipboard 
Print this post

Hi Peter
Yes, it works Peter, I will first divide the 32 bytes into two times 16 Bytes

Thanks for implemention !!!  

Bishop    
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 03:13pm 18 Dec 2024
Copy link to clipboard 
Print this post

Bishop

Which version are you testing on? I'll post an update for you to check my change
 
BishopXXL
Newbie

Joined: 13/01/2019
Location: Germany
Posts: 29
Posted: 03:27pm 18 Dec 2024
Copy link to clipboard 
Print this post

Peter,

i tested on:

PicoMite MMBasic RP2040 Edition V6.00.01RC9

Bishop
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9283
Posted: 03:33pm 18 Dec 2024
Copy link to clipboard 
Print this post

Will create a version
Edited 2024-12-19 01:37 by matherp
 
     Page 1 of 2    
Print this page
© JAQ Software 2024