Author Topic: AES256 command line utility (win32) which does not add a header  (Read 1968 times)

0 Members and 1 Guest are viewing this topic.

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
There is a lot of these around but all seem to add a header. For example aescrypt.exe adds a load of stuff to the front, starting with "AES" :)

I am looking for a bidirectional utility, password on the command line in hex, which just encrypts say 512 bytes into 512 bytes.

There are plenty of AES C sources so I can have a go at freelancer.com, if nobody here can think of something.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5029
  • Country: ro
  • .
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #2 on: August 16, 2022, 01:40:51 pm »
Thank you... yes indeed I came across some, but for a command line utility I am looking for a standalone win32 .exe, or, if I am going to give $50 to somebody (usually in Ukraine) to knock one up, C or C++ source.

Right now I am implementing https://github.com/kokke/tiny-AES-c/blob/master/aes.c on my target, and will test it against the test vectors, and if it works I can get a .exe done.

I was going to use the 32F417 hardware AES function but I don't actually need the speed, and doing so would preclude one day using a F407 (which doesn't have hardware crypto) or one of the chinese 407 knockoffs. ST do a lib which uses the hardware AES but it is so convoluted it would take me hours just to extract the bit I want :)

Incidentally are there win32 JS or PHP compilers which produce a standalone .exe?
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5029
  • Country: ro
  • .
Re: AES256 command line utility (win32) which does not add a header
« Reply #3 on: August 16, 2022, 01:59:32 pm »
No, can't make a standalone exe from php,  but you can run php scripts from command line ex php.exe script.php   and you can pick command line arguments / parameters by parsing the $argv array to get your input file, output file, whatever

Go / Golang is also quite easy to learn and the positive about it is that it builds static executables ... Here's a minimal example of AES-256 encryption and decryption : https://gist.github.com/yingray/57fdc3264b1927ef0f984b533d63abab


 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
Re: AES256 command line utility (win32) which does not add a header
« Reply #4 on: August 16, 2022, 04:29:44 pm »
Quote from: peter-h
Thank you... yes indeed I came across some, but for a command line utility I am looking for a standalone win32 .exe, or, if I am going to give $50 to somebody (usually in Ukraine) to knock one up, C or C++ source.

Right now I am implementing https://github.com/kokke/tiny-AES-c/blob/master/aes.c on my target, and will test it against the test vectors, and if it works I can get a .exe done.

Here is a self contained (no DLLs required) Windows command line program that does AES256 CTR using the C code you have specified.

Usage: AES256 infile outfile key

key is 64 hexadecimal characters

Encrypt and decrypt are symmetrical for CTR, so specifying the mode is not required.

I can modify to your spec and provide source code for approximately "Ukraine pricing".
« Last Edit: August 16, 2022, 04:52:33 pm by oPossum »
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
Re: AES256 command line utility (win32) which does not add a header
« Reply #5 on: August 16, 2022, 07:11:13 pm »
Update with support for ECB, CBC and CTR modes. Key length of 128, 192 or 256.

Usage: AESnnn mode key infile outfile

mode: ECBE, ECBD, CBCE, CBCD, or CTR
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #6 on: August 16, 2022, 07:13:17 pm »
Wow - amazing! Thank you.

Unfortunately, my AV software detected a virus in the zipfile and deleted it as soon as I downloaded it.

I had already given it to (yes...) a Ukrainian on freelancer.com who has written various utils for me before. The spec was

AES256.EXE

Requirements:

Normal win32 command line executable, standalone, no dependencies.
Runs in a win7-32, win7-64 (or higher) command line box.

C/C++ preferred. Needs to be possible to build a linux version later so no strange Microsoft dependencies.

aes256.exe e|d keyfile inputfile outputfile

All 4 parms mandatory.

e = encrypt
d = decrypt
Not case sensitive.

keyfile is the key and the file must be 32 bytes in size.

All 3 files are binary data.

Speed not important.

C source for aes256 provided. Note that the selection of aes256 (not aes192 or aes128) is already done in the .h file.
Same for CBC mode. Not ECB or CTR.

Test code provided - does 64 byte blocks. Final program needs to be tested with the data shown.

AES256 does 16 byte blocks, and uses a 32 byte key.

If input file size is not a multiple of 16 bytes, append 0x00 bytes until it is.
Output file size will be same as input file size.

If 'd' mode in use, input file MUST be a multiple of 16 bytes in size.

Clear error messages for each invalid parameter please.


I got it running on my target too, as an RTOS task. It does 512 bytes in 5ms which is amazingly slow. OK for the intended purpose, but pretty slow for use for TLS session encryption. The 32F417 hardware method is thus very desirable.

EDIT: your 2nd PDF does not show a virus. I guess it was a spurious detection. Are all your files pure binary i.e. keyfile = 32 bytes? I also wonder what is the correct IV. It seems to be 0x00 0x01 0x02...
« Last Edit: August 16, 2022, 07:22:57 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: AES256 command line utility (win32) which does not add a header
« Reply #7 on: August 16, 2022, 07:38:47 pm »
CAREFUL! Unless you really know what you are doing, can provide a key from a CSPRNG (the exact key fed directly to AES, not a passphrase or KDF!) and have a secure way of conveying that key to the recipient: this is almost surely not what you want to do. Chances that more than one of these conditions are met are very low.

The issue is that you need a means to determine initialization vector.(1) It must be stored explicitly or may be derived from a password. However, for password-based keys you must have salt or the password would be vulnerable to dictionary attacks. So you end up with the requirement to store the IV or the salt.

If you want to try, see the enc command in OpenSSL (home). But I implore you: do not ever deploy that to production or, worse, release as a product to other people without first consulting a person with sufficient crypto expertise.
Code: [Select]
openssl enc -aes256 -pbkdf2 -iter 1 -nosalt -pass file:KEYFILE -in INFILE -out OUTFILEWhere INFILE is the input file, OUTFILE is the output file, KEYFILE is a file with CSPRNG-generated value (at least 32 octets).(2) To decrypt:
Code: [Select]
openssl enc -d -aes256 -pbkdf2 -iter 1 -nosalt -pass file:KEYFILE -in INFILE -out OUTFILE
Obtaining OpenSSL on Windows may be a bit of a trouble, as the upstream is not offering binaries. As far as I can tell, cURL (home) offers the command in its Windows relase. If not, see if MSYS2 can’t provide it. If not, MinGW may be used to build it, or perhaps accessible over WSL.

The initial warning applies to any other software as well.


(1) Or a nonce in modes like CTR, CCM or GCM. The distinction is irrelevant in this post.
(2) That is not the key yet — it will still be fed to a KDF, but in this case with no implications for security.
People imagine AI as T1000. What we got so far is glorified T9.
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
Re: AES256 command line utility (win32) which does not add a header
« Reply #8 on: August 16, 2022, 07:47:12 pm »
aes256.exe e|d keyfile inputfile outputfile

All 4 parms mandatory.

e = encrypt
d = decrypt
Not case sensitive.

keyfile is the key and the file must be 32 bytes in size.

Updated per spec.

IV is all 0
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
Re: AES256 command line utility (win32) which does not add a header
« Reply #9 on: August 16, 2022, 07:48:57 pm »
Are all your files pure binary i.e. keyfile = 32 bytes? I also wonder what is the correct IV. It seems to be 0x00 0x01 0x02...

The first 2 posted take the key on the command line. IV is all 0.
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #10 on: August 16, 2022, 08:23:44 pm »
Virus detected in last zip.

Yes I am aware of the weaknesses. I've been involved to some extent in crypto for about 30 years. Used to make some DES line encryptor boxes for, apparently, banking... Z180, 14MHz, 38k baud full duplex. Did an ASIC for another crypto product.

In this application, the IV is fixed (the value used for the standard test vectors seems to be 0 1 2 3 ... 0x0f, so why not use that?) and the key is ... the key. The key is also fixed. It will be used to encrypt a firmware file which is then decrypted on the target. As you will all point out, the key must be stored on the target and is thus vulnerable. But not that vulnerable, if the CPU has security fuses blown. The only really good solution for this is to use a smartcard chip which does the decryption on-chip, and I am not doing that because the application doesn't warrant it. So it is really just to block casual disassembly of a firmware update file, because they are bound to fall into the wrong hands, and of course that file (if it is updating the entire CPU FLASH) will contain any keys. I am using CBC over large blocks.

A dictionary attack on the 32 byte key will not be possible.

One interesting lesson from this exercise is how slow TLS (HTTPS) must be if the session crypto is done in software. I know one can trade speed for size (and even 25 years ago people were doing DES at 1mbyte/sec on an 80x86 at 50MHz, by using megabyte-sized precomputed lookup tables) but that AES code doesn't look too bad to me, and it is still doing 1 byte in 50us, which is unsurprising for the amount of code, but would slow down ETH data rate to below 100kbyte/sec which, admittedly, almost nobody would notice on an "IOT box" doing HTTPS to a private server ;) But using the 32F417 hardware crypto is worthwhile in case something faster is needed one day.

The TLS setup (EC or RSA) takes a couple of seconds - long time even on a 168MHz 32F4. This is OK however.

FWIW attached is the MbedTLS aes.c, and an ST-supplied version using the 32F417 hardware. Both too bloated for me to follow easily.



« Last Edit: August 16, 2022, 08:33:35 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
Re: AES256 command line utility (win32) which does not add a header
« Reply #11 on: August 16, 2022, 08:37:19 pm »
Changed some build settings to eliminate false positive by AV.
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #12 on: August 16, 2022, 08:51:29 pm »
Still detecting a virus...

Is there a problem with using an easy to guess IV? A google search is silent on specifics - just the usual clickbait copied from one website to another.

Digging around, it looks like most people are generating (for CBC) the IV and the key in one go, using whatever method they use for the key.
« Last Edit: August 16, 2022, 09:03:03 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1417
  • Country: us
  • Very dangerous - may attack at any time
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: AES256 command line utility (win32) which does not add a header
« Reply #14 on: August 16, 2022, 09:36:57 pm »
Why do you even bother?

Most encryption schemes do not provide authentication, so you do not protect your device against malicious code. The cost of such an attack is greater, but that’s not the same meaning of word “protection” as used in cryptography. What you achieve is primarily separating the users from knowledge of what the device is doing.

Even worse: in the scenario you described the key is hardcoded. That means users can’t upload their own firmware, you losing the key prevents updates, and there is no mechanism to address encryption key leaks.
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #15 on: August 17, 2022, 05:15:04 am »
There is a CRC (with a nonstandard initialiser) also, to prevent malicious code upload. Obviously there are more complicated ways of doing that too...

I am not describing the full reasoning behind this :)

opossum - your file comes up positive on some online scanners e.g.



https://www.virustotal.com/gui/home/upload

Someone said to me that any code containing aes256 flags up as a virus. EDIT: It is MS VC whose runtimes are triggering this.
« Last Edit: August 17, 2022, 07:58:34 am by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline peter-hTopic starter

  • Super Contributor
  • ***
  • Posts: 3698
  • Country: gb
  • Doing electronics since the 1960s...
Re: AES256 command line utility (win32) which does not add a header
« Reply #16 on: August 17, 2022, 02:59:41 pm »
This job got a bit more complicated when I realised that the CBC mode must be done over 512 byte blocks only, not over the whole file which is what everybody else does. This is because I program the CPU FLASH from top down, doing the final boot block last (and doing it in one go - the 32k boot block takes about 300ms, so that is the sole "bricking window").

Anyway I have someone trying to knock this together now.

Thank you all for your suggestions.

Yes indeed if the key is lost then you can't upgrade the firmware, except with a debugger, which you can't do if you have set the security fuses to "max" :) But if you have set the security fuses to "max" you cannot upgrade the firmware anyway. There is no free lunch. What this scheme does give you is protection from disassembly of firmware distributed openly, which is actually really important because you never know who you are dealing with remotely. Whenever you bring out a new product, the first sales will be to competitors (in disguise of course). And even if somebody has got his hands on the physical product he still has quite a bit of hassle even if you didn't use any security fuses. And if you block SWD debugging then he has even more hassle. If the product has a huge value then the chinese will try to rip it off, and will succeed unless you have some sort of tamperproof key storage, which is more money...

FWIW I had a look at the aes256 code which comes with MbedTLS and while it is pretty opaque (to me) it uses much bigger tables, so I would expect it to be a lot faster than the 5ms=512 bytes of the tiny aes code I am using.
« Last Edit: August 17, 2022, 03:33:51 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: AES256 command line utility (win32) which does not add a header
« Reply #17 on: August 30, 2022, 12:02:47 pm »
Obtaining OpenSSL on Windows may be a bit of a trouble, as the upstream is not offering binaries

OpenSSL for Windows is easily installable via chocolatey (https://community.chocolatey.org/packages/openssl) and winget (https://winget.run/pkg/ShiningLight/OpenSSL) and probably other package managers as well.
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: AES256 command line utility (win32) which does not add a header
« Reply #18 on: August 30, 2022, 03:24:28 pm »
I though that the assumption, that software should be obtained from trusted sources, is implicit and obvious. |O

Yes, of course anyone can just search for “openssl windows installer” and find some link.  :palm:

« Last Edit: August 30, 2022, 03:30:59 pm by golden_labels »
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline gmb42

  • Frequent Contributor
  • **
  • Posts: 294
  • Country: gb
Re: AES256 command line utility (win32) which does not add a header
« Reply #19 on: August 31, 2022, 11:18:43 am »
I though that the assumption, that software should be obtained from trusted sources, is implicit and obvious. |O

Yes, of course anyone can just search for “openssl windows installer” and find some link.  :palm:

I'm missing something here.

Your initial issue was about the lack of pre-built binaries for Windows
Quote
Obtaining OpenSSL on Windows may be a bit of a trouble, as the upstream is not offering binaries
which is a bit odd as the upstream source (OpenSSL themselves) doesn't offer any pre-built binaries at all for any platform.

Obviously then, users that don't build OpenSSL themselves have to rely on the various distributions (and I include Windows here) to produce a "safe" package to install.

Why do you think the Windows packages of OpenSSL (personally I've only ever used those from ShiningLight) installed via a trusted package manager would be less secure than an OpenSSL package for [random OS] installed from [random distribution] via their trusted package manager?
 

Offline golden_labels

  • Super Contributor
  • ***
  • Posts: 1209
  • Country: pl
Re: AES256 command line utility (win32) which does not add a header
« Reply #20 on: August 31, 2022, 03:43:32 pm »
Obviously then, users that don't build OpenSSL themselves have to rely on the various distributions (and I include Windows here) to produce a "safe" package to install.
Windows can’t be included, as Windows is not an entity. It’s a product. The entity is Microsoft, and they are not providing OpenSSL.

Why do you think the Windows packages of OpenSSL (personally I've only ever used those from ShiningLight) installed via a trusted package manager would be less secure than an OpenSSL package for [random OS] installed from [random distribution] via their trusted package manager?
How does a package manager being trusted has, by itself, anything to do with trust in the packages it can install? Considering that those are random files provided by anonymous persons, even less trustworthy than e.g. cesspool like AUR?(1)

Are you aware, that you linked a binary that’s EOL’d, has over half hundred known vulnerabilities (including RCE), and can’t even deal with some commonly encountered situations a program may encounter in 2020s?


(1) I know AUR does not provide packages, but close enough for the argument. There are custom repos that build and publish pre-built packages from it (e.g. Chaotic).
People imagine AI as T1000. What we got so far is glorified T9.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: AES256 command line utility (win32) which does not add a header
« Reply #21 on: September 01, 2022, 06:04:06 am »
There are plenty of AES C sources so I can have a go at freelancer.com, if nobody here can think of something.

I needed to add some encryption of a file, just not to make it plaintext and i said, what the heck let's just use AES i'm sure to find something.
I don't know if you do Qt but it is just a few lines of code (get the stream from a QFile(path), call the oneliner to QAESEncryption, write the stream to the output QFile, using this library https://github.com/bricke/Qt-AES
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf