File: _crypto-test_verify_32.inc

package info (click to toggle)
tinyssh 20250501-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (77 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download | duplicates (2)
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
77
/* ----- verify, derived from supercop/crypto_verify/try.c */

/*
derived from djb work from lib25519/libntruprime
mj modifications:
- rename files to test-crypto.c and _crypto_<>.<>.inc
- fix compiler warnings
- include crypto.h
- use less rounds for valgrind test
- reformat using clang-format
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "crypto.h"

static unsigned char *test_verify_32_x;
static unsigned char *test_verify_32_y;

static void test_verify_32_check(void) {
    unsigned char *x = test_verify_32_x;
    unsigned char *y = test_verify_32_y;
    int r;

    secret(x, 32);
    secret(y, 32);
    r = crypto_verify_32(x, y);
    public(x, 32);
    public(y, 32);
    public(&r, sizeof r);

    if (r == 0) {
        if (memcmp(x, y, 32)) fail("failure: different strings pass verify\n");
    }
    else if (r == -1) {
        if (!memcmp(x, y, 32)) fail("failure: equal strings fail verify\n");
    }
    else { fail("failure: weird return value\n"); }
}

void test_verify_32_impl(long long impl) {
    unsigned char *x = test_verify_32_x;
    unsigned char *y = test_verify_32_y;

    if (targetn && atol(targetn) != impl) return;

    randombytes(x, 32);
    randombytes(y, 32);
    test_verify_32_check();
    memcpy(y, x, 32);
    test_verify_32_check();
    y[myrandom() % 32] = myrandom();
    test_verify_32_check();
    y[myrandom() % 32] = myrandom();
    test_verify_32_check();
    y[myrandom() % 32] = myrandom();
    test_verify_32_check();
}

static void test_verify_32(void) {
    if (targeto && strcmp(targeto, "verify")) return;
    if (targetp && strcmp(targetp, "32")) return;

    test_verify_32_x = callocplus(32);
    test_verify_32_y = callocplus(32);

    for (long long offset = 0; offset < 2; ++offset) {
        if (targetoffset && atol(targetoffset) != offset) continue;
        if (offset && valgrind) break;
        printf("verify_32 offset %lld\n", offset);
        forked(test_verify_32_impl, -1);
        ++test_verify_32_x;
        ++test_verify_32_y;
    }
}