~ubuntu-branches/ubuntu/trusty/gnunet/trusty

« back to all changes in this revision

Viewing changes to src/util/crypto_aes.c

  • Committer: Package Import Robot
  • Author(s): Bertrand Marc
  • Date: 2012-05-06 00:28:20 UTC
  • mfrom: (1.2.17)
  • Revision ID: package-import@ubuntu.com-20120506002820-n02u1ginv8pjh0ee
Tags: 0.9.2-1
* New maintainer (Closes: #660438).
* New upstream release (Closes: #621645).
* Remove debian/patches, not necessary any more.
* Update dependencies, according to README:
  + update minimal versions. 
  + depends on libunistring-dev.
  + depends on libltdl-dev instead of libltdl3-dev.
  + no need to depend on libgtk anymore.
  + gnunet-server suggests miniupnpc.
* debian/rules:
  + no need to define LOGFILE and PIDFILE.
  + create a minimal conf file.
  + cut gnunet-download-manager.scm extension.
  + remove autogenerated files in clean.
* Gnunet is now released under the GPL v3.
* Standards version 3.9.3.
* Add debian/watch.
* Adding Italian debconf translations, thanks to Beatrice Torracca
  (Closes: #663432).
* Move to dh-autoreconf and add extend-diff-ignore in debian/source/options.
* Remove gnunet-tools since gnunet-setup is not part of gnunet anymore
  (Closes:#651192).
* gnunet-server.init:
  + several services are now supervised by gnunet-service-arm.
  + define the logfile at run time.
* Use a secured group gnunetdns for SUID binaries and change permissions
  accordingly, see https://gnunet.org/gnunet-access-control-model
* gnunet-server.postinst, gnunet-server.postrm: use dpkg-statoverride to set
  and remove setuid permissions.
* Remove properly the old incompatible /etc/gnunetd.conf, use
  /etc/gnunet.conf instead.
* Add minimal (generic) man pages when information is available.
* Remove dpkg options for compression.
* gnunet-server.docs: add README.mysql and README.postgres.
* debian/rules: remove template binaries after dh_install.
* debian/copyright:
  + use copyright format 1.0.
  + mention AUTHORS and translators.
  + use GPL-3+ when possible.
  + add a paragraph for two files distributed under MIT/X11.
* Use dh_installdocs --link-doc to avoid redundancy.
* Remove unused debian/gnunet-dev.lintian-overrides.
* debian/control: add Vcs-git and Vcs-browser fields.
* gnunet-server.postrm:
  + use debconf to determine which user and group to delete.
  + remove /var/lib/gnunet on purge.
  + remove /etc/default/gnunet-server on purge (Closes: #668766).
* Make gnunet-dbg dependencies alternatives as it provides debugging
  symbol for all of them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
     This file is part of GNUnet.
 
3
     (C) 2001, 2002, 2003, 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
 
4
 
 
5
     GNUnet is free software; you can redistribute it and/or modify
 
6
     it under the terms of the GNU General Public License as published
 
7
     by the Free Software Foundation; either version 2, or (at your
 
8
     option) any later version.
 
9
 
 
10
     GNUnet is distributed in the hope that it will be useful, but
 
11
     WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
     General Public License for more details.
 
14
 
 
15
     You should have received a copy of the GNU General Public License
 
16
     along with GNUnet; see the file COPYING.  If not, write to the
 
17
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
     Boston, MA 02111-1307, USA.
 
19
*/
 
20
 
 
21
/**
 
22
 * @file util/crypto_aes.c
 
23
 * @brief Symmetric encryption services.
 
24
 * @author Christian Grothoff
 
25
 * @author Ioana Patrascu
 
26
 */
 
27
 
 
28
#include "platform.h"
 
29
#include "gnunet_common.h"
 
30
#include "gnunet_crypto_lib.h"
 
31
#include <gcrypt.h>
 
32
 
 
33
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
34
 
 
35
/**
 
36
 * Create a new SessionKey (for AES-256).
 
37
 */
 
38
void
 
39
GNUNET_CRYPTO_aes_create_session_key (struct GNUNET_CRYPTO_AesSessionKey *key)
 
40
{
 
41
  gcry_randomize (&key->key[0], GNUNET_CRYPTO_AES_KEY_LENGTH,
 
42
                  GCRY_STRONG_RANDOM);
 
43
  key->crc32 =
 
44
      htonl (GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH));
 
45
}
 
46
 
 
47
/**
 
48
 * Check that a new session key is well-formed.
 
49
 *
 
50
 * @return GNUNET_OK if the key is valid
 
51
 */
 
52
int
 
53
GNUNET_CRYPTO_aes_check_session_key (const struct GNUNET_CRYPTO_AesSessionKey
 
54
                                     *key)
 
55
{
 
56
  uint32_t crc;
 
57
 
 
58
  crc = GNUNET_CRYPTO_crc32_n (key, GNUNET_CRYPTO_AES_KEY_LENGTH);
 
59
  if (ntohl (key->crc32) == crc)
 
60
    return GNUNET_OK;
 
61
  GNUNET_break_op (0);
 
62
  return GNUNET_SYSERR;
 
63
}
 
64
 
 
65
 
 
66
/**
 
67
 * Encrypt a block with the public key of another
 
68
 * host that uses the same cyper.
 
69
 * @param block the block to encrypt
 
70
 * @param len the size of the block
 
71
 * @param sessionkey the key used to encrypt
 
72
 * @param iv the initialization vector to use, use INITVALUE
 
73
 *        for streams.
 
74
 * @param result the output parameter in which to store the encrypted result
 
75
 * @returns the size of the encrypted block, -1 for errors
 
76
 */
 
77
ssize_t
 
78
GNUNET_CRYPTO_aes_encrypt (const void *block, size_t len,
 
79
                           const struct GNUNET_CRYPTO_AesSessionKey *
 
80
                           sessionkey,
 
81
                           const struct GNUNET_CRYPTO_AesInitializationVector *
 
82
                           iv, void *result)
 
83
{
 
84
  gcry_cipher_hd_t handle;
 
85
  int rc;
 
86
 
 
87
  if (sessionkey->crc32 !=
 
88
      htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
 
89
  {
 
90
    GNUNET_break (0);
 
91
    return -1;
 
92
  }
 
93
  GNUNET_assert (0 ==
 
94
                 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
 
95
                                   GCRY_CIPHER_MODE_CFB, 0));
 
96
  rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
 
97
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
 
98
  rc = gcry_cipher_setiv (handle, iv,
 
99
                          sizeof (struct
 
100
                                  GNUNET_CRYPTO_AesInitializationVector));
 
101
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
 
102
  GNUNET_assert (0 == gcry_cipher_encrypt (handle, result, len, block, len));
 
103
  gcry_cipher_close (handle);
 
104
  return len;
 
105
}
 
106
 
 
107
/**
 
108
 * Decrypt a given block with the sessionkey.
 
109
 *
 
110
 * @param block the data to decrypt, encoded as returned by encrypt
 
111
 * @param size the size of the block to decrypt
 
112
 * @param sessionkey the key used to decrypt
 
113
 * @param iv the initialization vector to use, use INITVALUE
 
114
 *        for streams.
 
115
 * @param result address to store the result at
 
116
 * @return -1 on failure, size of decrypted block on success
 
117
 */
 
118
ssize_t
 
119
GNUNET_CRYPTO_aes_decrypt (const void *block, size_t size,
 
120
                           const struct GNUNET_CRYPTO_AesSessionKey *
 
121
                           sessionkey,
 
122
                           const struct GNUNET_CRYPTO_AesInitializationVector *
 
123
                           iv, void *result)
 
124
{
 
125
  gcry_cipher_hd_t handle;
 
126
  int rc;
 
127
 
 
128
  if (sessionkey->crc32 !=
 
129
      htonl (GNUNET_CRYPTO_crc32_n (sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH)))
 
130
  {
 
131
    GNUNET_break (0);
 
132
    return -1;
 
133
  }
 
134
  GNUNET_assert (0 ==
 
135
                 gcry_cipher_open (&handle, GCRY_CIPHER_AES256,
 
136
                                   GCRY_CIPHER_MODE_CFB, 0));
 
137
  rc = gcry_cipher_setkey (handle, sessionkey, GNUNET_CRYPTO_AES_KEY_LENGTH);
 
138
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
 
139
  rc = gcry_cipher_setiv (handle, iv,
 
140
                          sizeof (struct
 
141
                                  GNUNET_CRYPTO_AesInitializationVector));
 
142
  GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
 
143
  GNUNET_assert (0 == gcry_cipher_decrypt (handle, result, size, block, size));
 
144
  gcry_cipher_close (handle);
 
145
  return size;
 
146
}
 
147
 
 
148
/**
 
149
 * @brief Derive an IV
 
150
 * @param iv initialization vector
 
151
 * @param skey session key
 
152
 * @param salt salt for the derivation
 
153
 * @param salt_len size of the salt
 
154
 * @param ... pairs of void * & size_t for context chunks, terminated by NULL
 
155
 */
 
156
void
 
157
GNUNET_CRYPTO_aes_derive_iv (struct GNUNET_CRYPTO_AesInitializationVector *iv,
 
158
                             const struct GNUNET_CRYPTO_AesSessionKey *skey,
 
159
                             const void *salt, size_t salt_len, ...)
 
160
{
 
161
  va_list argp;
 
162
 
 
163
  va_start (argp, salt_len);
 
164
  GNUNET_CRYPTO_aes_derive_iv_v (iv, skey, salt, salt_len, argp);
 
165
  va_end (argp);
 
166
}
 
167
 
 
168
/**
 
169
 * @brief Derive an IV
 
170
 * @param iv initialization vector
 
171
 * @param skey session key
 
172
 * @param salt salt for the derivation
 
173
 * @param salt_len size of the salt
 
174
 * @param argp pairs of void * & size_t for context chunks, terminated by NULL
 
175
 */
 
176
void
 
177
GNUNET_CRYPTO_aes_derive_iv_v (struct GNUNET_CRYPTO_AesInitializationVector *iv,
 
178
                               const struct GNUNET_CRYPTO_AesSessionKey *skey,
 
179
                               const void *salt, size_t salt_len, va_list argp)
 
180
{
 
181
  GNUNET_CRYPTO_kdf_v (iv->iv, sizeof (iv->iv), salt, salt_len, skey->key,
 
182
                       sizeof (skey->key), argp);
 
183
}
 
184
 
 
185
/* end of crypto_aes.c */