From 71be5465628262f1a475b52eaf90c5caba5876ea Mon Sep 17 00:00:00 2001 From: Jan Chadima Date: Mon, 2 Aug 2010 10:56:34 +0200 Subject: Initial userspace library version --- userspace/ncrypto_session.c | 194 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 userspace/ncrypto_session.c (limited to 'userspace/ncrypto_session.c') 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 +#include +#include +#include +#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; + } +} + -- cgit