gnutls_cipher_int.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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;
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
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 }