michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef FREEBL_NO_DEPEND michael@0: #include "stubs.h" michael@0: #endif michael@0: michael@0: #include "blapi.h" michael@0: #include "secerr.h" michael@0: #include "secitem.h" michael@0: #include "secmpi.h" michael@0: michael@0: /* Hash an item's length and then its value. Only items smaller than 2^16 bytes michael@0: * are allowed. Lengths are hashed in network byte order. This is designed michael@0: * to match the OpenSSL J-PAKE implementation. michael@0: */ michael@0: static mp_err michael@0: hashSECItem(HASHContext * hash, const SECItem * it) michael@0: { michael@0: unsigned char length[2]; michael@0: michael@0: if (it->len > 0xffff) michael@0: return MP_BADARG; michael@0: michael@0: length[0] = (unsigned char) (it->len >> 8); michael@0: length[1] = (unsigned char) (it->len); michael@0: hash->hashobj->update(hash->hash_context, length, 2); michael@0: hash->hashobj->update(hash->hash_context, it->data, it->len); michael@0: return MP_OKAY; michael@0: } michael@0: michael@0: /* Hash all public components of the signature, each prefixed with its michael@0: length, and then convert the hash to an mp_int. */ michael@0: static mp_err michael@0: hashPublicParams(HASH_HashType hashType, const SECItem * g, michael@0: const SECItem * gv, const SECItem * gx, michael@0: const SECItem * signerID, mp_int * h) michael@0: { michael@0: mp_err err; michael@0: unsigned char hBuf[HASH_LENGTH_MAX]; michael@0: SECItem hItem; michael@0: HASHContext hash; michael@0: michael@0: hash.hashobj = HASH_GetRawHashObject(hashType); michael@0: if (hash.hashobj == NULL || hash.hashobj->length > sizeof hBuf) { michael@0: return MP_BADARG; michael@0: } michael@0: hash.hash_context = hash.hashobj->create(); michael@0: if (hash.hash_context == NULL) { michael@0: return MP_MEM; michael@0: } michael@0: michael@0: hItem.data = hBuf; michael@0: hItem.len = hash.hashobj->length; michael@0: michael@0: hash.hashobj->begin(hash.hash_context); michael@0: CHECK_MPI_OK( hashSECItem(&hash, g) ); michael@0: CHECK_MPI_OK( hashSECItem(&hash, gv) ); michael@0: CHECK_MPI_OK( hashSECItem(&hash, gx) ); michael@0: CHECK_MPI_OK( hashSECItem(&hash, signerID) ); michael@0: hash.hashobj->end(hash.hash_context, hItem.data, &hItem.len, michael@0: sizeof hBuf); michael@0: SECITEM_TO_MPINT(hItem, h); michael@0: michael@0: cleanup: michael@0: if (hash.hash_context != NULL) { michael@0: hash.hashobj->destroy(hash.hash_context, PR_TRUE); michael@0: } michael@0: michael@0: return err; michael@0: } michael@0: michael@0: /* Generate a Schnorr signature for round 1 or round 2 */ michael@0: SECStatus michael@0: JPAKE_Sign(PLArenaPool * arena, const PQGParams * pqg, HASH_HashType hashType, michael@0: const SECItem * signerID, const SECItem * x, michael@0: const SECItem * testRandom, const SECItem * gxIn, SECItem * gxOut, michael@0: SECItem * gv, SECItem * r) michael@0: { michael@0: SECStatus rv = SECSuccess; michael@0: mp_err err; michael@0: mp_int p; michael@0: mp_int q; michael@0: mp_int g; michael@0: mp_int X; michael@0: mp_int GX; michael@0: mp_int V; michael@0: mp_int GV; michael@0: mp_int h; michael@0: mp_int tmp; michael@0: mp_int R; michael@0: SECItem v; michael@0: michael@0: if (!arena || michael@0: !pqg || !pqg->prime.data || pqg->prime.len == 0 || michael@0: !pqg->subPrime.data || pqg->subPrime.len == 0 || michael@0: !pqg->base.data || pqg->base.len == 0 || michael@0: !signerID || !signerID->data || signerID->len == 0 || michael@0: !x || !x->data || x->len == 0 || michael@0: (testRandom && (!testRandom->data || testRandom->len == 0)) || michael@0: (gxIn == NULL && (!gxOut || gxOut->data != NULL)) || michael@0: (gxIn != NULL && (!gxIn->data || gxIn->len == 0 || gxOut != NULL)) || michael@0: !gv || gv->data != NULL || michael@0: !r || r->data != NULL) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: michael@0: MP_DIGITS(&p) = 0; michael@0: MP_DIGITS(&q) = 0; michael@0: MP_DIGITS(&g) = 0; michael@0: MP_DIGITS(&X) = 0; michael@0: MP_DIGITS(&GX) = 0; michael@0: MP_DIGITS(&V) = 0; michael@0: MP_DIGITS(&GV) = 0; michael@0: MP_DIGITS(&h) = 0; michael@0: MP_DIGITS(&tmp) = 0; michael@0: MP_DIGITS(&R) = 0; michael@0: michael@0: CHECK_MPI_OK( mp_init(&p) ); michael@0: CHECK_MPI_OK( mp_init(&q) ); michael@0: CHECK_MPI_OK( mp_init(&g) ); michael@0: CHECK_MPI_OK( mp_init(&X) ); michael@0: CHECK_MPI_OK( mp_init(&GX) ); michael@0: CHECK_MPI_OK( mp_init(&V) ); michael@0: CHECK_MPI_OK( mp_init(&GV) ); michael@0: CHECK_MPI_OK( mp_init(&h) ); michael@0: CHECK_MPI_OK( mp_init(&tmp) ); michael@0: CHECK_MPI_OK( mp_init(&R) ); michael@0: michael@0: SECITEM_TO_MPINT(pqg->prime, &p); michael@0: SECITEM_TO_MPINT(pqg->subPrime, &q); michael@0: SECITEM_TO_MPINT(pqg->base, &g); michael@0: SECITEM_TO_MPINT(*x, &X); michael@0: michael@0: /* gx = g^x */ michael@0: if (gxIn == NULL) { michael@0: CHECK_MPI_OK( mp_exptmod(&g, &X, &p, &GX) ); michael@0: MPINT_TO_SECITEM(&GX, gxOut, arena); michael@0: gxIn = gxOut; michael@0: } else { michael@0: SECITEM_TO_MPINT(*gxIn, &GX); michael@0: } michael@0: michael@0: /* v is a random value in the q subgroup */ michael@0: if (testRandom == NULL) { michael@0: v.data = NULL; michael@0: rv = DSA_NewRandom(arena, &pqg->subPrime, &v); michael@0: if (rv != SECSuccess) { michael@0: goto cleanup; michael@0: } michael@0: } else { michael@0: v.data = testRandom->data; michael@0: v.len = testRandom->len; michael@0: } michael@0: SECITEM_TO_MPINT(v, &V); michael@0: michael@0: /* gv = g^v (mod q), random v, 1 <= v < q */ michael@0: CHECK_MPI_OK( mp_exptmod(&g, &V, &p, &GV) ); michael@0: MPINT_TO_SECITEM(&GV, gv, arena); michael@0: michael@0: /* h = H(g, gv, gx, signerID) */ michael@0: CHECK_MPI_OK( hashPublicParams(hashType, &pqg->base, gv, gxIn, signerID, michael@0: &h) ); michael@0: michael@0: /* r = v - x*h (mod q) */ michael@0: CHECK_MPI_OK( mp_mulmod(&X, &h, &q, &tmp) ); michael@0: CHECK_MPI_OK( mp_submod(&V, &tmp, &q, &R) ); michael@0: MPINT_TO_SECITEM(&R, r, arena); michael@0: michael@0: cleanup: michael@0: mp_clear(&p); michael@0: mp_clear(&q); michael@0: mp_clear(&g); michael@0: mp_clear(&X); michael@0: mp_clear(&GX); michael@0: mp_clear(&V); michael@0: mp_clear(&GV); michael@0: mp_clear(&h); michael@0: mp_clear(&tmp); michael@0: mp_clear(&R); michael@0: michael@0: if (rv == SECSuccess && err != MP_OKAY) { michael@0: MP_TO_SEC_ERROR(err); michael@0: rv = SECFailure; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* Verify a Schnorr signature generated by the peer in round 1 or round 2. */ michael@0: SECStatus michael@0: JPAKE_Verify(PLArenaPool * arena, const PQGParams * pqg, HASH_HashType hashType, michael@0: const SECItem * signerID, const SECItem * peerID, michael@0: const SECItem * gx, const SECItem * gv, const SECItem * r) michael@0: { michael@0: SECStatus rv = SECSuccess; michael@0: mp_err err; michael@0: mp_int p; michael@0: mp_int q; michael@0: mp_int g; michael@0: mp_int p_minus_1; michael@0: mp_int GX; michael@0: mp_int h; michael@0: mp_int one; michael@0: mp_int R; michael@0: mp_int gr; michael@0: mp_int gxh; michael@0: mp_int gr_gxh; michael@0: SECItem calculated; michael@0: michael@0: if (!arena || michael@0: !pqg || !pqg->prime.data || pqg->prime.len == 0 || michael@0: !pqg->subPrime.data || pqg->subPrime.len == 0 || michael@0: !pqg->base.data || pqg->base.len == 0 || michael@0: !signerID || !signerID->data || signerID->len == 0 || michael@0: !peerID || !peerID->data || peerID->len == 0 || michael@0: !gx || !gx->data || gx->len == 0 || michael@0: !gv || !gv->data || gv->len == 0 || michael@0: !r || !r->data || r->len == 0 || michael@0: SECITEM_CompareItem(signerID, peerID) == SECEqual) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: MP_DIGITS(&p) = 0; michael@0: MP_DIGITS(&q) = 0; michael@0: MP_DIGITS(&g) = 0; michael@0: MP_DIGITS(&p_minus_1) = 0; michael@0: MP_DIGITS(&GX) = 0; michael@0: MP_DIGITS(&h) = 0; michael@0: MP_DIGITS(&one) = 0; michael@0: MP_DIGITS(&R) = 0; michael@0: MP_DIGITS(&gr) = 0; michael@0: MP_DIGITS(&gxh) = 0; michael@0: MP_DIGITS(&gr_gxh) = 0; michael@0: calculated.data = NULL; michael@0: michael@0: CHECK_MPI_OK( mp_init(&p) ); michael@0: CHECK_MPI_OK( mp_init(&q) ); michael@0: CHECK_MPI_OK( mp_init(&g) ); michael@0: CHECK_MPI_OK( mp_init(&p_minus_1) ); michael@0: CHECK_MPI_OK( mp_init(&GX) ); michael@0: CHECK_MPI_OK( mp_init(&h) ); michael@0: CHECK_MPI_OK( mp_init(&one) ); michael@0: CHECK_MPI_OK( mp_init(&R) ); michael@0: CHECK_MPI_OK( mp_init(&gr) ); michael@0: CHECK_MPI_OK( mp_init(&gxh) ); michael@0: CHECK_MPI_OK( mp_init(&gr_gxh) ); michael@0: michael@0: SECITEM_TO_MPINT(pqg->prime, &p); michael@0: SECITEM_TO_MPINT(pqg->subPrime, &q); michael@0: SECITEM_TO_MPINT(pqg->base, &g); michael@0: SECITEM_TO_MPINT(*gx, &GX); michael@0: SECITEM_TO_MPINT(*r, &R); michael@0: michael@0: CHECK_MPI_OK( mp_sub_d(&p, 1, &p_minus_1) ); michael@0: CHECK_MPI_OK( mp_exptmod(&GX, &q, &p, &one) ); michael@0: /* Check g^x is in [1, p-2], R is in [0, q-1], and (g^x)^q mod p == 1 */ michael@0: if (!(mp_cmp_z(&GX) > 0 && michael@0: mp_cmp(&GX, &p_minus_1) < 0 && michael@0: mp_cmp(&R, &q) < 0 && michael@0: mp_cmp_d(&one, 1) == 0)) { michael@0: goto badSig; michael@0: } michael@0: michael@0: CHECK_MPI_OK( hashPublicParams(hashType, &pqg->base, gv, gx, peerID, michael@0: &h) ); michael@0: michael@0: /* Calculate g^v = g^r * g^x^h */ michael@0: CHECK_MPI_OK( mp_exptmod(&g, &R, &p, &gr) ); michael@0: CHECK_MPI_OK( mp_exptmod(&GX, &h, &p, &gxh) ); michael@0: CHECK_MPI_OK( mp_mulmod(&gr, &gxh, &p, &gr_gxh) ); michael@0: michael@0: /* Compare calculated g^v to given g^v */ michael@0: MPINT_TO_SECITEM(&gr_gxh, &calculated, arena); michael@0: if (calculated.len == gv->len && michael@0: NSS_SecureMemcmp(calculated.data, gv->data, calculated.len) == 0) { michael@0: rv = SECSuccess; michael@0: } else { michael@0: badSig: PORT_SetError(SEC_ERROR_BAD_SIGNATURE); michael@0: rv = SECFailure; michael@0: } michael@0: michael@0: cleanup: michael@0: mp_clear(&p); michael@0: mp_clear(&q); michael@0: mp_clear(&g); michael@0: mp_clear(&p_minus_1); michael@0: mp_clear(&GX); michael@0: mp_clear(&h); michael@0: mp_clear(&one); michael@0: mp_clear(&R); michael@0: mp_clear(&gr); michael@0: mp_clear(&gxh); michael@0: mp_clear(&gr_gxh); michael@0: michael@0: if (rv == SECSuccess && err != MP_OKAY) { michael@0: MP_TO_SEC_ERROR(err); michael@0: rv = SECFailure; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: /* Calculate base = gx1*gx3*gx4 (mod p), i.e. g^(x1+x3+x4) (mod p) */ michael@0: static mp_err michael@0: jpake_Round2Base(const SECItem * gx1, const SECItem * gx3, michael@0: const SECItem * gx4, const mp_int * p, mp_int * base) michael@0: { michael@0: mp_err err; michael@0: mp_int GX1; michael@0: mp_int GX3; michael@0: mp_int GX4; michael@0: mp_int tmp; michael@0: michael@0: MP_DIGITS(&GX1) = 0; michael@0: MP_DIGITS(&GX3) = 0; michael@0: MP_DIGITS(&GX4) = 0; michael@0: MP_DIGITS(&tmp) = 0; michael@0: michael@0: CHECK_MPI_OK( mp_init(&GX1) ); michael@0: CHECK_MPI_OK( mp_init(&GX3) ); michael@0: CHECK_MPI_OK( mp_init(&GX4) ); michael@0: CHECK_MPI_OK( mp_init(&tmp) ); michael@0: michael@0: SECITEM_TO_MPINT(*gx1, &GX1); michael@0: SECITEM_TO_MPINT(*gx3, &GX3); michael@0: SECITEM_TO_MPINT(*gx4, &GX4); michael@0: michael@0: /* In round 2, the peer/attacker sends us g^x3 and g^x4 and the protocol michael@0: requires that these values are distinct. */ michael@0: if (mp_cmp(&GX3, &GX4) == 0) { michael@0: return MP_BADARG; michael@0: } michael@0: michael@0: CHECK_MPI_OK( mp_mul(&GX1, &GX3, &tmp) ); michael@0: CHECK_MPI_OK( mp_mul(&tmp, &GX4, &tmp) ); michael@0: CHECK_MPI_OK( mp_mod(&tmp, p, base) ); michael@0: michael@0: cleanup: michael@0: mp_clear(&GX1); michael@0: mp_clear(&GX3); michael@0: mp_clear(&GX4); michael@0: mp_clear(&tmp); michael@0: return err; michael@0: } michael@0: michael@0: SECStatus michael@0: JPAKE_Round2(PLArenaPool * arena, michael@0: const SECItem * p, const SECItem *q, const SECItem * gx1, michael@0: const SECItem * gx3, const SECItem * gx4, SECItem * base, michael@0: const SECItem * x2, const SECItem * s, SECItem * x2s) michael@0: { michael@0: mp_err err; michael@0: mp_int P; michael@0: mp_int Q; michael@0: mp_int X2; michael@0: mp_int S; michael@0: mp_int result; michael@0: michael@0: if (!arena || michael@0: !p || !p->data || p->len == 0 || michael@0: !q || !q->data || q->len == 0 || michael@0: !gx1 || !gx1->data || gx1->len == 0 || michael@0: !gx3 || !gx3->data || gx3->len == 0 || michael@0: !gx4 || !gx4->data || gx4->len == 0 || michael@0: !base || base->data != NULL || michael@0: (x2s != NULL && (x2s->data != NULL || michael@0: !x2 || !x2->data || x2->len == 0 || michael@0: !s || !s->data || s->len == 0))) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: MP_DIGITS(&P) = 0; michael@0: MP_DIGITS(&Q) = 0; michael@0: MP_DIGITS(&X2) = 0; michael@0: MP_DIGITS(&S) = 0; michael@0: MP_DIGITS(&result) = 0; michael@0: michael@0: CHECK_MPI_OK( mp_init(&P) ); michael@0: CHECK_MPI_OK( mp_init(&Q) ); michael@0: CHECK_MPI_OK( mp_init(&result) ); michael@0: michael@0: if (x2s != NULL) { michael@0: CHECK_MPI_OK( mp_init(&X2) ); michael@0: CHECK_MPI_OK( mp_init(&S) ); michael@0: michael@0: SECITEM_TO_MPINT(*q, &Q); michael@0: SECITEM_TO_MPINT(*x2, &X2); michael@0: michael@0: SECITEM_TO_MPINT(*s, &S); michael@0: /* S must be in [1, Q-1] */ michael@0: if (mp_cmp_z(&S) <= 0 || mp_cmp(&S, &Q) >= 0) { michael@0: err = MP_BADARG; michael@0: goto cleanup; michael@0: } michael@0: michael@0: CHECK_MPI_OK( mp_mulmod(&X2, &S, &Q, &result) ); michael@0: MPINT_TO_SECITEM(&result, x2s, arena); michael@0: } michael@0: michael@0: SECITEM_TO_MPINT(*p, &P); michael@0: CHECK_MPI_OK( jpake_Round2Base(gx1, gx3, gx4, &P, &result) ); michael@0: MPINT_TO_SECITEM(&result, base, arena); michael@0: michael@0: cleanup: michael@0: mp_clear(&P); michael@0: mp_clear(&Q); michael@0: mp_clear(&X2); michael@0: mp_clear(&S); michael@0: mp_clear(&result); michael@0: michael@0: if (err != MP_OKAY) { michael@0: MP_TO_SEC_ERROR(err); michael@0: return SECFailure; michael@0: } michael@0: return SECSuccess; michael@0: } michael@0: michael@0: SECStatus michael@0: JPAKE_Final(PLArenaPool * arena, const SECItem * p, const SECItem * q, michael@0: const SECItem * x2, const SECItem * gx4, const SECItem * x2s, michael@0: const SECItem * B, SECItem * K) michael@0: { michael@0: mp_err err; michael@0: mp_int P; michael@0: mp_int Q; michael@0: mp_int tmp; michael@0: mp_int exponent; michael@0: mp_int divisor; michael@0: mp_int base; michael@0: michael@0: if (!arena || michael@0: !p || !p->data || p->len == 0 || michael@0: !q || !q->data || q->len == 0 || michael@0: !x2 || !x2->data || x2->len == 0 || michael@0: !gx4 || !gx4->data || gx4->len == 0 || michael@0: !x2s || !x2s->data || x2s->len == 0 || michael@0: !B || !B->data || B->len == 0 || michael@0: !K || K->data != NULL) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: MP_DIGITS(&P) = 0; michael@0: MP_DIGITS(&Q) = 0; michael@0: MP_DIGITS(&tmp) = 0; michael@0: MP_DIGITS(&exponent) = 0; michael@0: MP_DIGITS(&divisor) = 0; michael@0: MP_DIGITS(&base) = 0; michael@0: michael@0: CHECK_MPI_OK( mp_init(&P) ); michael@0: CHECK_MPI_OK( mp_init(&Q) ); michael@0: CHECK_MPI_OK( mp_init(&tmp) ); michael@0: CHECK_MPI_OK( mp_init(&exponent) ); michael@0: CHECK_MPI_OK( mp_init(&divisor) ); michael@0: CHECK_MPI_OK( mp_init(&base) ); michael@0: michael@0: /* exponent = -x2s (mod q) */ michael@0: SECITEM_TO_MPINT(*q, &Q); michael@0: SECITEM_TO_MPINT(*x2s, &tmp); michael@0: /* q == 0 (mod q), so q - x2s == -x2s (mod q) */ michael@0: CHECK_MPI_OK( mp_sub(&Q, &tmp, &exponent) ); michael@0: michael@0: /* divisor = gx4^-x2s = 1/(gx4^x2s) (mod p) */ michael@0: SECITEM_TO_MPINT(*p, &P); michael@0: SECITEM_TO_MPINT(*gx4, &tmp); michael@0: CHECK_MPI_OK( mp_exptmod(&tmp, &exponent, &P, &divisor) ); michael@0: michael@0: /* base = B*divisor = B/(gx4^x2s) (mod p) */ michael@0: SECITEM_TO_MPINT(*B, &tmp); michael@0: CHECK_MPI_OK( mp_mulmod(&divisor, &tmp, &P, &base) ); michael@0: michael@0: /* tmp = base^x2 (mod p) */ michael@0: SECITEM_TO_MPINT(*x2, &exponent); michael@0: CHECK_MPI_OK( mp_exptmod(&base, &exponent, &P, &tmp) ); michael@0: michael@0: MPINT_TO_SECITEM(&tmp, K, arena); michael@0: michael@0: cleanup: michael@0: mp_clear(&P); michael@0: mp_clear(&Q); michael@0: mp_clear(&tmp); michael@0: mp_clear(&exponent); michael@0: mp_clear(&divisor); michael@0: mp_clear(&base); michael@0: michael@0: if (err != MP_OKAY) { michael@0: MP_TO_SEC_ERROR(err); michael@0: return SECFailure; michael@0: } michael@0: return SECSuccess; michael@0: }