File: box6.cpp

package info (click to toggle)
nacl 20110221-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,448 kB
  • ctags: 21,301
  • sloc: asm: 19,669; ansic: 13,302; cpp: 1,125; sh: 852; makefile: 10
file content (43 lines) | stat: -rw-r--r-- 1,040 bytes parent folder | download | duplicates (8)
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
#include <string>
using std::string;
#include <stdlib.h>
#include <stdio.h>
#include "crypto_box.h"
#include "randombytes.h"

main()
{
  int mlen;
  for (mlen = 0;mlen < 1000;++mlen) {
    string alicesk;
    string alicepk = crypto_box_keypair(&alicesk);
    string bobsk;
    string bobpk = crypto_box_keypair(&bobsk);
    unsigned char nbytes[crypto_box_NONCEBYTES];
    randombytes(nbytes,crypto_box_NONCEBYTES);
    string n((char *) nbytes,crypto_box_NONCEBYTES);
    unsigned char mbytes[mlen];
    randombytes(mbytes,mlen);
    string m((char *) mbytes,mlen);
    string c = crypto_box(m,n,bobpk,alicesk);
    int caught = 0;
    while (caught < 10) {
      c.replace(random() % c.size(),1,1,random());
      try {
        string m2 = crypto_box_open(c,n,alicepk,bobsk);
        if (m != m2) {
	  printf("forgery\n");
	  return 100;
        }
      } catch(const char *s) {
	if (string(s) == string("ciphertext fails verification"))
	  ++caught;
	else {
	  printf("%s\n",s);
	  return 111;
        }
      }
    }
  }
  return 0;
}