1 : /* -*- mode: C -*-
2 : *
3 : * File: pdf-crypt.c
4 : * Date: Fri Feb 22 21:05:05 2008
5 : *
6 : * GNU PDF Library - V2 backend encryption module
7 : *
8 : */
9 :
10 : /* Copyright (C) 2008 Free Software Foundation, Inc. */
11 :
12 : /* This program is free software: you can redistribute it and/or modify
13 : * it under the terms of the GNU General Public License as published by
14 : * the Free Software Foundation, either version 3 of the License, or
15 : * (at your option) any later version.
16 : *
17 : * This program is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : * GNU General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU General Public License
23 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : #include <config.h>
27 :
28 : #include <stdlib.h>
29 : #include <string.h>
30 : #include <gcrypt.h>
31 :
32 : #include <pdf-alloc.h>
33 : #include <pdf-types.h>
34 : #include <pdf-error.h>
35 : #include <pdf-crypt-c-v2.h>
36 :
37 :
38 : /* Creation and destruction of a v2 cipher */
39 :
40 : pdf_status_t
41 : pdf_crypt_cipher_v2_new (void ** cipher)
42 7 : {
43 : gcry_cipher_hd_t * hd;
44 :
45 7 : hd = pdf_alloc (sizeof (gcry_cipher_hd_t));
46 :
47 7 : if (hd != NULL)
48 : {
49 : gcry_error_t err;
50 :
51 7 : err = gcry_cipher_open (hd, GCRY_CIPHER_ARCFOUR, GCRY_CIPHER_MODE_STREAM, 0);
52 :
53 7 : if (err == GPG_ERR_NO_ERROR)
54 : {
55 7 : *cipher = hd;
56 7 : return PDF_OK;
57 : }
58 : else
59 : {
60 0 : pdf_dealloc (hd);
61 0 : return PDF_ERROR;
62 : }
63 : }
64 : else
65 : {
66 0 : pdf_dealloc (hd);
67 0 : return PDF_ENOMEM;
68 : }
69 :
70 : return PDF_OK;
71 : }
72 :
73 :
74 : pdf_status_t
75 : pdf_crypt_cipher_v2_destroy (void * cipher)
76 7 : {
77 7 : gcry_cipher_hd_t * hd = cipher;
78 7 : gcry_cipher_close (*hd);
79 7 : pdf_dealloc (cipher);
80 7 : return PDF_OK;
81 : }
82 :
83 :
84 :
85 : /* Encryption and decryption functions */
86 :
87 : pdf_status_t
88 : pdf_crypt_cipher_v2_setkey (void * cipher,
89 : pdf_char_t *key, pdf_size_t size)
90 5 : {
91 5 : gcry_cipher_hd_t * hd = cipher;
92 5 : if (gcry_cipher_setkey (*hd, key, size) != GPG_ERR_NO_ERROR)
93 : {
94 1 : return PDF_EBADV2KEY;
95 : }
96 : else
97 : {
98 4 : return PDF_OK;
99 : }
100 :
101 : }
102 :
103 : pdf_status_t
104 : pdf_crypt_cipher_v2_encrypt (void * cipher,
105 : pdf_char_t *out, pdf_size_t out_size,
106 : pdf_char_t *in, pdf_size_t in_size,
107 : pdf_size_t *result_size)
108 2 : {
109 2 : gcry_cipher_hd_t * hd = cipher;
110 :
111 2 : if (gcry_cipher_encrypt (*hd, out, out_size, in, in_size) == GPG_ERR_NO_ERROR)
112 : {
113 2 : if (result_size != NULL)
114 2 : *result_size = in_size;
115 2 : return PDF_OK;
116 : }
117 : else
118 : {
119 0 : return PDF_ERROR;
120 : }
121 : }
122 :
123 :
124 :
125 : pdf_status_t
126 : pdf_crypt_cipher_v2_decrypt (void * cipher,
127 : pdf_char_t *out, pdf_size_t out_size,
128 : pdf_char_t *in, pdf_size_t in_size,
129 : pdf_size_t *result_size)
130 2 : {
131 2 : gcry_cipher_hd_t * hd = cipher;
132 :
133 2 : if (gcry_cipher_decrypt (*hd, out, out_size, in, in_size) == GPG_ERR_NO_ERROR)
134 : {
135 2 : if (result_size != NULL)
136 2 : *result_size = in_size;
137 2 : return PDF_OK;
138 : }
139 : else
140 : {
141 0 : return PDF_ERROR;
142 : }
143 : }
144 :
145 : /* End of pdf-crypt-c-v2.c */
|