From: Jonathan Bastien-Filiatrault Date: Tue, 28 Jul 2009 01:47:55 +0000 (-0400) Subject: dtls: Add types and operations required for the DTLS epoch and sequence. X-Git-Tag: gnutls_2_99_0~254 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47d20bdbb1c21a42dca612d06687edb3ec56ad64;p=thirdparty%2Fgnutls.git dtls: Add types and operations required for the DTLS epoch and sequence. Signed-off-by: Nikos Mavrogiannopoulos --- diff --git a/lib/gnutls_int.h b/lib/gnutls_int.h index 81563215f5..2409776e8f 100644 --- a/lib/gnutls_int.h +++ b/lib/gnutls_int.h @@ -57,6 +57,11 @@ typedef struct unsigned char i[8]; } uint64; +typedef struct +{ + unsigned char i[6]; +} uint48; + #include /* diff --git a/lib/gnutls_num.c b/lib/gnutls_num.c index 2c75541ca6..eeadd3e280 100644 --- a/lib/gnutls_num.c +++ b/lib/gnutls_num.c @@ -62,6 +62,35 @@ _gnutls_uint64pp (uint64 * x) return 0; } +/* This function will add one to uint48 x. + * Returns 0 on success, or -1 if the uint48 max limit + * has been reached. + */ +int +_gnutls_uint48pp (uint48 * x) +{ + register int i, y = 0; + + for (i = 5; i >= 0; i--) + { + y = 0; + if (x->i[i] == 0xff) + { + x->i[i] = 0; + y = 1; + } + else + x->i[i]++; + + if (y == 0) + break; + } + if (y != 0) + return -1; /* over 48 bits! meh... */ + + return 0; +} + uint32_t _gnutls_uint24touint32 (uint24 num) { diff --git a/lib/gnutls_num.h b/lib/gnutls_num.h index 437539ec17..2157804c57 100644 --- a/lib/gnutls_num.h +++ b/lib/gnutls_num.h @@ -43,7 +43,8 @@ void _gnutls_write_uint16 (uint16_t num, opaque * data); uint32_t _gnutls_uint64touint32 (const uint64 *); int _gnutls_uint64pp (uint64 *); -#define _gnutls_uint64zero(x) x.i[0] = x.i[1] = x.i[2] = x.i[3] = x.i[4] = x.i[5] = x.i[6] = x.i[7] = 0 -#define UINT64DATA(x) ((x).i) +int _gnutls_uint48pp (uint48 *); +# define _gnutls_uint64zero(x) x.i[0] = x.i[1] = x.i[2] = x.i[3] = x.i[4] = x.i[5] = x.i[6] = x.i[7] = 0 +# define UINT64DATA(x) ((x).i) #endif /* GNUTLS_NUM_H */ diff --git a/lib/gnutls_record.c b/lib/gnutls_record.c index 66f85ca2a5..9c11d6d4fb 100644 --- a/lib/gnutls_record.c +++ b/lib/gnutls_record.c @@ -319,6 +319,38 @@ copy_record_version (gnutls_session_t session, } } +/* Increments the sequence value + */ +inline static int +sequence_increment (gnutls_session_t session, + uint64 * value) +{ + if (_gnutls_is_dtls(session)) + { + return _gnutls_uint48pp((uint48*)&value->i[2]); + } + else + { + return _gnutls_uint64pp(value); + } +} + +/* Read epoch and sequence from a DTLS packet */ +inline static void +sequence_read (uint64 * sequence, + opaque * data) +{ + memcpy(sequence->i, data, 8); +} + +/* Write epoch and sequence to a DTLS packet */ +inline static void +sequence_write (uint64 * sequence, + opaque * data) +{ + memcpy(data, sequence->i, 8); +} + /* This function behaves exactly like write(). The only difference is * that it accepts, the gnutls_session_t and the content_type_t of data to * send (if called by the user the Content is specific)