OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 #ifdef FREEBL_NO_DEPEND | 5 #ifdef FREEBL_NO_DEPEND |
6 #include "stubs.h" | 6 #include "stubs.h" |
7 #endif | 7 #endif |
8 #include "prtypes.h" | 8 #include "prtypes.h" |
9 #include "blapit.h" | 9 #include "blapit.h" |
10 #include "blapii.h" | 10 #include "blapii.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 /* | 75 /* |
76 * Used by counter mode. Increment the counter block. Not all bits in the | 76 * Used by counter mode. Increment the counter block. Not all bits in the |
77 * counter block are part of the counter, counterBits tells how many bits | 77 * counter block are part of the counter, counterBits tells how many bits |
78 * are part of the counter. The counter block is blocksize long. It's a | 78 * are part of the counter. The counter block is blocksize long. It's a |
79 * big endian value. | 79 * big endian value. |
80 * | 80 * |
81 * XXX Does not handle counter rollover. | 81 * XXX Does not handle counter rollover. |
82 */ | 82 */ |
83 static void | 83 static void |
84 ctr_GetNextCtr(unsigned char *counter, unsigned int counterBits, | 84 ctr_GetNextCtr(unsigned char *counter, unsigned int counterBits, |
85 » » unsigned int blocksize) | 85 » unsigned int blocksize) |
86 { | 86 { |
87 unsigned char *counterPtr = counter + blocksize - 1; | 87 unsigned char *counterPtr = counter + blocksize - 1; |
88 unsigned char mask, count; | 88 unsigned char mask, count; |
89 | 89 |
90 PORT_Assert(counterBits <= blocksize*PR_BITS_PER_BYTE); | 90 PORT_Assert(counterBits <= blocksize*PR_BITS_PER_BYTE); |
91 while (counterBits >= PR_BITS_PER_BYTE) { | 91 while (counterBits >= PR_BITS_PER_BYTE) { |
92 if (++(*(counterPtr--))) { | 92 if (++(*(counterPtr--))) { |
93 return; | 93 return; |
94 } | 94 } |
95 counterBits -= PR_BITS_PER_BYTE; | 95 counterBits -= PR_BITS_PER_BYTE; |
96 } | 96 } |
97 if (counterBits == 0) { | 97 if (counterBits == 0) { |
98 return; | 98 return; |
99 } | 99 } |
100 /* increment the final partial byte */ | 100 /* increment the final partial byte */ |
101 mask = (1 << counterBits)-1; | 101 mask = (1 << counterBits)-1; |
102 count = ++(*counterPtr) & mask; | 102 count = ++(*counterPtr) & mask; |
103 *counterPtr = ((*counterPtr) & ~mask) | count; | 103 *counterPtr = ((*counterPtr) & ~mask) | count; |
104 return; | 104 return; |
105 } | 105 } |
106 | 106 |
107 static void | 107 static void |
108 ctr_xor(unsigned char *target, const unsigned char *x, | 108 ctr_xor(unsigned char *target, const unsigned char *x, |
109 » const unsigned char *y, unsigned int count) | 109 » const unsigned char *y, unsigned int count) |
110 { | 110 { |
111 unsigned int i; | 111 unsigned int i; |
112 for (i=0; i < count; i++) { | 112 for (i=0; i < count; i++) { |
113 *target++ = *x++ ^ *y++; | 113 *target++ = *x++ ^ *y++; |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 SECStatus | 117 SECStatus |
118 CTR_Update(CTRContext *ctr, unsigned char *outbuf, | 118 CTR_Update(CTRContext *ctr, unsigned char *outbuf, |
119 » » unsigned int *outlen, unsigned int maxout, | 119 » unsigned int *outlen, unsigned int maxout, |
120 » » const unsigned char *inbuf, unsigned int inlen, | 120 » const unsigned char *inbuf, unsigned int inlen, |
121 » » unsigned int blocksize) | 121 » unsigned int blocksize) |
122 { | 122 { |
123 unsigned int tmp; | 123 unsigned int tmp; |
124 SECStatus rv; | 124 SECStatus rv; |
125 | 125 |
126 if (maxout < inlen) { | 126 if (maxout < inlen) { |
127 *outlen = inlen; | 127 *outlen = inlen; |
128 PORT_SetError(SEC_ERROR_OUTPUT_LEN); | 128 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
129 return SECFailure; | 129 return SECFailure; |
130 } | 130 } |
131 *outlen = 0; | 131 *outlen = 0; |
132 if (ctr->bufPtr != blocksize) { | 132 if (ctr->bufPtr != blocksize) { |
133 unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen); | 133 unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen); |
134 » ctr_xor(outbuf, inbuf, ctr->buffer+ctr->bufPtr, needed); | 134 » ctr_xor(outbuf, inbuf, ctr->buffer + ctr->bufPtr, needed); |
135 ctr->bufPtr += needed; | 135 ctr->bufPtr += needed; |
136 outbuf += needed; | 136 outbuf += needed; |
137 inbuf += needed; | 137 inbuf += needed; |
138 *outlen += needed; | 138 *outlen += needed; |
139 inlen -= needed; | 139 inlen -= needed; |
140 if (inlen == 0) { | 140 if (inlen == 0) { |
141 return SECSuccess; | 141 return SECSuccess; |
142 } | 142 } |
143 PORT_Assert(ctr->bufPtr == blocksize); | 143 PORT_Assert(ctr->bufPtr == blocksize); |
144 } | 144 } |
145 » | 145 |
146 while (inlen >= blocksize) { | 146 while (inlen >= blocksize) { |
147 rv = (*ctr->cipher)(ctr->context, ctr->buffer, &tmp, blocksize, | 147 rv = (*ctr->cipher)(ctr->context, ctr->buffer, &tmp, blocksize, |
148 ctr->counter, blocksize, blocksize); | 148 ctr->counter, blocksize, blocksize); |
149 ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize); | 149 ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize); |
150 if (rv != SECSuccess) { | 150 if (rv != SECSuccess) { |
151 return SECFailure; | 151 return SECFailure; |
152 } | 152 } |
153 ctr_xor(outbuf, inbuf, ctr->buffer, blocksize); | 153 ctr_xor(outbuf, inbuf, ctr->buffer, blocksize); |
154 outbuf += blocksize; | 154 outbuf += blocksize; |
155 inbuf += blocksize; | 155 inbuf += blocksize; |
(...skipping 27 matching lines...) Expand all Loading... |
183 SECStatus rv; | 183 SECStatus rv; |
184 | 184 |
185 if (maxout < inlen) { | 185 if (maxout < inlen) { |
186 *outlen = inlen; | 186 *outlen = inlen; |
187 PORT_SetError(SEC_ERROR_OUTPUT_LEN); | 187 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
188 return SECFailure; | 188 return SECFailure; |
189 } | 189 } |
190 *outlen = 0; | 190 *outlen = 0; |
191 if (ctr->bufPtr != blocksize) { | 191 if (ctr->bufPtr != blocksize) { |
192 unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen); | 192 unsigned int needed = PR_MIN(blocksize-ctr->bufPtr, inlen); |
193 » ctr_xor(outbuf, inbuf, ctr->buffer+ctr->bufPtr, needed); | 193 » ctr_xor(outbuf, inbuf, ctr->buffer + ctr->bufPtr, needed); |
194 ctr->bufPtr += needed; | 194 ctr->bufPtr += needed; |
195 outbuf += needed; | 195 outbuf += needed; |
196 inbuf += needed; | 196 inbuf += needed; |
197 *outlen += needed; | 197 *outlen += needed; |
198 inlen -= needed; | 198 inlen -= needed; |
199 if (inlen == 0) { | 199 if (inlen == 0) { |
200 return SECSuccess; | 200 return SECSuccess; |
201 } | 201 } |
202 PORT_Assert(ctr->bufPtr == blocksize); | 202 PORT_Assert(ctr->bufPtr == blocksize); |
203 } | 203 } |
(...skipping 16 matching lines...) Expand all Loading... |
220 ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize); | 220 ctr_GetNextCtr(ctr->counter, ctr->counterBits, blocksize); |
221 if (rv != SECSuccess) { | 221 if (rv != SECSuccess) { |
222 return SECFailure; | 222 return SECFailure; |
223 } | 223 } |
224 ctr_xor(outbuf, inbuf, ctr->buffer, inlen); | 224 ctr_xor(outbuf, inbuf, ctr->buffer, inlen); |
225 ctr->bufPtr = inlen; | 225 ctr->bufPtr = inlen; |
226 *outlen += inlen; | 226 *outlen += inlen; |
227 return SECSuccess; | 227 return SECSuccess; |
228 } | 228 } |
229 #endif | 229 #endif |
OLD | NEW |