summaryrefslogtreecommitdiff
path: root/regress/misc/fuzz-harness/sig_fuzz.cc
blob: dd1fda091f491ca0998679b645b92ca77ff8ddbe (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
// cc_fuzz_target test for public key parsing.

#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

extern "C" {

#include "includes.h"
#include "sshkey.h"
#include "ssherr.h"

static struct sshkey *generate_or_die(int type, unsigned bits) {
  int r;
  struct sshkey *ret;
  if ((r = sshkey_generate(type, bits, &ret)) != 0) {
    fprintf(stderr, "generate(%d, %u): %s", type, bits, ssh_err(r));
    abort();
  }
  return ret;
}

int LLVMFuzzerTestOneInput(const uint8_t* sig, size_t slen)
{
#ifdef WITH_OPENSSL
  static struct sshkey *rsa = generate_or_die(KEY_RSA, 2048);
  static struct sshkey *dsa = generate_or_die(KEY_DSA, 1024);
  static struct sshkey *ecdsa256 = generate_or_die(KEY_ECDSA, 256);
  static struct sshkey *ecdsa384 = generate_or_die(KEY_ECDSA, 384);
  static struct sshkey *ecdsa521 = generate_or_die(KEY_ECDSA, 521);
#endif
  static struct sshkey *ed25519 = generate_or_die(KEY_ED25519, 0);
  static const char *data = "If everyone started announcing his nose had "
      "run away, I don’t know how it would all end";
  static const size_t dlen = strlen(data);

#ifdef WITH_OPENSSL
  sshkey_verify(rsa, sig, slen, (const u_char *)data, dlen, NULL, 0);
  sshkey_verify(dsa, sig, slen, (const u_char *)data, dlen, NULL, 0);
  sshkey_verify(ecdsa256, sig, slen, (const u_char *)data, dlen, NULL, 0);
  sshkey_verify(ecdsa384, sig, slen, (const u_char *)data, dlen, NULL, 0);
  sshkey_verify(ecdsa521, sig, slen, (const u_char *)data, dlen, NULL, 0);
#endif
  sshkey_verify(ed25519, sig, slen, (const u_char *)data, dlen, NULL, 0);
  return 0;
}

} // extern