box8.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "crypto_box.h"
  4. #include "randombytes.h"
  5. unsigned char alicesk[crypto_box_SECRETKEYBYTES];
  6. unsigned char alicepk[crypto_box_PUBLICKEYBYTES];
  7. unsigned char bobsk[crypto_box_SECRETKEYBYTES];
  8. unsigned char bobpk[crypto_box_PUBLICKEYBYTES];
  9. unsigned char n[crypto_box_NONCEBYTES];
  10. unsigned char m[10000];
  11. unsigned char c[10000];
  12. unsigned char m2[10000];
  13. int main()
  14. {
  15. int mlen;
  16. int i;
  17. int caught;
  18. for (mlen = 0;mlen < 1000 && mlen + crypto_box_ZEROBYTES < sizeof m;++mlen) {
  19. crypto_box_keypair(alicepk,alicesk);
  20. crypto_box_keypair(bobpk,bobsk);
  21. randombytes(n,crypto_box_NONCEBYTES);
  22. randombytes(m + crypto_box_ZEROBYTES,mlen);
  23. crypto_box(c,m,mlen + crypto_box_ZEROBYTES,n,bobpk,alicesk);
  24. caught = 0;
  25. while (caught < 10) {
  26. c[random() % (mlen + crypto_box_ZEROBYTES)] = random();
  27. if (crypto_box_open(m2,c,mlen + crypto_box_ZEROBYTES,n,alicepk,bobsk) == 0) {
  28. for (i = 0;i < mlen + crypto_box_ZEROBYTES;++i)
  29. if (m2[i] != m[i]) {
  30. printf("forgery\n");
  31. return 100;
  32. }
  33. } else {
  34. ++caught;
  35. }
  36. }
  37. }
  38. return 0;
  39. }