summaryrefslogtreecommitdiff
path: root/nacl/tests/onetimeauth8.cpp
blob: ce554fb4977605178b1db47447ad9268bac668d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <string>
using std::string;
#include <stdio.h>
#include <stdlib.h>
#include "crypto_onetimeauth_poly1305.h"
#include "randombytes.h"

main()
{
  int clen;
  int i;
  for (clen = 0;clen < 10000;++clen) {
    unsigned char key_bytes[32];
    randombytes(key_bytes,sizeof key_bytes);
    string key((char *) key_bytes,sizeof key_bytes);
    unsigned char c_bytes[clen];
    randombytes(c_bytes,sizeof c_bytes);
    string c((char *) c_bytes,sizeof c_bytes);
    string a = crypto_onetimeauth_poly1305(c,key);
    try {
      crypto_onetimeauth_poly1305_verify(a,c,key);
    } catch(const char *s) {
      printf("fail %d %s\n",clen,s);
      return 100;
    }
    if (clen > 0) {
      size_t pos = random() % clen;
      c.replace(pos,1,1,c[pos] + 1 + (random() % 255));
      try {
        crypto_onetimeauth_poly1305_verify(a,c,key);
	printf("forgery %d\n",clen);
      } catch(const char *s) {
        ;
      }
      pos = random() % a.size();
      a.replace(pos,1,1,a[pos] + 1 + (random() % 255));
      try {
        crypto_onetimeauth_poly1305_verify(a,c,key);
	printf("forgery %d\n",clen);
      } catch(const char *s) {
        ;
      }
    }
  }
  return 0;
}