summaryrefslogtreecommitdiff
path: root/src/libsodium/crypto_verify/try.c
blob: 06684e781aad64287f40e0c3a2bf02445675dc12 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * crypto_verify/try.c version 20090118
 * D. J. Bernstein
 * Public domain.
 */

#include <stdlib.h>
#include "crypto_verify.h"
#include "windows/windows-quirks.h"

extern unsigned char *alignedcalloc(unsigned long long);

const char *primitiveimplementation = crypto_verify_IMPLEMENTATION;

static unsigned char *x;
static unsigned char *y;

void preallocate(void)
{
}

void allocate(void)
{
  x = alignedcalloc(crypto_verify_BYTES);
  y = alignedcalloc(crypto_verify_BYTES);
}

void predoit(void)
{
}

void doit(void)
{
  crypto_verify(x,y);
}

static const char *check(void)
{
  int r = crypto_verify(x,y);
  if (r == 0) {
    if (memcmp(x,y,crypto_verify_BYTES)) return "different strings pass verify";
  } else if (r == -1) {
    if (!memcmp(x,y,crypto_verify_BYTES)) return "equal strings fail verify";
  } else {
    return "weird return value from verify";
  }
  return 0;
}

char checksum[2];

const char *checksum_compute(void)
{
  long long tests;
  long long i;
  long long j;
  const char *c;

  for (tests = 0;tests < 100000;++tests) {
    for (i = 0;i < crypto_verify_BYTES;++i) x[i] = rand();
    for (i = 0;i < crypto_verify_BYTES;++i) y[i] = rand();
    c = check(); if (c) return c;
    for (i = 0;i < crypto_verify_BYTES;++i) y[i] = x[i];
    c = check(); if (c) return c;
    y[rand() % crypto_verify_BYTES] = rand();
    c = check(); if (c) return c;
    y[rand() % crypto_verify_BYTES] = rand();
    c = check(); if (c) return c;
    y[rand() % crypto_verify_BYTES] = rand();
    c = check(); if (c) return c;
  }

  checksum[0] = '0';
  checksum[1] = 0;
  return 0;
}