6 static const char *mode_cbc_encrypt(Byte *data, int blocks,
7 const Byte *iv, Byte *chain,
8 const BlockCipherAlgInfo *alg, int encr,
10 int blocksize= alg->blocksize;
11 memcpy(chain,iv,blocksize);
14 memxor(data, chain, blocksize);
15 alg->encrypt.crypt(sch, data, data);
16 memcpy(chain, data, blocksize);
18 blocks--; data += blocksize;
23 static const char *mode_cbc_decrypt(Byte *data, int blocks,
24 const Byte *iv, Byte *chain,
25 const BlockCipherAlgInfo *alg, int encr,
27 int blocksize= alg->blocksize;
30 memcpy(chain,iv,blocksize);
33 memcpy(chain + (cchain^blocksize), data, blocksize);
34 alg->decrypt.crypt(sch, data, data);
35 memxor(data, chain + cchain, blocksize);
38 blocks--; data += blocksize;
43 static void cbcmac_core(const Byte *data, int blocks,
44 const Byte *iv, Byte *buf,
45 const BlockCipherAlgInfo *alg,
47 int blocksize= alg->blocksize;
49 memcpy(buf,iv,blocksize);
52 memcpy(buf + blocksize, data, blocksize);
53 memxor(buf, buf + blocksize, blocksize);
55 alg->encrypt.crypt(sch, buf, buf);
57 blocks--; data += blocksize;
61 static const char *mode_cbc_mac(const Byte *data, int blocks,
62 const Byte *iv, Byte *buf,
63 const BlockCipherAlgInfo *alg,
65 cbcmac_core(data,blocks,iv,buf,alg,sch);
69 static const char *mode_cbc_mac2(const Byte *data, int blocks,
70 const Byte *iv, Byte *buf,
71 const BlockCipherAlgInfo *alg,
73 cbcmac_core(data,blocks,iv,buf,alg,sch);
74 alg->encrypt.crypt(sch, buf, buf);
78 static const char *mode_ecb(Byte *data, int blocks,
79 const Byte *iv, Byte *chain,
80 const BlockCipherAlgInfo *alg, int encr,
82 int blocksize= alg->blocksize;
85 (encr ? &alg->encrypt : &alg->decrypt)->crypt(sch, data, data);
86 blocks--; data += blocksize;
91 static const char *mode_ctr(Byte *data, int blocks,
92 const Byte *iv, Byte *counter,
93 const BlockCipherAlgInfo *alg, int encr,
95 int blocksize= alg->blocksize;
96 Byte *cipher= counter + blocksize;
99 memcpy(counter, iv, blocksize);
101 alg->encrypt.crypt(sch, counter, cipher);
102 memxor(data, cipher, blocksize);
103 for (byte=blocksize-1; byte>=0; byte--) {
104 if (++counter[byte]) break;
105 /* new value of zero implies carry, so increment next byte */
113 const BlockCipherModeInfo blockciphermodeinfos[]= {
114 { "cbc", 1, 2, 1, mode_cbc_encrypt, mode_cbc_decrypt, mode_cbc_mac },
115 { "cbc-mac2", 1, 2, 1, 0, 0, mode_cbc_mac2 },
116 { "ecb", 0, 0, 0, mode_ecb, mode_ecb, 0 },
117 { "ctr-sif", 1, 2, 0, mode_ctr, mode_ctr, 0 },