1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/math/gf2_8.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 1.4 +/* 1.5 + * gf2_8.c 1.6 + * 1.7 + * GF(256) finite field implementation, with the representation used 1.8 + * in the AES cipher. 1.9 + * 1.10 + * David A. McGrew 1.11 + * Cisco Systems, Inc. 1.12 + */ 1.13 + 1.14 +/* 1.15 + * 1.16 + * Copyright (c) 2001-2006, Cisco Systems, Inc. 1.17 + * All rights reserved. 1.18 + * 1.19 + * Redistribution and use in source and binary forms, with or without 1.20 + * modification, are permitted provided that the following conditions 1.21 + * are met: 1.22 + * 1.23 + * Redistributions of source code must retain the above copyright 1.24 + * notice, this list of conditions and the following disclaimer. 1.25 + * 1.26 + * Redistributions in binary form must reproduce the above 1.27 + * copyright notice, this list of conditions and the following 1.28 + * disclaimer in the documentation and/or other materials provided 1.29 + * with the distribution. 1.30 + * 1.31 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.32 + * contributors may be used to endorse or promote products derived 1.33 + * from this software without specific prior written permission. 1.34 + * 1.35 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.36 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.37 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.38 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.39 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.40 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.41 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.42 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.43 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.44 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.45 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.46 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.47 + * 1.48 + */ 1.49 + 1.50 + 1.51 +#include "datatypes.h" 1.52 +#include "gf2_8.h" 1.53 + 1.54 +/* gf2_8_shift() moved to gf2_8.h as an inline function */ 1.55 + 1.56 +gf2_8 1.57 +gf2_8_multiply(gf2_8 x, gf2_8 y) { 1.58 + gf2_8 z = 0; 1.59 + 1.60 + if (y & 1) z ^= x; x = gf2_8_shift(x); 1.61 + if (y & 2) z ^= x; x = gf2_8_shift(x); 1.62 + if (y & 4) z ^= x; x = gf2_8_shift(x); 1.63 + if (y & 8) z ^= x; x = gf2_8_shift(x); 1.64 + if (y & 16) z ^= x; x = gf2_8_shift(x); 1.65 + if (y & 32) z ^= x; x = gf2_8_shift(x); 1.66 + if (y & 64) z ^= x; x = gf2_8_shift(x); 1.67 + if (y & 128) z ^= x; 1.68 + 1.69 + return z; 1.70 +} 1.71 + 1.72 + 1.73 +/* this should use the euclidean algorithm */ 1.74 + 1.75 +gf2_8 1.76 +gf2_8_compute_inverse(gf2_8 x) { 1.77 + unsigned int i; 1.78 + 1.79 + if (x == 0) return 0; /* zero is a special case */ 1.80 + for (i=0; i < 256; i++) 1.81 + if (gf2_8_multiply((gf2_8) i, x) == 1) 1.82 + return (gf2_8) i; 1.83 + 1.84 + return 0; 1.85 +} 1.86 +