PL/SQL Packages and Types Reference 10g Release 1 (10.1) Part Number B10802-01 |
|
|
View PDF |
DBMS_CRYPTO
provides an interface to encrypt and decrypt stored data, and can be used in conjunction with PL/SQL programs running network communications. It provides support for several industry-standard encryption and hashing algorithms, including the Advanced Encryption Standard (AES) encryption algorithm. AES has been approved by the National Institute of Standards and Technology (NIST) to replace the Data Encryption Standard (DES).
See Also:
Oracle Database Security Guide for further information about using this package and about encrypting data in general. |
This chapter contains the following topics:
DBMS_CRYPTO
contains basic cryptographic functions and procedures. To use this package correctly and securely, a general level of security expertise is assumed.
The DBMS_CRYPTO
package enables encryption and decryption for common Oracle datatypes, including RAW
and large objects (LOB
s), such as images and sound. Specifically, it supports BLOB
s and CLOB
s. In addition, it provides Globalization Support for encrypting data across different database character sets.
The following cryptographic algorithms are supported:
Block cipher modifiers are also provided with DBMS_CRYPTO
. You can choose from several padding options, including PKCS (Public Key Cryptographic Standard) #5, and from four block cipher chaining modes, including Cipher Block Chaining (CBC).
Table 22-1 lists the DBMS_CRYPTO
package features in comparison to the other PL/SQL encryption package, the DBMS_OBFUSCATION_TOOLKIT
.
DBMS_CRYPTO
is intended to replace the DBMS_OBFUSCATION_TOOLKIT
, providing greater ease of use and support for a range of algorithms to accommodate new and existing systems. Specifically, 3DES_2KEY
and MD4 are provided for backward compatibility. It is not recommended that you use these algorithms because they do not provide the same level of security as provided by 3DES, AES, MD5, or SHA-1.
Oracle Database installs this package in the SYS
schema. You can then grant package access to existing users and roles as needed.
Parameters for the DBMS_CRYPTO
subprograms use these datatypes:
The following cryptographic algorithms, modifiers, and cipher suites are predefined in this package.
Name | Description |
---|---|
|
Same as MD5 hash function, except it requires a secret key to verify the hash value. |
|
Same as SHA hash function, except it requires a secret key to verify the hash value. |
1 Complies with IETF RFC 2104 standard |
Name | Description |
---|---|
|
|
|
|
1 See Table 22-5, " DBMS_CRYPTO Encryption Algorithms" 2 See Table 22-7, " DBMS_CRYPTO Block Cipher Chaining Modifiers" 3 See Table 22-8, " DBMS_CRYPTO Block Cipher Padding Modifiers" |
The VARCHAR2
datatype is not directly supported by DBMS_CRYPTO
. Before you can perform cryptographic operations on data of the type VARCHAR2
, you must convert it to the uniform database character set AL32UTF8, and then convert it to the RAW
datatype. After performing these conversions, you can then encrypt it with the DBMS_CRYPTO
package.
See Also:
"Conversion Rules" for information about converting datatypes. |
Table 22-9 lists exceptions that have been defined for DBMS_CRYPTO
.
This package includes both ENCRYPT
and DECRYPT
procedures and functions. The procedures are used to encrypt or decrypt LOB
datatypes (overloaded for CLOB
and BLOB
datatypes). In contrast, the ENCRYPT
and DECRYPT
functions are used to encrypt and decrypt RAW
datatypes. Data of type VARCHAR2
must be converted to RAW
before you can use DBMS_CRYPTO
functions to encrypt it.
This package includes two different types of one-way hash functions: the HASH
function and the MAC
function. Hash functions operate on an arbitrary-length input message, and return a fixed-length hash value. One-way hash functions work in one direction only. It is easy to compute a hash value from an input message, but it is extremely difficult to generate an input message that hashes to a particular value. Note that hash values should be at least 128 bits in length to be considered secure.
You can use hash values to verify whether data has been altered. For example, before storing data, Laurel runs DBMS_CRYPTO.HASH
against the stored data to create a hash value. When she retrieves the stored data at a later date, she can again run the hash function against it, using the same algorithm. If the second hash value is identical to the first one, then the data has not been altered. Hash values are similar to "file fingerprints" and are used to ensure data integrity.
The HASH
function included with DBMS_CRYPTO
, is a one-way hash function that you can use to generate a hash value from either RAW
or LOB
data. The MAC
function is also a one-way hash function, but with the addition of a secret key. It works the same way as the DBMS_CRYPTO.HASH
function, except only someone with the key can verify the hash value.
MACs can be used to authenticate files between users. They can also be used by a single user to determine if her files have been altered, perhaps by a virus. A user could compute the MAC of his files and store that value in a table. If the user did not use a MAC function, then the virus could compute the new hash value after infection and replace the table entry. A virus cannot do that with a MAC because the virus does not know the key.
The DBMS_CRYPTO
package can generate random material for encryption keys, but it does not provide a mechanism for maintaining them. Application developers must take care to ensure that the encryption keys used with this package are securely generated and stored. Also note that the encryption and decryption operations performed by DBMS_CRYPTO
occur on the server, not on the client. Consequently, if the key is sent over the connection between the client and the server, the connection must be protected by using network encryption. Otherwise, the key is vulnerable to capture over the wire.
Although DBMS_CRYPTO
cannot generate keys on its own, it does provide tools you can use to aid in key generation. For example, you can use the RANDOMBYTES
function to generate random material for keys. (Calls to the RANDOMBYTES
function behave like calls to the DESGETKEY
and DES3GETKEY
functions of the DBMS_OBFUSCATION_TOOLKIT
package.)
When generating encryption keys for DES, it is important to remember that some numbers are considered weak and semiweak keys. Keys are considered weak or semiweak when the pattern of the algorithm combines with the pattern of the initial key value to produce ciphertext that is more susceptible to cryptanalysis. To avoid this, filter out the known weak DES keys. Lists of the known weak and semiweak DES keys are available on several public Internet sites.
See Also:
|
VARCHAR2
to RAW
, use the UTL_I18N.STRING_TO_RAW
function to perform the following steps:
VARCHAR2
in the current database character set to VARCHAR2
in the AL32UTF8 database character.VARCHAR2
in the AL32UTF8 database character set to RAW
.Syntax example:
UTL_I18N.STRING_TO_RAW (string, 'AL32UTF8');
RAW
to VARCHAR2
, use the UTL_I18N.RAW_TO_CHAR
function to perform the following steps:
RAW
to VARCHAR2
in the AL32UTF8 database character set.VARCHAR2
in the AL32UTF8 database character set to VARCHAR2
in the database character set you wish to use.Syntax example:
UTL_I18N.RAW_TO_CHAR (data, 'AL32UTF8');
See Also:
Chapter 157, "UTL_I18N" for information about using the |
RAW
datatype in a VARCHAR2
database column, then use RAWTOHEX
or UTL_ENCODE.BASE64_ENCODE
to make it suitable for VARCHAR2
storage. These functions expand data size by 2 and 4/3, respectively.This function decrypts RAW
data using a stream or block cipher with a user supplied key and optional IV (initialization vector).
DBMS_CRYPTO.DECRYPT( src IN RAW, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL) RETURN RAW;
pragma restrict_references(decrypt,WNDS,RNDS,WNPS,RNPS);
DECRYPT
must be called with the same cipher, modifiers, key, and IV that was used to encrypt the data originally.
See Also:
"Usage Notes" for the |
VARCHAR2
data is converted to RAW
before encryption, then it must be converted back to the appropriate database character set by using the UTL_I18N
package.
See Also:
"Conversion Rules" for a discussion of the |
These procedures decrypt LOB
data using a stream or block cipher with a user supplied key and optional IV (initialization vector).
DBMS_CRYPTO.DECRYPT( dst IN OUT NOCOPY BLOB, src IN BLOB, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL); DBMS_CRYPT.DECRYPT( dst IN OUT NOCOPY CLOB CHARACTER SET ANY_CS, src IN BLOB, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL);
pragma restrict_references(decrypt,WNDS,RNDS,WNPS,RNPS);
This function encrypts RAW
data using a stream or block cipher with a user supplied key and optional IV (initialization vector).
DBMS_CRYPTO.ENCRYPT( src IN RAW, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL) RETURN RAW;
pragma restrict_references(encrypt,WNDS,RNDS,WNPS,RNPS);
DES_CBC_NONE CONSTANT PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_DES + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_NONE;
See Table 22-6 for the block cipher suites already defined as constants for this package.
VARCHAR2
data, it should first be converted to the AL32UTF8 character set.
See Also:
"Conversion Rules" for a discussion of the conversion process. |
These procedures encrypt LOB
data using a stream or block cipher with a user supplied key and optional IV (initialization vector).
DBMS_CRYPTO.ENCRYPT( dst IN OUT NOCOPY BLOB, src IN BLOB, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL); DBMS_CRYPTO.ENCRYPT( dst IN OUT NOCOPY BLOB, src IN CLOB CHARACTER SET ANY_CS, typ IN PLS_INTEGER, key IN RAW, iv IN RAW DEFAULT NULL);
pragma restrict_references(encrypt,WNDS,RNDS,WNPS,RNPS);
See "Conversion Rules" for usage notes about using the ENCRYPT
procedure.
A one-way hash function takes a variable-length input string, the data, and converts it to a fixed-length (generally smaller) output string called a hash value. The hash value serves as a unique identifier (like a fingerprint) of the input data. You can use the hash value to verify whether data has been changed or not.
Note that a one-way hash function is a hash function that works in one direction. It is easy to compute a hash value from the input data, but it is hard to generate data that hashes to a particular value. Consequently, one-way hash functions work well to ensure data integrity. Refer to "When to Use Hash or Message Authentication Code (MAC) Functions" for more information about using one-way hash functions.
This function applies to data one of the supported cryptographic hash algorithms listed in Table 22-3.
DBMS_CRYPTO.Hash ( src IN RAW, typ IN PLS_INTEGER) RETURN RAW; DBMS_CRYPTO.Hash ( src IN BLOB, typ IN PLS_INTEGER) RETURN RAW; DBMS_CRYPTO.Hash ( src IN CLOB CHARACTER SET ANY_CS, typ IN PLS_INTEGER) RETURN RAW;
pragma restrict_references(hash,WNDS,RNDS,WNPS,RNPS);
Parameter Name | Description |
---|---|
|
The source data to be hashed. |
|
The hash algorithm to be used. |
Oracle recommends that you use the SHA-1 (Secure Hash Algorithm), specified with the constant, HASH_SH1
, because it is more resistant to brute-force attacks than MD4 or MD5. If you must use a Message Digest algorithm, then MD5 provides greater security than MD4.
A Message Authentication Code, or MAC, is a key-dependent one-way hash function. MACs have the same properties as the one-way hash function described in "HASH Function", but they also include a key. Only someone with the identical key can verify the hash. Also refer to "When to Use Hash or Message Authentication Code (MAC) Functions" for more information about using MACs.
This function applies MAC algorithms to data to provide keyed message protection. See Table 22-4 for a list of MAC algorithms that have been defined for this package.
DBMS_CRYPTO.MAC ( src IN RAW, typ IN PLS_INTEGER, key IN RAW) RETURN RAW; DBMS_CRYPTO.MAC ( src IN BLOB, typ IN PLS_INTEGER key IN RAW) RETURN RAW; DBMS_CRYPTO.MAC ( src IN CLOB CHARACTER SET ANY_CS, typ IN PLS_INTEGER key IN RAW) RETURN RAW;
pragma restrict_references(mac,WNDS,RNDS,WNPS,RNPS);
Parameter Name | Description |
---|---|
|
Source data to which MAC algorithms are to be applied. |
|
MAC algorithm to be used. |
|
Key to be used for MAC algorithm. |
This function returns a RAW
value containing a cryptographically secure pseudo-random sequence of bytes, which can be used to generate random material for encryption keys. The RANDOMBYTES
function is based on the RSA X9.31 PRNG (Pseudo-Random Number Generator), and it draws its entropy (seed) from the sqlnet.ora
file parameter SQLNET.CRYPTO_SEED
.
DBMS_CRYPTO.RANDOMBYTES ( number_bytes IN POSITIVE) RETURN RAW;
pragma restrict_references(randombytes,WNDS,RNDS,WNPS,RNPS);
Parameter Name | Description |
---|---|
|
The number of pseudo-random bytes to be generated. |
number_bytes
value should not exceed the maximum length of a RAW
variable.SQLNET.CRYPTO_SEED
parameter can be set by entering 10 to 70 random characters with the following syntax in the sqlnet.ora
file:
SQLNET.CRYPTO_SEED = <10 to 70 random characters>
See Also:
Oracle Advanced Security Administrator's Guide for more information about the |
This function returns an integer in the complete range available for the Oracle BINARY_INTEGER
datatype.
DBMS_CRYPTO.RANDOMINTEGER RETURN BINARY_INTEGER;
pragma restrict_references(randominteger,WNDS,RNDS,WNPS,RNPS);
This function returns an integer in the Oracle NUMBER
datatype in the range of [0..2**128-1].
DBMS_CRYPTO.RandomNumber RETURN NUMBER;
pragma restrict_references(randomnumber,WNDS,RNDS,WNPS,RNPS);