diff options
| author | beck <> | 2000-12-15 02:58:47 +0000 |
|---|---|---|
| committer | beck <> | 2000-12-15 02:58:47 +0000 |
| commit | 9200bb13d15da4b2a23e6bc92c20e95b74aa2113 (patch) | |
| tree | 5c52d628ec1e34be76e7ef2a4235d248b7c44d24 /src/lib/libcrypto/evp/evp_enc.c | |
| parent | e131d25072e3d4197ba4b9bcc0d1b27d34d6488d (diff) | |
| download | openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.tar.gz openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.tar.bz2 openbsd-9200bb13d15da4b2a23e6bc92c20e95b74aa2113.zip | |
openssl-engine-0.9.6 merge
Diffstat (limited to 'src/lib/libcrypto/evp/evp_enc.c')
| -rw-r--r-- | src/lib/libcrypto/evp/evp_enc.c | 149 |
1 files changed, 110 insertions, 39 deletions
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c index 5299a65b6a..e2687f9879 100644 --- a/src/lib/libcrypto/evp/evp_enc.c +++ b/src/lib/libcrypto/evp/evp_enc.c | |||
| @@ -59,6 +59,8 @@ | |||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
| 61 | #include <openssl/evp.h> | 61 | #include <openssl/evp.h> |
| 62 | #include <openssl/err.h> | ||
| 63 | #include "evp_locl.h" | ||
| 62 | 64 | ||
| 63 | const char *EVP_version="EVP" OPENSSL_VERSION_PTEXT; | 65 | const char *EVP_version="EVP" OPENSSL_VERSION_PTEXT; |
| 64 | 66 | ||
| @@ -68,55 +70,84 @@ void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) | |||
| 68 | /* ctx->cipher=NULL; */ | 70 | /* ctx->cipher=NULL; */ |
| 69 | } | 71 | } |
| 70 | 72 | ||
| 71 | void EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *data, | 73 | int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 72 | unsigned char *key, unsigned char *iv, int enc) | 74 | unsigned char *key, unsigned char *iv, int enc) |
| 73 | { | 75 | { |
| 74 | if (enc) | 76 | if(enc && (enc != -1)) enc = 1; |
| 75 | EVP_EncryptInit(ctx,data,key,iv); | 77 | if (cipher) { |
| 76 | else | 78 | ctx->cipher=cipher; |
| 77 | EVP_DecryptInit(ctx,data,key,iv); | 79 | ctx->key_len = cipher->key_len; |
| 80 | if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT) { | ||
| 81 | if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) { | ||
| 82 | EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } else if(!ctx->cipher) { | ||
| 87 | EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_NO_CIPHER_SET); | ||
| 88 | return 0; | ||
| 78 | } | 89 | } |
| 90 | if(!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) { | ||
| 91 | switch(EVP_CIPHER_CTX_mode(ctx)) { | ||
| 92 | |||
| 93 | case EVP_CIPH_STREAM_CIPHER: | ||
| 94 | case EVP_CIPH_ECB_MODE: | ||
| 95 | break; | ||
| 96 | |||
| 97 | case EVP_CIPH_CFB_MODE: | ||
| 98 | case EVP_CIPH_OFB_MODE: | ||
| 79 | 99 | ||
| 80 | void EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 100 | ctx->num = 0; |
| 101 | |||
| 102 | case EVP_CIPH_CBC_MODE: | ||
| 103 | |||
| 104 | if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx)); | ||
| 105 | memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx)); | ||
| 106 | break; | ||
| 107 | |||
| 108 | default: | ||
| 109 | return 0; | ||
| 110 | break; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) { | ||
| 115 | if(!ctx->cipher->init(ctx,key,iv,enc)) return 0; | ||
| 116 | } | ||
| 117 | if(enc != -1) ctx->encrypt=enc; | ||
| 118 | ctx->buf_len=0; | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | ||
| 81 | unsigned char *in, int inl) | 123 | unsigned char *in, int inl) |
| 82 | { | 124 | { |
| 83 | if (ctx->encrypt) | 125 | if (ctx->encrypt) |
| 84 | EVP_EncryptUpdate(ctx,out,outl,in,inl); | 126 | return EVP_EncryptUpdate(ctx,out,outl,in,inl); |
| 85 | else EVP_DecryptUpdate(ctx,out,outl,in,inl); | 127 | else return EVP_DecryptUpdate(ctx,out,outl,in,inl); |
| 86 | } | 128 | } |
| 87 | 129 | ||
| 88 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 130 | int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
| 89 | { | 131 | { |
| 90 | if (ctx->encrypt) | 132 | if (ctx->encrypt) |
| 91 | { | 133 | return EVP_EncryptFinal(ctx,out,outl); |
| 92 | EVP_EncryptFinal(ctx,out,outl); | ||
| 93 | return(1); | ||
| 94 | } | ||
| 95 | else return(EVP_DecryptFinal(ctx,out,outl)); | 134 | else return(EVP_DecryptFinal(ctx,out,outl)); |
| 96 | } | 135 | } |
| 97 | 136 | ||
| 98 | void EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 137 | int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 99 | unsigned char *key, unsigned char *iv) | 138 | unsigned char *key, unsigned char *iv) |
| 100 | { | 139 | { |
| 101 | if (cipher != NULL) | 140 | return EVP_CipherInit(ctx, cipher, key, iv, 1); |
| 102 | ctx->cipher=cipher; | ||
| 103 | ctx->cipher->init(ctx,key,iv,1); | ||
| 104 | ctx->encrypt=1; | ||
| 105 | ctx->buf_len=0; | ||
| 106 | } | 141 | } |
| 107 | 142 | ||
| 108 | void EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, | 143 | int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, |
| 109 | unsigned char *key, unsigned char *iv) | 144 | unsigned char *key, unsigned char *iv) |
| 110 | { | 145 | { |
| 111 | if (cipher != NULL) | 146 | return EVP_CipherInit(ctx, cipher, key, iv, 0); |
| 112 | ctx->cipher=cipher; | ||
| 113 | ctx->cipher->init(ctx,key,iv,0); | ||
| 114 | ctx->encrypt=0; | ||
| 115 | ctx->buf_len=0; | ||
| 116 | } | 147 | } |
| 117 | 148 | ||
| 118 | 149 | ||
| 119 | void EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 150 | int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
| 120 | unsigned char *in, int inl) | 151 | unsigned char *in, int inl) |
| 121 | { | 152 | { |
| 122 | int i,j,bl; | 153 | int i,j,bl; |
| @@ -124,20 +155,20 @@ void EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
| 124 | i=ctx->buf_len; | 155 | i=ctx->buf_len; |
| 125 | bl=ctx->cipher->block_size; | 156 | bl=ctx->cipher->block_size; |
| 126 | *outl=0; | 157 | *outl=0; |
| 127 | if ((inl == 0) && (i != bl)) return; | 158 | if ((inl == 0) && (i != bl)) return 1; |
| 128 | if (i != 0) | 159 | if (i != 0) |
| 129 | { | 160 | { |
| 130 | if (i+inl < bl) | 161 | if (i+inl < bl) |
| 131 | { | 162 | { |
| 132 | memcpy(&(ctx->buf[i]),in,inl); | 163 | memcpy(&(ctx->buf[i]),in,inl); |
| 133 | ctx->buf_len+=inl; | 164 | ctx->buf_len+=inl; |
| 134 | return; | 165 | return 1; |
| 135 | } | 166 | } |
| 136 | else | 167 | else |
| 137 | { | 168 | { |
| 138 | j=bl-i; | 169 | j=bl-i; |
| 139 | if (j != 0) memcpy(&(ctx->buf[i]),in,j); | 170 | if (j != 0) memcpy(&(ctx->buf[i]),in,j); |
| 140 | ctx->cipher->do_cipher(ctx,out,ctx->buf,bl); | 171 | if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,bl)) return 0; |
| 141 | inl-=j; | 172 | inl-=j; |
| 142 | in+=j; | 173 | in+=j; |
| 143 | out+=bl; | 174 | out+=bl; |
| @@ -148,16 +179,17 @@ void EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
| 148 | inl-=i; | 179 | inl-=i; |
| 149 | if (inl > 0) | 180 | if (inl > 0) |
| 150 | { | 181 | { |
| 151 | ctx->cipher->do_cipher(ctx,out,in,inl); | 182 | if(!ctx->cipher->do_cipher(ctx,out,in,inl)) return 0; |
| 152 | *outl+=inl; | 183 | *outl+=inl; |
| 153 | } | 184 | } |
| 154 | 185 | ||
| 155 | if (i != 0) | 186 | if (i != 0) |
| 156 | memcpy(ctx->buf,&(in[inl]),i); | 187 | memcpy(ctx->buf,&(in[inl]),i); |
| 157 | ctx->buf_len=i; | 188 | ctx->buf_len=i; |
| 189 | return 1; | ||
| 158 | } | 190 | } |
| 159 | 191 | ||
| 160 | void EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 192 | int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
| 161 | { | 193 | { |
| 162 | int i,n,b,bl; | 194 | int i,n,b,bl; |
| 163 | 195 | ||
| @@ -165,24 +197,25 @@ void EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
| 165 | if (b == 1) | 197 | if (b == 1) |
| 166 | { | 198 | { |
| 167 | *outl=0; | 199 | *outl=0; |
| 168 | return; | 200 | return 1; |
| 169 | } | 201 | } |
| 170 | bl=ctx->buf_len; | 202 | bl=ctx->buf_len; |
| 171 | n=b-bl; | 203 | n=b-bl; |
| 172 | for (i=bl; i<b; i++) | 204 | for (i=bl; i<b; i++) |
| 173 | ctx->buf[i]=n; | 205 | ctx->buf[i]=n; |
| 174 | ctx->cipher->do_cipher(ctx,out,ctx->buf,b); | 206 | if(!ctx->cipher->do_cipher(ctx,out,ctx->buf,b)) return 0; |
| 175 | *outl=b; | 207 | *outl=b; |
| 208 | return 1; | ||
| 176 | } | 209 | } |
| 177 | 210 | ||
| 178 | void EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | 211 | int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, |
| 179 | unsigned char *in, int inl) | 212 | unsigned char *in, int inl) |
| 180 | { | 213 | { |
| 181 | int b,bl,n; | 214 | int b,bl,n; |
| 182 | int keep_last=0; | 215 | int keep_last=0; |
| 183 | 216 | ||
| 184 | *outl=0; | 217 | *outl=0; |
| 185 | if (inl == 0) return; | 218 | if (inl == 0) return 1; |
| 186 | 219 | ||
| 187 | b=ctx->cipher->block_size; | 220 | b=ctx->cipher->block_size; |
| 188 | if (b > 1) | 221 | if (b > 1) |
| @@ -197,13 +230,13 @@ void EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
| 197 | memcpy(&(ctx->buf[bl]),in,inl); | 230 | memcpy(&(ctx->buf[bl]),in,inl); |
| 198 | ctx->buf_len=b; | 231 | ctx->buf_len=b; |
| 199 | *outl=0; | 232 | *outl=0; |
| 200 | return; | 233 | return 1; |
| 201 | } | 234 | } |
| 202 | keep_last=1; | 235 | keep_last=1; |
| 203 | inl-=b; /* don't do the last block */ | 236 | inl-=b; /* don't do the last block */ |
| 204 | } | 237 | } |
| 205 | } | 238 | } |
| 206 | EVP_EncryptUpdate(ctx,out,outl,in,inl); | 239 | if(!EVP_EncryptUpdate(ctx,out,outl,in,inl)) return 0; |
| 207 | 240 | ||
| 208 | /* if we have 'decrypted' a multiple of block size, make sure | 241 | /* if we have 'decrypted' a multiple of block size, make sure |
| 209 | * we have a copy of this last block */ | 242 | * we have a copy of this last block */ |
| @@ -218,6 +251,7 @@ void EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl, | |||
| 218 | #endif | 251 | #endif |
| 219 | ctx->buf_len=b; | 252 | ctx->buf_len=b; |
| 220 | } | 253 | } |
| 254 | return 1; | ||
| 221 | } | 255 | } |
| 222 | 256 | ||
| 223 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | 257 | int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
| @@ -234,7 +268,7 @@ int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
| 234 | EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH); | 268 | EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_WRONG_FINAL_BLOCK_LENGTH); |
| 235 | return(0); | 269 | return(0); |
| 236 | } | 270 | } |
| 237 | EVP_EncryptUpdate(ctx,ctx->buf,&n,ctx->buf,0); | 271 | if(!EVP_EncryptUpdate(ctx,ctx->buf,&n,ctx->buf,0)) return 0; |
| 238 | if (n != b) | 272 | if (n != b) |
| 239 | return(0); | 273 | return(0); |
| 240 | n=ctx->buf[b-1]; | 274 | n=ctx->buf[b-1]; |
| @@ -261,10 +295,47 @@ int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) | |||
| 261 | return(1); | 295 | return(1); |
| 262 | } | 296 | } |
| 263 | 297 | ||
| 264 | void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) | 298 | int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) |
| 265 | { | 299 | { |
| 266 | if ((c->cipher != NULL) && (c->cipher->cleanup != NULL)) | 300 | if ((c->cipher != NULL) && (c->cipher->cleanup != NULL)) |
| 267 | c->cipher->cleanup(c); | 301 | { |
| 302 | if(!c->cipher->cleanup(c)) return 0; | ||
| 303 | } | ||
| 268 | memset(c,0,sizeof(EVP_CIPHER_CTX)); | 304 | memset(c,0,sizeof(EVP_CIPHER_CTX)); |
| 305 | return 1; | ||
| 306 | } | ||
| 307 | |||
| 308 | int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) | ||
| 309 | { | ||
| 310 | if(c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) | ||
| 311 | return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL); | ||
| 312 | if(c->key_len == keylen) return 1; | ||
| 313 | if((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) | ||
| 314 | { | ||
| 315 | c->key_len = keylen; | ||
| 316 | return 1; | ||
| 317 | } | ||
| 318 | EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH); | ||
| 319 | return 0; | ||
| 320 | } | ||
| 321 | |||
| 322 | int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) | ||
| 323 | { | ||
| 324 | int ret; | ||
| 325 | if(!ctx->cipher) { | ||
| 326 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET); | ||
| 327 | return 0; | ||
| 328 | } | ||
| 329 | |||
| 330 | if(!ctx->cipher->ctrl) { | ||
| 331 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED); | ||
| 332 | return 0; | ||
| 269 | } | 333 | } |
| 270 | 334 | ||
| 335 | ret = ctx->cipher->ctrl(ctx, type, arg, ptr); | ||
| 336 | if(ret == -1) { | ||
| 337 | EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED); | ||
| 338 | return 0; | ||
| 339 | } | ||
| 340 | return ret; | ||
| 341 | } | ||
