Debian Codegraph @

external/boringssl/crypto/newhope/newhope_test.cc

raw
1 /* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <math.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include <openssl/crypto.h>
20 #include <openssl/rand.h>
21
22 #include "../test/scoped_types.h"
23 #include "internal.h"
24
25
26 // Set to 10 for quick execution. Tested up to 1,000,000.
27 static const int kNumTests = 10;
28
29 static bool TestKeys(void) {
30 // Alice generates a public key.
31 ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
32 uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
33 NEWHOPE_offer(offer_msg, sk.get());
34
35 // Bob derives a secret key and creates a response.
36 uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
37 uint8_t accept_key[SHA256_DIGEST_LENGTH];
38 if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
39 fprintf(stderr, "ERROR accept key exchange failed\n");
40 return false;
41 }
42
43 // Alice uses Bob's response to get her secret key.
44 uint8_t offer_key[SHA256_DIGEST_LENGTH];
45 if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
46 fprintf(stderr, "ERROR finish key exchange failed\n");
47 return false;
48 }
49
50 if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) != 0) {
51 fprintf(stderr, "ERROR keys did not agree\n");
52 return false;
53 }
54
55 return true;
56 }
57
58 static bool TestInvalidSK(void) {
59 // Alice generates a public key.
60 uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
61 ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
62 NEWHOPE_offer(offer_msg, sk.get());
63
64 // Bob derives a secret key and creates a response.
65 uint8_t accept_key[SHA256_DIGEST_LENGTH];
66 uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
67 if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
68 fprintf(stderr, "ERROR accept key exchange failed\n");
69 return false;
70 }
71
72 // Corrupt the secret key. It turns out that you need to corrupt a lot of
73 // bits to ensure that the key exchange always fails!
74 sk->coeffs[PARAM_N - 1] = 0;
75 sk->coeffs[PARAM_N - 2] = 0;
76 sk->coeffs[PARAM_N - 3] = 0;
77 sk->coeffs[PARAM_N - 4] = 0;
78
79 // Alice uses Bob's response to get her secret key.
80 uint8_t offer_key[SHA256_DIGEST_LENGTH];
81 if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
82 fprintf(stderr, "ERROR finish key exchange failed\n");
83 return false;
84 }
85
86 if (memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH) == 0) {
87 fprintf(stderr, "ERROR keys agreed despite corrupt sk\n");
88 return false;
89 }
90
91 return true;
92 }
93
94 static bool TestInvalidAcceptMsg(void) {
95 // Alice generates a public key.
96 ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
97 uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
98 NEWHOPE_offer(offer_msg, sk.get());
99
100 // Bob derives a secret key and creates a response.
101 uint8_t accept_key[SHA256_DIGEST_LENGTH];
102 uint8_t accept_msg[NEWHOPE_ACCEPTMSG_LENGTH];
103 if (!NEWHOPE_accept(accept_key, accept_msg, offer_msg, sizeof(offer_msg))) {
104 fprintf(stderr, "ERROR accept key exchange failed\n");
105 return false;
106 }
107
108 // Corrupt the (polynomial part of the) accept message. It turns out that
109 // you need to corrupt a lot of bits to ensure that the key exchange always
110 // fails!
111 accept_msg[PARAM_N - 1] = 0;
112 accept_msg[PARAM_N - 2] = 0;
113 accept_msg[PARAM_N - 3] = 0;
114 accept_msg[PARAM_N - 4] = 0;
115
116 // Alice uses Bob's response to get her secret key.
117 uint8_t offer_key[SHA256_DIGEST_LENGTH];
118 if (!NEWHOPE_finish(offer_key, sk.get(), accept_msg, sizeof(accept_msg))) {
119 fprintf(stderr, "ERROR finish key exchange failed\n");
120 return false;
121 }
122
123 if (!memcmp(offer_key, accept_key, SHA256_DIGEST_LENGTH)) {
124 fprintf(stderr, "ERROR keys agreed despite corrupt accept message\n");
125 return false;
126 }
127
128 return true;
129 }
130
131 int main(void) {
132 for (int i = 0; i < kNumTests; i++) {
133 if (!TestKeys() ||
134 !TestInvalidSK() ||
135 !TestInvalidAcceptMsg()) {
136 return 1;
137 }
138 }
139
140 printf("PASS\n");
141 return 0;
142 }