1 /* t-encrypt-large.c - Regression test for large amounts of data.
2 * Copyright (C) 2005 g10 Code GmbH
3 *
4 * This file is part of GPGME.
5 *
6 * GPGME is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * GPGME is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 /* We need to include config.h so that we know whether we are building
22 with large file system (LFS) support. */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include <gpgme.h>
32
33 #include "t-support.h"
34
35
36 struct cb_parms
37 {
38 size_t bytes_to_send;
39 size_t bytes_received;
40 };
41
42
43
44 /* The read callback used by GPGME to read data. */
45 static ssize_t
read_cb(void * handle,void * buffer,size_t size)46 read_cb (void *handle, void *buffer, size_t size)
47 {
48 struct cb_parms *parms = handle;
49 char *p = buffer;
50
51 for (; size && parms->bytes_to_send; size--, parms->bytes_to_send--)
52 *p++ = rand ();
53
54 return (p - (char*)buffer);
55 }
56
57 /* The write callback used by GPGME to write data. */
58 static ssize_t
write_cb(void * handle,const void * buffer,size_t size)59 write_cb (void *handle, const void *buffer, size_t size)
60 {
61 struct cb_parms *parms = handle;
62
63 (void)buffer;
64
65 parms->bytes_received += size;
66
67 return size;
68 }
69
70
71 static void
progress_cb(void * opaque,const char * what,int type,int current,int total)72 progress_cb (void *opaque, const char *what, int type, int current, int total)
73 {
74 /* This is just a dummy. */
75 (void)opaque;
76 (void)what;
77 (void)type;
78 (void)current;
79 (void)total;
80 }
81
82
83
84
85
86 int
main(int argc,char * argv[])87 main (int argc, char *argv[])
88 {
89 gpgme_ctx_t ctx;
90 gpgme_error_t err;
91 struct gpgme_data_cbs cbs;
92 gpgme_data_t in, out;
93 gpgme_key_t key[3] = { NULL, NULL, NULL };
94 gpgme_encrypt_result_t result;
95 size_t nbytes;
96 struct cb_parms parms;
97
98 if (argc > 1)
99 nbytes = atoi (argv[1]);
100 else
101 nbytes = 100000;
102
103 init_gpgme (GPGME_PROTOCOL_OpenPGP);
104
105 memset (&cbs, 0, sizeof cbs);
106 cbs.read = read_cb;
107 cbs.write = write_cb;
108 memset (&parms, 0, sizeof parms);
109 parms.bytes_to_send = nbytes;
110
111 err = gpgme_new (&ctx);
112 fail_if_err (err);
113 gpgme_set_armor (ctx, 0);
114
115 /* Install a progress handler to enforce a bit of more work to the
116 gpgme i/o system. */
117 gpgme_set_progress_cb (ctx, progress_cb, NULL);
118
119 err = gpgme_data_new_from_cbs (&in, &cbs, &parms);
120 fail_if_err (err);
121
122 err = gpgme_data_new_from_cbs (&out, &cbs, &parms);
123 fail_if_err (err);
124
125 err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
126 &key[0], 0);
127 fail_if_err (err);
128 err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
129 &key[1], 0);
130 fail_if_err (err);
131
132 err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
133 fail_if_err (err);
134 result = gpgme_op_encrypt_result (ctx);
135 if (result->invalid_recipients)
136 {
137 fprintf (stderr, "Invalid recipient encountered: %s\n",
138 result->invalid_recipients->fpr);
139 exit (1);
140 }
141 printf ("plaintext=%u bytes, ciphertext=%u bytes\n",
142 (unsigned int)nbytes, (unsigned int)parms.bytes_received);
143
144 gpgme_key_unref (key[0]);
145 gpgme_key_unref (key[1]);
146 gpgme_data_release (in);
147 gpgme_data_release (out);
148 gpgme_release (ctx);
149 return 0;
150 }
151