diff options
Diffstat (limited to 'src/lib/libcrypto/evp/e_des.c')
-rw-r--r-- | src/lib/libcrypto/evp/e_des.c | 91 |
1 files changed, 68 insertions, 23 deletions
diff --git a/src/lib/libcrypto/evp/e_des.c b/src/lib/libcrypto/evp/e_des.c index 04376df232..ca009f2c52 100644 --- a/src/lib/libcrypto/evp/e_des.c +++ b/src/lib/libcrypto/evp/e_des.c | |||
@@ -72,7 +72,7 @@ static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); | |||
72 | /* Because of various casts and different names can't use IMPLEMENT_BLOCK_CIPHER */ | 72 | /* Because of various casts and different names can't use IMPLEMENT_BLOCK_CIPHER */ |
73 | 73 | ||
74 | static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 74 | static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
75 | const unsigned char *in, unsigned int inl) | 75 | const unsigned char *in, size_t inl) |
76 | { | 76 | { |
77 | BLOCK_CIPHER_ecb_loop() | 77 | BLOCK_CIPHER_ecb_loop() |
78 | DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), ctx->cipher_data, ctx->encrypt); | 78 | DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i), ctx->cipher_data, ctx->encrypt); |
@@ -80,24 +80,52 @@ static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
80 | } | 80 | } |
81 | 81 | ||
82 | static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 82 | static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
83 | const unsigned char *in, unsigned int inl) | 83 | const unsigned char *in, size_t inl) |
84 | { | 84 | { |
85 | DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data, (DES_cblock *)ctx->iv, &ctx->num); | 85 | while(inl>=EVP_MAXCHUNK) |
86 | { | ||
87 | DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, | ||
88 | (DES_cblock *)ctx->iv, &ctx->num); | ||
89 | inl-=EVP_MAXCHUNK; | ||
90 | in +=EVP_MAXCHUNK; | ||
91 | out+=EVP_MAXCHUNK; | ||
92 | } | ||
93 | if (inl) | ||
94 | DES_ofb64_encrypt(in, out, (long)inl, ctx->cipher_data, | ||
95 | (DES_cblock *)ctx->iv, &ctx->num); | ||
86 | return 1; | 96 | return 1; |
87 | } | 97 | } |
88 | 98 | ||
89 | static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 99 | static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
90 | const unsigned char *in, unsigned int inl) | 100 | const unsigned char *in, size_t inl) |
91 | { | 101 | { |
92 | DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data, | 102 | while(inl>=EVP_MAXCHUNK) |
93 | (DES_cblock *)ctx->iv, ctx->encrypt); | 103 | { |
104 | DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK, ctx->cipher_data, | ||
105 | (DES_cblock *)ctx->iv, ctx->encrypt); | ||
106 | inl-=EVP_MAXCHUNK; | ||
107 | in +=EVP_MAXCHUNK; | ||
108 | out+=EVP_MAXCHUNK; | ||
109 | } | ||
110 | if (inl) | ||
111 | DES_ncbc_encrypt(in, out, (long)inl, ctx->cipher_data, | ||
112 | (DES_cblock *)ctx->iv, ctx->encrypt); | ||
94 | return 1; | 113 | return 1; |
95 | } | 114 | } |
96 | 115 | ||
97 | static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 116 | static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
98 | const unsigned char *in, unsigned int inl) | 117 | const unsigned char *in, size_t inl) |
99 | { | 118 | { |
100 | DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data, | 119 | while(inl>=EVP_MAXCHUNK) |
120 | { | ||
121 | DES_cfb64_encrypt(in,out, (long)EVP_MAXCHUNK, ctx->cipher_data, | ||
122 | (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); | ||
123 | inl-=EVP_MAXCHUNK; | ||
124 | in +=EVP_MAXCHUNK; | ||
125 | out+=EVP_MAXCHUNK; | ||
126 | } | ||
127 | if (inl) | ||
128 | DES_cfb64_encrypt(in, out, (long)inl, ctx->cipher_data, | ||
101 | (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); | 129 | (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt); |
102 | return 1; | 130 | return 1; |
103 | } | 131 | } |
@@ -105,45 +133,62 @@ static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | |||
105 | /* Although we have a CFB-r implementation for DES, it doesn't pack the right | 133 | /* Although we have a CFB-r implementation for DES, it doesn't pack the right |
106 | way, so wrap it here */ | 134 | way, so wrap it here */ |
107 | static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 135 | static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
108 | const unsigned char *in, unsigned int inl) | 136 | const unsigned char *in, size_t inl) |
109 | { | 137 | { |
110 | unsigned int n; | 138 | size_t n,chunk=EVP_MAXCHUNK/8; |
111 | unsigned char c[1],d[1]; | 139 | unsigned char c[1],d[1]; |
112 | 140 | ||
113 | for(n=0 ; n < inl ; ++n) | 141 | if (inl<chunk) chunk=inl; |
142 | |||
143 | while (inl && inl>=chunk) | ||
114 | { | 144 | { |
115 | c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; | 145 | for(n=0 ; n < chunk*8; ++n) |
116 | DES_cfb_encrypt(c,d,1,1,ctx->cipher_data,(DES_cblock *)ctx->iv, | 146 | { |
147 | c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0; | ||
148 | DES_cfb_encrypt(c,d,1,1,ctx->cipher_data,(DES_cblock *)ctx->iv, | ||
117 | ctx->encrypt); | 149 | ctx->encrypt); |
118 | out[n/8]=(out[n/8]&~(0x80 >> (n%8)))|((d[0]&0x80) >> (n%8)); | 150 | out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) | |
151 | ((d[0]&0x80) >> (unsigned int)(n%8)); | ||
152 | } | ||
153 | inl-=chunk; | ||
154 | in +=chunk; | ||
155 | out+=chunk; | ||
156 | if (inl<chunk) chunk=inl; | ||
119 | } | 157 | } |
158 | |||
120 | return 1; | 159 | return 1; |
121 | } | 160 | } |
122 | 161 | ||
123 | static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, | 162 | static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
124 | const unsigned char *in, unsigned int inl) | 163 | const unsigned char *in, size_t inl) |
125 | { | 164 | { |
126 | DES_cfb_encrypt(in,out,8,inl,ctx->cipher_data,(DES_cblock *)ctx->iv, | 165 | while (inl>=EVP_MAXCHUNK) |
127 | ctx->encrypt); | 166 | { |
167 | DES_cfb_encrypt(in,out,8,(long)EVP_MAXCHUNK,ctx->cipher_data, | ||
168 | (DES_cblock *)ctx->iv,ctx->encrypt); | ||
169 | inl-=EVP_MAXCHUNK; | ||
170 | in +=EVP_MAXCHUNK; | ||
171 | out+=EVP_MAXCHUNK; | ||
172 | } | ||
173 | if (inl) | ||
174 | DES_cfb_encrypt(in,out,8,(long)inl,ctx->cipher_data, | ||
175 | (DES_cblock *)ctx->iv,ctx->encrypt); | ||
128 | return 1; | 176 | return 1; |
129 | } | 177 | } |
130 | 178 | ||
131 | BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64, | 179 | BLOCK_CIPHER_defs(des, DES_key_schedule, NID_des, 8, 8, 8, 64, |
132 | EVP_CIPH_RAND_KEY, | 180 | EVP_CIPH_RAND_KEY, des_init_key, NULL, |
133 | des_init_key, NULL, | ||
134 | EVP_CIPHER_set_asn1_iv, | 181 | EVP_CIPHER_set_asn1_iv, |
135 | EVP_CIPHER_get_asn1_iv, | 182 | EVP_CIPHER_get_asn1_iv, |
136 | des_ctrl) | 183 | des_ctrl) |
137 | 184 | ||
138 | BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1, | 185 | BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,1, |
139 | EVP_CIPH_RAND_KEY, | 186 | EVP_CIPH_RAND_KEY, des_init_key,NULL, |
140 | des_init_key, NULL, | ||
141 | EVP_CIPHER_set_asn1_iv, | 187 | EVP_CIPHER_set_asn1_iv, |
142 | EVP_CIPHER_get_asn1_iv,des_ctrl) | 188 | EVP_CIPHER_get_asn1_iv,des_ctrl) |
143 | 189 | ||
144 | BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8, | 190 | BLOCK_CIPHER_def_cfb(des,DES_key_schedule,NID_des,8,8,8, |
145 | EVP_CIPH_RAND_KEY, | 191 | EVP_CIPH_RAND_KEY,des_init_key,NULL, |
146 | des_init_key,NULL, | ||
147 | EVP_CIPHER_set_asn1_iv, | 192 | EVP_CIPHER_set_asn1_iv, |
148 | EVP_CIPHER_get_asn1_iv,des_ctrl) | 193 | EVP_CIPHER_get_asn1_iv,des_ctrl) |
149 | 194 | ||