~ubuntu-branches/ubuntu/vivid/nettle/vivid

« back to all changes in this revision

Viewing changes to testsuite/salsa20-test.c

  • Committer: Package Import Robot
  • Author(s): Magnus Holmgren
  • Date: 2012-08-25 18:28:37 UTC
  • mfrom: (1.5.1) (8.1.5 sid)
  • mto: (8.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20120825182837-i7h4w03l7mxgvmqb
Tags: 2.5-1
* New upstream release (Closes: #685855).
  - All symbols from nettle-internal.c have been dropped from the built
    library, and pkcs1_signature_prefix renamed with a leading underscore,
    without SONAME change, as they were all for internal use only.
* debian/watch: Updated to handle -pre releases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "testutils.h"
 
2
#include "salsa20.h"
 
3
 
 
4
#include "memxor.h"
 
5
 
 
6
static int
 
7
memzero_p (const uint8_t *p, size_t n)
 
8
{
 
9
  size_t i;
 
10
  for (i = 0; i < n; i++)
 
11
    if (p[i])
 
12
      return 0;
 
13
  return 1;
 
14
}
 
15
 
 
16
/* The ecrypt testcases encrypt 512 zero bytes (8 blocks), then give
 
17
   the xor of all blocks, and the data for block 0 (0-43), 3,4
 
18
   (192-319), 7 (448-511) */
 
19
 
 
20
#define STREAM_LENGTH 512
 
21
static void
 
22
test_salsa20_stream(unsigned key_length,
 
23
                    const uint8_t *key,
 
24
                    const uint8_t *iv,
 
25
                    const uint8_t *ciphertext,
 
26
                    const uint8_t *xor_ref)
 
27
{
 
28
  struct salsa20_ctx ctx;
 
29
  uint8_t data[STREAM_LENGTH + 1];
 
30
  uint8_t stream[STREAM_LENGTH + 1];
 
31
  uint8_t xor[SALSA20_BLOCK_SIZE];
 
32
  unsigned j;
 
33
 
 
34
  salsa20_set_key(&ctx, key_length, key);
 
35
  salsa20_set_iv(&ctx, iv);
 
36
  memset(stream, 0, STREAM_LENGTH + 1);
 
37
  salsa20_crypt(&ctx, STREAM_LENGTH, stream, stream);
 
38
  if (stream[STREAM_LENGTH])
 
39
    {
 
40
      fprintf(stderr, "Stream of %d bytes wrote too much!\n", STREAM_LENGTH);
 
41
      FAIL();
 
42
    }
 
43
  if (!MEMEQ (64, stream, ciphertext))
 
44
    {
 
45
      fprintf(stderr, "Error failed, offset 0:\n");
 
46
      fprintf(stderr, "\nOutput: ");
 
47
      print_hex(64, stream);
 
48
      fprintf(stderr, "\nExpected:");
 
49
      print_hex(64, ciphertext);
 
50
      fprintf(stderr, "\n");
 
51
      FAIL();
 
52
    }
 
53
  if (!MEMEQ (128, stream + 192, ciphertext + 64))
 
54
    {
 
55
      fprintf(stderr, "Error failed, offset 192:\n");
 
56
      fprintf(stderr, "\nOutput: ");
 
57
      print_hex(128, stream + 192);
 
58
      fprintf(stderr, "\nExpected:");
 
59
      print_hex(64, ciphertext + 64);
 
60
      fprintf(stderr, "\n");
 
61
      FAIL();
 
62
    }
 
63
  if (!MEMEQ (64, stream + 448, ciphertext + 192))
 
64
    {
 
65
      fprintf(stderr, "Error failed, offset 448:\n");
 
66
      fprintf(stderr, "\nOutput: ");
 
67
      print_hex(64, stream + 448);
 
68
      fprintf(stderr, "\nExpected:");
 
69
      print_hex(64, ciphertext + 192);
 
70
      fprintf(stderr, "\n");
 
71
      FAIL();
 
72
    }
 
73
 
 
74
  memxor3 (xor, stream, stream + SALSA20_BLOCK_SIZE, SALSA20_BLOCK_SIZE);
 
75
  for (j = 2*SALSA20_BLOCK_SIZE; j < STREAM_LENGTH; j += SALSA20_BLOCK_SIZE)
 
76
    memxor (xor, stream + j, SALSA20_BLOCK_SIZE);
 
77
 
 
78
  if (!MEMEQ (SALSA20_BLOCK_SIZE, xor, xor_ref))
 
79
    {
 
80
      fprintf(stderr, "Error failed, bad xor 448:\n");
 
81
      fprintf(stderr, "\nOutput: ");
 
82
      print_hex(SALSA20_BLOCK_SIZE, xor);
 
83
      fprintf(stderr, "\nExpected:");
 
84
      print_hex(SALSA20_BLOCK_SIZE, xor_ref);
 
85
      fprintf(stderr, "\n");
 
86
      FAIL();
 
87
    }
 
88
 
 
89
  for (j = 1; j <= STREAM_LENGTH; j++)
 
90
    {
 
91
      memset(data, 0, STREAM_LENGTH + 1);
 
92
      salsa20_set_iv(&ctx, iv);
 
93
      salsa20_crypt(&ctx, j, data, data);
 
94
 
 
95
      if (!MEMEQ(j, data, stream))
 
96
        {
 
97
          fprintf(stderr, "Encrypt failed for length %u:\n", j);
 
98
          fprintf(stderr, "\nOutput: ");
 
99
          print_hex(j, data);
 
100
          fprintf(stderr, "\nExpected:");
 
101
          print_hex(j, stream);
 
102
          fprintf(stderr, "\n");
 
103
          FAIL();
 
104
        }
 
105
      if (!memzero_p (data + j, STREAM_LENGTH + 1 - j))
 
106
        {
 
107
          fprintf(stderr, "Encrypt failed for length %u, wrote too much:\n", j);
 
108
          fprintf(stderr, "\nOutput: ");
 
109
          print_hex(STREAM_LENGTH + 1 - j, data + j);
 
110
          fprintf(stderr, "\n");
 
111
          FAIL();
 
112
        }
 
113
    }
 
114
}
 
115
 
 
116
static void
 
117
test_salsa20(unsigned key_length,
 
118
             const uint8_t *key,
 
119
             const uint8_t *iv,
 
120
             unsigned length,
 
121
             const uint8_t *cleartext,
 
122
             const uint8_t *ciphertext)
 
123
{
 
124
  struct salsa20_ctx ctx;
 
125
  uint8_t *data = xalloc(length + 1);
 
126
 
 
127
  salsa20_set_key(&ctx, key_length, key);
 
128
  salsa20_set_iv(&ctx, iv);
 
129
  data[length] = 17;
 
130
  salsa20_crypt(&ctx, length, data, cleartext);
 
131
  if (data[length] != 17)
 
132
    {
 
133
      fprintf(stderr, "Encrypt of %u bytes wrote too much!\nInput:", length);
 
134
      print_hex(length, cleartext);
 
135
      fprintf(stderr, "\n");
 
136
      FAIL();
 
137
    }
 
138
  if (!MEMEQ(length, data, ciphertext))
 
139
    {
 
140
      fprintf(stderr, "Encrypt failed:\nInput:");
 
141
      print_hex(length, cleartext);
 
142
      fprintf(stderr, "\nOutput: ");
 
143
      print_hex(length, data);
 
144
      fprintf(stderr, "\nExpected:");
 
145
      print_hex(length, ciphertext);
 
146
      fprintf(stderr, "\n");
 
147
      FAIL();
 
148
    }
 
149
  salsa20_set_key(&ctx, key_length, key);
 
150
  salsa20_set_iv(&ctx, iv);
 
151
  salsa20_crypt(&ctx, length, data, data);
 
152
 
 
153
  if (!MEMEQ(length, data, cleartext))
 
154
    {
 
155
      fprintf(stderr, "Decrypt failed:\nInput:");
 
156
      print_hex(length, ciphertext);
 
157
      fprintf(stderr, "\nOutput: ");
 
158
      print_hex(length, data);
 
159
      fprintf(stderr, "\nExpected:");
 
160
      print_hex(length, cleartext);
 
161
      fprintf(stderr, "\n");
 
162
      FAIL();
 
163
    }
 
164
 
 
165
  free(data);
 
166
}
 
167
  
 
168
int
 
169
test_main(void)
 
170
{
 
171
  /* http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup */
 
172
 
 
173
  test_salsa20(HL("80000000 00000000 00000000 00000000"),
 
174
               H("00000000 00000000"),
 
175
               HL("00000000 00000000"),
 
176
               H("4DFA5E48 1DA23EA0"));
 
177
 
 
178
  test_salsa20(HL("00000000 00000000 00000000 00000000"),
 
179
               H("80000000 00000000"),
 
180
               HL("00000000 00000000"),
 
181
               H("B66C1E44 46DD9557"));
 
182
 
 
183
  test_salsa20(HL("0053A6F94C9FF24598EB3E91E4378ADD"),
 
184
               H("0D74DB42A91077DE"),
 
185
               HL("00000000 00000000"),
 
186
               H("05E1E7BE B697D999"));
 
187
 
 
188
  test_salsa20(HL("80000000 00000000 00000000 00000000"
 
189
                  "00000000 00000000 00000000 00000000"),
 
190
               H("00000000 00000000"),
 
191
               HL("00000000 00000000"),
 
192
               H("E3BE8FDD 8BECA2E3"));
 
193
 
 
194
  test_salsa20(HL("00000000 00000000 00000000 00000000"
 
195
                  "00000000 00000000 00000000 00000000"),
 
196
               H("80000000 00000000"),
 
197
               HL("00000000 00000000"),
 
198
               H("2ABA3DC45B494700"));
 
199
 
 
200
  test_salsa20(HL("0053A6F94C9FF24598EB3E91E4378ADD"
 
201
                  "3083D6297CCF2275C81B6EC11467BA0D"),
 
202
               H("0D74DB42A91077DE"),
 
203
               HL("00000000 00000000"),
 
204
               H("F5FAD53F 79F9DF58"));
 
205
 
 
206
  test_salsa20_stream(HL("80000000000000000000000000000000"),
 
207
                      H("00000000 00000000"),
 
208
                      H("4DFA5E481DA23EA09A31022050859936"
 
209
                        "DA52FCEE218005164F267CB65F5CFD7F"
 
210
                        "2B4F97E0FF16924A52DF269515110A07"
 
211
                        "F9E460BC65EF95DA58F740B7D1DBB0AA"
 
212
                        "DA9C1581F429E0A00F7D67E23B730676"
 
213
                        "783B262E8EB43A25F55FB90B3E753AEF"
 
214
                        "8C6713EC66C51881111593CCB3E8CB8F"
 
215
                        "8DE124080501EEEB389C4BCB6977CF95"
 
216
                        "7D5789631EB4554400E1E025935DFA7B"
 
217
                        "3E9039D61BDC58A8697D36815BF1985C"
 
218
                        "EFDF7AE112E5BB81E37ECF0616CE7147"
 
219
                        "FC08A93A367E08631F23C03B00A8DA2F"
 
220
                        "B375703739DACED4DD4059FD71C3C47F"
 
221
                        "C2F9939670FAD4A46066ADCC6A564578"
 
222
                        "3308B90FFB72BE04A6B147CBE38CC0C3"
 
223
                        "B9267C296A92A7C69873F9F263BE9703"),
 
224
                      H("F7A274D268316790A67EC058F45C0F2A"
 
225
                        "067A99FCDE6236C0CEF8E056349FE54C"
 
226
                        "5F13AC74D2539570FD34FEAB06C57205"
 
227
                        "3949B59585742181A5A760223AFA22D4"));
 
228
 
 
229
  test_salsa20_stream(HL("48494A4B4C4D4E4F5051525354555657"
 
230
                         "58595A5B5C5D5E5F6061626364656667"),
 
231
                      H("0000000000000000"),
 
232
                      H("53AD3698A011F779AD71030F3EFBEBA0"
 
233
                        "A7EE3C55789681B1591EF33A7BE521ED"
 
234
                        "68FC36E58F53FFD6E1369B00E390E973"
 
235
                        "F656ACB097E0D603BE59A0B8F7975B98"
 
236
                        "A04698274C6AC6EC03F66ED3F94C08B7"
 
237
                        "9FFDBF2A1610E6F5814905E73AD6D0D2"
 
238
                        "8164EEB8450D8ED0BB4B644761B43512"
 
239
                        "52DD5DDF00C31E3DABA0BC17691CCFDC"
 
240
                        "B826C7F071E796D34E3BFFB3C96E76A1"
 
241
                        "209388392806947C7F19B86D379FA3AE"
 
242
                        "DFCD19EBF49803DACC6E577E5B97B0F6"
 
243
                        "D2036B6624D8196C96FCF02C865D30C1"
 
244
                        "B505D41E2C207FA1C0A0E93413DDCFFC"
 
245
                        "9BECA8030AFFAC2466E56482DA0EF428"
 
246
                        "E63880B5021D3051F18679505A2B9D4F"
 
247
                        "9B2C5A2D271D276DE3F51DBEBA934436"),
 
248
                      H("7849651A820B1CDFE36D5D6632716534"
 
249
                        "E0635EDEFD538122D80870B60FB055DB"
 
250
                        "637C7CA2B78B116F83AFF46E40F8F71D"
 
251
                        "4CD6D2E1B750D5E011D1DF2E80F7210A"));
 
252
 
 
253
  SUCCESS();
 
254
}