Manual Pages for UNIX Darwin command on man EVP_CIPHER_key_length
MyWebUniversity

Manual Pages for UNIX Darwin command on man EVP_CIPHER_key_length

EVPEncryptInit(3) OpenSSL EVPEncryptInit(3)

NAME

EVPCIPHERCTXinit, EVPEncryptInitex, EVPEncryptUpdate, EVPEncryptFinalex, EVPDecryptInitex, EVPDecryptUpdate, EVPDecryptFinalex, EVPCipherInitex, EVPCipherUpdate, EVPCipherFinalex, EVPCIPHERCTXsetkeylength, EVPCIPHERCTXctrl, EVPCIPHERCTXcleanup, EVPEncryptInit, EVPEncryptFinal, EVPDecryptInit, EVPDecryptFinal, EVPCipherInit, EVPCipherFinal, EVPgetcipherbyname, EVPgetcipherbynid, EVPgetcipherbyobj, EVPCIPHERnid, EVPCIPHERblocksize, EVPCIPHERkeylength, EVPCIPHERivlength, EVPCIPHERflags, EVPCIPHERmode, EVPCIPHERtype, EVPCIPHERCTXcipher, EVPCIPHERCTXnid, EVPCIPHERCTXblocksize, EVPCIPHERCTXkeylength, EVPCIPHERCTXivlength, EVPCIPHERCTXgetappdata, EVPCIPHERCTXsetappdata, EVPCIPHERCTXtype, EVPCIPHERCTXflags, EVPCIPHERCTXmode, EVPCIPHERparamtoasn1,

EVPCIPHERasn1toparam, EVPCIPHERCTXsetpadding - EVP cipher

routines

SYNOPSIS

#include

void EVPCIPHERCTXinit(EVPCIPHERCTX *a); int EVPEncryptInitex(EVPCIPHERCTX *ctx, const EVPCIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv); int EVPEncryptUpdate(EVPCIPHERCTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl); int EVPEncryptFinalex(EVPCIPHERCTX *ctx, unsigned char *out, int *outl); int EVPDecryptInitex(EVPCIPHERCTX *ctx, const EVPCIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv); int EVPDecryptUpdate(EVPCIPHERCTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl); int EVPDecryptFinalex(EVPCIPHERCTX *ctx, unsigned char *outm, int *outl); int EVPCipherInitex(EVPCIPHERCTX *ctx, const EVPCIPHER *type, ENGINE *impl, unsigned char *key, unsigned char *iv, int enc); int EVPCipherUpdate(EVPCIPHERCTX *ctx, unsigned char *out, int *outl, unsigned char *in, int inl); int EVPCipherFinalex(EVPCIPHERCTX *ctx, unsigned char *outm, int *outl); int EVPEncryptInit(EVPCIPHERCTX *ctx, const EVPCIPHER *type, unsigned char *key, unsigned char *iv); int EVPEncryptFinal(EVPCIPHERCTX *ctx, unsigned char *out, int *outl); int EVPDecryptInit(EVPCIPHERCTX *ctx, const EVPCIPHER *type, unsigned char *key, unsigned char *iv); int EVPDecryptFinal(EVPCIPHERCTX *ctx, unsigned char *outm, int *outl); int EVPCipherInit(EVPCIPHERCTX *ctx, const EVPCIPHER *type, unsigned char *key, unsigned char *iv, int enc); int EVPCipherFinal(EVPCIPHERCTX *ctx, unsigned char *outm, int *outl); int EVPCIPHERCTXsetpadding(EVPCIPHERCTX *x, int padding); int EVPCIPHERCTXsetkeylength(EVPCIPHERCTX *x, int keylen); int EVPCIPHERCTXctrl(EVPCIPHERCTX *ctx, int type, int arg, void *ptr); int EVPCIPHERCTXcleanup(EVPCIPHERCTX *a); const EVPCIPHER *EVPgetcipherbyname(const char *name);

#define EVPgetcipherbynid(a) EVPgetcipherbyname(OBJnid2sn(a))

#define EVPgetcipherbyobj(a) EVPgetcipherbynid(OBJobj2nid(a))

#define EVPCIPHERnid(e) ((e)->nid)

#define EVPCIPHERblocksize(e) ((e)->blocksize)

#define EVPCIPHERkeylength(e) ((e)->keylen)

#define EVPCIPHERivlength(e) ((e)->ivlen)

#define EVPCIPHERflags(e) ((e)->flags)

#define EVPCIPHERmode(e) ((e)->flags) & EVPCIPHMODE)

int EVPCIPHERtype(const EVPCIPHER *ctx);

#define EVPCIPHERCTXcipher(e) ((e)->cipher)

#define EVPCIPHERCTXnid(e) ((e)->cipher->nid)

#define EVPCIPHERCTXblocksize(e) ((e)->cipher->blocksize)

#define EVPCIPHERCTXkeylength(e) ((e)->keylen)

#define EVPCIPHERCTXivlength(e) ((e)->cipher->ivlen)

#define EVPCIPHERCTXgetappdata(e) ((e)->appdata)

#define EVPCIPHERCTXsetappdata(e,d) ((e)->appdata=(char *)(d))

#define EVPCIPHERCTXtype(c) EVPCIPHERtype(EVPCIPHERCTXcipher(c))

#define EVPCIPHERCTXflags(e) ((e)->cipher->flags)

#define EVPCIPHERCTXmode(e) ((e)->cipher->flags & EVPCIPHMODE)

int EVPCIPHERparamtoasn1(EVPCIPHERCTX *c, ASN1TYPE *type); int EVPCIPHERasn1toparam(EVPCIPHERCTX *c, ASN1TYPE *type);

DESCRIPTION

The EVP cipher routines are a high level interface to certain symmetric ciphers. EVPCIPHERCTXinit() initializes cipher contex ccttxx. EVPEncryptInitex() sets up cipher context ccttxx for encryption with cipher ttyyppee from ENGINE iimmppll. ccttxx must be initialized before calling this function. ttyyppee is normally supplied by a function such as EVPdescbc(). If iimmppll is NULL then the default implementation is used. kkeeyy is the symmetric key to use and iivv is the IV to use (if necessary), the actual number of bytes used for the key and IV depends on the cipher. It is possible to set all parameters to NULL except ttyyppee in an initial call and supply the remaining parameters in subsequent calls, all of which have ttyyppee set to NULL. This is done when the default cipher parameters are not appropriate. EVPEncryptUpdate() encrypts iinnll bytes from the buffer iinn and writes the encrypted version to oouutt. This function can be called multiple times to encrypt successive blocks of data. The amount of data written depends on the block alignment of the encrypted data: as a result the amount of data written may be anything from zero bytes to (inl +

cipherblocksize - 1) so oouuttll should contain sufficient room. The

actual number of bytes written is placed in oouuttll. If padding is enabled (the default) then EVPEncryptFinalex() encrypts the "final" data, that is any data that remains in a partial block. It uses standard block padding (aka PKCS padding). The encrypted final data is written to oouutt which should have sufficient space for one cipher block. The number of bytes written is placed in oouuttll. After this function is called the encryption operation is finished and no further calls to EVPEncryptUpdate() should be made. If padding is disabled then EVPEncryptFinalex() will not encrypt any more data and it will return an error if any data remains in a partial block: that is if the total data length is not a multiple of the block size. EVPDecryptInitex(), EVPDecryptUpdate() and EVPDecryptFinalex() are the corresponding decryption operations. EVPDecryptFinal() will return an error code if padding is enabled and the final block is not correctly formatted. The parameters and restrictions are identical to the encryption operations except that if padding is enabled the decrypted data buffer oouutt passed to EVPDecryptUpdate() should have sufficient room for (iinnll + cipherblocksize) bytes unless the cipher block size is 1 in which case iinnll bytes is sufficient. EVPCipherInitex(), EVPCipherUpdate() and EVPCipherFinalex() are functions that can be used for decryption or encryption. The operation performed depends on the value of the eenncc parameter. It should be set

to 1 for encryption, 0 for decryption and -1 to leave the value

unchanged (the actual value of 'enc' being supplied in a previous call). EVPCIPHERCTXcleanup() clears all information from a cipher context and free up any allocated memory associate with it. It should be called after all operations using a cipher are complete so sensitive information does not remain in memory. EVPEncryptInit(), EVPDecryptInit() and EVPCipherInit() behave in a similar way to EVPEncryptInitex(), EVPDecryptInitex and EVPCipherInitex() except the ccttxx paramter does not need to be initialized and they always use the default cipher implementation. EVPEncryptFinal(), EVPDecryptFinal() and EVPCipherFinal() behave in a similar way to EVPEncryptFinalex(), EVPDecryptFinalex() and EVPCipherFinalex() except ccttxx is automatically cleaned up after the call. EVPgetcipherbyname(), EVPgetcipherbynid() and EVPgetcipherbyobj() return an EVPCIPHER structure when passed a cipher name, a NID or an ASN1OBJECT structure. EVPCIPHERnid() and EVPCIPHERCTXnid() return the NID of a cipher when passed an EEVVPPCCIIPPHHEERR or EEVVPPCCIIPPHHEERRCCTTXX structure. The actual NID value is an internal value which may not have a corresponding OBJECT IDENTIFIER. EVPCIPHERCTXsetpadding() enables or disables padding. By default encryption operations are padded using standard block padding and the padding is checked and removed when decrypting. If the ppaadd parameter is zero then no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of the block size or an error will occur. EVPCIPHERkeylength() and EVPCIPHERCTXkeylength() return the key length of a cipher when passed an EEVVPPCCIIPPHHEERR or EEVVPPCCIIPPHHEERRCCTTXX structure. The constant EEVVPPMMAAXXKKEEYYLLEENNGGTTHH is the maximum key length for all ciphers. Note: although EVPCIPHERkeylength() is fixed for a given cipher, the value of EVPCIPHERCTXkeylength() may be different for variable key length ciphers. EVPCIPHERCTXsetkeylength() sets the key length of the cipher ctx. If the cipher is a fixed length cipher then attempting to set the key length to any value other than the fixed value is an error. EVPCIPHERivlength() and EVPCIPHERCTXivlength() return the IV length of a cipher when passed an EEVVPPCCIIPPHHEERR or EEVVPPCCIIPPHHEERRCCTTXX. It will return zero if the cipher does not use an IV. The constant EEVVPPMMAAXXIIVVLLEENNGGTTHH is the maximum IV length for all ciphers. EVPCIPHERblocksize() and EVPCIPHERCTXblocksize() return the block size of a cipher when passed an EEVVPPCCIIPPHHEERR or EEVVPPCCIIPPHHEERRCCTTXX structure. The constant EEVVPPMMAAXXIIVVLLEENNGGTTHH is also the maximum block length for all ciphers. EVPCIPHERtype() and EVPCIPHERCTXtype() return the type of the passed cipher or context. This "type" is the actual NID of the cipher OBJECT IDENTIFIER as such it ignores the cipher parameters and 40 bit RC2 and 128 bit RC2 have the same NID. If the cipher does not have an object identifier or does not have ASN1 support this function will return NNIIDDuunnddeeff. EVPCIPHERCTXcipher() returns the EEVVPPCCIIPPHHEERR structure when passed an EEVVPPCCIIPPHHEERRCCTTXX structure. EVPCIPHERmode() and EVPCIPHERCTXmode() return the block cipher mode: EVPCIPHECBMODE, EVPCIPHCBCMODE, EVPCIPHCFBMODE or EVPCIPHOFBMODE. If the cipher is a stream cipher then EVPCIPHSTREAMCIPHER is returned. EVPCIPHERparamtoasn1() sets the AlgorithmIdentifier "parameter" based on the passed cipher. This will typically include any parameters and an IV. The cipher IV (if any) must be set when this call is made. This call should be made before the cipher is actually "used" (before any EVPEncryptUpdate(), EVPDecryptUpdate() calls for example). This function may fail if the cipher does not have any ASN1 support. EVPCIPHERasn1toparam() sets the cipher parameters based on an ASN1 AlgorithmIdentifier "parameter". The precise effect depends on the cipher In the case of RC2, for example, it will set the IV and effective key length. This function should be called after the base cipher type is set but before the key is set. For example EVPCipherInit() will be called with the IV and key set to NULL, EVPCIPHERasn1toparam() will be called and finally EVPCipherInit() again with all parameters except the key set to NULL. It is possible for this function to fail if the cipher does not have any ASN1 support or the parameters cannot be set (for example the RC2 effective key length is not supported. EVPCIPHERCTXctrl() allows various cipher specific parameters to be determined and set. Currently only the RC2 effective key length and the number of rounds of RC5 can be set.

RETURN VALUES

EVPEncryptInitex(), EVPEncryptUpdate() and EVPEncryptFinalex() return 1 for success and 0 for failure. EVPDecryptInitex() and EVPDecryptUpdate() return 1 for success and 0 for failure. EVPDecryptFinalex() returns 0 if the decrypt failed or 1 for success. EVPCipherInitex() and EVPCipherUpdate() return 1 for success and 0 for failure. EVPCipherFinalex() returns 0 for a decryption failure or 1 for success. EVPCIPHERCTXcleanup() returns 1 for success and 0 for failure. EVPgetcipherbyname(), EVPgetcipherbynid() and EVPgetcipherbyobj() return an EEVVPPCCIIPPHHEERR structure or NULL on error. EVPCIPHERnid() and EVPCIPHERCTXnid() return a NID. EVPCIPHERblocksize() and EVPCIPHERCTXblocksize() return the block size. EVPCIPHERkeylength() and EVPCIPHERCTXkeylength() return the key length. EVPCIPHERCTXsetpadding() always returns 1. EVPCIPHERivlength() and EVPCIPHERCTXivlength() return the IV length or zero if the cipher does not use an IV. EVPCIPHERtype() and EVPCIPHERCTXtype() return the NID of the cipher's OBJECT IDENTIFIER or NIDundef if it has no defined OBJECT IDENTIFIER. EVPCIPHERCTXcipher() returns an EEVVPPCCIIPPHHEERR structure. EVPCIPHERparamtoasn1() and EVPCIPHERasn1toparam() return 1 for success or zero for failure. CCIIPPHHEERR LLIISSTTIINNGG All algorithms have a fixed key length unless otherwise stated. EVPencnull() Null cipher: does nothing. EVPdescbc(void), EVPdesecb(void), EVPdescfb(void), EVPdesofb(void) DES in CBC, ECB, CFB and OFB modes respectively. EVPdesedecbc(void), EVPdesede(), EVPdesedeofb(void), EVPdesedecfb(void) Two key triple DES in CBC, ECB, CFB and OFB modes respectively. EVPdesede3cbc(void), EVPdesede3(), EVPdesede3ofb(void), EVPdesede3cfb(void) Three key triple DES in CBC, ECB, CFB and OFB modes respectively. EVPdesxcbc(void) DESX algorithm in CBC mode. EVPrc4(void) RC4 stream cipher. This is a variable key length cipher with default key length 128 bits. EVPrc440(void) RC4 stream cipher with 40 bit key length. This is obsolete and new code should use EVPrc4() and the EVPCIPHERCTXsetkeylength() function. EVPideacbc() EVPideaecb(void), EVPideacfb(void), EVPideaofb(void), EVPideacbc(void) IDEA encryption algorithm in CBC, ECB, CFB and OFB modes respectively. EVPrc2cbc(void), EVPrc2ecb(void), EVPrc2cfb(void), EVPrc2ofb(void) RC2 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key length cipher with an additional parameter called "effective key bits" or "effective key length". By default both are set to 128 bits. EVPrc240cbc(void), EVPrc264cbc(void) RC2 algorithm in CBC mode with a default key length and effective key length of 40 and 64 bits. These are obsolete and new code should use EVPrc2cbc(), EVPCIPHERCTXsetkeylength() and EVPCIPHERCTXctrl() to set the key length and effective key length. EVPbfcbc(void), EVPbfecb(void), EVPbfcfb(void), EVPbfofb(void); Blowfish encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key length cipher. EVPcast5cbc(void), EVPcast5ecb(void), EVPcast5cfb(void), EVPcast5ofb(void) CAST encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key length cipher. EVPrc5321216cbc(void), EVPrc5321216ecb(void), EVPrc5321216cfb(void), EVPrc5321216ofb(void) RC5 encryption algorithm in CBC, ECB, CFB and OFB modes respectively. This is a variable key length cipher with an additional "number of rounds" parameter. By default the key length is set to 128 bits and 12 rounds. NNOOTTEESS Where possible the EEVVPP interface to symmetric ciphers should be used in preference to the low level interfaces. This is because the code then becomes transparent to the cipher used and much more flexible. PKCS padding works by adding nn padding bytes of value nn to make the total length of the encrypted data a multiple of the block size. Padding is always added so if the data is already a multiple of the block size nn will equal the block size. For example if the block size is 8 and 11 bytes are to be encrypted then 5 padding bytes of value 5 will be added. When decrypting the final block is checked to see if it has the correct form. Although the decryption operation can produce an error if padding is enabled, it is not a strong test that the input data or key is correct. A random block has better than 1 in 256 chance of being of the correct format and problems with the input data earlier on will not produce a final decrypt error. If padding is disabled then the decryption operation will always succeed if the total amount of data decrypted is a multiple of the block size. The functions EVPEncryptInit(), EVPEncryptFinal(), EVPDecryptInit(), EVPCipherInit() and EVPCipherFinal() are obsolete but are retained for compatibility with existing code. New code should use EVPEncryptInitex(), EVPEncryptFinalex(), EVPDecryptInitex(), EVPDecryptFinalex(), EVPCipherInitex() and EVPCipherFinalex() because they can reuse an existing context without allocating and freeing it up on each call.

BUGS

For RC5 the number of rounds can currently only be set to 8, 12 or 16. This is a limitation of the current RC5 code rather than the EVP interface. EVPMAXKEYLENGTH and EVPMAXIVLENGTH only refer to the internal ciphers with default key lengths. If custom ciphers exceed these values the results are unpredictable. This is because it has become standard practice to define a generic key as a fixed unsigned char array containing EVPMAXKEYLENGTH bytes. The ASN1 code is incomplete (and sometimes inaccurate) it has only been tested for certain common S/MIME ciphers (RC2, DES, triple DES) in CBC mode. EEXXAAMMPPLLEESS Get the number of rounds used in RC5: int nrounds; EVPCIPHERCTXctrl(ctx, EVPCTRLGETRC5ROUNDS, 0, &nrounds); Get the RC2 effective key length: int keybits; EVPCIPHERCTXctrl(ctx, EVPCTRLGETRC2KEYBITS, 0, &keybits); Set the number of rounds used in RC5: int nrounds; EVPCIPHERCTXctrl(ctx, EVPCTRLSETRC5ROUNDS, nrounds, NULL); Set the effective key length used in RC2: int keybits; EVPCIPHERCTXctrl(ctx, EVPCTRLSETRC2KEYBITS, keybits, NULL); Encrypt a string using blowfish: int docrypt(char *outfile) { unsigned char outbuf[1024]; int outlen, tmplen; /* Bogus key and IV: we'd normally set these from * another source. */ unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; unsigned char iv[] = {1,2,3,4,5,6,7,8}; char intext[] = "Some Crypto Text"; EVPCIPHERCTX ctx; FILE *out; EVPCIPHERCTXinit(&ctx); EVPEncryptInitex(&ctx, EVPbfcbc(), NULL, key, iv); if(!EVPEncryptUpdate(&ctx, outbuf, &outlen, intext, strlen(intext))) { /* Error */ return 0; } /* Buffer passed to EVPEncryptFinal() must be after data just * encrypted to avoid overwriting it. */ if(!EVPEncryptFinalex(&ctx, outbuf + outlen, &tmplen)) { /* Error */ return 0; } outlen += tmplen; EVPCIPHERCTXcleanup(&ctx); /* Need binary mode for fopen because encrypted data is * binary data. Also cannot use strlen() on it because * it wont be null terminated and may contain embedded * nulls. */ out = fopen(outfile, "wb"); fwrite(outbuf, 1, outlen, out); fclose(out); return 1; } The ciphertext from the above example can be decrypted using the ooppeennssssll utility with the command line:

S

General encryption, decryption function example using FILE I/O and RC2 with an 80 bit key: int docrypt(FILE *in, FILE *out, int doencrypt) { /* Allow enough space in output buffer for additional block */ inbuf[1024], outbuf[1024 + EVPMAXBLOCKLENGTH]; int inlen, outlen; /* Bogus key and IV: we'd normally set these from * another source. */ unsigned char key[] = "0123456789"; unsigned char iv[] = "12345678"; /* Don't set key or IV because we will modify the parameters */ EVPCIPHERCTXinit(&ctx); EVPCipherInitex(&ctx, EVPrc2(), NULL, NULL, NULL, doencrypt); EVPCIPHERCTXsetkeylength(&ctx, 10); /* We finished modifying parameters so now we can set key and IV */ EVPCipherInitex(&ctx, NULL, NULL, key, iv, doencrypt); for(;;) { inlen = fread(inbuf, 1, 1024, in); if(inlen <= 0) break; if(!EVPCipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) { /* Error */ EVPCIPHERCTXcleanup(&ctx); return 0; } fwrite(outbuf, 1, outlen, out); } if(!EVPCipherFinalex(&ctx, outbuf, &outlen)) { /* Error */ EVPCIPHERCTXcleanup(&ctx); return 0; } fwrite(outbuf, 1, outlen, out); EVPCIPHERCTXcleanup(&ctx); return 1; }

SEE ALSO

evp(3) HISTORY EVPCIPHERCTXinit(), EVPEncryptInitex(), EVPEncryptFinalex(), EVPDecryptInitex(), EVPDecryptFinalex(), EVPCipherInitex(), EVPCipherFinalex() and EVPCIPHERCTXsetpadding() appeared in OpenSSL 0.9.7.

0.9.7l 2005-04-15 EVPEncryptInit(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™