summaryrefslogtreecommitdiff
path: root/src/tspi/tsp_asym.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-11-25 14:36:20 +0000
committerIgor Pashev <pashev.igor@gmail.com>2012-11-25 14:36:20 +0000
commitc3649a2def02c41d837ae1f79dda729ccb91e677 (patch)
treebea46dff212fdef977fe9094a70a939e8cc21885 /src/tspi/tsp_asym.c
downloadtrousers-upstream.tar.gz
Imported Upstream version 0.3.9upstream/0.3.9upstream
Diffstat (limited to 'src/tspi/tsp_asym.c')
-rw-r--r--src/tspi/tsp_asym.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/tspi/tsp_asym.c b/src/tspi/tsp_asym.c
new file mode 100644
index 0000000..721e8fd
--- /dev/null
+++ b/src/tspi/tsp_asym.c
@@ -0,0 +1,117 @@
+
+/*
+ * Licensed Materials - Property of IBM
+ *
+ * trousers - An open source TCG Software Stack
+ *
+ * (C) Copyright International Business Machines Corp. 2004-2007
+ *
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "trousers/tss.h"
+#include "trousers/trousers.h"
+#include "trousers_types.h"
+#include "spi_utils.h"
+#include "capabilities.h"
+#include "tsplog.h"
+#include "obj.h"
+
+
+/* encrypt some data with the RSA public key of 'key', using the padding appropriate for the key */
+TSS_RESULT
+__tspi_rsa_encrypt(TSS_HKEY key,
+ UINT32 inDataLen,
+ BYTE* inData,
+ UINT32* outDataLen,
+ BYTE* outData)
+{
+ BYTE *blob;
+ UINT32 blobLen;
+ UINT64 offset;
+ TSS_RESULT result;
+ TSS_HCONTEXT tspContext;
+ TPM_PUBKEY pubKey;
+
+ if (!inData || !outDataLen || !outData)
+ return TSPERR(TSS_E_INTERNAL_ERROR);
+
+ if ((result = obj_rsakey_get_tsp_context(key, &tspContext)))
+ return result;
+
+ if ((result = obj_rsakey_get_pub_blob(key, &blobLen, &blob)))
+ return result;
+
+ offset = 0;
+ if ((result = Trspi_UnloadBlob_PUBKEY(&offset, blob, &pubKey))) {
+ free_tspi(tspContext, blob);
+ return result;
+ }
+ free_tspi(tspContext, blob);
+
+ if (pubKey.pubKey.keyLength < inDataLen) {
+ result = TSPERR(TSS_E_ENC_INVALID_LENGTH);
+ goto done;
+ }
+
+ if (pubKey.algorithmParms.encScheme == TPM_ES_RSAESPKCSv15 ||
+ pubKey.algorithmParms.encScheme == TSS_ES_RSAESPKCSV15) {
+ if ((result = Trspi_RSA_PKCS15_Encrypt(inData, inDataLen, outData, outDataLen,
+ pubKey.pubKey.key, pubKey.pubKey.keyLength)))
+ goto done;
+ } else {
+ if ((result = Trspi_TPM_RSA_OAEP_Encrypt(inData, inDataLen, outData, outDataLen,
+ pubKey.pubKey.key,
+ pubKey.pubKey.keyLength)))
+ goto done;
+ }
+
+done:
+ free(pubKey.pubKey.key);
+ free(pubKey.algorithmParms.parms);
+ return result;
+}
+
+TSS_RESULT
+__tspi_rsa_verify(TSS_HKEY key,
+ UINT32 type,
+ UINT32 hashLen,
+ BYTE* hash,
+ UINT32 sigLen,
+ BYTE* sig)
+{
+ BYTE *blob;
+ UINT32 blobLen;
+ UINT64 offset;
+ TSS_RESULT result;
+ TSS_HCONTEXT tspContext;
+ TPM_PUBKEY pubKey;
+
+ if (!hash || !sig)
+ return TSPERR(TSS_E_INTERNAL_ERROR);
+
+ if ((result = obj_rsakey_get_tsp_context(key, &tspContext)))
+ return result;
+
+ if ((result = obj_rsakey_get_pub_blob(key, &blobLen, &blob)))
+ return result;
+
+ offset = 0;
+ if ((result = Trspi_UnloadBlob_PUBKEY(&offset, blob, &pubKey))) {
+ free_tspi(tspContext, blob);
+ return result;
+ }
+ free_tspi(tspContext, blob);
+
+ result = Trspi_Verify(type, hash, hashLen, pubKey.pubKey.key, pubKey.pubKey.keyLength,
+ sig, sigLen);
+
+ free(pubKey.pubKey.key);
+ free(pubKey.algorithmParms.parms);
+
+ return result;
+}