Committer: Miika Komu <miika@xxxxxx> Date: Tue Mar 09 18:09:38 2010 +0200 Revision: 3875 Revision-id: miika@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Branch nick: trunk Log: Splitting misc.c continued (bug id 1139). Added a separate source and header file for transforms. Modified: A lib/core/transform.c A lib/core/transform.h M Makefile.am M hipd/input.c M lib/core/crypto.h M lib/core/misc.c M lib/core/misc.h === modified file 'Makefile.am' --- Makefile.am 2010-03-09 08:57:46 +0000 +++ Makefile.am 2010-03-09 16:09:38 +0000 @@ -257,6 +257,7 @@ lib/core/hostid.c \ lib/core/hip_udp.c \ lib/core/solve.c \ + lib/core/transform.c \ lib/core/sqlitedbapi.c \ lib/core/hashchain_store.c \ lib/core/hip_statistics.c \ === modified file 'hipd/input.c' --- hipd/input.c 2010-03-09 08:57:46 +0000 +++ hipd/input.c 2010-03-09 16:09:38 +0000 @@ -30,6 +30,7 @@ #include "lib/core/builder.h" #include "lib/core/hip_udp.h" #include "lib/core/solve.h" +#include "lib/core/transform.h" #include "dh.h" #include "lib/core/misc.h" #include "hidb.h" === modified file 'lib/core/crypto.h' --- lib/core/crypto.h 2010-03-08 21:21:01 +0000 +++ lib/core/crypto.h 2010-03-09 16:09:38 +0000 @@ -28,6 +28,7 @@ #include "hipd/hidb.h" #include "lib/core/debug.h" +#include "lib/core/transform.h" #include "lib/core/ife.h" #include "hipd/hadb.h" #define HIP_DSA_SIG_SIZE 41 /* T(1) + R(20) + S(20) from RFC 2536 */ === modified file 'lib/core/misc.c' --- lib/core/misc.c 2010-03-09 08:57:46 +0000 +++ lib/core/misc.c 2010-03-09 16:09:38 +0000 @@ -255,35 +255,6 @@ return ret; } -/** - * get transform key length for a transform - * @param tid transform - * - * @return the transform key length based for the chosen transform, - * or negative on error. - */ -int hip_transform_key_length(int tid) -{ - int ret = -1; - - switch (tid) { - case HIP_HIP_AES_SHA1: - ret = 16; - break; - case HIP_HIP_3DES_SHA1: - ret = 24; - break; - case HIP_HIP_NULL_SHA1: // XX FIXME: SHOULD BE NULL_SHA1? - ret = 0; - break; - default: - HIP_ERROR("unknown tid=%d\n", tid); - HIP_ASSERT(0); - break; - } - - return ret; -} /** * get authentication key length for an ESP transform @@ -315,111 +286,6 @@ } /** - * select a HIP transform - * - * @param ht HIP_TRANSFORM payload where the transform is selected from - * @return the first acceptable Transform-ID or negative if no - * acceptable transform was found. The return value is in host byte order. - */ -hip_transform_suite_t hip_select_hip_transform(struct hip_hip_transform *ht) -{ - hip_transform_suite_t tid = 0; - int i; - int length; - hip_transform_suite_t *suggestion; - - length = ntohs(ht->length); - suggestion = (hip_transform_suite_t *) &ht->suite_id[0]; - - if ((length >> 1) > 6) { - HIP_ERROR("Too many transforms (%d)\n", length >> 1); - goto out; - } - - for (i = 0; i < length; i++) { - switch (ntohs(*suggestion)) { - case HIP_HIP_AES_SHA1: - case HIP_HIP_3DES_SHA1: - case HIP_HIP_NULL_SHA1: - tid = ntohs(*suggestion); - goto out; - break; - - default: - /* Specs don't say what to do when unknown are found. - * We ignore. - */ - HIP_ERROR("Unknown HIP suite id suggestion (%u)\n", - ntohs(*suggestion)); - break; - } - suggestion++; - } - -out: - if (tid == 0) { - HIP_ERROR("None HIP transforms accepted\n"); - } else { - HIP_DEBUG("Chose HIP transform: %d\n", tid); - } - - return tid; -} - -/** - * select an ESP transform to use - * @param ht ESP_TRANSFORM payload where the transform is selected from - * - * @return the first acceptable Suite-ID or negative if no - * acceptable Suite-ID was found. - */ -hip_transform_suite_t hip_select_esp_transform(struct hip_esp_transform *ht) -{ - hip_transform_suite_t tid = 0; - int i; - int length; - hip_transform_suite_t *suggestion; - - length = hip_get_param_contents_len(ht); - suggestion = (uint16_t *) &ht->suite_id[0]; - - if (length > sizeof(struct hip_esp_transform) - - sizeof(struct hip_common)) { - HIP_ERROR("Too many transforms\n"); - goto out; - } - - for (i = 0; i < length; i++) { - switch (ntohs(*suggestion)) { - case HIP_ESP_AES_SHA1: - case HIP_ESP_NULL_NULL: - case HIP_ESP_3DES_SHA1: - case HIP_ESP_NULL_SHA1: - tid = ntohs(*suggestion); - goto out; - break; - default: - /* Specs don't say what to do when unknowns are found. - * We ignore. - */ - HIP_ERROR("Unknown ESP suite id suggestion (%u)\n", - ntohs(*suggestion)); - break; - } - suggestion++; - } - -out: - HIP_DEBUG("Took ESP transform %d\n", tid); - - if (tid == 0) { - HIP_ERROR("Faulty ESP transform\n"); - } - - return tid; -} - -/** * Generate the IPv4 header checksum * * @param s source address === modified file 'lib/core/misc.h' --- lib/core/misc.h 2010-03-09 08:57:46 +0000 +++ lib/core/misc.h 2010-03-09 16:09:38 +0000 @@ -126,10 +126,7 @@ int convert_string_to_address_v4(const char *str, struct in_addr *ip); int convert_string_to_address(const char *str, struct in6_addr *ip6); -hip_transform_suite_t hip_select_esp_transform(struct hip_esp_transform *ht); -hip_transform_suite_t hip_select_hip_transform(struct hip_hip_transform *ht); int hip_auth_key_length_esp(int tid); -int hip_transform_key_length(int tid); int hip_hmac_key_length(int tid); int hip_enc_key_length(int tid); uint64_t hip_get_current_birthday(void); === added file 'lib/core/transform.c' --- lib/core/transform.c 1970-01-01 00:00:00 +0000 +++ lib/core/transform.c 2010-03-09 16:09:38 +0000 @@ -0,0 +1,148 @@ +/** + * @file + * + * Distributed under <a href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a> + * + * @brief Transform related functions for HIP + * + * @author Miika Komu <miika@xxxxxx> + */ + +#include "debug.h" +#include "builder.h" +#include "transform.h" + +/** + * select a HIP transform + * + * @param ht HIP_TRANSFORM payload where the transform is selected from + * @return the first acceptable Transform-ID or negative if no + * acceptable transform was found. The return value is in host byte order. + */ +hip_transform_suite_t hip_select_hip_transform(struct hip_hip_transform *ht) +{ + hip_transform_suite_t tid = 0; + int i; + int length; + hip_transform_suite_t *suggestion; + + length = ntohs(ht->length); + suggestion = (hip_transform_suite_t *) &ht->suite_id[0]; + + if ((length >> 1) > 6) { + HIP_ERROR("Too many transforms (%d)\n", length >> 1); + goto out; + } + + for (i = 0; i < length; i++) { + switch (ntohs(*suggestion)) { + case HIP_HIP_AES_SHA1: + case HIP_HIP_3DES_SHA1: + case HIP_HIP_NULL_SHA1: + tid = ntohs(*suggestion); + goto out; + break; + + default: + /* Specs don't say what to do when unknown are found. + * We ignore. + */ + HIP_ERROR("Unknown HIP suite id suggestion (%u)\n", + ntohs(*suggestion)); + break; + } + suggestion++; + } + +out: + if (tid == 0) { + HIP_ERROR("None HIP transforms accepted\n"); + } else { + HIP_DEBUG("Chose HIP transform: %d\n", tid); + } + + return tid; +} + +/** + * select an ESP transform to use + * @param ht ESP_TRANSFORM payload where the transform is selected from + * + * @return the first acceptable Suite-ID or negative if no + * acceptable Suite-ID was found. + */ +hip_transform_suite_t hip_select_esp_transform(struct hip_esp_transform *ht) +{ + hip_transform_suite_t tid = 0; + int i; + int length; + hip_transform_suite_t *suggestion; + + length = hip_get_param_contents_len(ht); + suggestion = (uint16_t *) &ht->suite_id[0]; + + if (length > sizeof(struct hip_esp_transform) - + sizeof(struct hip_common)) { + HIP_ERROR("Too many transforms\n"); + goto out; + } + + for (i = 0; i < length; i++) { + switch (ntohs(*suggestion)) { + case HIP_ESP_AES_SHA1: + case HIP_ESP_NULL_NULL: + case HIP_ESP_3DES_SHA1: + case HIP_ESP_NULL_SHA1: + tid = ntohs(*suggestion); + goto out; + break; + default: + /* Specs don't say what to do when unknowns are found. + * We ignore. + */ + HIP_ERROR("Unknown ESP suite id suggestion (%u)\n", + ntohs(*suggestion)); + break; + } + suggestion++; + } + +out: + HIP_DEBUG("Took ESP transform %d\n", tid); + + if (tid == 0) { + HIP_ERROR("Faulty ESP transform\n"); + } + + return tid; +} + +/** + * get transform key length for a transform + * @param tid transform + * + * @return the transform key length based for the chosen transform, + * or negative on error. + */ +int hip_transform_key_length(int tid) +{ + int ret = -1; + + switch (tid) { + case HIP_HIP_AES_SHA1: + ret = 16; + break; + case HIP_HIP_3DES_SHA1: + ret = 24; + break; + case HIP_HIP_NULL_SHA1: // XX FIXME: SHOULD BE NULL_SHA1? + ret = 0; + break; + default: + HIP_ERROR("unknown tid=%d\n", tid); + HIP_ASSERT(0); + break; + } + + return ret; +} === added file 'lib/core/transform.h' --- lib/core/transform.h 1970-01-01 00:00:00 +0000 +++ lib/core/transform.h 2010-03-09 16:09:38 +0000 @@ -0,0 +1,14 @@ +#ifndef HIP_LIB_CORE_TRANSFORM_H +#define HIP_LIB_CORE_TRANSFORM_H + +#include "protodefs.h" + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif /* HAVE_CONFIG_H */ + +hip_transform_suite_t hip_select_esp_transform(struct hip_esp_transform *ht); +hip_transform_suite_t hip_select_hip_transform(struct hip_hip_transform *ht); +int hip_transform_key_length(int tid); + +#endif /* HIP_LIB_CORE_TRANSFORM_H */