comparison src/encauth/gcm/gcm_gf_mult.c @ 280:59400faa4b44 libtomcrypt-orig libtomcrypt-1.05

Re-import libtomcrypt 1.05 for cleaner propagating. From crypt-1.05.tar.bz2, SHA1 of 88250202bb51570dc64f7e8f1c943cda9479258f
author Matt Johnston <matt@ucc.asn.au>
date Wed, 08 Mar 2006 12:58:00 +0000
parents
children d5faf4814ddb
comparison
equal deleted inserted replaced
-1:000000000000 280:59400faa4b44
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11
12 /**
13 @file gcm_gf_mult.c
14 GCM implementation, initialize state, by Tom St Denis
15 */
16 #include "tomcrypt.h"
17
18 #ifdef GCM_MODE
19
20 /* right shift */
21 static void gcm_rightshift(unsigned char *a)
22 {
23 int x;
24 for (x = 15; x > 0; x--) {
25 a[x] = (a[x]>>1) | ((a[x-1]<<7)&0x80);
26 }
27 a[0] >>= 1;
28 }
29
30 /* c = b*a */
31 static const unsigned char mask[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
32 static const unsigned char poly[] = { 0x00, 0xE1 };
33
34
35 /**
36 GCM GF multiplier (internal use only)
37 @param a First value
38 @param b Second value
39 @param c Destination for a * b
40 */
41 void gcm_gf_mult(const unsigned char *a, const unsigned char *b, unsigned char *c)
42 {
43 unsigned char Z[16], V[16];
44 unsigned x, y, z;
45
46 zeromem(Z, 16);
47 XMEMCPY(V, a, 16);
48 for (x = 0; x < 128; x++) {
49 if (b[x>>3] & mask[x&7]) {
50 for (y = 0; y < 16; y++) {
51 Z[y] ^= V[y];
52 }
53 }
54 z = V[15] & 0x01;
55 gcm_rightshift(V);
56 V[0] ^= poly[z];
57 }
58 XMEMCPY(c, Z, 16);
59 }
60
61 /**
62 GCM multiply by H
63 @param gcm The GCM state which holds the H value
64 @param I The value to multiply H by
65 */
66 void gcm_mult_h(gcm_state *gcm, unsigned char *I)
67 {
68 unsigned char T[16];
69 #ifdef GCM_TABLES
70 int x, y;
71 XMEMCPY(T, &gcm->PC[0][I[0]][0], 16);
72 for (x = 1; x < 16; x++) {
73 #ifdef LTC_FAST
74 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
75 *((LTC_FAST_TYPE *)(T + y)) ^= *((LTC_FAST_TYPE *)(&gcm->PC[x][I[x]][y]));
76 }
77 #else
78 for (y = 0; y < 16; y++) {
79 T[y] ^= gcm->PC[x][I[x]][y];
80 }
81 #endif
82 }
83 #else
84 gcm_gf_mult(gcm->H, I, T);
85 #endif
86 XMEMCPY(I, T, 16);
87 }
88
89
90 #endif
91
92 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/gcm/gcm_gf_mult.c,v $ */
93 /* $Revision: 1.16 $ */
94 /* $Date: 2005/05/21 14:33:42 $ */