aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tests/base64.cc
blob: 7d49c88229d1786b2992b4b0e585c8d35b744de2 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
    Mosh: the mobile shell
    Copyright 2012 Keith Winstein

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations including
    the two.

    You must obey the GNU General Public License in all respects for all
    of the code used other than OpenSSL. If you modify file(s) with this
    exception, you may extend this exception to your version of the
    file(s), but you are not obligated to do so. If you do not wish to do
    so, delete this exception statement from your version. If you delete
    this exception statement from all source files in the program, then
    also delete it here.
*/

/* Test suite for the OCB-AES reference implementation included with Mosh.

   This tests cryptographic primitives implemented by others.  It uses the
   same interfaces and indeed the same compiled object code as the Mosh
   client and server.  It does not particularly test any code written for
   the Mosh project. */

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

#include "src/crypto/base64.h"
#include "base64_vector.h"
#include "src/crypto/crypto.h"
#include "src/crypto/prng.h"
#include "src/util/fatal_assert.h"
// #include "test_utils.h"

#define KEY_LEN   16
#define NONCE_LEN 12
#define TAG_LEN   16

bool verbose = false;

static void test_base64( void ) {
  /* run through a test vector */
  char encoded[25];
  uint8_t decoded[16];
  size_t b64_len = 24;
  size_t raw_len = 16;
  for ( base64_test_row *row = static_base64_vector; *row->native != '\0'; row++ ) {
    memset(encoded, '\0', sizeof encoded);
    memset(decoded, '\0', sizeof decoded);

    base64_encode(static_cast<const uint8_t *>(row->native), raw_len, encoded, b64_len);
    fatal_assert( b64_len == 24 );
    fatal_assert( !memcmp(row->encoded, encoded, sizeof encoded));

    fatal_assert( base64_decode(row->encoded, b64_len, decoded, &raw_len ));
    fatal_assert( raw_len == 16 );
    fatal_assert( !memcmp(row->native, decoded, sizeof decoded));
  }
  if ( verbose ) {
    printf( "validation PASSED\n" );
  }
  /* try 0..255 in the last byte; make sure the final two characters are output properly */
  uint8_t source[16];
  memset(source, '\0', sizeof source);
  for ( int i = 0; i < 256; i++ ) {
    source[15] = i;
    base64_encode(source, raw_len, encoded, b64_len);
    fatal_assert( b64_len == 24 );

    fatal_assert( base64_decode(encoded, b64_len, decoded, &raw_len ));
    fatal_assert( raw_len == 16 );
    fatal_assert( !memcmp(source, decoded, sizeof decoded));
  }
  if ( verbose ) {
    printf( "last-byte PASSED\n" );
  }

  /* randomly try keys */
  PRNG prng;
  for ( int i = 0; i < ( 1<<17 ); i++ ) {
    Base64Key key1(prng);
    Base64Key key2(key1.printable_key());
    fatal_assert( key1.printable_key() == key2.printable_key() && !memcmp(key1.data(), key2.data(), 16 ));
  }
  if ( verbose ) {
    printf( "random PASSED\n" );
  }

  /* test bad keys */
  const char *bad_keys[] = {
    "",
    "AAAAAAAAAAAAAAAAAAAAAA",
    "AAAAAAAAAAAAAAAAAAAAAA=",
    "AAAAAAAAAAAAAAAAAAAAA==",
    "AAAAAAAAAAAAAAAAAAAAAAA==",
    "AAAAAAAAAAAAAAAAAAAAAAAA==",
    "AAAAAAAAAAAAAAAAAAAAAA~=",
    "AAAAAAAAAAAAAAAAAAAAAA=~",
    "~AAAAAAAAAAAAAAAAAAAAA==",
    "AAAAAAAAAAAAAAAAAAAA~A==",
    "AAAAAAAAAAAAAAAAAAAAA~==",
    "AAAAAAAAAA~AAAAAAAAAAA==",
    "AAAAAAAAAA==",
    NULL,
  };
  for ( const char **key = bad_keys; *key != NULL; key++ ) {
    b64_len = 24;
    raw_len = 16;
    fatal_assert( !base64_decode(*key, b64_len, decoded, &raw_len ));
  }
  if ( verbose ) {
    printf( "bad-keys PASSED\n" );
  }
}

int main( int argc, char *argv[] )
{
  if ( argc >= 2 && strcmp( argv[ 1 ], "-v" ) == 0 ) {
    verbose = true;
  }

  try {
    test_base64();
  } catch ( const std::exception &e ) {
    fprintf( stderr, "Error: %s\r\n", e.what() );
    return 1;
  }
  return 0;
}