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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
|
/*
* Copyright (C) 2025 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
FILE_SECBOOT ( PERMITTED );
/** @file
*
* Elliptic curve digital signature algorithm (ECDSA)
*
* The elliptic curve public key format is documented in RFC 5480.
* The original private key format is documented in RFC 5915, and the
* generic container PKCS#8 format documented in RFC 5208.
*
*/
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ipxe/crypto.h>
#include <ipxe/bigint.h>
#include <ipxe/hmac_drbg.h>
#include <ipxe/ecdsa.h>
/* Disambiguate the various error causes */
#define EINVAL_POINTSIZE \
__einfo_error ( EINFO_EINVAL_POINTSIZE )
#define EINFO_EINVAL_POINTSIZE \
__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid point size" )
#define EINVAL_KEYSIZE \
__einfo_error ( EINFO_EINVAL_KEYSIZE )
#define EINFO_EINVAL_KEYSIZE \
__einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid key size" )
#define EINVAL_COMPRESSION \
__einfo_error ( EINFO_EINVAL_COMPRESSION )
#define EINFO_EINVAL_COMPRESSION \
__einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid compression")
#define EINVAL_INFINITY \
__einfo_error ( EINFO_EINVAL_INFINITY )
#define EINFO_EINVAL_INFINITY \
__einfo_uniqify ( EINFO_EINVAL, 0x04, "Point is infinity" )
#define EINVAL_SIGNATURE \
__einfo_error ( EINFO_EINVAL_SIGNATURE )
#define EINFO_EINVAL_SIGNATURE \
__einfo_uniqify ( EINFO_EINVAL, 0x05, "Invalid signature" )
/** "ecPublicKey" object identifier */
static uint8_t oid_ecpublickey[] = { ASN1_OID_ECPUBLICKEY };
/** Generic elliptic curve container algorithm
*
* The actual curve to be used is identified via the algorithm
* parameters, rather than the top-level OID.
*/
struct asn1_algorithm ecpubkey_algorithm __asn1_algorithm = {
.name = "ecPublicKey",
.oid = ASN1_CURSOR ( oid_ecpublickey ),
.pubkey = &ecdsa_algorithm,
};
/** An ECDSA key */
struct ecdsa_key {
/** Elliptic curve */
struct elliptic_curve *curve;
/** Public curve point */
const void *public;
/** Private multiple of base curve point (if applicable) */
const void *private;
};
/** ECDSA context */
struct ecdsa_context {
/** Key */
struct ecdsa_key key;
/** Big integer size */
unsigned int size;
/** Digest algorithm */
struct digest_algorithm *digest;
/** Digest length */
size_t zlen;
/** Dynamically allocated storage */
void *dynamic;
/** Element 0 of modulus N (i.e. curve group order */
bigint_element_t *modulus0;
/** Element 0 of constant N-2 (for Fermat's little theorem) */
bigint_element_t *fermat0;
/** Element 0 of Montgomery constant R^2 mod N */
bigint_element_t *square0;
/** Element 0 of constant 1 (in Montgomery form) */
bigint_element_t *one0;
/** Element 0 of digest value "z" */
bigint_element_t *z0;
/** Element 0 of random key "k" */
bigint_element_t *k0;
/** Element 0 of signature value "r" */
bigint_element_t *r0;
/** Element 0 of signature value "s" */
bigint_element_t *s0;
/** Element 0 of temporary value */
bigint_element_t *temp0;
/** Element 0 of product buffer */
bigint_element_t *product0;
/** Curve point 1 */
void *point1;
/** Curve point 2 */
void *point2;
/** Scalar value */
void *scalar;
/** HMAC_DRBG state for random value generation */
struct hmac_drbg_state *drbg;
};
/**
* Parse ECDSA key
*
* @v key ECDSA key
* @v raw ASN.1 cursor
* @ret rc Return status code
*/
static int ecdsa_parse_key ( struct ecdsa_key *key,
const struct asn1_cursor *raw ) {
struct asn1_algorithm *algorithm;
struct asn1_cursor cursor;
struct asn1_cursor curve;
struct asn1_cursor private;
const uint8_t *compression;
int is_private;
int rc;
/* Enter subjectPublicKeyInfo/ECPrivateKey */
memcpy ( &cursor, raw, sizeof ( cursor ) );
asn1_enter ( &cursor, ASN1_SEQUENCE );
asn1_invalidate_cursor ( &curve );
asn1_invalidate_cursor ( &private );
/* Determine key format */
if ( asn1_type ( &cursor ) == ASN1_INTEGER ) {
/* Private key */
is_private = 1;
/* Skip version */
asn1_skip_any ( &cursor );
/* Parse privateKeyAlgorithm, if present */
if ( asn1_type ( &cursor ) == ASN1_SEQUENCE ) {
/* PKCS#8 format */
DBGC ( key, "ECDSA %p is in PKCS#8 format\n", key );
/* Parse privateKeyAlgorithm */
memcpy ( &curve, &cursor, sizeof ( curve ) );
asn1_skip_any ( &cursor );
/* Enter privateKey */
asn1_enter ( &cursor, ASN1_OCTET_STRING );
/* Enter ECPrivateKey */
asn1_enter ( &cursor, ASN1_SEQUENCE );
/* Skip version */
asn1_skip ( &cursor, ASN1_INTEGER );
}
/* Parse privateKey */
memcpy ( &private, &cursor, sizeof ( private ) );
asn1_enter ( &private, ASN1_OCTET_STRING );
asn1_skip_any ( &cursor );
/* Parse parameters, if present */
if ( asn1_type ( &cursor ) == ASN1_EXPLICIT_TAG ( 0 ) ) {
memcpy ( &curve, &cursor, sizeof ( curve ) );
asn1_enter_any ( &curve );
asn1_skip_any ( &cursor );
}
/* Enter publicKey */
asn1_enter ( &cursor, ASN1_EXPLICIT_TAG ( 1 ) );
} else {
/* Public key */
is_private = 0;
/* Parse algorithm */
memcpy ( &curve, &cursor, sizeof ( curve ) );
asn1_skip_any ( &cursor );
}
/* Enter publicKey */
asn1_enter_bits ( &cursor, NULL );
/* Identify curve */
if ( ( rc = asn1_curve_algorithm ( &curve, &ecpubkey_algorithm,
&algorithm ) ) != 0 ) {
DBGC ( key, "ECDSA %p unknown curve: %s\n",
key, strerror ( rc ) );
DBGC_HDA ( key, 0, raw->data, raw->len );
return rc;
}
key->curve = algorithm->curve;
DBGC ( key, "ECDSA %p is a %s (%s) %s key\n", key, algorithm->name,
key->curve->name, ( is_private ? "private" : "public" ) );
/* Check public key length */
if ( cursor.len != ( sizeof ( *compression ) +
key->curve->pointsize ) ) {
DBGC ( key, "ECDSA %p invalid public key length %zd\n",
key, cursor.len );
DBGC_HDA ( key, 0, raw->data, raw->len );
return -EINVAL_POINTSIZE;
}
/* Check that key is uncompressed */
compression = cursor.data;
if ( *compression != ECDSA_UNCOMPRESSED ) {
DBGC ( key, "ECDSA %p invalid compression %#02x\n",
key, *compression );
DBGC_HDA ( key, 0, raw->data, raw->len );
return -EINVAL_COMPRESSION;
}
/* Extract public curve point */
key->public = ( cursor.data + sizeof ( *compression ) );
DBGC ( key, "ECDSA %p public curve point:\n", key );
DBGC_HDA ( key, 0, key->public, key->curve->pointsize );
/* Check that public key is not the point at infinity */
if ( elliptic_is_infinity ( key->curve, key->public ) ) {
DBGC ( key, "ECDSA %p public curve point is infinity\n", key );
return -EINVAL_INFINITY;
}
/* Extract private key, if applicable */
if ( is_private ) {
/* Check private key length */
if ( private.len != key->curve->keysize ) {
DBGC ( key, "ECDSA %p invalid private key length "
"%zd\n", key, private.len );
DBGC_HDA ( key, 0, raw->data, raw->len );
return -EINVAL_KEYSIZE;
}
/* Extract private key */
key->private = private.data;
DBGC ( key, "ECDSA %p private multiplier:\n", key );
DBGC_HDA ( key, 0, key->private, key->curve->keysize );
} else {
/* No private key */
key->private = NULL;
}
return 0;
}
/**
* Parse ECDSA signature value
*
* @v ctx ECDSA context
* @v rs0 Element 0 of signature "r" or "s" value
* @v raw ASN.1 cursor
* @ret rc Return status code
*/
static int ecdsa_parse_signature ( struct ecdsa_context *ctx,
bigint_element_t *rs0,
const struct asn1_cursor *raw ) {
size_t keysize = ctx->key.curve->keysize;
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
( ( void * ) ctx->modulus0 );
bigint_t ( size ) __attribute__ (( may_alias )) *rs =
( ( void * ) rs0 );
struct asn1_cursor cursor;
int rc;
/* Enter integer */
memcpy ( &cursor, raw, sizeof ( cursor ) );
if ( ( rc = asn1_enter_unsigned ( &cursor ) ) != 0 ) {
DBGC ( ctx, "ECDSA %p invalid integer:\n", ctx );
DBGC_HDA ( ctx, 0, raw->data, raw->len );
return rc;
}
/* Extract value */
if ( cursor.len > keysize ) {
DBGC ( ctx, "ECDSA %p invalid signature value:\n", ctx );
DBGC_HDA ( ctx, 0, raw->data, raw->len );
return -EINVAL_KEYSIZE;
}
bigint_init ( rs, cursor.data, cursor.len );
/* Check that value is within the required range */
if ( bigint_is_zero ( rs ) || bigint_is_geq ( rs, modulus ) ) {
DBGC ( ctx, "ECDSA %p out-of-range signature value:\n", ctx );
DBGC_HDA ( ctx, 0, raw->data, raw->len );
return -ERANGE;
}
return 0;
}
/**
* Prepend ECDSA signature value
*
* @v ctx ECDSA context
* @v rs0 Element 0 of signature "r" or "s" value
* @v builder ASN.1 builder
* @ret rc Return status code
*/
static int ecdsa_prepend_signature ( struct ecdsa_context *ctx,
bigint_element_t *rs0,
struct asn1_builder *builder ) {
size_t keysize = ctx->key.curve->keysize;
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *rs =
( ( void * ) rs0 );
uint8_t buf[ 1 /* potential sign byte */ + keysize ];
uint8_t *data;
size_t len;
int rc;
/* Construct value */
buf[0] = 0;
bigint_done ( rs, &buf[1], keysize );
/* Strip leading zeros */
data = buf;
len = sizeof ( buf );
while ( ( len > 1 ) && ( data[0] == 0 ) && ( data[1] < 0x80 ) ) {
data++;
len--;
}
/* Prepend integer */
if ( ( rc = asn1_prepend ( builder, ASN1_INTEGER, data, len ) ) != 0 )
return rc;
return 0;
}
/**
* Allocate ECDSA context dynamic storage
*
* @v ctx ECDSA context
* @ret rc Return status code
*/
static int ecdsa_alloc ( struct ecdsa_context *ctx ) {
struct elliptic_curve *curve = ctx->key.curve;
size_t pointsize = curve->pointsize;
size_t keysize = curve->keysize;
unsigned int size =
bigint_required_size ( keysize + 1 /* for addition */ );
struct {
bigint_t ( size ) modulus;
bigint_t ( size ) fermat;
bigint_t ( size ) square;
bigint_t ( size ) one;
bigint_t ( size ) z;
bigint_t ( size ) k;
bigint_t ( size ) r;
bigint_t ( size ) s;
bigint_t ( size ) temp;
bigint_t ( size * 2 ) product;
uint8_t point1[pointsize];
uint8_t point2[pointsize];
uint8_t scalar[keysize];
struct hmac_drbg_state drbg;
} *dynamic;
/* Allocate dynamic storage */
dynamic = malloc ( sizeof ( *dynamic ) );
if ( ! dynamic )
return -ENOMEM;
/* Populate context */
ctx->size = size;
ctx->dynamic = dynamic;
ctx->modulus0 = dynamic->modulus.element;
ctx->fermat0 = dynamic->fermat.element;
ctx->square0 = dynamic->square.element;
ctx->one0 = dynamic->one.element;
ctx->z0 = dynamic->z.element;
ctx->k0 = dynamic->k.element;
ctx->r0 = dynamic->r.element;
ctx->s0 = dynamic->s.element;
ctx->temp0 = dynamic->temp.element;
ctx->product0 = dynamic->product.element;
ctx->point1 = dynamic->point1;
ctx->point2 = dynamic->point2;
ctx->scalar = dynamic->scalar;
ctx->drbg = &dynamic->drbg;
return 0;
}
/**
* Free ECDSA context dynamic storage
*
* @v ctx ECDSA context
*/
static void ecdsa_free ( struct ecdsa_context *ctx ) {
/* Free dynamic storage */
free ( ctx->dynamic );
}
/**
* Initialise ECDSA values
*
* @v ctx ECDSA context
* @v digest Digest algorithm
* @v value Digest value
*/
static void ecdsa_init_values ( struct ecdsa_context *ctx,
struct digest_algorithm *digest,
const void *value ) {
struct elliptic_curve *curve = ctx->key.curve;
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
( ( void * ) ctx->modulus0 );
bigint_t ( size ) __attribute__ (( may_alias )) *fermat =
( ( void * ) ctx->fermat0 );
bigint_t ( size ) __attribute__ (( may_alias )) *square =
( ( void * ) ctx->square0 );
bigint_t ( size ) __attribute__ (( may_alias )) *one =
( ( void * ) ctx->one0 );
bigint_t ( size ) __attribute__ (( may_alias )) *z =
( ( void * ) ctx->z0 );
bigint_t ( size * 2 ) __attribute__ (( may_alias )) *product =
( ( void * ) ctx->product0 );
static const uint8_t two_raw[] = { 2 };
size_t zlen;
/* Initialise modulus N */
bigint_init ( modulus, curve->order, curve->keysize );
DBGC2 ( ctx, "ECDSA %p N = %s\n", ctx, bigint_ntoa ( modulus ) );
/* Calculate N-2 (using Montgomery constant as temporary buffer) */
bigint_copy ( modulus, fermat );
bigint_init ( square, two_raw, sizeof ( two_raw ) );
bigint_subtract ( square, fermat );
/* Calculate Montgomery constant */
bigint_reduce ( modulus, square );
DBGC2 ( ctx, "ECDSA %p R^2 = %s mod N\n",
ctx, bigint_ntoa ( square ) );
/* Construct one in Montgomery form */
bigint_grow ( square, product );
bigint_montgomery ( modulus, product, one );
DBGC2 ( ctx, "ECDSA %p R = %s mod N\n",
ctx, bigint_ntoa ( one ) );
/* Initialise digest */
ctx->digest = digest;
zlen = ctx->key.curve->keysize;
if ( zlen > digest->digestsize )
zlen = digest->digestsize;
ctx->zlen = zlen;
bigint_init ( z, value, zlen );
DBGC2 ( ctx, "ECDSA %p z = %s (%s)\n",
ctx, bigint_ntoa ( z ), digest->name );
}
/**
* Initialise ECDSA context
*
* @v ctx ECDSA context
* @v key Key
* @v digest Digest algorithm
* @v value Digest value
* @ret rc Return status code
*/
static int ecdsa_init ( struct ecdsa_context *ctx,
const struct asn1_cursor *key,
struct digest_algorithm *digest,
const void *value ) {
int rc;
/* Parse key */
if ( ( rc = ecdsa_parse_key ( &ctx->key, key ) ) != 0 )
goto err_parse;
/* Allocate dynamic storage */
if ( ( rc = ecdsa_alloc ( ctx ) ) != 0 )
goto err_alloc;
/* Initialise values */
ecdsa_init_values ( ctx, digest, value );
return 0;
ecdsa_free ( ctx );
err_alloc:
err_parse:
return rc;
}
/**
* Invert ECDSA value
*
* @v ctx ECDSA context
* @v val0 Element 0 of value to invert
*/
static void ecdsa_invert ( struct ecdsa_context *ctx,
bigint_element_t *val0 ) {
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
( ( void * ) ctx->modulus0 );
bigint_t ( size ) __attribute__ (( may_alias )) *fermat =
( ( void * ) ctx->fermat0 );
bigint_t ( size ) __attribute__ (( may_alias )) *square =
( ( void * ) ctx->square0 );
bigint_t ( size ) __attribute__ (( may_alias )) *one =
( ( void * ) ctx->one0 );
bigint_t ( size ) __attribute__ (( may_alias )) *temp =
( ( void * ) ctx->temp0 );
bigint_t ( size * 2 ) __attribute__ (( may_alias )) *product =
( ( void * ) ctx->product0 );
bigint_t ( size ) __attribute__ (( may_alias )) *val =
( ( void * ) val0 );
/* Convert value to Montgomery form */
bigint_multiply ( val, square, product );
bigint_montgomery ( modulus, product, temp );
/* Invert value via Fermat's little theorem */
bigint_copy ( one, val );
bigint_ladder ( val, temp, fermat, bigint_mod_exp_ladder, modulus,
product );
}
/**
* Generate ECDSA "r" and "s" values
*
* @v ctx ECDSA context
* @v sig Signature
* @ret rc Return status code
*/
static int ecdsa_sign_rs ( struct ecdsa_context *ctx ) {
struct digest_algorithm *digest = ctx->digest;
struct elliptic_curve *curve = ctx->key.curve;
size_t pointsize = curve->pointsize;
size_t keysize = curve->keysize;
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
( ( void * ) ctx->modulus0 );
bigint_t ( size ) __attribute__ (( may_alias )) *square =
( ( void * ) ctx->square0 );
bigint_t ( size ) __attribute__ (( may_alias )) *one =
( ( void * ) ctx->one0 );
bigint_t ( size ) __attribute__ (( may_alias )) *z =
( ( void * ) ctx->z0 );
bigint_t ( size ) __attribute__ (( may_alias )) *k =
( ( void * ) ctx->k0 );
bigint_t ( size ) __attribute__ (( may_alias )) *r =
( ( void * ) ctx->r0 );
bigint_t ( size ) __attribute__ (( may_alias )) *s =
( ( void * ) ctx->s0 );
bigint_t ( size ) __attribute__ (( may_alias )) *temp =
( ( void * ) ctx->temp0 );
bigint_t ( size * 2 ) __attribute__ (( may_alias )) *product =
( ( void * ) ctx->product0 );
bigint_t ( size ) __attribute__ (( may_alias )) *x1 =
( ( void * ) temp );
void *point1 = ctx->point1;
void *scalar = ctx->scalar;
int rc;
/* Loop until a suitable signature is generated */
while ( 1 ) {
/* Generate pseudo-random data */
if ( ( rc = hmac_drbg_generate ( digest, ctx->drbg, NULL, 0,
scalar, keysize ) ) != 0 ) {
DBGC ( ctx, "ECDSA %p could not generate: %s\n",
ctx, strerror ( rc ) );
return rc;
}
/* Check suitability of pseudo-random data */
bigint_init ( k, scalar, keysize );
DBGC2 ( ctx, "ECDSA %p k = %s\n",
ctx, bigint_ntoa ( k ) );
if ( bigint_is_zero ( k ) )
continue;
if ( bigint_is_geq ( k, modulus ) )
continue;
/* Calculate (x1,y1) = k*G */
elliptic_multiply ( curve, curve->base, scalar, point1 );
bigint_init ( x1, point1, ( pointsize / 2 ) );
DBGC2 ( ctx, "ECDSA %p x1 = %s mod N\n",
ctx, bigint_ntoa ( x1 ) );
/* Calculate r = x1 mod N */
bigint_multiply ( x1, one, product );
bigint_montgomery ( modulus, product, r );
DBGC2 ( ctx, "ECDSA %p r = %s\n",
ctx, bigint_ntoa ( r ) );
/* Check suitability of r */
if ( bigint_is_zero ( r ) )
continue;
/* Calculate k^-1 mod N (in Montgomery form) */
ecdsa_invert ( ctx, k->element );
DBGC2 ( ctx, "ECDSA %p (k^-1)R = %s mod N\n",
ctx, bigint_ntoa ( k ) );
/* Calculate r * dA */
bigint_init ( temp, ctx->key.private, keysize );
DBGC2 ( ctx, "ECDSA %p dA = %s\n",
ctx, bigint_ntoa ( temp ) );
bigint_multiply ( r, temp, product );
bigint_montgomery ( modulus, product, temp );
bigint_multiply ( temp, square, product );
bigint_montgomery ( modulus, product, temp );
DBGC2 ( ctx, "ECDSA %p r*dA = %s mod N\n",
ctx, bigint_ntoa ( temp ) );
/* Calculate k^-1 * (z + r*dA) */
bigint_add ( z, temp );
DBGC2 ( ctx, "ECDSA %p z+r*dA = %s mod N\n",
ctx, bigint_ntoa ( temp ) );
bigint_multiply ( k, temp, product );
bigint_montgomery ( modulus, product, s );
DBGC2 ( ctx, "ECDSA %p s = %s\n",
ctx, bigint_ntoa ( s ) );
/* Check suitability of s */
if ( bigint_is_zero ( s ) )
continue;
return 0;
}
}
/**
* Verify ECDSA "r" and "s" values
*
* @v ctx ECDSA context
* @v sig Signature
* @ret rc Return status code
*/
static int ecdsa_verify_rs ( struct ecdsa_context *ctx ) {
struct elliptic_curve *curve = ctx->key.curve;
size_t pointsize = curve->pointsize;
size_t keysize = curve->keysize;
const void *public = ctx->key.public;
unsigned int size = ctx->size;
bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
( ( void * ) ctx->modulus0 );
bigint_t ( size ) __attribute__ (( may_alias )) *one =
( ( void * ) ctx->one0 );
bigint_t ( size ) __attribute__ (( may_alias )) *z =
( ( void * ) ctx->z0 );
bigint_t ( size ) __attribute__ (( may_alias )) *r =
( ( void * ) ctx->r0 );
bigint_t ( size ) __attribute__ (( may_alias )) *s =
( ( void * ) ctx->s0 );
bigint_t ( size ) __attribute__ (( may_alias )) *temp =
( ( void * ) ctx->temp0 );
bigint_t ( size * 2 ) __attribute__ (( may_alias )) *product =
( ( void * ) ctx->product0 );
bigint_t ( size ) __attribute__ (( may_alias )) *u1 =
( ( void * ) temp );
bigint_t ( size ) __attribute__ (( may_alias )) *u2 =
( ( void * ) temp );
bigint_t ( size ) __attribute__ (( may_alias )) *x1 =
( ( void * ) temp );
void *point1 = ctx->point1;
void *point2 = ctx->point2;
void *scalar = ctx->scalar;
int valid;
int rc;
DBGC2 ( ctx, "ECDSA %p r = %s\n", ctx, bigint_ntoa ( r ) );
DBGC2 ( ctx, "ECDSA %p s = %s\n", ctx, bigint_ntoa ( s ) );
/* Calculate s^-1 mod N (in Montgomery form) */
ecdsa_invert ( ctx, s->element );
DBGC2 ( ctx, "ECDSA %p (s^-1)R = %s mod N\n", ctx, bigint_ntoa ( s ) );
/* Calculate u1 = (z * s^-1) mod N */
bigint_multiply ( z, s, product );
bigint_montgomery ( modulus, product, u1 );
DBGC2 ( ctx, "ECDSA %p u1 = %s mod N\n",
ctx, bigint_ntoa ( u1 ) );
bigint_done ( u1, scalar, keysize );
/* Calculate u1 * G */
if ( ( rc = elliptic_multiply ( curve, curve->base, scalar,
point1 ) ) != 0 ) {
DBGC ( ctx, "ECDSA %p could not calculate u1*G: %s\n",
ctx, strerror ( rc ) );
return rc;
}
/* Calculate u2 = (r * s^-1) mod N */
bigint_multiply ( r, s, product );
bigint_montgomery ( modulus, product, u2 );
bigint_done ( u2, scalar, keysize );
DBGC2 ( ctx, "ECDSA %p u2 = %s mod N\n",
ctx, bigint_ntoa ( u2 ) );
/* Calculate u2 * Qa */
if ( ( rc = elliptic_multiply ( curve, public, scalar,
point2 ) ) != 0 ) {
DBGC ( ctx, "ECDSA %p could not calculate u2*Qa: %s\n",
ctx, strerror ( rc ) );
return rc;
}
/* Calculate u1 * G + u2 * Qa */
if ( ( rc = elliptic_add ( curve, point1, point2, point1 ) ) != 0 ) {
DBGC ( ctx, "ECDSA %p could not calculate u1*G+u2*Qa: %s\n",
ctx, strerror ( rc ) );
return rc;
}
/* Check that result is not the point at infinity */
if ( elliptic_is_infinity ( curve, point1 ) ) {
DBGC ( ctx, "ECDSA %p result is point at infinity\n", ctx );
return -EINVAL;
}
/* Calculate x1 mod N */
bigint_init ( x1, point1, ( pointsize / 2 ) );
DBGC2 ( ctx, "ECDSA %p x1 = %s mod N\n", ctx, bigint_ntoa ( x1 ) );
bigint_multiply ( x1, one, product );
bigint_montgomery ( modulus, product, x1 );
DBGC2 ( ctx, "ECDSA %p x1 = %s\n", ctx, bigint_ntoa ( x1 ) );
/* Check signature */
bigint_subtract ( x1, r );
valid = bigint_is_zero ( r );
DBGC2 ( ctx, "ECDSA %p signature is%s valid\n",
ctx, ( valid ? "" : " not" ) );
return ( valid ? 0 : -EINVAL_SIGNATURE );
}
/**
* Encrypt using ECDSA
*
* @v key Key
* @v plaintext Plaintext
* @v ciphertext Ciphertext
* @ret rc Return status code
*/
static int ecdsa_encrypt ( const struct asn1_cursor *key __unused,
const struct asn1_cursor *plaintext __unused,
struct asn1_builder *ciphertext __unused ) {
/* Not a defined operation for ECDSA */
return -ENOTTY;
}
/**
* Decrypt using ECDSA
*
* @v key Key
* @v ciphertext Ciphertext
* @v plaintext Plaintext
* @ret rc Return status code
*/
static int ecdsa_decrypt ( const struct asn1_cursor *key __unused,
const struct asn1_cursor *ciphertext __unused,
struct asn1_builder *plaintext __unused ) {
/* Not a defined operation for ECDSA */
return -ENOTTY;
}
/**
* Sign digest value using ECDSA
*
* @v key Key
* @v digest Digest algorithm
* @v value Digest value
* @v signature Signature
* @ret rc Return status code
*/
static int ecdsa_sign ( const struct asn1_cursor *key,
struct digest_algorithm *digest, const void *value,
struct asn1_builder *signature ) {
struct ecdsa_context ctx;
int rc;
/* Initialise context */
if ( ( rc = ecdsa_init ( &ctx, key, digest, value ) ) != 0 )
goto err_init;
/* Fail unless we have a private key */
if ( ! ctx.key.private ) {
rc = -ENOTTY;
goto err_no_key;
}
/* Instantiate DRBG */
hmac_drbg_instantiate ( digest, ctx.drbg, ctx.key.private,
ctx.key.curve->keysize, value, ctx.zlen );
/* Create signature */
if ( ( rc = ecdsa_sign_rs ( &ctx ) ) != 0 )
goto err_signature;
/* Construct "r" and "s" values */
if ( ( rc = ecdsa_prepend_signature ( &ctx, ctx.s0, signature ) ) != 0)
goto err_s;
if ( ( rc = ecdsa_prepend_signature ( &ctx, ctx.r0, signature ) ) != 0)
goto err_r;
if ( ( rc = asn1_wrap ( signature, ASN1_SEQUENCE ) ) != 0 )
goto err_wrap;
/* Free context */
ecdsa_free ( &ctx );
return 0;
err_wrap:
err_r:
err_s:
err_signature:
err_no_key:
ecdsa_free ( &ctx );
err_init:
return rc;
}
/**
* Verify signed digest using ECDSA
*
* @v key Key
* @v digest Digest algorithm
* @v value Digest value
* @v signature Signature
* @ret rc Return status code
*/
static int ecdsa_verify ( const struct asn1_cursor *key,
struct digest_algorithm *digest, const void *value,
const struct asn1_cursor *signature ) {
struct ecdsa_context ctx;
struct asn1_cursor cursor;
int rc;
/* Initialise context */
if ( ( rc = ecdsa_init ( &ctx, key, digest, value ) ) != 0 )
goto err_init;
/* Enter sequence */
memcpy ( &cursor, signature, sizeof ( cursor ) );
asn1_enter ( &cursor, ASN1_SEQUENCE );
/* Extract "r" and "s" values */
if ( ( rc = ecdsa_parse_signature ( &ctx, ctx.r0, &cursor ) ) != 0 )
goto err_r;
asn1_skip_any ( &cursor );
if ( ( rc = ecdsa_parse_signature ( &ctx, ctx.s0, &cursor ) ) != 0 )
goto err_s;
/* Verify signature */
if ( ( rc = ecdsa_verify_rs ( &ctx ) ) != 0 )
goto err_verify;
/* Free context */
ecdsa_free ( &ctx );
return 0;
err_verify:
err_s:
err_r:
ecdsa_free ( &ctx );
err_init:
return rc;
}
/**
* Check for matching ECDSA public/private key pair
*
* @v private_key Private key
* @v public_key Public key
* @ret rc Return status code
*/
static int ecdsa_match ( const struct asn1_cursor *private_key,
const struct asn1_cursor *public_key ) {
struct elliptic_curve *curve;
struct ecdsa_key privkey;
struct ecdsa_key pubkey;
int rc;
/* Parse keys */
if ( ( rc = ecdsa_parse_key ( &privkey, private_key ) ) != 0 )
return rc;
if ( ( rc = ecdsa_parse_key ( &pubkey, public_key ) ) != 0 )
return rc;
/* Compare curves */
if ( privkey.curve != pubkey.curve )
return -ENOTTY;
curve = privkey.curve;
/* Compare public curve points */
if ( memcmp ( privkey.public, pubkey.public, curve->pointsize ) != 0 )
return -ENOTTY;
return 0;
}
/** ECDSA public-key algorithm */
struct pubkey_algorithm ecdsa_algorithm = {
.name = "ecdsa",
.encrypt = ecdsa_encrypt,
.decrypt = ecdsa_decrypt,
.sign = ecdsa_sign,
.verify = ecdsa_verify,
.match = ecdsa_match,
};
|