comparison libtomcrypt/src/encauth/gcm/gcm_process.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 11
12 /** 12 /**
13 @file gcm_process.c 13 @file gcm_process.c
14 GCM implementation, process message data, by Tom St Denis 14 GCM implementation, process message data, by Tom St Denis
29 int gcm_process(gcm_state *gcm, 29 int gcm_process(gcm_state *gcm,
30 unsigned char *pt, unsigned long ptlen, 30 unsigned char *pt, unsigned long ptlen,
31 unsigned char *ct, 31 unsigned char *ct,
32 int direction) 32 int direction)
33 { 33 {
34 unsigned long x, y; 34 unsigned long x;
35 int y, err;
35 unsigned char b; 36 unsigned char b;
36 int err;
37 37
38 LTC_ARGCHK(gcm != NULL); 38 LTC_ARGCHK(gcm != NULL);
39 if (ptlen > 0) { 39 if (ptlen > 0) {
40 LTC_ARGCHK(pt != NULL); 40 LTC_ARGCHK(pt != NULL);
41 LTC_ARGCHK(ct != NULL); 41 LTC_ARGCHK(ct != NULL);
57 gcm_mult_h(gcm, gcm->X); 57 gcm_mult_h(gcm, gcm->X);
58 } 58 }
59 59
60 /* increment counter */ 60 /* increment counter */
61 for (y = 15; y >= 12; y--) { 61 for (y = 15; y >= 12; y--) {
62 if (++gcm->Y[y]) { break; } 62 if (++gcm->Y[y] & 255) { break; }
63 } 63 }
64 /* encrypt the counter */ 64 /* encrypt the counter */
65 cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K); 65 if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
66 return err;
67 }
66 68
67 gcm->buflen = 0; 69 gcm->buflen = 0;
68 gcm->mode = GCM_MODE_TEXT; 70 gcm->mode = GCM_MODE_TEXT;
69 } 71 }
70 72
85 /* GMAC it */ 87 /* GMAC it */
86 gcm->pttotlen += 128; 88 gcm->pttotlen += 128;
87 gcm_mult_h(gcm, gcm->X); 89 gcm_mult_h(gcm, gcm->X);
88 /* increment counter */ 90 /* increment counter */
89 for (y = 15; y >= 12; y--) { 91 for (y = 15; y >= 12; y--) {
90 if (++gcm->Y[y]) { break; } 92 if (++gcm->Y[y] & 255) { break; }
91 } 93 }
92 cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K); 94 if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
95 return err;
96 }
93 } 97 }
94 } else { 98 } else {
95 for (x = 0; x < (ptlen & ~15); x += 16) { 99 for (x = 0; x < (ptlen & ~15); x += 16) {
96 /* ctr encrypt */ 100 /* ctr encrypt */
97 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) { 101 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
101 /* GMAC it */ 105 /* GMAC it */
102 gcm->pttotlen += 128; 106 gcm->pttotlen += 128;
103 gcm_mult_h(gcm, gcm->X); 107 gcm_mult_h(gcm, gcm->X);
104 /* increment counter */ 108 /* increment counter */
105 for (y = 15; y >= 12; y--) { 109 for (y = 15; y >= 12; y--) {
106 if (++gcm->Y[y]) { break; } 110 if (++gcm->Y[y] & 255) { break; }
107 } 111 }
108 cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K); 112 if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
113 return err;
114 }
109 } 115 }
110 } 116 }
111 } 117 }
112 #endif 118 #endif
113 119
117 gcm->pttotlen += 128; 123 gcm->pttotlen += 128;
118 gcm_mult_h(gcm, gcm->X); 124 gcm_mult_h(gcm, gcm->X);
119 125
120 /* increment counter */ 126 /* increment counter */
121 for (y = 15; y >= 12; y--) { 127 for (y = 15; y >= 12; y--) {
122 if (++gcm->Y[y]) { break; } 128 if (++gcm->Y[y] & 255) { break; }
123 } 129 }
124 cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K); 130 if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
131 return err;
132 }
125 gcm->buflen = 0; 133 gcm->buflen = 0;
126 } 134 }
127 135
128 if (direction == GCM_ENCRYPT) { 136 if (direction == GCM_ENCRYPT) {
129 b = ct[x] = pt[x] ^ gcm->buf[gcm->buflen]; 137 b = ct[x] = pt[x] ^ gcm->buf[gcm->buflen];
135 } 143 }
136 144
137 return CRYPT_OK; 145 return CRYPT_OK;
138 } 146 }
139 147
140
141
142 #endif 148 #endif
143
144 149
145 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/gcm/gcm_process.c,v $ */ 150 /* $Source: /cvs/libtom/libtomcrypt/src/encauth/gcm/gcm_process.c,v $ */
146 /* $Revision: 1.8 $ */ 151 /* $Revision: 1.14 $ */
147 /* $Date: 2005/05/05 14:35:58 $ */ 152 /* $Date: 2006/11/19 19:33:36 $ */