diff options
Diffstat (limited to 'src/lib/libcrypto/des/cfb_enc.c')
| -rw-r--r-- | src/lib/libcrypto/des/cfb_enc.c | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/src/lib/libcrypto/des/cfb_enc.c b/src/lib/libcrypto/des/cfb_enc.c index 17bf77ca9e..2600bdfc93 100644 --- a/src/lib/libcrypto/des/cfb_enc.c +++ b/src/lib/libcrypto/des/cfb_enc.c | |||
| @@ -64,32 +64,22 @@ | |||
| 64 | * the second. The second 12 bits will come from the 3rd and half the 4th | 64 | * the second. The second 12 bits will come from the 3rd and half the 4th |
| 65 | * byte. | 65 | * byte. |
| 66 | */ | 66 | */ |
| 67 | /* WARNING WARNING: this uses in and out in 8-byte chunks regardless of | ||
| 68 | * length */ | ||
| 69 | /* Until Aug 1 2003 this function did not correctly implement CFB-r, so it | ||
| 70 | * will not be compatible with any encryption prior to that date. Ben. */ | ||
| 67 | void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, | 71 | void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, |
| 68 | long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc) | 72 | long length, DES_key_schedule *schedule, DES_cblock *ivec, |
| 73 | int enc) | ||
| 69 | { | 74 | { |
| 70 | register DES_LONG d0,d1,v0,v1,n=(numbits+7)/8; | 75 | register DES_LONG d0,d1,v0,v1,n=(numbits+7)/8; |
| 71 | register DES_LONG mask0,mask1; | ||
| 72 | register unsigned long l=length; | 76 | register unsigned long l=length; |
| 73 | register int num=numbits; | 77 | register int num=numbits; |
| 74 | DES_LONG ti[2]; | 78 | DES_LONG ti[2]; |
| 75 | unsigned char *iv; | 79 | unsigned char *iv; |
| 80 | unsigned char ovec[16]; | ||
| 76 | 81 | ||
| 77 | if (num > 64) return; | 82 | if (num > 64) return; |
| 78 | if (num > 32) | ||
| 79 | { | ||
| 80 | mask0=0xffffffffL; | ||
| 81 | if (num == 64) | ||
| 82 | mask1=mask0; | ||
| 83 | else mask1=(1L<<(num-32))-1; | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | if (num == 32) | ||
| 88 | mask0=0xffffffffL; | ||
| 89 | else mask0=(1L<<num)-1; | ||
| 90 | mask1=0x00000000L; | ||
| 91 | } | ||
| 92 | |||
| 93 | iv = &(*ivec)[0]; | 83 | iv = &(*ivec)[0]; |
| 94 | c2l(iv,v0); | 84 | c2l(iv,v0); |
| 95 | c2l(iv,v1); | 85 | c2l(iv,v1); |
| @@ -103,8 +93,8 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, | |||
| 103 | DES_encrypt1((DES_LONG *)ti,schedule,DES_ENCRYPT); | 93 | DES_encrypt1((DES_LONG *)ti,schedule,DES_ENCRYPT); |
| 104 | c2ln(in,d0,d1,n); | 94 | c2ln(in,d0,d1,n); |
| 105 | in+=n; | 95 | in+=n; |
| 106 | d0=(d0^ti[0])&mask0; | 96 | d0^=ti[0]; |
| 107 | d1=(d1^ti[1])&mask1; | 97 | d1^=ti[1]; |
| 108 | l2cn(d0,d1,out,n); | 98 | l2cn(d0,d1,out,n); |
| 109 | out+=n; | 99 | out+=n; |
| 110 | /* 30-08-94 - eay - changed because l>>32 and | 100 | /* 30-08-94 - eay - changed because l>>32 and |
| @@ -113,15 +103,25 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, | |||
| 113 | { v0=v1; v1=d0; } | 103 | { v0=v1; v1=d0; } |
| 114 | else if (num == 64) | 104 | else if (num == 64) |
| 115 | { v0=d0; v1=d1; } | 105 | { v0=d0; v1=d1; } |
| 116 | else if (num > 32) /* && num != 64 */ | 106 | else |
| 117 | { | ||
| 118 | v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL; | ||
| 119 | v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL; | ||
| 120 | } | ||
| 121 | else /* num < 32 */ | ||
| 122 | { | 107 | { |
| 123 | v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL; | 108 | iv=&ovec[0]; |
| 124 | v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL; | 109 | l2c(v0,iv); |
| 110 | l2c(v1,iv); | ||
| 111 | l2c(d0,iv); | ||
| 112 | l2c(d1,iv); | ||
| 113 | /* shift ovec left most of the bits... */ | ||
| 114 | memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0)); | ||
| 115 | /* now the remaining bits */ | ||
| 116 | if(num%8 != 0) | ||
| 117 | for(n=0 ; n < 8 ; ++n) | ||
| 118 | { | ||
| 119 | ovec[n]<<=num%8; | ||
| 120 | ovec[n]|=ovec[n+1]>>(8-num%8); | ||
| 121 | } | ||
| 122 | iv=&ovec[0]; | ||
| 123 | c2l(iv,v0); | ||
| 124 | c2l(iv,v1); | ||
| 125 | } | 125 | } |
| 126 | } | 126 | } |
| 127 | } | 127 | } |
| @@ -141,18 +141,28 @@ void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, | |||
| 141 | { v0=v1; v1=d0; } | 141 | { v0=v1; v1=d0; } |
| 142 | else if (num == 64) | 142 | else if (num == 64) |
| 143 | { v0=d0; v1=d1; } | 143 | { v0=d0; v1=d1; } |
| 144 | else if (num > 32) /* && num != 64 */ | 144 | else |
| 145 | { | ||
| 146 | v0=((v1>>(num-32))|(d0<<(64-num)))&0xffffffffL; | ||
| 147 | v1=((d0>>(num-32))|(d1<<(64-num)))&0xffffffffL; | ||
| 148 | } | ||
| 149 | else /* num < 32 */ | ||
| 150 | { | 145 | { |
| 151 | v0=((v0>>num)|(v1<<(32-num)))&0xffffffffL; | 146 | iv=&ovec[0]; |
| 152 | v1=((v1>>num)|(d0<<(32-num)))&0xffffffffL; | 147 | l2c(v0,iv); |
| 148 | l2c(v1,iv); | ||
| 149 | l2c(d0,iv); | ||
| 150 | l2c(d1,iv); | ||
| 151 | /* shift ovec left most of the bits... */ | ||
| 152 | memmove(ovec,ovec+num/8,8+(num%8 ? 1 : 0)); | ||
| 153 | /* now the remaining bits */ | ||
| 154 | if(num%8 != 0) | ||
| 155 | for(n=0 ; n < 8 ; ++n) | ||
| 156 | { | ||
| 157 | ovec[n]<<=num%8; | ||
| 158 | ovec[n]|=ovec[n+1]>>(8-num%8); | ||
| 159 | } | ||
| 160 | iv=&ovec[0]; | ||
| 161 | c2l(iv,v0); | ||
| 162 | c2l(iv,v1); | ||
| 153 | } | 163 | } |
| 154 | d0=(d0^ti[0])&mask0; | 164 | d0^=ti[0]; |
| 155 | d1=(d1^ti[1])&mask1; | 165 | d1^=ti[1]; |
| 156 | l2cn(d0,d1,out,n); | 166 | l2cn(d0,d1,out,n); |
| 157 | out+=n; | 167 | out+=n; |
| 158 | } | 168 | } |
