onetimeauth8.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <string>
  2. using std::string;
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "crypto_onetimeauth_poly1305.h"
  6. #include "randombytes.h"
  7. main()
  8. {
  9. int clen;
  10. int i;
  11. for (clen = 0;clen < 10000;++clen) {
  12. unsigned char key_bytes[32];
  13. randombytes(key_bytes,sizeof key_bytes);
  14. string key((char *) key_bytes,sizeof key_bytes);
  15. unsigned char c_bytes[clen];
  16. randombytes(c_bytes,sizeof c_bytes);
  17. string c((char *) c_bytes,sizeof c_bytes);
  18. string a = crypto_onetimeauth_poly1305(c,key);
  19. try {
  20. crypto_onetimeauth_poly1305_verify(a,c,key);
  21. } catch(const char *s) {
  22. printf("fail %d %s\n",clen,s);
  23. return 100;
  24. }
  25. if (clen > 0) {
  26. size_t pos = random() % clen;
  27. c.replace(pos,1,1,c[pos] + 1 + (random() % 255));
  28. try {
  29. crypto_onetimeauth_poly1305_verify(a,c,key);
  30. printf("forgery %d\n",clen);
  31. } catch(const char *s) {
  32. ;
  33. }
  34. pos = random() % a.size();
  35. a.replace(pos,1,1,a[pos] + 1 + (random() % 255));
  36. try {
  37. crypto_onetimeauth_poly1305_verify(a,c,key);
  38. printf("forgery %d\n",clen);
  39. } catch(const char *s) {
  40. ;
  41. }
  42. }
  43. }
  44. return 0;
  45. }