No need for getline.h.
[shishi.git] / lib / crypto-ctx.c
blob8a48505299415462e2fea56de8dc6a5ef96258cd
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
23 #include "internal.h"
25 struct Shishi_crypto
27 Shishi *handle;
28 Shishi_key *key;
29 int keyusage;
30 int32_t etype;
31 char *iv;
32 size_t ivlen;
35 /**
36 * shishi_crypto:
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.
56 **/
57 Shishi_crypto *
58 shishi_crypto (Shishi * handle,
59 Shishi_key * key,
60 int keyusage, int32_t etype, const char *iv, size_t ivlen)
62 Shishi_crypto *ctx;
63 int rc;
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);
72 ctx->handle = handle;
73 ctx->keyusage = keyusage;
74 ctx->etype = etype;
75 if (iv)
76 ctx->iv = xmemdup (iv, ivlen);
77 else
78 ctx->iv = NULL;
79 ctx->ivlen = ivlen;
81 return ctx;
84 /**
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
94 * this call.
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)
107 char *ivout = NULL;
108 size_t ivoutlen;
109 int rc;
111 rc = shishi_encrypt_ivupdate_etype (ctx->handle, ctx->key, ctx->keyusage,
112 ctx->etype, ctx->iv, ctx->ivlen,
113 &ivout, &ivoutlen,
114 in, inlen, out, outlen);
115 if (rc == SHISHI_OK)
117 if (ctx->iv)
118 free (ctx->iv);
119 ctx->iv = ivout;
120 ctx->ivlen = ivoutlen;
123 return rc;
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
136 * this call.
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)
149 char *ivout = NULL;
150 size_t ivoutlen;
151 int rc;
153 rc = shishi_decrypt_ivupdate_etype (ctx->handle, ctx->key, ctx->keyusage,
154 ctx->etype, ctx->iv, ctx->ivlen,
155 &ivout, &ivoutlen,
156 in, inlen, out, outlen);
157 if (rc == SHISHI_OK)
159 if (ctx->iv)
160 free (ctx->iv);
161 ctx->iv = ivout;
162 ctx->ivlen = ivoutlen;
165 return rc;
169 * shishi_crypto_close:
170 * @ctx: crypto context as returned by shishi_crypto().
172 * Deallocate resources associated with the crypto context.
174 void
175 shishi_crypto_close (Shishi_crypto * ctx)
177 shishi_key_done (ctx->key);
178 if (ctx->iv)
179 free (ctx->iv);
180 free (ctx);