From 60e1158295f152cbe0d7d983dfd98d94b73314c1 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 From b777f05372cd72112c10344630e8b1dc7918aa35 Mon Sep 17 00:00:00 2001 From: Jan Chadima Date: Thu, 5 Aug 2010 16:17:26 +0200 Subject: Userspace library updates from Jan --- userspace/ncrypto_session.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index f8c1784..94a494a 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -21,7 +21,7 @@ ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_ io.init.algorithm = algorithm; io.init.key = key; - if (!params) + if (params) memmove(&io.init.params, params, sizeof(io.init.params)); io.init.op = op; io.op.data.kdata.input = input; @@ -37,7 +37,7 @@ ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0) return -1; - return 0; + return io.op.data.kdata.output_size; } int @@ -51,10 +51,10 @@ ncr_session_once_direct_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_ return -1; } - io.init.algorithm = algorithm; io.init.key = key; - if (!params) + if (params) memmove(&io.init.params, params, sizeof(io.init.params)); + io.init.algorithm = algorithm; io.init.op = op; io.op.data.udata.input = input; io.op.data.udata.input_size = input_size; @@ -70,7 +70,7 @@ ncr_session_once_direct_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0) return -1; - return 0; + return io.op.data.udata.output_size; } int @@ -86,7 +86,7 @@ ncr_session_init(ncr_session_t *session, ncr_key_t key, ncr_key_params_t params, io.algorithm = algorithm; io.key = key; - if (!params) + if (params) memmove(&io.params, params, sizeof(io.params)); io.op = op; @@ -168,9 +168,9 @@ ncr_session_final(ncr_session_t session, void *output, size_t output_size) } io.ses = session; - io.data.kdata.output = output; - io.data.kdata.output_size = output_size; - io.type = NCR_KEY_DATA; + io.data.udata.output = output; + io.data.udata.output_size = output_size; + io.type = NCR_DIRECT_DATA; if (__ncr_file_descriptor < 0) { errno = EBADF; @@ -185,7 +185,8 @@ ncr_session_final(ncr_session_t session, void *output, size_t output_size) errno = EDOM; return -1; case NCR_SUCCESS: - return (errno = 0); + errno = 0; + return io.data.udata.output_size; default: errno = EFAULT; return -1; -- cgit From 9065dbb9f53d1728536c95c47f55fd525d032696 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Thu, 5 Aug 2010 18:16:09 +0200 Subject: Don't assume NCR_SESSION_INVALID is 0 --- userspace/ncrypto_session.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index 94a494a..7cc0ab3 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -14,7 +14,7 @@ ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_ struct ncr_session_once_op_st io; memset(&io, 0, sizeof(io)); - if (!input || !output || !output_size) { + if (input == NCR_KEY_INVALID || !output || !output_size) { errno = EINVAL; return -1; } @@ -109,7 +109,7 @@ 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) { + if (session == NCR_SESSION_INVALID || input == NCR_KEY_INVALID) { errno = EINVAL; return -1; } @@ -135,7 +135,7 @@ ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_ struct ncr_session_op_st io; memset(&io, 0, sizeof(io)); - if (!session || !input || !input_size) { + if (session == NCR_SESSION_INVALID || !input || !input_size) { errno = EINVAL; return -1; } @@ -162,7 +162,7 @@ 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) { + if (session == NCR_SESSION_INVALID) { errno = EINVAL; return -1; } -- cgit From 77fc876d20876c608110941576ee7e2f1b36f95d Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Thu, 5 Aug 2010 18:18:30 +0200 Subject: Don't prohibit NCR_ALG_NULL. It's used in examples/speed.c for testing. --- userspace/ncrypto_session.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index 7cc0ab3..b8f5b67 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -79,7 +79,7 @@ ncr_session_init(ncr_session_t *session, ncr_key_t key, ncr_key_params_t params, struct ncr_session_st io; memset(&io, 0, sizeof(io)); - if (!session || (algorithm == NCR_ALG_NONE)) { + if (!session) { errno = EINVAL; return -1; } -- cgit From 8b68956147faae4ce64c8a557c64ae2d004401d9 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Fri, 6 Aug 2010 00:14:43 +0200 Subject: Support output data in NCRIO_SESSION_UPDATE --- userspace/ncrypto_session.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index b8f5b67..9f6d317 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -104,7 +104,7 @@ ncr_session_init(ncr_session_t *session, ncr_key_t key, ncr_key_params_t params, } int -ncr_session_update_key_data(ncr_session_t session, ncr_key_t input) +ncr_session_update_key_data(ncr_session_t session, ncr_key_t input, void *output, size_t output_size) { struct ncr_session_op_st io; memset(&io, 0, sizeof(io)); @@ -116,6 +116,8 @@ ncr_session_update_key_data(ncr_session_t session, ncr_key_t input) io.ses = session; io.data.kdata.input = input; + io.data.kdata.output = output; + io.data.kdata.output_size = output_size; io.type = NCR_KEY_DATA; if (__ncr_file_descriptor < 0) { @@ -126,11 +128,11 @@ ncr_session_update_key_data(ncr_session_t session, ncr_key_t input) if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0) return -1; - return 0; + return io.data.kdata.output_size; } int -ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_size) +ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_size, void *output, size_t output_size) { struct ncr_session_op_st io; memset(&io, 0, sizeof(io)); @@ -143,6 +145,8 @@ ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_ io.ses = session; io.data.udata.input = input; io.data.udata.input_size = input_size; + io.data.udata.output = output; + io.data.udata.output_size = output_size; io.type = NCR_DIRECT_DATA; if (__ncr_file_descriptor < 0) { @@ -153,7 +157,7 @@ ncr_session_update_direct_data(ncr_session_t session, void *input, size_t input_ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_UPDATE, &io) < 0) return -1; - return 0; + return io.data.udata.output_size; } int -- cgit From 276569aa1c2ce3f2584c7286e11be671f85d1f65 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Fri, 6 Aug 2010 00:22:48 +0200 Subject: Support NCR_OP_VERIFY in *_once_* --- userspace/ncrypto_session.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index 9f6d317..e37aa21 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -37,7 +37,17 @@ ncr_session_once_key_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_op_ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0) return -1; - return io.op.data.kdata.output_size; + switch (io.op.err) { + case NCR_VERIFICATION_FAILED: + errno = EDOM; + return -1; + case NCR_SUCCESS: + errno = 0; + return io.op.data.kdata.output_size; + default: + errno = EFAULT; + return -1; + } } int @@ -70,7 +80,17 @@ ncr_session_once_direct_data(ncr_key_t key, ncr_key_params_t params, ncr_crypto_ if (ioctl(__ncr_file_descriptor, NCRIO_SESSION_ONCE, &io) < 0) return -1; - return io.op.data.udata.output_size; + switch (io.op.err) { + case NCR_VERIFICATION_FAILED: + errno = EDOM; + return -1; + case NCR_SUCCESS: + errno = 0; + return io.op.data.udata.output_size; + default: + errno = EFAULT; + return -1; + } } int -- cgit From 6e6fafa6663724240e202ee95908b476e7443075 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 7 Aug 2010 07:14:40 +0200 Subject: Abstract from users Let users #include this header file alone, without caring about . To do so, set up a temporary copy of ncr.h so that the #include works at build time as well. --- userspace/ncrypto_session.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index e37aa21..e14dba2 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -3,7 +3,7 @@ #include #include #include -#include "../ncr.h" +#include #include "ncrypto.h" extern int __ncr_file_descriptor; -- cgit From 7687d57b337a16e82d0e70725a83c0e612d16f93 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Mon, 9 Aug 2010 20:05:22 +0200 Subject: Avoid unnecessary internal relocations Use __attribute__((visibility("hidden"))) for __ncr_file_descriptor to take advantage of PIC addressing instead of going through the dynamic linker. Add an internal alias for ncr_global_init() for the same reason. Add an internal header file to consolidate the "extern" references in the process. --- userspace/ncrypto_session.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index e14dba2..03ede25 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -5,8 +5,7 @@ #include #include #include "ncrypto.h" - -extern int __ncr_file_descriptor; +#include "ncrypto_internal.h" 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) -- cgit From bd0b751d6e7ce55369327740c0663b698cdbbe90 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Sat, 21 Aug 2010 10:41:05 +0200 Subject: Add Red Hat copyright notices to libcryptodev --- userspace/ncrypto_session.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'userspace/ncrypto_session.c') diff --git a/userspace/ncrypto_session.c b/userspace/ncrypto_session.c index 03ede25..a81fb8c 100644 --- a/userspace/ncrypto_session.c +++ b/userspace/ncrypto_session.c @@ -1,3 +1,30 @@ +/* + * Copyright 2010 Red Hat, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY RED HAT, INC. AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL RED HAT, INC. OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Red Hat author: Jan Chadima + */ #include #include -- cgit