Skip to content

Commit

Permalink
Bug 1518728 - update vendored libprio to 1.4 r=glandium
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D16266

--HG--
extra : moz-landing-system : lando
  • Loading branch information
rhelmer committed Jan 14, 2019
1 parent c4bdfad commit 03b8afc
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 66 deletions.
2 changes: 1 addition & 1 deletion third_party/prio/README-mozilla
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
This directory contains the Prio source from the upstream repo:
https://github.com/mozilla/libprio

Current version: 1.2 [commit 02a81fb652d385d0f4f10989d051317097ab55fb]
Current version: 1.4 [commit a95cfdd5eaf7104582709c54ef23395d24d7f7fd]

UPDATING:

Expand Down
90 changes: 62 additions & 28 deletions third_party/prio/include/mprio.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void Prio_clear();
* (2) the modulus we use for modular arithmetic.
* The default configuration uses an 87-bit modulus.
*
* The value `nFields` must be in the range `0 < nFields <= max`, where `max`
* is the value returned by the function `PrioConfig_maxDataFields()` below.
*
* The `batch_id` field specifies which "batch" of aggregate statistics we are
* computing. For example, if the aggregate statistics are computed every 24
* hours, the `batch_id` might be set to an encoding of the date. The clients
Expand All @@ -87,18 +90,23 @@ void Prio_clear();
* caller passes in, so you may free the `batch_id` string as soon as
* `PrioConfig_new` returns.
*/
PrioConfig PrioConfig_new(int n_fields, PublicKey server_a, PublicKey server_b,
const unsigned char* batch_id,
unsigned int batch_id_len);
PrioConfig PrioConfig_new(int nFields, PublicKey serverA, PublicKey serverB,
const unsigned char* batchId,
unsigned int batchIdLen);
void PrioConfig_clear(PrioConfig cfg);
int PrioConfig_numDataFields(const_PrioConfig cfg);

/*
* Return the maximum number of data fields that the implementation supports.
*/
int PrioConfig_maxDataFields(void);

/*
* Create a PrioConfig object with no encryption keys. This routine is
* useful for testing, but PrioClient_encode() will always fail when used with
* this config.
*/
PrioConfig PrioConfig_newTest(int n_fields);
PrioConfig PrioConfig_newTest(int nFields);

/*
* We use the PublicKey and PrivateKey objects for public-key encryption. Each
Expand All @@ -108,34 +116,57 @@ PrioConfig PrioConfig_newTest(int n_fields);
SECStatus Keypair_new(PrivateKey* pvtkey, PublicKey* pubkey);

/*
* Import a new curve25519 public key from the raw bytes given. The key passed
* in
* as `data` should be of length `CURVE25519_KEY_LEN`. This function allocates
* a new PublicKey object, which the caller must free using `PublicKey_clear`.
* Import a new curve25519 public/private key from the raw bytes given. When
* importing a private key, you must pass in the corresponding public key as
* well. The byte arrays given as input should be of length
* `CURVE25519_KEY_LEN`.
*
* These functions will allocate a new `PublicKey`/`PrivateKey` object, which
* the caller must free using `PublicKey_clear`/`PrivateKey_clear`.
*/
SECStatus PublicKey_import(PublicKey* pk, const unsigned char* data,
unsigned int dataLen);
SECStatus PrivateKey_import(PrivateKey* sk, const unsigned char* privData,
unsigned int privDataLen,
const unsigned char* pubData,
unsigned int pubDataLen);

/*
* Import a new curve25519 public key from a hex string that contains only the
* characters 0-9a-fA-F. The hex string passed in as `hex_data` should be of
* length `CURVE25519_KEY_LEN_HEX`. This function allocates a new PublicKey
* object, which the caller must free using `PublicKey_clear`.
* Import a new curve25519 public/private key from a hex string that contains
* only the characters 0-9a-fA-F.
*
* The hex strings passed in must each be of length `CURVE25519_KEY_LEN_HEX`.
* These functions will allocate a new `PublicKey`/`PrivateKey` object, which
* the caller must free using `PublicKey_clear`/`PrivateKey_clear`.
*/
SECStatus PublicKey_import_hex(PublicKey* pk, const unsigned char* hex_data,
SECStatus PublicKey_import_hex(PublicKey* pk, const unsigned char* hexData,
unsigned int dataLen);
SECStatus PrivateKey_import_hex(PrivateKey* sk,
const unsigned char* privHexData,
unsigned int privDataLen,
const unsigned char* pubHexData,
unsigned int pubDataLen);

/*
* Export a curve25519 public key as a raw byte-array.
* Export a curve25519 key as a raw byte-array.
*
* The output buffer `data` must have length exactly `CURVE25519_KEY_LEN`.
*/
SECStatus PublicKey_export(const_PublicKey pk,
unsigned char data[CURVE25519_KEY_LEN]);
SECStatus PublicKey_export(const_PublicKey pk, unsigned char* data,
unsigned int dataLen);
SECStatus PrivateKey_export(PrivateKey sk, unsigned char* data,
unsigned int dataLen);

/*
* Export a curve25519 public key as a NULL-terminated hex string.
* Export a curve25519 key as a NULL-terminated hex string.
*
* The output buffer `data` must have length exactly `CURVE25519_KEY_LEN_HEX +
* 1`.
*/
SECStatus PublicKey_export_hex(const_PublicKey pk,
unsigned char data[CURVE25519_KEY_LEN_HEX + 1]);
SECStatus PublicKey_export_hex(const_PublicKey pk, unsigned char* data,
unsigned int dataLen);
SECStatus PrivateKey_export_hex(PrivateKey sk, unsigned char* data,
unsigned int dataLen);

void PublicKey_clear(PublicKey pubkey);
void PrivateKey_clear(PrivateKey pvtkey);
Expand All @@ -152,8 +183,8 @@ void PrivateKey_clear(PrivateKey pvtkey);
* `for_server_b` to avoid memory leaks.
*/
SECStatus PrioClient_encode(const_PrioConfig cfg, const bool* data_in,
unsigned char** for_server_a, unsigned int* aLen,
unsigned char** for_server_b, unsigned int* bLen);
unsigned char** forServerA, unsigned int* aLen,
unsigned char** forServerB, unsigned int* bLen);

/*
* Generate a new PRG seed using the NSS global randomness source.
Expand All @@ -167,9 +198,9 @@ SECStatus PrioPRGSeed_randomize(PrioPRGSeed* seed);
* Pass in the _same_ secret PRGSeed when initializing the two servers.
* The PRGSeed must remain secret to the two servers.
*/
PrioServer PrioServer_new(const_PrioConfig cfg, PrioServerId server_idx,
PrivateKey server_priv,
const PrioPRGSeed server_shared_secret);
PrioServer PrioServer_new(const_PrioConfig cfg, PrioServerId serverIdx,
PrivateKey serverPriv,
const PrioPRGSeed serverSharedSecret);
void PrioServer_clear(PrioServer s);

/*
Expand Down Expand Up @@ -255,11 +286,14 @@ SECStatus PrioTotalShare_read(PrioTotalShare t, msgpack_unpacker* upk,

/*
* Read the output data into an array of unsigned longs. You should
* be sure that each data value can fit into a single long and that
* the pointer `output` points to a buffer large enough to store
* one long per data field.
* be sure that each data value can fit into a single `unsigned long`
* and that the pointer `output` points to a buffer large enough to
* store one long per data field.
*
* This function returns failure if some final data value is too
* long to fit in an `unsigned long`.
*/
SECStatus PrioTotalShare_final(const_PrioConfig cfg, unsigned long* output,
SECStatus PrioTotalShare_final(const_PrioConfig cfg, unsigned long long* output,
const_PrioTotalShare tA,
const_PrioTotalShare tB);

Expand Down
13 changes: 9 additions & 4 deletions third_party/prio/prio/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ initialize_roots(MPArray arr, const char values[], bool inverted)
return SECSuccess;
}

int
PrioConfig_maxDataFields(void)
{
const int n_roots = 1 << Generator2Order;
return (n_roots >> 1) - 1;
}

PrioConfig
PrioConfig_new(int n_fields, PublicKey server_a, PublicKey server_b,
const unsigned char* batch_id, unsigned int batch_id_len)
Expand All @@ -71,10 +78,8 @@ PrioConfig_new(int n_fields, PublicKey server_a, PublicKey server_b,
cfg->roots = NULL;
cfg->rootsInv = NULL;

if (cfg->num_data_fields >= cfg->n_roots) {
rv = SECFailure;
goto cleanup;
}
P_CHECKCB(cfg->n_roots > 1);
P_CHECKCB(cfg->num_data_fields <= PrioConfig_maxDataFields());

P_CHECKA(cfg->batch_id = malloc(batch_id_len));
strncpy((char*)cfg->batch_id, (char*)batch_id, batch_id_len);
Expand Down
Loading

0 comments on commit 03b8afc

Please sign in to comment.