1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is Mozilla Fonudation.
17 * Portions created by the Initial Developer are Copyright (C) 2010
18 * the Initial Developer. All Rights Reserved.
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 2 or later (the "GPL"), or
24 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK ***** */
43 jpake_mapStatus(SECStatus rv, CK_RV invalidArgsMapping) {
47 err = PORT_GetError();
49 /* XXX: SEC_ERROR_INVALID_ARGS might be caused by invalid template
51 case SEC_ERROR_INVALID_ARGS: return invalidArgsMapping;
52 case SEC_ERROR_BAD_SIGNATURE: return CKR_SIGNATURE_INVALID;
53 case SEC_ERROR_NO_MEMORY: return CKR_HOST_MEMORY;
55 return CKR_FUNCTION_FAILED;
58 /* If key is not NULL then the gx value will be stored as an attribute with
59 the type given by the gxAttrType parameter. */
61 jpake_Sign(PLArenaPool * arena, const PQGParams * pqg, HASH_HashType hashType,
62 const SECItem * signerID, const SECItem * x,
63 CK_NSS_JPAKEPublicValue * out)
68 PORT_Assert(arena != NULL);
73 crv = jpake_mapStatus(JPAKE_Sign(arena, pqg, hashType, signerID, x, NULL,
75 CKR_MECHANISM_PARAM_INVALID);
77 if ((out->pGX != NULL && out->ulGXLen >= gx.len) ||
78 (out->pGV != NULL && out->ulGVLen >= gv.len) ||
79 (out->pR != NULL && out->ulRLen >= r.len)) {
80 PORT_Memcpy(out->pGX, gx.data, gx.len);
81 PORT_Memcpy(out->pGV, gv.data, gv.len);
82 PORT_Memcpy(out->pR, r.data, r.len);
83 out->ulGXLen = gx.len;
84 out->ulGVLen = gv.len;
87 crv = CKR_MECHANISM_PARAM_INVALID;
94 jpake_Verify(PLArenaPool * arena, const PQGParams * pqg,
95 HASH_HashType hashType, const SECItem * signerID,
96 const CK_BYTE * peerIDData, CK_ULONG peerIDLen,
97 const CK_NSS_JPAKEPublicValue * publicValueIn)
99 SECItem peerID, gx, gv, r;
100 peerID.data = (unsigned char *) peerIDData; peerID.len = peerIDLen;
101 gx.data = publicValueIn->pGX; gx.len = publicValueIn->ulGXLen;
102 gv.data = publicValueIn->pGV; gv.len = publicValueIn->ulGVLen;
103 r.data = publicValueIn->pR; r.len = publicValueIn->ulRLen;
104 return jpake_mapStatus(JPAKE_Verify(arena, pqg, hashType, signerID, &peerID,
106 CKR_MECHANISM_PARAM_INVALID);
109 #define NUM_ELEM(x) (sizeof (x) / sizeof (x)[0])
111 /* If the template has the key type set, ensure that it was set to the correct
112 * value. If the template did not have the key type set, set it to the
116 jpake_enforceKeyType(SFTKObject * key, CK_KEY_TYPE keyType) {
118 SFTKAttribute * keyTypeAttr = sftk_FindAttribute(key, CKA_KEY_TYPE);
119 if (keyTypeAttr != NULL) {
120 crv = *(CK_KEY_TYPE *)keyTypeAttr->attrib.pValue == keyType
122 : CKR_TEMPLATE_INCONSISTENT;
123 sftk_FreeAttribute(keyTypeAttr);
125 crv = sftk_forceAttribute(key, CKA_KEY_TYPE, &keyType, sizeof keyType);
131 jpake_MultipleSecItem2Attribute(SFTKObject * key, const SFTKItemTemplate * attrs,
136 for (i = 0; i < attrsCount; ++i) {
137 CK_RV crv = sftk_forceAttribute(key, attrs[i].type, attrs[i].item->data,
146 jpake_Round1(HASH_HashType hashType, CK_NSS_JPAKERound1Params * params,
153 SFTKItemTemplate templateAttrs[] = {
154 { CKA_PRIME, &pqg.prime },
155 { CKA_SUBPRIME, &pqg.subPrime },
156 { CKA_BASE, &pqg.base },
157 { CKA_NSS_JPAKE_SIGNERID, &signerID }
159 SECItem x2, gx1, gx2;
160 const SFTKItemTemplate generatedAttrs[] = {
161 { CKA_NSS_JPAKE_X2, &x2 },
162 { CKA_NSS_JPAKE_GX1, &gx1 },
163 { CKA_NSS_JPAKE_GX2, &gx2 },
167 PORT_Assert(params != NULL);
168 PORT_Assert(key != NULL);
170 arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE);
172 crv = CKR_HOST_MEMORY;
174 crv = sftk_MultipleAttribute2SecItem(arena, key, templateAttrs,
175 NUM_ELEM(templateAttrs));
177 if (crv == CKR_OK && (signerID.data == NULL || signerID.len == 0))
178 crv = CKR_TEMPLATE_INCOMPLETE;
180 /* generate x1, g^x1 and the proof of knowledge of x1 */
183 crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x1),
184 CKR_TEMPLATE_INCONSISTENT);
187 crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x1, ¶ms->gx1);
189 /* generate x2, g^x2 and the proof of knowledge of x2 */
192 crv = jpake_mapStatus(DSA_NewRandom(arena, &pqg.subPrime, &x2),
193 CKR_TEMPLATE_INCONSISTENT);
196 crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2, ¶ms->gx2);
198 /* Save the values needed for round 2 into CKA_VALUE */
200 gx1.data = params->gx1.pGX;
201 gx1.len = params->gx1.ulGXLen;
202 gx2.data = params->gx2.pGX;
203 gx2.len = params->gx2.ulGXLen;
204 crv = jpake_MultipleSecItem2Attribute(key, generatedAttrs,
205 NUM_ELEM(generatedAttrs));
208 PORT_FreeArena(arena, PR_TRUE);
213 jpake_Round2(HASH_HashType hashType, CK_NSS_JPAKERound2Params * params,
214 SFTKObject * sourceKey, SFTKObject * key)
219 SECItem signerID, x2, gx1, gx2;
220 SFTKItemTemplate sourceAttrs[] = {
221 { CKA_PRIME, &pqg.prime },
222 { CKA_SUBPRIME, &pqg.subPrime },
223 { CKA_BASE, &pqg.base },
224 { CKA_NSS_JPAKE_SIGNERID, &signerID },
225 { CKA_NSS_JPAKE_X2, &x2 },
226 { CKA_NSS_JPAKE_GX1, &gx1 },
227 { CKA_NSS_JPAKE_GX2, &gx2 },
229 SECItem x2s, gx3, gx4;
230 const SFTKItemTemplate copiedAndGeneratedAttrs[] = {
231 { CKA_NSS_JPAKE_SIGNERID, &signerID },
232 { CKA_PRIME, &pqg.prime },
233 { CKA_SUBPRIME, &pqg.subPrime },
234 { CKA_NSS_JPAKE_X2, &x2 },
235 { CKA_NSS_JPAKE_X2S, &x2s },
236 { CKA_NSS_JPAKE_GX1, &gx1 },
237 { CKA_NSS_JPAKE_GX2, &gx2 },
238 { CKA_NSS_JPAKE_GX3, &gx3 },
239 { CKA_NSS_JPAKE_GX4, &gx4 }
243 PORT_Assert(params != NULL);
244 PORT_Assert(sourceKey != NULL);
245 PORT_Assert(key != NULL);
247 arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE);
249 crv = CKR_HOST_MEMORY;
251 /* TODO: check CKK_NSS_JPAKE_ROUND1 */
253 crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs,
254 NUM_ELEM(sourceAttrs));
256 /* Get the peer's ID out of the template and sanity-check it. */
258 crv = sftk_Attribute2SecItem(arena, &peerID, key,
259 CKA_NSS_JPAKE_PEERID);
260 if (crv == CKR_OK && (peerID.data == NULL || peerID.len == 0))
261 crv = CKR_TEMPLATE_INCOMPLETE;
262 if (crv == CKR_OK && SECITEM_CompareItem(&signerID, &peerID) == SECEqual)
263 crv = CKR_TEMPLATE_INCONSISTENT;
265 /* Verify zero-knowledge proofs for g^x3 and g^x4 */
267 crv = jpake_Verify(arena, &pqg, hashType, &signerID,
268 peerID.data, peerID.len, ¶ms->gx3);
270 crv = jpake_Verify(arena, &pqg, hashType, &signerID,
271 peerID.data, peerID.len, ¶ms->gx4);
273 /* Calculate the base and x2s for A=base^x2s */
276 s.data = params->pSharedKey;
277 s.len = params->ulSharedKeyLen;
278 gx3.data = params->gx3.pGX;
279 gx3.len = params->gx3.ulGXLen;
280 gx4.data = params->gx4.pGX;
281 gx4.len = params->gx4.ulGXLen;
282 pqg.base.data = NULL;
284 crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime,
285 &gx1, &gx3, &gx4, &pqg.base,
287 CKR_MECHANISM_PARAM_INVALID);
290 /* Generate A=base^x2s and its zero-knowledge proof. */
292 crv = jpake_Sign(arena, &pqg, hashType, &signerID, &x2s, ¶ms->A);
294 /* Copy P and Q from the ROUND1 key to the ROUND2 key and save the values
295 needed for the final key material derivation into CKA_VALUE. */
297 crv = sftk_forceAttribute(key, CKA_PRIME, pqg.prime.data,
300 crv = sftk_forceAttribute(key, CKA_SUBPRIME, pqg.subPrime.data,
303 crv = jpake_MultipleSecItem2Attribute(key, copiedAndGeneratedAttrs,
304 NUM_ELEM(copiedAndGeneratedAttrs));
308 crv = jpake_enforceKeyType(key, CKK_NSS_JPAKE_ROUND2);
310 PORT_FreeArena(arena, PR_TRUE);
315 jpake_Final(HASH_HashType hashType, const CK_NSS_JPAKEFinalParams * param,
316 SFTKObject * sourceKey, SFTKObject * key)
322 SECItem peerID, signerID, x2s, x2, gx1, gx2, gx3, gx4;
323 SFTKItemTemplate sourceAttrs[] = {
324 { CKA_NSS_JPAKE_PEERID, &peerID },
325 { CKA_NSS_JPAKE_SIGNERID, &signerID },
326 { CKA_PRIME, &pqg.prime },
327 { CKA_SUBPRIME, &pqg.subPrime },
328 { CKA_NSS_JPAKE_X2, &x2 },
329 { CKA_NSS_JPAKE_X2S, &x2s },
330 { CKA_NSS_JPAKE_GX1, &gx1 },
331 { CKA_NSS_JPAKE_GX2, &gx2 },
332 { CKA_NSS_JPAKE_GX3, &gx3 },
333 { CKA_NSS_JPAKE_GX4, &gx4 }
336 PORT_Assert(param != NULL);
337 PORT_Assert(sourceKey != NULL);
338 PORT_Assert(key != NULL);
340 arena = PORT_NewArena(NSS_SOFTOKEN_DEFAULT_CHUNKSIZE);
342 crv = CKR_HOST_MEMORY;
344 /* TODO: verify key type CKK_NSS_JPAKE_ROUND2 */
346 crv = sftk_MultipleAttribute2SecItem(arena, sourceKey, sourceAttrs,
347 NUM_ELEM(sourceAttrs));
349 /* Calculate base for B=base^x4s */
351 pqg.base.data = NULL;
352 crv = jpake_mapStatus(JPAKE_Round2(arena, &pqg.prime, &pqg.subPrime,
353 &gx1, &gx2, &gx3, &pqg.base,
355 CKR_MECHANISM_PARAM_INVALID);
358 /* Verify zero-knowledge proof for B */
360 crv = jpake_Verify(arena, &pqg, hashType, &signerID,
361 peerID.data, peerID.len, ¶m->B);
364 B.data = param->B.pGX;
365 B.len = param->B.ulGXLen;
367 crv = jpake_mapStatus(JPAKE_Final(arena, &pqg.prime, &pqg.subPrime,
368 &x2, &gx4, &x2s, &B, &K),
369 CKR_MECHANISM_PARAM_INVALID);
372 /* Save key material into CKA_VALUE. */
374 crv = sftk_forceAttribute(key, CKA_VALUE, K.data, K.len);
377 crv = jpake_enforceKeyType(key, CKK_GENERIC_SECRET);
379 PORT_FreeArena(arena, PR_TRUE);