Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::blowfish Class Reference

#include <blowfish.hpp>

Public Types

enum  { ECB =0 , CBC =1 , CFB =2 }
 

Public Member Functions

 blowfish ()
 
void start (unsigned char *ucKey, uint64_t n, const sblock &roChain=sblock(0UL, 0UL))
 
void reset_chain ()
 
void encrypt (unsigned char *buf, uint64_t n, int iMode=CFB)
 
void decrypt (unsigned char *buf, uint64_t n, int iMode=CFB)
 
void encrypt (const unsigned char *in, unsigned char *out, uint64_t n, int iMode=CFB)
 
void decrypt (const unsigned char *in, unsigned char *out, uint64_t n, int iMode=CFB)
 

Detailed Description

Definition at line 139 of file blowfish.hpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
ECB 
CBC 
CFB 

Definition at line 142 of file blowfish.hpp.

Constructor & Destructor Documentation

◆ blowfish()

fc::blowfish::blowfish ( )

Definition at line 303 of file blowfish.cpp.

304{}

Member Function Documentation

◆ decrypt() [1/2]

void fc::blowfish::decrypt ( const unsigned char * in,
unsigned char * out,
uint64_t n,
int iMode = CFB )

Definition at line 581 of file blowfish.cpp.

582{
583 //Check the buffer's length - should be > 0 and multiple of 8
584 if((n==0)||(n%8!=0))
585 FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) );
586 sblock work;
587 if(iMode == CBC) //CBC mode, using the Chain
588 {
589 sblock crypt, chain(m_oChain);
590 for(; n >= 8; n -= 8, in += 8)
591 {
592 BytesToBlock(in, work);
593 crypt = work;
594 decrypt(work);
595 work ^= chain;
596 chain = crypt;
597 BlockToBytes(work, out+=8);
598 }
599 }
600 else if(iMode == CFB) //CFB mode, using the Chain, not using decrypt()
601 {
602 sblock crypt, chain(m_oChain);
603 for(; n >= 8; n -= 8, in += 8)
604 {
605 BytesToBlock(in, work);
606 encrypt(chain);
607 crypt = work;
608 work ^= chain;
609 chain = crypt;
610 BlockToBytes(work, out+=8);
611 }
612 }
613 else //ECB mode, not using the Chain
614 {
615 for(; n >= 8; n -= 8, in += 8)
616 {
617 BytesToBlock(in, work);
618 decrypt(work);
619 BlockToBytes(work, out+=8);
620 }
621 }
622}
void decrypt(unsigned char *buf, uint64_t n, int iMode=CFB)
Definition blowfish.cpp:493
void encrypt(unsigned char *buf, uint64_t n, int iMode=CFB)
Definition blowfish.cpp:450
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
void BlockToBytes(sblock const &b, unsigned char *p)
Definition blowfish.cpp:425
void BytesToBlock(unsigned char const *p, sblock &b)
Definition blowfish.cpp:394
Here is the call graph for this function:

◆ decrypt() [2/2]

void fc::blowfish::decrypt ( unsigned char * buf,
uint64_t n,
int iMode = CFB )

Definition at line 493 of file blowfish.cpp.

494{
495 //Check the buffer's length - should be > 0 and multiple of 8
496 if((n==0)||(n%8!=0))
497 FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) );
498 sblock work;
499 if(iMode == CBC) //CBC mode, using the Chain
500 {
501 sblock crypt, chain(m_oChain);
502 for(; n >= 8; n -= 8)
503 {
504 BytesToBlock(buf, work);
505 crypt = work;
506 decrypt(work);
507 work ^= chain;
508 chain = crypt;
509 BlockToBytes(work, buf+=8);
510 }
511 }
512 else if(iMode == CFB) //CFB mode, using the Chain, not using decrypt()
513 {
514 sblock crypt, chain(m_oChain);
515 for(; n >= 8; n -= 8)
516 {
517 BytesToBlock(buf, work);
518 encrypt(chain);
519 crypt = work;
520 work ^= chain;
521 chain = crypt;
522 BlockToBytes(work, buf+=8);
523 }
524 }
525 else //ECB mode, not using the Chain
526 {
527 for(; n >= 8; n -= 8)
528 {
529 BytesToBlock(buf, work);
530 decrypt(work);
531 BlockToBytes(work, buf+=8);
532 }
533 }
534}
uint8_t buf[2048]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encrypt() [1/2]

void fc::blowfish::encrypt ( const unsigned char * in,
unsigned char * out,
uint64_t n,
int iMode = CFB )

Definition at line 538 of file blowfish.cpp.

539{
540 //Check the buffer's length - should be > 0 and multiple of 8
541 if((n==0)||(n%8!=0))
542 FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) );
543 sblock work;
544 if(iMode == CBC) //CBC mode, using the Chain
545 {
546 sblock chain(m_oChain);
547 for(; n >= 8; n -= 8, in += 8)
548 {
549 BytesToBlock(in, work);
550 work ^= chain;
551 encrypt(work);
552 chain = work;
553 BlockToBytes(work, out+=8);
554 }
555 }
556 else if(iMode == CFB) //CFB mode, using the Chain
557 {
558 sblock chain(m_oChain);
559 for(; n >= 8; n -= 8, in += 8)
560 {
561 encrypt(chain);
562 BytesToBlock(in, work);
563 work ^= chain;
564 chain = work;
565 BlockToBytes(work, out+=8);
566 }
567 }
568 else //ECB mode, not using the Chain
569 {
570 for(; n >= 8; n -= 8, in += 8)
571 {
572 BytesToBlock(in, work);
573 encrypt(work);
574 BlockToBytes(work, out+=8);
575 }
576 }
577}
Here is the call graph for this function:

◆ encrypt() [2/2]

void fc::blowfish::encrypt ( unsigned char * buf,
uint64_t n,
int iMode = CFB )

Definition at line 450 of file blowfish.cpp.

451{
452 //Check the buffer's length - should be > 0 and multiple of 8
453 if((n==0)||(n%8!=0))
454 FC_THROW_EXCEPTION( exception, "invalid buffer length ${n}, not multiple of 8", ("n", n) );
455 sblock work;
456 if(iMode == CBC) //CBC mode, using the Chain
457 {
458 sblock chain(m_oChain);
459 for(; n >= 8; n -= 8)
460 {
461 BytesToBlock(buf, work);
462 work ^= chain;
463 encrypt(work);
464 chain = work;
465 BlockToBytes(work, buf+=8);
466 }
467 }
468 else if(iMode == CFB) //CFB mode, using the Chain
469 {
470 sblock chain(m_oChain);
471 for(; n >= 8; n -= 8)
472 {
473 encrypt(chain);
474 BytesToBlock(buf, work);
475 work ^= chain;
476 chain = work;
477 BlockToBytes(work, buf+=8);
478 }
479 }
480 else //ECB mode, not using the Chain
481 {
482 for(; n >= 8; n -= 8)
483 {
484 BytesToBlock(buf, work);
485 encrypt(work);
486 BlockToBytes(work, buf+=8);
487 }
488 }
489}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset_chain()

void fc::blowfish::reset_chain ( )
inline

Definition at line 149 of file blowfish.hpp.

149{ m_oChain = m_oChain0; }

◆ start()

void fc::blowfish::start ( unsigned char * ucKey,
uint64_t n,
const sblock & roChain = sblock(0UL,0UL) )

Definition at line 306 of file blowfish.cpp.

307{
308 m_oChain0 = roChain;
309 m_oChain = roChain;
310
311 if(keysize<1)
312 FC_THROW_EXCEPTION( exception, "invalid key length" );
313 //Check the Key - the key length should be between 1 and 56 bytes
314 if(keysize>56)
315 keysize = 56;
316 unsigned char aucLocalKey[56];
317 unsigned int i, j;
318 memcpy(aucLocalKey, ucKey, static_cast<size_t>(keysize));
319 //Reflexive Initialization of the Blowfish.
320 //Generating the Subkeys from the Key flood P and S boxes with PI
321 memcpy(m_auiP, scm_auiInitP, sizeof m_auiP);
322 memcpy(m_auiS, scm_auiInitS, sizeof m_auiS);
323 //Load P boxes with key bytes
324 const unsigned char* p = aucLocalKey;
325 unsigned int x=0;
326 //Repeatedly cycle through the key bits until the entire P array has been XORed with key bits
327 uint32_t iCount = 0;
328 for(i=0; i<18; i++)
329 {
330 x=0;
331 for(int n=4; n--; )
332 {
333 //int iVal = (int)(*p);
334 x <<= 8;
335 x |= *(p++);
336 iCount++;
337 if(iCount == keysize)
338 {
339 //All bytes used, so recycle bytes
340 iCount = 0;
341 p = aucLocalKey;
342 }
343 }
344 m_auiP[i] ^= x;
345 }
346 //Reflect P and S boxes through the evolving Blowfish
347 sblock block(0UL,0UL); //all-zero block
348 for(i=0; i<18; )
349 encrypt(block), m_auiP[i++] = block.m_uil, m_auiP[i++] = block.m_uir;
350 for(j=0; j<4; j++)
351 for(int k=0; k<256; )
352 encrypt(block), m_auiS[j][k++] = block.m_uil, m_auiS[j][k++] = block.m_uir;
353}
const mie::Vuint & p
Definition bn.cpp:27
unsigned int uint32_t
Definition stdint.h:126
uint16_t j
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

The documentation for this class was generated from the following files: