| OLD | NEW |
| 1 /* crypto/evp/e_des.c */ | 1 /* crypto/evp/e_des.c */ |
| 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
| 6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
| 7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
| 8 * | 8 * |
| 9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
| 10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 #include <openssl/des.h> | 65 #include <openssl/des.h> |
| 66 #include <openssl/rand.h> | 66 #include <openssl/rand.h> |
| 67 | 67 |
| 68 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 68 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 69 const unsigned char *iv, int enc); | 69 const unsigned char *iv, int enc); |
| 70 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); | 70 static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); |
| 71 | 71 |
| 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); |
| 79 return 1; | 79 return 1; |
| 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 *)ct
x->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 } |
| 104 | 132 |
| 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 |
| 150 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 195 static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
| 151 const unsigned char *iv, int enc) | 196 const unsigned char *iv, int enc) |
| 152 { | 197 { |
| 153 DES_cblock *deskey = (DES_cblock *)key; | 198 DES_cblock *deskey = (DES_cblock *)key; |
| 154 #ifdef EVP_CHECK_DES_KEY | 199 #ifdef EVP_CHECK_DES_KEY |
| 155 if(DES_set_key_checked(deskey,ctx->cipher_data) != 0) | 200 if(DES_set_key_checked(deskey,ctx->cipher_data) != 0) |
| 156 return 0; | 201 return 0; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 170 return 0; | 215 return 0; |
| 171 DES_set_odd_parity((DES_cblock *)ptr); | 216 DES_set_odd_parity((DES_cblock *)ptr); |
| 172 return 1; | 217 return 1; |
| 173 | 218 |
| 174 default: | 219 default: |
| 175 return -1; | 220 return -1; |
| 176 } | 221 } |
| 177 } | 222 } |
| 178 | 223 |
| 179 #endif | 224 #endif |
| OLD | NEW |