Skip to content

Commit

Permalink
Various fixes and improvements (#21)
Browse files Browse the repository at this point in the history
* fix potential write to read only memory

* fix infinite loop

* move macros to the source file, make PUT a statement rather than a block
  • Loading branch information
Wirtos committed Apr 7, 2021
1 parent 9836e93 commit 9a9e55b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 9 deletions.
6 changes: 6 additions & 0 deletions tgcrypto/aes256.c
Expand Up @@ -20,6 +20,12 @@

#include "aes256.h"

#define LROTL(x) (((x) << 8) | ((x) >> 24))
#define LROTR(x) (((x) >> 8) | ((x) << 24))
#define SWAP(x) ((LROTL((x)) & 0x00ff00ff) | (LROTR((x)) & 0xff00ff00))
#define GET(p) SWAP(*((uint32_t *)(p)))
#define PUT(ct, st) (*((uint32_t *)(ct)) = SWAP((st)))

static const uint32_t Te0[256] = {
0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554,
0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a,
Expand Down
6 changes: 0 additions & 6 deletions tgcrypto/aes256.h
Expand Up @@ -28,12 +28,6 @@
#define AES_BLOCK_SIZE 16
#define EXPANDED_KEY_SIZE 60

#define LROTL(x) (((x) << 8) | ((x) >> 24))
#define LROTR(x) (((x) >> 8) | ((x) << 24))
#define SWAP(x) ((LROTL((x)) & 0x00ff00ff) | (LROTR((x)) & 0xff00ff00))
#define GET(p) SWAP(*((uint32_t *)(p)))
#define PUT(ct, st) {*((uint32_t *)(ct)) = SWAP((st));}

void aes256_set_encryption_key(const uint8_t key[32], uint32_t expandedKey[60]);

void aes256_set_decryption_key(const uint8_t key[32], uint32_t expandedKey[60]);
Expand Down
2 changes: 1 addition & 1 deletion tgcrypto/cbc256.h
Expand Up @@ -21,6 +21,6 @@
#ifndef CBC256_H
#define CBC256_H

uint8_t *cbc256(const uint8_t in[], uint32_t length, const uint8_t key[32], const uint8_t iv[16], uint8_t encrypt);
uint8_t *cbc256(const uint8_t in[], uint32_t length, const uint8_t key[32], uint8_t iv[16], uint8_t encrypt);

#endif // CBC256_H
3 changes: 2 additions & 1 deletion tgcrypto/ctr256.c
Expand Up @@ -41,7 +41,8 @@ uint8_t *ctr256(const uint8_t in[], uint32_t length, const uint8_t key[32], uint
*state = 0;

if (*state == 0) {
for (k = AES_BLOCK_SIZE - 1; k >= 0; --k)
k = AES_BLOCK_SIZE;
while(k--)
if (++iv[k])
break;

Expand Down
2 changes: 1 addition & 1 deletion tgcrypto/ctr256.h
Expand Up @@ -21,6 +21,6 @@
#ifndef CTR256_H
#define CTR256_H

uint8_t *ctr256(const uint8_t in[], uint32_t length, const uint8_t key[32], const uint8_t iv[16], uint8_t *state);
uint8_t *ctr256(const uint8_t in[], uint32_t length, const uint8_t key[32], uint8_t iv[16], uint8_t *state);

#endif // CTR256_H

0 comments on commit 9a9e55b

Please sign in to comment.