gnutls_cipher_int.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000, 2004, 2005 Free Software Foundation
00003  *
00004  * Author: Nikos Mavrogiannopoulos
00005  *
00006  * This file is part of GNUTLS.
00007  *
00008  * The GNUTLS library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00021  * USA
00022  *
00023  */
00024 
00025 #include <gnutls_int.h>
00026 #include <gnutls_errors.h>
00027 #include <gnutls_cipher_int.h>
00028 #include <gnutls_datum.h>
00029 
00030 cipher_hd_t
00031 MHD_gtls_cipher_init (enum MHD_GNUTLS_CipherAlgorithm cipher,
00032                       const MHD_gnutls_datum_t * key,
00033                       const MHD_gnutls_datum_t * iv)
00034 {
00035   cipher_hd_t ret = NULL;
00036   int err = GC_INVALID_CIPHER;  /* doesn't matter */
00037 
00038   switch (cipher)
00039     {
00040     case MHD_GNUTLS_CIPHER_AES_128_CBC:
00041       err = MHD_gc_cipher_open (GC_AES128, GC_CBC, &ret);
00042       break;
00043     case MHD_GNUTLS_CIPHER_AES_256_CBC:
00044       err = MHD_gc_cipher_open (GC_AES256, GC_CBC, &ret);
00045       break;
00046     case MHD_GNUTLS_CIPHER_3DES_CBC:
00047       err = MHD_gc_cipher_open (GC_3DES, GC_CBC, &ret);
00048       break;
00049     case MHD_GNUTLS_CIPHER_ARCFOUR_128:
00050       err = MHD_gc_cipher_open (GC_ARCFOUR128, GC_STREAM, &ret);
00051       break;
00052     default:
00053       return NULL;
00054     }
00055 
00056   if (err == 0)
00057     {
00058       MHD_gc_cipher_setkey (ret, key->size, (const char *) key->data);
00059       if (iv->data != NULL && iv->size > 0)
00060         MHD_gc_cipher_setiv (ret, iv->size, (const char *) iv->data);
00061     }
00062   else if (cipher != MHD_GNUTLS_CIPHER_NULL)
00063     {
00064       MHD_gnutls_assert ();
00065       MHD__gnutls_x509_log ("Crypto cipher[%d] error: %d\n", cipher, err);
00066       /* FIXME: MHD_gc_strerror */
00067     }
00068 
00069   return ret;
00070 }
00071 
00072 int
00073 MHD_gtls_cipher_encrypt (cipher_hd_t handle, void *text, int textlen)
00074 {
00075   if (handle != GNUTLS_CIPHER_FAILED)
00076     {
00077       if (MHD_gc_cipher_encrypt_inline (handle, textlen, text) != 0)
00078         {
00079           MHD_gnutls_assert ();
00080           return GNUTLS_E_INTERNAL_ERROR;
00081         }
00082     }
00083   return 0;
00084 }
00085 
00086 int
00087 MHD_gtls_cipher_decrypt (cipher_hd_t handle, void *ciphertext,
00088                          int ciphertextlen)
00089 {
00090   if (handle != GNUTLS_CIPHER_FAILED)
00091     {
00092       if (MHD_gc_cipher_decrypt_inline (handle, ciphertextlen, ciphertext) !=
00093           0)
00094         {
00095           MHD_gnutls_assert ();
00096           return GNUTLS_E_INTERNAL_ERROR;
00097         }
00098     }
00099   return 0;
00100 }
00101 
00102 void
00103 MHD_gnutls_cipher_deinit (cipher_hd_t handle)
00104 {
00105   if (handle != GNUTLS_CIPHER_FAILED)
00106     {
00107       MHD_gc_cipher_close (handle);
00108     }
00109 }

Generated on Fri Feb 27 18:31:19 2009 for GNU libmicrohttpd by  doxygen 1.5.7.1