Skip to content

Commit 5056e8a

Browse files
committed
[crypto] Expose shifted out bit from big integer shifts
Expose the bit shifted out as a result of shifting a big integer left or right. Signed-off-by: Michael Brown <[email protected]>
1 parent bd90abf commit 5056e8a

File tree

7 files changed

+146
-85
lines changed

7 files changed

+146
-85
lines changed

src/arch/arm32/include/bits/bigint.h

+14-8
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,18 @@ bigint_subtract_raw ( const uint32_t *subtrahend0, uint32_t *value0,
123123
*
124124
* @v value0 Element 0 of big integer
125125
* @v size Number of elements
126+
* @ret out Bit shifted out
126127
*/
127-
static inline __attribute__ (( always_inline )) void
128+
static inline __attribute__ (( always_inline )) int
128129
bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
129130
bigint_t ( size ) __attribute__ (( may_alias )) *value =
130131
( ( void * ) value0 );
131132
uint32_t *discard_value;
132133
uint32_t *discard_end;
133134
uint32_t discard_value_i;
135+
int carry;
134136

135-
__asm__ __volatile__ ( "adds %1, %0, %5, lsl #2\n\t" /* clear CF */
137+
__asm__ __volatile__ ( "adds %1, %0, %1, lsl #2\n\t" /* clear CF */
136138
"\n1:\n\t"
137139
"ldr %2, [%0]\n\t"
138140
"adcs %2, %2\n\t"
@@ -142,26 +144,29 @@ bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
142144
: "=l" ( discard_value ),
143145
"=l" ( discard_end ),
144146
"=l" ( discard_value_i ),
147+
"=@cccs" ( carry ),
145148
"+m" ( *value )
146-
: "0" ( value0 ), "1" ( size )
147-
: "cc" );
149+
: "0" ( value0 ), "1" ( size ) );
150+
return carry;
148151
}
149152

150153
/**
151154
* Shift big integer right
152155
*
153156
* @v value0 Element 0 of big integer
154157
* @v size Number of elements
158+
* @ret out Bit shifted out
155159
*/
156-
static inline __attribute__ (( always_inline )) void
160+
static inline __attribute__ (( always_inline )) int
157161
bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
158162
bigint_t ( size ) __attribute__ (( may_alias )) *value =
159163
( ( void * ) value0 );
160164
uint32_t *discard_value;
161165
uint32_t *discard_end;
162166
uint32_t discard_value_i;
167+
int carry;
163168

164-
__asm__ __volatile__ ( "adds %1, %0, %5, lsl #2\n\t" /* clear CF */
169+
__asm__ __volatile__ ( "adds %1, %0, %1, lsl #2\n\t" /* clear CF */
165170
"\n1:\n\t"
166171
"ldmdb %1!, {%2}\n\t"
167172
"rrxs %2, %2\n\t"
@@ -171,9 +176,10 @@ bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
171176
: "=l" ( discard_value ),
172177
"=l" ( discard_end ),
173178
"=l" ( discard_value_i ),
179+
"=@cccs" ( carry ),
174180
"+m" ( *value )
175-
: "0" ( value0 ), "1" ( size )
176-
: "cc" );
181+
: "0" ( value0 ), "1" ( size ) );
182+
return carry;
177183
}
178184

179185
/**

src/arch/arm64/include/bits/bigint.h

+18-13
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ bigint_subtract_raw ( const uint64_t *subtrahend0, uint64_t *value0,
122122
*
123123
* @v value0 Element 0 of big integer
124124
* @v size Number of elements
125+
* @ret out Bit shifted out
125126
*/
126-
static inline __attribute__ (( always_inline )) void
127+
static inline __attribute__ (( always_inline )) int
127128
bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
128129
bigint_t ( size ) __attribute__ (( may_alias )) *value =
129130
( ( void * ) value0 );
130131
uint64_t *discard_value;
131132
uint64_t discard_value_i;
132133
unsigned int discard_size;
134+
int carry;
133135

134136
__asm__ __volatile__ ( "cmn xzr, xzr\n\t" /* clear CF */
135137
"\n1:\n\t"
@@ -141,40 +143,43 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
141143
: "=r" ( discard_value ),
142144
"=r" ( discard_size ),
143145
"=r" ( discard_value_i ),
146+
"=@cccs" ( carry ),
144147
"+m" ( *value )
145-
: "0" ( value0 ), "1" ( size )
146-
: "cc" );
148+
: "0" ( value0 ), "1" ( size ) );
149+
return carry;
147150
}
148151

149152
/**
150153
* Shift big integer right
151154
*
152155
* @v value0 Element 0 of big integer
153156
* @v size Number of elements
157+
* @ret out Bit shifted out
154158
*/
155-
static inline __attribute__ (( always_inline )) void
159+
static inline __attribute__ (( always_inline )) int
156160
bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
157161
bigint_t ( size ) __attribute__ (( may_alias )) *value =
158162
( ( void * ) value0 );
159163
uint64_t *discard_value;
160-
uint64_t discard_value_i;
161-
uint64_t discard_value_j;
164+
uint64_t discard_high;
162165
unsigned int discard_size;
166+
uint64_t low;
163167

164-
__asm__ __volatile__ ( "mov %3, #0\n\t"
168+
__asm__ __volatile__ ( "mov %2, #0\n\t"
165169
"\n1:\n\t"
166170
"sub %w1, %w1, #1\n\t"
167-
"ldr %2, [%0, %1, lsl #3]\n\t"
168-
"extr %3, %3, %2, #1\n\t"
169-
"str %3, [%0, %1, lsl #3]\n\t"
170-
"mov %3, %2\n\t"
171+
"ldr %3, [%0, %1, lsl #3]\n\t"
172+
"extr %2, %2, %3, #1\n\t"
173+
"str %2, [%0, %1, lsl #3]\n\t"
174+
"mov %2, %3\n\t"
171175
"cbnz %w1, 1b\n\t"
172176
: "=r" ( discard_value ),
173177
"=r" ( discard_size ),
174-
"=r" ( discard_value_i ),
175-
"=r" ( discard_value_j ),
178+
"=r" ( discard_high ),
179+
"=r" ( low ),
176180
"+m" ( *value )
177181
: "0" ( value0 ), "1" ( size ) );
182+
return ( low & 1 );
178183
}
179184

180185
/**

src/arch/loong64/include/bits/bigint.h

+20-16
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,27 @@ bigint_subtract_raw ( const uint64_t *subtrahend0, uint64_t *value0,
144144
*
145145
* @v value0 Element 0 of big integer
146146
* @v size Number of elements
147+
* @ret out Bit shifted out
147148
*/
148-
static inline __attribute__ (( always_inline )) void
149+
static inline __attribute__ (( always_inline )) int
149150
bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
150151
bigint_t ( size ) __attribute__ (( may_alias )) *value =
151152
( ( void * ) value0 );
152153
uint64_t *discard_value;
153154
uint64_t discard_value_i;
154-
uint64_t discard_carry;
155155
uint64_t discard_temp;
156156
unsigned int discard_size;
157+
uint64_t carry;
157158

158159
__asm__ __volatile__ ( "\n1:\n\t"
159160
/* Load value[i] */
160161
"ld.d %2, %0, 0\n\t"
161162
/* Shift left */
162163
"rotri.d %2, %2, 63\n\t"
163-
"andi %4, %2, 1\n\t"
164-
"xor %2, %2, %4\n\t"
165-
"or %2, %2, %3\n\t"
166-
"move %3, %4\n\t"
164+
"andi %3, %2, 1\n\t"
165+
"xor %2, %2, %3\n\t"
166+
"or %2, %2, %4\n\t"
167+
"move %4, %3\n\t"
167168
/* Store value[i] */
168169
"st.d %2, %0, 0\n\t"
169170
/* Loop */
@@ -173,37 +174,39 @@ bigint_shl_raw ( uint64_t *value0, unsigned int size ) {
173174
: "=r" ( discard_value ),
174175
"=r" ( discard_size ),
175176
"=r" ( discard_value_i ),
176-
"=r" ( discard_carry ),
177177
"=r" ( discard_temp ),
178+
"=r" ( carry ),
178179
"+m" ( *value )
179-
: "0" ( value0 ), "1" ( size ), "3" ( 0 )
180+
: "0" ( value0 ), "1" ( size ), "4" ( 0 )
180181
: "cc" );
182+
return ( carry & 1 );
181183
}
182184

183185
/**
184186
* Shift big integer right
185187
*
186188
* @v value0 Element 0 of big integer
187189
* @v size Number of elements
190+
* @ret out Bit shifted out
188191
*/
189-
static inline __attribute__ (( always_inline )) void
192+
static inline __attribute__ (( always_inline )) int
190193
bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
191194
bigint_t ( size ) __attribute__ (( may_alias )) *value =
192195
( ( void * ) value0 );
193196
uint64_t *discard_value;
194197
uint64_t discard_value_i;
195-
uint64_t discard_carry;
196198
uint64_t discard_temp;
197199
unsigned int discard_size;
200+
uint64_t carry;
198201

199202
__asm__ __volatile__ ( "\n1:\n\t"
200203
/* Load value[i] */
201204
"ld.d %2, %0, -8\n\t"
202205
/* Shift right */
203-
"andi %4, %2, 1\n\t"
204-
"xor %2, %2, %4\n\t"
205-
"or %2, %2, %3\n\t"
206-
"move %3, %4\n\t"
206+
"andi %3, %2, 1\n\t"
207+
"xor %2, %2, %3\n\t"
208+
"or %2, %2, %4\n\t"
209+
"move %4, %3\n\t"
207210
"rotri.d %2, %2, 1\n\t"
208211
/* Store value[i] */
209212
"st.d %2, %0, -8\n\t"
@@ -214,11 +217,12 @@ bigint_shr_raw ( uint64_t *value0, unsigned int size ) {
214217
: "=r" ( discard_value ),
215218
"=r" ( discard_size ),
216219
"=r" ( discard_value_i ),
217-
"=r" ( discard_carry ),
218220
"=r" ( discard_temp ),
221+
"=r" ( carry ),
219222
"+m" ( *value )
220-
: "0" ( value0 + size ), "1" ( size ), "3" ( 0 )
223+
: "0" ( value0 + size ), "1" ( size ), "4" ( 0 )
221224
: "cc" );
225+
return ( carry & 1 );
222226
}
223227

224228
/**

src/arch/riscv/include/bits/bigint.h

+20-16
Original file line numberDiff line numberDiff line change
@@ -143,77 +143,81 @@ bigint_subtract_raw ( const unsigned long *subtrahend0, unsigned long *value0,
143143
*
144144
* @v value0 Element 0 of big integer
145145
* @v size Number of elements
146+
* @ret out Bit shifted out
146147
*/
147-
static inline __attribute__ (( always_inline )) void
148+
static inline __attribute__ (( always_inline )) int
148149
bigint_shl_raw ( unsigned long *value0, unsigned int size ) {
149150
bigint_t ( size ) __attribute__ (( may_alias )) *value =
150151
( ( void * ) value0 );
151152
unsigned long *valueN = ( value0 + size );
152153
unsigned long *discard_value;
153154
unsigned long discard_value_i;
154-
unsigned long discard_carry;
155155
unsigned long discard_temp;
156+
unsigned long carry;
156157

157158
__asm__ __volatile__ ( "\n1:\n\t"
158159
/* Load value[i] */
159160
LOADN " %1, (%0)\n\t"
160161
/* Shift left */
161-
"slli %3, %1, 1\n\t"
162-
"or %3, %3, %2\n\t"
163-
"srli %2, %1, %7\n\t"
162+
"slli %2, %1, 1\n\t"
163+
"or %2, %2, %3\n\t"
164+
"srli %3, %1, %7\n\t"
164165
/* Store value[i] */
165-
STOREN " %3, (%0)\n\t"
166+
STOREN " %2, (%0)\n\t"
166167
/* Loop */
167168
"addi %0, %0, %6\n\t"
168169
"bne %0, %5, 1b\n\t"
169170
: "=&r" ( discard_value ),
170171
"=&r" ( discard_value_i ),
171-
"=&r" ( discard_carry ),
172172
"=&r" ( discard_temp ),
173+
"=&r" ( carry ),
173174
"+m" ( *value )
174175
: "r" ( valueN ),
175176
"i" ( sizeof ( unsigned long ) ),
176177
"i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ),
177-
"0" ( value0 ), "2" ( 0 ) );
178+
"0" ( value0 ), "3" ( 0 ) );
179+
return carry;
178180
}
179181

180182
/**
181183
* Shift big integer right
182184
*
183185
* @v value0 Element 0 of big integer
184186
* @v size Number of elements
187+
* @ret out Bit shifted out
185188
*/
186-
static inline __attribute__ (( always_inline )) void
189+
static inline __attribute__ (( always_inline )) int
187190
bigint_shr_raw ( unsigned long *value0, unsigned int size ) {
188191
bigint_t ( size ) __attribute__ (( may_alias )) *value =
189192
( ( void * ) value0 );
190193
unsigned long *valueN = ( value0 + size );
191194
unsigned long *discard_value;
192195
unsigned long discard_value_i;
193-
unsigned long discard_carry;
194196
unsigned long discard_temp;
197+
unsigned long carry;
195198

196199
__asm__ __volatile__ ( "\n1:\n\t"
197200
/* Load value[i] */
198201
LOADN " %1, %6(%0)\n\t"
199202
/* Shift right */
200-
"srli %3, %1, 1\n\t"
201-
"or %3, %3, %2\n\t"
202-
"slli %2, %1, %7\n\t"
203+
"srli %2, %1, 1\n\t"
204+
"or %2, %2, %3\n\t"
205+
"slli %3, %1, %7\n\t"
203206
/* Store value[i] */
204-
STOREN " %3, %6(%0)\n\t"
207+
STOREN " %2, %6(%0)\n\t"
205208
/* Loop */
206209
"addi %0, %0, %6\n\t"
207210
"bne %0, %5, 1b\n\t"
208211
: "=&r" ( discard_value ),
209212
"=&r" ( discard_value_i ),
210-
"=&r" ( discard_carry ),
211213
"=&r" ( discard_temp ),
214+
"=&r" ( carry ),
212215
"+m" ( *value )
213216
: "r" ( value0 ),
214217
"i" ( -( sizeof ( unsigned long ) ) ),
215218
"i" ( ( 8 * sizeof ( unsigned long ) - 1 ) ),
216-
"0" ( valueN ), "2" ( 0 ) );
219+
"0" ( valueN ), "3" ( 0 ) );
220+
return ( !! carry );
217221
}
218222

219223
/**

src/arch/x86/include/bits/bigint.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -116,42 +116,49 @@ bigint_subtract_raw ( const uint32_t *subtrahend0, uint32_t *value0,
116116
*
117117
* @v value0 Element 0 of big integer
118118
* @v size Number of elements
119+
* @ret out Bit shifted out
119120
*/
120-
static inline __attribute__ (( always_inline )) void
121+
static inline __attribute__ (( always_inline )) int
121122
bigint_shl_raw ( uint32_t *value0, unsigned int size ) {
122123
bigint_t ( size ) __attribute__ (( may_alias )) *value =
123124
( ( void * ) value0 );
124125
long index;
125126
long discard_c;
127+
int out;
126128

127129
__asm__ __volatile__ ( "xor %0, %0\n\t" /* Zero %0 and clear CF */
128130
"\n1:\n\t"
129-
"rcll $1, (%3,%0,4)\n\t"
131+
"rcll $1, (%4,%0,4)\n\t"
130132
"inc %0\n\t" /* Does not affect CF */
131133
"loop 1b\n\t"
132134
: "=&r" ( index ), "=&c" ( discard_c ),
133-
"+m" ( *value )
135+
"=@ccc" ( out ), "+m" ( *value )
134136
: "r" ( value0 ), "1" ( size ) );
137+
return out;
135138
}
136139

137140
/**
138141
* Shift big integer right
139142
*
140143
* @v value0 Element 0 of big integer
141144
* @v size Number of elements
145+
* @ret out Bit shifted out
142146
*/
143-
static inline __attribute__ (( always_inline )) void
147+
static inline __attribute__ (( always_inline )) int
144148
bigint_shr_raw ( uint32_t *value0, unsigned int size ) {
145149
bigint_t ( size ) __attribute__ (( may_alias )) *value =
146150
( ( void * ) value0 );
147151
long discard_c;
152+
int out;
148153

149154
__asm__ __volatile__ ( "clc\n\t"
150155
"\n1:\n\t"
151-
"rcrl $1, -4(%2,%0,4)\n\t"
156+
"rcrl $1, -4(%3,%0,4)\n\t"
152157
"loop 1b\n\t"
153-
: "=&c" ( discard_c ), "+m" ( *value )
158+
: "=&c" ( discard_c ), "=@ccc" ( out ),
159+
"+m" ( *value )
154160
: "r" ( value0 ), "0" ( size ) );
161+
return out;
155162
}
156163

157164
/**

0 commit comments

Comments
 (0)