1 /* crypto-ctx.c high-level crypto functions
2 * Copyright (C) 2002, 2003, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 * Floor, Boston, MA 02110-1301, USA
37 * @handle: shishi handle as allocated by shishi_init().
38 * @key: key to encrypt with.
39 * @keyusage: integer specifying what this key will encrypt/decrypt.
40 * @etype: integer specifying what cipher to use.
41 * @iv: input array with initialization vector
42 * @ivlen: size of input array with initialization vector.
44 * Initialize a crypto context. This store a key, keyusage,
45 * encryption type and initialization vector in a "context", and the
46 * caller can then use this context to perform encryption via
47 * shishi_crypto_encrypt() and decryption via shishi_crypto_encrypt()
48 * without supplying all those details again. The functions also
49 * takes care of propagating the IV between calls.
51 * When the application no longer need to use the context, it should
52 * deallocate resources associated with it by calling
53 * shishi_crypto_done().
55 * Return value: Return a newly allocated crypto context.
58 shishi_crypto (Shishi
* handle
,
60 int keyusage
, int32_t etype
, const char *iv
, size_t ivlen
)
65 ctx
= xmalloc (sizeof (*ctx
));
67 rc
= shishi_key (handle
, &ctx
->key
);
68 /* XXX handle rc, or rather:
69 change shishi_key() to return key instead of int. */
70 shishi_key_copy (ctx
->key
, key
);
73 ctx
->keyusage
= keyusage
;
76 ctx
->iv
= xmemdup (iv
, ivlen
);
85 * shishi_crypto_encrypt:
86 * @ctx: crypto context as returned by shishi_crypto().
87 * @in: input array with data to encrypt.
88 * @inlen: size of input array with data to encrypt.
89 * @out: output array with newly allocated encrypted data.
90 * @outlen: output variable with size of newly allocated output array.
92 * Encrypt data, using information (e.g., key and initialization
93 * vector) from context. The IV is updated inside the context after
96 * When the application no longer need to use the context, it should
97 * deallocate resources associated with it by calling
98 * shishi_crypto_done().
100 * Return value: Returns %SHISHI_OK iff successful.
103 shishi_crypto_encrypt (Shishi_crypto
* ctx
,
104 const char *in
, size_t inlen
,
105 char **out
, size_t * outlen
)
111 rc
= shishi_encrypt_ivupdate_etype (ctx
->handle
, ctx
->key
, ctx
->keyusage
,
112 ctx
->etype
, ctx
->iv
, ctx
->ivlen
,
114 in
, inlen
, out
, outlen
);
120 ctx
->ivlen
= ivoutlen
;
127 * shishi_crypto_decrypt:
128 * @ctx: crypto context as returned by shishi_crypto().
129 * @in: input array with data to decrypt.
130 * @inlen: size of input array with data to decrypt.
131 * @out: output array with newly allocated decrypted data.
132 * @outlen: output variable with size of newly allocated output array.
134 * Decrypt data, using information (e.g., key and initialization
135 * vector) from context. The IV is updated inside the context after
138 * When the application no longer need to use the context, it should
139 * deallocate resources associated with it by calling
140 * shishi_crypto_done().
142 * Return value: Returns %SHISHI_OK iff successful.
145 shishi_crypto_decrypt (Shishi_crypto
* ctx
,
146 const char *in
, size_t inlen
,
147 char **out
, size_t * outlen
)
153 rc
= shishi_decrypt_ivupdate_etype (ctx
->handle
, ctx
->key
, ctx
->keyusage
,
154 ctx
->etype
, ctx
->iv
, ctx
->ivlen
,
156 in
, inlen
, out
, outlen
);
162 ctx
->ivlen
= ivoutlen
;
169 * shishi_crypto_close:
170 * @ctx: crypto context as returned by shishi_crypto().
172 * Deallocate resources associated with the crypto context.
175 shishi_crypto_close (Shishi_crypto
* ctx
)
177 shishi_key_done (ctx
->key
);