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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
/*
* dhaes.c
*
* DHAES, code
*
* This code implements the encryption scheme from the paper:
*
* "DHAES: An Encryption Scheme Based on the Diffie-Hellman Problem"
* Michel Abdalla, Mihir Bellare, Phillip Rogaway
* September 1998
*
* Copyright (c) 2000, 2001 Virtual Unlimited, B.V.
*
* Author: Bob Deblier <bob@virtualunlimited.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#define BEECRYPT_DLL_EXPORT
#include "dhaes.h"
#include "dlsvdp-dh.h"
#include "blockmode.h"
#include "blockpad.h"
#if HAVE_STDLIB_H
# include <stdlib.h>
#endif
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
/**
* Good combinations will be:
*
* For 64-bit encryption:
* DHAES(MD5, Blowfish, HMAC-MD5) <- best candidate
* DHAES(MD5, Blowfish, HMAC-SHA-1)
* DHAES(MD5, Blowfish, HMAC-SHA-256)
*
* For 96-bit encryption with 64-bit mac:
* DHAES(SHA-1, Blowfish, HMAC-MD5, 96)
* DHAES(SHA-1, Blowfish, HMAC-SHA-1, 96) <- best candidate
* DHAES(SHA-1, Blowfish, HMAC-SHA-256, 96) <- best candidate
*
* For 128-bit encryption:
* DHAES(SHA-256, Blowfish, HMAC-MD5)
* DHAES(SHA-256, Blowfish, HMAC-SHA-1)
* DHAES(SHA-256, Blowfish, HMAC-SHA-256)
*/
int dhaes_pUsable(const dhaes_pParameters* params)
{
int keybits = (params->hash->digestsize << 3); /* digestsize in bytes times 8 bits */
int cipherkeybits = params->cipherkeybits;
int mackeybits = params->mackeybits;
/* test if keybits is a multiple of 32 */
if ((keybits & 31) != 0)
return 0;
/* test if cipherkeybits + mackeybits < keybits */
if ((cipherkeybits + mackeybits) > keybits)
return 0;
if (mackeybits == 0)
{
if (cipherkeybits == 0)
cipherkeybits = mackeybits = (keybits >> 1);
else
mackeybits = keybits - cipherkeybits;
}
/* test if keybits length is appropriate for cipher */
if ((cipherkeybits < params->cipher->keybitsmin) ||
(cipherkeybits > params->cipher->keybitsmax))
return 0;
if (((cipherkeybits - params->cipher->keybitsmin) % params->cipher->keybitsinc) != 0)
return 0;
/* test if keybits length is appropriate for mac */
if ((mackeybits < params->mac->keybitsmin) ||
(params->mackeybits > params->mac->keybitsmax))
return 0;
if (((mackeybits - params->mac->keybitsmin) % params->mac->keybitsinc) != 0)
return 0;
return 1;
}
int dhaes_pContextInit(dhaes_pContext* ctxt, const dhaes_pParameters* params)
{
if (ctxt == (dhaes_pContext*) 0)
return -1;
if (params == (dhaes_pParameters*) 0)
return -1;
if (params->param == (dldp_p*) 0)
return -1;
if (params->hash == (hashFunction*) 0)
return -1;
if (params->cipher == (blockCipher*) 0)
return -1;
if (params->mac == (keyedHashFunction*) 0)
return -1;
if (!dhaes_pUsable(params))
return -1;
dldp_pInit(&ctxt->param);
dldp_pCopy(&ctxt->param, params->param);
mp32nzero(&ctxt->pub);
mp32nzero(&ctxt->pri);
if (hashFunctionContextInit(&ctxt->hash, params->hash))
return -1;
if (blockCipherContextInit(&ctxt->cipher, params->cipher))
return -1;
if (keyedHashFunctionContextInit(&ctxt->mac, params->mac))
return -1;
ctxt->cipherkeybits = params->cipherkeybits;
ctxt->mackeybits = params->mackeybits;
return 0;
}
int dhaes_pContextInitDecrypt(dhaes_pContext* ctxt, const dhaes_pParameters* params, const mp32number* pri)
{
if (dhaes_pContextInit(ctxt, params))
return -1;
mp32ncopy(&ctxt->pri, pri);
return 0;
}
int dhaes_pContextInitEncrypt(dhaes_pContext* ctxt, const dhaes_pParameters* params, const mp32number* pub)
{
if (dhaes_pContextInit(ctxt, params))
return -1;
mp32ncopy(&ctxt->pub, pub);
return 0;
}
int dhaes_pContextFree(dhaes_pContext* ctxt)
{
dldp_pFree(&ctxt->param);
mp32nfree(&ctxt->pub);
mp32nfree(&ctxt->pri);
if (hashFunctionContextFree(&ctxt->hash))
return -1;
if (blockCipherContextFree(&ctxt->cipher))
return -1;
if (keyedHashFunctionContextFree(&ctxt->mac))
return -1;
return 0;
}
static int dhaes_pContextSetup(dhaes_pContext* ctxt, const mp32number* private, const mp32number* public, const mp32number* message, cipherOperation op)
{
register int rc;
mp32number secret;
mp32number digest;
/* compute the shared secret, Diffie-Hellman style */
mp32nzero(&secret);
if (dlsvdp_pDHSecret(&ctxt->param, private, public, &secret))
return -1;
/* compute the hash of the message (ephemeral public) key and the shared secret */
mp32nzero(&digest);
hashFunctionContextReset (&ctxt->hash);
hashFunctionContextUpdateMP32(&ctxt->hash, message);
hashFunctionContextUpdateMP32(&ctxt->hash, &secret);
hashFunctionContextDigest (&ctxt->hash, &digest);
/* we don't need the secret anymore */
mp32nwipe(&secret);
mp32nfree(&secret);
/**
* NOTE: blockciphers and keyed hash functions take keys with sizes
* specified in bits and key data passed in 32-bit words.
*
* Both blockcipher and keyed hash function have a min and max key size.
*
* This function will split the digest of the shared secret in two halves,
* and pad with zero bits or truncate if necessary to meet algorithm key
* size requirements.
*/
if (digest.size > 0)
{
uint32* mackey = digest.data;
uint32* cipherkey = digest.data + ((ctxt->mackeybits + 31) >> 5);
if ((rc = keyedHashFunctionContextSetup(&ctxt->mac, mackey, ctxt->mackeybits)))
goto setup_end;
if ((rc = blockCipherContextSetup(&ctxt->cipher, cipherkey, ctxt->cipherkeybits, op)))
goto setup_end;
rc = 0;
}
else
rc = -1;
setup_end:
mp32nwipe(&digest);
mp32nfree(&digest);
return rc;
}
memchunk* dhaes_pContextEncrypt(dhaes_pContext* ctxt, mp32number* ephemeralPublicKey, mp32number* mac, const memchunk* cleartext, randomGeneratorContext* rng)
{
memchunk* ciphertext = (memchunk*) 0;
memchunk* paddedtext;
mp32number ephemeralPrivateKey;
/* make the ephemeral keypair */
mp32nzero(&ephemeralPrivateKey);
dldp_pPair(&ctxt->param, rng, &ephemeralPrivateKey, ephemeralPublicKey);
/* Setup the key and initialize the mac and the blockcipher */
if (dhaes_pContextSetup(ctxt, &ephemeralPrivateKey, &ctxt->pub, ephemeralPublicKey, ENCRYPT))
goto encrypt_end;
/* add pkcs-5 padding */
paddedtext = pkcs5PadCopy(ctxt->cipher.algo->blocksize, cleartext);
/* encrypt the memchunk in CBC mode */
if (blockEncrypt(ctxt->cipher.algo, ctxt->cipher.param, CBC, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32*) paddedtext->data, (const uint32*) paddedtext->data))
{
free(paddedtext->data);
free(paddedtext);
goto encrypt_end;
}
/* Compute the mac */
if (keyedHashFunctionContextUpdateMC(&ctxt->mac, paddedtext))
{
free(paddedtext->data);
free(paddedtext);
goto encrypt_end;
}
if (keyedHashFunctionContextDigest(&ctxt->mac, mac))
{
free(paddedtext->data);
free(paddedtext);
goto encrypt_end;
}
ciphertext = paddedtext;
encrypt_end:
mp32nwipe(&ephemeralPrivateKey);
mp32nfree(&ephemeralPrivateKey);
return ciphertext;
}
memchunk* dhaes_pContextDecrypt(dhaes_pContext* ctxt, const mp32number* ephemeralPublicKey, const mp32number* mac, const memchunk* ciphertext)
{
memchunk* cleartext = (memchunk*) 0;
memchunk* paddedtext;
/* Setup the key and initialize the mac and the blockcipher */
if (dhaes_pContextSetup(ctxt, &ctxt->pri, ephemeralPublicKey, ephemeralPublicKey, DECRYPT))
goto decrypt_end;
/* Verify the mac */
if (keyedHashFunctionContextUpdateMC(&ctxt->mac, ciphertext))
goto decrypt_end;
if (keyedHashFunctionContextDigestMatch(&ctxt->mac, mac) == 0)
goto decrypt_end;
/* decrypt the memchunk with CBC mode */
paddedtext = (memchunk*) calloc(1, sizeof(memchunk));
if (paddedtext == (memchunk*) 0)
goto decrypt_end;
paddedtext->size = ciphertext->size;
paddedtext->data = (byte*) malloc(ciphertext->size);
if (paddedtext->data == (byte*) 0)
{
free(paddedtext);
goto decrypt_end;
}
if (blockDecrypt(ctxt->cipher.algo, ctxt->cipher.param, CBC, paddedtext->size / ctxt->cipher.algo->blocksize, (uint32*) paddedtext->data, (const uint32*) ciphertext->data))
{
free(paddedtext->data);
free(paddedtext);
goto decrypt_end;
}
/* remove pkcs-5 padding */
cleartext = pkcs5Unpad(ctxt->cipher.algo->blocksize, paddedtext);
if (cleartext == (memchunk*) 0)
{
free(paddedtext->data);
free(paddedtext);
}
decrypt_end:
return cleartext;
}
|