Home | History | Annotate | Line # | Download | only in tspi
      1 
      2 /*
      3  * Licensed Materials - Property of IBM
      4  *
      5  * trousers - An open source TCG Software Stack
      6  *
      7  * (C) Copyright International Business Machines Corp. 2004-2007
      8  *
      9  */
     10 
     11 
     12 #include <stdlib.h>
     13 #include <stdio.h>
     14 #include <string.h>
     15 
     16 #include "trousers/tss.h"
     17 #include "trousers/trousers.h"
     18 #include "trousers_types.h"
     19 #include "spi_utils.h"
     20 #include "capabilities.h"
     21 #include "tsplog.h"
     22 #include "obj.h"
     23 
     24 
     25 /* encrypt some data with the RSA public key of 'key', using the padding appropriate for the key */
     26 TSS_RESULT
     27 __tspi_rsa_encrypt(TSS_HKEY key,
     28 	    UINT32   inDataLen,
     29 	    BYTE*    inData,
     30 	    UINT32*  outDataLen,
     31 	    BYTE*    outData)
     32 {
     33 	BYTE *blob;
     34 	UINT32 blobLen;
     35 	UINT64 offset;
     36 	TSS_RESULT result;
     37 	TSS_HCONTEXT tspContext;
     38 	TPM_PUBKEY pubKey;
     39 
     40 	if (!inData || !outDataLen || !outData)
     41 		return TSPERR(TSS_E_INTERNAL_ERROR);
     42 
     43 	if ((result = obj_rsakey_get_tsp_context(key, &tspContext)))
     44 		return result;
     45 
     46 	if ((result = obj_rsakey_get_pub_blob(key, &blobLen, &blob)))
     47 		return result;
     48 
     49 	offset = 0;
     50 	if ((result = Trspi_UnloadBlob_PUBKEY(&offset, blob, &pubKey))) {
     51 		free_tspi(tspContext, blob);
     52 		return result;
     53 	}
     54 	free_tspi(tspContext, blob);
     55 
     56 	if (pubKey.pubKey.keyLength < inDataLen) {
     57 		result = TSPERR(TSS_E_ENC_INVALID_LENGTH);
     58 		goto done;
     59 	}
     60 
     61 	if (pubKey.algorithmParms.encScheme == TPM_ES_RSAESPKCSv15 ||
     62 	    pubKey.algorithmParms.encScheme == TSS_ES_RSAESPKCSV15) {
     63 		if ((result = Trspi_RSA_PKCS15_Encrypt(inData, inDataLen, outData, outDataLen,
     64 						       pubKey.pubKey.key, pubKey.pubKey.keyLength)))
     65 			goto done;
     66 	} else {
     67 		if ((result = Trspi_TPM_RSA_OAEP_Encrypt(inData, inDataLen, outData, outDataLen,
     68 							 pubKey.pubKey.key,
     69 							 pubKey.pubKey.keyLength)))
     70 			goto done;
     71 	}
     72 
     73 done:
     74 	free(pubKey.pubKey.key);
     75 	free(pubKey.algorithmParms.parms);
     76 	return result;
     77 }
     78 
     79 TSS_RESULT
     80 __tspi_rsa_verify(TSS_HKEY key,
     81 	   UINT32   type,
     82 	   UINT32   hashLen,
     83 	   BYTE*    hash,
     84 	   UINT32   sigLen,
     85 	   BYTE*    sig)
     86 {
     87 	BYTE *blob;
     88 	UINT32 blobLen;
     89 	UINT64 offset;
     90 	TSS_RESULT result;
     91 	TSS_HCONTEXT tspContext;
     92 	TPM_PUBKEY pubKey;
     93 
     94 	if (!hash || !sig)
     95 		return TSPERR(TSS_E_INTERNAL_ERROR);
     96 
     97 	if ((result = obj_rsakey_get_tsp_context(key, &tspContext)))
     98 		return result;
     99 
    100 	if ((result = obj_rsakey_get_pub_blob(key, &blobLen, &blob)))
    101 		return result;
    102 
    103 	offset = 0;
    104 	if ((result = Trspi_UnloadBlob_PUBKEY(&offset, blob, &pubKey))) {
    105 		free_tspi(tspContext, blob);
    106 		return result;
    107 	}
    108 	free_tspi(tspContext, blob);
    109 
    110 	result = Trspi_Verify(type, hash, hashLen, pubKey.pubKey.key, pubKey.pubKey.keyLength,
    111 			      sig, sigLen);
    112 
    113 	free(pubKey.pubKey.key);
    114 	free(pubKey.algorithmParms.parms);
    115 
    116 	return result;
    117 }
    118