summaryrefslogtreecommitdiffstats
path: root/userspace/ncrypto_session.c
diff options
context:
space:
mode:
authorJan Chadima <jchadima@redhat.com>2010-08-02 10:56:34 +0200
committerMiloslav Trmač <mitr@redhat.com>2010-08-24 20:58:30 +0200
commit71be5465628262f1a475b52eaf90c5caba5876ea (patch)
treefff8c000bb3480741e5dc34d66196ddbc11ad6f2 /userspace/ncrypto_session.c
parentaf2c2e1da1a898c8968281824ba8a4d9616670dc (diff)
Initial userspace library version
Diffstat (limited to 'userspace/ncrypto_session.c')
-rw-r--r--userspace/ncrypto_session.c194
1 files changed, 194 insertions, 0 deletions
diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c
new file mode 100644
index 0000000..f8c1784
--- /dev/null
+++ b/userspace/ncrypto_session.c
@@ -0,0 +1,194 @@
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <string.h>
+#include <errno.h>
+#include "../ncr.h"
+#include "ncrypto.h"
+
+extern int __ncr_file_descriptor;
+
+int
+ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm, ncr_key_t input, void *output, size_t output_size)
+{
+ struct ncr_session_once_op_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!input || !output || !output_size) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.init.algorithm = algorithm;
+ io.init.key = key;
+ if (!params)
+ memmove(&io.init.params, params, sizeof(io.init.params));
+ io.init.op = op;
+ io.op.data.kdata.input = input;
+ io.op.data.kdata.output = output;
+ io.op.data.kdata.output_size = output_size;
+ io.op.type = NCR_KEY_DATA;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0)
+ return -1;
+
+ return 0;
+}
+
+int
+ncr_session_once_direct_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm, void *input, size_t input_size, void *output, size_t output_size)
+{
+ struct ncr_session_once_op_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!input || !input_size || !output || !output_size) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.init.algorithm = algorithm;
+ io.init.key = key;
+ if (!params)
+ memmove(&io.init.params, params, sizeof(io.init.params));
+ io.init.op = op;
+ io.op.data.udata.input = input;
+ io.op.data.udata.input_size = input_size;
+ io.op.data.udata.output = output;
+ io.op.data.udata.output_size = output_size;
+ io.op.type = NCR_DIRECT_DATA;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0)
+ return -1;
+
+ return 0;
+}
+
+int
+ncr_session_init(ncr_session_t *session, ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_t op, ncr_algorithm_t algorithm)
+{
+ struct ncr_session_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!session || (algorithm == NCR_ALG_NONE)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.algorithm = algorithm;
+ io.key = key;
+ if (!params)
+ memmove(&io.params, params, sizeof(io.params));
+ io.op = op;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_INIT, &io) < 0)
+ return -1;
+
+ *session = io.ses;
+
+ return 0;
+}
+
+int
+ncr_session_update_key_data(ncr_session_t session, ncr_key_t input)
+{
+ struct ncr_session_op_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!session || !input) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.ses = session;
+ io.data.kdata.input = input;
+ io.type = NCR_KEY_DATA;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0)
+ return -1;
+
+ return 0;
+}
+
+int
+ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_size)
+{
+ struct ncr_session_op_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!session || !input || !input_size) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.ses = session;
+ io.data.udata.input = input;
+ io.data.udata.input_size = input_size;
+ io.type = NCR_DIRECT_DATA;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0)
+ return -1;
+
+ return 0;
+}
+
+int
+ncr_session_final(ncr_session_t session, void *output, size_t output_size)
+{
+ struct ncr_session_op_st io;
+ memset(&io, 0, sizeof(io));
+
+ if (!session) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ io.ses = session;
+ io.data.kdata.output = output;
+ io.data.kdata.output_size = output_size;
+ io.type = NCR_KEY_DATA;
+
+ if (__ncr_file_descriptor < 0) {
+ errno = EBADF;
+ return -1;
+ }
+
+ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_FINAL, &io) < 0)
+ return -1;
+
+ switch (io.err) {
+ case NCR_VERIFICATION_FAILED:
+ errno = EDOM;
+ return -1;
+ case NCR_SUCCESS:
+ return (errno = 0);
+ default:
+ errno = EFAULT;
+ return -1;
+ }
+}
+