-
Notifications
You must be signed in to change notification settings - Fork 0
/
yhsm2_lib.go
2350 lines (2159 loc) · 88.6 KB
/
yhsm2_lib.go
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package yhsm2go
/*
#cgo CFLAGS: -I/usr/include -std=c99
#cgo LDFLAGS: -L/usr/lib -lyubihsm -lyubihsm_usb -lyubihsm_http
#include <stdlib.h>
#include <stdio.h>
#include <yubihsm.h>
void yh_set_debug_output_bridge(yh_connector *connector, const char *fpath){
FILE* fio = fopen(fpath, "w+b");
yh_set_debug_output(connector, fio);
return;
}
*/
import "C"
import (
"unsafe"
)
/**
* Return a string describing an error condition
*
* @param err #yh_rc error code
*
* @return String with descriptive error
**/
func YH_strerror(err YH_rc) string {
err_ch := C.yh_strerror(C.yh_rc(err))
return C.GoString(err_ch)
}
/**
* Set verbosity level when executing commands. Default verbosity is
*#YH_VERB_QUIET
*
* This function may be called prior to global library initialization to set
* the debug level
*
* @param connector If not NULL, the verbosity of the specific connector will
* be set
* @param verbosity The desired level of debug output
*
* @return #YHR_SUCCESS
*
* @see YH_VERB_QUIET, YH_VERB_INTERMEDIATE, YH_VERB_CRYPTO, YH_VERB_RAW,
* YH_VERB_INFO, YH_VERB_ERR, YH_VERB_ALL
**/
func YH_set_verbosity(connector *YH_connector, verbosity uint8) YH_rc {
return YH_rc(C.yh_set_verbosity((*C.yh_connector)(unsafe.Pointer(connector)), C.uint8_t(verbosity)))
}
/**
* Get verbosity level when executing commands
*
* @param verbosity The verbosity level
*
* @return #YHR_SUCCESS if seccessful.
* #YHR_INVALID_PARAMETERS if verbosity is NULL
*
* @see YH_VERB_QUIET, YH_VERB_INTERMEDIATE, YH_VERB_CRYPTO, YH_VERB_RAW,
* YH_VERB_INFO, YH_VERB_ERR, YH_VERB_ALL
**/
func YH_get_verbosity(verbosity *uint8) YH_rc {
return YH_rc(C.yh_get_verbosity((*C.uint8_t)(unsafe.Pointer(verbosity))))
}
/**
* Set file for debug output
*
* @param connector If not NULL, the debug messages will be written to the
*specified output file
* @param fpath The filepath for the destination of the debug messages
*
* @return void
**/
func YH_set_debug_output(connector *YH_connector, fpath string) {
dbg_fpath := C.CString(fpath)
C.yh_set_debug_output_bridge((*C.yh_connector)(unsafe.Pointer(connector)), dbg_fpath)
C.free(unsafe.Pointer(dbg_fpath))
return
}
/**
* Global library initialization
*
* @return #YHR_SUCCESS
**/
func YH_init() YH_rc {
return YH_rc(C.yh_init())
}
/**
* Global library clean up
*
* @return #YHR_SUCCESS
**/
func YH_exit() YH_rc {
return YH_rc(C.yh_exit())
}
/**
* Instantiate a new connector
*
* @param url URL associated with this connector
* @param connector Connector to the device
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if either the URL or the connector are NULL.
* #YHR_GENERIC_ERROR if failed to load the backend.
* #YHR_MEMORY_ERROR if failed to allocate memory for the connector.
* #YHR_CONNECTION_ERROR if failed to create the connector
*/
func YH_init_connector(url string) (*YH_connector, YH_rc) {
curl := C.CString(url)
var connector *YH_connector
rc := C.yh_init_connector(curl, (**C.yh_connector)(unsafe.Pointer(&connector)))
C.free(unsafe.Pointer(curl))
return connector, YH_rc(rc)
}
/**
* Set connector options.
*
* Note that backend options are not supported with winhttp or USB connectors
*
* @param connector Connector to set an option on
* @param opt Option to set. See #yh_connector_option
* @param val Value of the option. Type of value is specific to the given
*option
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the connector or the value are NULL or if
*the option is unknown. #YHR_CONNECTOR_ERROR if failed to set the option
**/
func YH_set_connector_option(connector *YH_connector, opt YH_connector_option, value string) YH_rc {
v := C.CString(value)
rc := C.yh_set_connector_option( (*C.yh_connector)(unsafe.Pointer(connector)),
(C.yh_connector_option)(opt),
unsafe.Pointer(v))
C.free(unsafe.Pointer(v))
return YH_rc(rc)
}
/**
* Connect to the device through the specified connector
*
* @param connector Connector to the device
* @param timeout Connection timeout in seconds
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the connector does not exist.
* See #yh_rc for other possible errors
**/
func YH_connect(connector *YH_connector, timeout int) YH_rc {
return YH_rc(C.yh_connect((*C.yh_connector)(unsafe.Pointer(connector)), C.int(timeout)))
}
/**
* Disconnect from a connector
*
* @param connector Connector from which to disconnect
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the connector is NULL
**/
func YH_disconnect(connector *YH_connector) YH_rc {
return YH_rc(C.yh_disconnect((*C.yh_connector)(unsafe.Pointer(connector))))
}
/**
* Send a plain (unencrypted) message to the device through a connector
*
* @param connector Connector to the device
* @param cmd Command to send. See #yh_cmd
* @param data Data to send
* @param data_len length of data to send
* @param response_cmd Response command
* @param response Response data
* @param response_len Length of response data
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL.
* #YHR_BUFFER_TOO_SMALL if the actual response was longer than
*response_len. See #yh_rc for other possible errors
**/
func YH_send_plain_msg(connector *YH_connector, cmd YH_cmd, data []byte, data_len int, response_cmd *YH_cmd, response []byte, response_len *int) YH_rc {
rc := C.yh_send_plain_msg((*C.yh_connector)(unsafe.Pointer(connector)), (C.yh_cmd)(cmd),
(*C.uint8_t)(unsafe.Pointer(&data[0])), (C.size_t)(data_len),
(*C.yh_cmd)(unsafe.Pointer(response_cmd)), (*C.uint8_t)(unsafe.Pointer(&response[0])),
(*C.size_t)(unsafe.Pointer(response_len)))
return YH_rc(rc)
}
/**
* Send an encrypted message to the device over a session. The session has to be
*authenticated
*
* @param session Session to send the message over
* @param cmd Command to send
* @param data Data to send
* @param data_len length of data to send
* @param response_cmd Response command
* @param response Response data
* @param response_buffer_len Length of response buffer
*
* @return #YHR_SUCCESS if successful. See #yh_rc for possible errors
**/
func YH_send_secure_msg(session *YH_session, cmd YH_cmd, data []byte, data_len int, response_cmd *YH_cmd, response []byte, response_len *int) YH_rc {
rc := C.yh_send_secure_msg( (*C.yh_session)(unsafe.Pointer(session)), (C.yh_cmd)(cmd),
(*C.uint8_t)(unsafe.Pointer(&data[0])), (C.size_t)(data_len),
(*C.yh_cmd)(unsafe.Pointer(response_cmd)), (*C.uint8_t)(unsafe.Pointer(&response[0])),
(*C.size_t)(unsafe.Pointer(response_len)))
return YH_rc(rc)
}
/**
* Create a session that uses an encryption key and a MAC key derived from a
*password
*
* @param connector Connector to the device
* @param authkey_id Object ID of the Authentication Key used to authenticate
*the session
* @param password Password used to derive the session encryption key and MAC
*key
* @param password_len Length of the password in bytes
* @param recreate_session If true, the session will be recreated if expired.
*This caches the password in memory
* @param session The created session
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the connector, the password or the session
*are NULL. #YHR_GENERIC_ERROR if failed to derive the session encryption key
*and/or the MAC key or if PRNG related errors occur. #YHR_MEMORY_ERROR if
*failed to allocate memory for the session. See #yh_rc for other possible
*errors
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>
**/
func YH_create_session_derived(connector *YH_connector, authkey_id uint16, password string, recreate_session bool) (*YH_session, YH_rc) {
p := C.CString(password)
var session *YH_session
rc := C.yh_create_session_derived( (*C.yh_connector)(unsafe.Pointer(connector)),
C.uint16_t(authkey_id),
(*C.uint8_t)(unsafe.Pointer(p)),
C.size_t(len(password)),
C.bool(recreate_session),
(**C.yh_session)(unsafe.Pointer(&session)))
C.free(unsafe.Pointer(p))
return session, YH_rc(rc)
}
/**
* Create a session that uses the specified encryption key and MAC key to derive
*session-specific keys
*
* @param connector Connector to the device
* @param authkey_id Object ID of the Authentication Key used to authenticate
*the session
* @param key_enc Encryption key used to derive the session encryption key
* @param key_enc_len Length of the encryption key.
* @param key_mac MAC key used to derive the session MAC key
* @param key_mac_len Length of the MAC key.
* @param recreate_session If true, the session will be recreated if expired.
*This caches the password in memory
* @param session created session
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL or incorrect.
* See #yh_rc for other possible errors
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>,
* <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Object.html">Authentication
*Key</a>
**/
func YH_create_session(connector *YH_connector, authkey_id uint16, key_enc []byte, key_enc_len int, key_mac []byte, key_mac_len int, recreate_session bool) (*YH_session, YH_rc) {
var session *YH_session
rc := C.yh_create_session( (*C.yh_connector)(unsafe.Pointer(connector)),
(C.uint16_t)(authkey_id),
(*C.uint8_t)(unsafe.Pointer(&key_enc[0])), (C.size_t)(key_enc_len),
(*C.uint8_t)(unsafe.Pointer(&key_mac[0])), (C.size_t)(key_mac_len),
(C.bool)(recreate_session),
(**C.yh_session)(unsafe.Pointer(&session)))
return session, YH_rc(rc)
}
/**
* Begin creating an external session. The session's encryption key and MAC key
*are not stored in the device.
*
* This function must be followed by yh_finish_create_session_ext() to set the
*session keys.
*
* @param connector Connector to the device
* @param authkey_id Object ID of the Authentication Key used to authenticate
*the session
* @param context pointer to where context data is saved
* @param card_cryptogram Card cryptogram
* @param card_cryptogram_len Length of card cryptogram
* @param session created session
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL.
* #YHR_MEMORY_ERROR if failed to allocate memory for the session.
* See #yh_rc for other possible errors
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>
**/
func YH_begin_create_session_ext(connector *YH_connector, authkey_id uint16, card_cryptogram []byte, card_cryptogram_len int) (*YH_session, *uint8, YH_rc) {
var session *YH_session
var context *uint8
rc := C.yh_begin_create_session_ext( (*C.yh_connector)(unsafe.Pointer(connector)),
(C.uint16_t)(authkey_id),
(**C.uint8_t)(unsafe.Pointer(&context)),
(*C.uint8_t)(unsafe.Pointer(&card_cryptogram[0])), (C.size_t)(card_cryptogram_len),
(**C.yh_session)(unsafe.Pointer(&session)))
return session, context, YH_rc(rc)
}
/**
* Finish creating external session. The session's encryption key and MAC key
*are not stored in the device.
*
* This function must be called after yh_begin_create_session_ext().
*
* @param connector Connector to the device
* @param session The session created with yh_begin_create_session_ext()
* @param key_senc Session encryption key used to encrypt the messages exchanged
*with the device
* @param key_senc_len Lenght of the encryption key. Must be #YH_KEY_LEN
* @param key_smac Session MAC key used for creating the authentication tag for
*each message
* @param key_smac_len Length of the MAC key. Must be #YH_KEY_LEN
* @param key_srmac Session return MAC key used for creating the authentication
*tag for each response message
* @param key_srmac_len Length of the return MAC key. Must be #YH_KEY_LEN
* @param card_cryptogram Card cryptogram
* @param card_cryptogram_len Length of card cryptogram
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL or any of the
*key lengths are not #YH_KEY_LEN.
* See #yh_rc for other possible errors
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>
**/
func YH_finish_create_session_ext(connector *YH_connector, session *YH_session,
key_senc []byte, key_senc_len int,
key_smac []byte, key_smac_len int,
key_srmac []byte, key_srmac_len int,
card_cryptogram []byte, card_cryptogram_len int) YH_rc {
rc := C.yh_finish_create_session_ext( (*C.yh_connector)(unsafe.Pointer(connector)), (*C.yh_session)(unsafe.Pointer(session)),
(*C.uint8_t)(unsafe.Pointer(&key_senc[0])), (C.size_t)(key_senc_len),
(*C.uint8_t)(unsafe.Pointer(&key_smac[0])), (C.size_t)(key_smac_len),
(*C.uint8_t)(unsafe.Pointer(&key_srmac[0])), (C.size_t)(key_srmac_len),
(*C.uint8_t)(unsafe.Pointer(&card_cryptogram[0])), (C.size_t)(card_cryptogram_len))
return YH_rc(rc)
}
/**
* Free data associated with the session
*
* @param session Pointer to the session to destroy
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the session is NULL.
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>
**/
func YH_destroy_session(session *YH_session) YH_rc {
return YH_rc(C.yh_destroy_session((**C.yh_session)(unsafe.Pointer(&session))))
}
/**
* Authenticate session
*
* @param session Session to authenticate
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the session is NULL.
* #YHR_SESSION_AUTHENTICATION_FAILED if the session fails to
*authenticate. See #yh_rc for other possible errors
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Session.html">Session</a>
**/
func YH_authenticate_session(session *YH_session) YH_rc {
return YH_rc(C.yh_authenticate_session((*C.yh_session)(unsafe.Pointer(session))))
}
/**
* Get device version, device serial number, supported algorithms and available
*log entries.
*
* @param connector Connector to the device
* @param major Device major version number
* @param minor Device minor version number
* @param patch Device build version number
* @param serial Device serial number
* @param log_total Total number of log entries
* @param log_used Number of written log entries
* @param algorithms List of supported algorithms
* @param n_algorithms Number of supported algorithms
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the connector is NULL.
* #YHR_BUFFER_TOO_SMALL if n_algorithms is smaller than the number of
*actually supported algorithms. See #yh_rc for other possible errors.
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Algorithms.html">Algorithms</a>
**/
func YH_util_get_device_info(connector *YH_connector, major,
minor, patch *uint8, serial *uint32,
log_total, log_used *uint8,
algorithms []YH_algorithm, n_algorithms *int) YH_rc {
rc := C.yh_util_get_device_info((*C.yh_connector)(unsafe.Pointer(connector)),
(*C.uint8_t)(unsafe.Pointer(major)), (*C.uint8_t)(unsafe.Pointer(minor)),
(*C.uint8_t)(unsafe.Pointer(patch)), (*C.uint32_t)(unsafe.Pointer(serial)),
(*C.uint8_t)(unsafe.Pointer(log_total)), (*C.uint8_t)(unsafe.Pointer(log_used)),
(*C.yh_algorithm)(unsafe.Pointer(&algorithms[0])), (*C.size_t)(unsafe.Pointer(n_algorithms)))
return YH_rc(rc)
}
/**
* List objects accessible from the session
*
* @param session Authenticated session to use
* @param id Object ID to filter by (0 to not filter by ID)
* @param type Object type to filter by (0 to not filter by type). See
*#yh_object_type
* @param domains Domains to filter by (0 to not filter by domain)
* @param capabilities Capabilities to filter by (0 to not filter by
*capabilities). See #yh_capabilities
* @param algorithm Algorithm to filter by (0 to not filter by algorithm)
* @param label Label to filter by
* @param objects Array of objects returned
* @param n_objects Max number of objects (will be set to number found on
*return)
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL.
* #YHR_BUFFER_TOO_SMALL if n_objects is smaller than the number of
*objects found. See #yh_rc for other possible errors.
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Object.html">Objects</a>,
* <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Domain.html">Domains</a>,
* <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Capability.html">Capabilities</a>,
* <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Algorithms.html">Algorithms</a>,
* <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Label.html">Labels</a>
**/
func YH_util_list_objects(session *YH_session, id uint16, object_type YH_object_type, domains uint16, capabilities *YH_capabilities,
algorithm YH_algorithm, label string, objects []YH_object_descriptor, n_objects *int) YH_rc {
l := C.CString(label)
rc := C.yh_util_list_objects((*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(id), (C.yh_object_type)(object_type),
(C.uint16_t)(domains), (*C.yh_capabilities)(unsafe.Pointer(capabilities)), (C.yh_algorithm)(algorithm), l,
(*C.yh_object_descriptor)(unsafe.Pointer(&objects[0])), (*C.size_t)(unsafe.Pointer(n_objects)))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Get metadata of the object with the specified Object ID and Type
*
* @param session Authenticated session to use
* @param id Object ID of the object to get
* @param type Object type. See #yh_object_type
* @param object Object information
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the session is NULL.
* See #yh_rc for other possible errors.
*
* @see <a
*href="https://developers.yubico.com/YubiHSM2/Concepts/Object.html">Objects</a>
**/
func YH_util_get_object_info(session *YH_session, id uint16, object_type YH_object_type, objects *YH_object_descriptor) YH_rc {
rc := C.yh_util_get_object_info((*C.yh_session)(unsafe.Pointer(session)),
(C.uint16_t)(id), (C.yh_object_type)(object_type),
(*C.yh_object_descriptor)(unsafe.Pointer(objects)))
return YH_rc(rc)
}
/**
* Get the value of the public key with the specified Object ID
*
* @param session Authenticated session to use
* @param id Object ID of the public key
* @param data Value of the public key
* @param data_len Length of the public key in bytes
* @param algorithm Algorithm of the key
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if input parameters are NULL.
* #YHR_BUFFER_TOO_SMALL if the actual key length was bigger than
*data_len. See #yh_rc for other possible errors
**/
func YH_util_get_public_key(session *YH_session, id uint16, data []byte, data_len int, algorithm *YH_algorithm) YH_rc {
rc := C.yh_util_get_public_key((*C.yh_session)(unsafe.Pointer(session)),
(C.uint16_t)(id), (*C.uint8_t)(unsafe.Pointer(&data[0])), (*C.size_t)(unsafe.Pointer(&data_len)),
(*C.yh_algorithm)(unsafe.Pointer(algorithm)))
return YH_rc(rc)
}
/**
* Close a session
*
* @param session Session to close
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS if the session is NULL.
* See #yh_rc for other possible errors
**/
func YH_util_close_session(session *YH_session) YH_rc {
return YH_rc(C.yh_util_close_session((*C.yh_session)(unsafe.Pointer(session))))
}
/**
* Sign data using RSA-PKCS#1v1.5
*
* <tt>in</tt> is either a raw hashed message (sha1, sha256, sha384 or sha512)
*or that with correct digestinfo pre-pended
*
* @param session Authenticated session to use
* @param key_id Object ID of the signing key
* @param hashed true if data is only hashed
* @param in data to sign
* @param in_len length of data to sign
* @param out signed data
* @param out_len length of signed data
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>in_len</tt> is not 20, 34, 48 or 64. See #yh_rc for other possible errors
**/
func YH_util_sign_pkcs1v1_5(session *YH_session, key_id uint16, hashed bool, in []byte, in_len int, out []byte, out_len *int) YH_rc {
rc := C.yh_util_sign_pkcs1v1_5( (*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(key_id), (C.bool)(hashed),
(*C.uint8_t)(unsafe.Pointer(&in[0])), (C.size_t)(in_len),
(*C.uint8_t)(unsafe.Pointer(&out[0])), (*C.size_t)(unsafe.Pointer(out_len)))
return YH_rc(rc)
}
/**
* Sign data using RSA-PSS
*
* <tt>in</tt> is a raw hashed message (sha1, sha256, sha384 or sha512)
*
* @param session Authenticated session to use
* @param key_id Object ID of the signing key
* @param in Data to sign
* @param in_len Length of data to sign
* @param out Signed data
* @param out_len Length of signed data
* @param salt_len Length of salt
* @param mgf1Algo Algorithm for mgf1 (mask generation function for PSS)
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>in_len</tt> is not 20, 34, 48 or 64. See #yh_rc for other possible errors
*
* @see <a href="https://tools.ietf.org/html/rfc8017#section-9.1">PSS
*specifications</a>
**/
func YH_util_sign_pss(session *YH_session, key_id uint16, in []byte, in_len int, out []byte, out_len *int, salt_len int, algorithm YH_algorithm) YH_rc {
rc := C.yh_util_sign_pss( (*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(key_id),
(*C.uint8_t)(unsafe.Pointer(&in[0])), (C.size_t)(in_len),
(*C.uint8_t)(unsafe.Pointer(&out[0])), (*C.size_t)(unsafe.Pointer(out_len)),
(C.size_t)(salt_len), (C.yh_algorithm)(algorithm))
return YH_rc(rc)
}
/**
* Sign data using ECDSA
*
* <tt>in</tt> is a raw hashed message, a truncated hash to the curve length or
*a padded hash to the curve length
*
* @param session Authenticated session to use
* @param key_id Object ID of the signing key
* @param in Data to sign
* @param in_len Length of data to sign
* @param out Signed data
* @param out_len Length of signed data
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>in_len</tt> is not 20, 28, 34, 48, 64 or 66. See #yh_rc for other possible
*errors
**/
func YH_util_sign_ecdsa(session *YH_session, key_id uint16, in []byte, in_len int, out []byte, out_len *int) YH_rc {
rc := C.yh_util_sign_ecdsa( (*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(key_id),
(*C.uint8_t)(unsafe.Pointer(&in[0])), (C.size_t)(in_len),
(*C.uint8_t)(unsafe.Pointer(&out[0])), (*C.size_t)(unsafe.Pointer(out_len)))
return YH_rc(rc)
}
/**
* Sign data using EdDSA
*
* @param session Authenticated session to use
* @param key_id Object ID of the signing key
* @param in Data to sign
* @param in_len Length of data to sign
* @param out Signed data
* @param out_len Length of signed data
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>in_len</tt> is bigger than YH_MSG_BUF_SIZE-2. See #yh_rc for other
*possible errors
**/
func YH_util_sign_eddsa(session *YH_session, key_id uint16, in []byte, in_len int, out []byte, out_len *int) YH_rc {
rc := C.yh_util_sign_eddsa( (*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(key_id),
(*C.uint8_t)(unsafe.Pointer(&in[0])), (C.size_t)(in_len),
(*C.uint8_t)(unsafe.Pointer(&out[0])), (*C.size_t)(unsafe.Pointer(out_len)))
return YH_rc(rc)
}
/**
* Sign data using HMAC
*
* @param session Authenticated session to use
* @param key_id Object ID of the signing key
* @param in Data to HMAC
* @param in_len Length of data to hmac
* @param out HMAC
* @param out_len Length of HMAC
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>in_len</tt> is bigger than YH_MSG_BUF_SIZE-2. See #yh_rc for other
*possible errors
**/
func YH_util_sign_hmac(session *YH_session, key_id uint16, in []byte, in_len int, out []byte, out_len *int) YH_rc {
rc := C.yh_util_sign_hmac((*C.yh_session)(unsafe.Pointer(session)), (C.uint16_t)(key_id),
(*C.uint8_t)(unsafe.Pointer(&in[0])), (C.size_t)(in_len),
(*C.uint8_t)(unsafe.Pointer(&out[0])), (*C.size_t)(unsafe.Pointer(out_len)))
return YH_rc(rc)
}
/**
* Get a fixed number of pseudo-random bytes from the device
*
* @param session Authenticated session to use
* @param len Length of pseudo-random data to get
* @param out Pseudo-random data out
* @param out_len Length of pseudo-random data
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL.
* See #yh_rc for other possible errors
**/
func YH_util_get_pseudo_random(session *YH_session, rlen int, out []byte, out_len *int) YH_rc {
rc := C.yh_util_get_pseudo_random((*C.yh_session)(unsafe.Pointer(session)),
C.size_t(rlen),
(*C.uint8_t)(unsafe.Pointer(&out[0])),
(*C.size_t)(unsafe.Pointer(out_len)))
return YH_rc(rc)
}
/**
* Import an RSA key into the device
*
* @param session Authenticated session to use
* @param key_id Object ID the key. 0 if Object ID should be generated by
*the device
* @param label Label of the key. Maximum length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs specified as an unsigned int.
*See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm of the key to import. Must be one of:
*#YH_ALGO_RSA_2048, #YH_ALGO_RSA_3072 or #YH_ALGO_RSA_4096
* @param p P component of the RSA key to import
* @param q Q component of the RSA key to import
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not one of #YH_ALGO_RSA_2048, #YH_ALGO_RSA_3072 or #YH_ALGO_RSA_4096. See
*#yh_rc for other possible errors
**/
func YH_util_import_rsa_key( session *YH_session,
key_id *uint16,
label string,
domains uint16,
capabilities *YH_capabilities,
algorithm YH_algorithm,
p, q []byte) YH_rc {
l := C.CString(label)
rc := C.yh_util_import_rsa_key( (*C.yh_session)(unsafe.Pointer(session)), (*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains), (*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm), (*C.uint8_t)(unsafe.Pointer(&p[0])), (*C.uint8_t)(unsafe.Pointer(&q[0])))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Import an Elliptic Curve key into the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be generated
*by the device
* @param label Label of the key. Maximum length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs specified as
*an unsigned int. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm of the key to import. Must be one of:
*#YH_ALGO_EC_P224, #YH_ALGO_EC_P256, #YH_ALGO_EC_K256, #YH_ALGO_EC_BP256,
*#YH_ALGO_EC_P384, #YH_ALGO_EC_BP384, #YH_ALGO_EC_BP512 or #YH_ALGO_EC_P521
* @param s the EC key to import
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not one of #YH_ALGO_EC_P224, #YH_ALGO_EC_P256, #YH_ALGO_EC_K256,
*#YH_ALGO_EC_BP256, #YH_ALGO_EC_P384, #YH_ALGO_EC_BP384, #YH_ALGO_EC_BP512 or
*#YH_ALGO_EC_P521.
* See #yh_rc for other possible errors
**/
func YH_util_import_ec_key( session *YH_session,
key_id *uint16,
label string,
domains uint16,
capabilities *YH_capabilities,
algorithm YH_algorithm,
s []byte) YH_rc {
l := C.CString(label)
rc := C.yh_util_import_ec_key( (*C.yh_session)(unsafe.Pointer(session)), (*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains), (*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm), (*C.uint8_t)(unsafe.Pointer(&s[0])))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Import an ED key into the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key will have. 0 if the Object ID should be
*generated by the device
* @param label Label of the key. Maximum length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm of the key to import. Must be #YH_ALGO_EC_ED25519
* @param k the ED key to import
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not #YH_ALGO_EC_ED25519. See #yh_rc for other possible errors
**/
func YH_util_import_ed_key( session *YH_session,
key_id *uint16,
label string,
domains uint16,
capabilities *YH_capabilities,
algorithm YH_algorithm,
k []byte) YH_rc {
l := C.CString(label)
rc := C.yh_util_import_ed_key( (*C.yh_session)(unsafe.Pointer(session)), (*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains), (*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm), (*C.uint8_t)(unsafe.Pointer(&k[0])))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Import an HMAC key into the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be
*generated by the device
* @param label Label of the key. Maxium length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm of the key to import. Must be one of:
*#YH_ALGO_HMAC_SHA1, #YH_ALGO_HMAC_SHA256, #YH_ALGO_HMAC_SHA384
*or #YH_ALGO_HMAC_SHA512
* @param key The HMAC key to import
* @param key_len Length of the HMAC key to import
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL.
* See #yh_rc for other possible errors
**/
func YH_util_import_hmac_key( session *YH_session,
key_id *uint16,
label string,
domains uint16,
capabilities *YH_capabilities,
algorithm YH_algorithm,
key []byte) YH_rc {
l := C.CString(label)
rc := C.yh_util_import_hmac_key( (*C.yh_session)(unsafe.Pointer(session)), (*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains), (*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm), (*C.uint8_t)(unsafe.Pointer(&key[0])), (C.size_t)(len(key)))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Generate an RSA key in the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be
*generated by the device
* @param label Label of the key. Maximum length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm to use to generate the RSA key. Supported
*algorithms: #YH_ALGO_RSA_2048, #YH_ALGO_RSA_3072 and #YH_ALGO_RSA_4096
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not one of #YH_ALGO_RSA_2048, #YH_ALGO_RSA_3072 or #YH_ALGO_RSA_4096.
* See #yh_rc for other possible errors
**/
func YH_util_generate_rsa_key( session *YH_session, key_id *uint16, label string, domains uint16,
capabilities *YH_capabilities, algorithm YH_algorithm) YH_rc {
l := C.CString(label)
rc := C.yh_util_generate_rsa_key((*C.yh_session)(unsafe.Pointer(session)),
(*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains),
(*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Generate an Elliptic Curve key in the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be generated
*by the device
* @param label Label of the key. Maximum length is #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm to use to generate the EC key. Supported
*algorithm: #YH_ALGO_EC_P224, #YH_ALGO_EC_P256, #YH_ALGO_EC_K256,
*#YH_ALGO_EC_BP256, #YH_ALGO_EC_P384, #YH_ALGO_EC_BP384, #YH_ALGO_EC_BP512 and
*#YH_ALGO_EC_P521.
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not one of #YH_ALGO_EC_P224, #YH_ALGO_EC_P256, #YH_ALGO_EC_K256,
*#YH_ALGO_EC_BP256, #YH_ALGO_EC_P384, #YH_ALGO_EC_BP384, #YH_ALGO_EC_BP512 or
*#YH_ALGO_EC_P521.
* See #yh_rc for other possible errors
**/
func YH_util_generate_ec_key( session *YH_session, key_id *uint16, label string, domains uint16,
capabilities *YH_capabilities, algorithm YH_algorithm) YH_rc {
l := C.CString(label)
rc := C.yh_util_generate_ec_key((*C.yh_session)(unsafe.Pointer(session)),
(*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains),
(*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Generate an ED key in the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be generated
*by the device
* @param label Label for the key. Maximum length #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the ED key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm to use to generate the ED key. Supported
*algorithm: #YH_ALGO_EC_ED25519
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or the algorithm is
*not #YH_ALGO_EC_ED25519.
* See #yh_rc for other possible errors
**/
func YH_util_generate_ed_key( session *YH_session, key_id *uint16, label string, domains uint16,
capabilities *YH_capabilities, algorithm YH_algorithm) YH_rc {
l := C.CString(label)
rc := C.yh_util_generate_ed_key((*C.yh_session)(unsafe.Pointer(session)),
(*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains),
(*C.yh_capabilities)(unsafe.Pointer(capabilities)),
(C.yh_algorithm)(algorithm))
C.free(unsafe.Pointer(l))
return YH_rc(rc)
}
/**
* Verify a generated HMAC
*
* @param session Authenticated session to use
* @param key_id Object ID of the HMAC key
* @param signature HMAC signature (20, 32, 48 or 64 bytes)
* @param signature_len length of HMAC signature
* @param data data to verify
* @param data_len length of data to verify
* @param verified true if verification succeeded
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL or if
*<tt>signature_len</tt> + <tt>data_len</tt> is too long.
* See #yh_rc for other possible errors
*
**/
func YH_util_verify_hmac( session *YH_session, key_id uint16, signature, data []byte, verified *bool) YH_rc {
rc := C.yh_util_verify_hmac((*C.yh_session)(unsafe.Pointer(session)),
(C.uint16_t)(key_id),
(*C.uint8_t)(unsafe.Pointer(&signature[0])), (C.size_t)(len(signature)),
(*C.uint8_t)(unsafe.Pointer(&data[0])), (C.size_t)(len(data)),
(*C.bool)(unsafe.Pointer(verified)))
return YH_rc(rc)
}
/**
* Generate an HMAC key in the device
*
* @param session Authenticated session to use
* @param key_id Object ID of the key. 0 if the Object ID should be
*generated by the device
* @param label Label of the key. Maximum length #YH_OBJ_LABEL_LEN
* @param domains Domains to which the key belongs. See #yh_string_to_domains()
* @param capabilities Capabilities of the key. See
*#yh_string_to_capabilities()
* @param algorithm Algorithm to use to generate the HMAC key. Supported
*algorithims: #YH_ALGO_HMAC_SHA1, #YH_ALGO_HMAC_SHA256, #YH_ALGO_HMAC_SHA384
*and #YH_ALGO_HMAC_SHA512
*
* @return #YHR_SUCCESS if successful.
* #YHR_INVALID_PARAMETERS input parameters are NULL.
* See #yh_rc for other possible errors
*
**/
func YH_util_generate_hmac_key( session *YH_session, key_id *uint16, label string, domains uint16,
capabilities *YH_capabilities, algorithm YH_algorithm) YH_rc {
l := C.CString(label)
rc := C.yh_util_generate_hmac_key((*C.yh_session)(unsafe.Pointer(session)),
(*C.uint16_t)(unsafe.Pointer(key_id)),
l, (C.uint16_t)(domains),
(*C.yh_capabilities)(unsafe.Pointer(capabilities)),