xref: /titanic_51/usr/src/lib/pkcs11/pkcs11_tpm/common/mech_md5.c (revision 47e946e784719ae402ace34695f67b0e6e76ae5c)
1 /*
2  *		Common Public License Version 0.5
3  *
4  *		THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
5  *		THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
6  *		REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
7  *		RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
8  *
9  *		1. DEFINITIONS
10  *
11  *		"Contribution" means:
12  *		      a) in the case of the initial Contributor, the
13  *		      initial code and documentation distributed under
14  *		      this Agreement, and
15  *
16  *		      b) in the case of each subsequent Contributor:
17  *		      i) changes to the Program, and
18  *		      ii) additions to the Program;
19  *
20  *		      where such changes and/or additions to the Program
21  *		      originate from and are distributed by that
22  *		      particular Contributor. A Contribution 'originates'
23  *		      from a Contributor if it was added to the Program
24  *		      by such Contributor itself or anyone acting on such
25  *		      Contributor's behalf. Contributions do not include
26  *		      additions to the Program which: (i) are separate
27  *		      modules of software distributed in conjunction with
28  *		      the Program under their own license agreement, and
29  *		      (ii) are not derivative works of the Program.
30  *
31  *
32  *		"Contributor" means any person or entity that distributes
33  *		the Program.
34  *
35  *		"Licensed Patents " mean patent claims licensable by a
36  *		Contributor which are necessarily infringed by the use or
37  *		sale of its Contribution alone or when combined with the
38  *		Program.
39  *
40  *		"Program" means the Contributions distributed in
41  *		accordance with this Agreement.
42  *
43  *		"Recipient" means anyone who receives the Program under
44  *		this Agreement, including all Contributors.
45  *
46  *		2. GRANT OF RIGHTS
47  *
48  *		      a) Subject to the terms of this Agreement, each
49  *		      Contributor hereby grants Recipient a
50  *		      non-exclusive, worldwide, royalty-free copyright
51  *		      license to reproduce, prepare derivative works of,
52  *		      publicly display, publicly perform, distribute and
53  *		      sublicense the Contribution of such Contributor, if
54  *		      any, and such derivative works, in source code and
55  *		      object code form.
56  *
57  *		      b) Subject to the terms of this Agreement, each
58  *		      Contributor hereby grants Recipient a
59  *		      non-exclusive, worldwide, royalty-free patent
60  *		      license under Licensed Patents to make, use, sell,
61  *		      offer to sell, import and otherwise transfer the
62  *		      Contribution of such Contributor, if any, in source
63  *		      code and object code form. This patent license
64  *		      shall apply to the combination of the Contribution
65  *		      and the Program if, at the time the Contribution is
66  *		      added by the Contributor, such addition of the
67  *		      Contribution causes such combination to be covered
68  *		      by the Licensed Patents. The patent license shall
69  *		      not apply to any other combinations which include
70  *		      the Contribution. No hardware per se is licensed
71  *		      hereunder.
72  *
73  *		      c) Recipient understands that although each
74  *		      Contributor grants the licenses to its
75  *		      Contributions set forth herein, no assurances are
76  *		      provided by any Contributor that the Program does
77  *		      not infringe the patent or other intellectual
78  *		      property rights of any other entity. Each
79  *		      Contributor disclaims any liability to Recipient
80  *		      for claims brought by any other entity based on
81  *		      infringement of intellectual property rights or
82  *		      otherwise. As a condition to exercising the rights
83  *		      and licenses granted hereunder, each Recipient
84  *		      hereby assumes sole responsibility to secure any
85  *		      other intellectual property rights needed, if any.
86  *
87  *		      For example, if a third party patent license is
88  *		      required to allow Recipient to distribute the
89  *		      Program, it is Recipient's responsibility to
90  *		      acquire that license before distributing the
91  *		      Program.
92  *
93  *		      d) Each Contributor represents that to its
94  *		      knowledge it has sufficient copyright rights in its
95  *		      Contribution, if any, to grant the copyright
96  *		      license set forth in this Agreement.
97  *
98  *		3. REQUIREMENTS
99  *
100  *		A Contributor may choose to distribute the Program in
101  *		object code form under its own license agreement, provided
102  *		that:
103  *		      a) it complies with the terms and conditions of
104  *		      this Agreement; and
105  *
106  *		      b) its license agreement:
107  *		      i) effectively disclaims on behalf of all
108  *		      Contributors all warranties and conditions, express
109  *		      and implied, including warranties or conditions of
110  *		      title and non-infringement, and implied warranties
111  *		      or conditions of merchantability and fitness for a
112  *		      particular purpose;
113  *
114  *		      ii) effectively excludes on behalf of all
115  *		      Contributors all liability for damages, including
116  *		      direct, indirect, special, incidental and
117  *		      consequential damages, such as lost profits;
118  *
119  *		      iii) states that any provisions which differ from
120  *		      this Agreement are offered by that Contributor
121  *		      alone and not by any other party; and
122  *
123  *		      iv) states that source code for the Program is
124  *		      available from such Contributor, and informs
125  *		      licensees how to obtain it in a reasonable manner
126  *		      on or through a medium customarily used for
127  *		      software exchange.
128  *
129  *		When the Program is made available in source code form:
130  *		      a) it must be made available under this Agreement;
131  *		      and
132  *		      b) a copy of this Agreement must be included with
133  *		      each copy of the Program.
134  *
135  *		Contributors may not remove or alter any copyright notices
136  *		contained within the Program.
137  *
138  *		Each Contributor must identify itself as the originator of
139  *		its Contribution, if any, in a manner that reasonably
140  *		allows subsequent Recipients to identify the originator of
141  *		the Contribution.
142  *
143  *
144  *		4. COMMERCIAL DISTRIBUTION
145  *
146  *		Commercial distributors of software may accept certain
147  *		responsibilities with respect to end users, business
148  *		partners and the like. While this license is intended to
149  *		facilitate the commercial use of the Program, the
150  *		Contributor who includes the Program in a commercial
151  *		product offering should do so in a manner which does not
152  *		create potential liability for other Contributors.
153  *		Therefore, if a Contributor includes the Program in a
154  *		commercial product offering, such Contributor ("Commercial
155  *		Contributor") hereby agrees to defend and indemnify every
156  *		other Contributor ("Indemnified Contributor") against any
157  *		losses, damages and costs (collectively "Losses") arising
158  *		from claims, lawsuits and other legal actions brought by a
159  *		third party against the Indemnified Contributor to the
160  *		extent caused by the acts or omissions of such Commercial
161  *		Contributor in connection with its distribution of the
162  *		Program in a commercial product offering. The obligations
163  *		in this section do not apply to any claims or Losses
164  *		relating to any actual or alleged intellectual property
165  *		infringement. In order to qualify, an Indemnified
166  *		Contributor must: a) promptly notify the Commercial
167  *		Contributor in writing of such claim, and b) allow the
168  *		Commercial Contributor to control, and cooperate with the
169  *		Commercial Contributor in, the defense and any related
170  *		settlement negotiations. The Indemnified Contributor may
171  *		participate in any such claim at its own expense.
172  *
173  *
174  *		For example, a Contributor might include the Program in a
175  *		commercial product offering, Product X. That Contributor
176  *		is then a Commercial Contributor. If that Commercial
177  *		Contributor then makes performance claims, or offers
178  *		warranties related to Product X, those performance claims
179  *		and warranties are such Commercial Contributor's
180  *		responsibility alone. Under this section, the Commercial
181  *		Contributor would have to defend claims against the other
182  *		Contributors related to those performance claims and
183  *		warranties, and if a court requires any other Contributor
184  *		to pay any damages as a result, the Commercial Contributor
185  *		must pay those damages.
186  *
187  *
188  *		5. NO WARRANTY
189  *
190  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
191  *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
192  *		WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
193  *		IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
194  *		CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
195  *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
196  *		responsible for determining the appropriateness of using
197  *		and distributing the Program and assumes all risks
198  *		associated with its exercise of rights under this
199  *		Agreement, including but not limited to the risks and
200  *		costs of program errors, compliance with applicable laws,
201  *		damage to or loss of data, programs or equipment, and
202  *		unavailability or interruption of operations.
203  *
204  *		6. DISCLAIMER OF LIABILITY
205  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
206  *		RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
207  *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
208  *		OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
209  *		LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
210  *		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211  *		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
212  *		OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
213  *		OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
214  *		POSSIBILITY OF SUCH DAMAGES.
215  *
216  *		7. GENERAL
217  *
218  *		If any provision of this Agreement is invalid or
219  *		unenforceable under applicable law, it shall not affect
220  *		the validity or enforceability of the remainder of the
221  *		terms of this Agreement, and without further action by the
222  *		parties hereto, such provision shall be reformed to the
223  *		minimum extent necessary to make such provision valid and
224  *		enforceable.
225  *
226  *
227  *		If Recipient institutes patent litigation against a
228  *		Contributor with respect to a patent applicable to
229  *		software (including a cross-claim or counterclaim in a
230  *		lawsuit), then any patent licenses granted by that
231  *		Contributor to such Recipient under this Agreement shall
232  *		terminate as of the date such litigation is filed. In
233  *		addition, If Recipient institutes patent litigation
234  *		against any entity (including a cross-claim or
235  *		counterclaim in a lawsuit) alleging that the Program
236  *		itself (excluding combinations of the Program with other
237  *		software or hardware) infringes such Recipient's
238  *		patent(s), then such Recipient's rights granted under
239  *		Section 2(b) shall terminate as of the date such
240  *		litigation is filed.
241  *
242  *		All Recipient's rights under this Agreement shall
243  *		terminate if it fails to comply with any of the material
244  *		terms or conditions of this Agreement and does not cure
245  *		such failure in a reasonable period of time after becoming
246  *		aware of such noncompliance. If all Recipient's rights
247  *		under this Agreement terminate, Recipient agrees to cease
248  *		use and distribution of the Program as soon as reasonably
249  *		practicable. However, Recipient's obligations under this
250  *		Agreement and any licenses granted by Recipient relating
251  *		to the Program shall continue and survive.
252  *
253  *		Everyone is permitted to copy and distribute copies of
254  *		this Agreement, but in order to avoid inconsistency the
255  *		Agreement is copyrighted and may only be modified in the
256  *		following manner. The Agreement Steward reserves the right
257  *		to publish new versions (including revisions) of this
258  *		Agreement from time to time. No one other than the
259  *		Agreement Steward has the right to modify this Agreement.
260  *
261  *		IBM is the initial Agreement Steward. IBM may assign the
262  *		responsibility to serve as the Agreement Steward to a
263  *		suitable separate entity. Each new version of the
264  *		Agreement will be given a distinguishing version number.
265  *		The Program (including Contributions) may always be
266  *		distributed subject to the version of the Agreement under
267  *		which it was received. In addition, after a new version of
268  *		the Agreement is published, Contributor may elect to
269  *		distribute the Program (including its Contributions) under
270  *		the new version. Except as expressly stated in Sections
271  *		2(a) and 2(b) above, Recipient receives no rights or
272  *		licenses to the intellectual property of any Contributor
273  *		under this Agreement, whether expressly, by implication,
274  *		estoppel or otherwise. All rights in the Program not
275  *		expressly granted under this Agreement are reserved.
276  *
277  *
278  *		This Agreement is governed by the laws of the State of New
279  *		York and the intellectual property laws of the United
280  *		States of America. No party to this Agreement will bring a
281  *		legal action under this Agreement more than one year after
282  *		the cause of action arose. Each party waives its rights to
283  *		a jury trial in any resulting litigation.
284  *
285  *
286  *
287  * (C) COPYRIGHT International Business Machines Corp. 2001,2002
288  */
289 /*
290  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
291  * Use is subject to license terms.
292  */
293 
294 #include "tpmtok_int.h"
295 
296 CK_RV
297 md5_hash(SESSION	*sess,
298 	CK_BBOOL	length_only,
299 	DIGEST_CONTEXT  *ctx,
300 	CK_BYTE	 *in_data,
301 	CK_ULONG	in_data_len,
302 	CK_BYTE	 *out_data,
303 	CK_ULONG	*out_data_len)
304 {
305 	if (! sess || ! ctx || ! out_data_len) {
306 		return (CKR_FUNCTION_FAILED);
307 	}
308 
309 	*out_data_len = MD5_DIGEST_LENGTH;
310 	if (length_only == TRUE) {
311 		return (CKR_OK);
312 	}
313 
314 	MD5Update(ctx->context.md5ctx, in_data, in_data_len);
315 	MD5Final(out_data, ctx->context.md5ctx);
316 
317 	return (CKR_OK);
318 }
319 
320 CK_RV
321 md5_hmac_sign(SESSION		* sess,
322 	CK_BBOOL length_only,
323 	SIGN_VERIFY_CONTEXT  * ctx,
324 	CK_BYTE	* in_data,
325 	CK_ULONG in_data_len,
326 	CK_BYTE	* out_data,
327 	CK_ULONG * out_data_len)
328 {
329 	OBJECT	 * key_obj = NULL;
330 	CK_ATTRIBUTE    * attr    = NULL;
331 	CK_BYTE	   hash[MD5_DIGEST_LENGTH];
332 	DIGEST_CONTEXT    digest_ctx;
333 	CK_MECHANISM	digest_mech;
334 	CK_BYTE	   k_ipad[MD5_BLOCK_SIZE];
335 	CK_BYTE	   k_opad[MD5_BLOCK_SIZE];
336 	CK_ULONG  key_bytes, hash_len, hmac_len;
337 	CK_ULONG  i;
338 	CK_RV rc;
339 
340 	if (! sess || ! ctx || ! out_data_len) {
341 		return (CKR_FUNCTION_FAILED);
342 	}
343 
344 	if (ctx->mech.mechanism == CKM_MD5_HMAC_GENERAL) {
345 		hmac_len = *(CK_ULONG *)ctx->mech.pParameter;
346 
347 		if (hmac_len == 0) {
348 			*out_data_len = 0;
349 			return (CKR_OK);
350 		}
351 	}
352 	else
353 		hmac_len = MD5_DIGEST_LENGTH;
354 
355 
356 	*out_data_len = hmac_len;
357 	if (length_only == TRUE) {
358 		return (CKR_OK);
359 	}
360 
361 	(void) memset(&digest_ctx, 0x0, sizeof (DIGEST_CONTEXT));
362 
363 	rc = object_mgr_find_in_map1(sess->hContext, ctx->key, &key_obj);
364 	if (rc != CKR_OK)
365 		return (rc);
366 	rc = template_attribute_find(key_obj->template, CKA_VALUE, &attr);
367 	if (rc == FALSE) {
368 		return (CKR_FUNCTION_FAILED);
369 	}
370 	else
371 		key_bytes = attr->ulValueLen;
372 
373 	// build (K XOR ipad), (K XOR opad)
374 	//
375 	if (key_bytes > MD5_BLOCK_SIZE) {
376 		digest_mech.mechanism	= CKM_MD5;
377 		digest_mech.ulParameterLen = 0;
378 		digest_mech.pParameter	= NULL;
379 
380 		rc = digest_mgr_init(sess, &digest_ctx, &digest_mech);
381 		if (rc != CKR_OK) {
382 			(void) digest_mgr_cleanup(&digest_ctx);
383 			return (rc);
384 		}
385 
386 		hash_len = *out_data_len;
387 		rc = digest_mgr_digest(sess, FALSE, &digest_ctx,
388 		    attr->pValue, attr->ulValueLen,
389 		    hash,  &hash_len);
390 		if (rc != CKR_OK) {
391 			(void) digest_mgr_cleanup(&digest_ctx);
392 			return (rc);
393 		}
394 
395 		(void) digest_mgr_cleanup(&digest_ctx);
396 		(void) memset(&digest_ctx, 0x0, sizeof (DIGEST_CONTEXT));
397 
398 		for (i = 0; i < hash_len; i++) {
399 			k_ipad[i] = hash[i] ^ 0x36;
400 			k_opad[i] = hash[i] ^ 0x5C;
401 		}
402 
403 		(void) memset(&k_ipad[i], 0x36, MD5_BLOCK_SIZE - i);
404 		(void) memset(&k_opad[i], 0x5C, MD5_BLOCK_SIZE - i);
405 	} else {
406 		CK_BYTE *key = attr->pValue;
407 
408 		for (i = 0; i < key_bytes; i++) {
409 			k_ipad[i] = key[i] ^ 0x36;
410 			k_opad[i] = key[i] ^ 0x5C;
411 		}
412 
413 		(void) memset(&k_ipad[i], 0x36, MD5_BLOCK_SIZE - key_bytes);
414 		(void) memset(&k_opad[i], 0x5C, MD5_BLOCK_SIZE - key_bytes);
415 	}
416 
417 	digest_mech.mechanism	= CKM_MD5;
418 	digest_mech.ulParameterLen = 0;
419 	digest_mech.pParameter	= NULL;
420 
421 	rc = digest_mgr_init(sess, &digest_ctx, &digest_mech);
422 	if (rc != CKR_OK) {
423 		(void) digest_mgr_cleanup(&digest_ctx);
424 		return (rc);
425 	}
426 
427 	rc = digest_mgr_digest_update(sess, &digest_ctx,
428 	    k_ipad, MD5_BLOCK_SIZE);
429 	if (rc != CKR_OK) {
430 		(void) digest_mgr_cleanup(&digest_ctx);
431 		return (rc);
432 	}
433 
434 	rc = digest_mgr_digest_update(sess, &digest_ctx,
435 	    in_data, in_data_len);
436 	if (rc != CKR_OK) {
437 		(void) digest_mgr_cleanup(&digest_ctx);
438 		return (rc);
439 	}
440 
441 	hash_len = sizeof (hash);
442 	rc = digest_mgr_digest_final(sess, &digest_ctx, hash, &hash_len);
443 	if (rc != CKR_OK) {
444 		(void) digest_mgr_cleanup(&digest_ctx);
445 		return (rc);
446 	}
447 
448 	(void) digest_mgr_cleanup(&digest_ctx);
449 	(void) memset(&digest_ctx, 0x0, sizeof (DIGEST_CONTEXT));
450 
451 	rc = digest_mgr_init(sess, &digest_ctx, &digest_mech);
452 	if (rc != CKR_OK) {
453 		(void) digest_mgr_cleanup(&digest_ctx);
454 		return (rc);
455 	}
456 
457 	rc = digest_mgr_digest_update(sess, &digest_ctx,
458 	    k_opad, MD5_BLOCK_SIZE);
459 	if (rc != CKR_OK) {
460 		(void) digest_mgr_cleanup(&digest_ctx);
461 		return (rc);
462 	}
463 
464 	rc = digest_mgr_digest_update(sess, &digest_ctx, hash, hash_len);
465 	if (rc != CKR_OK) {
466 		(void) digest_mgr_cleanup(&digest_ctx);
467 		return (rc);
468 	}
469 
470 	hash_len = sizeof (hash);
471 	rc = digest_mgr_digest_final(sess, &digest_ctx, hash, &hash_len);
472 	if (rc != CKR_OK) {
473 		(void) digest_mgr_cleanup(&digest_ctx);
474 		return (rc);
475 	}
476 
477 	(void) memcpy(out_data, hash, hmac_len);
478 	*out_data_len = hmac_len;
479 
480 	(void) digest_mgr_cleanup(&digest_ctx);
481 	return (CKR_OK);
482 }
483 
484 CK_RV
485 md5_hmac_verify(SESSION		* sess,
486 	SIGN_VERIFY_CONTEXT  * ctx,
487 	CK_BYTE		* in_data,
488 	CK_ULONG	in_data_len,
489 	CK_BYTE		* signature,
490 	CK_ULONG	sig_len)
491 {
492 	CK_BYTE		hmac[MD5_DIGEST_LENGTH];
493 	SIGN_VERIFY_CONTEXT  hmac_ctx;
494 	CK_ULONG	hmac_len, len;
495 	CK_RV		rc;
496 
497 	if (! sess || ! ctx || ! in_data || ! signature) {
498 		return (CKR_FUNCTION_FAILED);
499 	}
500 	if (ctx->mech.mechanism == CKM_MD5_HMAC_GENERAL)
501 		hmac_len = *(CK_ULONG *)ctx->mech.pParameter;
502 	else
503 		hmac_len = MD5_DIGEST_LENGTH;
504 
505 	(void) memset(&hmac_ctx, 0, sizeof (SIGN_VERIFY_CONTEXT));
506 
507 	rc = sign_mgr_init(sess, &hmac_ctx, &ctx->mech, FALSE, ctx->key);
508 	if (rc != CKR_OK) {
509 		(void) sign_mgr_cleanup(&hmac_ctx);
510 		return (rc);
511 	}
512 
513 	len = sizeof (hmac);
514 	rc = sign_mgr_sign(sess, FALSE, &hmac_ctx,
515 	    in_data, in_data_len, hmac,   &len);
516 	if (rc != CKR_OK) {
517 		(void) sign_mgr_cleanup(&hmac_ctx);
518 		return (rc);
519 	}
520 
521 	if ((len != hmac_len) || (len != sig_len)) {
522 		return (CKR_SIGNATURE_LEN_RANGE);
523 	}
524 	if (memcmp(hmac, signature, hmac_len) != 0) {
525 		rc = CKR_SIGNATURE_INVALID;
526 	}
527 	(void) sign_mgr_cleanup(&hmac_ctx);
528 	return (rc);
529 }
530