forked from skair39/milagro-crypto-c
/
oct.c
428 lines (381 loc) · 8.26 KB
/
oct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
/*** Basic Octet string maintainance routines ***/
/* SU=m, m is Stack Usage */
#include <string.h>
#include "amcl.h"
/* Output an octet string (Debug Only) */
/* SU= 16 */
/* output octet */
void OCT_output(octet *w)
{
int i;
unsigned char ch;
for (i=0; i<w->len; i++)
{
ch=w->val[i];
printf("%02x",ch);
}
printf("\n");
}
/* SU= 16 */
void OCT_output_string(octet *w)
{
int i;
unsigned char ch;
for (i=0; i<w->len; i++)
{
ch=w->val[i];
printf("%c",ch);
}
/* printf("\n"); */
}
/* Convert C string to octet format - truncates if no room */
void OCT_jstring(octet *y,char *s)
{
int i,j;
i=y->len;
j=0;
while (s[j]!=0 && i<y->max)
{
y->val[i]=s[j];
y->len++;
i++;
j++;
}
}
/* compare 2 octet strings.
* If x==y return TRUE, else return FALSE */
/* SU= 8 */
int OCT_comp(octet *x,octet *y)
{
int i;
if (x->len>y->len) return 0;
if (x->len<y->len) return 0;
for (i=0; i<x->len; i++)
{
if (x->val[i]!=y->val[i]) return 0;
}
return 1;
}
/* check are first n bytes the same */
int OCT_ncomp(octet *x,octet *y,int n)
{
int i;
if (n>y->len || n>x->len) return 0;
for (i=0; i<n; i++)
{
if (x->val[i]!=y->val[i]) return 0;
}
return 1;
}
/* Shift octet to the left by n bytes. Leftmost bytes disappear */
void OCT_shl(octet *x,int n)
{
int i;
if (n>=x->len)
{
x->len=0;
return;
}
x->len-=n;
for (i=0; i<x->len; i++)
x->val[i]=x->val[i+n];
}
/* Append binary string to octet - truncates if no room */
/* SU= 12 */
void OCT_jbytes(octet *y,char *b,int len)
{
int i,j;
i=y->len;
for (j=0; j<len && i<y->max; j++)
{
y->val[i]=b[j];
y->len++;
i++;
}
}
/* Concatenates two octet strings */
/* SU= 8 */
void OCT_joctet(octet *y,octet *x)
{
/* y=y || x */
int i,j;
if (x==NULL) return;
for (i=0; i<x->len; i++)
{
j=y->len+i;
if (j>=y->max)
{
y->len=y->max;
return;
}
y->val[j]=x->val[i];
}
y->len+=x->len;
}
/* Append byte to octet rep times */
/* SU= 8 */
void OCT_jbyte(octet *y,int ch,int rep)
{
int i,j;
i=y->len;
for (j=0; j<rep && i<y->max; j++)
{
y->val[i]=ch;
y->len++;
i++;
}
}
/* XOR common bytes of x with y */
/* SU= 8 */
void OCT_xor(octet *y,octet *x)
{
/* xor first x->len bytes of y */
int i;
for (i=0; i<x->len && i<y->len; i++)
{
y->val[i]^=x->val[i];
}
}
/* clear an octet */
void OCT_empty(octet *w)
{
w->len=0;
}
/* Kill an octet string - Zeroise it for security */
void OCT_clear(octet *w)
{
int i;
for (i=0; i<w->max; i++) w->val[i]=0;
w->len=0;
}
/* appends int x of length len bytes to OCTET string */
/* SU= 8 */
void OCT_jint(octet *y,int x,int len)
{
int i,n;
n=y->len+len;
if (n>y->max || len<=0) return;
for (i=y->len; i<n; i++) y->val[i]=0;
y->len=n;
i=y->len;
while (x>0 && i>0)
{
i--;
y->val[i]=x%256;
x/=256;
}
}
/* Pad an octet to a given length */
/* SU= 8 */
int OCT_pad(octet *w,int n)
{
int i,d;
if (w->len>n || n>w->max) return 0;
if (n==w->len) return 1;
d=n-w->len;
for (i=n-1; i>=d; i--)
w->val[i]=w->val[i-d];
for (i=d-1; i>=0; i--)
w->val[i]=0;
w->len=n;
return 1;
}
/* Convert an octet string to base64 string */
/* SU= 56 */
void OCT_tobase64(char *b,octet *w)
{
int i,j,k,rem,last;
int c,ch[4];
unsigned char ptr[3];
rem=w->len%3;
j=k=0;
last=4;
while (j<w->len)
{
for (i=0; i<3; i++)
{
if (j<w->len) ptr[i]=w->val[j++];
else
{
ptr[i]=0;
last--;
}
}
ch[0]=(ptr[0]>>2)&0x3f;
ch[1]=((ptr[0]<<4)|(ptr[1]>>4))&0x3f;
ch[2]=((ptr[1]<<2)|(ptr[2]>>6))&0x3f;
ch[3]=ptr[2]&0x3f;
for (i=0; i<last; i++)
{
c=ch[i];
if (c<26) c+=65;
if (c>=26 && c<52) c+=71;
if (c>=52 && c<62) c-=4;
if (c==62) c='+';
if (c==63) c='/';
b[k++]=c;
}
}
if (rem>0) for (i=rem; i<3; i++) b[k++]='=';
b[k]='\0'; /* dangerous! */
}
/* SU= 56 */
void OCT_frombase64(octet *w,char *b)
{
int i,j,k,pads,len=(int)strlen(b);
int c,ch[4],ptr[3];
/* int lead=1; */
j=k=0;
while (j<len && k<w->max)
{
pads=0;
for (i=0; i<4; i++)
{
c=80+b[j++];
if (c<=112) continue; /* ignore white space */
if (c>144 && c<171) c-=145;
if (c>176 && c<203) c-=151;
if (c>127 && c<138) c-=76;
if (c==123) c=62;
if (c==127) c=63;
if (c==141)
{
pads++; /* ignore pads '=' */
continue;
}
ch[i]=c;
}
ptr[0]=(ch[0]<<2)|(ch[1]>>4);
ptr[1]=(ch[1]<<4)|(ch[2]>>2);
ptr[2]=(ch[2]<<6)|ch[3];
for (i=0; i<3-pads && k<w->max; i++)
{
/* don't put in leading zeros */
/* if (lead && ptr[i]==0) continue; */
w->val[k++]=ptr[i];
/* lead=0; */
}
}
w->len=k;
}
/* copy an octet string - truncates if no room */
/* SU= 16 */
void OCT_copy(octet *y,octet *x)
{
int i;
OCT_clear(y);
y->len=x->len;
if (y->len>y->max) y->len=y->max;
for (i=0; i<y->len; i++)
y->val[i]=x->val[i];
}
/* XOR m with all of x */
void OCT_xorbyte(octet *x,int m)
{
int i;
for (i=0; i<x->len; i++) x->val[i]^=m;
}
/* truncates x to n bytes and places the rest in y (if y is not NULL) */
/* SU= 8 */
void OCT_chop(octet *x,octet *y,int n)
{
int i;
if (n>=x->len)
{
if (y!=NULL) y->len=0;
return;
}
if (y!=NULL) y->len=x->len-n;
x->len=n;
if (y!=NULL)
{
for (i=0; i<y->len && i<y->max; i++) y->val[i]=x->val[i+n];
}
}
/* set x to len random bytes */
void OCT_rand(octet *x,csprng *RNG,int len)
{
int i;
if (len>x->max) len=x->max;
x->len=len;
for (i=0; i<len; i++) x->val[i]=RAND_byte(RNG);
}
/* Convert an octet to a hex string */
void OCT_toHex(octet *src,char *dst)
{
int i;
unsigned char ch;
for (i=0; i<src->len; i++)
{
ch=src->val[i];
sprintf(&dst[i*2],"%02x", ch);
}
}
static int char2int(char input)
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
return 0;
}
/* Convert from a hex string */
void OCT_fromHex(octet *dst,char *src)
{
int i=0;
int j=0;
OCT_clear(dst);
while(src[j]!=0)
{
dst->val[i++] = char2int(src[j])*16 + char2int(src[j+1]);
j += 2;
}
dst->len=i;
}
/* Convert an octet to a string */
void OCT_toStr(octet *src,char *dst)
{
int i;
unsigned char ch;
for (i=0; i<src->len; i++)
{
ch=src->val[i];
sprintf(&dst[i],"%c", ch);
}
}
/* Test program
#include <stdio.h>
#include "amcl.h"
char test[]="abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
int main()
{
char gm[100],gn[100],t[100];
octet m={0,sizeof(gm),gm};
octet n={0,sizeof(gn),gn};
OCT_jbytes(&m,test,strlen(test));
OCT_output(&m);
OCT_tobase64(t,&m);
printf(t); printf("\n");
OCT_frombase64(&n,t);
OCT_output(&n);
return 0;
}
*/