Skip to contentSkip to conversation

RFC, v2: "New" /dev/crypto user-space interface

PATCH In review Miloslav Trmač 12 participants 45 messages Aug 20, 2010 – Sep 3, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover RFC, v2: "New" /dev/crypto user-space interface

Hello,
following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

Major changes since the previous post:
* "struct nlattr"-based extensible attributes used for extensibility of most operations, both for input and output attributes
* algorithms (ciphers, key wrapping, key derivation) are identified using strings in the userspace API
* Full compat_ioctl implementation
* Version number added to the data format used when wrapping keys for storage
* Patch set split into smaller parts, reordered to keep the tree buildable. (If you want to review primarily the user-space API, see patches 1,14,15,17,18.)

The libtom* patches will probably still be too large for the mailing list; the whole patch set is also available at
http://people.redhat.com/mitr/cryptodev-ncr/v2/ .

Original patch set description follows.

These are the major differences compared to the BSD-like interface:

* The API supports key storage and management inside the kernel. An application can thus ask the kernel to generate a key; the key is then referenced via an integer identifier, and the application can be prevented from accessing the raw key data. Such a key can, if so configured, still be wrapped for key transport to the recipient of the message, and unwrapped by the recipient.

The kernel key storage does not span system reboots, but applications can also wrap the keys for persistent storage, receiving an encrypted blob that does not reveal the raw key data, but can be later loaded back into the kernel.

* More algorithms and mechanisms are supported by the API, including public key algorithms (RSA/DSA encryption and signing, D-H key derivation, key wrapping).

Motivations for the extensions: governments are asking for more security features in the operating systems they procure, which make user-space implementations impractical. A few examples:

* Advanced crypto module for OSPP for Common Criteria requires OS services implementing several low-level crypto algorithms (e.g. AES, RSA). This requires the separation of crypto services from the consumer of those services. (The threat model is that apps tend to have more vulnerabilities than libraries and compromise of the app will lead to the ability to access key material.) An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

* FIPS-140-3 calls out for cryptographic functions to be non-debuggable (ptrace) meaning that you cannot get to the key material. The solution is the same as above.

* GPOSPP requires auditing for crypto events (so does FIPS-140 level 2 cert). To do this you need any crypto to have CAP_AUDIT_WRITE permissions which means making everything that links to openssl, libgcrypt, or nss setuid root. Making firefox and 400 other applications setuid root is a non-starter. So, the solution is again to use crypto in the kernel where auditing needs no special permissions.

Other advantages to having kernel crypto available to user space:

* User space will be able to take advantage of kernel drivers for hardware crypto accelerators.

* glibc, which in some configurations links to libfreebl3.so for hashes necessary for crypt(), will be able to use the kernel implementation; this means one less library to load and dynamically link for each such process.

The code is derived from the original cryptodev-linux patch set; most of the new implementation was written by Nikos Mavrogiannopoulos <[email protected]>. Attributions are included in the respective source files.

#1 lore raw

PATCH 1/19 User-space API definition

This patch introduces the new user-space API, <ncr.h>.

Quick overview:

* open("/dev/crypto") to get a FD, which acts as a namespace for key and session identifiers.

* ioctl(NCRIO_KEY_INIT) to allocate a key object; then generate the key material inside the kernel, load a plaintext key, unwrap a key, or derive a key. Similarly the key material can be copied out of the kernel or wrapped.

* ioctl(NCRIO_SESSION_INIT) to allocate a crypto session (to encrypt, decrypt, hash, sign, or verify signature), then ioctl(NCRIO_SESSION_UPDATE) to act on chunks of data. Deallocate the session, and optionally retrieve session results (e.g. hash or signature), using ioctl(NCRIO_SESSION_FINAL).

There is also NCRIO_SESSION_ONCE for an one-shot crypto operation using a single user->kernel context switch.

Full documentation of the interface is in
Documentation/crypto/userspace.txt .

---
 Documentation/crypto/userspace.txt |  510 ++++++++++++++++++++++++++++++++++++
 include/linux/Kbuild               |    1 +
 include/linux/ncr.h                |  273 +++++++++++++++++++
 3 files changed, 784 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/crypto/userspace.txt
 create mode 100644 include/linux/ncr.h
Documentation/crypto/userspace.txt new file +510 -0
new file mode 100644
index 0000000..5839fda
@@ -0,0 +1,510 @@
+CRYPTO(4)                  Linux Programmer’s Manual                 CRYPTO(4)
+
+NAME
+       /dev/crypto - kernel cryptographic module interface
+
+SYNOPSIS
+       #include <ncr.h>
+       int fd = open("/dev/crypto", O_RDWR);
+       int res = ioctl(fd, NCRIO..., &data);
+
+DESCRIPTION
+       The  /dev/crypto device file provides an ioctl(2) interface to the ker-
+       nel-space crypto implementation.
+
+       Each open(2) of the /dev/crypto file establishes a  separate  namespace
+       within  which  crypto  operations  work.   The  namespace can be shared
+       across threads and processes by  sharing  the  open  file  description.
+       Last  close  of  the  open  file description automatically destroys all
+       objects allocated within the namespace.
+
+       All ioctl(2)s have the same form: The user sets  up  a  data  structure
+       with  input  data,  and  passes  a pointer to the data structure as the
+       third parameter to ioctl(2).  On success, output data is  available  in
+       the same structure.
+
+OPERATION ATTRIBUTES
+       In addition to the fixed members of data structures, a process can pass
+       additional attributes on input of most operations,  and  receive  addi-
+       tional attributes back from the kernel.  If this is supported by a spe-
+       cific operation, the fixed data structure members  for  this  operation
+       include input_size and output_size.
+
+       The  input  attributes immediately follow the fixed data structure as a
+       sequence of correctly aligned (struct nlattr,  attribute  data)  pairs,
+       using the same data layout and formatting rules as netlink(7) messages.
+       The input_size member must be initialized to the total length of  input
+       (including both the fixed data structure and the attributes).
+
+       There  is  an  (unspecified)  upper  limit  on  the  total  size of all
+       attributes, which should be large enough to accommodate any  reasonable
+       application.  If a larger input is provided nevertheless, the operation
+       will fail with EOVERFLOW.
+
+       Output attributes, if any, are written by the kernel in the same format
+       following  the  fixed  data structure (overwriting input attributes, if
+       any).  The output_size member must be initialized to  the  total  space
+       available  for output (including the fixed data structure and space for
+       attributes), and is updated by the kernel with the total space actually
+       used for attributes.
+
+       If  the  space available for output attributes is too small, the opera-
+       tion will fail with ERANGE.
+
+       As a special  case,  input_size  may  be  zero  to  indicate  no  input
+       attributes  are  supplied.  Similarly, output_size may be zero to indi-
+       cate no space for output attributes is  provided;  in  such  case  out-
+       put_size is not overwritten by the kernel with the total space actually
+       used (which is equal to size of the fixed data structure)  and  remains
+       zero when the operation finishes.
+
+       The kernel accepts and silently ignores unknown attributes.
+
+       The kernel will not add new output attributes to existing operations in
+       future  releases  unless  the  new  output  attribute  was   explicitly
+       requested  by  the  application.   (Therefore, the application does not
+       have to allocate any extra space for  output  attributes  it  does  not
+       expect.)
+
+       The following attributes are used in more than one operation:
+
+       NCR_ATTR_ALGORITHM
+              A  NUL-terminated  string specifying an algorithm (to be used in
+              an operation, or as a property of a key), using the Linux crypto
+              API algorithm names.
+
+              Three additional algorithm names are recognized: rsa, dsa, dh.
+
+       NCR_ATTR_IV
+              Unformatted binary data specifying an initialization vector,
+
+       NCR_ATTR_KEY_FLAGS
+              An  32-bit  unsigned integer in native byte order specifying key
+              flags, a combination of NCR_KEY_FLAG_EXPORTABLE (the  key  mate-
+              rial   can   be   exported  in  plaintext  to  user  space)  and
+              NCR_KEY_FLAG_WRAPPABLE (the key material can be wrapped and  the
+              result made available to user space).
+
+       NCR_ATTR_KEY_ID
+              Arbitrary  binary  data which can be used by the application for
+              key identification.
+
+       NCR_ATTR_KEY_TYPE
+              An 32-bit unsigned integer in native byte order  specifying  key
+              type,   one   of  NCR_KEY_TYPE_SECRET,  NCR_KEY_TYPE_PUBLIC  and
+              NCR_KEY_TYPE_PRIVATE.
+
+       NCR_ATTR_WRAPPING_ALGORITHM
+              A NUL-terminated string specifying  a  key  wrapping  algorithm.
+              The  values  walg-aes-rfc3394 and walg-aes-rfc5649 are currently
+              supported.
+
+OPERATIONS
+       The following operations are defined:
+
+       NCRIO_KEY_INIT
+              Allocate a kernel-space key object.  The third ioctl(s)  parame-
+              ter is ignored (key attributes are set later, when the key mate-
+              rial is initialized).  Returns an ncr_key_t descriptor  for  the
+              key  object  (valid within the current /dev/crypto namespace) on
+              success.
+
+              There is a per-process and per-user (not per-namespace) limit on
+              the number key objects that can be allocated.
+
+       NCRIO_KEY_DEINIT
+              Deallocate a kernel-space key object.  The parameter points to a
+              ncr_key_t descriptor of the key object.  After all other  opera-
+              tions using this key object (if any) terminate, the key material
+              will be cleared and the object will be freed.   Note  that  this
+              may  happen  both  before  this  operation returns, and after it
+              returns, depending on other references to this key object.
+
+       NCRIO_KEY_GENERATE
+              Clear existing key material in the  specified  key  object,  and
+              generate new key material.
+
+              The parameter points to struct ncr_key_generate, which specifies
+              the destination key object in its key member.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_ALGORITHM
+                     Mandatory.
+
+              NCR_ATTR_KEY_FLAGS
+                     Optional, flags are unchanged if not present.
+
+              NCR_ATTR_SECRET_KEY_BITS
+                     Mandatory for symmetric keys.  An 32-bit unsigned integer
+                     in native byte order specifying key length in bits.
+
+              Only symmetric keys can be currently generated using this opera-
+              tion.
+
+              In addition to generating the key material, the "persistent" key
+              ID is reset to a random value.
+
+       NCRIO_KEY_GENERATE_PAIR
+              Similar to NCRIO_KEY_GENERATE, except that a pair of public/pri-
+              vate keys is generated.
+
+              The parameter  points  to  struct  ncr_key_generate_pair,  which
+              specifies  the  destination  key  objects in its private_key and
+              public_key members.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_KEY_FLAGS
+                     Optional, flags are unchanged if not present.
+
+              NCR_ATTR_RSA_E
+                     For RSA keys, the public exponent as a big-endian  multi-
+                     ple-precision integer.  Optional, defaults to 65537.
+
+              NCR_ATTR_RSA_MODULUS_BITS
+                     Mandatory  for  RSA  keys.  An 32-bit unsigned integer in
+                     native byte order specifying modulus length in bits.
+
+              NCR_ATTR_DSA_P_BITS
+                     For DSA keys, length of the "p" key parameter in bits  as
+                     an   32-bit   unsigned  integer  in  native  byte  order.
+                     Optional, defaults to 1024.
+
+              NCR_ATTR_DSA_Q_BITS
+                     For DSA keys, length of the "q" key parameter in bits  as
+                     an   32-bit   unsigned  integer  in  native  byte  order.
+                     Optional, defaults to 160.
+
+              NCR_ATTR_DH_BASE
+                     Mandatory for D-H keys.  The prime modulus of a D-H group
+                     as a big-endian multiple-precision integer.
+
+              NCR_ATTR_DH_PRIME
+                     Mandatory  for D-H keys.  The generator of a D-H group as
+                     a big-endian multiple-precision integer.
+
+              The NCR_KEY_FLAG_EXPORTABLE and NCR_KEY_FLAG_WRAPPABLE flags are
+              automatically set on the public key.
+
+              In addition to generating the key material, the "persistent" key
+              ID of both keys is set to a same value derived from  the  public
+              key.
+
+       NCRIO_KEY_DERIVE
+              Derive a new key using one key and additional data.
+
+              The  parameter  points to struct ncr_key_derive, which specifies
+              the source and destination keys in  the  input_key  and  new_key
+              members.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_KEY_FLAGS
+                     Optional, flags are unchanged if not present.
+
+              NCR_ATTR_DERIVATION_ALGORITHM
+                     Mandatory.   A  NUL-terminated  string  specifying  a key
+                     derivation algorithm.  Only dh is currently supported.
+
+              NCR_ATTR_DH_PUBLIC
+                     Mandatory for D-H  derivation.   The  peer’s  public  D-H
+                     value as a big-endian multiple-precision integer.
+
+       NCRIO_KEY_EXPORT
+              Export  key  material in the specified key object to user space.
+              Only keys with the NCR_KEY_FLAG_EXPORTABLE flag can be  exported
+              using this operation.
+
+              The  parameter  points to struct ncr_key_export, which specifies
+              the key to export in the  key  member,  and  a  buffer  for  the
+              exported data in the buffer and buffer_size members.
+
+              On success, size of the exported key is returned.
+
+              Symmetric keys are written directly into the destination buffer.
+              Public and private keys are formatted using ASN.1, except for DH
+              public keys, which are written a raw binary number.
+
+       NCRIO_KEY_IMPORT
+              Clear  existing  key  material  in the specified key object, and
+              import key material from user space.
+
+              The parameter points to struct ncr_key_import,  which  specifies
+              the destination key in the key member, and the input data in the
+              data and data_size members.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_ALGORITHM
+                     Mandatory.
+
+              NCR_ATTR_KEY_FLAGS
+                     Optional, flags are unchanged if not present.
+
+              NCR_ATTR_KEY_ID
+                     Optional, the "persistent" key ID  is  unchanged  if  not
+                     present.
+
+              NCR_ATTR_KEY_TYPE
+                     Mandatory.
+
+              The  data  format  is the same as in the NCRIO_KEY_EXPORT opera-
+              tion.
+
+       NCRIO_KEY_GET_INFO
+              Get metadata of an existing key.
+
+              The parameter points to struct ncr_key_get_info, which specifies
+              key, the key descriptor.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_WANTED_ATTRS
+                     An  array  of  unsigned  16-bit  integers  in native byte
+                     order, specifying  the  set  of  output  attributes  that
+                     should       be       returned.       NCR_ATTR_ALGORITHM,
+                     NCR_ATTR_KEY_FLAGS and  NCR_ATTR_KEY_TYPE  are  currently
+                     supported.   Unsupported  attribute requests are silently
+                     ignored
+
+              The    output     attributes     explicitly     requested     in
+              NCR_ATTR_WANTED_ATTRS,  and  no  other  output  attributes,  are
+              returned.
+
+       NCRIO_KEY_WRAP
+              Wrap one key using another, and write the result to user  space.
+              Only  keys  with  the NCR_KEY_FLAG_WRAPPABLE flag can be wrapped
+              using this operation.
+
+              The parameter points to struct ncr_key_wrap, which specifies the
+              key  to  wrap  in the source_key member, the wrapping key in the
+              wrapping_key member, and a buffer for the wrapped  data  in  the
+              buffer and buffer_size members.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_IV
+                     Optional, an empty IV is used if not present.
+
+              NCR_ATTR_WRAPPING_ALGORITHM
+                     Mandatory.
+
+              Only secret keys can be currently wrapped.
+
+              On success, size of the wrapped key is returned.
+
+       NCRIO_KEY_UNWRAP
+              Unwrap  user-space  data  into  a kernel-space key using another
+              key.
+
+              The parameter points to struct ncr_key_unwrap,  which  specifies
+              the  destination key in the dest_key member, the wrapping key in
+              the wrapping_key member, and the wrapped data in  the  data  and
+              data_size members.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_IV
+                     Optional, an empty IV is used if not present.
+
+              NCR_ATTR_WRAPPING_ALGORITHM
+                     Mandatory.
+
+              The unwrapped key will have the NCR_KEY_FLAG_WRAPPABLE flag set,
+              and the NCR_KEY_FLAG_EXPORTABLE flag clear.
+
+       NCRIO_KEY_STORAGE_WRAP
+              Wrap a key object and associated metadata using the  system-wide
+              storage master key, and write the result to user space.
+
+              Only  keys  with  the NCR_KEY_FLAG_WRAPPABLE flag can be wrapped
+              using this operation.
+
+              The parameter points to struct ncr_key_storage_wrap, which spec-
+              ifies  the  key  to wrap in the key member, and a buffer for the
+              wrapped data in the buffer and buffer_size members.
+
+              On success, size of the wrapped key is returned.
+
+              Both symmetric and asymmetric keys can  be  wrapped  using  this
+              operation.   The  wrapped  data  includes data corresponding the
+              NCR_ATTR_ALGORITHM,  NCR_ATTR_KEY_FLAGS,  NCR_ATTR_KEY_TYPE  and
+              NCR_ATTR_KEY_ID attributes in addition to the raw key material:
+
+       NCRIO_KEY_STORAGE_UNWRAP
+              Unwrap key and associated metadata created using NCRIO_KEY_STOR-
+              AGE_WRAP, and restore  the  information  into  a  specified  key
+              object.
+
+              The  parameter  points  to  struct ncr_key_storage_unwrap, which
+              specifies the destination key in the key member and the  wrapped
+              data in the data and data_size members.
+
+              See NCRIO_KEY_STORAGE_WRAP above for the list of attributes that
+              will be restored.
+
+       NCRIO_SESSION_INIT
+              Allocate a session for performing crypto operations.
+
+              The parameter points to struct ncr_session_init, which specifies
+              the operation to perform, one of NCR_OP_ENCRYPT, NCR_OP_DECRYPT,
+              NCR_OP_SIGN  and  NCR_OP_VERIFY,  in   the   op   member.    Use
+              NCR_OP_SIGN  for  computing  an  unkeyed  hash  as well as keyed
+              hashes and signatures.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_ALGORITHM
+                     Mandatory.
+
+              NCR_ATTR_IV
+                     Mandatory for some operations and algorithms.
+
+              NCR_ATTR_KEY
+                     Mandatory for some operations and algorithms.  An  32-bit
+                     unsigned  integer in native byte order specifying the key
+                     to use for the operation.
+
+              NCR_ATTR_RSA_ENCODING_METHOD
+                     Mandatory for RSA.  An 32-bit unsigned integer in  native
+                     byte  order  specifying  a  RSA  encoding  method, one of
+                     RSA_PKCS1_V1_5, RSA_PKCS1_OAEP and RSA_PKCS1_PSS.
+
+              NCR_ATTR_RSA_OAEP_HASH_ALGORITHM
+                     Mandatory for RSA with RSA_PKCS1_OAEP.  A  NUL-terminated
+                     string  specifying  a  hash  algorithm  used  in the OAEP
+                     encoding method.
+
+              NCR_ATTR_RSA_PSS_SALT_LENGTH
+                     For RSA with RSA_PKCS1_PSS.  An 32-bit  unsigned  integer
+                     in  native  byte  order  specifying  the PSS salt length.
+                     Optional, defaults to 0.
+
+              NCR_ATTR_SIGNATURE_HASH_ALGORITHM
+                     Mandatory for some operations and algorithms.  A NUL-ter-
+                     minated  string  specifying a hash algorithm underlying a
+                     signature, using the same formats as  NCR_ATTR_ALGORITHM.
+
+              On success, an integer descriptor for the created session (valid
+              within the current /dev/crypto namespace) is returned.
+
+       NCRIO_SESSION_UPDATE
+              Update an existing crypto session with new data (for operations,
+              such  as  hashing, for which data can be supplied in pieces), or
+              perform a single operation using the session context (for opera-
+              tions,  such  as  public  key  encryption, that work on separate
+              units of data).
+
+              The parameter points to struct ncr_session_update, which  speci-
+              fies the descriptor of the session in the ses member.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_UPDATE_INPUT_DATA
+                     A  struct ncr_session_input_data specifying input for the
+                     operation in its data and data_size members.
+
+              NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA
+                     An 32-bit unsigned integer in native byte order  specify-
+                     ing  the  key  descriptor serving as input for the opera-
+                     tion.  This can be currently used only to compute or ver-
+                     ify  a  signature  or hash of a symmetric key: the keying
+                     material is directly used as input data for the  underly-
+                     ing hash.
+
+              NCR_ATTR_UPDATE_OUTPUT_BUFFER
+                     Mandatory  for  some operations and algorithms.  A struct
+                     ncr_session_output_buffer specifying buffer for operation
+                     output in its buffer and buffer_size members.  On success
+                     the size of output is written to the variable pointed  to
+                     by the result_size_ptr member.
+
+              It is mandatory to include one of the NCR_ATTR_UPDATE_INPUT_DATA
+              and NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA attributes.
+
+              For the NCR_OP_ENCRYPT and NCR_OP_DECRYPT operations using  sym-
+              metric  ciphers,  the  operation is performed on the input data,
+              resulting in an output data block of the same size;  for  opera-
+              tions  using public-key cryptography, a single operation is per-
+              formed on the input data, resulting in output data.
+
+              For the NCR_OP_SIGN and NCR_OP_VERIFY operations, the input data
+              is  supplied  to the underlying hash function; no output data is
+              produced.
+
+       NCRIO_SESSION_FINAL
+              Finalize an existing crypto session and deallocate it.
+
+              The parameter points to struct ncr_session_final,  which  speci-
+              fies the descriptor of the session in the ses member.
+
+              If     one     of     the     NCR_ATTR_UPDATE_INPUT_DATA     and
+              NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA  attributes  is  present,  all
+              attributes are first processed as if using NCRIO_SESSION_UPDATE;
+              thus, the last update operation can be performed  together  with
+              the finalization in one step.
+
+              The following input attributes are recognized:
+
+              NCR_ATTR_FINAL_INPUT_DATA
+                     Mandatory  for  some operations and algorithms.  A struct
+                     ncr_session_input_data  as  described  above,  specifying
+                     input for the operation.
+
+              NCR_ATTR_FINAL_OUTPUT_BUFFER
+                     Mandatory  for  some operations and algorithms.  A struct
+                     ncr_session_output_buffer as described above,  specifying
+                     buffer for operation output.
+
+              There  is  no  specific  finalization  operation  performed  for
+              NCR_OP_ENCRYPT and NCR_OP_DECRYPT.
+
+              For the NCR_OP_SIGN operation,  the  signature  is  created  and
+              written as output data.
+
+              For  the NCR_OP_VERIFY operation, a signature specified as input
+              is verified and the result of this operation is  returned:  non-
+              zero for a valid signature, zero for an invalid signature.  Note
+              that the ioctl(2) operation return value will  be  non-negative,
+              i.e.  "success",  even  if  the signature verification fails, as
+              long all inputs were specified correctly.
+
+              The session will be deallocated even if the  NCRIO_SESSION_FINAL
+              operation  reports an error, as long as a valid session descrip-
+              tor was specified.
+
+       NCRIO_SESSION_ONCE
+              Perform an one-shot crypto  operation,  allocating  a  temporary
+              session, supplying a single instance of data, and finalizing the
+              session in one operation.
+
+              The parameter points to struct ncr_session_once, which specifies
+              the operation to perform in the op member.
+
+              The  attributes handled as if by passing to a NCRIO_SESSION_INIT
+              operation followed by a NCRIO_SESSION_FINAL operation,  and  the
+              return  value of the NCRIO_SESSION_FINAL is returned on success.
+
+       NCRIO_MASTER_KEY_SET
+              Set the system-wide storage master key.   Only  a  process  with
+              EUID  0  and  the CAP_SYS_ADMIN capability is allowed to perform
+              this operation.  Once a master key is set,  it  can  be  changed
+              only by rebooting the system and setting a different key.
+
+              The  parameter points to struct ncr_master_key_set, which speci-
+              fies the key material in user space using the key  and  key_size
+              members.
+
+              Only  an  AES  key  with  size  16, 24, or 32 bytes is currently
+              acceptable.
+
+CONFIGURATION
+       The NCRIO_KEY_STORAGE_WRAP and NCRIO_KEY_STORAGE_UNWRAP  ioctl()s  work
+       only after a storage master key is configured by the system administra-
+       tor.  See NCRIO_MASTER_KEY_SET above.
+
+FILES
+       /dev/crypto
+
+Linux                             2010-08-20                         CRYPTO(4)
include/linux/Kbuild +1 -0
index 756f831..41790cd 100644
@@ -116,6 +116,7 @@ header-y += mmtimer.h
 header-y += mqueue.h
 header-y += mtio.h
 header-y += ncp_no.h
+header-y += ncr.h
 header-y += neighbour.h
 header-y += net_dropmon.h
 header-y += net_tstamp.h
include/linux/ncr.h new file +273 -0

273 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#2 lore raw

Re: [PATCH 01/19] User-space API definition

Quoted 2 lines · Miloslav Trmač
include/linux/ncr.h new file +0 -0

[...]

include/linux/ncr.h line 240

Quoted 10 lines
include/linux/ncr.h new file +10 -0
+struct ncr_session_input_data {
+	const void __user *data;
+	__kernel_size_t data_size;
+};
+
+struct ncr_session_output_buffer {
+	void __user *buffer;
+	__kernel_size_t buffer_size;
+	__kernel_size_t __user *result_size_ptr;
+};

Why not using fixed-size fit-all members?

struct ncr_session_input_data {
	__u64 data;		/* user pointer, cast to/from u64 */
	__u32 data_size;	/* or __u64? */
};
struct ncr_session_output_buffer {
	__u64 buffer;
	__u64 result_size_ptr;	/* can't this be a direct output member? */
	__u32 buffer_size;	/* or __u64? */
};

And then get rid of all the COMAPT code paths.

Signature · 3 lines
Stefan Richter
-=====-==-=- =--- =-=--
http://arcgraph.de/sr/
#3 lore raw

Re: [PATCH 01/19] User-space API definition

2010/8/20 Stefan Richter <[email protected]>:

Quoted 11 lines

include/linux/ncr.h line 240

Quoted 6 lines
include/linux/ncr.h new file +6 -0
+struct ncr_session_input_data {
+     const void __user *data;
+     __kernel_size_t data_size;
+};
+
+};

Why not using fixed-size fit-all members?
struct ncr_session_input_data {
       __u64 data;             /* user pointer, cast to/from u64 */ __u32 data_size;        /* or __u64? */
};

A reason is that using (void*) is cleaner as an API. It avoids the pointer to int casting and the warnings that such a cast will have.

regards,
Nikos

#4 lore raw

Re: [PATCH 01/19] User-space API definition

In reply to Stefan Richter
Quoted 11 lines · ----- "Stefan Richter
Quoted 2 lines · Miloslav Trmač
include/linux/ncr.h new file +0 -0

[...]

include/linux/ncr.h line 234

Quoted 4 lines
include/linux/ncr.h new file +4 -0
+struct ncr_session_input_data {
+	const void __user *data;
+	__kernel_size_t data_size;
+};

Why not using fixed-size fit-all members?

Quoted 6 lines
struct ncr_session_input_data {
	__u64 data;		/* user pointer, cast to/from u64 */
	__u32 data_size;	/* or __u64? */
};

And then get rid of all the COMAPT code paths.

That would make the interface less natural, and any architecture that wanted to have larger pointers (I understand IBM did this back the '80s, so it can happen again) would result in a significantly worse mess than the widely used compat_ioctl mechanism.
Mirek

#5 lore raw

Re: [PATCH 01/19] User-space API definition

In reply to Miloslav Trmač
Quoted 5 lines · On Fri, 20 Aug 2010 10:45:44 +0200 Miloslav Trmač wrote:
include/linux/ncr.h new file +5 -0
+#define NCRIO_KEY_INIT			_IO('c', 204)
+/* generate a secret key */
+#define NCRIO_KEY_GENERATE     	_IOWR('c', 205, struct ncr_key_generate)
+/* generate a public key pair */
+#define NCRIO_KEY_GENERATE_PAIR _IOWR('c', 206, struct ncr_key_generate_pair)

include/linux/ncr.h line 162

Quoted 10 lines
include/linux/ncr.h new file +10 -0
+/* derive a new key from an old one */
+#define NCRIO_KEY_DERIVE        _IOWR('c', 207, struct ncr_key_derive)
+/* return information on a key */
+#define NCRIO_KEY_GET_INFO      _IOWR('c', 208, struct ncr_key_get_info)
+/* export a secret key */
+#define NCRIO_KEY_EXPORT       	_IOWR('c', 209, struct ncr_key_export)
+/* import a secret key */
+#define NCRIO_KEY_IMPORT       	_IOWR('c', 210, struct ncr_key_import)
+
+#define NCRIO_KEY_DEINIT       _IOR ('c', 215, ncr_key_t)

Please add (another) 'c' entry to Documentation/ioctl/ioctl-number.txt.

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#6 lore raw

Re: [PATCH 01/19] User-space API definition

In reply to Miloslav Trmač
Quoted 7 lines · Miloslav Trmač

This patch introduces the new user-space API, <ncr.h>.

Quick overview:

* open("/dev/crypto") to get a FD, which acts as a namespace for key and session identifiers.

Quoted 6 lines

* ioctl(NCRIO_KEY_INIT) to allocate a key object; then generate the key material inside the kernel, load a plaintext key, unwrap a key, or derive a key.  Similarly the key material can be copied out of the kernel or wrapped.

[...snip...]

Ugh... We already have one very nice key/keyring API in the kernel (see Documentation/keys.txt) that's being used for crypto keys for NFSv4, AFS, etc. Can't you just add a bunch of cryptoapi key types to that API instead?

David Howells added to CC, since I believe he wrote most of that code initially.

Cheers,
Kyle Moffett

#7 lore raw

Re: [PATCH 01/19] User-space API definition

Quoted 12 lines · Kyle Moffett
Quoted 12 lines

This patch introduces the new user-space API, <ncr.h>.

Quick overview:

* open("/dev/crypto") to get a FD, which acts as a namespace for key and session identifiers.

* ioctl(NCRIO_KEY_INIT) to allocate a key object; then generate the key material inside the kernel, load a plaintext key, unwrap a key, or derive a key. Similarly the key material can be copied out of the kernel or wrapped.

Quoted 6 lines
Quoted 1 line

[...snip...]

Ugh... We already have one very nice key/keyring API in the kernel (see Documentation/keys.txt) that's being used for crypto keys for NFSv4, AFS, etc. Can't you just add a bunch of cryptoapi key types to that API instead?

It is not that simple. My understanding of the keyring API is that it allows exporting of the keys to user-space and this crypto API explicitly prevents that, so enhancing the API that way will remove the benefits of using it. It would be ideal to combine somehow those solutions but this is more elaborate work than adding a bunch of new key types. If anyone is interested in attempting that I'd be glad to help.

regards,
Nikos

#8 lore raw

Re: [PATCH 01/19] User-space API definition

Quoted 3 lines · Nikos Mavrogiannopoulos

It is not that simple. My understanding of the keyring API is that it allows exporting of the keys to user-space and this crypto API explicitly prevents that

That's simple. Don't provide a read() key type operation, then.

David

#9 lore raw

Re: [PATCH 01/19] User-space API definition

In reply to Miloslav Trmač
Quoted 18 lines · Miloslav Trmač

This patch introduces the new user-space API, <ncr.h>.

Quick overview:

* open("/dev/crypto") to get a FD, which acts as a namespace for key and session identifiers.

* ioctl(NCRIO_KEY_INIT) to allocate a key object; then generate the key material inside the kernel, load a plaintext key, unwrap a key, or derive a key. Similarly the key material can be copied out of the kernel or wrapped.

* ioctl(NCRIO_SESSION_INIT) to allocate a crypto session (to encrypt, decrypt, hash, sign, or verify signature), then ioctl(NCRIO_SESSION_UPDATE) to act on chunks of data. Deallocate the session, and optionally retrieve session results (e.g. hash or signature), using ioctl(NCRIO_SESSION_FINAL).

Quoted 5 lines

There is also NCRIO_SESSION_ONCE for an one-shot crypto operation using a single user->kernel context switch.

Full documentation of the interface is in
Documentation/crypto/userspace.txt .

Thanks for the updated patch-set. It does indeed fulfil some of the requirements raised earlier.

However, as far as I can see this still does not address the extensibility. For example, say we want add an interface to allow the xoring of two arbitrary data streams using DMA offload, this interface would make that quite awkward.

In fact the whole interface is really tailored to the traditional encryption/hash operations that BSD provided so I think this is not a good foundation for our user-space API.

I will be looking at this myself so please stay tuned and be ready to yell if you see that your requirements are not met.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#10 lore raw

Re: [PATCH 01/19] User-space API definition

2010/9/3 Herbert Xu <[email protected]>:

Quoted 10 lines
Quoted 9 lines

* ioctl(NCRIO_SESSION_INIT) to allocate a crypto session (to encrypt, decrypt, hash, sign, or verify signature), then ioctl(NCRIO_SESSION_UPDATE) to act on chunks of data.  Deallocate the session, and optionally retrieve session results (e.g. hash or signature), using ioctl(NCRIO_SESSION_FINAL).
  There is also NCRIO_SESSION_ONCE for an one-shot crypto operation using a single user->kernel context switch.
Full documentation of the interface is in Documentation/crypto/userspace.txt .

Quoted 7 lines

Thanks for the updated patch-set.  It does indeed fulfil some of the requirements raised earlier.

However, as far as I can see this still does not address the extensibility.  For example, say we want add an interface to allow the xoring of two arbitrary data streams using DMA offload, this interface would make that quite awkward.

Although I think the current API could handle something like this, I also think it is undesirable to do so. The API was designed with cryptographic operations in mind, and its extensibility might better be judged on how it can incorporate future cryptographic protocols (sigma-proofs and other zero-knowledge protocols and other protocols that I might not know of). An XOR operation, doesn't really fit into a cryptographic API. It could however be included in the current design with some utility ioctls() that address such helper operations. Even better, I think a XOR operation deserves a system call, since it is quite useful in a variety of applications, not only cryptographic ones.

Quoted 3 lines

In fact the whole interface is really tailored to the traditional encryption/hash operations that BSD provided so I think this is not a good foundation for our user-space API.

It supports much more than the openbsd API, but indeed it is designed with cryptographic operations in mind and this limitation can allow a semi-formal verification of its properties. I'll try to post a link to the design document as soon.

regards,
Nikos

#11 lore raw

Re: [PATCH 01/19] User-space API definition

In reply to Herbert Xu
Quoted 2 lines · Herbert Xu

I will be looking at this myself so please stay tuned and be ready to yell if you see that your requirements are not met.

Quoted 4 lines · Ted Ts'o

So I'm bit at a list what's the whole point of this patch series. Could you explain that in the documentation, please? Especially for crypto, explaining when something should be used, what the threat model is, etc., is often very important.

Hello,
The document discussing the model, threats and design goals of this
framework can be found at:
https://www.cosic.esat.kuleuven.be/publications/article-1490.pdf

Comments and suggestions are welcome.

regards,
Nikos

PS. The existing (proposed) implementation covers about 90% of the design goals, the rest are work in progress.

#12 lore raw

PATCH 2/19 Add CRYPTO_USERSPACE config option

In reply to Miloslav Trmač
---
 crypto/Kconfig            |    5 +++++
 crypto/Makefile           |    2 ++
 crypto/userspace/Makefile |    1 +
 3 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100644 crypto/userspace/Makefile
crypto/Kconfig +5 -0
index 81c185a..022768a 100644
@@ -31,6 +31,11 @@ config CRYPTO_FIPS
 	  this is. Note that CRYPTO_ANSI_CPRNG is requred if this
 	  option is selected
 
+config CRYPTO_USERSPACE
+	tristate "User-space crypto API"
+	help
+	  This option provides an user-space API for cryptographic operations.
+
 config CRYPTO_ALGAPI
 	tristate
 	select CRYPTO_ALGAPI2
crypto/Makefile +2 -0
index 9e8f619..d284dff 100644
@@ -90,3 +90,5 @@ obj-$(CONFIG_CRYPTO_GHASH) += ghash-generic.o
 #
 obj-$(CONFIG_XOR_BLOCKS) += xor.o
 obj-$(CONFIG_ASYNC_CORE) += async_tx/
+
+obj-$(CONFIG_CRYPTO_USERSPACE) += userspace/
crypto/userspace/Makefile new file +1 -0
new file mode 100644
index 0000000..b607b90
@@ -0,0 +1 @@
+ccflags-y += -I$(src)/libtommath -I$(src)/libtomcrypt/headers -I$(src) -DLTC_SOURCE
Signature · 1 line
1.7.2.1
#13 lore raw

PATCH 3/19 Add libtommath headers

In reply to Miloslav Trmač

Not a good patch to start with, but the header file dependencies do not allow otherwise.

(Reviewing this in detail is probably premature, we are considering replacing the implementation by something based on libgcrypt, which is more actively maintained and has been probably more thorouhgly examined for vulnerabilities.)

---
 crypto/userspace/libtommath/tommath.h            |  555 ++++++++++++
 crypto/userspace/libtommath/tommath_class.h      |  999 ++++++++++++++++++++++
 crypto/userspace/libtommath/tommath_superclass.h |   76 ++
 3 files changed, 1630 insertions(+), 0 deletions(-)
 create mode 100644 crypto/userspace/libtommath/tommath.h
 create mode 100644 crypto/userspace/libtommath/tommath_class.h
 create mode 100644 crypto/userspace/libtommath/tommath_superclass.h
crypto/userspace/libtommath/tommath.h new file +555 -0
new file mode 100644
index 0000000..37fb23c
@@ -0,0 +1,555 @@
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://math.libtomcrypt.com
+ */
+#ifndef BN_H_
+#define BN_H_
+
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/random.h>
+#include <linux/ctype.h>
+
+#define CHAR_BIT sizeof(uint8_t)*8
+
+inline static int rand(void)
+{
+   int res;
+   
+   get_random_bytes(&res, sizeof(int));
+   
+   return res;
+}
+
+#include <tommath_class.h>
+
+#ifndef MIN
+   #define MIN(x,y) ((x)<(y)?(x):(y))
+#endif
+
+#ifndef MAX
+   #define MAX(x,y) ((x)>(y)?(x):(y))
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+
+/* C++ compilers don't like assigning void * to mp_digit * */
+#define  OPT_CAST(x)  (x *)
+
+#else
+
+/* C on the other hand doesn't care */
+#define  OPT_CAST(x)
+
+#endif
+
+
+/* some default configurations.
+ *
+ * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
+ * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
+ *
+ * At the very least a mp_digit must be able to hold 7 bits
+ * [any size beyond that is ok provided it doesn't overflow the data type]
+ */
+#if BITS_PER_LONG <= 32
+
+   typedef uint16_t      mp_digit;
+   typedef uint32_t      mp_word;
+# define DIGIT_BIT          15
+
+#elif BITS_PER_LONG == 64
+   
+   typedef uint32_t      mp_digit;
+   typedef uint64_t      mp_word;
+# define DIGIT_BIT          31
+
+#endif
+
+#if 0
+   /* if we could get a way to use an 128 bit integer
+    * in kernel, use this.
+    */
+   typedef uint64_t      mp_digit;
+   typedef __uint128_t      mp_word;
+# define DIGIT_BIT          60
+
+#endif
+
+/* define heap macros */
+#ifndef XMALLOC
+# define XMALLOC(x) kmalloc(x, GFP_KERNEL)
+# define XFREE kfree
+# define XREALLOC(x,y) krealloc(x,y, GFP_KERNEL)
+# define XCALLOC(x,y) kzalloc(x*y, GPF_KERNEL)
+#endif
+
+/* otherwise the bits per digit is calculated automatically from the size of a mp_digit */
+#ifndef DIGIT_BIT
+//   #define DIGIT_BIT     ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))  /* bits per digit */
+#endif
+
+#define MP_DIGIT_BIT     DIGIT_BIT
+#define MP_MASK          ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
+#define MP_DIGIT_MAX     MP_MASK
+
+/* equalities */
+#define MP_LT        -1   /* less than */
+#define MP_EQ         0   /* equal to */
+#define MP_GT         1   /* greater than */
+
+#define MP_ZPOS       0   /* positive integer */
+#define MP_NEG        1   /* negative */
+
+#define MP_OKAY       0   /* ok result */
+#define MP_MEM        -2  /* out of mem */
+#define MP_VAL        -3  /* invalid input */
+#define MP_RANGE      MP_VAL
+
+#define MP_YES        1   /* yes response */
+#define MP_NO         0   /* no response */
+
+/* Primality generation flags */
+#define LTM_PRIME_BBS      0x0001 /* BBS style prime */
+#define LTM_PRIME_SAFE     0x0002 /* Safe prime (p-1)/2 == prime */
+#define LTM_PRIME_2MSB_ON  0x0008 /* force 2nd MSB to 1 */
+
+typedef int           mp_err;
+
+/* you'll have to tune these... */
+extern int KARATSUBA_MUL_CUTOFF,
+           KARATSUBA_SQR_CUTOFF,
+           TOOM_MUL_CUTOFF,
+           TOOM_SQR_CUTOFF;
+
+/* define this to use lower memory usage routines (exptmods mostly) */
+/* We use this to reduce stack usage --nmav */
+#define MP_LOW_MEM
+
+/* default precision */
+#ifndef MP_PREC
+   #ifndef MP_LOW_MEM
+      #define MP_PREC                 32     /* default digits of precision */
+   #else
+      #define MP_PREC                 8      /* default digits of precision */
+   #endif   
+#endif
+
+/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
+#define MP_WARRAY               (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
+
+/* the infamous mp_int structure */
+typedef struct  {
+    int used, alloc, sign;
+    mp_digit *dp;
+} mp_int;
+
+/* callback for mp_prime_random, should fill dst with random bytes and return how many read [upto len] */
+typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
+
+
+#define USED(m)    ((m)->used)
+#define DIGIT(m,k) ((m)->dp[(k)])
+#define SIGN(m)    ((m)->sign)
+
+/* error code to char* string */
+char *mp_error_to_string(int code);
+
+/* ---> init and deinit bignum functions <--- */
+/* init a bignum */
+int mp_init(mp_int *a);
+
+/* free a bignum */
+void mp_clear(mp_int *a);
+
+/* init a null terminated series of arguments */
+int mp_init_multi(mp_int *mp, ...);
+
+/* clear a null terminated series of arguments */
+void mp_clear_multi(mp_int *mp, ...);
+
+/* exchange two ints */
+void mp_exch(mp_int *a, mp_int *b);
+
+/* shrink ram required for a bignum */
+int mp_shrink(mp_int *a);
+
+/* grow an int to a given size */
+int mp_grow(mp_int *a, int size);
+
+/* init to a given number of digits */
+int mp_init_size(mp_int *a, int size);
+
+/* ---> Basic Manipulations <--- */
+#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
+#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
+#define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
+
+/* set to zero */
+void mp_zero(mp_int *a);
+
+/* set to a digit */
+void mp_set(mp_int *a, mp_digit b);
+
+/* set a 32-bit const */
+int mp_set_int(mp_int *a, unsigned long b);
+
+/* get a 32-bit value */
+unsigned long mp_get_int(mp_int * a);
+
+/* initialize and set a digit */
+int mp_init_set (mp_int * a, mp_digit b);
+
+/* initialize and set 32-bit value */
+int mp_init_set_int (mp_int * a, unsigned long b);
+
+/* copy, b = a */
+int mp_copy(mp_int *a, mp_int *b);
+
+/* inits and copies, a = b */
+int mp_init_copy(mp_int *a, mp_int *b);
+
+/* trim unused digits */
+void mp_clamp(mp_int *a);
+
+/* ---> digit manipulation <--- */
+
+/* right shift by "b" digits */
+void mp_rshd(mp_int *a, int b);
+
+/* left shift by "b" digits */
+int mp_lshd(mp_int *a, int b);
+
+/* c = a / 2**b */
+int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d);
+
+/* b = a/2 */
+int mp_div_2(mp_int *a, mp_int *b);
+
+/* c = a * 2**b */
+int mp_mul_2d(mp_int *a, int b, mp_int *c);
+
+/* b = a*2 */
+int mp_mul_2(mp_int *a, mp_int *b);
+
+/* c = a mod 2**d */
+int mp_mod_2d(mp_int *a, int b, mp_int *c);
+
+/* computes a = 2**b */
+int mp_2expt(mp_int *a, int b);
+
+/* Counts the number of lsbs which are zero before the first zero bit */
+int mp_cnt_lsb(mp_int *a);
+
+/* I Love Earth! */
+
+/* makes a pseudo-random int of a given size */
+int mp_rand(mp_int *a, int digits);
+
+/* ---> binary operations <--- */
+/* c = a XOR b  */
+int mp_xor(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = a OR b */
+int mp_or(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = a AND b */
+int mp_and(mp_int *a, mp_int *b, mp_int *c);
+
+/* ---> Basic arithmetic <--- */
+
+/* b = -a */
+int mp_neg(mp_int *a, mp_int *b);
+
+/* b = |a| */
+int mp_abs(mp_int *a, mp_int *b);
+
+/* compare a to b */
+int mp_cmp(mp_int *a, mp_int *b);
+
+/* compare |a| to |b| */
+int mp_cmp_mag(mp_int *a, mp_int *b);
+
+/* c = a + b */
+int mp_add(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = a - b */
+int mp_sub(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = a * b */
+int mp_mul(mp_int *a, mp_int *b, mp_int *c);
+
+/* b = a*a  */
+int mp_sqr(mp_int *a, mp_int *b);
+
+/* a/b => cb + d == a */
+int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
+
+/* c = a mod b, 0 <= c < b  */
+int mp_mod(mp_int *a, mp_int *b, mp_int *c);
+
+/* ---> single digit functions <--- */
+
+/* compare against a single digit */
+int mp_cmp_d(mp_int *a, mp_digit b);
+
+/* c = a + b */
+int mp_add_d(mp_int *a, mp_digit b, mp_int *c);
+
+/* c = a - b */
+int mp_sub_d(mp_int *a, mp_digit b, mp_int *c);
+
+/* c = a * b */
+int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
+
+/* a/b => cb + d == a */
+int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
+
+/* a/3 => 3c + d == a */
+int mp_div_3(mp_int *a, mp_int *c, mp_digit *d);
+
+/* c = a**b */
+int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
+
+/* c = a mod b, 0 <= c < b  */
+int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
+
+/* ---> number theory <--- */
+
+/* d = a + b (mod c) */
+int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
+
+/* d = a - b (mod c) */
+int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
+
+/* d = a * b (mod c) */
+int mp_mulmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
+
+/* c = a * a (mod b) */
+int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = 1/a (mod b) */
+int mp_invmod(mp_int *a, mp_int *b, mp_int *c);
+
+/* c = (a, b) */
+int mp_gcd(mp_int *a, mp_int *b, mp_int *c);
+
+/* produces value such that U1*a + U2*b = U3 */
+int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3);
+
+/* c = [a, b] or (a*b)/(a, b) */
+int mp_lcm(mp_int *a, mp_int *b, mp_int *c);
+
+/* finds one of the b'th root of a, such that |c|**b <= |a|
+ *
+ * returns error if a < 0 and b is even
+ */
+int mp_n_root(mp_int *a, mp_digit b, mp_int *c);
+
+/* special sqrt algo */
+int mp_sqrt(mp_int *arg, mp_int *ret);
+
+/* is number a square? */
+int mp_is_square(mp_int *arg, int *ret);
+
+/* computes the jacobi c = (a | n) (or Legendre if b is prime)  */
+int mp_jacobi(mp_int *a, mp_int *n, int *c);
+
+/* used to setup the Barrett reduction for a given modulus b */
+int mp_reduce_setup(mp_int *a, mp_int *b);
+
+/* Barrett Reduction, computes a (mod b) with a precomputed value c
+ *
+ * Assumes that 0 < a <= b*b, note if 0 > a > -(b*b) then you can merely
+ * compute the reduction as -1 * mp_reduce(mp_abs(a)) [pseudo code].
+ */
+int mp_reduce(mp_int *a, mp_int *b, mp_int *c);
+
+/* setups the montgomery reduction */
+int mp_montgomery_setup(mp_int *a, mp_digit *mp);
+
+/* computes a = B**n mod b without division or multiplication useful for
+ * normalizing numbers in a Montgomery system.
+ */
+int mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
+
+/* computes x/R == x (mod N) via Montgomery Reduction */
+int mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
+
+/* returns 1 if a is a valid DR modulus */
+int mp_dr_is_modulus(mp_int *a);
+
+/* sets the value of "d" required for mp_dr_reduce */
+void mp_dr_setup(mp_int *a, mp_digit *d);
+
+/* reduces a modulo b using the Diminished Radix method */
+int mp_dr_reduce(mp_int *a, mp_int *b, mp_digit mp);
+
+/* returns true if a can be reduced with mp_reduce_2k */
+int mp_reduce_is_2k(mp_int *a);
+
+/* determines k value for 2k reduction */
+int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
+
+/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
+int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
+
+/* returns true if a can be reduced with mp_reduce_2k_l */
+int mp_reduce_is_2k_l(mp_int *a);
+
+/* determines k value for 2k reduction */
+int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
+
+/* reduces a modulo b where b is of the form 2**p - k [0 <= a] */
+int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
+
+/* d = a**b (mod c) */
+int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
+
+/* ---> Primes <--- */
+
+/* number of primes */
+#ifdef MP_8BIT
+   #define PRIME_SIZE      31
+#else
+   #define PRIME_SIZE      256
+#endif
+
+/* table of first PRIME_SIZE primes */
+extern const mp_digit ltm_prime_tab[];
+
+/* result=1 if a is divisible by one of the first PRIME_SIZE primes */
+int mp_prime_is_divisible(mp_int *a, int *result);
+
+/* performs one Fermat test of "a" using base "b".
+ * Sets result to 0 if composite or 1 if probable prime
+ */
+int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
+
+/* performs one Miller-Rabin test of "a" using base "b".
+ * Sets result to 0 if composite or 1 if probable prime
+ */
+int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
+
+/* This gives [for a given bit size] the number of trials required
+ * such that Miller-Rabin gives a prob of failure lower than 2^-96 
+ */
+int mp_prime_rabin_miller_trials(int size);
+
+/* performs t rounds of Miller-Rabin on "a" using the first
+ * t prime bases.  Also performs an initial sieve of trial
+ * division.  Determines if "a" is prime with probability
+ * of error no more than (1/4)**t.
+ *
+ * Sets result to 1 if probably prime, 0 otherwise
+ */
+int mp_prime_is_prime(mp_int *a, int t, int *result);
+
+/* finds the next prime after the number "a" using "t" trials
+ * of Miller-Rabin.
+ *
+ * bbs_style = 1 means the prime must be congruent to 3 mod 4
+ */
+int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
+
+/* makes a truly random prime of a given size (bytes),
+ * call with bbs = 1 if you want it to be congruent to 3 mod 4 
+ *
+ * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
+ * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
+ * so it can be NULL
+ *
+ * The prime generated will be larger than 2^(8*size).
+ */
+#define mp_prime_random(a, t, size, bbs, cb, dat) mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
+
+/* makes a truly random prime of a given size (bits),
+ *
+ * Flags are as follows:
+ * 
+ *   LTM_PRIME_BBS      - make prime congruent to 3 mod 4
+ *   LTM_PRIME_SAFE     - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
+ *   LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
+ *   LTM_PRIME_2MSB_ON  - make the 2nd highest bit one
+ *
+ * You have to supply a callback which fills in a buffer with random bytes.  "dat" is a parameter you can
+ * have passed to the callback (e.g. a state or something).  This function doesn't use "dat" itself
+ * so it can be NULL
+ *
+ */
+int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback cb, void *dat);
+
+/* ---> radix conversion <--- */
+int mp_count_bits(mp_int *a);
+
+int mp_unsigned_bin_size(mp_int *a);
+int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c);
+int mp_to_unsigned_bin(mp_int *a, unsigned char *b);
+int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
+
+int mp_signed_bin_size(mp_int *a);
+int mp_read_signed_bin(mp_int *a, const unsigned char *b, int c);
+int mp_to_signed_bin(mp_int *a,  unsigned char *b);
+int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen);
+
+int mp_read_radix(mp_int *a, const char *str, int radix);
+int mp_toradix(mp_int *a, char *str, int radix);
+int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen);
+int mp_radix_size(mp_int *a, int radix, int *size);
+
+#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
+#define mp_raw_size(mp)           mp_signed_bin_size(mp)
+#define mp_toraw(mp, str)         mp_to_signed_bin((mp), (str))
+#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
+#define mp_mag_size(mp)           mp_unsigned_bin_size(mp)
+#define mp_tomag(mp, str)         mp_to_unsigned_bin((mp), (str))
+
+#define mp_tobinary(M, S)  mp_toradix((M), (S), 2)
+#define mp_tooctal(M, S)   mp_toradix((M), (S), 8)
+#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
+#define mp_tohex(M, S)     mp_toradix((M), (S), 16)
+
+/* lowlevel functions, do not call! */
+int s_mp_add(mp_int *a, mp_int *b, mp_int *c);
+int s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
+#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
+int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
+int s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
+int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
+int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs);
+int fast_s_mp_sqr(mp_int *a, mp_int *b);
+int s_mp_sqr(mp_int *a, mp_int *b);
+int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c);
+int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c);
+int mp_karatsuba_sqr(mp_int *a, mp_int *b);
+int mp_toom_sqr(mp_int *a, mp_int *b);
+int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c);
+int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
+int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
+int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode);
+int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode);
+void bn_reverse(unsigned char *s, int len);
+
+extern const char *mp_s_rmap;
+
+#ifdef __cplusplus
+   }
+#endif
+
+#endif
+
+
+/* $Source: /cvs/libtom/libtommath/tommath.h,v $ */
+/* $Revision: 1.8 $ */
+/* $Date: 2006/03/31 14:18:44 $ */
crypto/userspace/libtommath/tommath_class.h new file +999 -0

999 lines not on this page · raw message

crypto/userspace/libtommath/tommath_superclass.h new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#14 lore raw

PATCH 4/19 Add libtomcrypt headers

In reply to Miloslav Trmač

Same as the previous patch - the header file dependencies do not allow otherwise.

(Reviewing this in detail is probably premature, we are considering replacing the implementation by something based on libgcrypt, which is more actively maintained and has been probably more thorouhgly examined for vulnerabilities.)

---
 crypto/userspace/libtomcrypt/headers/tomcrypt.h    |   82 ++++
 .../libtomcrypt/headers/tomcrypt_argchk.h          |   36 ++
 .../userspace/libtomcrypt/headers/tomcrypt_cfg.h   |  135 +++++++
 .../libtomcrypt/headers/tomcrypt_custom.h          |  408 +++++++++++++++++++
 .../userspace/libtomcrypt/headers/tomcrypt_hash.h  |   14 +
 .../libtomcrypt/headers/tomcrypt_macros.h          |  424 ++++++++++++++++++++
 .../userspace/libtomcrypt/headers/tomcrypt_math.h  |   13 +
 .../userspace/libtomcrypt/headers/tomcrypt_misc.h  |   23 +
 crypto/userspace/libtomcrypt/headers/tomcrypt_pk.h |  359 +++++++++++++++++
 .../userspace/libtomcrypt/headers/tomcrypt_pkcs.h  |   67 +++
 10 files changed, 1561 insertions(+), 0 deletions(-)
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_argchk.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_cfg.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_custom.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_hash.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_macros.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_math.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_misc.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_pk.h
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_pkcs.h
crypto/userspace/libtomcrypt/headers/tomcrypt.h new file +82 -0
new file mode 100644
index 0000000..51fe804
@@ -0,0 +1,82 @@
+#ifndef TOMCRYPT_H_
+#define TOMCRYPT_H_
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/string.h>
+#include <linux/ctype.h>
+#include <linux/nls.h>
+
+/* use configuration data */
+#include <tomcrypt_custom.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* version */
+#define CRYPT   0x0117
+#define SCRYPT  "1.17"
+
+/* max size of either a cipher/hash block or symmetric key [largest of the two] */
+#define MAXBLOCKSIZE  128
+
+/* descriptor table size */
+#define TAB_SIZE      32
+
+/* error codes [will be expanded in future releases] */
+enum {
+   CRYPT_OK=0,             /* Result OK */
+   CRYPT_ERROR,            /* Generic Error */
+   CRYPT_NOP,              /* Not a failure but no operation was performed */
+
+   CRYPT_INVALID_KEYSIZE,  /* Invalid key size given */
+   CRYPT_INVALID_ROUNDS,   /* Invalid number of rounds */
+   CRYPT_FAIL_TESTVECTOR,  /* Algorithm failed test vectors */
+
+   CRYPT_BUFFER_OVERFLOW,  /* Not enough space for output */
+   CRYPT_INVALID_PACKET,   /* Invalid input packet given */
+
+   CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */
+   CRYPT_ERROR_READPRNG,   /* Could not read enough from PRNG */
+
+   CRYPT_INVALID_CIPHER,   /* Invalid cipher specified */
+   CRYPT_INVALID_HASH,     /* Invalid hash specified */
+   CRYPT_INVALID_PRNG,     /* Invalid PRNG specified */
+
+   CRYPT_MEM,              /* Out of memory */
+
+   CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */
+   CRYPT_PK_NOT_PRIVATE,   /* Requires a private PK key */
+
+   CRYPT_INVALID_ARG,      /* Generic invalid argument */
+   CRYPT_FILE_NOTFOUND,    /* File Not Found */
+
+   CRYPT_PK_INVALID_TYPE,  /* Invalid type of PK key */
+   CRYPT_PK_INVALID_SYSTEM,/* Invalid PK system specified */
+   CRYPT_PK_DUP,           /* Duplicate key already in key ring */
+   CRYPT_PK_NOT_FOUND,     /* Key not found in keyring */
+   CRYPT_PK_INVALID_SIZE,  /* Invalid size input for PK parameters */
+
+   CRYPT_INVALID_PRIME_SIZE,/* Invalid size of prime requested */
+   CRYPT_PK_INVALID_PADDING /* Invalid padding on input */
+};
+
+#include <tomcrypt_cfg.h>
+#include <tomcrypt_macros.h>
+#include <tomcrypt_math.h>
+#include <tomcrypt_pk.h>
+#include <tomcrypt_hash.h>
+#include <tomcrypt_misc.h>
+#include <tomcrypt_argchk.h>
+#include <tomcrypt_pkcs.h>
+
+#ifdef __cplusplus
+   }
+#endif
+
+#endif /* TOMCRYPT_H_ */
+
+
+/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt.h,v $ */
+/* $Revision: 1.21 $ */
+/* $Date: 2006/12/16 19:34:05 $ */
crypto/userspace/libtomcrypt/headers/tomcrypt_argchk.h new file +36 -0
new file mode 100644
index 0000000..1b94434
@@ -0,0 +1,36 @@
+/* Defines the LTC_ARGCHK macro used within the library */
+/* ARGTYPE is defined in mycrypt_cfg.h */
+#if ARGTYPE == 0
+
+/* this is the default LibTomCrypt macro  */
+void crypt_argchk(char *v, char *s, int d);
+#define LTC_ARGCHK(x) if (!(x)) { crypt_argchk(#x, __FILE__, __LINE__); }
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 1
+
+/* fatal type of error */
+#define LTC_ARGCHK(x) assert((x))
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 2
+
+#define LTC_ARGCHK(x) if (!(x)) { fprintf(stderr, "\nwarning: ARGCHK failed at %s:%d\n", __FILE__, __LINE__); }
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 3
+
+#define LTC_ARGCHK(x) 
+#define LTC_ARGCHKVD(x) LTC_ARGCHK(x)
+
+#elif ARGTYPE == 4
+
+#define LTC_ARGCHK(x)   if (!(x)) return CRYPT_INVALID_ARG;
+#define LTC_ARGCHKVD(x) if (!(x)) return;
+
+#endif
+
+
+/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_argchk.h,v $ */
+/* $Revision: 1.5 $ */
+/* $Date: 2006/08/27 20:50:21 $ */
crypto/userspace/libtomcrypt/headers/tomcrypt_cfg.h new file +135 -0

135 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_custom.h new file +408 -0

408 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_hash.h new file +14 -0
new file mode 100644
index 0000000..e4e84e4
@@ -0,0 +1,14 @@
+/* ---- HASH FUNCTIONS ---- */
+
+struct algo_properties_st;
+
+int hash_is_valid(const struct algo_properties_st *hash);
+
+int hash_memory(const struct algo_properties_st *hash,
+                const unsigned char *in,  unsigned long inlen, 
+                      unsigned char *out, unsigned long *outlen);
+int hash_memory_multi(const struct algo_properties_st *hash, unsigned char *out, unsigned long *outlen,
+                      const unsigned char *in, unsigned long inlen, ...);
+
+int hash_get_oid(const struct algo_properties_st *hash, oid_st* st);
+
crypto/userspace/libtomcrypt/headers/tomcrypt_macros.h new file +424 -0

424 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_math.h new file +13 -0
new file mode 100644
index 0000000..6c0e6c5
@@ -0,0 +1,13 @@
+/** math functions **/
+
+#define LTC_MP_LT   -1
+#define LTC_MP_EQ    0
+#define LTC_MP_GT    1
+
+#define LTC_MP_NO    0
+#define LTC_MP_YES   1
+
+#include <tommath.h>
+
+typedef mp_int* mp_int_t;
+
crypto/userspace/libtomcrypt/headers/tomcrypt_misc.h new file +23 -0
new file mode 100644
index 0000000..f5384ca
@@ -0,0 +1,23 @@
+/* ---- LTC_BASE64 Routines ---- */
+#ifdef LTC_BASE64
+int base64_encode(const unsigned char *in,  unsigned long len, 
+                        unsigned char *out, unsigned long *outlen);
+
+int base64_decode(const unsigned char *in,  unsigned long len, 
+                        unsigned char *out, unsigned long *outlen);
+#endif
+
+/* ---- MEM routines ---- */
+void zeromem(void *dst, size_t len);
+void burn_stack(unsigned long len);
+
+const char *error_to_string(int err);
+
+extern const char *crypt_build_settings;
+
+/* ---- HMM ---- */
+int crypt_fsa(void *mp, ...);
+
+/* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_misc.h,v $ */
+/* $Revision: 1.5 $ */
+/* $Date: 2007/05/12 14:32:35 $ */
crypto/userspace/libtomcrypt/headers/tomcrypt_pk.h new file +359 -0

359 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_pkcs.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#15 lore raw

PATCH 5/19 Add internal /dev/crypto implementation headers

In reply to Miloslav Trmač

That's it, .c files will finally follow in the next patch.

---
 crypto/userspace/cryptodev_int.h |   82 +++++++++++++
 crypto/userspace/ncr-dh.h        |   25 ++++
 crypto/userspace/ncr-int.h       |  245 ++++++++++++++++++++++++++++++++++++++
 crypto/userspace/ncr-pk.h        |   55 +++++++++
 4 files changed, 407 insertions(+), 0 deletions(-)
 create mode 100644 crypto/userspace/cryptodev_int.h
 create mode 100644 crypto/userspace/ncr-dh.h
 create mode 100644 crypto/userspace/ncr-int.h
 create mode 100644 crypto/userspace/ncr-pk.h
crypto/userspace/cryptodev_int.h new file +82 -0
new file mode 100644
index 0000000..6754427
@@ -0,0 +1,82 @@
+/* cipher stuff */
+#ifndef CRYPTODEV_INT_H
+# define CRYPTODEV_INT_H
+
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/fdtable.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/ncr.h>
+#include <linux/scatterlist.h>
+
+#define PFX "cryptodev: "
+#define dprintk(level,severity,format,a...)			\
+	do {						\
+		if (level <= cryptodev_verbosity)				\
+			printk(severity PFX "%s[%u]: " format,	\
+			       current->comm, current->pid,	\
+			       ##a);				\
+	} while (0)
+
+extern int cryptodev_verbosity;
+
+/* For zero copy */
+int __get_userbuf(uint8_t __user *addr, uint32_t len, int write,
+		int pgcount, struct page **pg, struct scatterlist *sg);
+void release_user_pages(struct page **pg, int pagecount);
+
+/* last page - first page + 1 */
+#define PAGECOUNT(buf, buflen) \
+        ((((unsigned long)(buf + buflen - 1) & PAGE_MASK) >> PAGE_SHIFT) - \
+         (((unsigned long) buf               & PAGE_MASK) >> PAGE_SHIFT) + 1)
+
+#define DEFAULT_PREALLOC_PAGES 32
+
+struct cipher_data
+{
+	int init; /* 0 uninitialized */
+	int blocksize;
+	int ivsize;
+	struct {
+		struct crypto_ablkcipher* s;
+		struct cryptodev_result *result;
+		struct ablkcipher_request *request;
+		uint8_t iv[NCR_CIPHER_MAX_BLOCK_LEN];
+	} async;
+};
+
+int cryptodev_cipher_init(struct cipher_data* out, const char* alg_name, uint8_t * key, size_t keylen);
+void cryptodev_cipher_deinit(struct cipher_data* cdata);
+ssize_t cryptodev_cipher_decrypt( struct cipher_data* cdata, const struct scatterlist *sg1, struct scatterlist *sg2, size_t len);
+ssize_t cryptodev_cipher_encrypt( struct cipher_data* cdata, const struct scatterlist *sg1, struct scatterlist *sg2, size_t len);
+
+void cryptodev_cipher_set_iv(struct cipher_data* cdata, void* iv, size_t iv_size);
+int _cryptodev_cipher_decrypt(struct cipher_data* cdata, const void* ciphertext,
+	size_t ciphertext_size, void* plaintext, size_t plaintext_size);
+int _cryptodev_cipher_encrypt(struct cipher_data* cdata, const void* plaintext,
+	size_t plaintext_size, void* ciphertext, size_t ciphertext_size);
+
+/* hash stuff */
+struct hash_data
+{
+	int init; /* 0 uninitialized */
+	int digestsize;
+	struct {
+		struct crypto_ahash *s;
+		struct cryptodev_result *result;
+		struct ahash_request *request;
+	} async;
+};
+
+int cryptodev_hash_final( struct hash_data* hdata, void* output);
+ssize_t cryptodev_hash_update( struct hash_data* hdata, struct scatterlist *sg, size_t len);
+ssize_t _cryptodev_hash_update( struct hash_data* hdata, const void* data, size_t len);
+int cryptodev_hash_reset( struct hash_data* hdata);
+void cryptodev_hash_deinit(struct hash_data* hdata);
+int cryptodev_hash_init( struct hash_data* hdata, const char* alg_name, int hmac_mode, void* mackey, size_t mackeylen);
+
+#endif /* CRYPTODEV_INT_H */
crypto/userspace/ncr-dh.h new file +25 -0
new file mode 100644
index 0000000..cc45d32
@@ -0,0 +1,25 @@
+#ifndef NCR_DH_H
+# define NCR_DH_H
+
+#include <tomcrypt.h>
+
+typedef struct {
+	int type; /* PK_PRIVATE or PK_PUBLIC */
+	mp_int p;
+	mp_int g;
+	mp_int x; /* private */
+	mp_int y; /* public: y=g^x */
+} dh_key;
+
+int dh_generate_key(dh_key * key);
+int dh_import_params(dh_key * key, uint8_t* p, size_t p_size, uint8_t* g, size_t g_size);
+void dh_free(dh_key * key);
+int dh_generate_public(dh_key * public, dh_key* private);
+
+int dh_export(uint8_t *out, unsigned long *outlen, int type, dh_key *key);
+int dh_import(const uint8_t *in, size_t inlen, dh_key *key);
+
+int dh_derive_gxy(struct key_item_st* newkey, dh_key * key,
+	void* pk, size_t pk_size);
+
+#endif
crypto/userspace/ncr-int.h new file +245 -0

245 lines not on this page · raw message

crypto/userspace/ncr-pk.h new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#16 lore raw

PATCH 6/19 Add ioctl() argument and attribute handling utils

In reply to Miloslav Trmač

Main entry points:

NCR_GET_INPUT_ARGS:
Read a fixed struct and any attached attributes from userspace

NCR_GET_INPUT_ARGS_NO_OUTPUT:
Same as above, and inform the users the kernel will attach no additional attributes.

NCR_OUT_INIT/ncr_out_free:
Allocate and free a context that allows operation handlers to return attributes to userspace.

ncr_out_finish:
Write the attributes from the context to userspace. Split like this so that only NCR_OUT_INIT (in future "ncr.c") deals with the raw ioctl() argument, but the specific operation can use this call to detect errors and perhaps clean up.

ncr_out_*:
Add attributes to the context.

---
 crypto/userspace/Makefile |    5 +
 crypto/userspace/utils.c  |  297 +++++++++++++++++++++++++++++++++++++++++++++
 crypto/userspace/utils.h  |   98 +++++++++++++++
 3 files changed, 400 insertions(+), 0 deletions(-)
 create mode 100644 crypto/userspace/utils.c
 create mode 100644 crypto/userspace/utils.h
crypto/userspace/Makefile +5 -0
index b607b90..f7f3ea2 100644
@@ -1 +1,6 @@
 ccflags-y += -I$(src)/libtommath -I$(src)/libtomcrypt/headers -I$(src) -DLTC_SOURCE
+
+cryptodev-objs := utils.o
+
+
+obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/utils.c new file +297 -0

297 lines not on this page · raw message

crypto/userspace/utils.h new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#17 lore raw

Re: [PATCH 06/19] Add ioctl() argument and attribute handling utils

Quoted 2 lines · Miloslav Trmač
crypto/userspace/utils.c new file +0 -0

[...]

Quoted 8 lines
crypto/userspace/utils.c new file +8 -0
+#ifdef CONFIG_COMPAT
+/* max() is too clever for compile-time constants */
+#define CONST_MAX(A, B) ((A) > (B) ? (A) : (B))
+
+#define MAX_SESSION_INPUT_DATA_SIZE					\
+	(CONST_MAX(sizeof(struct ncr_session_input_data),		\
+		   sizeof(struct compat_ncr_session_input_data)))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE					\

crypto/userspace/utils.c line 48

Quoted 10 lines
crypto/userspace/utils.c new file +10 -0
+	(CONST_MAX(sizeof(struct ncr_session_output_buffer),		\
+		   sizeof(struct compat_ncr_session_output_buffer)))
+
+#else /* !CONFIG_COMPAT */
+
+#define MAX_SESSION_INPUT_DATA_SIZE (sizeof(struct ncr_session_input_data))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE			\
+	(sizeof(struct ncr_session_output_buffer))
+
+#endif /* !CONFIG_COMPAT */

struct compat_xyz cannot be bigger than struct xyz, can it?

Signature · 3 lines
Stefan Richter
-=====-==-=- =--- =-=--
http://arcgraph.de/sr/
#18 lore raw

Re: [PATCH 06/19] Add ioctl() argument and attribute handling utils

Quoted 16 lines · ----- "Stefan Richter
Quoted 2 lines · Miloslav Trmač
crypto/userspace/utils.c new file +0 -0

[...]

Quoted 12 lines
crypto/userspace/utils.c new file +12 -0
+#ifdef CONFIG_COMPAT
+/* max() is too clever for compile-time constants */
+#define CONST_MAX(A, B) ((A) > (B) ? (A) : (B))
+
+#define MAX_SESSION_INPUT_DATA_SIZE					\
+	(CONST_MAX(sizeof(struct ncr_session_input_data),		\
+		   sizeof(struct compat_ncr_session_input_data)))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE					\
+	(CONST_MAX(sizeof(struct ncr_session_output_buffer),		\
+		   sizeof(struct compat_ncr_session_output_buffer)))
+
+#else /* !CONFIG_COMPAT */
Quoted 8 lines

crypto/userspace/utils.c line 48

Quoted 6 lines
crypto/userspace/utils.c new file +6 -0
+
+#define MAX_SESSION_INPUT_DATA_SIZE (sizeof(struct ncr_session_input_data))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE			\
+	(sizeof(struct ncr_session_output_buffer))
+
+#endif /* !CONFIG_COMPAT */

struct compat_xyz cannot be bigger than struct xyz, can it?

Perhaps not, but the code has been written and it's easier to keep it than to speculate :) It also helps document the fact that the data format depends on properties of the userspace.
Mirek

#19 lore raw

Re: [PATCH 06/19] Add ioctl() argument and attribute handling utils

Quoted 26 lines · Miloslav Trmac
Quoted 16 lines · ----- "Stefan Richter
Quoted 2 lines · Miloslav Trmač
crypto/userspace/utils.c new file +0 -0

[...]

Quoted 12 lines
crypto/userspace/utils.c new file +12 -0
+#ifdef CONFIG_COMPAT
+/* max() is too clever for compile-time constants */
+#define CONST_MAX(A, B) ((A) > (B) ? (A) : (B))
+
+#define MAX_SESSION_INPUT_DATA_SIZE					\
+	(CONST_MAX(sizeof(struct ncr_session_input_data),		\
+		   sizeof(struct compat_ncr_session_input_data)))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE					\
+	(CONST_MAX(sizeof(struct ncr_session_output_buffer),		\
+		   sizeof(struct compat_ncr_session_output_buffer)))
+
+#else /* !CONFIG_COMPAT */
Quoted 8 lines

crypto/userspace/utils.c line 48

Quoted 6 lines
crypto/userspace/utils.c new file +6 -0
+
+#define MAX_SESSION_INPUT_DATA_SIZE (sizeof(struct ncr_session_input_data))
+#define MAX_SESSION_OUTPUT_BUFFER_SIZE			\
+	(sizeof(struct ncr_session_output_buffer))
+
+#endif /* !CONFIG_COMPAT */

struct compat_xyz cannot be bigger than struct xyz, can it?

Quoted 2 lines

Perhaps not, but the code has been written and it's easier to keep it than to speculate :)

What's there to speculate? :-) COMPAT = 32bit user on 64bit kernel. The latter has larger long and pointers and possibly different alignment of 64bit data.

Quoted 2 lines

It also helps document the fact that the data format depends on properties of the userspace.

Since it is a new ABI, it can be written to not need COMPAT ifdef'ery nor compat handlers.

Signature · 3 lines
Stefan Richter
-=====-==-=- =--- =-=-=
http://arcgraph.de/sr/
#20 lore raw

PATCH 7/19 Add crypto API utilities.

In reply to Miloslav Trmač

This encapsulates allocation/deallocation of all necessary objects, dealing with the asynchronous nature of ablkcipher/ahash.

Long term, I'm not quite sure this layer makes sense; For now, it provides a truly simple API for internal callers in libtomcrypt, at least.

---
 crypto/userspace/Makefile           |    2 +-
 crypto/userspace/cryptodev_cipher.c |  339 +++++++++++++++++++++++++++++++++++
 crypto/userspace/cryptodev_main.c   |   13 ++
 3 files changed, 353 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/cryptodev_cipher.c
 create mode 100644 crypto/userspace/cryptodev_main.c
crypto/userspace/Makefile +1 -1
index f7f3ea2..a37969b 100644
@@ -1,6 +1,6 @@
 ccflags-y += -I$(src)/libtommath -I$(src)/libtomcrypt/headers -I$(src) -DLTC_SOURCE
 
-cryptodev-objs := utils.o
+cryptodev-objs := cryptodev_main.o cryptodev_cipher.o utils.o
 
 
 obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/cryptodev_cipher.c new file +339 -0

339 lines not on this page · raw message

crypto/userspace/cryptodev_main.c new file +13 -0
new file mode 100644
index 0000000..c6419f4
@@ -0,0 +1,13 @@
+#include "cryptodev_int.h"
+#include "ncr-int.h"
+#include <linux/version.h>
+
+MODULE_AUTHOR("Nikos Mavrogiannopoulos <[email protected]>");
+MODULE_DESCRIPTION("CryptoDev driver");
+MODULE_LICENSE("GPL");
+
+/* ====== Module parameters ====== */
+
+int cryptodev_verbosity = 0;
+module_param(cryptodev_verbosity, int, 0644);
+MODULE_PARM_DESC(cryptodev_verbosity, "0: normal, 1: verbose, 2: debug");
Signature · 1 line
1.7.2.1
#21 lore raw

PATCH 8/19 Add per-process and per-user limits

In reply to Miloslav Trmač

Right now only key objects, not crypto sessions, are limited.

---
 crypto/userspace/Makefile     |    2 +-
 crypto/userspace/ncr-limits.c |  247 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 248 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/ncr-limits.c
crypto/userspace/Makefile +1 -1
index a37969b..3dfbc39 100644
@@ -1,6 +1,6 @@
 ccflags-y += -I$(src)/libtommath -I$(src)/libtomcrypt/headers -I$(src) -DLTC_SOURCE
 
-cryptodev-objs := cryptodev_main.o cryptodev_cipher.o utils.o
+cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o utils.o
 
 
 obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/ncr-limits.c new file +247 -0

247 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#22 lore raw

PATCH 9/19 Add libtommath implementation

In reply to Miloslav Trmač

No use postponing it any more I'm afraid.

(Reviewing this in detail is probably premature, we are considering replacing the implementation by something based on libgcrypt, which is more actively maintained and has been probably more thorouhgly examined for vulnerabilities.)

---
 crypto/userspace/Makefile                          |   31 ++-
 crypto/userspace/libtommath/LICENSE                |    4 +
 crypto/userspace/libtommath/bn_error.c             |   47 +++
 crypto/userspace/libtommath/bn_fast_mp_invmod.c    |  148 ++++++++
 .../libtommath/bn_fast_mp_montgomery_reduce.c      |  172 +++++++++
 .../userspace/libtommath/bn_fast_s_mp_mul_digs.c   |  107 ++++++
 .../libtommath/bn_fast_s_mp_mul_high_digs.c        |   98 +++++
 crypto/userspace/libtommath/bn_fast_s_mp_sqr.c     |  114 ++++++
 crypto/userspace/libtommath/bn_mp_2expt.c          |   48 +++
 crypto/userspace/libtommath/bn_mp_abs.c            |   43 +++
 crypto/userspace/libtommath/bn_mp_add.c            |   53 +++
 crypto/userspace/libtommath/bn_mp_add_d.c          |  112 ++++++
 crypto/userspace/libtommath/bn_mp_addmod.c         |   41 ++
 crypto/userspace/libtommath/bn_mp_and.c            |   57 +++
 crypto/userspace/libtommath/bn_mp_clamp.c          |   44 +++
 crypto/userspace/libtommath/bn_mp_clear.c          |   45 +++
 crypto/userspace/libtommath/bn_mp_clear_multi.c    |   34 ++
 crypto/userspace/libtommath/bn_mp_cmp.c            |   43 +++
 crypto/userspace/libtommath/bn_mp_cmp_d.c          |   44 +++
 crypto/userspace/libtommath/bn_mp_cmp_mag.c        |   55 +++
 crypto/userspace/libtommath/bn_mp_cnt_lsb.c        |   53 +++
 crypto/userspace/libtommath/bn_mp_copy.c           |   68 ++++
 crypto/userspace/libtommath/bn_mp_count_bits.c     |   45 +++
 crypto/userspace/libtommath/bn_mp_div.c            |  292 ++++++++++++++
 crypto/userspace/libtommath/bn_mp_div_2.c          |   68 ++++
 crypto/userspace/libtommath/bn_mp_div_2d.c         |   97 +++++
 crypto/userspace/libtommath/bn_mp_div_3.c          |   79 ++++
 crypto/userspace/libtommath/bn_mp_div_d.c          |  115 ++++++
 crypto/userspace/libtommath/bn_mp_dr_is_modulus.c  |   43 +++
 crypto/userspace/libtommath/bn_mp_dr_reduce.c      |   94 +++++
 crypto/userspace/libtommath/bn_mp_dr_setup.c       |   32 ++
 crypto/userspace/libtommath/bn_mp_exch.c           |   34 ++
 crypto/userspace/libtommath/bn_mp_expt_d.c         |   57 +++
 crypto/userspace/libtommath/bn_mp_exptmod.c        |  112 ++++++
 crypto/userspace/libtommath/bn_mp_exptmod_fast.c   |  321 ++++++++++++++++
 crypto/userspace/libtommath/bn_mp_exteuclid.c      |   82 ++++
 crypto/userspace/libtommath/bn_mp_gcd.c            |  105 +++++
 crypto/userspace/libtommath/bn_mp_get_int.c        |   45 +++
 crypto/userspace/libtommath/bn_mp_grow.c           |   58 +++
 crypto/userspace/libtommath/bn_mp_init.c           |   47 +++
 crypto/userspace/libtommath/bn_mp_init_copy.c      |   32 ++
 crypto/userspace/libtommath/bn_mp_init_multi.c     |   59 +++
 crypto/userspace/libtommath/bn_mp_init_set.c       |   32 ++
 crypto/userspace/libtommath/bn_mp_init_set_int.c   |   31 ++
 crypto/userspace/libtommath/bn_mp_init_size.c      |   49 +++
 crypto/userspace/libtommath/bn_mp_invmod.c         |   43 +++
 crypto/userspace/libtommath/bn_mp_invmod_slow.c    |  175 +++++++++
 crypto/userspace/libtommath/bn_mp_is_square.c      |  109 ++++++
 crypto/userspace/libtommath/bn_mp_jacobi.c         |  105 +++++
 crypto/userspace/libtommath/bn_mp_karatsuba_mul.c  |  167 ++++++++
 crypto/userspace/libtommath/bn_mp_karatsuba_sqr.c  |  121 ++++++
 crypto/userspace/libtommath/bn_mp_lcm.c            |   60 +++
 crypto/userspace/libtommath/bn_mp_lshd.c           |   67 ++++
 crypto/userspace/libtommath/bn_mp_mod.c            |   48 +++
 crypto/userspace/libtommath/bn_mp_mod_2d.c         |   55 +++
 crypto/userspace/libtommath/bn_mp_mod_d.c          |   27 ++
 .../bn_mp_montgomery_calc_normalization.c          |   59 +++
 .../userspace/libtommath/bn_mp_montgomery_reduce.c |  118 ++++++
 .../userspace/libtommath/bn_mp_montgomery_setup.c  |   59 +++
 crypto/userspace/libtommath/bn_mp_mul.c            |   66 ++++
 crypto/userspace/libtommath/bn_mp_mul_2.c          |   82 ++++
 crypto/userspace/libtommath/bn_mp_mul_2d.c         |   85 +++++
 crypto/userspace/libtommath/bn_mp_mul_d.c          |   79 ++++
 crypto/userspace/libtommath/bn_mp_mulmod.c         |   40 ++
 crypto/userspace/libtommath/bn_mp_n_root.c         |  132 +++++++
 crypto/userspace/libtommath/bn_mp_neg.c            |   40 ++
 crypto/userspace/libtommath/bn_mp_or.c             |   50 +++
 crypto/userspace/libtommath/bn_mp_prime_fermat.c   |   62 +++
 .../libtommath/bn_mp_prime_is_divisible.c          |   50 +++
 crypto/userspace/libtommath/bn_mp_prime_is_prime.c |   83 ++++
 .../libtommath/bn_mp_prime_miller_rabin.c          |  103 +++++
 .../userspace/libtommath/bn_mp_prime_next_prime.c  |  170 +++++++++
 .../libtommath/bn_mp_prime_rabin_miller_trials.c   |   52 +++
 .../userspace/libtommath/bn_mp_prime_random_ex.c   |  126 ++++++
 crypto/userspace/libtommath/bn_mp_radix_size.c     |   78 ++++
 crypto/userspace/libtommath/bn_mp_radix_smap.c     |   24 ++
 crypto/userspace/libtommath/bn_mp_rand.c           |   55 +++
 crypto/userspace/libtommath/bn_mp_read_radix.c     |   85 +++++
 .../userspace/libtommath/bn_mp_read_signed_bin.c   |   41 ++
 .../userspace/libtommath/bn_mp_read_unsigned_bin.c |   55 +++
 crypto/userspace/libtommath/bn_mp_reduce.c         |  100 +++++
 crypto/userspace/libtommath/bn_mp_reduce_2k.c      |   61 +++
 crypto/userspace/libtommath/bn_mp_reduce_2k_l.c    |   62 +++
 .../userspace/libtommath/bn_mp_reduce_2k_setup.c   |   47 +++
 .../userspace/libtommath/bn_mp_reduce_2k_setup_l.c |   44 +++
 crypto/userspace/libtommath/bn_mp_reduce_is_2k.c   |   52 +++
 crypto/userspace/libtommath/bn_mp_reduce_is_2k_l.c |   44 +++
 crypto/userspace/libtommath/bn_mp_reduce_setup.c   |   34 ++
 crypto/userspace/libtommath/bn_mp_rshd.c           |   72 ++++
 crypto/userspace/libtommath/bn_mp_set.c            |   29 ++
 crypto/userspace/libtommath/bn_mp_set_int.c        |   48 +++
 crypto/userspace/libtommath/bn_mp_shrink.c         |   36 ++
 .../userspace/libtommath/bn_mp_signed_bin_size.c   |   27 ++
 crypto/userspace/libtommath/bn_mp_sqr.c            |   58 +++
 crypto/userspace/libtommath/bn_mp_sqrmod.c         |   41 ++
 crypto/userspace/libtommath/bn_mp_sqrt.c           |   81 ++++
 crypto/userspace/libtommath/bn_mp_sub.c            |   59 +++
 crypto/userspace/libtommath/bn_mp_sub_d.c          |   93 +++++
 crypto/userspace/libtommath/bn_mp_submod.c         |   42 ++
 crypto/userspace/libtommath/bn_mp_to_signed_bin.c  |   33 ++
 .../userspace/libtommath/bn_mp_to_signed_bin_n.c   |   31 ++
 .../userspace/libtommath/bn_mp_to_unsigned_bin.c   |   48 +++
 .../userspace/libtommath/bn_mp_to_unsigned_bin_n.c |   31 ++
 crypto/userspace/libtommath/bn_mp_toom_mul.c       |  284 ++++++++++++++
 crypto/userspace/libtommath/bn_mp_toom_sqr.c       |  226 +++++++++++
 crypto/userspace/libtommath/bn_mp_toradix.c        |   75 ++++
 crypto/userspace/libtommath/bn_mp_toradix_n.c      |   88 +++++
 .../userspace/libtommath/bn_mp_unsigned_bin_size.c |   28 ++
 crypto/userspace/libtommath/bn_mp_xor.c            |   51 +++
 crypto/userspace/libtommath/bn_mp_zero.c           |   36 ++
 crypto/userspace/libtommath/bn_prime_tab.c         |   61 +++
 crypto/userspace/libtommath/bn_reverse.c           |   39 ++
 crypto/userspace/libtommath/bn_s_mp_add.c          |  109 ++++++
 crypto/userspace/libtommath/bn_s_mp_exptmod.c      |  252 +++++++++++++
 crypto/userspace/libtommath/bn_s_mp_mul_digs.c     |   90 +++++
 .../userspace/libtommath/bn_s_mp_mul_high_digs.c   |   81 ++++
 crypto/userspace/libtommath/bn_s_mp_sqr.c          |   84 ++++
 crypto/userspace/libtommath/bn_s_mp_sub.c          |   89 +++++
 crypto/userspace/libtommath/bncore.c               |   36 ++
 crypto/userspace/libtommath/changes.txt            |  397 ++++++++++++++++++++
 crypto/userspace/libtommath/pretty.build           |   66 ++++
 121 files changed, 9425 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/libtommath/LICENSE
 create mode 100644 crypto/userspace/libtommath/bn_error.c
 create mode 100644 crypto/userspace/libtommath/bn_fast_mp_invmod.c
 create mode 100644 crypto/userspace/libtommath/bn_fast_mp_montgomery_reduce.c
 create mode 100644 crypto/userspace/libtommath/bn_fast_s_mp_mul_digs.c
 create mode 100644 crypto/userspace/libtommath/bn_fast_s_mp_mul_high_digs.c
 create mode 100644 crypto/userspace/libtommath/bn_fast_s_mp_sqr.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_2expt.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_abs.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_add.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_add_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_addmod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_and.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_clamp.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_clear.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_clear_multi.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_cmp.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_cmp_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_cmp_mag.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_cnt_lsb.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_copy.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_count_bits.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_div.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_div_2.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_div_2d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_div_3.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_div_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_dr_is_modulus.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_dr_reduce.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_dr_setup.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_exch.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_expt_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_exptmod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_exptmod_fast.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_exteuclid.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_gcd.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_get_int.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_grow.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init_copy.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init_multi.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init_set.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init_set_int.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_init_size.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_invmod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_invmod_slow.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_is_square.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_jacobi.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_karatsuba_mul.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_karatsuba_sqr.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_lcm.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_lshd.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mod_2d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mod_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_montgomery_calc_normalization.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_montgomery_reduce.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_montgomery_setup.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mul.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mul_2.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mul_2d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mul_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_mulmod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_n_root.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_neg.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_or.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_fermat.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_is_divisible.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_is_prime.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_miller_rabin.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_next_prime.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_rabin_miller_trials.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_prime_random_ex.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_radix_size.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_radix_smap.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_rand.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_read_radix.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_read_signed_bin.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_read_unsigned_bin.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_2k.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_2k_l.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_2k_setup.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_2k_setup_l.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_is_2k.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_is_2k_l.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_reduce_setup.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_rshd.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_set.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_set_int.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_shrink.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_signed_bin_size.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_sqr.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_sqrmod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_sqrt.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_sub.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_sub_d.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_submod.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_to_signed_bin.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_to_signed_bin_n.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_to_unsigned_bin.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_to_unsigned_bin_n.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_toom_mul.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_toom_sqr.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_toradix.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_toradix_n.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_unsigned_bin_size.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_xor.c
 create mode 100644 crypto/userspace/libtommath/bn_mp_zero.c
 create mode 100644 crypto/userspace/libtommath/bn_prime_tab.c
 create mode 100644 crypto/userspace/libtommath/bn_reverse.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_add.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_exptmod.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_mul_digs.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_mul_high_digs.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_sqr.c
 create mode 100644 crypto/userspace/libtommath/bn_s_mp_sub.c
 create mode 100644 crypto/userspace/libtommath/bncore.c
 create mode 100644 crypto/userspace/libtommath/changes.txt
 create mode 100644 crypto/userspace/libtommath/pretty.build
crypto/userspace/Makefile +30 -1
index 3dfbc39..60a9b31 100644
@@ -1,6 +1,35 @@
 ccflags-y += -I$(src)/libtommath -I$(src)/libtomcrypt/headers -I$(src) -DLTC_SOURCE
 
-cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o utils.o
+TOMMATH_OBJECTS = libtommath/bncore.o libtommath/bn_mp_init.o libtommath/bn_mp_clear.o libtommath/bn_mp_exch.o libtommath/bn_mp_grow.o libtommath/bn_mp_shrink.o \
+	libtommath/bn_mp_clamp.o libtommath/bn_mp_zero.o  libtommath/bn_mp_set.o libtommath/bn_mp_set_int.o libtommath/bn_mp_init_size.o libtommath/bn_mp_copy.o \
+	libtommath/bn_mp_init_copy.o libtommath/bn_mp_abs.o libtommath/bn_mp_neg.o libtommath/bn_mp_cmp_mag.o libtommath/bn_mp_cmp.o libtommath/bn_mp_cmp_d.o \
+	libtommath/bn_mp_rshd.o libtommath/bn_mp_lshd.o libtommath/bn_mp_mod_2d.o libtommath/bn_mp_div_2d.o libtommath/bn_mp_mul_2d.o libtommath/bn_mp_div_2.o \
+	libtommath/bn_mp_mul_2.o libtommath/bn_s_mp_add.o libtommath/bn_s_mp_sub.o libtommath/bn_fast_s_mp_mul_digs.o libtommath/bn_s_mp_mul_digs.o \
+	libtommath/bn_fast_s_mp_mul_high_digs.o libtommath/bn_s_mp_mul_high_digs.o libtommath/bn_fast_s_mp_sqr.o libtommath/bn_s_mp_sqr.o \
+	libtommath/bn_mp_add.o libtommath/bn_mp_sub.o libtommath/bn_mp_karatsuba_mul.o libtommath/bn_mp_mul.o libtommath/bn_mp_karatsuba_sqr.o \
+	libtommath/bn_mp_sqr.o libtommath/bn_mp_div.o libtommath/bn_mp_mod.o libtommath/bn_mp_add_d.o libtommath/bn_mp_sub_d.o libtommath/bn_mp_mul_d.o \
+	libtommath/bn_mp_div_d.o libtommath/bn_mp_mod_d.o libtommath/bn_mp_expt_d.o libtommath/bn_mp_addmod.o libtommath/bn_mp_submod.o \
+	libtommath/bn_mp_mulmod.o libtommath/bn_mp_sqrmod.o libtommath/bn_mp_gcd.o libtommath/bn_mp_lcm.o libtommath/bn_fast_mp_invmod.o libtommath/bn_mp_invmod.o \
+	libtommath/bn_mp_reduce.o libtommath/bn_mp_montgomery_setup.o libtommath/bn_fast_mp_montgomery_reduce.o libtommath/bn_mp_montgomery_reduce.o \
+	libtommath/bn_mp_exptmod_fast.o libtommath/bn_mp_exptmod.o libtommath/bn_mp_2expt.o libtommath/bn_mp_n_root.o libtommath/bn_mp_jacobi.o libtommath/bn_reverse.o \
+	libtommath/bn_mp_count_bits.o libtommath/bn_mp_read_unsigned_bin.o libtommath/bn_mp_read_signed_bin.o libtommath/bn_mp_to_unsigned_bin.o \
+	libtommath/bn_mp_to_signed_bin.o libtommath/bn_mp_unsigned_bin_size.o libtommath/bn_mp_signed_bin_size.o  \
+	libtommath/bn_mp_xor.o libtommath/bn_mp_and.o libtommath/bn_mp_or.o libtommath/bn_mp_rand.o libtommath/bn_mp_montgomery_calc_normalization.o \
+	libtommath/bn_mp_prime_is_divisible.o libtommath/bn_prime_tab.o libtommath/bn_mp_prime_fermat.o libtommath/bn_mp_prime_miller_rabin.o \
+	libtommath/bn_mp_prime_is_prime.o libtommath/bn_mp_prime_next_prime.o libtommath/bn_mp_dr_reduce.o \
+	libtommath/bn_mp_dr_is_modulus.o libtommath/bn_mp_dr_setup.o libtommath/bn_mp_reduce_setup.o \
+	libtommath/bn_mp_toom_mul.o libtommath/bn_mp_toom_sqr.o libtommath/bn_mp_div_3.o libtommath/bn_s_mp_exptmod.o \
+	libtommath/bn_mp_reduce_2k.o libtommath/bn_mp_reduce_is_2k.o libtommath/bn_mp_reduce_2k_setup.o \
+	libtommath/bn_mp_reduce_2k_l.o libtommath/bn_mp_reduce_is_2k_l.o libtommath/bn_mp_reduce_2k_setup_l.o \
+	libtommath/bn_mp_radix_smap.o libtommath/bn_mp_read_radix.o libtommath/bn_mp_toradix.o libtommath/bn_mp_radix_size.o \
+	libtommath/bn_mp_cnt_lsb.o libtommath/bn_error.o \
+	libtommath/bn_mp_init_multi.o libtommath/bn_mp_clear_multi.o libtommath/bn_mp_exteuclid.o libtommath/bn_mp_toradix_n.o \
+	libtommath/bn_mp_prime_random_ex.o libtommath/bn_mp_get_int.o libtommath/bn_mp_sqrt.o libtommath/bn_mp_is_square.o libtommath/bn_mp_init_set.o \
+	libtommath/bn_mp_init_set_int.o libtommath/bn_mp_invmod_slow.o libtommath/bn_mp_prime_rabin_miller_trials.o \
+	libtommath/bn_mp_to_signed_bin_n.o libtommath/bn_mp_to_unsigned_bin_n.o
+
+cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
+	utils.o $(TOMMATH_OBJECTS)
 
 
 obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/libtommath/LICENSE new file +4 -0
new file mode 100644
index 0000000..5baa792
@@ -0,0 +1,4 @@
+LibTomMath is hereby released into the Public Domain.  
+
+-- Tom St Denis
+
crypto/userspace/libtommath/bn_error.c new file +47 -0

47 lines not on this page · raw message

crypto/userspace/libtommath/bn_fast_mp_invmod.c new file +148 -0

148 lines not on this page · raw message

crypto/userspace/libtommath/bn_fast_mp_montgomery_reduce.c new file +172 -0

172 lines not on this page · raw message

crypto/userspace/libtommath/bn_fast_s_mp_mul_digs.c new file +107 -0

107 lines not on this page · raw message

crypto/userspace/libtommath/bn_fast_s_mp_mul_high_digs.c new file +98 -0

98 lines not on this page · raw message

crypto/userspace/libtommath/bn_fast_s_mp_sqr.c new file +114 -0

114 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_2expt.c new file +48 -0

48 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_abs.c new file +43 -0

43 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_add.c new file +53 -0

53 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_add_d.c new file +112 -0

112 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_addmod.c new file +41 -0

41 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_and.c new file +57 -0

57 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_clamp.c new file +44 -0

44 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_clear.c new file +45 -0

45 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_clear_multi.c new file +34 -0
new file mode 100644
index 0000000..daaea79
@@ -0,0 +1,34 @@
+#include <tommath.h>
+#ifdef BN_MP_CLEAR_MULTI_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include <stdarg.h>
+
+void mp_clear_multi(mp_int *mp, ...) 
+{
+    mp_int* next_mp = mp;
+    va_list args;
+    va_start(args, mp);
+    while (next_mp != NULL) {
+        mp_clear(next_mp);
+        next_mp = va_arg(args, mp_int*);
+    }
+    va_end(args);
+}
+#endif
+
+/* $Source: /cvs/libtom/libtommath/bn_mp_clear_multi.c,v $ */
+/* $Revision: 1.4 $ */
+/* $Date: 2006/12/28 01:25:13 $ */
crypto/userspace/libtommath/bn_mp_cmp.c new file +43 -0

43 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_cmp_d.c new file +44 -0

44 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_cmp_mag.c new file +55 -0

55 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_cnt_lsb.c new file +53 -0

53 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_copy.c new file +68 -0

68 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_count_bits.c new file +45 -0

45 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_div.c new file +292 -0

292 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_div_2.c new file +68 -0

68 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_div_2d.c new file +97 -0

97 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_div_3.c new file +79 -0

79 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_div_d.c new file +115 -0

115 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_dr_is_modulus.c new file +43 -0

43 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_dr_reduce.c new file +94 -0

94 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_dr_setup.c new file +32 -0
new file mode 100644
index 0000000..1d7d856
@@ -0,0 +1,32 @@
+#include <tommath.h>
+#ifdef BN_MP_DR_SETUP_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+
+/* determines the setup value */
+void mp_dr_setup(mp_int *a, mp_digit *d)
+{
+   /* the casts are required if DIGIT_BIT is one less than
+    * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
+    */
+   *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - 
+        ((mp_word)a->dp[0]));
+}
+
+#endif
+
+/* $Source: /cvs/libtom/libtommath/bn_mp_dr_setup.c,v $ */
+/* $Revision: 1.4 $ */
+/* $Date: 2006/12/28 01:25:13 $ */
crypto/userspace/libtommath/bn_mp_exch.c new file +34 -0
new file mode 100644
index 0000000..38574e0
@@ -0,0 +1,34 @@
+#include <tommath.h>
+#ifdef BN_MP_EXCH_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis
+ *
+ * LibTomMath is a library that provides multiple-precision
+ * integer arithmetic as well as number theoretic functionality.
+ *
+ * The library was designed directly after the MPI library by
+ * Michael Fromberger but has been written from scratch with
+ * additional optimizations in place.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+
+/* swap the elements of two integers, for cases where you can't simply swap the 
+ * mp_int pointers around
+ */
+void
+mp_exch (mp_int * a, mp_int * b)
+{
+  mp_int  t;
+
+  t  = *a;
+  *a = *b;
+  *b = t;
+}
+#endif
+
+/* $Source: /cvs/libtom/libtommath/bn_mp_exch.c,v $ */
+/* $Revision: 1.4 $ */
+/* $Date: 2006/12/28 01:25:13 $ */
crypto/userspace/libtommath/bn_mp_expt_d.c new file +57 -0

57 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_exptmod.c new file +112 -0

112 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_exptmod_fast.c new file +321 -0

321 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_exteuclid.c new file +82 -0

82 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_gcd.c new file +105 -0

63 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_get_int.c new file +45 -0

45 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_grow.c new file +58 -0

58 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_init.c new file +47 -0

47 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_init_copy.c new file +32 -0
new file mode 100644
index 0000000..0160811

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_init_multi.c new file +59 -0

59 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_init_set.c new file +32 -0
new file mode 100644
index 0000000..34edad9

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_init_set_int.c new file +31 -0
new file mode 100644
index 0000000..5c55993

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_init_size.c new file +49 -0

49 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_invmod.c new file +43 -0

43 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_invmod_slow.c new file +175 -0

175 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_is_square.c new file +109 -0

109 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_jacobi.c new file +105 -0

105 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_karatsuba_mul.c new file +167 -0

167 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_karatsuba_sqr.c new file +121 -0

121 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_lcm.c new file +60 -0

60 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_lshd.c new file +67 -0

67 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mod.c new file +48 -0

48 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mod_2d.c new file +55 -0

55 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mod_d.c new file +27 -0
new file mode 100644
index 0000000..9ca37e6

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_montgomery_calc_normalization.c new file +59 -0

59 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_montgomery_reduce.c new file +118 -0

118 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_montgomery_setup.c new file +59 -0

59 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mul.c new file +66 -0

66 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mul_2.c new file +82 -0

82 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mul_2d.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mul_d.c new file +79 -0

79 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_mulmod.c new file +40 -0
new file mode 100644
index 0000000..003ceb9

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_n_root.c new file +132 -0

132 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_neg.c new file +40 -0
new file mode 100644
index 0000000..a7d035a

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_or.c new file +50 -0

50 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_fermat.c new file +62 -0

62 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_is_divisible.c new file +50 -0

50 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_is_prime.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_miller_rabin.c new file +103 -0

103 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_next_prime.c new file +170 -0

170 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_rabin_miller_trials.c new file +52 -0

52 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_prime_random_ex.c new file +126 -0

126 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_radix_size.c new file +78 -0

78 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_radix_smap.c new file +24 -0
new file mode 100644
index 0000000..7d72feb

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_rand.c new file +55 -0

55 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_read_radix.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_read_signed_bin.c new file +41 -0

41 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_read_unsigned_bin.c new file +55 -0

55 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce.c new file +100 -0

100 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_2k.c new file +61 -0

61 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_2k_l.c new file +62 -0

62 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_2k_setup.c new file +47 -0

47 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_2k_setup_l.c new file +44 -0

44 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_is_2k.c new file +52 -0

52 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_is_2k_l.c new file +44 -0

44 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_reduce_setup.c new file +34 -0
new file mode 100644
index 0000000..370f20b

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_rshd.c new file +72 -0

72 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_set.c new file +29 -0
new file mode 100644
index 0000000..174adcb

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_set_int.c new file +48 -0

48 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_shrink.c new file +36 -0
new file mode 100644
index 0000000..3e38556

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_signed_bin_size.c new file +27 -0
new file mode 100644
index 0000000..6739d19

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_sqr.c new file +58 -0

58 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_sqrmod.c new file +41 -0

41 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_sqrt.c new file +81 -0

81 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_sub.c new file +59 -0

59 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_sub_d.c new file +93 -0

93 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_submod.c new file +42 -0

42 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_to_signed_bin.c new file +33 -0
new file mode 100644
index 0000000..9df83ca

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_to_signed_bin_n.c new file +31 -0
new file mode 100644
index 0000000..677f827

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_to_unsigned_bin.c new file +48 -0

48 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_to_unsigned_bin_n.c new file +31 -0
new file mode 100644
index 0000000..0dc00c6

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_toom_mul.c new file +284 -0

284 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_toom_sqr.c new file +226 -0

226 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_toradix.c new file +75 -0

75 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_toradix_n.c new file +88 -0

88 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_unsigned_bin_size.c new file +28 -0
new file mode 100644
index 0000000..6dc3bd5

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_mp_xor.c new file +51 -0

51 lines not on this page · raw message

crypto/userspace/libtommath/bn_mp_zero.c new file +36 -0
new file mode 100644
index 0000000..b0977d4

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_prime_tab.c new file +61 -0

61 lines not on this page · raw message

crypto/userspace/libtommath/bn_reverse.c new file +39 -0
new file mode 100644
index 0000000..ddfa827

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/bn_s_mp_add.c new file +109 -0

109 lines not on this page · raw message

crypto/userspace/libtommath/bn_s_mp_exptmod.c new file +252 -0

252 lines not on this page · raw message

crypto/userspace/libtommath/bn_s_mp_mul_digs.c new file +90 -0

90 lines not on this page · raw message

crypto/userspace/libtommath/bn_s_mp_mul_high_digs.c new file +81 -0

81 lines not on this page · raw message

crypto/userspace/libtommath/bn_s_mp_sqr.c new file +84 -0

84 lines not on this page · raw message

crypto/userspace/libtommath/bn_s_mp_sub.c new file +89 -0

89 lines not on this page · raw message

crypto/userspace/libtommath/bncore.c new file +36 -0
new file mode 100644
index 0000000..8fb1824

Lines of this file are not shown. View the raw message.

crypto/userspace/libtommath/changes.txt new file +397 -0

397 lines not on this page · raw message

crypto/userspace/libtommath/pretty.build new file +66 -0

66 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.2.1

Part of the diff is not shown here. View the raw message.

#23 lore raw

PATCH 10/19 Add libtomcrypt implementation

In reply to Miloslav Trmač

No use postponing it any more I'm afraid.

(Reviewing this in detail is probably premature, we are considering replacing the implementation by something based on libgcrypt, which is more actively maintained and has been probably more thorouhgly examined for vulnerabilities.)

---
 crypto/userspace/Makefile                          |   38 ++-
 .../libtomcrypt/hashes/crypt_hash_is_valid.c       |   30 ++
 crypto/userspace/libtomcrypt/hashes/hash_get_oid.c |   78 ++++
 crypto/userspace/libtomcrypt/hashes/hash_memory.c  |   66 ++++
 .../libtomcrypt/hashes/hash_memory_multi.c         |   84 +++++
 .../userspace/libtomcrypt/headers/tomcrypt_prng.h  |   80 ++++
 crypto/userspace/libtomcrypt/math/rand_prime.c     |   80 ++++
 .../libtomcrypt/misc/crypt/crypt_argchk.c          |   28 ++
 crypto/userspace/libtomcrypt/misc/pk_get_oid.c     |   40 ++
 crypto/userspace/libtomcrypt/misc/qsort.c          |  247 ++++++++++++
 crypto/userspace/libtomcrypt/misc/zeromem.c        |   34 ++
 .../pk/asn1/der/bit/der_decode_bit_string.c        |  106 ++++++
 .../pk/asn1/der/bit/der_encode_bit_string.c        |   92 +++++
 .../pk/asn1/der/bit/der_length_bit_string.c        |   54 +++
 .../pk/asn1/der/boolean/der_decode_boolean.c       |   47 +++
 .../pk/asn1/der/boolean/der_encode_boolean.c       |   51 +++
 .../pk/asn1/der/boolean/der_length_boolean.c       |   35 ++
 .../pk/asn1/der/choice/der_decode_choice.c         |  182 +++++++++
 .../pk/asn1/der/ia5/der_decode_ia5_string.c        |   96 +++++
 .../pk/asn1/der/ia5/der_encode_ia5_string.c        |   85 +++++
 .../pk/asn1/der/ia5/der_length_ia5_string.c        |  194 ++++++++++
 .../pk/asn1/der/integer/der_decode_integer.c       |  110 ++++++
 .../pk/asn1/der/integer/der_encode_integer.c       |  130 +++++++
 .../pk/asn1/der/integer/der_length_integer.c       |   82 ++++
 .../der_decode_object_identifier.c                 |   99 +++++
 .../der_encode_object_identifier.c                 |  111 ++++++
 .../der_length_object_identifier.c                 |   89 +++++
 .../pk/asn1/der/octet/der_decode_octet_string.c    |   91 +++++
 .../pk/asn1/der/octet/der_encode_octet_string.c    |   86 +++++
 .../pk/asn1/der/octet/der_length_octet_string.c    |   53 +++
 .../printable_string/der_decode_printable_string.c |   96 +++++
 .../printable_string/der_encode_printable_string.c |   85 +++++
 .../printable_string/der_length_printable_string.c |  166 ++++++++
 .../pk/asn1/der/sequence/der_decode_sequence_ex.c  |  287 ++++++++++++++
 .../asn1/der/sequence/der_decode_sequence_flexi.c  |  394 ++++++++++++++++++++
 .../asn1/der/sequence/der_decode_sequence_multi.c  |  140 +++++++
 .../pk/asn1/der/sequence/der_encode_sequence_ex.c  |  335 +++++++++++++++++
 .../asn1/der/sequence/der_encode_sequence_multi.c  |  139 +++++++
 .../pk/asn1/der/sequence/der_length_sequence.c     |  169 +++++++++
 .../pk/asn1/der/sequence/der_sequence_free.c       |   66 ++++
 .../libtomcrypt/pk/asn1/der/set/der_encode_set.c   |  104 +++++
 .../libtomcrypt/pk/asn1/der/set/der_encode_setof.c |  163 ++++++++
 .../der/short_integer/der_decode_short_integer.c   |   68 ++++
 .../der/short_integer/der_encode_short_integer.c   |   97 +++++
 .../der/short_integer/der_length_short_integer.c   |   70 ++++
 .../pk/asn1/der/utctime/der_decode_utctime.c       |  127 +++++++
 .../pk/asn1/der/utctime/der_encode_utctime.c       |   83 ++++
 .../pk/asn1/der/utctime/der_length_utctime.c       |   46 +++
 .../pk/asn1/der/utf8/der_decode_utf8_string.c      |  111 ++++++
 .../pk/asn1/der/utf8/der_encode_utf8_string.c      |  105 ++++++
 .../pk/asn1/der/utf8/der_length_utf8_string.c      |   83 ++++
 .../der/x509/der_decode_subject_public_key_info.c  |  112 ++++++
 .../der/x509/der_encode_subject_public_key_info.c  |   83 ++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c   |   99 +++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c     |   34 ++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c   |  101 +++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c |  125 ++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c   |  147 ++++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c |  126 +++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_verify_key.c  |  100 +++++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c  |   51 +++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c   |   91 +++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c      |  192 ++++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c      |  164 ++++++++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c  |   36 ++
 .../libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c       |  168 +++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c       |  157 ++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c      |  110 ++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c      |   95 +++++
 .../userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c |  107 ++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c |   96 +++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c   |   87 +++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c  |  147 ++++++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c     |   34 ++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c   |  128 +++++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c |  105 ++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c   |  130 +++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c |  170 +++++++++
 78 files changed, 8526 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_get_oid.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_memory.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_prng.h
 create mode 100644 crypto/userspace/libtomcrypt/math/rand_prime.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/crypt/crypt_argchk.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/pk_get_oid.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/qsort.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/zeromem.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_length_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_set.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_setof.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c
crypto/userspace/Makefile +37 -1
index 60a9b31..81e4122 100644
@@ -28,8 +28,44 @@ TOMMATH_OBJECTS = libtommath/bncore.o libtommath/bn_mp_init.o libtommath/bn_mp_c
 	libtommath/bn_mp_init_set_int.o libtommath/bn_mp_invmod_slow.o libtommath/bn_mp_prime_rabin_miller_trials.o \
 	libtommath/bn_mp_to_signed_bin_n.o libtommath/bn_mp_to_unsigned_bin_n.o
 
+TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argchk.o \
+	libtomcrypt/math/rand_prime.o  libtomcrypt/misc/qsort.o libtomcrypt/hashes/hash_get_oid.o \
+	libtomcrypt/hashes/crypt_hash_is_valid.o libtomcrypt/hashes/hash_memory.o libtomcrypt/hashes/hash_memory_multi.o \
+	libtomcrypt/pk/dsa/dsa_make_key.o libtomcrypt/pk/dsa/dsa_export.o libtomcrypt/pk/dsa/dsa_import.o \
+	libtomcrypt/pk/dsa/dsa_free.o libtomcrypt/pk/dsa/dsa_sign_hash.o libtomcrypt/pk/dsa/dsa_verify_hash.o \
+	libtomcrypt/pk/dsa/dsa_verify_key.o \
+	libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.o libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.o \
+	libtomcrypt/pk/asn1/der/bit/der_length_bit_string.o libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.o \
+	libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.o libtomcrypt/pk/asn1/der/boolean/der_length_boolean.o \
+	libtomcrypt/pk/asn1/der/choice/der_decode_choice.o libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.o \
+	libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.o libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.o \
+	libtomcrypt/pk/asn1/der/integer/der_decode_integer.o libtomcrypt/pk/asn1/der/integer/der_encode_integer.o \
+	libtomcrypt/pk/asn1/der/integer/der_length_integer.o libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.o \
+	libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.o libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.o \
+	libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.o libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.o \
+	libtomcrypt/pk/asn1/der/octet/der_length_octet_string.o libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.o \
+	libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.o libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.o \
+	libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.o libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.o \
+	libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.o libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.o \
+	libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.o libtomcrypt/pk/asn1/der/sequence/der_length_sequence.o \
+	libtomcrypt/pk/asn1/der/sequence/der_sequence_free.o libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.o \
+	libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.o libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.o \
+	libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.o libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.o \
+	libtomcrypt/pk/asn1/der/utctime/der_length_utctime.o libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.o \
+	libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.o libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.o \
+	libtomcrypt/pk/asn1/der/set/der_encode_set.o  libtomcrypt/pk/asn1/der/set/der_encode_setof.o \
+	libtomcrypt/pk/rsa/rsa_decrypt_key.o libtomcrypt/pk/rsa/rsa_encrypt_key.o libtomcrypt/pk/rsa/rsa_export.o \
+	libtomcrypt/pk/rsa/rsa_exptmod.o libtomcrypt/pk/rsa/rsa_free.o libtomcrypt/pk/rsa/rsa_import.o \
+	libtomcrypt/pk/rsa/rsa_make_key.o libtomcrypt/pk/rsa/rsa_sign_hash.o libtomcrypt/pk/rsa/rsa_verify_hash.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_i2osp.o libtomcrypt/pk/pkcs1/pkcs_1_mgf1.o 	libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.o libtomcrypt/pk/pkcs1/pkcs_1_os2ip.o libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.o libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.o libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.o \
+	libtomcrypt/misc/pk_get_oid.o libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.o \
+	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
+
 cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
-	utils.o $(TOMMATH_OBJECTS)
+	utils.o $(TOMMATH_OBJECTS) \
+	$(TOMCRYPT_OBJECTS)
 
 
 obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c new file +30 -0
new file mode 100644
index 0000000..d01d418
@@ -0,0 +1,30 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_hash_is_valid.c
+  Determine if hash is valid, Tom St Denis
+*/  
+
+/*
+   Test if a hash index is valid
+   @param idx   The hash to search for
+   @return CRYPT_OK if valid
+*/
+int hash_is_valid(const struct algo_properties_st *hash)
+{
+   return CRYPT_OK;
+}
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c,v $ */
+/* $Revision: 1.6 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/hashes/hash_get_oid.c new file +78 -0

78 lines not on this page · raw message

crypto/userspace/libtomcrypt/hashes/hash_memory.c new file +66 -0

66 lines not on this page · raw message

crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c new file +84 -0

84 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_prng.h new file +80 -0

80 lines not on this page · raw message

crypto/userspace/libtomcrypt/math/rand_prime.c new file +80 -0

80 lines not on this page · raw message

crypto/userspace/libtomcrypt/misc/crypt/crypt_argchk.c new file +28 -0
new file mode 100644
index 0000000..143b784
@@ -0,0 +1,28 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_argchk.c
+  Perform argument checking, Tom St Denis
+*/  
+
+#if (ARGTYPE == 0)
+void crypt_argchk(char *v, char *s, int d)
+{
+ printk("LTC_ARGCHK '%s' failure on line %d of file %s\n",
+         v, d, s);
+}
+#endif
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_argchk.c,v $ */
+/* $Revision: 1.5 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/misc/pk_get_oid.c new file +40 -0
new file mode 100644
index 0000000..197d7ae
@@ -0,0 +1,40 @@
+/* LibTomCrypt, modular cryptographic library
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ */
+#include "tomcrypt.h"
+
+static const oid_st rsa_oid = {
+   .OIDlen = 7,
+   .OID = { 1, 2, 840, 113549, 1, 1, 1  },
+};
+
+static const oid_st dsa_oid = {
+   .OIDlen = 6,
+   .OID = { 1, 2, 840, 10040, 4, 1  },
+};
+
+/*
+   Returns the OID of the public key algorithm.
+   @return CRYPT_OK if valid
+*/
+int pk_get_oid(int pk, oid_st *st)
+{
+   switch (pk) {
+      case PKA_RSA:
+         memcpy(st, &rsa_oid, sizeof(*st));
+         break;
+      case PKA_DSA:
+         memcpy(st, &dsa_oid, sizeof(*st));
+         break;
+      default:
+         return CRYPT_INVALID_ARG;
+   }
+   return CRYPT_OK;
+}
+
crypto/userspace/libtomcrypt/misc/qsort.c new file +247 -0

247 lines not on this page · raw message

crypto/userspace/libtomcrypt/misc/zeromem.c new file +34 -0
new file mode 100644
index 0000000..a4bb124
@@ -0,0 +1,34 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+   @file zeromem.c
+   Zero a block of memory, Tom St Denis
+*/
+
+/**
+   Zero a block of memory
+   @param out    The destination of the area to zero
+   @param outlen The length of the area to zero (octets)
+*/
+void zeromem(void *out, size_t outlen)
+{
+   unsigned char *mem = out;
+   LTC_ARGCHKVD(out != NULL);
+   while (outlen-- > 0) {
+      *mem++ = 0;
+   }
+}
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/zeromem.c,v $ */
+/* $Revision: 1.7 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c new file +106 -0

106 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c new file +92 -0

92 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c new file +54 -0

54 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c new file +47 -0

47 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c new file +51 -0

51 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c new file +35 -0
new file mode 100644
index 0000000..aa3e03d
@@ -0,0 +1,35 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file der_length_boolean.c
+  ASN.1 DER, get length of a BOOLEAN, Tom St Denis
+*/
+
+#ifdef LTC_DER
+/**
+  Gets length of DER encoding of a BOOLEAN 
+  @param outlen [out] The length of the DER encoding
+  @return CRYPT_OK if successful
+*/
+int der_length_boolean(unsigned long *outlen)
+{
+   LTC_ARGCHK(outlen != NULL);
+   *outlen = 3;
+   return CRYPT_OK;
+}
+
+#endif
+
+/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/boolean/der_length_boolean.c,v $ */
+/* $Revision: 1.3 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c new file +182 -0

182 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c new file +194 -0

194 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c new file +110 -0

110 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c new file +130 -0

130 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_length_integer.c new file +82 -0

82 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c new file +99 -0

99 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c new file +111 -0

111 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c new file +89 -0

89 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c new file +91 -0

91 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c new file +86 -0

86 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c new file +53 -0

53 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c new file +166 -0

166 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c new file +287 -0

48 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c new file +394 -0

394 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c new file +140 -0

140 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c new file +335 -0

335 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c new file +139 -0

139 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c new file +169 -0

169 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c new file +66 -0

66 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_set.c new file +104 -0

104 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_setof.c new file +163 -0

163 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c new file +68 -0

68 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c new file +97 -0

97 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c new file +70 -0

70 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c new file +127 -0

127 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c new file +46 -0

46 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c new file +111 -0

111 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c new file +105 -0

105 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.c new file +112 -0

112 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c new file +99 -0

99 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c new file +34 -0
new file mode 100644
index 0000000..37a330d

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c new file +101 -0

101 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c new file +125 -0

125 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c new file +147 -0

147 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c new file +126 -0

126 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_key.c new file +100 -0

100 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c new file +51 -0

51 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c new file +91 -0

91 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c new file +192 -0

192 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c new file +164 -0

164 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c new file +36 -0
new file mode 100644
index 0000000..513abb6

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c new file +168 -0

168 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c new file +157 -0

157 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c new file +110 -0

110 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c new file +95 -0

95 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c new file +107 -0

107 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c new file +87 -0

87 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c new file +147 -0

147 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c new file +34 -0
new file mode 100644
index 0000000..d38b266

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c new file +128 -0

128 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c new file +105 -0

105 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c new file +130 -0

130 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c new file +170 -0

170 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.2.1

Part of the diff is not shown here. View the raw message.

#24 lore raw

PATCH 10/19 Add libtommath implementation

In reply to Miloslav Trmač

No use postponing it any more I'm afraid.

(Reviewing this in detail is probably premature, we are considering replacing the implementation by something based on libgcrypt, which is more actively maintained and has been probably more thorouhgly examined for vulnerabilities.)

---
 crypto/userspace/Makefile                          |   38 ++-
 .../libtomcrypt/hashes/crypt_hash_is_valid.c       |   30 ++
 crypto/userspace/libtomcrypt/hashes/hash_get_oid.c |   78 ++++
 crypto/userspace/libtomcrypt/hashes/hash_memory.c  |   66 ++++
 .../libtomcrypt/hashes/hash_memory_multi.c         |   84 +++++
 .../userspace/libtomcrypt/headers/tomcrypt_prng.h  |   80 ++++
 crypto/userspace/libtomcrypt/math/rand_prime.c     |   80 ++++
 .../libtomcrypt/misc/crypt/crypt_argchk.c          |   28 ++
 crypto/userspace/libtomcrypt/misc/pk_get_oid.c     |   40 ++
 crypto/userspace/libtomcrypt/misc/qsort.c          |  247 ++++++++++++
 crypto/userspace/libtomcrypt/misc/zeromem.c        |   34 ++
 .../pk/asn1/der/bit/der_decode_bit_string.c        |  106 ++++++
 .../pk/asn1/der/bit/der_encode_bit_string.c        |   92 +++++
 .../pk/asn1/der/bit/der_length_bit_string.c        |   54 +++
 .../pk/asn1/der/boolean/der_decode_boolean.c       |   47 +++
 .../pk/asn1/der/boolean/der_encode_boolean.c       |   51 +++
 .../pk/asn1/der/boolean/der_length_boolean.c       |   35 ++
 .../pk/asn1/der/choice/der_decode_choice.c         |  182 +++++++++
 .../pk/asn1/der/ia5/der_decode_ia5_string.c        |   96 +++++
 .../pk/asn1/der/ia5/der_encode_ia5_string.c        |   85 +++++
 .../pk/asn1/der/ia5/der_length_ia5_string.c        |  194 ++++++++++
 .../pk/asn1/der/integer/der_decode_integer.c       |  110 ++++++
 .../pk/asn1/der/integer/der_encode_integer.c       |  130 +++++++
 .../pk/asn1/der/integer/der_length_integer.c       |   82 ++++
 .../der_decode_object_identifier.c                 |   99 +++++
 .../der_encode_object_identifier.c                 |  111 ++++++
 .../der_length_object_identifier.c                 |   89 +++++
 .../pk/asn1/der/octet/der_decode_octet_string.c    |   91 +++++
 .../pk/asn1/der/octet/der_encode_octet_string.c    |   86 +++++
 .../pk/asn1/der/octet/der_length_octet_string.c    |   53 +++
 .../printable_string/der_decode_printable_string.c |   96 +++++
 .../printable_string/der_encode_printable_string.c |   85 +++++
 .../printable_string/der_length_printable_string.c |  166 ++++++++
 .../pk/asn1/der/sequence/der_decode_sequence_ex.c  |  287 ++++++++++++++
 .../asn1/der/sequence/der_decode_sequence_flexi.c  |  394 ++++++++++++++++++++
 .../asn1/der/sequence/der_decode_sequence_multi.c  |  140 +++++++
 .../pk/asn1/der/sequence/der_encode_sequence_ex.c  |  335 +++++++++++++++++
 .../asn1/der/sequence/der_encode_sequence_multi.c  |  139 +++++++
 .../pk/asn1/der/sequence/der_length_sequence.c     |  169 +++++++++
 .../pk/asn1/der/sequence/der_sequence_free.c       |   66 ++++
 .../libtomcrypt/pk/asn1/der/set/der_encode_set.c   |  104 +++++
 .../libtomcrypt/pk/asn1/der/set/der_encode_setof.c |  163 ++++++++
 .../der/short_integer/der_decode_short_integer.c   |   68 ++++
 .../der/short_integer/der_encode_short_integer.c   |   97 +++++
 .../der/short_integer/der_length_short_integer.c   |   70 ++++
 .../pk/asn1/der/utctime/der_decode_utctime.c       |  127 +++++++
 .../pk/asn1/der/utctime/der_encode_utctime.c       |   83 ++++
 .../pk/asn1/der/utctime/der_length_utctime.c       |   46 +++
 .../pk/asn1/der/utf8/der_decode_utf8_string.c      |  111 ++++++
 .../pk/asn1/der/utf8/der_encode_utf8_string.c      |  105 ++++++
 .../pk/asn1/der/utf8/der_length_utf8_string.c      |   83 ++++
 .../der/x509/der_decode_subject_public_key_info.c  |  112 ++++++
 .../der/x509/der_encode_subject_public_key_info.c  |   83 ++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c   |   99 +++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c     |   34 ++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c   |  101 +++++
 crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c |  125 ++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c   |  147 ++++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c |  126 +++++++
 .../userspace/libtomcrypt/pk/dsa/dsa_verify_key.c  |  100 +++++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c  |   51 +++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c   |   91 +++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c      |  192 ++++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c      |  164 ++++++++
 .../userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c  |   36 ++
 .../libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c       |  168 +++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c       |  157 ++++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c      |  110 ++++++
 .../libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c      |   95 +++++
 .../userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c |  107 ++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c |   96 +++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c   |   87 +++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c  |  147 ++++++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c     |   34 ++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c   |  128 +++++++
 crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c |  105 ++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c   |  130 +++++++
 .../userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c |  170 +++++++++
 78 files changed, 8526 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_get_oid.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_memory.c
 create mode 100644 crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/headers/tomcrypt_prng.h
 create mode 100644 crypto/userspace/libtomcrypt/math/rand_prime.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/crypt/crypt_argchk.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/pk_get_oid.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/qsort.c
 create mode 100644 crypto/userspace/libtomcrypt/misc/zeromem.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_length_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_set.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_setof.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c
 create mode 100644 crypto/userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c
crypto/userspace/Makefile +37 -1
index 60a9b31..81e4122 100644
@@ -28,8 +28,44 @@ TOMMATH_OBJECTS = libtommath/bncore.o libtommath/bn_mp_init.o libtommath/bn_mp_c
 	libtommath/bn_mp_init_set_int.o libtommath/bn_mp_invmod_slow.o libtommath/bn_mp_prime_rabin_miller_trials.o \
 	libtommath/bn_mp_to_signed_bin_n.o libtommath/bn_mp_to_unsigned_bin_n.o
 
+TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argchk.o \
+	libtomcrypt/math/rand_prime.o  libtomcrypt/misc/qsort.o libtomcrypt/hashes/hash_get_oid.o \
+	libtomcrypt/hashes/crypt_hash_is_valid.o libtomcrypt/hashes/hash_memory.o libtomcrypt/hashes/hash_memory_multi.o \
+	libtomcrypt/pk/dsa/dsa_make_key.o libtomcrypt/pk/dsa/dsa_export.o libtomcrypt/pk/dsa/dsa_import.o \
+	libtomcrypt/pk/dsa/dsa_free.o libtomcrypt/pk/dsa/dsa_sign_hash.o libtomcrypt/pk/dsa/dsa_verify_hash.o \
+	libtomcrypt/pk/dsa/dsa_verify_key.o \
+	libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.o libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.o \
+	libtomcrypt/pk/asn1/der/bit/der_length_bit_string.o libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.o \
+	libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.o libtomcrypt/pk/asn1/der/boolean/der_length_boolean.o \
+	libtomcrypt/pk/asn1/der/choice/der_decode_choice.o libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.o \
+	libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.o libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.o \
+	libtomcrypt/pk/asn1/der/integer/der_decode_integer.o libtomcrypt/pk/asn1/der/integer/der_encode_integer.o \
+	libtomcrypt/pk/asn1/der/integer/der_length_integer.o libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.o \
+	libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.o libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.o \
+	libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.o libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.o \
+	libtomcrypt/pk/asn1/der/octet/der_length_octet_string.o libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.o \
+	libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.o libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.o \
+	libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.o libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.o \
+	libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.o libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.o \
+	libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.o libtomcrypt/pk/asn1/der/sequence/der_length_sequence.o \
+	libtomcrypt/pk/asn1/der/sequence/der_sequence_free.o libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.o \
+	libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.o libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.o \
+	libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.o libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.o \
+	libtomcrypt/pk/asn1/der/utctime/der_length_utctime.o libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.o \
+	libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.o libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.o \
+	libtomcrypt/pk/asn1/der/set/der_encode_set.o  libtomcrypt/pk/asn1/der/set/der_encode_setof.o \
+	libtomcrypt/pk/rsa/rsa_decrypt_key.o libtomcrypt/pk/rsa/rsa_encrypt_key.o libtomcrypt/pk/rsa/rsa_export.o \
+	libtomcrypt/pk/rsa/rsa_exptmod.o libtomcrypt/pk/rsa/rsa_free.o libtomcrypt/pk/rsa/rsa_import.o \
+	libtomcrypt/pk/rsa/rsa_make_key.o libtomcrypt/pk/rsa/rsa_sign_hash.o libtomcrypt/pk/rsa/rsa_verify_hash.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_i2osp.o libtomcrypt/pk/pkcs1/pkcs_1_mgf1.o 	libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.o libtomcrypt/pk/pkcs1/pkcs_1_os2ip.o libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.o \
+	libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.o libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.o libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.o \
+	libtomcrypt/misc/pk_get_oid.o libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.o \
+	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
+
 cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
-	utils.o $(TOMMATH_OBJECTS)
+	utils.o $(TOMMATH_OBJECTS) \
+	$(TOMCRYPT_OBJECTS)
 
 
 obj-$(CONFIG_CRYPTO_USERSPACE) += cryptodev.o
crypto/userspace/libtomcrypt/hashes/crypt_hash_is_valid.c new file +30 -0
new file mode 100644
index 0000000..d01d418
@@ -0,0 +1,30 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_hash_is_valid.c
+  Determine if hash is valid, Tom St Denis
+*/  
+
+/*
+   Test if a hash index is valid
+   @param idx   The hash to search for
+   @return CRYPT_OK if valid
+*/
+int hash_is_valid(const struct algo_properties_st *hash)
+{
+   return CRYPT_OK;
+}
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c,v $ */
+/* $Revision: 1.6 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/hashes/hash_get_oid.c new file +78 -0

78 lines not on this page · raw message

crypto/userspace/libtomcrypt/hashes/hash_memory.c new file +66 -0

66 lines not on this page · raw message

crypto/userspace/libtomcrypt/hashes/hash_memory_multi.c new file +84 -0

84 lines not on this page · raw message

crypto/userspace/libtomcrypt/headers/tomcrypt_prng.h new file +80 -0

80 lines not on this page · raw message

crypto/userspace/libtomcrypt/math/rand_prime.c new file +80 -0

80 lines not on this page · raw message

crypto/userspace/libtomcrypt/misc/crypt/crypt_argchk.c new file +28 -0
new file mode 100644
index 0000000..143b784
@@ -0,0 +1,28 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file crypt_argchk.c
+  Perform argument checking, Tom St Denis
+*/  
+
+#if (ARGTYPE == 0)
+void crypt_argchk(char *v, char *s, int d)
+{
+ printk("LTC_ARGCHK '%s' failure on line %d of file %s\n",
+         v, d, s);
+}
+#endif
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/crypt/crypt_argchk.c,v $ */
+/* $Revision: 1.5 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/misc/pk_get_oid.c new file +40 -0
new file mode 100644
index 0000000..197d7ae
@@ -0,0 +1,40 @@
+/* LibTomCrypt, modular cryptographic library
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ */
+#include "tomcrypt.h"
+
+static const oid_st rsa_oid = {
+   .OIDlen = 7,
+   .OID = { 1, 2, 840, 113549, 1, 1, 1  },
+};
+
+static const oid_st dsa_oid = {
+   .OIDlen = 6,
+   .OID = { 1, 2, 840, 10040, 4, 1  },
+};
+
+/*
+   Returns the OID of the public key algorithm.
+   @return CRYPT_OK if valid
+*/
+int pk_get_oid(int pk, oid_st *st)
+{
+   switch (pk) {
+      case PKA_RSA:
+         memcpy(st, &rsa_oid, sizeof(*st));
+         break;
+      case PKA_DSA:
+         memcpy(st, &dsa_oid, sizeof(*st));
+         break;
+      default:
+         return CRYPT_INVALID_ARG;
+   }
+   return CRYPT_OK;
+}
+
crypto/userspace/libtomcrypt/misc/qsort.c new file +247 -0

247 lines not on this page · raw message

crypto/userspace/libtomcrypt/misc/zeromem.c new file +34 -0
new file mode 100644
index 0000000..a4bb124
@@ -0,0 +1,34 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+   @file zeromem.c
+   Zero a block of memory, Tom St Denis
+*/
+
+/**
+   Zero a block of memory
+   @param out    The destination of the area to zero
+   @param outlen The length of the area to zero (octets)
+*/
+void zeromem(void *out, size_t outlen)
+{
+   unsigned char *mem = out;
+   LTC_ARGCHKVD(out != NULL);
+   while (outlen-- > 0) {
+      *mem++ = 0;
+   }
+}
+
+/* $Source: /cvs/libtom/libtomcrypt/src/misc/zeromem.c,v $ */
+/* $Revision: 1.7 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_decode_bit_string.c new file +106 -0

106 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_encode_bit_string.c new file +92 -0

92 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/bit/der_length_bit_string.c new file +54 -0

54 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_decode_boolean.c new file +47 -0

47 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_encode_boolean.c new file +51 -0

51 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/boolean/der_length_boolean.c new file +35 -0
new file mode 100644
index 0000000..aa3e03d
@@ -0,0 +1,35 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis
+ *
+ * LibTomCrypt is a library that provides various cryptographic
+ * algorithms in a highly modular and flexible manner.
+ *
+ * The library is free for all purposes without any express
+ * guarantee it works.
+ *
+ * Tom St Denis, [email protected], http://libtom.org
+ */
+#include "tomcrypt.h"
+
+/**
+  @file der_length_boolean.c
+  ASN.1 DER, get length of a BOOLEAN, Tom St Denis
+*/
+
+#ifdef LTC_DER
+/**
+  Gets length of DER encoding of a BOOLEAN 
+  @param outlen [out] The length of the DER encoding
+  @return CRYPT_OK if successful
+*/
+int der_length_boolean(unsigned long *outlen)
+{
+   LTC_ARGCHK(outlen != NULL);
+   *outlen = 3;
+   return CRYPT_OK;
+}
+
+#endif
+
+/* $Source: /cvs/libtom/libtomcrypt/src/pk/asn1/der/boolean/der_length_boolean.c,v $ */
+/* $Revision: 1.3 $ */
+/* $Date: 2006/12/28 01:27:24 $ */
crypto/userspace/libtomcrypt/pk/asn1/der/choice/der_decode_choice.c new file +182 -0

182 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_decode_ia5_string.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_encode_ia5_string.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/ia5/der_length_ia5_string.c new file +194 -0

194 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_decode_integer.c new file +110 -0

110 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_encode_integer.c new file +130 -0

130 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/integer/der_length_integer.c new file +82 -0

82 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_decode_object_identifier.c new file +99 -0

99 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_encode_object_identifier.c new file +111 -0

111 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/object_identifier/der_length_object_identifier.c new file +89 -0

89 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_decode_octet_string.c new file +91 -0

91 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_encode_octet_string.c new file +86 -0

86 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/octet/der_length_octet_string.c new file +53 -0

53 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_decode_printable_string.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_encode_printable_string.c new file +85 -0

85 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/printable_string/der_length_printable_string.c new file +166 -0

166 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_ex.c new file +287 -0

48 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_flexi.c new file +394 -0

394 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_decode_sequence_multi.c new file +140 -0

140 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_ex.c new file +335 -0

335 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_encode_sequence_multi.c new file +139 -0

139 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_length_sequence.c new file +169 -0

169 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/sequence/der_sequence_free.c new file +66 -0

66 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_set.c new file +104 -0

104 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/set/der_encode_setof.c new file +163 -0

163 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_decode_short_integer.c new file +68 -0

68 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_encode_short_integer.c new file +97 -0

97 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/short_integer/der_length_short_integer.c new file +70 -0

70 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_decode_utctime.c new file +127 -0

127 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_encode_utctime.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utctime/der_length_utctime.c new file +46 -0

46 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_decode_utf8_string.c new file +111 -0

111 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_encode_utf8_string.c new file +105 -0

105 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/utf8/der_length_utf8_string.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.c new file +112 -0

112 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.c new file +83 -0

83 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_export.c new file +99 -0

99 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_free.c new file +34 -0
new file mode 100644
index 0000000..37a330d

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/dsa/dsa_import.c new file +101 -0

101 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_make_key.c new file +125 -0

125 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_sign_hash.c new file +147 -0

147 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_hash.c new file +126 -0

126 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/dsa/dsa_verify_key.c new file +100 -0

100 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_i2osp.c new file +51 -0

51 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_mgf1.c new file +91 -0

91 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_decode.c new file +192 -0

192 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_oaep_encode.c new file +164 -0

164 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_os2ip.c new file +36 -0
new file mode 100644
index 0000000..513abb6

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_decode.c new file +168 -0

168 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_pss_encode.c new file +157 -0

157 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_decode.c new file +110 -0

110 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/pkcs1/pkcs_1_v1_5_encode.c new file +95 -0

95 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_decrypt_key.c new file +107 -0

107 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_encrypt_key.c new file +96 -0

96 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_export.c new file +87 -0

87 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_exptmod.c new file +147 -0

147 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_free.c new file +34 -0
new file mode 100644
index 0000000..d38b266

Lines of this file are not shown. View the raw message.

crypto/userspace/libtomcrypt/pk/rsa/rsa_import.c new file +128 -0

128 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_make_key.c new file +105 -0

105 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_sign_hash.c new file +130 -0

130 lines not on this page · raw message

crypto/userspace/libtomcrypt/pk/rsa/rsa_verify_hash.c new file +170 -0

170 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.2.1

Part of the diff is not shown here. View the raw message.

#25 lore raw

PATCH 11/19 Add algorithm properties table.

In reply to Miloslav Trmač

Pointers to this table are used to identify algorithms throughout the code.

---
 crypto/userspace/Makefile       |    2 +-
 crypto/userspace/ncr-sessions.c |  150 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 151 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/ncr-sessions.c
crypto/userspace/Makefile +1 -1
index 81e4122..0891c2b 100644
@@ -64,7 +64,7 @@ TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argch
 	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
 
 cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
-	utils.o $(TOMMATH_OBJECTS) \
+	ncr-sessions.o utils.o $(TOMMATH_OBJECTS) \
 	$(TOMCRYPT_OBJECTS)
 
 
crypto/userspace/ncr-sessions.c new file +150 -0

150 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#26 lore raw

PATCH 12/19 Add DH implementation and pubkey abstraction layer

In reply to Miloslav Trmač

Add basic Diffie-Hellman implementation, because it is not provided by libtomcrypt.

Finally, add an algorithm-independent pubkey interface that encapsulates the separate pubkey algorithm implementations.

---
 crypto/userspace/Makefile |    2 +-
 crypto/userspace/ncr-dh.c |  282 +++++++++++++++++++
 crypto/userspace/ncr-pk.c |  659 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 942 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/ncr-dh.c
 create mode 100644 crypto/userspace/ncr-pk.c
crypto/userspace/Makefile +1 -1
index 0891c2b..fa4168d 100644
@@ -64,7 +64,7 @@ TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argch
 	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
 
 cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
-	ncr-sessions.o utils.o $(TOMMATH_OBJECTS) \
+	ncr-pk.o ncr-sessions.o ncr-dh.o utils.o $(TOMMATH_OBJECTS) \
 	$(TOMCRYPT_OBJECTS)
 
 
crypto/userspace/ncr-dh.c new file +282 -0

282 lines not on this page · raw message

crypto/userspace/ncr-pk.c new file +659 -0

659 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#27 lore raw

PATCH 13/19 Add /dev/crypto auditing infrastructure

In reply to Miloslav Trmač

Posted separately for review on linux-audit

---
 include/linux/audit.h |   38 ++++++++++++++
 kernel/auditfilter.c  |    2 +
 kernel/auditsc.c      |  136 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 176 insertions(+), 0 deletions(-)
include/linux/audit.h +38 -0
index 3c7a358..8faa4e0 100644
@@ -122,6 +122,9 @@
 #define AUDIT_MAC_UNLBL_STCADD	1416	/* NetLabel: add a static label */
 #define AUDIT_MAC_UNLBL_STCDEL	1417	/* NetLabel: del a static label */
 
+#define AUDIT_CRYPTO_STORAGE_KEY    1600 /* Key storage key configured */
+#define AUDIT_CRYPTO_USERSPACE_OP   1601 /* User-space crypto operation */
+
 #define AUDIT_FIRST_KERN_ANOM_MSG   1700
 #define AUDIT_LAST_KERN_ANOM_MSG    1799
 #define AUDIT_ANOM_PROMISCUOUS      1700 /* Device changed promiscuous mode */
@@ -207,6 +210,7 @@
 #define AUDIT_OBJ_TYPE	21
 #define AUDIT_OBJ_LEV_LOW	22
 #define AUDIT_OBJ_LEV_HIGH	23
+#define AUDIT_CRYPTO_OP	24
 
 				/* These are ONLY useful when checking
 				 * at syscall exit time (AUDIT_AT_EXIT). */
@@ -314,6 +318,20 @@ enum {
 #define AUDIT_PERM_READ		4
 #define AUDIT_PERM_ATTR		8
 
+#define AUDIT_CRYPTO_OP_CONTEXT_NEW	1
+#define AUDIT_CRYPTO_OP_CONTEXT_DEL	2
+#define AUDIT_CRYPTO_OP_SESSION_INIT	3
+#define AUDIT_CRYPTO_OP_SESSION_OP	4
+#define AUDIT_CRYPTO_OP_SESSION_FINAL	5
+#define AUDIT_CRYPTO_OP_KEY_IMPORT	6
+#define AUDIT_CRYPTO_OP_KEY_EXPORT	7
+#define AUDIT_CRYPTO_OP_KEY_WRAP	8
+#define AUDIT_CRYPTO_OP_KEY_UNWRAP	9
+#define AUDIT_CRYPTO_OP_KEY_GEN		10
+#define AUDIT_CRYPTO_OP_KEY_DERIVE	11
+#define AUDIT_CRYPTO_OP_KEY_ZEROIZE	12
+#define AUDIT_CRYPTO_OP_KEY_GET_INFO	13
+
 struct audit_status {
 	__u32		mask;		/* Bit mask for valid entries */
 	__u32		enabled;	/* 1 = enabled, 0 = disabled */
@@ -479,6 +497,10 @@ extern int __audit_log_bprm_fcaps(struct linux_binprm *bprm,
 				  const struct cred *new,
 				  const struct cred *old);
 extern void __audit_log_capset(pid_t pid, const struct cred *new, const struct cred *old);
+extern int __audit_log_crypto_op(int op, int context, int session,
+				 const char *operation, const char *algorithm,
+				 int key1, void *key1_id, size_t key1_id_size,
+				 int key2, void *key2_id, size_t key2_id_size);
 
 static inline void audit_ipc_obj(struct kern_ipc_perm *ipcp)
 {
@@ -532,6 +554,21 @@ static inline void audit_log_capset(pid_t pid, const struct cred *new,
 		__audit_log_capset(pid, new, old);
 }
 
+static inline int audit_log_crypto_op(int op, int context, int session,
+				      const char *operation,
+				      const char *algorithm, int key1,
+				      void *key1_id, size_t key1_id_size,
+				      int key2, void *key2_id,
+				      size_t key2_id_size)
+{
+	if (unlikely(!audit_dummy_context()))
+		return __audit_log_crypto_op(op, context, session, operation,
+					     algorithm, key1, key1_id,
+					     key1_id_size, key2, key2_id,
+					     key2_id_size);
+	return 0;
+}
+
 extern int audit_n_rules;
 extern int audit_signals;
 #else
@@ -565,6 +602,7 @@ extern int audit_signals;
 #define audit_mq_getsetattr(d,s) ((void)0)
 #define audit_log_bprm_fcaps(b, ncr, ocr) ({ 0; })
 #define audit_log_capset(pid, ncr, ocr) ((void)0)
+#define audit_log_crypto_op(op, context, session, key1, key1_id, key1_id_size, key2, key2_id, key2_id_size) (0)
 #define audit_ptrace(t) ((void)0)
 #define audit_n_rules 0
 #define audit_signals 0
kernel/auditfilter.c +2 -0
index a706040..a25a587 100644
@@ -363,6 +363,7 @@ static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
 		case AUDIT_DEVMINOR:
 		case AUDIT_EXIT:
 		case AUDIT_SUCCESS:
+		case AUDIT_CRYPTO_OP:
 			/* bit ops are only useful on syscall args */
 			if (f->op == Audit_bitmask || f->op == Audit_bittest)
 				goto exit_free;
@@ -457,6 +458,7 @@ static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
 		case AUDIT_ARG1:
 		case AUDIT_ARG2:
 		case AUDIT_ARG3:
+		case AUDIT_CRYPTO_OP:
 			break;
 		case AUDIT_ARCH:
 			entry->rule.arch_f = f;
kernel/auditsc.c +136 -0

196 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#28 lore raw

PATCH 14/19 Add most operations on key objects.

In reply to Miloslav Trmač

This includes:
- ncr_key_init
- ncr_key_deinit
- ncr_key_export (as plaintext)
- ncr_key_import
- ncr_key_generate
- ncr_key_generate_pair
- ncr_key_derive
- ncr_key_get_info
and supporting infrastructure.

---
 crypto/userspace/Makefile  |    2 +-
 crypto/userspace/ncr-key.c |  689 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 690 insertions(+), 1 deletions(-)
 create mode 100644 crypto/userspace/ncr-key.c
crypto/userspace/Makefile +1 -1
index fa4168d..454ed9d 100644
@@ -63,7 +63,7 @@ TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argch
 	libtomcrypt/misc/pk_get_oid.o libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.o \
 	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
 
-cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-limits.o \
+cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-key.o ncr-limits.o \
 	ncr-pk.o ncr-sessions.o ncr-dh.o utils.o $(TOMMATH_OBJECTS) \
 	$(TOMCRYPT_OBJECTS)
 
crypto/userspace/ncr-key.c new file +689 -0

689 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#29 lore raw

PATCH 15/19 Add key wrapping operations

In reply to Miloslav Trmač

This includes:
- ncr_key_wrap
- ncr_key_unwrap
- ncr_key_storage_wrap
- ncr_key_storage_unwrap

---
 crypto/userspace/Makefile          |    5 +-
 crypto/userspace/ncr-key-storage.c |  136 +++++++
 crypto/userspace/ncr-key-wrap.c    |  763 ++++++++++++++++++++++++++++++++++++
 crypto/userspace/ncr.c             |   29 ++
 4 files changed, 931 insertions(+), 2 deletions(-)
 create mode 100644 crypto/userspace/ncr-key-storage.c
 create mode 100644 crypto/userspace/ncr-key-wrap.c
 create mode 100644 crypto/userspace/ncr.c
crypto/userspace/Makefile +3 -2
index 454ed9d..689ee0d 100644
@@ -63,8 +63,9 @@ TOMCRYPT_OBJECTS = libtomcrypt/misc/zeromem.o libtomcrypt/misc/crypt/crypt_argch
 	libtomcrypt/misc/pk_get_oid.o libtomcrypt/pk/asn1/der/x509/der_encode_subject_public_key_info.o \
 	libtomcrypt/pk/asn1/der/x509/der_decode_subject_public_key_info.o
 
-cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr-key.o ncr-limits.o \
-	ncr-pk.o ncr-sessions.o ncr-dh.o utils.o $(TOMMATH_OBJECTS) \
+cryptodev-objs := cryptodev_main.o cryptodev_cipher.o ncr.o \
+	ncr-key.o ncr-limits.o  ncr-pk.o ncr-sessions.o ncr-dh.o \
+	ncr-key-wrap.o ncr-key-storage.o utils.o $(TOMMATH_OBJECTS) \
 	$(TOMCRYPT_OBJECTS)
 
 
crypto/userspace/ncr-key-storage.c new file +136 -0

136 lines not on this page · raw message

crypto/userspace/ncr-key-wrap.c new file +763 -0

763 lines not on this page · raw message

crypto/userspace/ncr.c new file +29 -0
new file mode 100644
index 0000000..1838aab
@@ -0,0 +1,29 @@
+/*
+ * New driver for /dev/crypto device (aka CryptoDev)
+ *
+ * Copyright (c) 2010 Katholieke Universiteit Leuven
+ *
+ * Author: Nikos Mavrogiannopoulos <[email protected]>
+ *
+ * This file is part of linux cryptodev.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include "ncr-int.h"
+
+/* This is the master wrapping key for storage of keys
+ */
+struct key_item_st master_key;
Signature · 1 line
1.7.2.1
#30 lore raw

PATCH 16/19 Add helpers for zero-copy userspace access

In reply to Miloslav Trmač
---
 crypto/userspace/cryptodev_main.c |   87 +++++++++++++++++++++++++++++++++++++
 1 files changed, 87 insertions(+), 0 deletions(-)
crypto/userspace/cryptodev_main.c +87 -0
index c6419f4..a6712db 100644
@@ -1,3 +1,46 @@
+/*
+ * Driver for /dev/crypto device (aka CryptoDev)
+ *
+ * Copyright (c) 2004 Michal Ludvig <[email protected]>, SuSE Labs
+ * Copyright (c) 2009,2010 Nikos Mavrogiannopoulos <[email protected]>
+ *
+ * This file is part of linux cryptodev.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+/*
+ * Device /dev/crypto provides an interface for 
+ * accessing kernel CryptoAPI algorithms (ciphers,
+ * hashes) from userspace programs.
+ *
+ * /dev/crypto interface was originally introduced in
+ * OpenBSD and this module attempts to keep the API.
+ *
+ */
+
+#include <linux/audit.h>
+#include <linux/crypto.h>
+#include <linux/mm.h>
+#include <linux/highmem.h>
+#include <linux/ioctl.h>
+#include <linux/random.h>
+#include <linux/syscalls.h>
+#include <linux/pagemap.h>
+#include <linux/uaccess.h>
+#include <linux/scatterlist.h>
 #include "cryptodev_int.h"
 #include "ncr-int.h"
 #include <linux/version.h>
@@ -11,3 +54,47 @@ MODULE_LICENSE("GPL");
 int cryptodev_verbosity = 0;
 module_param(cryptodev_verbosity, int, 0644);
 MODULE_PARM_DESC(cryptodev_verbosity, "0: normal, 1: verbose, 2: debug");
+
+/* ====== CryptoAPI ====== */
+
+void release_user_pages(struct page **pg, int pagecount)
+{
+	while (pagecount--) {
+		if (!PageReserved(pg[pagecount]))
+			SetPageDirty(pg[pagecount]);
+		page_cache_release(pg[pagecount]);
+	}
+}
+
+/* offset of buf in it's first page */
+#define PAGEOFFSET(buf) ((unsigned long)buf & ~PAGE_MASK)
+
+/* fetch the pages addr resides in into pg and initialise sg with them */
+int __get_userbuf(uint8_t __user *addr, uint32_t len, int write,
+		int pgcount, struct page **pg, struct scatterlist *sg)
+{
+	int ret, pglen, i = 0;
+	struct scatterlist *sgp;
+
+	down_write(&current->mm->mmap_sem);
+	ret = get_user_pages(current, current->mm,
+			(unsigned long)addr, pgcount, write, 0, pg, NULL);
+	up_write(&current->mm->mmap_sem);
+	if (ret != pgcount)
+		return -EINVAL;
+
+	sg_init_table(sg, pgcount);
+
+	pglen = min((ptrdiff_t)(PAGE_SIZE - PAGEOFFSET(addr)), (ptrdiff_t)len);
+	sg_set_page(sg, pg[i++], pglen, PAGEOFFSET(addr));
+
+	len -= pglen;
+	for (sgp = sg_next(sg); len; sgp = sg_next(sgp)) {
+		pglen = min((uint32_t)PAGE_SIZE, len);
+		sg_set_page(sgp, pg[i++], pglen, 0);
+		len -= pglen;
+	}
+	sg_mark_end(sg_last(sg, pgcount));
+	return 0;
+}
+
Signature · 1 line
1.7.2.1
#31 lore raw

PATCH 17/19 Add session operations

In reply to Miloslav Trmač

This includes:
- ncr_session_init
- ncr_session_update
- ncr_session_final
- ncr_session_once

The ncr_session_*_from_nla() functions are separate from the main session code because they belong into ncr.c along with other code that deals directly with user-space data structures and handles CONFIG_COMPAT.

---
 crypto/userspace/ncr-sessions.c |  953 +++++++++++++++++++++++++++++++++++++++
 crypto/userspace/ncr.c          |   86 ++++
 2 files changed, 1039 insertions(+), 0 deletions(-)
crypto/userspace/ncr-sessions.c +953 -0
index e6fd995..b2adb8b 100644
@@ -32,6 +32,104 @@
 #include <linux/scatterlist.h>
 #include <net/netlink.h>
 
+static int _ncr_session_update_key(struct ncr_lists *lists, ncr_session_t ses,
+				   struct nlattr *tb[]);
+static void _ncr_session_remove(struct ncr_lists *lst, ncr_session_t desc);
+
+static int session_list_deinit_fn(int id, void *item, void *unused)
+{
+	(void)unused;
+	_ncr_sessions_item_put(item);
+	return 0;
+}
+
+void ncr_sessions_list_deinit(struct ncr_lists *lst)
+{
+	/* The mutex is not necessary, but doesn't hurt and makes it easier to
+	   verify locking correctness. */
+	mutex_lock(&lst->session_idr_mutex);
+	idr_for_each(&lst->session_idr, session_list_deinit_fn, NULL);
+	idr_remove_all(&lst->session_idr);
+	idr_destroy(&lst->session_idr);
+	mutex_unlock(&lst->session_idr_mutex);
+}
+
+/* returns the data item corresponding to desc */
+struct session_item_st* ncr_sessions_item_get(struct ncr_lists *lst, ncr_session_t desc)
+{
+struct session_item_st* item;
+
+	mutex_lock(&lst->session_idr_mutex);
+	item = idr_find(&lst->session_idr, desc);
+	if (item != NULL) {
+		atomic_inc(&item->refcnt);
+		mutex_unlock(&lst->session_idr_mutex);
+		return item;
+	}
+	mutex_unlock(&lst->session_idr_mutex);
+
+	err();
+	return NULL;
+}
+
+void _ncr_sessions_item_put( struct session_item_st* item)
+{
+	if (atomic_dec_and_test(&item->refcnt)) {
+		cryptodev_cipher_deinit(&item->cipher);
+		ncr_pk_cipher_deinit(&item->pk);
+		cryptodev_hash_deinit(&item->hash);
+		if (item->key)
+			_ncr_key_item_put(item->key);
+		kfree(item->sg);
+		kfree(item->pages);
+		kfree(item);
+	}
+}
+
+struct session_item_st* ncr_session_new(struct ncr_lists *lst)
+{
+	struct session_item_st* sess;
+
+	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
+	if (sess == NULL) {
+		err();
+		return NULL;
+	}
+
+	sess->array_size = DEFAULT_PREALLOC_PAGES;
+	sess->pages = kzalloc(sess->array_size *
+			sizeof(struct page *), GFP_KERNEL);
+	sess->sg = kzalloc(sess->array_size *
+			sizeof(struct scatterlist), GFP_KERNEL);
+	if (sess->sg == NULL || sess->pages == NULL) {
+		err();
+		goto err_sess;
+	}
+	mutex_init(&sess->mem_mutex);
+
+	atomic_set(&sess->refcnt, 2); /* One for lst->list, one for "sess" */
+
+	mutex_lock(&lst->session_idr_mutex);
+	/* idr_pre_get() should preallocate enough, and, due to
+	   session_idr_mutex, nobody else can use the preallocated data.
+	   Therefore the loop recommended in idr_get_new() documentation is not
+	   necessary. */
+	if (idr_pre_get(&lst->session_idr, GFP_KERNEL) == 0 ||
+	    idr_get_new(&lst->session_idr, sess, &sess->desc) != 0) {
+		mutex_unlock(&lst->session_idr_mutex);
+		goto err_sess;
+	}
+	mutex_unlock(&lst->session_idr_mutex);
+
+	return sess;
+
+err_sess:
+	kfree(sess->sg);
+	kfree(sess->pages);
+	kfree(sess);
+	return NULL;
+}
+
 static const struct algo_properties_st algo_properties[] = {
 #define KSTR(x) .kstr = x, .kstr_len = sizeof(x) - 1
 	{ .algo = NCR_ALG_NULL, KSTR("ecb(cipher_null)"),
@@ -142,9 +240,864 @@ const struct algo_properties_st *_ncr_nla_to_properties(const struct nlattr *nla
 	return NULL;
 }
 
+static const char *ncr_op_name(ncr_crypto_op_t op)
+{
+	static const char *const names[] = {
+		[NCR_OP_ENCRYPT] = "encrypt",
+		[NCR_OP_DECRYPT] = "decrypt",
+		[NCR_OP_SIGN] = "sign",
+		[NCR_OP_VERIFY] = "verify"
+	};
+
+	const char *res;
+
+	res = NULL;
+	if (op < ARRAY_SIZE(names))
+		res = names[op];
+	if (res == NULL)
+		res = "unknown";
+	return res;
+}
+
 const char *ncr_algorithm_name(const struct algo_properties_st *algo)
 {
 	if (algo != NULL && algo->kstr != NULL)
 		return algo->kstr;
 	return "unknown";
 }
+
+static int key_item_get_nla_read(struct key_item_st **st,
+				 struct ncr_lists *lists,
+				 const struct nlattr *nla)
+{
+	int ret;
+
+	if (nla == NULL) {
+		err();
+		return -EINVAL;
+	}
+	ret = ncr_key_item_get_read(st, lists, nla_get_u32(nla));
+	if (ret < 0) {
+		err();
+		return ret;
+	}
+	return ret;
+}
+
+static int _ncr_session_init(struct ncr_lists *lists, ncr_crypto_op_t op,
+			     struct nlattr *tb[])
+{
+	const struct nlattr *nla;
+	struct session_item_st* ns = NULL;
+	int ret, audit_ret;
+	const struct algo_properties_st *sign_hash;
+
+	ns = ncr_session_new(lists);
+	if (ns == NULL) {
+		err();
+		return -ENOMEM;
+	}
+
+	ns->op = op;
+	ns->algorithm = _ncr_nla_to_properties(tb[NCR_ATTR_ALGORITHM]);
+	if (ns->algorithm == NULL) {
+		err();
+		ret = -EINVAL;
+		goto fail;
+	}
+	
+	switch(op) {
+		case NCR_OP_ENCRYPT:
+		case NCR_OP_DECRYPT:
+			if (!ns->algorithm->can_encrypt) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			/* read key */
+			ret = key_item_get_nla_read(&ns->key, lists,
+						    tb[NCR_ATTR_KEY]);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+			if (ns->key->type == NCR_KEY_TYPE_SECRET) {
+				int keysize = ns->key->key.secret.size;
+				
+				if (ns->algorithm->algo == NCR_ALG_NULL)
+				  keysize = 0;
+				
+				if (ns->algorithm->is_pk) {
+					err();
+					ret = -EINVAL;
+					goto fail;
+				}
+
+				ret = cryptodev_cipher_init(&ns->cipher, ns->algorithm->kstr,
+					ns->key->key.secret.data, keysize);
+				if (ret < 0) {
+					err();
+					goto fail;
+				}
+
+				if (ns->algorithm->needs_iv) {
+					nla = tb[NCR_ATTR_IV];
+					if (nla == NULL) {
+						err();
+						ret = -EINVAL;
+						goto fail;
+					}
+					cryptodev_cipher_set_iv(&ns->cipher,
+								nla_data(nla),
+								nla_len(nla));
+				}
+			} else if (ns->key->type == NCR_KEY_TYPE_PRIVATE || ns->key->type == NCR_KEY_TYPE_PUBLIC) {
+				ret = ncr_pk_cipher_init(ns->algorithm, &ns->pk, 
+							 tb, ns->key, NULL);
+				if (ret < 0) {
+					err();
+					goto fail;
+				}
+			} else {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+			break;
+
+		case NCR_OP_SIGN:
+		case NCR_OP_VERIFY:
+			if (!ns->algorithm->can_sign && !ns->algorithm->can_digest) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			if (ns->algorithm->can_digest) {
+				if (ns->algorithm->is_pk) {
+					err();
+					ret = -EINVAL;
+					goto fail;
+				}
+
+				ret = cryptodev_hash_init(&ns->hash, ns->algorithm->kstr, 0, NULL, 0);
+				if (ret < 0) {
+					err();
+					goto fail;
+				}
+			
+			} else {
+				/* read key */
+				ret = key_item_get_nla_read(&ns->key, lists,
+							    tb[NCR_ATTR_KEY]);
+				if (ret < 0) {
+					err();
+					goto fail;
+				}
+
+				if (ns->algorithm->is_hmac && ns->key->type == NCR_KEY_TYPE_SECRET) {
+					if (ns->algorithm->is_pk) {
+						err();
+						ret = -EINVAL;
+						goto fail;
+					}
+
+					ret = cryptodev_hash_init(&ns->hash, ns->algorithm->kstr, 1,
+						ns->key->key.secret.data, ns->key->key.secret.size);
+					if (ret < 0) {
+						err();
+						goto fail;
+					}
+
+				} else if (ns->algorithm->is_pk && (ns->key->type == NCR_KEY_TYPE_PRIVATE || ns->key->type == NCR_KEY_TYPE_PUBLIC)) {
+					nla = tb[NCR_ATTR_SIGNATURE_HASH_ALGORITHM];
+					sign_hash = _ncr_nla_to_properties(nla);
+					if (sign_hash == NULL) {
+						err();
+						ret = -EINVAL;
+						goto fail;
+					}
+
+					if (!sign_hash->can_digest) {
+						err();
+						ret = -EINVAL;
+						goto fail;
+					}
+
+					if (sign_hash->is_pk) {
+						err();
+						ret = -EINVAL;
+						goto fail;
+					}
+
+					ret = ncr_pk_cipher_init(ns->algorithm, &ns->pk, 
+						tb, ns->key, sign_hash);
+					if (ret < 0) {
+						err();
+						goto fail;
+					}
+
+					ret = cryptodev_hash_init(&ns->hash, sign_hash->kstr, 0, NULL, 0);
+					if (ret < 0) {
+						err();
+						goto fail;
+					}
+				} else {
+					err();
+					ret = -EINVAL;
+					goto fail;
+				}
+			}
+
+			break;
+		default:
+			err();
+			ret = -EINVAL;
+			goto fail;
+	}
+	
+	ret = ns->desc;
+
+fail:
+	// algorithm, params, op
+	audit_ret = audit_log_crypto_op(AUDIT_CRYPTO_OP_SESSION_INIT, lists->id,
+					ns->desc, ncr_op_name(ns->op),
+					ncr_algorithm_name(ns->algorithm),
+					ns->key != NULL ? ns->key->desc : -1,
+					ns->key != NULL ? ns->key->key_id : NULL,
+					ns->key != NULL ? ns->key->key_id_size : 0,
+					-1, NULL, 0);
+	if (audit_ret < 0 && ret == 0)
+		ret = audit_ret;
+	if (ret < 0) {
+		_ncr_session_remove(lists, ns->desc);
+	}
+	_ncr_sessions_item_put(ns);
+
+	return ret;
+}
+
+int ncr_session_init(struct ncr_lists *lists,
+		     const struct ncr_session_init *session,
+		     struct nlattr *tb[])
+{
+	return _ncr_session_init(lists, session->op, tb);
+}
+
+static int _ncr_session_encrypt(struct session_item_st* sess, const struct scatterlist* input, unsigned input_cnt,
+	size_t input_size, void *output, unsigned output_cnt, size_t *output_size)
+{
+int ret;
+
+	if (sess->algorithm->is_symmetric) {
+		/* read key */
+		ret = cryptodev_cipher_encrypt(&sess->cipher, input, 
+			output, input_size);
+		if (ret < 0) {
+			err();
+			return ret;
+		}
+		/* FIXME: handle ciphers that do not require that */
+		*output_size = input_size;
+	} else { /* public key */
+		ret = ncr_pk_cipher_encrypt(&sess->pk, input, input_cnt, input_size,
+			output, output_cnt, output_size);
+		
+		if (ret < 0) {
+			err();
+			return ret;
+		}
+	}
+	
+	return 0;
+}
+
+static int _ncr_session_decrypt(struct session_item_st* sess, const struct scatterlist* input, 
+	unsigned input_cnt, size_t input_size,
+	struct scatterlist *output, unsigned output_cnt, size_t *output_size)
+{
+int ret;
+
+	if (sess->algorithm->is_symmetric) {
+		/* read key */
+		ret = cryptodev_cipher_decrypt(&sess->cipher, input, 
+			output, input_size);
+		if (ret < 0) {
+			err();
+			return ret;
+		}
+		/* FIXME: handle ciphers that do not require equality */
+		*output_size = input_size;
+	} else { /* public key */
+		ret = ncr_pk_cipher_decrypt(&sess->pk, input, input_cnt, input_size,
+			output, output_cnt, output_size);
+		
+		if (ret < 0) {
+			err();
+			return ret;
+		}
+	}
+	
+	return 0;
+}
+
+static void _ncr_session_remove(struct ncr_lists *lst, ncr_session_t desc)
+{
+	struct session_item_st * item;
+
+	mutex_lock(&lst->session_idr_mutex);
+	item = idr_find(&lst->session_idr, desc);
+	if (item != NULL)
+		idr_remove(&lst->session_idr, desc); /* Steal the reference */
+	mutex_unlock(&lst->session_idr_mutex);
+
+	if (item != NULL)
+		_ncr_sessions_item_put(item);
+}
+
+static int _ncr_session_grow_pages(struct session_item_st *ses, int pagecount)
+{
+	struct scatterlist *sg;
+	struct page **pages;
+	int array_size;
+
+	if (likely(pagecount < ses->array_size))
+		return 0;
+
+	for (array_size = ses->array_size; array_size < pagecount;
+	     array_size *= 2)
+		;
+
+	dprintk(2, KERN_DEBUG, "%s: reallocating to %d elements\n",
+		__func__, array_size);
+	pages = krealloc(ses->pages, array_size * sizeof(struct page *),
+			 GFP_KERNEL);
+	if (unlikely(pages == NULL))
+		return -ENOMEM;
+	ses->pages = pages;
+	sg = krealloc(ses->sg, array_size * sizeof(struct scatterlist),
+		      GFP_KERNEL);
+	if (unlikely(sg == NULL))
+		return -ENOMEM;
+	ses->sg = sg;
+
+	ses->array_size = array_size;
+	return 0;
+}
+
+/* Make NCR_ATTR_UPDATE_INPUT_DATA and NCR_ATTR_UPDATE_OUTPUT_BUFFER available
+   in scatterlists */
+static int get_userbuf2(struct session_item_st *ses, struct nlattr *tb[],
+			struct scatterlist **src_sg, unsigned *src_cnt,
+			size_t *src_size, struct ncr_session_output_buffer *dst,
+			struct scatterlist **dst_sg, unsigned *dst_cnt,
+			int compat)
+{
+	const struct nlattr *src_nla, *dst_nla;
+	struct ncr_session_input_data src;
+	int src_pagecount, dst_pagecount = 0, pagecount, write_src = 1, ret;
+	size_t input_size;
+
+	src_nla = tb[NCR_ATTR_UPDATE_INPUT_DATA];
+	dst_nla = tb[NCR_ATTR_UPDATE_OUTPUT_BUFFER];
+
+	ret = ncr_session_input_data_from_nla(&src, src_nla, compat);
+	if (unlikely(ret != 0)) {
+		err();
+		return ret;
+	}
+	*src_size = src.data_size;
+
+	if (dst_nla != NULL) {
+		ret = ncr_session_output_buffer_from_nla(dst, dst_nla, compat);
+		if (unlikely(ret != 0)) {
+			err();
+			return ret;
+		}
+	}
+
+	input_size = src.data_size;
+	src_pagecount = PAGECOUNT(src.data, input_size);
+
+	if (dst_nla == NULL || src.data != dst->buffer) {	/* non-in-situ transformation */
+		write_src = 0;
+		if (dst_nla != NULL) {
+			dst_pagecount = PAGECOUNT(dst->buffer,
+						  dst->buffer_size);
+		} else {
+			dst_pagecount = 0;
+		}
+	} else {
+		src_pagecount = max((int)(PAGECOUNT(dst->buffer,
+						    dst->buffer_size)),
+				    src_pagecount);
+		input_size = max(input_size, dst->buffer_size);
+	}
+
+	pagecount = src_pagecount + dst_pagecount;
+	ret = _ncr_session_grow_pages(ses, pagecount);
+	if (ret != 0) {
+		err();
+		return ret;
+	}
+
+	if (__get_userbuf((void __user *)src.data, input_size, write_src,
+			  src_pagecount, ses->pages, ses->sg)) {
+		err();
+		printk("write: %d\n", write_src);
+		return -EINVAL;
+	}
+	(*src_sg) = ses->sg;
+	*src_cnt = src_pagecount;
+
+	if (dst_pagecount) {
+		*dst_cnt = dst_pagecount;
+		(*dst_sg) = ses->sg + src_pagecount;
+
+		if (__get_userbuf(dst->buffer, dst->buffer_size, 1,
+				  dst_pagecount, ses->pages + src_pagecount,
+				  *dst_sg)) {
+			err();
+			release_user_pages(ses->pages, src_pagecount);
+			return -EINVAL;
+		}
+	} else {
+		if (dst_nla != NULL) {
+			*dst_cnt = src_pagecount;
+			(*dst_sg) = (*src_sg);
+		} else {
+			*dst_cnt = 0;
+			*dst_sg = NULL;
+		}
+	}
+	
+	ses->available_pages = pagecount;
+
+	return 0;
+}
+
+/* Called when userspace buffers are used */
+static int _ncr_session_update(struct ncr_lists *lists, ncr_session_t ses,
+			       struct nlattr *tb[], int compat)
+{
+	int ret;
+	struct session_item_st* sess;
+	struct scatterlist *isg = NULL;
+	struct scatterlist *osg = NULL;
+	unsigned osg_cnt=0, isg_cnt=0;
+	size_t isg_size = 0, osg_size;
+	struct ncr_session_output_buffer out;
+
+	sess = ncr_sessions_item_get(lists, ses);
+	if (sess == NULL) {
+		err();
+		return -EINVAL;
+	}
+
+	if (mutex_lock_interruptible(&sess->mem_mutex)) {
+		err();
+		_ncr_sessions_item_put(sess);
+		return -ERESTARTSYS;
+	}
+
+	ret = get_userbuf2(sess, tb, &isg, &isg_cnt, &isg_size, &out, &osg,
+			   &osg_cnt, compat);
+	if (ret < 0) {
+		err();
+		goto fail;
+	}
+
+	switch(sess->op) {
+		case NCR_OP_ENCRYPT:
+			if (osg == NULL) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			osg_size = out.buffer_size;
+			if (osg_size < isg_size) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			ret = _ncr_session_encrypt(sess, isg, isg_cnt, isg_size, 
+				osg, osg_cnt, &osg_size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+
+			ret = ncr_session_output_buffer_set_size(&out, osg_size,
+								 compat);
+			if (ret != 0) {
+				err();
+				goto fail;
+			}
+			break;
+		case NCR_OP_DECRYPT:
+			if (osg == NULL) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			osg_size = out.buffer_size;
+			if (osg_size < isg_size) {
+				err();
+				ret = -EINVAL;
+				goto fail;
+			}
+
+			ret = _ncr_session_decrypt(sess, isg, isg_cnt, isg_size, 
+				osg, osg_cnt, &osg_size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+
+			ret = ncr_session_output_buffer_set_size(&out, osg_size,
+								 compat);
+			if (ret != 0) {
+				err();
+				goto fail;
+			}
+			break;
+
+		case NCR_OP_SIGN:
+		case NCR_OP_VERIFY:
+			ret = cryptodev_hash_update(&sess->hash, isg, isg_size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+			break;
+		default:
+			err();
+			ret = -EINVAL;
+			goto fail;
+	}
+
+	ret = 0;
+
+fail:
+	audit_log_crypto_op(AUDIT_CRYPTO_OP_SESSION_OP, lists->id, sess->desc,
+			    ncr_op_name(sess->op),
+			    ncr_algorithm_name(sess->algorithm), -1, NULL, 0,
+			    -1, NULL, 0);
+
+	if (sess->available_pages) {
+		release_user_pages(sess->pages, sess->available_pages);
+		sess->available_pages = 0;
+	}
+	mutex_unlock(&sess->mem_mutex);
+	_ncr_sessions_item_put(sess);
+
+	return ret;
+}
+
+static int try_session_update(struct ncr_lists *lists, ncr_session_t ses,
+			      struct nlattr *tb[], int compat)
+{
+	if (tb[NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA] != NULL)
+		return _ncr_session_update_key(lists, ses, tb);
+	else if (tb[NCR_ATTR_UPDATE_INPUT_DATA] != NULL)
+		return _ncr_session_update(lists, ses, tb, compat);
+
+	return 0;
+}
+
+static int _ncr_session_final(struct ncr_lists *lists, ncr_session_t ses,
+			      struct nlattr *tb[], int compat)
+{
+	const struct nlattr *nla;
+	int ret;
+	struct session_item_st* sess;
+	int digest_size;
+	uint8_t digest[NCR_HASH_MAX_OUTPUT_SIZE];
+	void *buffer = NULL;
+
+	sess = ncr_sessions_item_get(lists, ses);
+	if (sess == NULL) {
+		err();
+		return -EINVAL;
+	}
+
+	ret = try_session_update(lists, ses, tb, compat);
+	if (ret < 0) {
+		err();
+		_ncr_sessions_item_put(sess);
+		return ret;
+	}
+
+	if (mutex_lock_interruptible(&sess->mem_mutex)) {
+		err();
+		_ncr_sessions_item_put(sess);
+		return -ERESTARTSYS;
+	}
+
+	switch(sess->op) {
+	case NCR_OP_ENCRYPT:
+	case NCR_OP_DECRYPT:
+		break;
+	case NCR_OP_VERIFY: {
+		struct ncr_session_input_data src;
+
+		nla = tb[NCR_ATTR_FINAL_INPUT_DATA];
+		ret = ncr_session_input_data_from_nla(&src, nla, compat);
+		if (unlikely(ret != 0)) {
+			err();
+			goto fail;
+		}
+
+		buffer = kmalloc(src.data_size, GFP_KERNEL);
+		if (buffer == NULL) {
+			err();
+			ret = -ENOMEM;
+			goto fail;
+		}
+		if (unlikely(copy_from_user(buffer, src.data, src.data_size))) {
+			err();
+			ret = -EFAULT;
+			goto fail;
+		}
+
+		digest_size = sess->hash.digestsize;
+		if (digest_size == 0 || sizeof(digest) < digest_size) {
+			err();
+			ret = -EINVAL;
+			goto fail;
+		}
+		ret = cryptodev_hash_final(&sess->hash, digest);
+		if (ret < 0) {
+			err();
+			goto fail;
+		}
+
+		if (!sess->algorithm->is_pk)
+			ret = (digest_size == src.data_size
+			       && memcmp(buffer, digest, digest_size) == 0);
+		else {
+			ret = ncr_pk_cipher_verify(&sess->pk, buffer,
+						   src.data_size, digest,
+						   digest_size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+		}
+		break;
+	}
+
+	case NCR_OP_SIGN: {
+		struct ncr_session_output_buffer dst;
+		size_t output_size;
+
+		nla = tb[NCR_ATTR_FINAL_OUTPUT_BUFFER];
+		ret = ncr_session_output_buffer_from_nla(&dst, nla, compat);
+		if (unlikely(ret != 0)) {
+			err();
+			goto fail;
+		}
+
+		digest_size = sess->hash.digestsize;
+		if (digest_size == 0) {
+			err();
+			ret = -EINVAL;
+			goto fail;
+		}
+
+		ret = cryptodev_hash_final(&sess->hash, digest);
+		if (ret < 0) {
+			err();
+			goto fail;
+		}
+
+		cryptodev_hash_deinit(&sess->hash);
+
+		if (!sess->algorithm->is_pk) {
+			if (dst.buffer_size < digest_size) {
+				err();
+				ret = -ERANGE;
+				goto fail;
+			}
+			if (unlikely(copy_to_user(dst.buffer, digest,
+						  digest_size))) {
+				err();
+				ret = -EFAULT;
+				goto fail;
+			}
+			output_size = digest_size;
+		} else {
+			output_size = dst.buffer_size;
+			buffer = kmalloc(output_size, GFP_KERNEL);
+			if (buffer == NULL) {
+				err();
+				ret = -ENOMEM;
+				goto fail;
+			}
+			ret = ncr_pk_cipher_sign(&sess->pk, digest, digest_size,
+						 buffer, &output_size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+			if (unlikely(copy_to_user(dst.buffer, buffer,
+						  output_size))) {
+				err();
+				ret = -EFAULT;
+				goto fail;
+			}
+		}
+
+		ret = ncr_session_output_buffer_set_size(&dst, output_size,
+							 compat);
+		if (ret != 0) {
+			err();
+			goto fail;
+		}
+		break;
+	}
+	default:
+		err();
+		ret = -EINVAL;
+		goto fail;
+	}
+
+fail:
+	audit_log_crypto_op(AUDIT_CRYPTO_OP_SESSION_FINAL, lists->id,
+			    sess->desc, ncr_op_name(sess->op),
+			    ncr_algorithm_name(sess->algorithm), -1, NULL, 0,
+			    -1, NULL, 0);
+
+	kfree(buffer);
+	mutex_unlock(&sess->mem_mutex);
+
+	cryptodev_hash_deinit(&sess->hash);
+	if (sess->algorithm->is_symmetric) {
+		cryptodev_cipher_deinit(&sess->cipher);
+	} else {
+		ncr_pk_cipher_deinit(&sess->pk);
+	}
+
+	_ncr_sessions_item_put(sess);
+	_ncr_session_remove(lists, ses);
+
+	return ret;
+}
+
+/* Direct with key: Allows to hash a key */
+static int _ncr_session_update_key(struct ncr_lists *lists, ncr_session_t ses,
+				   struct nlattr *tb[])
+{
+	int ret;
+	struct session_item_st* sess;
+	struct key_item_st* key = NULL;
+
+	sess = ncr_sessions_item_get(lists, ses);
+	if (sess == NULL) {
+		err();
+		return -EINVAL;
+	}
+
+	/* read key */
+	ret = key_item_get_nla_read(&key, lists,
+				    tb[NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA]);
+	if (ret < 0) {
+		err();
+		goto fail;
+	}
+	
+	if (key->type != NCR_KEY_TYPE_SECRET) {
+		err();
+		ret = -EINVAL;
+		goto fail;
+	}
+
+	switch(sess->op) {
+		case NCR_OP_ENCRYPT:
+		case NCR_OP_DECRYPT:
+			err();
+			ret = -EINVAL;
+			goto fail;
+		case NCR_OP_SIGN:
+		case NCR_OP_VERIFY:
+			ret = _cryptodev_hash_update(&sess->hash, 
+				key->key.secret.data, key->key.secret.size);
+			if (ret < 0) {
+				err();
+				goto fail;
+			}
+			break;
+		default:
+			err();
+			ret = -EINVAL;
+			goto fail;
+	}
+
+	ret = 0;
+
+fail:
+	audit_log_crypto_op(AUDIT_CRYPTO_OP_SESSION_OP, lists->id, sess->desc,
+			    ncr_op_name(sess->op),
+			    ncr_algorithm_name(sess->algorithm),
+			    key != NULL ? key->desc : -1,
+			    key != NULL ? key->key_id : NULL,
+			    key != NULL ? key->key_id_size : 0, -1, NULL, 0);
+
+	if (key) _ncr_key_item_put(key);
+	_ncr_sessions_item_put(sess);
+
+	return ret;
+}
+
+int ncr_session_update(struct ncr_lists *lists,
+		       const struct ncr_session_update *op, struct nlattr *tb[],
+		       int compat)
+{
+	int ret;
+
+	if (tb[NCR_ATTR_UPDATE_INPUT_DATA] != NULL)
+		ret = _ncr_session_update(lists, op->ses, tb, compat);
+	else if (tb[NCR_ATTR_UPDATE_INPUT_KEY_AS_DATA] != NULL)
+		ret = _ncr_session_update_key(lists, op->ses, tb);
+	else
+		ret = -EINVAL;
+
+	if (unlikely(ret)) {
+		err();
+		return ret;
+	}
+
+	return 0;
+}
+
+int ncr_session_final(struct ncr_lists *lists,
+		      const struct ncr_session_final *op, struct nlattr *tb[],
+		      int compat)
+{
+	return _ncr_session_final(lists, op->ses, tb, compat);
+}
+
+int ncr_session_once(struct ncr_lists *lists,
+		     const struct ncr_session_once *once, struct nlattr *tb[],
+		     int compat)
+{
+	int ret;
+
+	ret = _ncr_session_init(lists, once->op, tb);
+	if (ret < 0) {
+		err();
+		return ret;
+	}
+
+	ret = _ncr_session_final(lists, ret, tb, compat);
+	if (ret < 0) {
+		err();
+		return ret;
+	}
+
+	return ret;
+}
crypto/userspace/ncr.c +86 -0

94 lines not on this page · raw message

Signature · 1 line
1.7.2.1
#32 lore raw

PATCH 18/19 Add ioctl handlers

In reply to Miloslav Trmač

Add ioctl and compat_ioctl handling. This is the only file that directly accesses structured data from userspace (other files may access unformated data such as cipher input or multiple-precision integers).

Also add the last operation, ncr_master_key_set.

---
 crypto/userspace/ncr.c |  405 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 405 insertions(+), 0 deletions(-)
crypto/userspace/ncr.c +405 -0
index 9fb56ad..ee812ab 100644
@@ -44,6 +44,411 @@
  */
 struct key_item_st master_key;
 
+static struct mutex lists_ida_mutex;
+static DEFINE_IDA(lists_ida);
+
+struct ncr_lists *ncr_init_lists(void)
+{
+	struct ncr_lists *lst;
+
+	lst = kmalloc(sizeof(*lst), GFP_KERNEL);
+	if(!lst) {
+		err();
+		return NULL;
+	}
+
+	memset(lst, 0, sizeof(*lst));
+
+	mutex_init(&lists_ida_mutex);
+	mutex_lock(&lists_ida_mutex);
+	/* ida_pre_get() should preallocate enough, and, due to lists_ida_mutex,
+	   nobody else can use the preallocated data.  Therefore the loop
+	   recommended in idr_get_new() documentation is not necessary. */
+	if (ida_pre_get(&lists_ida, GFP_KERNEL) == 0 ||
+	    ida_get_new(&lists_ida, &lst->id) != 0) {
+		mutex_unlock(&lists_ida_mutex);
+		kfree(lst);
+		return NULL;
+	}
+	mutex_unlock(&lists_ida_mutex);
+
+	mutex_init(&lst->key_idr_mutex);
+	idr_init(&lst->key_idr);
+
+	mutex_init(&lst->session_idr_mutex);
+	idr_init(&lst->session_idr);
+
+	return lst;
+}
+
+void ncr_deinit_lists(struct ncr_lists *lst)
+{
+	if(lst) {
+		ncr_key_list_deinit(lst);
+		ncr_sessions_list_deinit(lst);
+
+		mutex_lock(&lists_ida_mutex);
+		ida_remove(&lists_ida, lst->id);
+		mutex_unlock(&lists_ida_mutex);
+
+		kfree(lst);
+	}
+}
+
+void ncr_master_key_reset(void)
+{
+	memset(&master_key, 0, sizeof(master_key));
+}
+
+static int ncr_master_key_set(const struct ncr_master_key_set *st,
+			      struct nlattr *tb[])
+{
+struct audit_buffer *ab;
+int ret;
+
+	/* This will also cause auditing of the syscall, including information
+	   about the process, and success/failure indication.  Note that on
+	   error the AUDIT_CRYPTO_STORAGE_KEY record will be empty. */
+	ab = audit_log_start(current->audit_context, GFP_KERNEL,
+			     AUDIT_CRYPTO_STORAGE_KEY);
+
+	if (current_euid() != 0 && !capable(CAP_SYS_ADMIN)) {
+		err();
+		ret = -EPERM;
+		goto end;
+	}
+
+	if (st->key_size > sizeof(master_key.key.secret.data)) {
+		err();
+		ret = -EINVAL;
+		goto end;
+	}
+
+	if (st->key_size != 16 && st->key_size != 24 && st->key_size != 32) {
+		dprintk(0, KERN_DEBUG, "Master key size must be 16,24 or 32.\n");
+		ret = -EINVAL;
+		goto end;
+	}
+
+	if (master_key.type != NCR_KEY_TYPE_INVALID) {
+		dprintk(0, KERN_DEBUG, "Master key was previously initialized.\n");
+	}
+
+	if (unlikely(copy_from_user(master_key.key.secret.data, st->key,
+				    st->key_size))) {
+		err();
+		ret = -EFAULT;
+		goto end;
+	}
+
+	dprintk(0, KERN_INFO, "Initializing master key.\n");
+	/* Not much we can reveal... */
+	audit_log_format(ab, "key_size=%u", (unsigned)st->key_size);
+
+	master_key.type = NCR_KEY_TYPE_SECRET;
+	master_key.key.secret.size = st->key_size;
+
+	ret = 0;
+
+end:
+	audit_log_end(ab);
+
+	return ret;
+}
+
+int
+ncr_ioctl(struct ncr_lists *lst, unsigned int cmd, unsigned long arg_)
+{
+	void __user *arg = (void __user *)arg_;
+	struct nlattr *tb[NCR_ATTR_MAX + 1];
+	void *attr_buf;
+	int ret;
+
+	if (unlikely(!lst))
+		BUG();
+
+	switch (cmd) {
+#define CASE_(LABEL, STRUCT, FUNCTION, ARGS)				\
+	case (LABEL): {							\
+		struct STRUCT data;					\
+									\
+		attr_buf = NCR_GET_INPUT_ARGS_NO_OUTPUT(&data, tb, arg); \
+		if (IS_ERR(attr_buf)) {					\
+			err();						\
+			return PTR_ERR(attr_buf);			\
+		}							\
+		ret = (FUNCTION)ARGS;					\
+		break;							\
+	}
+#define CASE_NO_OUTPUT(LABEL, STRUCT, FUNCTION)				\
+		CASE_(LABEL, STRUCT, FUNCTION, (lst, &data, tb))
+#define CASE_NO_OUTPUT_COMPAT(LABEL, STRUCT, FUNCTION)			\
+		CASE_(LABEL, STRUCT, FUNCTION, (lst, &data, tb, 0))
+
+	case NCRIO_KEY_INIT:
+		return ncr_key_init(lst);
+	CASE_NO_OUTPUT(NCRIO_KEY_GENERATE, ncr_key_generate, ncr_key_generate);
+	CASE_NO_OUTPUT(NCRIO_KEY_GENERATE_PAIR, ncr_key_generate_pair,
+		       ncr_key_generate_pair);
+	CASE_NO_OUTPUT(NCRIO_KEY_DERIVE, ncr_key_derive, ncr_key_derive);
+	case NCRIO_KEY_GET_INFO: {
+		struct ncr_key_get_info data;
+		struct ncr_out out;
+
+		attr_buf = NCR_GET_INPUT_ARGS(&data, tb, arg);
+		if (IS_ERR(attr_buf)) {
+			err();
+			return PTR_ERR(attr_buf);
+		}
+		ret = NCR_OUT_INIT(&out, &data, arg);
+		if (ret != 0) {
+			err();
+			break;
+		}
+		ret = ncr_key_get_info(lst, &out, &data, tb);
+		ncr_out_free(&out);
+		break;
+	}
+	CASE_NO_OUTPUT(NCRIO_KEY_EXPORT, ncr_key_export, ncr_key_export);
+	CASE_NO_OUTPUT(NCRIO_KEY_IMPORT, ncr_key_import, ncr_key_import);
+	case NCRIO_KEY_DEINIT: {
+		ncr_key_t key;
+
+		ret = get_user(key, (const ncr_key_t __user *)arg);
+		if (unlikely(ret)) {
+			err();
+			return ret;
+		}
+		return ncr_key_deinit(lst, key);
+	}
+	CASE_NO_OUTPUT(NCRIO_KEY_WRAP, ncr_key_wrap, ncr_key_wrap);
+	CASE_NO_OUTPUT(NCRIO_KEY_UNWRAP, ncr_key_unwrap, ncr_key_unwrap);
+	CASE_NO_OUTPUT(NCRIO_KEY_STORAGE_WRAP, ncr_key_storage_wrap,
+		       ncr_key_storage_wrap);
+	CASE_NO_OUTPUT(NCRIO_KEY_STORAGE_UNWRAP, ncr_key_storage_unwrap,
+		       ncr_key_storage_unwrap);
+	CASE_NO_OUTPUT(NCRIO_SESSION_INIT, ncr_session_init, ncr_session_init);
+	CASE_NO_OUTPUT_COMPAT(NCRIO_SESSION_UPDATE, ncr_session_update,
+			      ncr_session_update);
+	CASE_NO_OUTPUT_COMPAT(NCRIO_SESSION_FINAL, ncr_session_final,
+			      ncr_session_final);
+	CASE_NO_OUTPUT_COMPAT(NCRIO_SESSION_ONCE, ncr_session_once,
+			      ncr_session_once);
+	CASE_(NCRIO_MASTER_KEY_SET, ncr_master_key_set, ncr_master_key_set,
+	      (&data, tb));
+	default:
+		return -EINVAL;
+#undef CASE_
+#undef CASE_NO_OUTPUT
+#undef CASE_NO_OUTPUT_COMPAT
+	}
+	kfree(attr_buf);
+	return ret;
+}
+
+#ifdef CONFIG_COMPAT
+struct compat_ncr_key_export {
+	__u32 input_size, output_size;
+	ncr_key_t key;
+	compat_uptr_t buffer;
+	compat_int_t buffer_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_EXPORT _IOWR('c', 209, struct compat_ncr_key_export)
+
+static void convert_ncr_key_export(struct ncr_key_export *new,
+				   const struct compat_ncr_key_export *old)
+{
+	new->key = old->key;
+	new->buffer = compat_ptr(old->buffer);
+	new->buffer_size = old->buffer_size;
+}
+
+struct compat_ncr_key_import {
+	__u32 input_size, output_size;
+	ncr_key_t key;
+	compat_uptr_t data;
+	__u32 data_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_IMPORT _IOWR('c', 210, struct compat_ncr_key_import)
+
+static void convert_ncr_key_import(struct ncr_key_import *new,
+				   const struct compat_ncr_key_import *old)
+{
+	new->key = old->key;
+	new->data = compat_ptr(old->data);
+	new->data_size = old->data_size;
+}
+
+struct compat_ncr_key_wrap {
+	__u32 input_size, output_size;
+	ncr_key_t wrapping_key;
+	ncr_key_t source_key;
+	compat_uptr_t buffer;
+	compat_int_t buffer_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_WRAP _IOWR('c', 250, struct compat_ncr_key_wrap)
+
+static void convert_ncr_key_wrap(struct ncr_key_wrap *new,
+				 const struct compat_ncr_key_wrap *old)
+{
+	new->wrapping_key = old->wrapping_key;
+	new->source_key = old->source_key;
+	new->buffer = compat_ptr(old->buffer);
+	new->buffer_size = old->buffer_size;
+}
+
+struct compat_ncr_key_unwrap {
+	__u32 input_size, output_size;
+	ncr_key_t wrapping_key;
+	ncr_key_t dest_key;
+	compat_uptr_t data;
+	__u32 data_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_UNWRAP _IOWR('c', 251, struct compat_ncr_key_unwrap)
+
+static void convert_ncr_key_unwrap(struct ncr_key_unwrap *new,
+				   const struct compat_ncr_key_unwrap *old)
+{
+	new->wrapping_key = old->wrapping_key;
+	new->dest_key = old->dest_key;
+	new->data = compat_ptr(old->data);
+	new->data_size = old->data_size;
+}
+
+struct compat_ncr_key_storage_wrap {
+	__u32 input_size, output_size;
+	ncr_key_t key;
+	compat_uptr_t buffer;
+	compat_int_t buffer_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_STORAGE_WRAP				\
+	_IOWR('c', 261, struct compat_ncr_key_storage_wrap)
+
+static void convert_ncr_key_storage_wrap(struct ncr_key_storage_wrap *new,
+					 const struct compat_ncr_key_storage_wrap *old)
+{
+	new->key = old->key;
+	new->buffer = compat_ptr(old->buffer);
+	new->buffer_size = old->buffer_size;
+}
+
+struct compat_ncr_key_storage_unwrap {
+	__u32 input_size, output_size;
+	ncr_key_t key;
+	compat_uptr_t data;
+	__u32 data_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_KEY_STORAGE_UNWRAP				\
+	_IOWR('c', 262, struct compat_ncr_key_storage_wrap)
+
+static void convert_ncr_key_storage_unwrap(struct ncr_key_storage_unwrap *new,
+					   const struct compat_ncr_key_storage_unwrap *old)
+{
+	new->key = old->key;
+	new->data = compat_ptr(old->data);
+	new->data_size = old->data_size;
+}
+
+struct compat_ncr_master_key_set {
+	__u32 input_size, output_size;
+	compat_uptr_t key;
+	__u32 key_size;
+	__NL_ATTRIBUTES;
+};
+#define COMPAT_NCRIO_MASTER_KEY_SET				\
+	_IOWR('c', 260, struct compat_ncr_master_key_set)
+
+static void convert_ncr_master_key_set(struct ncr_master_key_set *new,
+				       const struct compat_ncr_master_key_set *old)
+{
+	new->key = compat_ptr(old->key);
+	new->key_size = old->key_size;
+}
+
+long
+ncr_compat_ioctl(struct ncr_lists *lst, unsigned int cmd, unsigned long arg_)
+{
+	void __user *arg = (void __user *)arg_;
+	struct nlattr *tb[NCR_ATTR_MAX + 1];
+	void *attr_buf;
+	int ret;
+
+	if (unlikely(!lst))
+		BUG();
+
+	switch (cmd) {
+	case NCRIO_KEY_INIT:
+	case NCRIO_KEY_GENERATE:
+	case NCRIO_KEY_GENERATE_PAIR:
+	case NCRIO_KEY_DERIVE:
+	case NCRIO_KEY_GET_INFO:
+	case NCRIO_KEY_DEINIT:
+	case NCRIO_SESSION_INIT:
+		return ncr_ioctl(lst, cmd, arg_);
+
+#define CASE_(LABEL, STRUCT, FUNCTION, ARGS)				\
+	case (LABEL): {							\
+		struct compat_##STRUCT old;				\
+		struct STRUCT new;					\
+									\
+		attr_buf = NCR_GET_INPUT_ARGS_NO_OUTPUT(&old, tb, arg);	\
+		if (IS_ERR(attr_buf)) {					\
+			err();						\
+			return PTR_ERR(attr_buf);			\
+		}							\
+		convert_##STRUCT(&new, &old);				\
+		ret = (FUNCTION)ARGS;					\
+		break;							\
+	}
+#define CASE_NO_OUTPUT(LABEL, STRUCT, FUNCTION)			\
+		CASE_(LABEL, STRUCT, FUNCTION, (lst, &new, tb))
+
+#define CASE_COMPAT_ONLY(LABEL, STRUCT, FUNCTION)			\
+	case (LABEL): {							\
+		struct STRUCT data;					\
+									\
+		attr_buf = NCR_GET_INPUT_ARGS_NO_OUTPUT(&data, tb, arg); \
+		if (IS_ERR(attr_buf)) {					\
+			err();						\
+			return PTR_ERR(attr_buf);			\
+		}							\
+		ret = (FUNCTION)(lst, &data, tb, 1);			\
+		break;							\
+	}
+
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_EXPORT, ncr_key_export, ncr_key_export);
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_IMPORT, ncr_key_import, ncr_key_import);
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_WRAP, ncr_key_wrap, ncr_key_wrap);
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_UNWRAP, ncr_key_unwrap, ncr_key_unwrap);
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_STORAGE_WRAP, ncr_key_storage_wrap,
+		       ncr_key_storage_wrap);
+	CASE_NO_OUTPUT(COMPAT_NCRIO_KEY_STORAGE_UNWRAP, ncr_key_storage_unwrap,
+		       ncr_key_storage_unwrap);
+	CASE_COMPAT_ONLY(NCRIO_SESSION_UPDATE, ncr_session_update,
+			 ncr_session_update);
+	CASE_COMPAT_ONLY(NCRIO_SESSION_FINAL, ncr_session_final,
+			 ncr_session_final);
+	CASE_COMPAT_ONLY(NCRIO_SESSION_ONCE, ncr_session_once,
+			 ncr_session_once);
+	CASE_(COMPAT_NCRIO_MASTER_KEY_SET, ncr_master_key_set,
+	      ncr_master_key_set, (&new, tb));
+	default:
+		return -EINVAL;
+#undef CASE_
+#undef CASE_NO_OUTPUT
+#undef CASE_COMPAT_ONLY
+	}
+	kfree(attr_buf);
+	return ret;
+}
+#endif
+
 int ncr_session_input_data_from_nla(struct ncr_session_input_data *dest,
 				    const struct nlattr *nla, int compat)
 {
Signature · 1 line
1.7.2.1
#33 lore raw

PATCH 19/19 Finally, add the /dev/crypto device.

In reply to Miloslav Trmač
---
 crypto/userspace/cryptodev_main.c |  130 +++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)
crypto/userspace/cryptodev_main.c +130 -0
index a6712db..6ba9bd6 100644
@@ -98,3 +98,133 @@ int __get_userbuf(uint8_t __user *addr, uint32_t len, int write,
 	return 0;
 }
 
+/* ====== /dev/crypto ====== */
+
+static int
+cryptodev_open(struct inode *inode, struct file *filp)
+{
+	struct ncr_lists *ncr;
+	int ret;
+
+	ncr = ncr_init_lists();
+	if (ncr == NULL) {
+		return -ENOMEM;
+	}
+
+	ret = audit_log_crypto_op(AUDIT_CRYPTO_OP_CONTEXT_NEW, ncr->id, -1,
+				  NULL, NULL, -1, NULL, 0, -1, NULL, 0);
+	if (ret < 0) {
+		ncr_deinit_lists(ncr);
+		return ret;
+	}
+
+	filp->private_data = ncr;
+	return 0;
+}
+
+static int
+cryptodev_release(struct inode *inode, struct file *filp)
+{
+	struct ncr_lists *ncr = filp->private_data;
+
+	if (ncr) {
+		audit_log_crypto_op(AUDIT_CRYPTO_OP_CONTEXT_DEL, ncr->id, -1,
+				    NULL, NULL, -1, NULL, 0, -1, NULL, 0);
+		ncr_deinit_lists(ncr);
+		filp->private_data = NULL;
+	}
+
+	return 0;
+}
+
+static int
+cryptodev_ioctl(struct inode *inode, struct file *filp,
+		unsigned int cmd, unsigned long arg)
+{
+	void *ncr = filp->private_data;
+
+	if (unlikely(!ncr))
+		BUG();
+
+	return ncr_ioctl(ncr, cmd, arg);
+}
+
+/* compatibility code for 32bit userlands */
+#ifdef CONFIG_COMPAT
+
+static long
+cryptodev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	void *ncr = file->private_data;
+
+	if (unlikely(!ncr))
+		BUG();
+
+	return ncr_compat_ioctl(ncr, cmd, arg);
+}
+
+#endif /* CONFIG_COMPAT */
+
+static const struct file_operations cryptodev_fops = {
+	.owner = THIS_MODULE,
+	.open = cryptodev_open,
+	.release = cryptodev_release,
+	.ioctl = cryptodev_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl = cryptodev_compat_ioctl,
+#endif /* CONFIG_COMPAT */
+};
+
+static struct miscdevice cryptodev = {
+	.minor = MISC_DYNAMIC_MINOR,
+	.name = "crypto",
+	.fops = &cryptodev_fops,
+};
+
+static int __init
+cryptodev_register(void)
+{
+	int rc;
+
+	ncr_limits_init();
+	ncr_master_key_reset();
+	
+	rc = misc_register (&cryptodev);
+	if (unlikely(rc)) {
+		ncr_limits_deinit();
+		printk(KERN_ERR PFX "registration of /dev/crypto failed\n");
+		return rc;
+	}
+
+	return 0;
+}
+
+static void __exit
+cryptodev_deregister(void)
+{
+	misc_deregister(&cryptodev);
+	ncr_limits_deinit();
+}
+
+/* ====== Module init/exit ====== */
+static int __init init_cryptodev(void)
+{
+	int rc;
+
+	rc = cryptodev_register();
+	if (unlikely(rc))
+		return rc;
+
+	printk(KERN_INFO PFX "driver loaded.\n");
+
+	return 0;
+}
+
+static void __exit exit_cryptodev(void)
+{
+	cryptodev_deregister();
+	printk(KERN_INFO PFX "driver unloaded.\n");
+}
+
+module_init(init_cryptodev);
+module_exit(exit_cryptodev);
Signature · 1 line
1.7.2.1
#34 lore raw
In reply to Miloslav Trmač
Quoted 3 lines · Miloslav Trmač

Hello, following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

What's the goal of exporting the kernel crypto routines to userspace, as opposed to just simply doing the crypto in userspace? Is it to access hardware crypto accelerators? (1) I wasn't aware the kernel crypto routines actually used crypto accelerators, and (2) more often than not, by the time you take into account the time to move the crypto context as well as the data into kernel space and back out, and after you take into account price/performance, most hardware crypto accellerators have marginal performance benefits; in fact, more often than not, it's a lose.

If the goal is access to hardware-escrowed keys, don't we have the TPM interface for that already?

So I'm bit at a list what's the whole point of this patch series. Could you explain that in the documentation, please? Especially for crypto, explaining when something should be used, what the threat model is, etc., is often very important.

Thanks, regards,

- Ted

#35 lore raw
Quoted 7 lines · Ted Ts'o
Quoted 3 lines · Miloslav Trmač

Hello, following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

What's the goal of exporting the kernel crypto routines to userspace, as opposed to just simply doing the crypto in userspace?

This was the goal of the original cryptodev OpenBSD API and the subsequent linux port in http://home.gna.org/cryptodev-linux/. In typical PCs it might even be slower to use such an accelerator in kernel space, but in embedded systems where the hardware version of AES might be 100 times faster than the software it might make sense.

However the design goal of this API is to separate cryptographic operations from the applications. That is applications in userspace can use keys, but the keys cannot be extracted from them, so for example code injection in the web server will not be able to extract the private key of the web service. (this approach is also required for certification of linux on certain fields as Miloslav described in the first post).

The interface is designed in a way that it can be wrapped by a PKCS #11 module and used transparently by other crypto libraries (openssl/nss/gnutls). TPM is quite limited in this respect and cannot fulfill this goal.

Quoted 4 lines

So I'm bit at a list what's the whole point of this patch series. Could you explain that in the documentation, please? Especially for crypto, explaining when something should be used, what the threat model is, etc., is often very important.

A detailed document describing this framework, threats and model is on its way.

best regards,
Nikos

#36 lore raw
Quoted 6 lines · Nikos Mavrogiannopoulos
Quoted 5 lines · Ted Ts'o
Quoted 3 lines · Miloslav Trmač

Hello, following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

Quoted 8 lines
Quoted 2 lines

What's the goal of exporting the kernel crypto routines to userspace, as opposed to just simply doing the crypto in userspace?

This was the goal of the original cryptodev OpenBSD API and the subsequent linux port in http://home.gna.org/cryptodev-linux/. In typical PCs it might even be slower to use such an accelerator in kernel space, but in embedded systems where the hardware version of AES might be 100 times faster than the software it might make sense.

OK, but I hope that in that case, we don't go encouraging applications to use the /dev/crypto API directly. I know a number of distributions have been standardizing on NSS as the library that all of their applications will use, such that by simply configuring libnss differently, the crypto can either be done in userspace, or it can be done in hardware, either for crypto acceleration purposes or for when the key is locked inside hardware can only be used with appropriate authentication to encrypt or sign data passed to the hardware device.

If you encourage applications to use /dev/crypto directly, then either
(a) they will be much slower on PC's, or (b) the applications will need to be rewritten when they are moved between your embedded devices and standard PC's.

- Ted

#37 lore raw
Quoted 16 lines · Ted Ts'o
Quoted 6 lines · Nikos Mavrogiannopoulos
Quoted 5 lines · Ted Ts'o
Quoted 3 lines · Miloslav Trmač

Hello, following is a patchset providing an user-space interface to the kernel crypto API. It is based on the older, BSD-compatible, implementation, but the user-space interface is different.

Quoted 8 lines
Quoted 2 lines

What's the goal of exporting the kernel crypto routines to userspace, as opposed to just simply doing the crypto in userspace?

This was the goal of the original cryptodev OpenBSD API and the subsequent linux port in http://home.gna.org/cryptodev-linux/. In typical PCs it might even be slower to use such an accelerator in kernel space, but in embedded systems where the hardware version of AES might be 100 times faster than the software it might make sense.

Quoted 8 lines

OK, but I hope that in that case, we don't go encouraging applications to use the /dev/crypto API directly. I know a number of distributions have been standardizing on NSS as the library that all of their applications will use, such that by simply configuring libnss differently, the crypto can either be done in userspace, or it can be done in hardware, either for crypto acceleration purposes or for when the key is locked inside hardware can only be used with appropriate authentication to encrypt or sign data passed to the hardware device.

Yes, this exactly is the plan. All the major crypto libraries - NSS, OpenSSL, libgcrypt - are going to be patched to use the kernel API in case they are configured to. By default they will still be using their internal implementation of the cryptographic algorithms. Of course there still might be some applications (for example glibc libcrypt password hashing) that decide to use the kernel interface directly, but these will be a very small minority I think.

Signature · 3 lines
Tomas Mraz
No matter how far down the wrong road you've gone, turn back.
                                              Turkish proverb
#38 lore raw
In reply to Miloslav Trmač
Quoted 4 lines · On Friday 20 August 2010 10:45:43 Miloslav Trmač wrote:

Major changes since the previous post:
* "struct nlattr"-based extensible attributes used for extensibility of most operations, both for input and output attributes

The API here looks overly complex resulting from the use of a combination of ioctl and netlink. If your interface cannot be easily expressed using simple (no indirect pointers or variable-length fields please) ioctl and read/write operations, why not go all the way and turn the interface into a netlink facility?

Quoted 1 line

* Full compat_ioctl implementation

New drivers should be written to *avoid* compat_ioctl calls, using only very simple fixed-length data structures as ioctl commands.

Quoted 1 line

* Version number added to the data format used when wrapping keys for storage

Again, wrong direction. If you think you need a version number, the interface is probably not ready for inclusion yet. Make sure it is simple enough that you don't run into the case where you have to make incompatible changes that require API versioning.

Quoted 3 lines

The libtom* patches will probably still be too large for the mailing list; the whole patch set is also available at
http://people.redhat.com/mitr/cryptodev-ncr/v2/ .

They actually seem to have made it to the list. However, the more signficant problem is the amount of code added to a security module. 20000 lines of code that is essentially a user-level library moved into kernel space can open up so many possible holes that you end up with a less secure (and slower) setup in the end than just doing everything in user space.

Quoted 10 lines

Original patch set description follows.

These are the major differences compared to the BSD-like interface:

* The API supports key storage and management inside the kernel. An application can thus ask the kernel to generate a key; the key is then referenced via an integer identifier, and the application can be prevented from accessing the raw key data. Such a key can, if so configured, still be wrapped for key transport to the recipient of the message, and unwrapped by the recipient.

As Kyle mentioned, we already have a key management API in the kernel. I think you should make a better effort of interfacing with that and adding features you need to it, like a way to prevent the kernel from handing out keys as you mentioned in your reply.

Quoted 4 lines

An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

I think you will have to back that statement by measurements. There are reasonably fast ways to do IPC and the interface you suggest to put in the kernel does not exactly look tuned for performance.

Quoted 3 lines

* FIPS-140-3 calls out for cryptographic functions to be non-debuggable (ptrace) meaning that you cannot get to the key material. The solution is the same as above.

We have kgdb, kdb, qemu gdbserver, tracing and more things that would very much make your code debuggable.

OTOH, disabling ptrace with a root-only prctl should be an easy thing to implement if there is a use case for it.

Quoted 4 lines

Other advantages to having kernel crypto available to user space:

* User space will be able to take advantage of kernel drivers for hardware crypto accelerators.

I can see this as a good reason to put a proper interface for asymmetric crypto into the kernel. There are PCI cards and other things that are supported using ad-hoc interfaces right now. It would be wonderful if someone could clean that up and create a simple common interface that covers all the hardware variants.
But that does not justify putting a software implementation into the kernel.

For symmetric crypto and hashing hardware acceleration is typically implemented as CPU instructions anyway and available to user space already, without the need for kernel support.

Quoted 3 lines

* glibc, which in some configurations links to libfreebl3.so for hashes necessary for crypt(), will be able to use the kernel implementation; this means one less library to load and dynamically link for each such process.

If you are really worried about library load times, you can probably create a patch that statically links libfreebl3 into glibc.

Arnd

#39 lore raw
Quoted 10 lines · Arnd Bergmann
Quoted 4 lines · On Friday 20 August 2010 10:45:43 Miloslav Trmač wrote:

Major changes since the previous post:
* "struct nlattr"-based extensible attributes used for extensibility of most operations, both for input and output attributes

The API here looks overly complex resulting from the use of a combination of ioctl and netlink. If your interface cannot be easily expressed using simple (no indirect pointers or variable-length fields please) ioctl and read/write operations, why not go all the way and turn the interface into a netlink facility?

I believe that this is the result of the discussion in the version 1 of the proposal. The original API was specified with ioctls only.

Quoted 3 lines
Quoted 1 line

* Full compat_ioctl implementation

New drivers should be written to *avoid* compat_ioctl calls, using only very simple fixed-length data structures as ioctl commands.

There are cases where this cannot be easily done, when say pointers are involved. IMHO forcing pointers to be u64 or u32 is dirtier than using the compat interface.

Quoted 5 lines
Quoted 1 line

* Version number added to the data format used when wrapping keys for storage

Again, wrong direction. If you think you need a version number, the interface is probably not ready for inclusion yet. Make sure it is simple enough that you don't run into the case where you have to make incompatible changes that require API versioning.

Note that the version number is not to the interface but to data that are intended for storage. It is desirable to have such a version there.

Quoted 8 lines
Quoted 3 lines

The libtom* patches will probably still be too large for the mailing list; the whole patch set is also available at
http://people.redhat.com/mitr/cryptodev-ncr/v2/ .

They actually seem to have made it to the list. However, the more signficant problem is the amount of code added to a security module. 20000 lines of code that is essentially a user-level library moved into kernel space can open up so many possible holes that you end up with a less secure (and slower) setup in the end than just doing everything in user space.

The same argument could apply to an other algorithm in the kernel such as deflate, lzma, AES etc. There are cases that the benefits outweigh the risks of adding them. I believe this is such a case.

Quoted 11 lines
Quoted 7 lines

These are the major differences compared to the BSD-like interface:
* The API supports key storage and management inside the kernel. An application can thus ask the kernel to generate a key; the key is then referenced via an integer identifier, and the application can be prevented from accessing the raw key data. Such a key can, if so configured, still be wrapped for key transport to the recipient of the message, and unwrapped by the recipient.

As Kyle mentioned, we already have a key management API in the kernel. I think you should make a better effort of interfacing with that and adding features you need to it, like a way to prevent the kernel from handing out keys as you mentioned in your reply.

Note that the NCR does not do key management. Integrating with the key management API could be nice, but it is not something critical and is not duplicating code or efforts in any way.

Quoted 7 lines
Quoted 4 lines

An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

I think you will have to back that statement by measurements. There are reasonably fast ways to do IPC and the interface you suggest to put in the kernel does not exactly look tuned for performance.

This is an alternative design. There quite some reasons against that, such as the auditing features. For me the main reason was that there was no way to make it as fast (zero-copy) as this design, for the requirements we had (interface with existing crypto libraries through pkcs11). Zero-copy is important since crypto operations might involve large chunks of data.

Quoted 8 lines
Quoted 3 lines

* FIPS-140-3 calls out for cryptographic functions to be non-debuggable (ptrace) meaning that you cannot get to the key material. The solution is the same as above.

We have kgdb, kdb, qemu gdbserver, tracing and more things that would very much make your code debuggable.
OTOH, disabling ptrace with a root-only prctl should be an easy thing to implement if there is a use case for it.

You are right. Debugging by the administrator was not an issue. Only users should be prevented from that. It should have been mentioned.

regards,
Nikos

#40 lore raw
Quoted 10 lines · On Sunday 22 August 2010 09:52:14 Nikos Mavrogiannopoulos wrote:
Quoted 5 lines · Arnd Bergmann
Quoted 1 line · On Friday 20 August 2010 10:45:43 Miloslav Trmač wrote:

* Full compat_ioctl implementation

New drivers should be written to *avoid* compat_ioctl calls, using only very simple fixed-length data structures as ioctl commands.

There are cases where this cannot be easily done, when say pointers are involved. IMHO forcing pointers to be u64 or u32 is dirtier than using the compat interface.

Yes, but that only means you should avoid pointers in data structures that are passed to ioctl. Ideally, you would use ioctl to control the device while you use read and write to pass actual bits of data.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

* Version number added to the data format used when wrapping keys for storage

Again, wrong direction. If you think you need a version number, the interface is probably not ready for inclusion yet. Make sure it is simple enough that you don't run into the case where you have to make incompatible changes that require API versioning.

Note that the version number is not to the interface but to data that are intended for storage. It is desirable to have such a version there.

ok, makes sense.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

The libtom* patches will probably still be too large for the mailing list; the whole patch set is also available at
http://people.redhat.com/mitr/cryptodev-ncr/v2/ .

They actually seem to have made it to the list. However, the more signficant problem is the amount of code added to a security module. 20000 lines of code that is essentially a user-level library moved into kernel space can open up so many possible holes that you end up with a less secure (and slower) setup in the end than just doing everything in user space.

Quoted 3 lines

The same argument could apply to an other algorithm in the kernel such as deflate, lzma, AES etc. There are cases that the benefits outweigh the risks of adding them. I believe this is such a case.

The algorithms that are in the kernel already are there specifically because they are used by the kernel itself, which is an entirely different story.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

I think you will have to back that statement by measurements. There are reasonably fast ways to do IPC and the interface you suggest to put in the kernel does not exactly look tuned for performance.

Quoted 6 lines

This is an alternative design. There quite some reasons against that, such as the auditing features. For me the main reason was that there was no way to make it as fast (zero-copy) as this design, for the requirements we had (interface with existing crypto libraries through pkcs11). Zero-copy is important since crypto operations might involve large chunks of data.

You mean using a shared memory segment would not be possible without changing the libpkcs11 interface?

Arnd

#41 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 6 lines

This is an alternative design. There quite some reasons against that, such as the auditing features. For me the main reason was  that there was no way to make it as fast (zero-copy) as this design, for the requirements we had (interface with existing crypto libraries through pkcs11). Zero-copy is important since crypto operations might involve large chunks of data.

You mean using a shared memory segment would not be possible without changing the libpkcs11 interface?

Indeed. The pkcs11 backend would have to copy the data to the shared segment, thus high-performance applications requiring zero-copy, would avoid to use this interface. Moreover if more than one applications are using the interface, the shared segment it is going to be a bottleneck. Having multiple shared segments might help, but I don't know how practical is something like that with the posix ipc.

regards,
Nikos

#42 lore raw
In reply to Miloslav Trmač

Hi!

Quoted 4 lines

Motivations for the extensions: governments are asking for more security features in the operating systems they procure, which make user-space implementations impractical. A few examples:

Quoted 9 lines

* Advanced crypto module for OSPP for Common Criteria requires OS services implementing several low-level crypto algorithms (e.g. AES, RSA). This requires the separation of crypto services from the consumer of those services. (The threat model is that apps tend to have more vulnerabilities than libraries and compromise of the app will lead to the ability to access key material.) An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

Hmm, root daemon seems like a way to go. You already do the switch into the kernel... and "IPC is slow" is not good enough reason to put everything in kernel. Plus, you should be able to get better usage of multicore with daemon.

Numbers?
Pavel

Quoted 29 lines

* FIPS-140-3 calls out for cryptographic functions to be non-debuggable (ptrace) meaning that you cannot get to the key material. The solution is the same as above.

* GPOSPP requires auditing for crypto events (so does FIPS-140 level 2 cert). To do this you need any crypto to have CAP_AUDIT_WRITE permissions which means making everything that links to openssl, libgcrypt, or nss setuid root. Making firefox and 400 other applications setuid root is a non-starter. So, the solution is again to use crypto in the kernel where auditing needs no special permissions.

Other advantages to having kernel crypto available to user space:

* User space will be able to take advantage of kernel drivers for hardware crypto accelerators.

* glibc, which in some configurations links to libfreebl3.so for hashes necessary for crypt(), will be able to use the kernel implementation; this means one less library to load and dynamically link for each such process.

The code is derived from the original cryptodev-linux patch set; most of the new implementation was written by Nikos Mavrogiannopoulos <[email protected]>. Attributions are included in the respective source files.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#43 lore raw
Quoted 16 lines · Pavel Machek

Hi!

Quoted 4 lines

Motivations for the extensions: governments are asking for more security features in the operating systems they procure, which make user-space implementations impractical. A few examples:

Quoted 9 lines

* Advanced crypto module for OSPP for Common Criteria requires OS services implementing several low-level crypto algorithms (e.g. AES, RSA). This requires the separation of crypto services from the consumer of those services. (The threat model is that apps tend to have more vulnerabilities than libraries and compromise of the app will lead to the ability to access key material.) An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

Quoted 4 lines

Hmm, root daemon seems like a way to go. You already do the switch into the kernel... and "IPC is slow" is not good enough reason to put everything in kernel. Plus, you should be able to get better usage of multicore with daemon.

Actually not, and the arguments why multicore would not be really used better anyway were stated here as well. If an application needs some cryptography function in most of the cases it has to wait for the operation to finish before it can proceed further. To use asynchronous crypto interfaces efficiently would require serious redesign and rewrite of the existing applications which is nowhere near to be accomplished. In case of applications where the benefits of asynchronous crypto would be obvious and easily utilized it is quite easier just to split the threads for crypto processing and the rest of the application directly inside the application process.

Signature · 3 lines
Tomas Mraz
No matter how far down the wrong road you've gone, turn back.
                                              Turkish proverb
#44 lore raw
In reply to Pavel Machek
Quoted 10 lines · ----- "Pavel Machek
Quoted 9 lines

* Advanced crypto module for OSPP for Common Criteria requires OS services implementing several low-level crypto algorithms (e.g. AES, RSA). This requires the separation of crypto services from the consumer of those services. (The threat model is that apps tend to have more vulnerabilities than libraries and compromise of the app will lead to the ability to access key material.) An user-space library is not separated, options are a) root running daemon that does crypto, but this would be slow due to context switches, scheduler mismatching and all the IPC overhead and b) use crypto that is in the kernel.

Quoted 6 lines

Hmm, root daemon seems like a way to go. You already do the switch into the kernel... and "IPC is slow" is not good enough reason to put everything in kernel. Plus, you should be able to get better usage of multicore with daemon.

Numbers?

Posted here before - adding a single syscall (to the one existing ioctl()) adds a 20% performance penalty when encrypting 256 bytes using cbc(aes). Even with a 32KB block the penalty is 2.7%, and lots of crypto is done on quite small data sizes.

Inter-process IPC would have even higher overhead.

Using more cores is actually easier with the kernel implementation, where each crypto operation runs in the context of the calling task. This naturally scales to the number of concurrent tasks; as Tomas has said, none of the existing crypto APIs naturally support parallel operations started by a single thread, so any performance advantage gained through this is unrealistic. A daemon would have to explicitly create a thread for each core (and watch for CPU hotplug?), and the incoming requests would have to be distributed among the threads (with system-wide lock contention on the request queue/pipe/socket/whatever).
Mirek