⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serpent.c

📁 开源的目录加密软件
💻 C
📖 第 1 页 / 共 2 页
字号:
    skey->serpent.K[4 * (r) + 1] = b; \    skey->serpent.K[4 * (r) + 2] = c; \    skey->serpent.K[4 * (r) + 3] = d/* the linear transformation and its inverse    */#define rot(a,b,c,d)    \    a = ROL(a, 13);    \    c = ROL(c, 3);     \    d ^= c ^ (a << 3);  \    b ^= a ^ c;         \    d = ROL(d, 7);     \    b = ROL(b, 1);     \    a ^= b ^ d;         \    c ^= d ^ (b << 7);  \    a = ROL(a, 5);     \    c = ROL(c, 22)#define irot(a,b,c,d)   \    c = ROR(c, 22);    \    a = ROR(a, 5);     \    c ^= d ^ (b << 7);  \    a ^= b ^ d;         \    d = ROR(d, 7);     \    b = ROR(b, 1);     \    d ^= c ^ (a << 3);  \    b ^= a ^ c;         \    c = ROR(c, 3);     \    a = ROR(a, 13)/**  Initialize the Serpent block cipher  @param key The symmetric key you wish to pass  @param keylen The key length in bytes  @param num_rounds The number of rounds desired (0 for default)  @param skey The key in as scheduled by this function.  @return CRYPT_OK if successful */#ifdef LTC_CLEAN_STACKstatic int _serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)#elseint serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)#endif{    unsigned long lkey[140], t, a, b, c, d, e, f, g, h, x;    unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;    unsigned char buf[32];    LTC_ARGCHK(key != NULL);    LTC_ARGCHK(skey != NULL);    /* check rounds */    if (num_rounds != 0 && num_rounds != 32) {       return CRYPT_INVALID_ROUNDS;    }    /* check keylen */    if (keylen < 16 || keylen > 32) {       return CRYPT_INVALID_KEYSIZE;    }    /* copy key and expand to 32bytes as required */    for (x = 0; x < (unsigned long)keylen; x++) {        buf[x] = key[x];    }    if (x < 32) {       buf[x++] = (unsigned char)0x01;       while (x < 32) {           buf[x++] = (unsigned char)0;       }    }    /* copy key into 32-bit words */    for (x = 0; x < 8; x++) {        LOAD32L(lkey[x], &buf[x*4]);    }    /* expand using the LFSR to 140 words */    for (x = 0; x < 132; x++) {        t = lkey[x] ^ lkey[x+3] ^ lkey[x+5] ^ lkey[x+7] ^ x ^ 0x9E3779B9UL;        lkey[x + 8] = ROL(t, 11);    }    /* perform the substituions */    for (x = 0; x < 32; ) {       k_set( x,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb2(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb1(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb0(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb7(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb6(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb5(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;       k_set( x,a,b,c,d);sb4(a,b,c,d,e,f,g,h);k_get( x,e,f,g,h); ++x;    }    k_set(32,a,b,c,d);sb3(a,b,c,d,e,f,g,h);k_get(32,e,f,g,h);    return CRYPT_OK;}#ifdef LTC_CLEAN_STACKint serpent_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey){   int x;   x = _serpent_setup(key, keylen, num_rounds, skey);   burn_stack(sizeof(unsigned long)*166 + sizeof(unsigned char)*32);   return x;}#endif/**  Encrypts a block of text with Serpent  @param pt The input plaintext (16 bytes)  @param ct The output ciphertext (16 bytes)  @param skey The key as scheduled  @return CRYPT_OK if successful*/#ifdef LTC_CLEAN_STACKstatic int _serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)#elseint serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)#endif{    unsigned long a,b,c,d,e,f,g,h;    unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;    LTC_ARGCHK(pt != NULL);    LTC_ARGCHK(ct != NULL);    LTC_ARGCHK(skey != NULL);    LOAD32L(a, &pt[0]);LOAD32L(b, &pt[4]);LOAD32L(c, &pt[8]);LOAD32L(d, &pt[12]);    k_xor( 0,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor( 1,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor( 2,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor( 3,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor( 4,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor( 5,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor( 6,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor( 7,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor( 8,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor( 9,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);     k_xor(10,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(11,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(12,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(13,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(14,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(15,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(16,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(17,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(18,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(19,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(20,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(21,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(22,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(23,e,f,g,h); sb7(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(24,a,b,c,d); sb0(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(25,e,f,g,h); sb1(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(26,a,b,c,d); sb2(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(27,e,f,g,h); sb3(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(28,a,b,c,d); sb4(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(29,e,f,g,h); sb5(e,f,g,h,a,b,c,d); rot(a,b,c,d);    k_xor(30,a,b,c,d); sb6(a,b,c,d,e,f,g,h); rot(e,f,g,h);    k_xor(31,e,f,g,h); sb7(e,f,g,h,a,b,c,d); k_xor(32,a,b,c,d);    STORE32L(a, &ct[0]);STORE32L(b, &ct[4]);STORE32L(c, &ct[8]);STORE32L(d, &ct[12]);	return CRYPT_OK;}#ifdef LTC_CLEAN_STACKint serpent_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey){   int err = _serpent_ecb_encrypt(pt, ct, skey);   burn_stack(sizeof(unsigned long)*24);   return err;}#endif/**  Decrypts a block of text with Serpent  @param ct The input ciphertext (16 bytes)  @param pt The output plaintext (16 bytes)  @param skey The key as scheduled   @return CRYPT_OK if successful*/#ifdef LTC_CLEAN_STACKstatic int _serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)#elseint serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)#endif{    unsigned long a,b,c,d,e,f,g,h;    unsigned long t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16;    LTC_ARGCHK(pt != NULL);    LTC_ARGCHK(ct != NULL);    LTC_ARGCHK(skey != NULL);    LOAD32L(a, &ct[0]);LOAD32L(b, &ct[4]);LOAD32L(c, &ct[8]);LOAD32L(d, &ct[12]);    k_xor(32,a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(31,e,f,g,h);    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(30,a,b,c,d);    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(29,e,f,g,h);    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(28,a,b,c,d);    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(27,e,f,g,h);    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(26,a,b,c,d);    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(25,e,f,g,h);    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(24,a,b,c,d);    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(23,e,f,g,h);    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(22,a,b,c,d);    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(21,e,f,g,h);    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(20,a,b,c,d);    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(19,e,f,g,h);    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(18,a,b,c,d);    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor(17,e,f,g,h);    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor(16,a,b,c,d);    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor(15,e,f,g,h);    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor(14,a,b,c,d);    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor(13,e,f,g,h);    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor(12,a,b,c,d);    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor(11,e,f,g,h);    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor(10,a,b,c,d);    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 9,e,f,g,h);    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 8,a,b,c,d);    irot(a,b,c,d); ib7(a,b,c,d,e,f,g,h); k_xor( 7,e,f,g,h);    irot(e,f,g,h); ib6(e,f,g,h,a,b,c,d); k_xor( 6,a,b,c,d);    irot(a,b,c,d); ib5(a,b,c,d,e,f,g,h); k_xor( 5,e,f,g,h);    irot(e,f,g,h); ib4(e,f,g,h,a,b,c,d); k_xor( 4,a,b,c,d);    irot(a,b,c,d); ib3(a,b,c,d,e,f,g,h); k_xor( 3,e,f,g,h);    irot(e,f,g,h); ib2(e,f,g,h,a,b,c,d); k_xor( 2,a,b,c,d);    irot(a,b,c,d); ib1(a,b,c,d,e,f,g,h); k_xor( 1,e,f,g,h);    irot(e,f,g,h); ib0(e,f,g,h,a,b,c,d); k_xor( 0,a,b,c,d);    STORE32L(a, &pt[0]);STORE32L(b, &pt[4]);STORE32L(c, &pt[8]);STORE32L(d, &pt[12]);	return CRYPT_OK;}#ifdef LTC_CLEAN_STACKint serpent_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey){   int err = _serpent_ecb_decrypt(ct, pt, skey);   burn_stack(sizeof(unsigned long)*24);   return err;}#endif/**  Performs a self-test of the Twofish block cipher  @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled*/int serpent_test(void){ #ifndef LTC_TEST    return CRYPT_NOP; #else       static const struct {       int keylen;       unsigned char key[32], pt[16], ct[16];   } tests[] = {   {      16,      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0xdd, 0xd2, 0x6b, 0x98, 0xa5, 0xff, 0xd8, 0x2c,        0x05, 0x34, 0x5a, 0x9d, 0xad, 0xbf, 0xaf, 0x49 }   },   {      16,      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 },      { 0x4a, 0xe9, 0xa2, 0x0b, 0x2b, 0x14, 0xa1, 0x02,        0x90, 0xcb, 0xb8, 0x20, 0xb7, 0xff, 0xb5, 0x10 }   },   {      24,      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 },      { 0xe1, 0x1b, 0x01, 0x52, 0x4e, 0xa1, 0xf4, 0x65,         0xa2, 0xa2, 0x00, 0x43, 0xeb, 0x9f, 0x7e, 0x8a }   },   {      32,      { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0xe0, 0x88, 0x5d, 0x44, 0x60, 0x37, 0x34, 0x69,        0xd1, 0xfa, 0x6c, 0x36, 0xa6, 0xe1, 0xc5, 0x2f }   },   {      32,      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x17, 0xc6, 0x25, 0x8e, 0x60, 0x09, 0xe2, 0x82,        0x66, 0x18, 0x69, 0xd5, 0x25, 0xf7, 0xd2, 0x04 }   },   {      32,      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      { 0x9f, 0xe1, 0x43, 0x25, 0x0d, 0x00, 0xe2, 0x56,         0x96, 0xb0, 0x1e, 0x0a, 0x2e, 0xd0, 0x5d, 0xb3 }   }   };   unsigned char buf[2][16];   int x, err;   symmetric_key key;   for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) {      /* setup key */      if ((err = serpent_setup(tests[x].key, tests[x].keylen, 0, &key))!= CRYPT_OK) {         return err;      }      /* encrypt and decrypt */      serpent_ecb_encrypt(tests[x].pt, buf[0], &key);      serpent_ecb_decrypt(buf[0], buf[1], &key);      /* compare */      if (memcmp(buf[0], tests[x].ct, 16) != 0 || memcmp(buf[1], tests[x].pt, 16) != 0) {         return CRYPT_FAIL_TESTVECTOR;      }   }   return CRYPT_OK;  #endif}/** Terminate the context    @param skey    The scheduled key*/void serpent_done(symmetric_key *skey){}/**  Gets suitable key size  @param keysize [in/out] The length of the recommended key (in bytes).  This function will store the suitable size back in this variable.  @return CRYPT_OK if the input key size is acceptable.*/int serpent_keysize(int *keysize){   LTC_ARGCHK(keysize != NULL);   if (*keysize < 16)      return CRYPT_INVALID_KEYSIZE;   if (*keysize > 32)      *keysize = 32;   return CRYPT_OK;}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -