secretbox8.c 1007 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "crypto_secretbox.h"
  4. #include "randombytes.h"
  5. unsigned char k[crypto_secretbox_KEYBYTES];
  6. unsigned char n[crypto_secretbox_NONCEBYTES];
  7. unsigned char m[10000];
  8. unsigned char c[10000];
  9. unsigned char m2[10000];
  10. int main()
  11. {
  12. int mlen;
  13. int i;
  14. int caught;
  15. for (mlen = 0;mlen < 1000 && mlen + crypto_secretbox_ZEROBYTES < sizeof m;++mlen) {
  16. randombytes(k,crypto_secretbox_KEYBYTES);
  17. randombytes(n,crypto_secretbox_NONCEBYTES);
  18. randombytes(m + crypto_secretbox_ZEROBYTES,mlen);
  19. crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k);
  20. caught = 0;
  21. while (caught < 10) {
  22. c[random() % (mlen + crypto_secretbox_ZEROBYTES)] = random();
  23. if (crypto_secretbox_open(m2,c,mlen + crypto_secretbox_ZEROBYTES,n,k) == 0) {
  24. for (i = 0;i < mlen + crypto_secretbox_ZEROBYTES;++i)
  25. if (m2[i] != m[i]) {
  26. printf("forgery\n");
  27. return 100;
  28. }
  29. } else {
  30. ++caught;
  31. }
  32. }
  33. }
  34. return 0;
  35. }