2

I'm using aes-256-gcm encryption. I used a sample key provided in the docs and it works fine.

I also tried a key generated from here and it works fine. There's no option to specify gcm here yet the key generated works fine.

I'm trying to generate the key using openssl using the command

openssl enc -aes-256-gcm -k secret -P -md sha1

inspired from here but I'm getting the error

enc: AEAD ciphers not supported

How can I generate the keys in .pem format using a command line utility?

An example of the key that works from the docs is as follows

4u7x!A%D*G-KaPdRgUkXp2s5v8y/B?E(

3
  • Here's why openssl does not support it: github.com/openssl/openssl/issues/12220
    – user284677
    Nov 6, 2022 at 13:30
  • @Spyros how can I generate it then? Nov 6, 2022 at 13:34
  • gcm is an encryption mode, I'm not sure I understand the question. What are you trying to achieve?
    – user284677
    Nov 6, 2022 at 14:10

2 Answers 2

5

There are a few things to unpack here.

First, regardless of the AES mode being used (GCM, CBC, ECB, CTR, etc), if you are using AES-256, then the key must be 256 bits in length. To generate a random 256-bit key, encoded in hexadecimal format, you can use the following command:

head /dev/urandom | sha256sum

This produces a random 256-bit key, such as:

3fd00454580de44ea216d8b7b234267a2a6a6aec7e56d2b38e641a45597af0f2

Now, you can use this key with an iv with openssl to encrypt some plain text using aes-256-cbc, like so:

echo -n 'hello world' |  openssl aes-256-cbc -e -K 3fd00454580de44ea216d8b7b234267a2a6a6aec7e56d2b38e641a45597af0f2 -iv 302775dfc35a35c8081bbc6fdeacbd86 | xxd -p

This produces the cyphertext:

7d5b44298bf959af149a0086d79334e6

Now, you can decrypt the ciphertext using the same key and iv, to produce the original plainext, like so:

echo -n '7d5b44298bf959af149a0086d79334e6' | xxd -p -r | openssl aes-256-cbc -d -K 3fd00454580de44ea216d8b7b234267a2a6a6aec7e56d2b38e641a45597af0f2 -iv 302775dfc35a35c8081bbc6fdeacbd86

This produces:

hello world

However, you asked about using openssl to do AES encryption using GCM mode, not CBC mode. Unfortunately, this is not possible with the command line interface for openssl, because AES-GCM is not supported. For more info on this, see https://www.openssl.org/docs/man1.1.1/man1/enc.html, and scroll down to the section 'supported ciphers, and note where it reads:

The enc program does not support authenticated encryption modes like CCM and GCM, and will not support such modes in the future. The enc interface by necessity must begin streaming output (e.g., to standard output when -out is not used) before the authentication tag could be validated, leading to the usage of enc in pipelines that begin processing untrusted data and are not capable of rolling back upon authentication failure.

1
  • 3
    Instead of using a shasum to generate a fixed-sized key, we could pick the correct number of bytes from urandom and then encode them, or just use openssl rand -hex 32 (length specified in bytes).
    – amon
    Nov 6, 2022 at 15:42
3

We speak about key generation only then, when it is hard to do that manually. For instance, to create a pair of keys for RSA or for a specific ECC curve like 25519, one needs to do a lot of computations with relatively big numbers.

Keys for AES-256 can easily be created manually. Any 256 bit long bit sequence can be used as an AES-256 key. You don't have to use any generators and create a valid key manually. Human generated keys have usually less entropy than the generated by some quality random generator that is based on quality entropy source, that's why it is recommended to use a generator.

PEM format is used for PKI only. PKI uses asymmetric encryption. Where as AES is symmetric algorithm. In case of AES there is no private key, public key, certificates, that's why PEM is not applicable in case of AES.

9
  • So in what file format can I store the key? Nov 7, 2022 at 14:16
  • @WilfredAlmeida: It depends on how are you going to use it. If your application reads the key in binary format from file, store it as binary in the file. If your application reads it as binary from the database, store it as a BLOB in the database. If the application expects the key as a text, e.g. in the hexadecimal or in the Base64 format, use this format.
    – mentallurg
    Nov 7, 2022 at 17:35
  • I need it as a text. So is it fine if I store it as base64 in a txt file? Or a .key file. Nov 8, 2022 at 6:38
  • To keep it as a text, you can use hexadecimal or Base64 format. Why do you ask about format?
    – mentallurg
    Nov 8, 2022 at 9:39
  • My concern is not the format. It's the type of file I should the store the key in. Nov 8, 2022 at 9:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .