1 /* -*- Mode: C; c-file-style: "bsd" -*-
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
30 struct cdk_cipher_hd_s
{
36 static int cdk_cipher_to_gcry( int 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
;
51 cdk_cipher_new( int algo
, int pgp_sync
)
54 unsigned int flags
= 0;
57 if( cdk_cipher_test_algo( algo
) )
59 hd
= cdk_calloc( 1, sizeof * hd
);
64 flags
= GCRY_CIPHER_ENABLE_SYNC
;
68 err
= gcry_cipher_open( &hd
->hd
, cdk_cipher_to_gcry(algo
),
69 GCRY_CIPHER_MODE_CFB
, flags
);
79 cdk_cipher_open( int algo
, int pgp_sync
,
80 const byte
* key
, size_t keylen
,
81 const byte
* ivbuf
, size_t ivlen
)
86 hd
= cdk_cipher_new( algo
, pgp_sync
);
88 err
= gcry_cipher_setkey( hd
->hd
, key
, keylen
);
90 err
= gcry_cipher_setiv( hd
->hd
, ivbuf
, ivlen
);
92 cdk_cipher_close( hd
);
101 cdk_cipher_close( cdk_cipher_hd_t hd
)
105 gcry_cipher_close( hd
->hd
);
111 cdk_cipher_decrypt( cdk_cipher_hd_t hd
, byte
* outbuf
, const byte
*inbuf
,
117 return CDK_Inv_Value
;
118 err
= gcry_cipher_decrypt( hd
->hd
, outbuf
, nbytes
, inbuf
, nbytes
);
120 if (err
) return CDK_Gcry_Error
;
126 cdk_cipher_encrypt( cdk_cipher_hd_t hd
, byte
* outbuf
, const byte
*inbuf
,
132 return CDK_Inv_Value
;
133 err
= gcry_cipher_encrypt( hd
->hd
, outbuf
, nbytes
, inbuf
, nbytes
);
135 if (err
) return CDK_Gcry_Error
;
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
)
151 return CDK_Inv_Value
;
153 gcry_cipher_setiv( hd
->hd
, ivbuf
, ivlen
);
159 cdk_cipher_setkey( cdk_cipher_hd_t hd
, const byte
*keybuf
, size_t keylen
)
162 return CDK_Inv_Value
;
164 gcry_cipher_setkey( hd
->hd
, keybuf
, keylen
);
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
));