Skip to content

Instantly share code, notes, and snippets.

@hirokuma
Created July 28, 2018 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirokuma/f9ee0da7bd428458662473964b57fa6c to your computer and use it in GitHub Desktop.
Save hirokuma/f9ee0da7bd428458662473964b57fa6c to your computer and use it in GitHub Desktop.
sodium_mbedtls_chacha20.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/sodium/crypto_stream_chacha20.h"
#include "include/sodium/randombytes.h"
#include "include/mbedtls/chacha20.h"
static void dump(const unsigned char *pData, size_t Len)
{
for (size_t lp = 0; lp < Len; lp++) {
printf("%02x", pData[lp]);
}
printf("\n");
}
int main(void)
{
int ret;
unsigned char result[32];
unsigned char nonce[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
const unsigned char key[32] = {
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
};
crypto_stream_chacha20(result, sizeof(result), nonce, key);
dump(result, sizeof(result));
memset(result, 0, sizeof(result));
crypto_stream_chacha20(result, sizeof(result), nonce, key);
dump(result, sizeof(result));
memset(result, 0, sizeof(result));
unsigned char nonce2[12] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8 };
unsigned char *dummy = (unsigned char *)malloc(sizeof(result));
memset(dummy, 0, sizeof(result));
ret = mbedtls_chacha20_crypt(key, nonce2, 0, sizeof(result), dummy, result);
if (ret) { printf("bad\n"); return -1; }
dump(result, sizeof(result));
memset(result, 0, sizeof(result));
ret = mbedtls_chacha20_crypt(key, nonce2, 0, sizeof(result), dummy, result);
if (ret) { printf("bad2\n"); return -1; }
dump(result, sizeof(result));
free(dummy);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment