Skip to content

Instantly share code, notes, and snippets.

@braydonf
Last active June 25, 2019 01:28
Show Gist options
  • Save braydonf/c7166ff7d06789d95f96022cac439a71 to your computer and use it in GitHub Desktop.
Save braydonf/c7166ff7d06789d95f96022cac439a71 to your computer and use it in GitHub Desktop.
libnettle aes 256 ctr example encrypt/decrypt
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <nettle/aes.h>
#include <nettle/ctr.h>
int main() {
struct aes256_ctx *ctx = malloc(sizeof(struct aes256_ctx));
uint8_t key[32] = {148,86,234,177,203,100,167,131,80,160,6,244,74,153,
168,217,127,183,61,14,9,191,195,249,145,5,251,218,
232,170,152,31};
// Encrypt
uint8_t ctr[16] = {188,14,95,229,78,112,182,107,34,206,248,225,52,22,16,183};
aes256_set_encrypt_key(ctx, key);
uint8_t *clear_text = "hello";
uint8_t *data = malloc(strlen(clear_text));
ctr_crypt(ctx, (nettle_cipher_func *)aes256_encrypt,
AES_BLOCK_SIZE, ctr,
strlen(clear_text), data, clear_text);
// Decrypt
uint8_t ctr2[16] = {188,14,95,229,78,112,182,107,34,206,248,225,52,22,16,183};
ctr_crypt(ctx, (nettle_cipher_func *)aes256_encrypt,
AES_BLOCK_SIZE, ctr2,
strlen(clear_text), data, data);
printf("data: %s\n", data);
return 0;
}
@sherifomran
Copy link

i have one question, what is the strength of nettle against openssl? we can get the same aes encryption with openssl but i don't see the strength of nettle except in encrypting the key transmission

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment