crypto_wrapper.cpp 21.6 KB
Newer Older
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
/* crypto_wrapper.cpp  
 *
 * Copyright (C) 2003 Sawtooth Consulting Ltd.
 *
 * This file is part of yaSSL.
 *
 * yaSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * yaSSL 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

/*  The crypto wrapper source implements the policies for the cipher
 *  components used by SSL.
 *
 *  The implementation relies on a specfic library, taoCrypt.
 */

#if !defined(USE_CRYPTOPP_LIB)

#include "runtime.hpp"
#include "crypto_wrapper.hpp"
#include "cert_wrapper.hpp"

#include "md5.hpp"
#include "sha.hpp"
#include "ripemd.hpp"
#include "hmac.hpp"
#include "modes.hpp"
#include "des.hpp"
#include "arc4.hpp"
#include "aes.hpp"
#include "rsa.hpp"
#include "dsa.hpp"
#include "dh.hpp"
#include "random.hpp"
#include "file.hpp"
#include "coding.hpp"


namespace yaSSL {


// MD5 Implementation
struct MD5::MD5Impl {
    TaoCrypt::MD5 md5_;
    MD5Impl() {}
    explicit MD5Impl(const TaoCrypt::MD5& md5) : md5_(md5) {}
};


unknown's avatar
unknown committed
61
MD5::MD5() : pimpl_(NEW_YS MD5Impl) {}
62 63


64
MD5::~MD5() { ysDelete(pimpl_); }
65 66


unknown's avatar
unknown committed
67
MD5::MD5(const MD5& that) : Digest(), pimpl_(NEW_YS 
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
                                             MD5Impl(that.pimpl_->md5_)) {}


MD5& MD5::operator=(const MD5& that)
{
    pimpl_->md5_ = that.pimpl_->md5_;
    return *this;
}


uint MD5::get_digestSize() const
{
    return MD5_LEN;
}


uint MD5::get_padSize() const
{
    return PAD_MD5;
}


// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz
void MD5::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->md5_.Update(in, sz);
    pimpl_->md5_.Final(out);
}

// Fill out with MD5 digest from previous updates
void MD5::get_digest(byte* out)
{
    pimpl_->md5_.Final(out);
}


// Update the current digest
void MD5::update(const byte* in, unsigned int sz)
{
    pimpl_->md5_.Update(in, sz);
}


// SHA Implementation
struct SHA::SHAImpl {
    TaoCrypt::SHA sha_;
    SHAImpl() {}
    explicit SHAImpl(const TaoCrypt::SHA& sha) : sha_(sha) {}
};


unknown's avatar
unknown committed
119
SHA::SHA() : pimpl_(NEW_YS SHAImpl) {}
120 121


122
SHA::~SHA() { ysDelete(pimpl_); }
123 124


unknown's avatar
unknown committed
125
SHA::SHA(const SHA& that) : Digest(), pimpl_(NEW_YS SHAImpl(that.pimpl_->sha_)) {}
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

SHA& SHA::operator=(const SHA& that)
{
    pimpl_->sha_ = that.pimpl_->sha_;
    return *this;
}


uint SHA::get_digestSize() const
{
    return SHA_LEN;
}


uint SHA::get_padSize() const
{
    return PAD_SHA;
}


// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz
void SHA::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->sha_.Update(in, sz);
    pimpl_->sha_.Final(out);
}


// Fill out with SHA digest from previous updates
void SHA::get_digest(byte* out)
{
    pimpl_->sha_.Final(out);
}


// Update the current digest
void SHA::update(const byte* in, unsigned int sz)
{
    pimpl_->sha_.Update(in, sz);
}


// RMD-160 Implementation
struct RMD::RMDImpl {
    TaoCrypt::RIPEMD160 rmd_;
    RMDImpl() {}
    explicit RMDImpl(const TaoCrypt::RIPEMD160& rmd) : rmd_(rmd) {}
};


unknown's avatar
unknown committed
176
RMD::RMD() : pimpl_(NEW_YS RMDImpl) {}
177 178


179
RMD::~RMD() { ysDelete(pimpl_); }
180 181


unknown's avatar
unknown committed
182
RMD::RMD(const RMD& that) : Digest(), pimpl_(NEW_YS RMDImpl(that.pimpl_->rmd_)) {}
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

RMD& RMD::operator=(const RMD& that)
{
    pimpl_->rmd_ = that.pimpl_->rmd_;
    return *this;
}


uint RMD::get_digestSize() const
{
    return RMD_LEN;
}


uint RMD::get_padSize() const
{
    return PAD_RMD;
}


// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz
void RMD::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->rmd_.Update(in, sz);
    pimpl_->rmd_.Final(out);
}


// Fill out with RMD digest from previous updates
void RMD::get_digest(byte* out)
{
    pimpl_->rmd_.Final(out);
}


// Update the current digest
void RMD::update(const byte* in, unsigned int sz)
{
    pimpl_->rmd_.Update(in, sz);
}


// HMAC_MD5 Implementation
struct HMAC_MD5::HMAC_MD5Impl {
    TaoCrypt::HMAC<TaoCrypt::MD5> mac_;
    HMAC_MD5Impl() {}
};


HMAC_MD5::HMAC_MD5(const byte* secret, unsigned int len) 
unknown's avatar
unknown committed
233
    : pimpl_(NEW_YS HMAC_MD5Impl) 
234 235 236 237 238
{
    pimpl_->mac_.SetKey(secret, len);
}


239
HMAC_MD5::~HMAC_MD5() { ysDelete(pimpl_); }
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


uint HMAC_MD5::get_digestSize() const
{
    return MD5_LEN;
}


uint HMAC_MD5::get_padSize() const
{
    return PAD_MD5;
}


// Fill out with MD5 digest from in that is sz bytes, out must be >= digest sz
void HMAC_MD5::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with MD5 digest from previous updates
void HMAC_MD5::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_MD5::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}


// HMAC_SHA Implementation
struct HMAC_SHA::HMAC_SHAImpl {
    TaoCrypt::HMAC<TaoCrypt::SHA> mac_;
    HMAC_SHAImpl() {}
};


HMAC_SHA::HMAC_SHA(const byte* secret, unsigned int len) 
unknown's avatar
unknown committed
283
    : pimpl_(NEW_YS HMAC_SHAImpl) 
284 285 286 287 288
{
    pimpl_->mac_.SetKey(secret, len);
}


289
HMAC_SHA::~HMAC_SHA() { ysDelete(pimpl_); }
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


uint HMAC_SHA::get_digestSize() const
{
    return SHA_LEN;
}


uint HMAC_SHA::get_padSize() const
{
    return PAD_SHA;
}


// Fill out with SHA digest from in that is sz bytes, out must be >= digest sz
void HMAC_SHA::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with SHA digest from previous updates
void HMAC_SHA::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_SHA::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}



// HMAC_RMD Implementation
struct HMAC_RMD::HMAC_RMDImpl {
    TaoCrypt::HMAC<TaoCrypt::RIPEMD160> mac_;
    HMAC_RMDImpl() {}
};


HMAC_RMD::HMAC_RMD(const byte* secret, unsigned int len) 
unknown's avatar
unknown committed
334
    : pimpl_(NEW_YS HMAC_RMDImpl) 
335 336 337 338 339
{
    pimpl_->mac_.SetKey(secret, len);
}


340
HMAC_RMD::~HMAC_RMD() { ysDelete(pimpl_); }
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381


uint HMAC_RMD::get_digestSize() const
{
    return RMD_LEN;
}


uint HMAC_RMD::get_padSize() const
{
    return PAD_RMD;
}


// Fill out with RMD digest from in that is sz bytes, out must be >= digest sz
void HMAC_RMD::get_digest(byte* out, const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
    pimpl_->mac_.Final(out);
}

// Fill out with RMD digest from previous updates
void HMAC_RMD::get_digest(byte* out)
{
    pimpl_->mac_.Final(out);
}


// Update the current digest
void HMAC_RMD::update(const byte* in, unsigned int sz)
{
    pimpl_->mac_.Update(in, sz);
}


struct DES::DESImpl {
    TaoCrypt::DES_CBC_Encryption encryption;
    TaoCrypt::DES_CBC_Decryption decryption;
};


unknown's avatar
unknown committed
382
DES::DES() : pimpl_(NEW_YS DESImpl) {}
383

384
DES::~DES() { ysDelete(pimpl_); }
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417


void DES::set_encryptKey(const byte* k, const byte* iv)
{
    pimpl_->encryption.SetKey(k, DES_KEY_SZ, iv);
}


void DES::set_decryptKey(const byte* k, const byte* iv)
{
    pimpl_->decryption.SetKey(k, DES_KEY_SZ, iv);
}

// DES encrypt plain of length sz into cipher
void DES::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// DES decrypt cipher of length sz into plain
void DES::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}


struct DES_EDE::DES_EDEImpl {
    TaoCrypt::DES_EDE3_CBC_Encryption encryption;
    TaoCrypt::DES_EDE3_CBC_Decryption decryption;
};


unknown's avatar
unknown committed
418
DES_EDE::DES_EDE() : pimpl_(NEW_YS DES_EDEImpl) {}
419

420
DES_EDE::~DES_EDE() { ysDelete(pimpl_); }
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455


void DES_EDE::set_encryptKey(const byte* k, const byte* iv)
{
    pimpl_->encryption.SetKey(k, DES_EDE_KEY_SZ, iv);
}


void DES_EDE::set_decryptKey(const byte* k, const byte* iv)
{
    pimpl_->decryption.SetKey(k, DES_EDE_KEY_SZ, iv);
}


// 3DES encrypt plain of length sz into cipher
void DES_EDE::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// 3DES decrypt cipher of length sz into plain
void DES_EDE::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}


// Implementation of alledged RC4
struct RC4::RC4Impl {
    TaoCrypt::ARC4::Encryption encryption;
    TaoCrypt::ARC4::Decryption decryption;
};


unknown's avatar
unknown committed
456
RC4::RC4() : pimpl_(NEW_YS RC4Impl) {}
457

458
RC4::~RC4() { ysDelete(pimpl_); }
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497


void RC4::set_encryptKey(const byte* k, const byte*)
{
    pimpl_->encryption.SetKey(k, RC4_KEY_SZ);
}


void RC4::set_decryptKey(const byte* k, const byte*)
{
    pimpl_->decryption.SetKey(k, RC4_KEY_SZ);
}


// RC4 encrypt plain of length sz into cipher
void RC4::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// RC4 decrypt cipher of length sz into plain
void RC4::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}



// Implementation of AES
struct AES::AESImpl {
    TaoCrypt::AES_CBC_Encryption encryption;
    TaoCrypt::AES_CBC_Decryption decryption;
    unsigned int keySz_;

    AESImpl(unsigned int ks) : keySz_(ks) {}
};


unknown's avatar
unknown committed
498
AES::AES(unsigned int ks) : pimpl_(NEW_YS AESImpl(ks)) {}
499

500
AES::~AES() { ysDelete(pimpl_); }
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538


int AES::get_keySize() const
{
    return pimpl_->keySz_;
}


void AES::set_encryptKey(const byte* k, const byte* iv)
{
    pimpl_->encryption.SetKey(k, pimpl_->keySz_, iv);
}


void AES::set_decryptKey(const byte* k, const byte* iv)
{
    pimpl_->decryption.SetKey(k, pimpl_->keySz_, iv);
}


// AES encrypt plain of length sz into cipher
void AES::encrypt(byte* cipher, const byte* plain, unsigned int sz)
{
    pimpl_->encryption.Process(cipher, plain, sz);
}


// AES decrypt cipher of length sz into plain
void AES::decrypt(byte* plain, const byte* cipher, unsigned int sz)
{
    pimpl_->decryption.Process(plain, cipher, sz);
}


struct RandomPool::RandomImpl {
    TaoCrypt::RandomNumberGenerator RNG_;
};

unknown's avatar
unknown committed
539
RandomPool::RandomPool() : pimpl_(NEW_YS RandomImpl) {}
540

541
RandomPool::~RandomPool() { ysDelete(pimpl_); }
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

int RandomPool::GetError() const
{
    return pimpl_->RNG_.GetError(); 
}

void RandomPool::Fill(opaque* dst, uint sz) const
{
    pimpl_->RNG_.GenerateBlock(dst, sz);
}


// Implementation of DSS Authentication
struct DSS::DSSImpl {
    void SetPublic (const byte*, unsigned int);
    void SetPrivate(const byte*, unsigned int);
    TaoCrypt::DSA_PublicKey publicKey_;
    TaoCrypt::DSA_PrivateKey privateKey_;
};


// Decode and store the public key
void DSS::DSSImpl::SetPublic(const byte* key, unsigned int sz)
{
    TaoCrypt::Source source(key, sz);
    publicKey_.Initialize(source);
}


// Decode and store the public key
void DSS::DSSImpl::SetPrivate(const byte* key, unsigned int sz)
{
    TaoCrypt::Source source(key, sz);
    privateKey_.Initialize(source);
    publicKey_ = TaoCrypt::DSA_PublicKey(privateKey_);

}


// Set public or private key
DSS::DSS(const byte* key, unsigned int sz, bool publicKey) 
unknown's avatar
unknown committed
583
    : pimpl_(NEW_YS DSSImpl)
584 585 586 587 588 589 590 591 592 593
{
    if (publicKey) 
        pimpl_->SetPublic(key, sz);
    else
        pimpl_->SetPrivate(key, sz);
}


DSS::~DSS()
{
594
    ysDelete(pimpl_);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
}


uint DSS::get_signatureLength() const
{
    return pimpl_->publicKey_.SignatureLength();
}


// DSS Sign message of length sz into sig
void DSS::sign(byte* sig,  const byte* sha_digest, unsigned int /* shaSz */,
               const RandomPool& random)
{
    using namespace TaoCrypt;

    DSA_Signer signer(pimpl_->privateKey_);
    signer.Sign(sha_digest, sig, random.pimpl_->RNG_);
}


// DSS Verify message of length sz against sig, is it correct?
bool DSS::verify(const byte* sha_digest, unsigned int /* shaSz */,
                 const byte* sig, unsigned int /* sigSz */)
{
    using namespace TaoCrypt;

    DSA_Verifier ver(pimpl_->publicKey_);
    return ver.Verify(sha_digest, sig);
}


// Implementation of RSA key interface
struct RSA::RSAImpl {
    void SetPublic (const byte*, unsigned int);
    void SetPrivate(const byte*, unsigned int);
    TaoCrypt::RSA_PublicKey publicKey_;
    TaoCrypt::RSA_PrivateKey privateKey_;
};


// Decode and store the public key
void RSA::RSAImpl::SetPublic(const byte* key, unsigned int sz)
{
    TaoCrypt::Source source(key, sz);
    publicKey_.Initialize(source);
}


// Decode and store the private key
void RSA::RSAImpl::SetPrivate(const byte* key, unsigned int sz)
{
    TaoCrypt::Source source(key, sz);
    privateKey_.Initialize(source);
    publicKey_ = TaoCrypt::RSA_PublicKey(privateKey_);
}


// Set public or private key
RSA::RSA(const byte* key, unsigned int sz, bool publicKey) 
unknown's avatar
unknown committed
654
    : pimpl_(NEW_YS RSAImpl)
655 656 657 658 659 660 661 662 663
{
    if (publicKey) 
        pimpl_->SetPublic(key, sz);
    else
        pimpl_->SetPrivate(key, sz);
}

RSA::~RSA()
{
664
    ysDelete(pimpl_);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
}


// get cipher text length, varies on key size
unsigned int RSA::get_cipherLength() const
{
    return pimpl_->publicKey_.FixedCiphertextLength();
}


// get signautre length, varies on key size
unsigned int RSA::get_signatureLength() const
{
    return get_cipherLength();
}


// RSA Sign message of length sz into sig
void RSA::sign(byte* sig,  const byte* message, unsigned int sz,
               const RandomPool& random)
{
    TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_);
    dec.SSL_Sign(message, sz, sig, random.pimpl_->RNG_);
}


// RSA Verify message of length sz against sig
bool RSA::verify(const byte* message, unsigned int sz, const byte* sig,
                 unsigned int)
{
    TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_);
    return enc.SSL_Verify(message, sz, sig);
}


// RSA public encrypt plain of length sz into cipher
void RSA::encrypt(byte* cipher, const byte* plain, unsigned int sz,
                  const RandomPool& random)
{
  
    TaoCrypt::RSAES_Encryptor enc(pimpl_->publicKey_);
    enc.Encrypt(plain, sz, cipher, random.pimpl_->RNG_);
}


// RSA private decrypt cipher of length sz into plain
void RSA::decrypt(byte* plain, const byte* cipher, unsigned int sz,
                  const RandomPool& random)
{
    TaoCrypt::RSAES_Decryptor dec(pimpl_->privateKey_);
    dec.Decrypt(cipher, sz, plain, random.pimpl_->RNG_);
}


struct Integer::IntegerImpl {
    TaoCrypt::Integer int_;

    IntegerImpl() {}
    explicit IntegerImpl(const TaoCrypt::Integer& i) : int_(i) {}
};

unknown's avatar
unknown committed
726
Integer::Integer() : pimpl_(NEW_YS IntegerImpl) {}
727

728
Integer::~Integer() { ysDelete(pimpl_); }
729 730 731



unknown's avatar
unknown committed
732
Integer::Integer(const Integer& other) : pimpl_(NEW_YS 
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
                                               IntegerImpl(other.pimpl_->int_))
{}


Integer& Integer::operator=(const Integer& that)
{
    pimpl_->int_ = that.pimpl_->int_;

    return *this;
}


void Integer::assign(const byte* num, unsigned int sz)
{
    pimpl_->int_ = TaoCrypt::Integer(num, sz);
}


struct DiffieHellman::DHImpl {
    TaoCrypt::DH                     dh_;
    TaoCrypt::RandomNumberGenerator& ranPool_;
    byte* publicKey_;
    byte* privateKey_;
    byte* agreedKey_;

    DHImpl(TaoCrypt::RandomNumberGenerator& r) : ranPool_(r), publicKey_(0),
                                               privateKey_(0), agreedKey_(0) {}
760 761 762 763 764 765
    ~DHImpl() 
    {   
        ysArrayDelete(agreedKey_); 
        ysArrayDelete(privateKey_); 
        ysArrayDelete(publicKey_);
    }
766 767 768 769 770 771 772 773 774 775

    DHImpl(const DHImpl& that) : dh_(that.dh_), ranPool_(that.ranPool_),
                                 publicKey_(0), privateKey_(0), agreedKey_(0)
    {
        uint length = dh_.GetByteLength();
        AllocKeys(length, length, length);
    }

    void AllocKeys(unsigned int pubSz, unsigned int privSz, unsigned int agrSz)
    {
unknown's avatar
unknown committed
776 777 778
        publicKey_  = NEW_YS byte[pubSz];
        privateKey_ = NEW_YS byte[privSz];
        agreedKey_  = NEW_YS byte[agrSz];
779 780 781 782 783 784 785 786
    }
};



/*
// server Side DH, server's view
DiffieHellman::DiffieHellman(const char* file, const RandomPool& random)
unknown's avatar
unknown committed
787
    : pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_))
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
{
    using namespace TaoCrypt;
    Source source;
    FileSource(file, source);
    if (source.size() == 0)
        return; // TODO add error state, and force check
    HexDecoder hd(source);

    pimpl_->dh_.Initialize(source);

    uint length = pimpl_->dh_.GetByteLength();

    pimpl_->AllocKeys(length, length, length);
    pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
                                                  pimpl_->publicKey_);
}
*/


// server Side DH, client's view
DiffieHellman::DiffieHellman(const byte* p, unsigned int pSz, const byte* g,
                             unsigned int gSz, const byte* pub,
                             unsigned int pubSz, const RandomPool& random)
unknown's avatar
unknown committed
811
    : pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_))
812 813 814 815
{
    using TaoCrypt::Integer;

    pimpl_->dh_.Initialize(Integer(p, pSz).Ref(), Integer(g, gSz).Ref());
unknown's avatar
unknown committed
816
    pimpl_->publicKey_ = NEW_YS opaque[pubSz];
817 818 819 820 821 822 823
    memcpy(pimpl_->publicKey_, pub, pubSz);
}


// Server Side DH, server's view
DiffieHellman::DiffieHellman(const Integer& p, const Integer& g,
                             const RandomPool& random)
unknown's avatar
unknown committed
824
: pimpl_(NEW_YS DHImpl(random.pimpl_->RNG_))
825 826 827 828 829 830 831 832 833 834 835 836
{
    using TaoCrypt::Integer;

    pimpl_->dh_.Initialize(p.pimpl_->int_, g.pimpl_->int_);

    uint length = pimpl_->dh_.GetByteLength();

    pimpl_->AllocKeys(length, length, length);
    pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
                                                  pimpl_->publicKey_);
}

837
DiffieHellman::~DiffieHellman() { ysDelete(pimpl_); }
838 839 840 841


// Client side and view, use server that for p and g
DiffieHellman::DiffieHellman(const DiffieHellman& that) 
unknown's avatar
unknown committed
842
    : pimpl_(NEW_YS DHImpl(*that.pimpl_))
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
{   
    pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
                                                  pimpl_->publicKey_);
}


DiffieHellman& DiffieHellman::operator=(const DiffieHellman& that)
{
    pimpl_->dh_ = that.pimpl_->dh_;
    pimpl_->dh_.GenerateKeyPair(pimpl_->ranPool_, pimpl_->privateKey_,
                                                  pimpl_->publicKey_);
    return *this;
}


unknown's avatar
unknown committed
858
void DiffieHellman::makeAgreement(const byte* other, unsigned int otherSz)
859
{
unknown's avatar
unknown committed
860
    pimpl_->dh_.Agree(pimpl_->agreedKey_, pimpl_->privateKey_, other, otherSz); 
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
}


uint DiffieHellman::get_agreedKeyLength() const
{
    return pimpl_->dh_.GetByteLength();
}


const byte* DiffieHellman::get_agreedKey() const
{
    return pimpl_->agreedKey_;
}


const byte* DiffieHellman::get_publicKey() const
{
    return pimpl_->publicKey_;
}


void DiffieHellman::set_sizes(int& pSz, int& gSz, int& pubSz) const
{
    using TaoCrypt::Integer;
    Integer p = pimpl_->dh_.GetP();
    Integer g = pimpl_->dh_.GetG();

    pSz   = p.ByteCount();
    gSz   = g.ByteCount();
    pubSz = pimpl_->dh_.GetByteLength();
}


void DiffieHellman::get_parms(byte* bp, byte* bg, byte* bpub) const
{
    using TaoCrypt::Integer;
    Integer p = pimpl_->dh_.GetP();
    Integer g = pimpl_->dh_.GetG();

    p.Encode(bp, p.ByteCount());
    g.Encode(bg, g.ByteCount());
    memcpy(bpub, pimpl_->publicKey_, pimpl_->dh_.GetByteLength());
}


// convert PEM file to DER x509 type
x509* PemToDer(const char* fname, CertType type)
{
    using namespace TaoCrypt;

    char header[80];
    char footer[80];

    if (type == Cert) {
        strncpy(header, "-----BEGIN CERTIFICATE-----", sizeof(header));
        strncpy(footer, "-----END CERTIFICATE-----", sizeof(footer));
    } else {
        strncpy(header, "-----BEGIN RSA PRIVATE KEY-----", sizeof(header));
        strncpy(footer, "-----END RSA PRIVATE KEY-----", sizeof(header));
    }

    FILE* file = fopen(fname, "rb");
    if (!file)
        return 0;

    long begin = -1;
    long end   = 0;
    bool foundEnd = false;

    char line[80];

    while(fgets(line, sizeof(line), file))
        if (strncmp(header, line, strlen(header)) == 0) {
            begin = ftell(file);
            break;
        }

    while(fgets(line, sizeof(line), file))
        if (strncmp(footer, line, strlen(footer)) == 0) {
            foundEnd = true;
            break;
        }
        else
            end = ftell(file);

    if (begin == -1 || !foundEnd) {
        fclose(file);
        return 0;
    }

    input_buffer tmp(end - begin);
    fseek(file, begin, SEEK_SET);
    size_t bytes = fread(tmp.get_buffer(), end - begin, 1, file);
    if (bytes != 1) {
        fclose(file);
        return 0;
    }
    
    Source der(tmp.get_buffer(), end - begin);
    Base64Decoder b64Dec(der);

    uint sz = der.size();
unknown's avatar
unknown committed
963
    mySTL::auto_ptr<x509> x(NEW_YS x509(sz), ysDelete);
964 965 966 967 968 969 970 971 972
    memcpy(x->use_buffer(), der.get_buffer(), sz);

    fclose(file);
    return x.release();
}


} // namespace

973

974
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
namespace yaSSL {
template void ysDelete<DiffieHellman::DHImpl>(DiffieHellman::DHImpl*);
template void ysDelete<Integer::IntegerImpl>(Integer::IntegerImpl*);
template void ysDelete<RSA::RSAImpl>(RSA::RSAImpl*);
template void ysDelete<DSS::DSSImpl>(DSS::DSSImpl*);
template void ysDelete<RandomPool::RandomImpl>(RandomPool::RandomImpl*);
template void ysDelete<AES::AESImpl>(AES::AESImpl*);
template void ysDelete<RC4::RC4Impl>(RC4::RC4Impl*);
template void ysDelete<DES_EDE::DES_EDEImpl>(DES_EDE::DES_EDEImpl*);
template void ysDelete<DES::DESImpl>(DES::DESImpl*);
template void ysDelete<HMAC_RMD::HMAC_RMDImpl>(HMAC_RMD::HMAC_RMDImpl*);
template void ysDelete<HMAC_SHA::HMAC_SHAImpl>(HMAC_SHA::HMAC_SHAImpl*);
template void ysDelete<HMAC_MD5::HMAC_MD5Impl>(HMAC_MD5::HMAC_MD5Impl*);
template void ysDelete<RMD::RMDImpl>(RMD::RMDImpl*);
template void ysDelete<SHA::SHAImpl>(SHA::SHAImpl*);
template void ysDelete<MD5::MD5Impl>(MD5::MD5Impl*);
}
992
#endif // HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
993

994
#endif // !USE_CRYPTOPP_LIB