LCOV - code coverage report
Current view: top level - shishi/lib - crypto-ctx.c (source / functions) Hit Total Coverage
Test: GNU Shishi Lines: 35 35 100.0 %
Date: 2010-05-20 Functions: 4 4 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 4 6 66.7 %

           Branch data     Line data    Source code
       1                 :            : /* crypto-ctx.c   high-level crypto functions
       2                 :            :  * Copyright (C) 2002, 2003, 2007, 2010  Simon Josefsson
       3                 :            :  *
       4                 :            :  * This file is part of Shishi.
       5                 :            :  *
       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.
      10                 :            :  *
      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.
      15                 :            :  *
      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
      20                 :            :  *
      21                 :            :  */
      22                 :            : 
      23                 :            : #include "internal.h"
      24                 :            : 
      25                 :            : struct Shishi_crypto
      26                 :            : {
      27                 :            :   Shishi *handle;
      28                 :            :   Shishi_key *key;
      29                 :            :   int keyusage;
      30                 :            :   int32_t etype;
      31                 :            :   char *iv;
      32                 :            :   size_t ivlen;
      33                 :            : };
      34                 :            : 
      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.
      43                 :            :  *
      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.
      50                 :            :  *
      51                 :            :  * When the application no longer need to use the context, it should
      52                 :            :  * deallocate resources associated with it by calling
      53                 :            :  * shishi_crypto_close().
      54                 :            :  *
      55                 :            :  * Return value: Return a newly allocated crypto context.
      56                 :            :  **/
      57                 :            : Shishi_crypto *
      58                 :         20 : shishi_crypto (Shishi * handle,
      59                 :            :                Shishi_key * key,
      60                 :            :                int keyusage, int32_t etype, const char *iv, size_t ivlen)
      61                 :            : {
      62                 :            :   Shishi_crypto *ctx;
      63                 :            :   int rc;
      64                 :            : 
      65                 :         20 :   ctx = xmalloc (sizeof (*ctx));
      66                 :            : 
      67                 :         20 :   rc = shishi_key (handle, &ctx->key);
      68                 :            :   /* XXX handle rc, or rather:
      69                 :            :      change shishi_key() to return key instead of int. */
      70                 :         20 :   shishi_key_copy (ctx->key, key);
      71                 :            : 
      72                 :         20 :   ctx->handle = handle;
      73                 :         20 :   ctx->keyusage = keyusage;
      74                 :         20 :   ctx->etype = etype;
      75         [ +  + ]:         20 :   if (iv)
      76                 :         16 :     ctx->iv = xmemdup (iv, ivlen);
      77                 :            :   else
      78                 :          4 :     ctx->iv = NULL;
      79                 :         20 :   ctx->ivlen = ivlen;
      80                 :            : 
      81                 :         20 :   return ctx;
      82                 :            : }
      83                 :            : 
      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.
      91                 :            :  *
      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.
      95                 :            :  *
      96                 :            :  * When the application no longer need to use the context, it should
      97                 :            :  * deallocate resources associated with it by calling
      98                 :            :  * shishi_crypto_close().
      99                 :            :  *
     100                 :            :  * Return value: Returns %SHISHI_OK iff successful.
     101                 :            :  **/
     102                 :            : int
     103                 :        336 : shishi_crypto_encrypt (Shishi_crypto * ctx,
     104                 :            :                        const char *in, size_t inlen,
     105                 :            :                        char **out, size_t * outlen)
     106                 :            : {
     107                 :        336 :   char *ivout = NULL;
     108                 :            :   size_t ivoutlen;
     109                 :            :   int rc;
     110                 :            : 
     111                 :        336 :   rc = shishi_encrypt_ivupdate_etype (ctx->handle, ctx->key, ctx->keyusage,
     112                 :        336 :                                       ctx->etype, ctx->iv, ctx->ivlen,
     113                 :            :                                       &ivout, &ivoutlen,
     114                 :            :                                       in, inlen, out, outlen);
     115         [ +  - ]:        336 :   if (rc == SHISHI_OK)
     116                 :            :     {
     117                 :        336 :       free (ctx->iv);
     118                 :        336 :       ctx->iv = ivout;
     119                 :        336 :       ctx->ivlen = ivoutlen;
     120                 :            :     }
     121                 :            : 
     122                 :        336 :   return rc;
     123                 :            : }
     124                 :            : 
     125                 :            : /**
     126                 :            :  * shishi_crypto_decrypt:
     127                 :            :  * @ctx: crypto context as returned by shishi_crypto().
     128                 :            :  * @in: input array with data to decrypt.
     129                 :            :  * @inlen: size of input array with data to decrypt.
     130                 :            :  * @out: output array with newly allocated decrypted data.
     131                 :            :  * @outlen: output variable with size of newly allocated output array.
     132                 :            :  *
     133                 :            :  * Decrypt data, using information (e.g., key and initialization
     134                 :            :  * vector) from context.  The IV is updated inside the context after
     135                 :            :  * this call.
     136                 :            :  *
     137                 :            :  * When the application no longer need to use the context, it should
     138                 :            :  * deallocate resources associated with it by calling
     139                 :            :  * shishi_crypto_close().
     140                 :            :  *
     141                 :            :  * Return value: Returns %SHISHI_OK iff successful.
     142                 :            :  **/
     143                 :            : int
     144                 :        336 : shishi_crypto_decrypt (Shishi_crypto * ctx,
     145                 :            :                        const char *in, size_t inlen,
     146                 :            :                        char **out, size_t * outlen)
     147                 :            : {
     148                 :        336 :   char *ivout = NULL;
     149                 :            :   size_t ivoutlen;
     150                 :            :   int rc;
     151                 :            : 
     152                 :        336 :   rc = shishi_decrypt_ivupdate_etype (ctx->handle, ctx->key, ctx->keyusage,
     153                 :        336 :                                       ctx->etype, ctx->iv, ctx->ivlen,
     154                 :            :                                       &ivout, &ivoutlen,
     155                 :            :                                       in, inlen, out, outlen);
     156         [ +  - ]:        336 :   if (rc == SHISHI_OK)
     157                 :            :     {
     158                 :        336 :       free (ctx->iv);
     159                 :        336 :       ctx->iv = ivout;
     160                 :        336 :       ctx->ivlen = ivoutlen;
     161                 :            :     }
     162                 :            : 
     163                 :        336 :   return rc;
     164                 :            : }
     165                 :            : 
     166                 :            : /**
     167                 :            :  * shishi_crypto_close:
     168                 :            :  * @ctx: crypto context as returned by shishi_crypto().
     169                 :            :  *
     170                 :            :  * Deallocate resources associated with the crypto context.
     171                 :            :  **/
     172                 :            : void
     173                 :         20 : shishi_crypto_close (Shishi_crypto * ctx)
     174                 :            : {
     175                 :         20 :   shishi_key_done (ctx->key);
     176                 :         20 :   free (ctx->iv);
     177                 :         20 :   free (ctx);
     178                 :         20 : }

Generated by: LCOV version 1.8