File: sym-cipher.c

package info (click to toggle)
gnutls13 1.4.4-3%2Betch5
  • links: PTS
  • area: main
  • in suites: etch
  • size: 24,052 kB
  • ctags: 12,507
  • sloc: ansic: 94,474; xml: 17,016; sh: 9,652; perl: 628; makefile: 607; sed: 16
file content (187 lines) | stat: -rw-r--r-- 4,117 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* -*- Mode: C; c-file-style: "bsd" -*-
 * sym-cipher.c
 *        Copyright (C) 2003 Timo Schulz
 *
 * This file is part of OpenCDK.
 *
 * OpenCDK is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OpenCDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenCDK; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */


#include <stdio.h>
#include <string.h>
#include "opencdk.h"
#include "main.h"
#include "cipher.h"


struct cdk_cipher_hd_s {
    gcry_cipher_hd_t hd;
    int algo;
};

inline
static int cdk_cipher_to_gcry( int algo)
{
    switch( algo ) {
    case CDK_CIPHER_BLOWFISH: return GCRY_CIPHER_BLOWFISH;
    case CDK_CIPHER_TWOFISH:  return GCRY_CIPHER_TWOFISH;
    case CDK_CIPHER_3DES:     return GCRY_CIPHER_3DES;
    case CDK_CIPHER_CAST5:    return GCRY_CIPHER_CAST5;
    case CDK_CIPHER_AES:      return GCRY_CIPHER_AES;
    case CDK_CIPHER_AES192:   return GCRY_CIPHER_AES192;
    case CDK_CIPHER_AES256:   return GCRY_CIPHER_AES256;
    default: return -1;
    }
}

cdk_cipher_hd_t
cdk_cipher_new( int algo, int pgp_sync )
{
    cdk_cipher_hd_t hd;
    unsigned int flags = 0;
    gcry_error_t err;
    
    if( cdk_cipher_test_algo( algo ) )
        return NULL;
    hd = cdk_calloc( 1, sizeof * hd );
    if( !hd )
        return NULL;

    if( pgp_sync )
        flags = GCRY_CIPHER_ENABLE_SYNC;

    hd->algo = algo;

    err = gcry_cipher_open( &hd->hd, cdk_cipher_to_gcry(algo), 
	GCRY_CIPHER_MODE_CFB, flags);
    if( err ) {
        cdk_free( hd );
        return NULL;
    }
    return hd;
}


cdk_cipher_hd_t
cdk_cipher_open( int algo, int pgp_sync,
                 const byte * key, size_t keylen,
                 const byte * ivbuf, size_t ivlen )
{
    cdk_cipher_hd_t hd;
    gcry_error_t err;

    hd = cdk_cipher_new( algo, pgp_sync );
    if( hd ) {
        err = gcry_cipher_setkey( hd->hd, key, keylen );
        if( !err)
            err = gcry_cipher_setiv( hd->hd, ivbuf, ivlen );
        if( err) {
            cdk_cipher_close( hd );
            hd = NULL;
        }
    }
    return hd;
}
    

void
cdk_cipher_close( cdk_cipher_hd_t hd )
{
    if( !hd )
        return;
    gcry_cipher_close( hd->hd);
    cdk_free( hd );
}


int
cdk_cipher_decrypt( cdk_cipher_hd_t hd, byte * outbuf, const byte *inbuf,
                    size_t nbytes )
{
gcry_error_t err;

    if( !hd )
        return CDK_Inv_Value;
    err = gcry_cipher_decrypt( hd->hd, outbuf, nbytes, inbuf, nbytes);

    if (err) return CDK_Gcry_Error;
    else return 0;
}


int
cdk_cipher_encrypt( cdk_cipher_hd_t hd, byte * outbuf, const byte *inbuf,
                    size_t nbytes )
{
gcry_error_t err;

    if( !hd )
        return CDK_Inv_Value;
    err = gcry_cipher_encrypt( hd->hd, outbuf, nbytes, inbuf, nbytes);

    if (err) return CDK_Gcry_Error;
    else return 0;
}


void
cdk_cipher_sync( cdk_cipher_hd_t hd )
{
    gcry_cipher_sync( hd->hd);
}


int
cdk_cipher_setiv( cdk_cipher_hd_t hd, const byte *ivbuf, size_t ivlen )
{
    if( !hd )
        return CDK_Inv_Value;

    gcry_cipher_setiv( hd->hd, ivbuf, ivlen);
    return 0;
}


int
cdk_cipher_setkey( cdk_cipher_hd_t hd, const byte *keybuf, size_t keylen )
{
    if( !hd )
        return CDK_Inv_Value;

    gcry_cipher_setkey( hd->hd, keybuf, keylen);
    return 0;
}


int
cdk_cipher_get_algo_blklen( int algo )
{
    return gcry_cipher_get_algo_blklen( cdk_cipher_to_gcry( algo));
}


int
cdk_cipher_get_algo_keylen( int algo )
{
    return gcry_cipher_get_algo_keylen( cdk_cipher_to_gcry( algo));
}


int
cdk_cipher_test_algo( int algo )
{
    return gcry_cipher_test_algo( cdk_cipher_to_gcry( algo));
}