| 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 <string.h> |
| 16 |
|
| 17 |
#include <openssl/mem.h> |
| 18 |
#include <openssl/rand.h> |
| 19 |
|
| 20 |
#include "internal.h" |
| 21 |
|
| 22 |
|
| 23 |
NEWHOPE_POLY *NEWHOPE_POLY_new(void) { |
| 24 |
return (NEWHOPE_POLY *)OPENSSL_malloc(sizeof(NEWHOPE_POLY)); |
| 25 |
} |
| 26 |
|
| 27 |
void NEWHOPE_POLY_free(NEWHOPE_POLY *p) { OPENSSL_free(p); } |
| 28 |
|
| 29 |
/* Encodes reconciliation data from |c| into |r|. */ |
| 30 |
static void encode_rec(const NEWHOPE_POLY *c, uint8_t *r) { |
| 31 |
int i; |
| 32 |
for (i = 0; i < PARAM_N / 4; i++) { |
| 33 |
r[i] = c->coeffs[4 * i] | (c->coeffs[4 * i + 1] << 2) | |
| 34 |
(c->coeffs[4 * i + 2] << 4) | (c->coeffs[4 * i + 3] << 6); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/* Decodes reconciliation data from |r| into |c|. */ |
| 39 |
static void decode_rec(const uint8_t *r, NEWHOPE_POLY *c) { |
| 40 |
int i; |
| 41 |
for (i = 0; i < PARAM_N / 4; i++) { |
| 42 |
c->coeffs[4 * i + 0] = r[i] & 0x03; |
| 43 |
c->coeffs[4 * i + 1] = (r[i] >> 2) & 0x03; |
| 44 |
c->coeffs[4 * i + 2] = (r[i] >> 4) & 0x03; |
| 45 |
c->coeffs[4 * i + 3] = (r[i] >> 6); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void NEWHOPE_offer(uint8_t *offermsg, NEWHOPE_POLY *s) { |
| 50 |
NEWHOPE_POLY_noise_ntt(s); |
| 51 |
|
| 52 |
/* The first part of the offer message is the seed, which compactly encodes |
| 53 |
* a. */ |
| 54 |
NEWHOPE_POLY a; |
| 55 |
uint8_t *seed = &offermsg[NEWHOPE_POLY_LENGTH]; |
| 56 |
RAND_bytes(seed, SEED_LENGTH); |
| 57 |
newhope_poly_uniform(&a, seed); |
| 58 |
|
| 59 |
NEWHOPE_POLY e; |
| 60 |
NEWHOPE_POLY_noise_ntt(&e); |
| 61 |
|
| 62 |
/* The second part of the offer message is the polynomial pk = a*s+e */ |
| 63 |
NEWHOPE_POLY pk; |
| 64 |
NEWHOPE_offer_computation(&pk, s, &e, &a); |
| 65 |
NEWHOPE_POLY_tobytes(offermsg, &pk); |
| 66 |
} |
| 67 |
|
| 68 |
int NEWHOPE_accept(uint8_t key[SHA256_DIGEST_LENGTH], |
| 69 |
uint8_t acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH], |
| 70 |
const uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH], |
| 71 |
size_t msg_len) { |
| 72 |
if (msg_len != NEWHOPE_OFFERMSG_LENGTH) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
/* Decode the |offermsg|, generating the same |a| as the peer, from the peer's |
| 77 |
* seed. */ |
| 78 |
NEWHOPE_POLY pk, a; |
| 79 |
NEWHOPE_offer_frommsg(&pk, &a, offermsg); |
| 80 |
|
| 81 |
/* Generate noise polynomials used to generate our key. */ |
| 82 |
NEWHOPE_POLY sp, ep, epp; |
| 83 |
NEWHOPE_POLY_noise_ntt(&sp); |
| 84 |
NEWHOPE_POLY_noise_ntt(&ep); |
| 85 |
NEWHOPE_POLY_noise(&epp); /* intentionally not NTT */ |
| 86 |
|
| 87 |
/* Generate random bytes used for reconciliation. (The reference |
| 88 |
* implementation calls ChaCha20 here.) */ |
| 89 |
uint8_t rand[32]; |
| 90 |
RAND_bytes(rand, sizeof(rand)); |
| 91 |
|
| 92 |
/* Encode |bp| and |c| as the |acceptmsg|. */ |
| 93 |
NEWHOPE_POLY bp, c; |
| 94 |
uint8_t k[NEWHOPE_KEY_LENGTH]; |
| 95 |
NEWHOPE_accept_computation(k, &bp, &c, &sp, &ep, &epp, rand, &pk, &a); |
| 96 |
NEWHOPE_POLY_tobytes(acceptmsg, &bp); |
| 97 |
encode_rec(&c, &acceptmsg[NEWHOPE_POLY_LENGTH]); |
| 98 |
|
| 99 |
SHA256_CTX ctx; |
| 100 |
if (!SHA256_Init(&ctx) || |
| 101 |
!SHA256_Update(&ctx, k, NEWHOPE_KEY_LENGTH) || |
| 102 |
!SHA256_Final(key, &ctx)) { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
return 1; |
| 107 |
} |
| 108 |
|
| 109 |
int NEWHOPE_finish(uint8_t key[SHA256_DIGEST_LENGTH], const NEWHOPE_POLY *sk, |
| 110 |
const uint8_t acceptmsg[NEWHOPE_ACCEPTMSG_LENGTH], |
| 111 |
size_t msg_len) { |
| 112 |
if (msg_len != NEWHOPE_ACCEPTMSG_LENGTH) { |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
/* Decode the accept message into |bp| and |c|. */ |
| 117 |
NEWHOPE_POLY bp, c; |
| 118 |
NEWHOPE_POLY_frombytes(&bp, acceptmsg); |
| 119 |
decode_rec(&acceptmsg[NEWHOPE_POLY_LENGTH], &c); |
| 120 |
|
| 121 |
uint8_t k[NEWHOPE_KEY_LENGTH]; |
| 122 |
NEWHOPE_finish_computation(k, sk, &bp, &c); |
| 123 |
SHA256_CTX ctx; |
| 124 |
if (!SHA256_Init(&ctx) || |
| 125 |
!SHA256_Update(&ctx, k, NEWHOPE_KEY_LENGTH) || |
| 126 |
!SHA256_Final(key, &ctx)) { |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
return 1; |
| 131 |
} |
| 132 |
|
| 133 |
void NEWHOPE_offer_computation(NEWHOPE_POLY *out_pk, |
| 134 |
const NEWHOPE_POLY *s, const NEWHOPE_POLY *e, |
| 135 |
const NEWHOPE_POLY *a) { |
| 136 |
NEWHOPE_POLY r; |
| 137 |
newhope_poly_pointwise(&r, s, a); |
| 138 |
newhope_poly_add(out_pk, e, &r); |
| 139 |
} |
| 140 |
|
| 141 |
void NEWHOPE_accept_computation( |
| 142 |
uint8_t k[NEWHOPE_KEY_LENGTH], NEWHOPE_POLY *bp, |
| 143 |
NEWHOPE_POLY *reconciliation, |
| 144 |
const NEWHOPE_POLY *sp, const NEWHOPE_POLY *ep, const NEWHOPE_POLY *epp, |
| 145 |
const uint8_t rand[32], |
| 146 |
const NEWHOPE_POLY *pk, const NEWHOPE_POLY *a) { |
| 147 |
/* bp = a*s' + e' */ |
| 148 |
newhope_poly_pointwise(bp, a, sp); |
| 149 |
newhope_poly_add(bp, bp, ep); |
| 150 |
|
| 151 |
/* v = pk * s' + e'' */ |
| 152 |
NEWHOPE_POLY v; |
| 153 |
newhope_poly_pointwise(&v, pk, sp); |
| 154 |
newhope_poly_invntt(&v); |
| 155 |
newhope_poly_add(&v, &v, epp); |
| 156 |
newhope_helprec(reconciliation, &v, rand); |
| 157 |
newhope_reconcile(k, &v, reconciliation); |
| 158 |
} |
| 159 |
|
| 160 |
void NEWHOPE_finish_computation(uint8_t k[NEWHOPE_KEY_LENGTH], |
| 161 |
const NEWHOPE_POLY *sk, const NEWHOPE_POLY *bp, |
| 162 |
const NEWHOPE_POLY *reconciliation) { |
| 163 |
NEWHOPE_POLY v; |
| 164 |
newhope_poly_pointwise(&v, sk, bp); |
| 165 |
newhope_poly_invntt(&v); |
| 166 |
newhope_reconcile(k, &v, reconciliation); |
| 167 |
} |
| 168 |
|
| 169 |
void NEWHOPE_offer_frommsg(NEWHOPE_POLY *out_pk, NEWHOPE_POLY *out_a, |
| 170 |
const uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH]) { |
| 171 |
NEWHOPE_POLY_frombytes(out_pk, offermsg); |
| 172 |
const uint8_t *seed = offermsg + NEWHOPE_POLY_LENGTH; |
| 173 |
newhope_poly_uniform(out_a, seed); |
| 174 |
} |