*** empty log message ***
[gnutls.git] / libextra / opencdk / sym-cipher.c
blob5405b89a16a75067e6c333ff92bd1ec82fe151be
1 /* -*- Mode: C; c-file-style: "bsd" -*-
2 * sym-cipher.c
3 * Copyright (C) 2003 Timo Schulz
5 * This file is part of OpenCDK.
7 * OpenCDK is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * OpenCDK is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with OpenCDK; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <string.h>
25 #include "opencdk.h"
26 #include "main.h"
27 #include "cipher.h"
30 struct cdk_cipher_hd_s {
31 gcry_cipher_hd_t hd;
32 int algo;
35 inline
36 static int cdk_cipher_to_gcry( int algo)
38 switch( algo ) {
39 case CDK_CIPHER_BLOWFISH: return GCRY_CIPHER_BLOWFISH;
40 case CDK_CIPHER_TWOFISH: return GCRY_CIPHER_TWOFISH;
41 case CDK_CIPHER_3DES: return GCRY_CIPHER_3DES;
42 case CDK_CIPHER_CAST5: return GCRY_CIPHER_CAST5;
43 case CDK_CIPHER_AES: return GCRY_CIPHER_AES;
44 case CDK_CIPHER_AES192: return GCRY_CIPHER_AES192;
45 case CDK_CIPHER_AES256: return GCRY_CIPHER_AES256;
46 default: return -1;
50 cdk_cipher_hd_t
51 cdk_cipher_new( int algo, int pgp_sync )
53 cdk_cipher_hd_t hd;
54 unsigned int flags = 0;
55 gcry_error_t err;
57 if( cdk_cipher_test_algo( algo ) )
58 return NULL;
59 hd = cdk_calloc( 1, sizeof * hd );
60 if( !hd )
61 return NULL;
63 if( pgp_sync )
64 flags = GCRY_CIPHER_ENABLE_SYNC;
66 hd->algo = algo;
68 err = gcry_cipher_open( &hd->hd, cdk_cipher_to_gcry(algo),
69 GCRY_CIPHER_MODE_CFB, flags);
70 if( err ) {
71 cdk_free( hd );
72 return NULL;
74 return hd;
78 cdk_cipher_hd_t
79 cdk_cipher_open( int algo, int pgp_sync,
80 const byte * key, size_t keylen,
81 const byte * ivbuf, size_t ivlen )
83 cdk_cipher_hd_t hd;
84 gcry_error_t err;
86 hd = cdk_cipher_new( algo, pgp_sync );
87 if( hd ) {
88 err = gcry_cipher_setkey( hd->hd, key, keylen );
89 if( !err)
90 err = gcry_cipher_setiv( hd->hd, ivbuf, ivlen );
91 if( err) {
92 cdk_cipher_close( hd );
93 hd = NULL;
96 return hd;
100 void
101 cdk_cipher_close( cdk_cipher_hd_t hd )
103 if( !hd )
104 return;
105 gcry_cipher_close( hd->hd);
106 cdk_free( hd );
111 cdk_cipher_decrypt( cdk_cipher_hd_t hd, byte * outbuf, const byte *inbuf,
112 size_t nbytes )
114 gcry_error_t err;
116 if( !hd )
117 return CDK_Inv_Value;
118 err = gcry_cipher_decrypt( hd->hd, outbuf, nbytes, inbuf, nbytes);
120 if (err) return CDK_Gcry_Error;
121 else return 0;
126 cdk_cipher_encrypt( cdk_cipher_hd_t hd, byte * outbuf, const byte *inbuf,
127 size_t nbytes )
129 gcry_error_t err;
131 if( !hd )
132 return CDK_Inv_Value;
133 err = gcry_cipher_encrypt( hd->hd, outbuf, nbytes, inbuf, nbytes);
135 if (err) return CDK_Gcry_Error;
136 else return 0;
140 void
141 cdk_cipher_sync( cdk_cipher_hd_t hd )
143 gcry_cipher_sync( hd->hd);
148 cdk_cipher_setiv( cdk_cipher_hd_t hd, const byte *ivbuf, size_t ivlen )
150 if( !hd )
151 return CDK_Inv_Value;
153 gcry_cipher_setiv( hd->hd, ivbuf, ivlen);
154 return 0;
159 cdk_cipher_setkey( cdk_cipher_hd_t hd, const byte *keybuf, size_t keylen )
161 if( !hd )
162 return CDK_Inv_Value;
164 gcry_cipher_setkey( hd->hd, keybuf, keylen);
165 return 0;
170 cdk_cipher_get_algo_blklen( int algo )
172 return gcry_cipher_get_algo_blklen( cdk_cipher_to_gcry( algo));
177 cdk_cipher_get_algo_keylen( int algo )
179 return gcry_cipher_get_algo_keylen( cdk_cipher_to_gcry( algo));
184 cdk_cipher_test_algo( int algo )
186 return gcry_cipher_test_algo( cdk_cipher_to_gcry( algo));