Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | /*
|
| 2 | Copyright (C) 2005, 2004 Erik Eliasson, Johan Bilien
|
| 3 |
|
| 4 | This library is free software; you can redistribute it and/or
|
| 5 | modify it under the terms of the GNU Lesser General Public
|
| 6 | License as published by the Free Software Foundation; either
|
| 7 | version 2.1 of the License, or (at your option) any later version.
|
| 8 |
|
| 9 | This library is distributed in the hope that it will be useful,
|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 12 | Lesser General Public License for more details.
|
| 13 |
|
| 14 | You should have received a copy of the GNU Lesser General Public
|
| 15 | License along with this library; if not, write to the Free Software
|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 17 |
|
| 18 | * In addition, as a special exception, the copyright holders give
|
| 19 | * permission to link the code of portions of this program with the
|
| 20 | * OpenSSL library under certain conditions as described in each
|
| 21 | * individual source file, and distribute linked combinations
|
| 22 | * including the two.
|
| 23 | * You must obey the GNU General Public License in all respects
|
| 24 | * for all of the code used other than OpenSSL. If you modify
|
| 25 | * file(s) with this exception, you may extend this exception to your
|
| 26 | * version of the file(s), but you are not obligated to do so. If you
|
| 27 | * do not wish to do so, delete this exception statement from your
|
| 28 | * version. If you delete this exception statement from all source
|
| 29 | * files in the program, then also delete it here.
|
| 30 | */
|
| 31 |
|
| 32 | /**
|
| 33 | * Implments the SRTP encryption modes as defined in RFC3711
|
| 34 | *
|
| 35 | * The SRTP specification defines two encryption modes, AES-CTR
|
| 36 | * (AES Counter mode) and AES-F8 mode. The AES-CTR is required,
|
| 37 | * AES-F8 is optional.
|
| 38 | *
|
| 39 | * Both modes are desinged to encrypt/decrypt data of arbitrary length
|
| 40 | * (with a specified upper limit, refer to RFC 3711). These modes do
|
| 41 | * <em>not</em> require that the amount of data to encrypt is a multiple
|
| 42 | * of the AES blocksize (128byte), no padding is necessary.
|
| 43 | *
|
| 44 | * The implementation uses the openSSL library as its cryptographic
|
| 45 | * backend.
|
| 46 | *
|
| 47 | * @author Erik Eliasson <eliasson@it.kth.se>
|
| 48 | * @author Johan Bilien <jobi@via.ecp.fr>
|
| 49 | * @author Werner Dittmann <Werner.Dittmann@t-online.de>
|
| 50 | */
|
| 51 |
|
| 52 |
|
| 53 | #ifndef AESSRTP_H
|
| 54 | #define AESSRTP_H
|
| 55 |
|
| 56 | #include <cc++/config.h>
|
| 57 |
|
| 58 | #include <ccrtp/CryptoContext.h>
|
| 59 |
|
| 60 | #ifndef SRTP_BLOCK_SIZE
|
| 61 | #define SRTP_BLOCK_SIZE 16
|
| 62 | #endif
|
| 63 |
|
| 64 | typedef struct _f8_ctx {
|
| 65 | unsigned char *S;
|
| 66 | unsigned char *ivAccent;
|
| 67 | uint32_t J;
|
| 68 | } F8_CIPHER_CTX;
|
| 69 |
|
| 70 |
|
| 71 | class __EXPORT AesSrtp {
|
| 72 | public:
|
| 73 | AesSrtp(int algo = SrtpEncryptionAESCM);
|
| 74 | AesSrtp(uint8* key, int32 key_length, int algo = SrtpEncryptionAESCM);
|
| 75 | ~AesSrtp();
|
| 76 |
|
| 77 | /**
|
| 78 | * Encrypts the inpout to the output.
|
| 79 | *
|
| 80 | * Encrypts one input block to one output block. Each block
|
| 81 | * is 16 bytes according to the AES encryption algorithm used.
|
| 82 | *
|
| 83 | * @param input
|
| 84 | * Pointer to input block, must be 16 bytes
|
| 85 | *
|
| 86 | * @param output
|
| 87 | * Pointer to output block, must be 16 bytes
|
| 88 | */
|
| 89 | void encrypt( const uint8* input, uint8* output );
|
| 90 |
|
| 91 | /**
|
| 92 | * Set new key
|
| 93 | *
|
| 94 | * @param key
|
| 95 | * Pointer to key data, must have at least a size of keyLength
|
| 96 | *
|
| 97 | * @param keyLength
|
| 98 | * Length of the key in bytes, must be 16, 24, or 32
|
| 99 | *
|
| 100 | * @return
|
| 101 | * false if key could not set.
|
| 102 | */
|
| 103 | bool setNewKey(const uint8* key, int32 keyLength);
|
| 104 |
|
| 105 | /**
|
| 106 | * Computes the cipher stream for AES CM mode.
|
| 107 | *
|
| 108 | * @param output
|
| 109 | * Pointer to a buffer that receives the cipher stream. Must be
|
| 110 | * at least <code>length</code> bytes long.
|
| 111 | *
|
| 112 | * @param length
|
| 113 | * Number of cipher stream bytes to produce. Usually the same
|
| 114 | * length as the data to be encrypted.
|
| 115 | *
|
| 116 | * @param iv
|
| 117 | * The initialization vector as input to create the cipher stream.
|
| 118 | * Refer to chapter 4.1.1 in RFC 3711.
|
| 119 | */
|
| 120 | void get_ctr_cipher_stream(uint8* output, uint32 length, uint8* iv);
|
| 121 |
|
| 122 | /**
|
| 123 | * Counter-mode encryption.
|
| 124 | *
|
| 125 | * This method performs the AES CM encryption.
|
| 126 | *
|
| 127 | * @param input
|
| 128 | * Pointer to input buffer, must be <code>inputLen</code> bytes.
|
| 129 | *
|
| 130 | * @param inputLen
|
| 131 | * Number of bytes to process.
|
| 132 | *
|
| 133 | * @param output
|
| 134 | * Pointer to output buffer, must be <code>inputLen</code> bytes.
|
| 135 | *
|
| 136 | * @param iv
|
| 137 | * The initialization vector as input to create the cipher stream.
|
| 138 | * Refer to chapter 4.1.1 in RFC 3711.
|
| 139 | */
|
| 140 | void ctr_encrypt( const uint8* input,
|
| 141 | uint32 inputLen,
|
| 142 | uint8* output, uint8* iv );
|
| 143 |
|
| 144 | /**
|
| 145 | * Counter-mode encryption, in place.
|
| 146 | *
|
| 147 | * This method performs the AES CM encryption.
|
| 148 | *
|
| 149 | * @param data
|
| 150 | * Pointer to input and output block, must be <code>dataLen</code>
|
| 151 | * bytes.
|
| 152 | *
|
| 153 | * @param dataLen
|
| 154 | * Number of bytes to process.
|
| 155 | *
|
| 156 | * @param iv
|
| 157 | * The initialization vector as input to create the cipher stream.
|
| 158 | * Refer to chapter 4.1.1 in RFC 3711.
|
| 159 | */
|
| 160 | void ctr_encrypt( uint8* data,
|
| 161 | uint32 data_length,
|
| 162 | uint8* iv );
|
| 163 |
|
| 164 | /**
|
| 165 | * AES F8 mode encryption, in place.
|
| 166 | *
|
| 167 | * This method performs the AES F8 encryption, see chapter 4.1.2
|
| 168 | * in RFC 3711.
|
| 169 | *
|
| 170 | * @param data
|
| 171 | * Pointer to input and output block, must be <code>dataLen</code>
|
| 172 | * bytes.
|
| 173 | *
|
| 174 | * @param dataLen
|
| 175 | * Number of bytes to process.
|
| 176 | *
|
| 177 | * @param iv
|
| 178 | * The initialization vector as input to create the cipher stream.
|
| 179 | * Refer to chapter 4.1.1 in RFC 3711.
|
| 180 | *
|
| 181 | * @param key
|
| 182 | * Pointer to the computed SRTP session key.
|
| 183 | *
|
| 184 | * @param keyLen
|
| 185 | * The length in bytes of the computed SRTP session key.
|
| 186 | *
|
| 187 | * @param salt
|
| 188 | * pointer to the computed session salt.
|
| 189 | *
|
| 190 | * @param saltLen
|
| 191 | * The length in bytes of the computed SRTP session salt.
|
| 192 | *
|
| 193 | * @param f8Cipher
|
| 194 | * An AES cipher context used for intermediate f8 AES encryption.
|
| 195 | */
|
| 196 | void f8_encrypt( const uint8* data,
|
| 197 | uint32 dataLen,
|
| 198 | uint8* iv,
|
| 199 | uint8* key,
|
| 200 | int32 keyLen,
|
| 201 | uint8* salt,
|
| 202 | int32 saltLen,
|
| 203 | AesSrtp* f8Cipher);
|
| 204 |
|
| 205 | /**
|
| 206 | * AES F8 mode encryption.
|
| 207 | *
|
| 208 | * This method performs the AES F8 encryption, see chapter 4.1.2
|
| 209 | * in RFC 3711.
|
| 210 | *
|
| 211 | * @param data
|
| 212 | * Pointer to input and output block, must be <code>dataLen</code>
|
| 213 | * bytes.
|
| 214 | *
|
| 215 | * @param dataLen
|
| 216 | * Number of bytes to process.
|
| 217 | *
|
| 218 | * @param out
|
| 219 | * Pointer to output buffer, must be <code>dataLen</code> bytes.
|
| 220 | *
|
| 221 | * @param iv
|
| 222 | * The initialization vector as input to create the cipher stream.
|
| 223 | * Refer to chapter 4.1.1 in RFC 3711.
|
| 224 | *
|
| 225 | * @param key
|
| 226 | * Pointer to the computed SRTP session key.
|
| 227 | *
|
| 228 | * @param keyLen
|
| 229 | * The length in bytes of the computed SRTP session key.
|
| 230 | *
|
| 231 | * @param salt
|
| 232 | * pointer to the computed session salt.
|
| 233 | *
|
| 234 | * @param saltLen
|
| 235 | * The length in bytes of the computed SRTP session salt.
|
| 236 | */
|
| 237 | void f8_encrypt(const uint8* data,
|
| 238 | uint32 dataLen,
|
| 239 | uint8* out,
|
| 240 | uint8* iv,
|
| 241 | uint8* key,
|
| 242 | int32 keyLen,
|
| 243 | uint8* salt,
|
| 244 | int32 saltLen,
|
| 245 | AesSrtp* f8Cipher);
|
| 246 |
|
| 247 |
|
| 248 | private:
|
| 249 | int processBlock(F8_CIPHER_CTX *f8ctx,
|
| 250 | const uint8* in,
|
| 251 | int32 length,
|
| 252 | uint8* out);
|
| 253 | void* key;
|
| 254 | int32_t algorithm;
|
| 255 | };
|
| 256 |
|
| 257 | #endif
|
| 258 |
|
| 259 | /** EMACS **
|
| 260 | * Local variables:
|
| 261 | * mode: c++
|
| 262 | * c-default-style: ellemtel
|
| 263 | * c-basic-offset: 4
|
| 264 | * End:
|
| 265 | */
|
| 266 |
|