xref: /openbsd-src/lib/libfido2/src/extern.h (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*
2  * Copyright (c) 2018 Yubico AB. All rights reserved.
3  * Use of this source code is governed by a BSD-style
4  * license that can be found in the LICENSE file.
5  */
6 
7 #ifndef _EXTERN_H
8 #define _EXTERN_H
9 
10 #include <stdint.h>
11 
12 #include "fido/types.h"
13 #include "blob.h"
14 
15 /* aes256 */
16 int aes256_cbc_dec(const fido_blob_t *, const fido_blob_t *, fido_blob_t *);
17 int aes256_cbc_enc(const fido_blob_t *, const fido_blob_t *, fido_blob_t *);
18 
19 /* cbor encoding functions */
20 cbor_item_t *cbor_flatten_vector(cbor_item_t **, size_t);
21 cbor_item_t *cbor_encode_assert_options(fido_opt_t, fido_opt_t);
22 cbor_item_t *cbor_encode_change_pin_auth(const fido_blob_t *,
23     const fido_blob_t *, const fido_blob_t *);
24 cbor_item_t *cbor_encode_extensions(const fido_cred_ext_t *);
25 cbor_item_t *cbor_encode_hmac_secret_param(const fido_blob_t *,
26     const es256_pk_t *, const fido_blob_t *);
27 cbor_item_t *cbor_encode_options(fido_opt_t, fido_opt_t);
28 cbor_item_t *cbor_encode_pin_auth(const fido_blob_t *, const fido_blob_t *);
29 cbor_item_t *cbor_encode_pin_enc(const fido_blob_t *, const fido_blob_t *);
30 cbor_item_t *cbor_encode_pin_hash_enc(const fido_blob_t *, const fido_blob_t *);
31 cbor_item_t *cbor_encode_pin_opt(void);
32 cbor_item_t *cbor_encode_pubkey(const fido_blob_t *);
33 cbor_item_t *cbor_encode_pubkey_list(const fido_blob_array_t *);
34 cbor_item_t *cbor_encode_pubkey_param(int);
35 cbor_item_t *cbor_encode_rp_entity(const fido_rp_t *);
36 cbor_item_t *cbor_encode_set_pin_auth(const fido_blob_t *, const fido_blob_t *);
37 cbor_item_t *cbor_encode_user_entity(const fido_user_t *);
38 cbor_item_t *es256_pk_encode(const es256_pk_t *, int);
39 
40 /* cbor decoding functions */
41 int cbor_decode_attstmt(const cbor_item_t *, fido_attstmt_t *);
42 int cbor_decode_cred_authdata(const cbor_item_t *, int, fido_blob_t *,
43     fido_authdata_t *, fido_attcred_t *, fido_cred_ext_t *);
44 int cbor_decode_assert_authdata(const cbor_item_t *, fido_blob_t *,
45     fido_authdata_t *, int *, fido_blob_t *);
46 int cbor_decode_cred_id(const cbor_item_t *, fido_blob_t *);
47 int cbor_decode_fmt(const cbor_item_t *, char **);
48 int cbor_decode_pubkey(const cbor_item_t *, int *, void *);
49 int cbor_decode_rp_entity(const cbor_item_t *, fido_rp_t *);
50 int cbor_decode_uint64(const cbor_item_t *, uint64_t *);
51 int cbor_decode_user(const cbor_item_t *, fido_user_t *);
52 int es256_pk_decode(const cbor_item_t *, es256_pk_t *);
53 int rs256_pk_decode(const cbor_item_t *, rs256_pk_t *);
54 int eddsa_pk_decode(const cbor_item_t *, eddsa_pk_t *);
55 
56 /* auxiliary cbor routines */
57 int cbor_add_bool(cbor_item_t *, const char *, fido_opt_t);
58 int cbor_add_bytestring(cbor_item_t *, const char *, const unsigned char *,
59     size_t);
60 int cbor_add_string(cbor_item_t *, const char *, const char *);
61 int cbor_array_iter(const cbor_item_t *, void *, int(*)(const cbor_item_t *,
62     void *));
63 int cbor_build_frame(uint8_t, cbor_item_t *[], size_t, fido_blob_t *);
64 int cbor_bytestring_copy(const cbor_item_t *, unsigned char **, size_t *);
65 int cbor_map_iter(const cbor_item_t *, void *, int(*)(const cbor_item_t *,
66     const cbor_item_t *, void *));
67 int cbor_string_copy(const cbor_item_t *, char **);
68 int cbor_parse_reply(const unsigned char *, size_t, void *,
69     int(*)(const cbor_item_t *, const cbor_item_t *, void *));
70 int cbor_add_pin_params(fido_dev_t *, const fido_blob_t *, const es256_pk_t *,
71     const fido_blob_t *,const char *, cbor_item_t **, cbor_item_t **);
72 void cbor_vector_free(cbor_item_t **, size_t);
73 
74 #ifndef nitems
75 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
76 #endif
77 
78 /* buf */
79 int fido_buf_read(const unsigned char **, size_t *, void *, size_t);
80 int fido_buf_write(unsigned char **, size_t *, const void *, size_t);
81 
82 /* hid i/o */
83 void *fido_hid_open(const char *);
84 void  fido_hid_close(void *);
85 int fido_hid_read(void *, unsigned char *, size_t, int);
86 int fido_hid_write(void *, const unsigned char *, size_t);
87 
88 /* generic i/o */
89 int fido_rx_cbor_status(fido_dev_t *, int);
90 int fido_rx(fido_dev_t *, uint8_t, void *, size_t, int);
91 int fido_tx(fido_dev_t *, uint8_t, const void *, size_t);
92 
93 /* log */
94 #ifdef FIDO_NO_DIAGNOSTIC
95 #define fido_log_init(...)	do { /* nothing */ } while (0)
96 #define fido_log_debug(...)	do { /* nothing */ } while (0)
97 #define fido_log_xxd(...)	do { /* nothing */ } while (0)
98 #else
99 #ifdef __GNUC__
100 void fido_log_init(void);
101 void fido_log_debug(const char *, ...)
102     __attribute__((__format__ (printf, 1, 2)));
103 void fido_log_xxd(const void *, size_t);
104 #else
105 void fido_log_init(void);
106 void fido_log_debug(const char *, ...);
107 void fido_log_xxd(const void *, size_t);
108 #endif /* __GNUC__ */
109 #endif /* FIDO_NO_DIAGNOSTIC */
110 
111 /* u2f */
112 int u2f_register(fido_dev_t *, fido_cred_t *, int);
113 int u2f_authenticate(fido_dev_t *, fido_assert_t *, int);
114 
115 /* unexposed fido ops */
116 int fido_dev_authkey(fido_dev_t *, es256_pk_t *);
117 int fido_dev_get_cbor_info_wait(fido_dev_t *, fido_cbor_info_t *, int);
118 int fido_dev_get_pin_token(fido_dev_t *, const char *, const fido_blob_t *,
119     const es256_pk_t *, fido_blob_t *);
120 int fido_do_ecdh(fido_dev_t *, es256_pk_t **, fido_blob_t **);
121 
122 /* misc */
123 void fido_assert_reset_rx(fido_assert_t *);
124 void fido_assert_reset_tx(fido_assert_t *);
125 void fido_cred_reset_rx(fido_cred_t *);
126 void fido_cred_reset_tx(fido_cred_t *);
127 int fido_check_rp_id(const char *, const unsigned char *);
128 int fido_check_flags(uint8_t, fido_opt_t, fido_opt_t);
129 
130 /* crypto */
131 int fido_verify_sig_es256(const fido_blob_t *, const es256_pk_t *,
132     const fido_blob_t *);
133 int fido_verify_sig_rs256(const fido_blob_t *, const rs256_pk_t *,
134     const fido_blob_t *);
135 int fido_verify_sig_eddsa(const fido_blob_t *, const eddsa_pk_t *,
136     const fido_blob_t *);
137 
138 /* hid device manifest */
139 int fido_hid_manifest(fido_dev_info_t *, size_t, size_t *);
140 
141 /* device manifest registration */
142 typedef int (*dev_manifest_func_t)(fido_dev_info_t *, size_t, size_t *);
143 int fido_dev_register_manifest_func(const dev_manifest_func_t);
144 void fido_dev_unregister_manifest_func(const dev_manifest_func_t);
145 
146 #endif /* !_EXTERN_H */
147