3

novice to aes. in reading http://en.wikipedia.org/wiki/AES_implementations, I am a bit surprised. I should need just one function

char16 *aes128(char16 key, char16 *secrets, int len);

where char16 is an 8*16=128bit character type. and, presumably, ignoring memory leaks,

assert( bcmp( anystring, aes128(anykey, aes128(anykey, anystring, len), len )==0 );

I am looking over the description of the algorithm on wikipedia, and although I can see myself making enough coding mistakes to take me a few days to debug my own implementation, it does not seem too complex. maybe 100 lines? I did see versions in C#, such as Using AES encryption in C#. that seem themselves almost as long as the algorithm itself. earlier recommendations on stackoverflow mostly recommend the use of individual functions inside larger libraries, but it would be nice to have a go-to function for this task that one could compile into one's code.

so, is AES implementation too complex to be for the faint of heart? or is it reasonably short and simple?

how many lines does a C implementation take? is there a self-contained aes128() C function already in free form somewhere for the taking?

another question: is each block independently encoded? presumably, it would strengthen the encryption if the first block would create a salt that the second block would then use. otoh, this would mean that disk corruption of one block would make every subsequent block undecryptable.

/iaw

Community
  • 1
  • 1
ivo Welch
  • 2,427
  • 2
  • 23
  • 31

3 Answers3

2

You're not seeing a single function like you expect because there are so many options. For example, the block encoding mechanism you described (CBC) is just one option or mode in AES encryption. See here for more information: http://www.heliontech.com/aes_modes_basic.htm

The general rule of thumb in any language is: Don't reinvent something that's already been done and done well. This is especially true in anything related to cryptography.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
  • thank you. hmmm...has someone posted a small stand-alone function that does a CBC mode implementation, which thus reduces the generality and thus length of the problem? my good reason to want this is that I want to use this in an embedded system with minimal porting and learning curve. I just need something that "works." I even considered the sequential random number generator with a seed and then XOR-ing the string as I go along...probably a terrible idea, but very simple to implement. – ivo Welch Jun 20 '14 at 05:05
  • well, reading on wikipedia, using not only the C standard rand() function, but even a better mersenne twister would still be a bad idea. maybe MTGP, but then I am back in a world in which I need to do work I should leave to the experts... – ivo Welch Jun 20 '14 at 05:18
2

ok, so I found a reasonable standalone implementation:

http://www.literatecode.com/aes256

About 400 lines. I will probably use this one.

hope it helps others, too.

ivo Welch
  • 2,427
  • 2
  • 23
  • 31
1

well using just the AES function is basically insecure as any block X will always be encoded to block Y with key K which is too much information to give an attacker... (according to cryptographers)

so you use some method to change the block cipher at each block. you can use a nonce or Cipher Block Chaining or some other method. but there is a pretty good example on wikipedia (the penguin picture): http://en.wikipedia.org/wiki/Electronic_code_book#Electronic_codebook_.28ECB.29

so in short you can implement AES in one function that is secure (as a block cipher), but it isn't secure if you have data that is longer than 16 bytes.

also AES is fairly complex because of all the round keys... I wouldn't really want to implement it, especially with all of the many good implementations around, but I guess it wouldn't be so bad if you had a good reason to do it.

so in short, to construct a secure stream cipher from a block cipher you need to adopt some strategy to change the effective key along the stream.

Grady Player
  • 14,399
  • 2
  • 48
  • 76