summaryrefslogtreecommitdiff
path: root/nacl/tests/onetimeauth7.c
blob: 349b8751fcf63fb74f810aa84569215bdaf82a96 (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
#include <stdio.h>
#include <stdlib.h>
#include "crypto_onetimeauth_poly1305.h"
#include "randombytes.h"

unsigned char key[32];
unsigned char c[10000];
unsigned char a[16];

main()
{
  int clen;
  int i;
  for (clen = 0;clen < 10000;++clen) {
    randombytes(key,sizeof key);
    randombytes(c,clen);
    crypto_onetimeauth_poly1305(a,c,clen,key);
    if (crypto_onetimeauth_poly1305_verify(a,c,clen,key) != 0) {
      printf("fail %d\n",clen);
      return 100;
    }
    if (clen > 0) {
      c[random() % clen] += 1 + (random() % 255);
      if (crypto_onetimeauth_poly1305_verify(a,c,clen,key) == 0) {
        printf("forgery %d\n",clen);
        return 100;
      }
      a[random() % sizeof a] += 1 + (random() % 255);
      if (crypto_onetimeauth_poly1305_verify(a,c,clen,key) == 0) {
        printf("forgery %d\n",clen);
        return 100;
      }
    }
  }
  return 0;
}