* Add new libcryptsetup API (documented in libcryptsetup.h).

Signed-off-by: Milan Broz <mbroz@redhat.com>

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@96 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
Milan Broz
2009-08-30 18:09:53 +00:00
parent 344a162a29
commit d70782478e
3 changed files with 1470 additions and 19 deletions

View File

@@ -8,13 +8,411 @@ extern "C" {
struct crypt_device; /* crypt device handle */
#define CRYPT_ANY_SLOT -1
typedef enum { SLOT_INVALID, SLOT_INACTIVE, SLOT_ACTIVE, SLOT_ACTIVE_LAST } crypt_keyslot_info;
/**
* Initialise crypt device handle and check if provided device exists.
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - returns pointer to crypt device handle
*
* Note that logging is not initialized here, possible messages uses
* default log function.
*/
int crypt_init(struct crypt_device **cd, const char *device);
/**
* Set log function.
*
* @cd - crypt device handle
* @usrptr - provided identification in callback
* @class - log type below (debug messages can uses other levels)
* @msg - log message
*/
#define CRYPT_LOG_NORMAL 0
#define CRYPT_LOG_ERROR 1
#define CRYPT_LOG_DEBUG -1 /* always on stdout */
#define CRYPT_LOG_DEBUG -1 /* always on stdout */
void crypt_set_log_callback(struct crypt_device *cd,
void (*log)(int class, const char *msg, void *usrptr),
void *usrptr);
/**
* Set confirmation callback (yes/no)
*
* If code need confirmation (like deleting last key slot) this function
* is called. If not defined, everything is confirmed.
*
* Calback should return 0 if operation is declined, other values mean accepted.
*
* @cd - crypt device handle
* @usrptr - provided identification in callback
* @msg - Message for user to confirm
*/
void crypt_set_confirm_callback(struct crypt_device *cd,
int (*confirm)(const char *msg, void *usrptr),
void *usrptr);
/**
* Set password query callback.
*
* If code need _interactive_ query for password, this callback is called.
* If not defined, compiled-in default is called (uses terminal input).
*
* @cd - crypt device handle
* @usrptr - provided identification in callback
* @msg - Message for user
* @buf - buffer for password
* @length - size of buffer
*
* - Note that if this function is defined, verify option is ignored
* (caller whch provided callback is responsible fo password verification)
* - Only zero terminated passwords can be enteted this way, for complex
* API functions directly.
* - Maximal length of password is limited to @length-1 (minimal 511 chars)
*/
void crypt_set_password_callback(struct crypt_device *cd,
int (*password)(const char *msg, char *buf, size_t length, void *usrptr),
void *usrptr);
/**
* Various crypt device parameters
*
* @cd - crypt device handle
* @timeout - timeout in secons for password entry if compiled-in function used
* @password_retry - number of tries for password if not verified
* @iteration_time - iteration time for LUKS header in miliseconds
* @password_verify - for compiled-in password query always verify passwords twice
*/
void crypt_set_timeout(struct crypt_device *cd, uint64_t timeout_sec);
void crypt_set_password_retry(struct crypt_device *cd, int tries);
void crypt_set_iterarion_time(struct crypt_device *cd, uint64_t iteration_time_ms);
void crypt_set_password_verify(struct crypt_device *cd, int password_verify);
/**
* Helper to lock/unlock memory to avoid swapping sesitive data_alignment
*
* @cd - crypt device handle, can be NULL
* @lock - 0 to unloct otherwise lock memory
*
* Return value indicated that memory is locked (function can be called multiple times).
* Only root can do this. Note it locks/unlocks all process memory, not only crypt context.
*/
int crypt_memory_lock(struct crypt_device *cd, int lock);
#define CRYPT_PLAIN "PLAIN" /* regular crypt device, no on-disk header */
#define CRYPT_LUKS1 "LUKS1" /* LUKS version 1 header on-disk */
struct crypt_params_plain {
const char *hash; /* password hash function */
uint64_t offset; /* offset in sectors */
uint64_t skip; /* IV initilisation sector */
};
struct crypt_params_luks1 {
const char *hash; /* hash used in LUKS header */
size_t data_alignment; /* in sectors, data offset is multiple of this */
};
/**
* Create (format) new crypt device (and possible header on-disk) but not activates it.
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle
* @type - type of device (optional params struct must be of this type)
* @cipher - (e.g. "aes")
* @cipher_mode - including IV specification (e.g. "xts-plain")
* @uuid - requested UUID or NULL if it should be generated
* @volume_key - pre-generated volume key or NULL if it should be generated (only for LUKS)
* @volume_key_size - size og volume key in bytes.
* @params - crypt type specific parameters
*
* Note that crypt_format do not enable any keyslot, but it stores volume key internally
* and subsequent crypt_keyslot_add_* calls can be used.
* (It is the only situation when crypt_keyslot_add_* do not require active key slots.)
*/
int crypt_format(struct crypt_device *cd,
const char *type,
const char *cipher,
const char *cipher_mode,
const char *uuid,
const char *volume_key,
size_t volume_key_size,
void *params);
/**
* Load crypt device parameters from on-disk header
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle
* @requested_type - use NULL for all known
* @params - crypt type specific parameters
*/
int crypt_load(struct crypt_device *cd,
const char *requested_type,
void *params);
/**
* Releases crypt device context and used memory.
*
* @cd - crypt device handle
*/
void crypt_free(struct crypt_device *cd);
/**
* Add key slot using provided passphrase
*
* Returns allocated key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @keyslot - requested keyslot or CRYPT_ANY_SLOT
* @passphrase - passphrase used to unlock volume key, NULL for query
* @passphrase_size - size of @passphrase (binary data)
* @new_passphrase - passphrase for new keyslot, NULL for query
* @new_passphrase_size - size of @new_passphrase (binary data)
*/
#define CRYPT_ANY_SLOT -1
int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
int keyslot,
const char *passphrase,
size_t passphrase_size,
const char *new_passphrase,
size_t new_passphrase_size);
/**
* Add key slot using provided key file path
*
* Returns allocated key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @keyslot - requested keyslot or CRYPT_ANY_SLOT
* @keyfile - key file used to unlock volume key, NULL for passphrase query
* @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
* @new_keyfile - keyfile for new keyslot, NULL for passphrase query
* @new_keyfile_size - number of bytes to read from @new_keyfile, 0 is unlimited
*
* Note that @keyfile can be "-" for STDIN
*/
int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
int keyslot,
const char *keyfile,
size_t keyfile_size,
const char *new_keyfile,
size_t new_keyfile_size);
/**
* Add key slot using provided volume key
*
* Returns allocated key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @keyslot - requested keyslot or CRYPT_ANY_SLOT
* @volume_key - provided volume key or NULL if used after crypt_format
* @volume_key_size - size of @volume_key
* @passphrase - passphrase for new keyslot, NULL for query
* @passphrase_size - size of @passphrase
*/
int crypt_keyslot_add_by_volume_key(struct crypt_device *cd,
int keyslot,
const char *volume_key,
size_t volume_key_size,
const char *passphrase,
size_t passphrase_size);
/**
* Destroy (and disable) key slot
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle
* @keyslot - requested key slot to destroy
*
* Note that there is no passphrase verification used.
*/
int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot);
/**
* Activation flags
*/
#define CRYPT_ACTIVATE_READONLY (1 << 0)
#define CRYPT_ACTIVATE_NO_UUID (1 << 1)
/**
* Activate device or check passphrase
*
* Returns unlocked key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @name - name of device to create, if NULL only check passphrase
* @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
* @passphrase - passphrase used to unlock volume key, NULL for query
* @passphrase_size - size of @passphrase
* @flags - activation flags
*/
int crypt_activate_by_passphrase(struct crypt_device *cd,
const char *name,
int keyslot,
const char *passphrase,
size_t passphrase_size,
uint32_t flags);
/**
* Activate device or check using key file
*
* Returns unlocked key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @name - name of device to create, if NULL only check keyfile
* @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
* @keyfile - key file used to unlock volume key
* @keyfile_size - number of bytes to read from @keyfile, 0 is unlimited
* @flags - activation flags
*/
int crypt_activate_by_keyfile(struct crypt_device *cd,
const char *name,
int keyslot,
const char *keyfile,
size_t keyfile_size,
uint32_t flags);
/**
* Activate device using provided volume key
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle
* @name - name of device to create, if NULL only check volume key
* @volume_key - provided volume key
* @volume_key_size - size of @volume_key
* @flags - activation flags
*/
int crypt_activate_by_volume_key(struct crypt_device *cd,
const char *name,
const char *volume_key,
size_t volume_key_size,
uint32_t flags);
/**
* Deactivate crypt device
*
* @cd - crypt device handle, can be NULL
* @name - name of device to deactivate
*/
int crypt_deactivate(struct crypt_device *cd, const char *name);
/**
* Get volume key from of crypt device
*
* Returns unlocked key slot number or negative errno otherwise.
*
* @cd - crypt device handle
* @keyslot - use this keyslot or CRYPT_ANY_SLOT
* @volume_key - buffer for volume key
* @volume_key_size - on input, size of buffer @volume_key,
* on output size of @volume_key
* @passphrase - passphrase used to unlock volume key, NULL for query
* @passphrase_size - size of @passphrase
*/
int crypt_volume_key_get(struct crypt_device *cd,
int keyslot,
char *volume_key,
size_t *volume_key_size,
const char *passphrase,
size_t passphrase_size);
/**
* Verify that provided volume key is valid for crypt device
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle
* @volume_key - provided volume key
* @volume_key_size - size of @volume_key
*/
int crypt_volume_key_verify(struct crypt_device *cd,
const char *volume_key,
size_t volume_key_size);
/**
* Get status info about device name
*
* Returns value defined by crypt_status_info.
*
* @cd - crypt device handle, can be NULL
* @name -crypt device name
*
* INACTIVE - no such mapped device
* ACTIVE - device is active
* BUSY - device is active and has open count > 0
*/
typedef enum { INVALID, INACTIVE, ACTIVE, BUSY } crypt_status_info;
crypt_status_info crypt_status(struct crypt_device *cd, const char *name);
/**
* Dump text-formatted information about crypt device to log output
*
* Returns 0 on success or negative errno value otherwise.
*
* @cd - crypt device handle, can be NULL
*/
int crypt_dump(struct crypt_device *cd);
/**
* Various crypt device info functions
*
* @cd - crypt device handle
*
* cipher - used cipher, e.g. "aes" or NULL otherwise
* cipher_mode - used cipher mode including IV, e.g. "xts-plain" or NULL otherwise
* uuid - device UUID or NULL if not set
* data_offset - device offset in sectors where real data starts on underlying device)
* volume_key_size - size (in bytes) of volume key for crypt device
*/
const char *crypt_get_cipher(struct crypt_device *cd);
const char *crypt_get_cipher_mode(struct crypt_device *cd);
const char *crypt_get_uuid(struct crypt_device *cd);
uint64_t crypt_get_data_offset(struct crypt_device *cd);
int crypt_get_volume_key_size(struct crypt_device *cd);
/**
* Get information about particular key slot
*
* Returns value defined by crypt_keyslot_info.
*
* @cd - crypt device handle
* @keyslot - requested keyslot to check or CRYPT_ANY_SLOT
*/
typedef enum { SLOT_INVALID, SLOT_INACTIVE, SLOT_ACTIVE, SLOT_ACTIVE_LAST } crypt_keyslot_info;
crypt_keyslot_info crypt_keyslot_status(struct crypt_device *cd, int keyslot);
/**
* Receives last reported error
*
* @buf - buffef for message
* @size - size of buffer
*
* Note that this is old API function using global context.
* All error messages are reported also through log callback.
*/
void crypt_get_error(char *buf, size_t size);
/**
* Get directory where mapped crypt devices are created
*/
const char *crypt_get_dir(void);
/**
* Set library debug level
*/
#define CRYPT_DEBUG_ALL -1
#define CRYPT_DEBUG_NONE 0
void crypt_set_debug_level(int level);
/**
* OLD DEPRECATED API **********************************
*
* Provided only for backward compatibility.
*/
struct interface_callbacks {
int (*yesDialog)(char *msg);
@@ -69,9 +467,7 @@ int crypt_luksUUID(struct crypt_options *options);
int crypt_isLuks(struct crypt_options *options);
int crypt_luksDump(struct crypt_options *options);
void crypt_get_error(char *buf, size_t size);
void crypt_put_options(struct crypt_options *options);
const char *crypt_get_dir(void);
#ifdef __cplusplus
}