1280304Sjkim/*-
2160814Ssimon * ModExp / RSA (with/without KM) plugin API
3160814Ssimon *
4160814Ssimon * The application will load a dynamic library which
5160814Ssimon * exports entrypoint(s) defined in this file.
6160814Ssimon *
7160814Ssimon * This set of entrypoints provides only a multithreaded,
8160814Ssimon * synchronous-within-each-thread, facility.
9160814Ssimon *
10160814Ssimon *
11160814Ssimon * This file is Copyright 1998-2000 nCipher Corporation Limited.
12160814Ssimon *
13160814Ssimon * Redistribution and use in source and binary forms, with opr without
14160814Ssimon * modification, are permitted provided that the following conditions
15160814Ssimon * are met:
16160814Ssimon *
17160814Ssimon * 1. Redistributions of source code must retain the copyright notice,
18160814Ssimon *    this list of conditions, and the following disclaimer.
19160814Ssimon *
20160814Ssimon * 2. Redistributions in binary form must reproduce the above
21160814Ssimon *    copyright notice, this list of conditions, and the following
22160814Ssimon *    disclaimer, in the documentation and/or other materials provided
23160814Ssimon *    with the distribution
24160814Ssimon *
25160814Ssimon * IN NO EVENT SHALL NCIPHER CORPORATION LIMITED (`NCIPHER') AND/OR
26160814Ssimon * ANY OTHER AUTHORS OR DISTRIBUTORS OF THIS FILE BE LIABLE for any
27160814Ssimon * damages arising directly or indirectly from this file, its use or
28160814Ssimon * this licence.  Without prejudice to the generality of the
29160814Ssimon * foregoing: all liability shall be excluded for direct, indirect,
30160814Ssimon * special, incidental, consequential or other damages or any loss of
31160814Ssimon * profits, business, revenue goodwill or anticipated savings;
32160814Ssimon * liability shall be excluded even if nCipher or anyone else has been
33160814Ssimon * advised of the possibility of damage.  In any event, if the
34160814Ssimon * exclusion of liability is not effective, the liability of nCipher
35160814Ssimon * or any author or distributor shall be limited to the lesser of the
36160814Ssimon * price paid and 1,000 pounds sterling. This licence only fails to
37160814Ssimon * exclude or limit liability for death or personal injury arising out
38160814Ssimon * of negligence, and only to the extent that such an exclusion or
39160814Ssimon * limitation is not effective.
40160814Ssimon *
41160814Ssimon * NCIPHER AND THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ALL
42160814Ssimon * AND ANY WARRANTIES (WHETHER EXPRESS OR IMPLIED), including, but not
43160814Ssimon * limited to, any implied warranties of merchantability, fitness for
44160814Ssimon * a particular purpose, satisfactory quality, and/or non-infringement
45160814Ssimon * of any third party rights.
46160814Ssimon *
47160814Ssimon * US Government use: This software and documentation is Commercial
48160814Ssimon * Computer Software and Computer Software Documentation, as defined in
49160814Ssimon * sub-paragraphs (a)(1) and (a)(5) of DFAR 252.227-7014, "Rights in
50160814Ssimon * Noncommercial Computer Software and Noncommercial Computer Software
51160814Ssimon * Documentation."  Use, duplication or disclosure by the Government is
52160814Ssimon * subject to the terms and conditions specified here.
53160814Ssimon *
54160814Ssimon * By using or distributing this file you will be accepting these
55160814Ssimon * terms and conditions, including the limitation of liability and
56160814Ssimon * lack of warranty.  If you do not wish to accept these terms and
57160814Ssimon * conditions, DO NOT USE THE FILE.
58160814Ssimon *
59160814Ssimon *
60160814Ssimon * The actual dynamically loadable plugin, and the library files for
61160814Ssimon * static linking, which are also provided in some distributions, are
62160814Ssimon * not covered by the licence described above.  You should have
63160814Ssimon * received a separate licence with terms and conditions for these
64160814Ssimon * library files; if you received the library files without a licence,
65160814Ssimon * please contact nCipher.
66160814Ssimon *
67160814Ssimon *
68160814Ssimon * $Id: hwcryptohook.h,v 1.1 2002/10/11 17:10:59 levitte Exp $
69160814Ssimon */
70160814Ssimon
71160814Ssimon#ifndef HWCRYPTOHOOK_H
72280304Sjkim# define HWCRYPTOHOOK_H
73160814Ssimon
74280304Sjkim# include <sys/types.h>
75280304Sjkim# include <stdio.h>
76160814Ssimon
77280304Sjkim# ifndef HWCRYPTOHOOK_DECLARE_APPTYPES
78280304Sjkim#  define HWCRYPTOHOOK_DECLARE_APPTYPES 1
79280304Sjkim# endif
80160814Ssimon
81280304Sjkim# define HWCRYPTOHOOK_ERROR_FAILED   -1
82280304Sjkim# define HWCRYPTOHOOK_ERROR_FALLBACK -2
83280304Sjkim# define HWCRYPTOHOOK_ERROR_MPISIZE  -3
84160814Ssimon
85280304Sjkim# if HWCRYPTOHOOK_DECLARE_APPTYPES
86160814Ssimon
87280304Sjkim/*-
88280304Sjkim * These structs are defined by the application and opaque to the
89160814Ssimon * crypto plugin.  The application may define these as it sees fit.
90160814Ssimon * Default declarations are provided here, but the application may
91160814Ssimon *  #define HWCRYPTOHOOK_DECLARE_APPTYPES 0
92160814Ssimon * to prevent these declarations, and instead provide its own
93160814Ssimon * declarations of these types.  (Pointers to them must still be
94160814Ssimon * ordinary pointers to structs or unions, or the resulting combined
95160814Ssimon * program will have a type inconsistency.)
96160814Ssimon */
97160814Ssimontypedef struct HWCryptoHook_MutexValue HWCryptoHook_Mutex;
98160814Ssimontypedef struct HWCryptoHook_CondVarValue HWCryptoHook_CondVar;
99280304Sjkimtypedef struct HWCryptoHook_PassphraseContextValue
100280304Sjkim HWCryptoHook_PassphraseContext;
101160814Ssimontypedef struct HWCryptoHook_CallerContextValue HWCryptoHook_CallerContext;
102160814Ssimon
103280304Sjkim# endif                         /* HWCRYPTOHOOK_DECLARE_APPTYPES */
104160814Ssimon
105280304Sjkim/*-
106280304Sjkim * These next two structs are opaque to the application.  The crypto
107160814Ssimon * plugin will return pointers to them; the caller simply manipulates
108160814Ssimon * the pointers.
109160814Ssimon */
110160814Ssimontypedef struct HWCryptoHook_Context *HWCryptoHook_ContextHandle;
111160814Ssimontypedef struct HWCryptoHook_RSAKey *HWCryptoHook_RSAKeyHandle;
112160814Ssimon
113160814Ssimontypedef struct {
114280304Sjkim    char *buf;
115280304Sjkim    size_t size;
116160814Ssimon} HWCryptoHook_ErrMsgBuf;
117280304Sjkim/*-
118280304Sjkim * Used for error reporting.  When a HWCryptoHook function fails it
119160814Ssimon * will return a sentinel value (0 for pointer-valued functions, or a
120160814Ssimon * negative number, usually HWCRYPTOHOOK_ERROR_FAILED, for
121160814Ssimon * integer-valued ones).  It will, if an ErrMsgBuf is passed, also put
122160814Ssimon * an error message there.
123280304Sjkim *
124160814Ssimon * size is the size of the buffer, and will not be modified.  If you
125160814Ssimon * pass 0 for size you must pass 0 for buf, and nothing will be
126160814Ssimon * recorded (just as if you passed 0 for the struct pointer).
127160814Ssimon * Messages written to the buffer will always be null-terminated, even
128160814Ssimon * when truncated to fit within size bytes.
129160814Ssimon *
130160814Ssimon * The contents of the buffer are not defined if there is no error.
131160814Ssimon */
132160814Ssimon
133160814Ssimontypedef struct HWCryptoHook_MPIStruct {
134280304Sjkim    unsigned char *buf;
135280304Sjkim    size_t size;
136160814Ssimon} HWCryptoHook_MPI;
137280304Sjkim/*-
138280304Sjkim * When one of these is returned, a pointer is passed to the function.
139160814Ssimon * At call, size is the space available.  Afterwards it is updated to
140160814Ssimon * be set to the actual length (which may be more than the space available,
141160814Ssimon * if there was not enough room and the result was truncated).
142160814Ssimon * buf (the pointer) is not updated.
143160814Ssimon *
144160814Ssimon * size is in bytes and may be zero at call or return, but must be a
145160814Ssimon * multiple of the limb size.  Zero limbs at the MS end are not
146160814Ssimon * permitted.
147160814Ssimon */
148160814Ssimon
149280304Sjkim# define HWCryptoHook_InitFlags_FallbackModExp    0x0002UL
150280304Sjkim# define HWCryptoHook_InitFlags_FallbackRSAImmed  0x0004UL
151280304Sjkim/*-
152280304Sjkim * Enable requesting fallback to software in case of problems with the
153160814Ssimon * hardware support.  This indicates to the crypto provider that the
154160814Ssimon * application is prepared to fall back to software operation if the
155160814Ssimon * ModExp* or RSAImmed* functions return HWCRYPTOHOOK_ERROR_FALLBACK.
156160814Ssimon * Without this flag those calls will never return
157160814Ssimon * HWCRYPTOHOOK_ERROR_FALLBACK.  The flag will also cause the crypto
158160814Ssimon * provider to avoid repeatedly attempting to contact dead hardware
159160814Ssimon * within a short interval, if appropriate.
160160814Ssimon */
161160814Ssimon
162280304Sjkim# define HWCryptoHook_InitFlags_SimpleForkCheck   0x0010UL
163280304Sjkim/*-
164280304Sjkim * Without _SimpleForkCheck the library is allowed to assume that the
165160814Ssimon * application will not fork and call the library in the child(ren).
166160814Ssimon *
167160814Ssimon * When it is specified, this is allowed.  However, after a fork
168160814Ssimon * neither parent nor child may unload any loaded keys or call
169160814Ssimon * _Finish.  Instead, they should call exit (or die with a signal)
170160814Ssimon * without calling _Finish.  After all the children have died the
171160814Ssimon * parent may unload keys or call _Finish.
172160814Ssimon *
173160814Ssimon * This flag only has any effect on UN*X platforms.
174160814Ssimon */
175160814Ssimon
176160814Ssimontypedef struct {
177280304Sjkim    unsigned long flags;
178280304Sjkim    void *logstream;            /* usually a FILE*.  See below. */
179280304Sjkim    size_t limbsize;            /* bignum format - size of radix type, must
180280304Sjkim                                 * be power of 2 */
181280304Sjkim    int mslimbfirst;            /* 0 or 1 */
182280304Sjkim    int msbytefirst;            /* 0 or 1; -1 = native */
183280304Sjkim  /*-
184280304Sjkim   * All the callback functions should return 0 on success, or a
185160814Ssimon   * nonzero integer (whose value will be visible in the error message
186160814Ssimon   * put in the buffer passed to the call).
187160814Ssimon   *
188160814Ssimon   * If a callback is not available pass a null function pointer.
189160814Ssimon   *
190160814Ssimon   * The callbacks may not call down again into the crypto plugin.
191160814Ssimon   */
192280304Sjkim  /*-
193280304Sjkim   * For thread-safety.  Set everything to 0 if you promise only to be
194160814Ssimon   * singlethreaded.  maxsimultaneous is the number of calls to
195160814Ssimon   * ModExp[Crt]/RSAImmed{Priv,Pub}/RSA.  If you don't know what to
196160814Ssimon   * put there then say 0 and the hook library will use a default.
197160814Ssimon   *
198160814Ssimon   * maxmutexes is a small limit on the number of simultaneous mutexes
199160814Ssimon   * which will be requested by the library.  If there is no small
200160814Ssimon   * limit, set it to 0.  If the crypto plugin cannot create the
201160814Ssimon   * advertised number of mutexes the calls to its functions may fail.
202160814Ssimon   * If a low number of mutexes is advertised the plugin will try to
203160814Ssimon   * do the best it can.  Making larger numbers of mutexes available
204160814Ssimon   * may improve performance and parallelism by reducing contention
205160814Ssimon   * over critical sections.  Unavailability of any mutexes, implying
206160814Ssimon   * single-threaded operation, should be indicated by the setting
207160814Ssimon   * mutex_init et al to 0.
208160814Ssimon   */
209280304Sjkim    int maxmutexes;
210280304Sjkim    int maxsimultaneous;
211280304Sjkim    size_t mutexsize;
212280304Sjkim    int (*mutex_init) (HWCryptoHook_Mutex *,
213280304Sjkim                       HWCryptoHook_CallerContext * cactx);
214280304Sjkim    int (*mutex_acquire) (HWCryptoHook_Mutex *);
215280304Sjkim    void (*mutex_release) (HWCryptoHook_Mutex *);
216280304Sjkim    void (*mutex_destroy) (HWCryptoHook_Mutex *);
217280304Sjkim   /*-
218280304Sjkim    * For greater efficiency, can use condition vars internally for
219280304Sjkim    * synchronisation.  In this case maxsimultaneous is ignored, but
220280304Sjkim    * the other mutex stuff must be available.  In singlethreaded
221280304Sjkim    * programs, set everything to 0.
222280304Sjkim    */
223280304Sjkim    size_t condvarsize;
224280304Sjkim    int (*condvar_init) (HWCryptoHook_CondVar *,
225280304Sjkim                         HWCryptoHook_CallerContext * cactx);
226280304Sjkim    int (*condvar_wait) (HWCryptoHook_CondVar *, HWCryptoHook_Mutex *);
227280304Sjkim    void (*condvar_signal) (HWCryptoHook_CondVar *);
228280304Sjkim    void (*condvar_broadcast) (HWCryptoHook_CondVar *);
229280304Sjkim    void (*condvar_destroy) (HWCryptoHook_CondVar *);
230280304Sjkim   /*-
231280304Sjkim    * The semantics of acquiring and releasing mutexes and broadcasting
232280304Sjkim    * and waiting on condition variables are expected to be those from
233280304Sjkim    * POSIX threads (pthreads).  The mutexes may be (in pthread-speak)
234280304Sjkim    * fast mutexes, recursive mutexes, or nonrecursive ones.
235280304Sjkim    *
236280304Sjkim    * The _release/_signal/_broadcast and _destroy functions must
237280304Sjkim    * always succeed when given a valid argument; if they are given an
238280304Sjkim    * invalid argument then the program (crypto plugin + application)
239280304Sjkim    * has an internal error, and they should abort the program.
240280304Sjkim    */
241280304Sjkim    int (*getpassphrase) (const char *prompt_info,
242280304Sjkim                          int *len_io, char *buf,
243280304Sjkim                          HWCryptoHook_PassphraseContext * ppctx,
244280304Sjkim                          HWCryptoHook_CallerContext * cactx);
245280304Sjkim   /*-
246280304Sjkim    * Passphrases and the prompt_info, if they contain high-bit-set
247280304Sjkim    * characters, are UTF-8.  The prompt_info may be a null pointer if
248280304Sjkim    * no prompt information is available (it should not be an empty
249280304Sjkim    * string).  It will not contain text like `enter passphrase';
250280304Sjkim    * instead it might say something like `Operator Card for John
251280304Sjkim    * Smith' or `SmartCard in nFast Module #1, Slot #1'.
252280304Sjkim    *
253280304Sjkim    * buf points to a buffer in which to return the passphrase; on
254280304Sjkim    * entry *len_io is the length of the buffer.  It should be updated
255280304Sjkim    * by the callback.  The returned passphrase should not be
256280304Sjkim    * null-terminated by the callback.
257280304Sjkim    */
258280304Sjkim    int (*getphystoken) (const char *prompt_info,
259280304Sjkim                         const char *wrong_info,
260280304Sjkim                         HWCryptoHook_PassphraseContext * ppctx,
261280304Sjkim                         HWCryptoHook_CallerContext * cactx);
262280304Sjkim   /*-
263280304Sjkim    * Requests that the human user physically insert a different
264280304Sjkim    * smartcard, DataKey, etc.  The plugin should check whether the
265280304Sjkim    * currently inserted token(s) are appropriate, and if they are it
266280304Sjkim    * should not make this call.
267280304Sjkim    *
268280304Sjkim    * prompt_info is as before.  wrong_info is a description of the
269280304Sjkim    * currently inserted token(s) so that the user is told what
270280304Sjkim    * something is.  wrong_info, like prompt_info, may be null, but
271280304Sjkim    * should not be an empty string.  Its contents should be
272280304Sjkim    * syntactically similar to that of prompt_info.
273280304Sjkim    */
274280304Sjkim   /*-
275280304Sjkim    * Note that a single LoadKey operation might cause several calls to
276280304Sjkim    * getpassphrase and/or requestphystoken.  If requestphystoken is
277280304Sjkim    * not provided (ie, a null pointer is passed) then the plugin may
278280304Sjkim    * not support loading keys for which authorisation by several cards
279280304Sjkim    * is required.  If getpassphrase is not provided then cards with
280280304Sjkim    * passphrases may not be supported.
281280304Sjkim    *
282280304Sjkim    * getpassphrase and getphystoken do not need to check that the
283280304Sjkim    * passphrase has been entered correctly or the correct token
284280304Sjkim    * inserted; the crypto plugin will do that.  If this is not the
285280304Sjkim    * case then the crypto plugin is responsible for calling these
286280304Sjkim    * routines again as appropriate until the correct token(s) and
287280304Sjkim    * passphrase(s) are supplied as required, or until any retry limits
288280304Sjkim    * implemented by the crypto plugin are reached.
289280304Sjkim    *
290280304Sjkim    * In either case, the application must allow the user to say `no'
291280304Sjkim    * or `cancel' to indicate that they do not know the passphrase or
292280304Sjkim    * have the appropriate token; this should cause the callback to
293280304Sjkim    * return nonzero indicating error.
294280304Sjkim    */
295280304Sjkim    void (*logmessage) (void *logstream, const char *message);
296280304Sjkim   /*-
297280304Sjkim    * A log message will be generated at least every time something goes
298280304Sjkim    * wrong and an ErrMsgBuf is filled in (or would be if one was
299280304Sjkim    * provided).  Other diagnostic information may be written there too,
300280304Sjkim    * including more detailed reasons for errors which are reported in an
301280304Sjkim    * ErrMsgBuf.
302280304Sjkim    *
303280304Sjkim    * When a log message is generated, this callback is called.  It
304280304Sjkim    * should write a message to the relevant logging arrangements.
305280304Sjkim    *
306280304Sjkim    * The message string passed will be null-terminated and may be of arbitrary
307280304Sjkim    * length.  It will not be prefixed by the time and date, nor by the
308280304Sjkim    * name of the library that is generating it - if this is required,
309280304Sjkim    * the logmessage callback must do it.  The message will not have a
310280304Sjkim    * trailing newline (though it may contain internal newlines).
311280304Sjkim    *
312280304Sjkim    * If a null pointer is passed for logmessage a default function is
313280304Sjkim    * used.  The default function treats logstream as a FILE* which has
314280304Sjkim    * been converted to a void*.  If logstream is 0 it does nothing.
315280304Sjkim    * Otherwise it prepends the date and time and library name and
316280304Sjkim    * writes the message to logstream.  Each line will be prefixed by a
317280304Sjkim    * descriptive string containing the date, time and identity of the
318280304Sjkim    * crypto plugin.  Errors on the logstream are not reported
319280304Sjkim    * anywhere, and the default function doesn't flush the stream, so
320280304Sjkim    * the application must set the buffering how it wants it.
321280304Sjkim    *
322280304Sjkim    * The crypto plugin may also provide a facility to have copies of
323280304Sjkim    * log messages sent elsewhere, and or for adjusting the verbosity
324280304Sjkim    * of the log messages; any such facilities will be configured by
325280304Sjkim    * external means.
326280304Sjkim    */
327160814Ssimon} HWCryptoHook_InitInfo;
328160814Ssimon
329160814Ssimontypedef
330280304SjkimHWCryptoHook_ContextHandle HWCryptoHook_Init_t(const HWCryptoHook_InitInfo *
331280304Sjkim                                               initinfo, size_t initinfosize,
332280304Sjkim                                               const HWCryptoHook_ErrMsgBuf *
333280304Sjkim                                               errors,
334280304Sjkim                                               HWCryptoHook_CallerContext *
335280304Sjkim                                               cactx);
336160814Ssimonextern HWCryptoHook_Init_t HWCryptoHook_Init;
337160814Ssimon
338280304Sjkim/*-
339280304Sjkim * Caller should set initinfosize to the size of the HWCryptoHook struct,
340160814Ssimon * so it can be extended later.
341160814Ssimon *
342160814Ssimon * On success, a message for display or logging by the server,
343160814Ssimon * including the name and version number of the plugin, will be filled
344160814Ssimon * in into *errors; on failure *errors is used for error handling, as
345160814Ssimon * usual.
346160814Ssimon */
347160814Ssimon
348280304Sjkim/*-
349280304Sjkim * All these functions return 0 on success, HWCRYPTOHOOK_ERROR_FAILED
350160814Ssimon * on most failures.  HWCRYPTOHOOK_ERROR_MPISIZE means at least one of
351160814Ssimon * the output MPI buffer(s) was too small; the sizes of all have been
352160814Ssimon * set to the desired size (and for those where the buffer was large
353160814Ssimon * enough, the value may have been copied in), and no error message
354160814Ssimon * has been recorded.
355160814Ssimon *
356160814Ssimon * You may pass 0 for the errors struct.  In any case, unless you set
357160814Ssimon * _NoStderr at init time then messages may be reported to stderr.
358160814Ssimon */
359160814Ssimon
360280304Sjkim/*-
361280304Sjkim * The RSAImmed* functions (and key managed RSA) only work with
362160814Ssimon * modules which have an RSA patent licence - currently that means KM
363160814Ssimon * units; the ModExp* ones work with all modules, so you need a patent
364160814Ssimon * licence in the software in the US.  They are otherwise identical.
365160814Ssimon */
366160814Ssimon
367160814Ssimontypedef
368160814Ssimonvoid HWCryptoHook_Finish_t(HWCryptoHook_ContextHandle hwctx);
369160814Ssimonextern HWCryptoHook_Finish_t HWCryptoHook_Finish;
370160814Ssimon/* You must not have any calls going or keys loaded when you call this. */
371160814Ssimon
372160814Ssimontypedef
373160814Ssimonint HWCryptoHook_RandomBytes_t(HWCryptoHook_ContextHandle hwctx,
374160814Ssimon                               unsigned char *buf, size_t len,
375280304Sjkim                               const HWCryptoHook_ErrMsgBuf * errors);
376160814Ssimonextern HWCryptoHook_RandomBytes_t HWCryptoHook_RandomBytes;
377160814Ssimon
378160814Ssimontypedef
379160814Ssimonint HWCryptoHook_ModExp_t(HWCryptoHook_ContextHandle hwctx,
380160814Ssimon                          HWCryptoHook_MPI a,
381160814Ssimon                          HWCryptoHook_MPI p,
382160814Ssimon                          HWCryptoHook_MPI n,
383280304Sjkim                          HWCryptoHook_MPI * r,
384280304Sjkim                          const HWCryptoHook_ErrMsgBuf * errors);
385160814Ssimonextern HWCryptoHook_ModExp_t HWCryptoHook_ModExp;
386160814Ssimon
387160814Ssimontypedef
388160814Ssimonint HWCryptoHook_RSAImmedPub_t(HWCryptoHook_ContextHandle hwctx,
389160814Ssimon                               HWCryptoHook_MPI m,
390160814Ssimon                               HWCryptoHook_MPI e,
391160814Ssimon                               HWCryptoHook_MPI n,
392280304Sjkim                               HWCryptoHook_MPI * r,
393280304Sjkim                               const HWCryptoHook_ErrMsgBuf * errors);
394160814Ssimonextern HWCryptoHook_RSAImmedPub_t HWCryptoHook_RSAImmedPub;
395160814Ssimon
396160814Ssimontypedef
397160814Ssimonint HWCryptoHook_ModExpCRT_t(HWCryptoHook_ContextHandle hwctx,
398160814Ssimon                             HWCryptoHook_MPI a,
399160814Ssimon                             HWCryptoHook_MPI p,
400160814Ssimon                             HWCryptoHook_MPI q,
401160814Ssimon                             HWCryptoHook_MPI dmp1,
402160814Ssimon                             HWCryptoHook_MPI dmq1,
403160814Ssimon                             HWCryptoHook_MPI iqmp,
404280304Sjkim                             HWCryptoHook_MPI * r,
405280304Sjkim                             const HWCryptoHook_ErrMsgBuf * errors);
406160814Ssimonextern HWCryptoHook_ModExpCRT_t HWCryptoHook_ModExpCRT;
407160814Ssimon
408160814Ssimontypedef
409160814Ssimonint HWCryptoHook_RSAImmedPriv_t(HWCryptoHook_ContextHandle hwctx,
410160814Ssimon                                HWCryptoHook_MPI m,
411160814Ssimon                                HWCryptoHook_MPI p,
412160814Ssimon                                HWCryptoHook_MPI q,
413160814Ssimon                                HWCryptoHook_MPI dmp1,
414160814Ssimon                                HWCryptoHook_MPI dmq1,
415160814Ssimon                                HWCryptoHook_MPI iqmp,
416280304Sjkim                                HWCryptoHook_MPI * r,
417280304Sjkim                                const HWCryptoHook_ErrMsgBuf * errors);
418160814Ssimonextern HWCryptoHook_RSAImmedPriv_t HWCryptoHook_RSAImmedPriv;
419160814Ssimon
420280304Sjkim/*-
421280304Sjkim * The RSAImmed* and ModExp* functions may return E_FAILED or
422160814Ssimon * E_FALLBACK for failure.
423160814Ssimon *
424160814Ssimon * E_FAILED means the failure is permanent and definite and there
425160814Ssimon *    should be no attempt to fall back to software.  (Eg, for some
426160814Ssimon *    applications, which support only the acceleration-only
427160814Ssimon *    functions, the `key material' may actually be an encoded key
428160814Ssimon *    identifier, and doing the operation in software would give wrong
429160814Ssimon *    answers.)
430160814Ssimon *
431160814Ssimon * E_FALLBACK means that doing the computation in software would seem
432160814Ssimon *    reasonable.  If an application pays attention to this and is
433160814Ssimon *    able to fall back, it should also set the Fallback init flags.
434160814Ssimon */
435160814Ssimon
436160814Ssimontypedef
437160814Ssimonint HWCryptoHook_RSALoadKey_t(HWCryptoHook_ContextHandle hwctx,
438160814Ssimon                              const char *key_ident,
439280304Sjkim                              HWCryptoHook_RSAKeyHandle * keyhandle_r,
440280304Sjkim                              const HWCryptoHook_ErrMsgBuf * errors,
441280304Sjkim                              HWCryptoHook_PassphraseContext * ppctx);
442160814Ssimonextern HWCryptoHook_RSALoadKey_t HWCryptoHook_RSALoadKey;
443280304Sjkim/*-
444280304Sjkim * The key_ident is a null-terminated string configured by the
445160814Ssimon * user via the application's usual configuration mechanisms.
446160814Ssimon * It is provided to the user by the crypto provider's key management
447160814Ssimon * system.  The user must be able to enter at least any string of between
448160814Ssimon * 1 and 1023 characters inclusive, consisting of printable 7-bit
449160814Ssimon * ASCII characters.  The provider should avoid using
450160814Ssimon * any characters except alphanumerics and the punctuation
451160814Ssimon * characters  _ - + . / @ ~  (the user is expected to be able
452160814Ssimon * to enter these without quoting).  The string may be case-sensitive.
453160814Ssimon * The application may allow the user to enter other NULL-terminated strings,
454160814Ssimon * and the provider must cope (returning an error if the string is not
455160814Ssimon * valid).
456160814Ssimon *
457160814Ssimon * If the key does not exist, no error is recorded and 0 is returned;
458160814Ssimon * keyhandle_r will be set to 0 instead of to a key handle.
459160814Ssimon */
460160814Ssimon
461160814Ssimontypedef
462160814Ssimonint HWCryptoHook_RSAGetPublicKey_t(HWCryptoHook_RSAKeyHandle k,
463280304Sjkim                                   HWCryptoHook_MPI * n,
464280304Sjkim                                   HWCryptoHook_MPI * e,
465280304Sjkim                                   const HWCryptoHook_ErrMsgBuf * errors);
466160814Ssimonextern HWCryptoHook_RSAGetPublicKey_t HWCryptoHook_RSAGetPublicKey;
467280304Sjkim/*-
468280304Sjkim * The crypto plugin will not store certificates.
469160814Ssimon *
470160814Ssimon * Although this function for acquiring the public key value is
471160814Ssimon * provided, it is not the purpose of this API to deal fully with the
472160814Ssimon * handling of the public key.
473160814Ssimon *
474160814Ssimon * It is expected that the crypto supplier's key generation program
475160814Ssimon * will provide general facilities for producing X.509
476160814Ssimon * self-certificates and certificate requests in PEM format.  These
477160814Ssimon * will be given to the user so that they can configure them in the
478160814Ssimon * application, send them to CAs, or whatever.
479160814Ssimon *
480160814Ssimon * In case this kind of certificate handling is not appropriate, the
481160814Ssimon * crypto supplier's key generation program should be able to be
482160814Ssimon * configured not to generate such a self-certificate or certificate
483160814Ssimon * request.  Then the application will need to do all of this, and
484160814Ssimon * will need to store and handle the public key and certificates
485160814Ssimon * itself.
486160814Ssimon */
487160814Ssimon
488160814Ssimontypedef
489160814Ssimonint HWCryptoHook_RSAUnloadKey_t(HWCryptoHook_RSAKeyHandle k,
490280304Sjkim                                const HWCryptoHook_ErrMsgBuf * errors);
491160814Ssimonextern HWCryptoHook_RSAUnloadKey_t HWCryptoHook_RSAUnloadKey;
492160814Ssimon/* Might fail due to locking problems, or other serious internal problems. */
493160814Ssimon
494160814Ssimontypedef
495160814Ssimonint HWCryptoHook_RSA_t(HWCryptoHook_MPI m,
496160814Ssimon                       HWCryptoHook_RSAKeyHandle k,
497280304Sjkim                       HWCryptoHook_MPI * r,
498280304Sjkim                       const HWCryptoHook_ErrMsgBuf * errors);
499160814Ssimonextern HWCryptoHook_RSA_t HWCryptoHook_RSA;
500160814Ssimon/* RSA private key operation (sign or decrypt) - raw, unpadded. */
501160814Ssimon
502280304Sjkim#endif                          /* HWCRYPTOHOOK_H */
503