diff options
author | Moritz Schulte <mo@g10code.com> | 2003-07-21 22:38:51 +0000 |
---|---|---|
committer | Moritz Schulte <mo@g10code.com> | 2003-07-21 22:38:51 +0000 |
commit | 8eb1640a0e24466283359b662c349d0e950b6282 (patch) | |
tree | ae3cca88e6d97868f143ab0bf75dcf172d89c563 /cipher/cipher.c | |
parent | 8ddfcd51c697ff61c1987d7264528a46745312ab (diff) | |
download | libgcrypt-8eb1640a0e24466283359b662c349d0e950b6282.tar.gz |
2003-07-21 Moritz Schulte <moritz@g10code.com>
* pubkey.c (gcry_pk_lookup_func_name): Use new member name
`aliases' instead of `sexp_names'.
* ac.c (gcry_ac_key_data_get): New function.
* cipher.c (gcry_cipher_lookup_func_name): Fix return value.
2003-07-20 Moritz Schulte <moritz@g10code.com>
* blowfish.c: Adjusted for new gcry_cipher_spec_t structure.
* cast5.c: Likewise.
* twofish.c: Likewise.
* arcfour.c: Likewise.
* rijndael.c (rijndael_oids, rijndael192_oids, rijndael256_oids):
New variables, adjust for new gcry_cipher_spec_t structure.
* des.c (oids_tripledes): New variable, adjust for new
gcry_cipher_spec_t structure.
* md.c (oid_table): Removed.
* tiger.c (oid_spec_tiger): New variable.
(digest_spec_tiger): Adjusted for new gry_md_spec_t structure.
* sha512.c (oid_spec_sha512): New variable.
(digest_spec_sha512): Adjusted for new gry_md_spec_t structure.
* sha512.c (oid_spec_sha384): New variable.
(digest_spec_sha384): Adjusted for new gry_md_spec_t structure.
* sha256.c (oid_spec_sha256): New variable.
(digest_spec_sha256): Adjusted for new gry_md_spec_t structure.
* sha1.c (oid_spec_sha1): New variable.
(digest_spec_sha1): Adjusted for new gry_md_spec_t structure.
* rmd160.c (oid_spec_rmd160): New variable.
(digest_spec_rnd160): Adjusted for new gry_md_spec_t structure.
* md5.c (oid_spec_md5): New variable.
(digest_spec_md5): Adjusted for new gry_md_spec_t structure.
* md4.c (oid_spec_md4): New variable.
(digest_spec_md4): Adjusted for new gry_md_spec_t structure.
* crc.c (digest_spec_crc32, digest_spec_crc32_rfc1510,
digest_spec_crc32_rfc2440): Adjusted for new gry_md_spec_t
structure.
2003-07-19 Moritz Schulte <moritz@g10code.com>
* md.c (gcry_md_lookup_func_oid): New function.
(search_oid): New function, copied from cipher.c.
(gcry_md_map_name): Adjust for new search_oid_interface.
* cipher.c (oid_table): Removed table.
(gcry_cipher_lookup_func_oid): New function.
(search_oid): Rewritten to use the module functions.
(gcry_cipher_map_name): Adjust for new search_oid interface.
(gcry_cipher_mode_from_oid): Likewise.
Diffstat (limited to 'cipher/cipher.c')
-rw-r--r-- | cipher/cipher.c | 145 |
1 files changed, 82 insertions, 63 deletions
diff --git a/cipher/cipher.c b/cipher/cipher.c index 1f2bbced..13b50403 100644 --- a/cipher/cipher.c +++ b/cipher/cipher.c @@ -34,35 +34,6 @@ #define CTX_MAGIC_NORMAL 0x24091964 #define CTX_MAGIC_SECURE 0x46919042 -static struct -{ - const char *oidstring; - int algo; - int mode; -} oid_table[] = - { - { "1.2.840.113549.3.7", GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC }, - - /* OIDs from NIST. See http://csrc.nist.gov.csor/ */ - { "2.16.840.1.101.3.4.1.1", GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB }, - { "2.16.840.1.101.3.4.1.2", GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC }, - { "2.16.840.1.101.3.4.1.3", GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_OFB }, - { "2.16.840.1.101.3.4.1.4", GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CFB }, - { "2.16.840.1.101.3.4.1.21", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_ECB }, - { "2.16.840.1.101.3.4.1.22", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CBC }, - { "2.16.840.1.101.3.4.1.23", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_OFB }, - { "2.16.840.1.101.3.4.1.24", GCRY_CIPHER_AES192, GCRY_CIPHER_MODE_CFB }, - { "2.16.840.1.101.3.4.1.41", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_ECB }, - { "2.16.840.1.101.3.4.1.42", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC }, - { "2.16.840.1.101.3.4.1.43", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB }, - { "2.16.840.1.101.3.4.1.44", GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CFB }, - - /* Teletrust specific OID for 3DES. */ - { "1.3.36.3.1.3.2.1", GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC }, - - { NULL } - }; - /* This is the list of the default ciphers, which are included in libgcrypt. */ static struct cipher_table_entry @@ -212,11 +183,28 @@ gcry_cipher_lookup_func_name (void *spec, void *data) gcry_cipher_spec_t *cipher = (gcry_cipher_spec_t *) spec; char *name = (char *) data; const char **aliases = cipher->aliases; - int ret = stricmp (name, cipher->name), i; + int i, ret = ! stricmp (name, cipher->name); - if (ret && aliases) - for (i = 0; aliases[i] && ret; i++) - ret = stricmp (name, aliases[i]); + if (aliases) + for (i = 0; aliases[i] && (! ret); i++) + ret = ! stricmp (name, aliases[i]); + + return ret; +} + +/* Internal callback function. Used via _gcry_module_lookup. */ +static int +gcry_cipher_lookup_func_oid (void *spec, void *data) +{ + gcry_cipher_spec_t *cipher = (gcry_cipher_spec_t *) spec; + char *oid = (char *) data; + gcry_cipher_oid_spec_t *oid_specs = cipher->oids; + int ret = 0, i; + + if (oid_specs) + for (i = 0; oid_specs[i].oid && (! ret); i++) + if (! stricmp (oid, oid_specs[i].oid)) + ret = 1; return ret; } @@ -233,6 +221,18 @@ gcry_cipher_lookup_name (const char *name) return cipher; } +/* Internal function. Lookup a cipher entry by it's oid. */ +static gcry_module_t +gcry_cipher_lookup_oid (const char *oid) +{ + gcry_module_t cipher; + + cipher = _gcry_module_lookup (ciphers_registered, (void *) oid, + gcry_cipher_lookup_func_oid); + + return cipher; +} + /* Register a new cipher module whose specification can be found in CIPHER. On success, a new algorithm ID is stored in ALGORITHM_ID and a pointer representhing this module is stored in MODULE. */ @@ -271,24 +271,34 @@ gcry_cipher_unregister (gcry_module_t module) /* locate the OID in the oid table and return the index or -1 when not found */ static int -search_oid (const char *string) +search_oid (const char *oid, int *algorithm, gcry_cipher_oid_spec_t *oid_spec) { - const char *s; - int i; + gcry_module_t module; + int ret = 0; - if (string && (digitp (string) - || !strncmp (string, "oid.", 4) - || !strncmp (string, "OID.", 4) )) - { - s = digitp(string)? string : (string+4); + if (oid && ((! strncmp (oid, "oid.", 4)) + || (! strncmp (oid, "OID.", 4)))) + oid += 4; - for (i=0; oid_table[i].oidstring; i++) - { - if (!strcmp (s, oid_table[i].oidstring)) - return i; - } + module = gcry_cipher_lookup_oid (oid); + if (module) + { + gcry_cipher_spec_t *cipher = module->spec; + int i; + + for (i = 0; cipher->oids[i].oid && (! ret); i++) + if (! stricmp (oid, cipher->oids[i].oid)) + { + if (algorithm) + *algorithm = module->mod_id; + if (oid_spec) + *oid_spec = cipher->oids[i]; + ret = 1; + } + _gcry_module_release (module); } - return -1; + + return ret; } /**************** @@ -297,30 +307,33 @@ search_oid (const char *string) * 0 if the name is not known. */ int -gcry_cipher_map_name( const char *string ) +gcry_cipher_map_name (const char *string) { gcry_module_t cipher; - int i, algorithm = 0; - - if (!string) + int ret, algorithm = 0; + + if (! string) return 0; + REGISTER_DEFAULT_CIPHERS; + /* If the string starts with a digit (optionally prefixed with either "OID." or "oid."), we first look into our table of ASN.1 object identifiers to figure out the algorithm */ - i = search_oid (string); - if (i != -1) - return oid_table[i].algo; - - REGISTER_DEFAULT_CIPHERS; ath_mutex_lock (&ciphers_registered_lock); - cipher = gcry_cipher_lookup_name (string); - if (cipher) + + ret = search_oid (string, &algorithm, NULL); + if (! ret) { - algorithm = cipher->mod_id; - _gcry_module_release (cipher); + cipher = gcry_cipher_lookup_name (string); + if (cipher) + { + algorithm = cipher->mod_id; + _gcry_module_release (cipher); + } } + ath_mutex_unlock (&ciphers_registered_lock); return algorithm; @@ -329,10 +342,16 @@ gcry_cipher_map_name( const char *string ) int gcry_cipher_mode_from_oid (const char *string) { - int i; + gcry_cipher_oid_spec_t oid_spec; + int ret = 0, mode = 0; + + ath_mutex_lock (&ciphers_registered_lock); + ret = search_oid (string, NULL, &oid_spec); + if (ret) + mode = oid_spec.mode; + ath_mutex_unlock (&ciphers_registered_lock); - i = search_oid (string); - return i == -1? 0 : oid_table[i].mode; + return mode; } |