libopensc: some usefull macros, crc32 calculation procedure

Introduce some usefull define macros, error code 'inconsistent configuration'.
Introduce procedure to calculate CRC32 digest,
to be used in minidriver to calculate the 'freshness' values.
This commit is contained in:
Viktor Tarasov 2012-05-29 11:29:44 +02:00
parent 9c882ff5c2
commit 9d5404bac6
8 changed files with 74 additions and 16 deletions

View File

@ -94,7 +94,7 @@ const char *sc_strerror(int error)
"Requested object not found",
"Not supported",
"Passphrase required",
"UNUSED",
"Inconsistent configuration",
"Decryption failed",
"Wrong padding",
"Unsupported card",

View File

@ -87,7 +87,7 @@ extern "C" {
#define SC_ERROR_OBJECT_NOT_FOUND -1407
#define SC_ERROR_NOT_SUPPORTED -1408
#define SC_ERROR_PASSPHRASE_REQUIRED -1409
/* Unused: -1410 */
#define SC_ERROR_INCONSISTENT_CONFIGURATION -1410
#define SC_ERROR_DECRYPT_FAILED -1411
#define SC_ERROR_WRONG_PADDING -1412
#define SC_ERROR_WRONG_CARD -1413

View File

@ -34,7 +34,7 @@
#define IASECC_DOCP_TAG_TRIES_REMAINING 0x9B
#define IASECC_DOCP_TAG_USAGE_MAXIMUM 0x9C
#define IASECC_DOCP_TAG_USAGE_REMAINING 0x9D
#define IASECC_DOCP_TAG_NON_REPUDATION 0x9E
#define IASECC_DOCP_TAG_NON_REPUDATION 0x9E
#define IASECC_DOCP_TAG_SIZE 0x80
#define IASECC_DOCP_TAG_ACLS 0xA1
#define IASECC_DOCP_TAG_ACLS_CONTACT 0x8C
@ -55,6 +55,11 @@
#define IASECC_ACLS_RSAKEY_PUT_DATA 5
#define IASECC_ACLS_RSAKEY_GET_DATA 6
#define IASECC_ACLS_KEYSET_EXTERNAL_AUTH 1
#define IASECC_ACLS_KEYSET_MUTUAL_AUTH 3
#define IASECC_ACLS_KEYSET_PUT_DATA 5
#define IASECC_ACLS_KEYSET_GET_DATA 6
#define IASECC_SDO_CHV_TAG 0x7F41
#define IASECC_SDO_CHV_TAG_SIZE_MAX 0x80
#define IASECC_SDO_CHV_TAG_SIZE_MIN 0x81

View File

@ -52,6 +52,7 @@
#define IASECC_FCP_TAG_SFID 0x88
#define IASECC_FCP_TAG_ACLS 0xA1
#define IASECC_FCP_TAG_ACLS_CONTACT 0x8C
#define IASECC_FCP_TAG_ACLS_CONTACTLESS 0x9C
#define IASECC_FCP_TYPE_EF 0x01
#define IASECC_FCP_TYPE_DF 0x38

View File

@ -294,4 +294,5 @@ sc_card_find_rsa_alg
sc_print_cache
sc_find_app
sc_remote_data_init
sc_crc32
sc_perform_pace

View File

@ -111,10 +111,11 @@ extern "C" {
/* Or should the HASH_NONE be 0x00000010 and HASHES be 0x00008010 */
/* May need more bits if card can do more hashes */
/* TODO: -DEE Will overload RSA_HASHES with EC_HASHES */
/* TODO: -DEE Will overload RSA_HASHES with EC_HASHES */
/* Not clear if these need their own bits or not */
/* The PIV card does not support and hashes */
#define SC_ALGORITHM_ECDSA_RAW 0x00010000
#define SC_ALGORITHM_ECDSA_RAW 0x00010000
#define SC_ALGORITHM_ECDH_CDH_RAW 0x00020000
#define SC_ALGORITHM_ECDSA_HASH_NONE SC_ALGORITHM_RSA_HASH_NONE
#define SC_ALGORITHM_ECDSA_HASH_SHA1 SC_ALGORITHM_RSA_HASH_SHA1
#define SC_ALGORITHM_ECDSA_HASH_SHA224 SC_ALGORITHM_RSA_HASH_SHA224
@ -282,7 +283,7 @@ typedef struct sc_reader {
const struct sc_reader_operations *ops;
void *drv_data;
char *name;
unsigned long flags, capabilities;
unsigned int supported_protocols, active_protocol;
@ -754,11 +755,21 @@ typedef struct {
/** mutex functions to use (optional) */
sc_thread_context_t *thread_ctx;
} sc_context_param_t;
/**
* Repairs an already existing sc_context_t object. This may occur if
* multithreaded issues mean that another context in the same heap is deleted.
* @param ctx pointer to a sc_context_t pointer containing the (partial)
* context.
* @return SC_SUCCESS or an error value if an error occurred.
*/
int sc_context_repair(sc_context_t **ctx);
/**
* Creates a new sc_context_t object.
* @param ctx pointer to a sc_context_t pointer for the newly
* created sc_context_t object.
* @param parm parameters for the sc_context_t creation (see
* @param parm parameters for the sc_context_t creation (see
* sc_context_param_t for a description of the supported
* options). This parameter is optional and can be NULL.
* @return SC_SUCCESS on success and an error code otherwise.
@ -1245,9 +1256,16 @@ struct sc_algorithm_info * sc_card_find_gostr3410_alg(sc_card_t *card,
unsigned int key_length);
/**
* Used to initialize the @c sc_remote_data structure --
* reset the header of the 'remote APDUs' list, set the handlers
* to manipulate the list.
* Get CRC-32 digest
* @param value pointer to data used for CRC calculation
* @param len length of data used for CRC calculation
*/
unsigned sc_crc32(unsigned char *value, size_t len);
/**
* Used to initialize the @c sc_remote_data structure --
* reset the header of the 'remote APDUs' list, set the handlers
* to manipulate the list.
*/
void sc_remote_data_init(struct sc_remote_data *rdata);

View File

@ -47,7 +47,7 @@ int sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
struct sc_pkcs15_data *data_object;
u8 *data = NULL;
size_t len;
if (p15card == NULL || info == NULL || data_object_out == NULL)
return SC_ERROR_INVALID_ARGUMENTS;
SC_FUNC_CALLED(p15card->card->ctx, SC_LOG_DEBUG_VERBOSE);
@ -61,11 +61,11 @@ int sc_pkcs15_read_data_object(struct sc_pkcs15_card *p15card,
return SC_ERROR_OUT_OF_MEMORY;
}
memset(data_object, 0, sizeof(struct sc_pkcs15_data));
data_object->data = data;
data_object->data_len = len;
*data_object_out = data_object;
return 0;
return SC_SUCCESS;
}
static const struct sc_asn1_entry c_asn1_data[] = {
@ -99,7 +99,7 @@ int sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card *p15card,
sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
sc_copy_asn1_entry(c_asn1_data, asn1_data);
sc_format_asn1_entry(asn1_com_data_attr + 0, &info.app_label, &label_len, 0);
sc_format_asn1_entry(asn1_com_data_attr + 1, &info.app_oid, NULL, 0);
sc_format_asn1_entry(asn1_type_data_attr + 0, &info.path, NULL, 0);
@ -129,7 +129,7 @@ int sc_pkcs15_decode_dodf_entry(struct sc_pkcs15_card *p15card,
SC_FUNC_RETURN(ctx, SC_LOG_DEBUG_NORMAL, SC_ERROR_OUT_OF_MEMORY);
memcpy(obj->data, &info, sizeof(info));
return 0;
return SC_SUCCESS;
}
int sc_pkcs15_encode_dodf_entry(sc_context_t *ctx,
@ -151,7 +151,7 @@ int sc_pkcs15_encode_dodf_entry(sc_context_t *ctx,
sc_copy_asn1_entry(c_asn1_com_data_attr, asn1_com_data_attr);
sc_copy_asn1_entry(c_asn1_type_data_attr, asn1_type_data_attr);
sc_copy_asn1_entry(c_asn1_data, asn1_data);
if (label_len) {
sc_format_asn1_entry(asn1_com_data_attr + 0,
&info->app_label, &label_len, 1);

View File

@ -813,6 +813,39 @@ void sc_remote_data_init(struct sc_remote_data *rdata)
rdata->free = sc_remote_apdu_free;
}
static unsigned long sc_CRC_tab32[256];
static int sc_CRC_tab32_initialized = 0;
unsigned sc_crc32(unsigned char *value, size_t len)
{
size_t ii, jj;
unsigned long crc;
unsigned long index, long_c;
if (!sc_CRC_tab32_initialized) {
for (ii=0; ii<256; ii++) {
crc = (unsigned long) ii;
for (jj=0; jj<8; jj++) {
if ( crc & 0x00000001L )
crc = ( crc >> 1 ) ^ 0xEDB88320l;
else
crc = crc >> 1;
}
sc_CRC_tab32[ii] = crc;
}
sc_CRC_tab32_initialized = 1;
}
crc = 0xffffffffL;
for (ii=0; ii<len; ii++) {
long_c = 0x000000ffL & (unsigned long) (*(value + ii));
index = crc ^ long_c;
crc = (crc >> 8) ^ sc_CRC_tab32[ index & 0xff ];
}
crc ^= 0xffffffff;
return crc%0xffff;
}
/**************************** mutex functions ************************/
int sc_mutex_create(const sc_context_t *ctx, void **mutex)