netwerk/srtp/src/crypto/kernel/key.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/srtp/src/crypto/kernel/key.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + * key.c
     1.6 + *
     1.7 + * key usage limits enforcement
     1.8 + * 
     1.9 + * David A. Mcgrew
    1.10 + * Cisco Systems, Inc.
    1.11 + */
    1.12 +/*
    1.13 + *	
    1.14 + * Copyright (c) 2001-2006 Cisco Systems, Inc.
    1.15 + * All rights reserved.
    1.16 + * 
    1.17 + * Redistribution and use in source and binary forms, with or without
    1.18 + * modification, are permitted provided that the following conditions
    1.19 + * are met:
    1.20 + * 
    1.21 + *   Redistributions of source code must retain the above copyright
    1.22 + *   notice, this list of conditions and the following disclaimer.
    1.23 + * 
    1.24 + *   Redistributions in binary form must reproduce the above
    1.25 + *   copyright notice, this list of conditions and the following
    1.26 + *   disclaimer in the documentation and/or other materials provided
    1.27 + *   with the distribution.
    1.28 + * 
    1.29 + *   Neither the name of the Cisco Systems, Inc. nor the names of its
    1.30 + *   contributors may be used to endorse or promote products derived
    1.31 + *   from this software without specific prior written permission.
    1.32 + * 
    1.33 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.34 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.35 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    1.36 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    1.37 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    1.38 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    1.39 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    1.40 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.41 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.42 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.43 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.44 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.45 + *
    1.46 + */
    1.47 +
    1.48 +#include "key.h"
    1.49 +
    1.50 +#define soft_limit 0x10000
    1.51 +
    1.52 +err_status_t
    1.53 +key_limit_set(key_limit_t key, const xtd_seq_num_t s) {
    1.54 +#ifdef NO_64BIT_MATH
    1.55 +  if (high32(s) == 0 && low32(s) < soft_limit)
    1.56 +    return err_status_bad_param;
    1.57 +#else
    1.58 +  if (s < soft_limit)
    1.59 +    return err_status_bad_param;
    1.60 +#endif
    1.61 +  key->num_left = s;
    1.62 +  key->state = key_state_normal;
    1.63 +  return err_status_ok;
    1.64 +}
    1.65 +
    1.66 +err_status_t
    1.67 +key_limit_clone(key_limit_t original, key_limit_t *new_key) {
    1.68 +  if (original == NULL)
    1.69 +    return err_status_bad_param;
    1.70 +  *new_key = original;
    1.71 +  return err_status_ok;
    1.72 +}
    1.73 +
    1.74 +err_status_t
    1.75 +key_limit_check(const key_limit_t key) {
    1.76 +  if (key->state == key_state_expired)
    1.77 +    return err_status_key_expired;
    1.78 +  return err_status_ok;
    1.79 +}
    1.80 +
    1.81 +key_event_t
    1.82 +key_limit_update(key_limit_t key) {
    1.83 +#ifdef NO_64BIT_MATH
    1.84 +  if (low32(key->num_left) == 0)
    1.85 +  {
    1.86 +	  // carry
    1.87 +	  key->num_left = make64(high32(key->num_left)-1,low32(key->num_left) - 1);
    1.88 +  }
    1.89 +  else
    1.90 +  {
    1.91 +	  // no carry
    1.92 +	  key->num_left = make64(high32(key->num_left),low32(key->num_left) - 1);
    1.93 +  }
    1.94 +  if (high32(key->num_left) != 0 || low32(key->num_left) >= soft_limit) {
    1.95 +    return key_event_normal;   /* we're above the soft limit */
    1.96 +  }
    1.97 +#else
    1.98 +  key->num_left--;
    1.99 +  if (key->num_left >= soft_limit) {
   1.100 +    return key_event_normal;   /* we're above the soft limit */
   1.101 +  }
   1.102 +#endif
   1.103 +  if (key->state == key_state_normal) {
   1.104 +    /* we just passed the soft limit, so change the state */
   1.105 +    key->state = key_state_past_soft_limit;
   1.106 +  }
   1.107 +#ifdef NO_64BIT_MATH
   1.108 +  if (low32(key->num_left) == 0 && high32(key->num_left == 0))
   1.109 +#else
   1.110 +  if (key->num_left < 1)
   1.111 +#endif
   1.112 +  { /* we just hit the hard limit */
   1.113 +    key->state = key_state_expired;
   1.114 +    return key_event_hard_limit;
   1.115 +  }
   1.116 +   return key_event_soft_limit;
   1.117 +}
   1.118 +

mercurial