1 /* yubikey.h --- Prototypes for low-level Yubikey OTP functions.
2  *
3  * Written by Simon Josefsson <simon@josefsson.org>.
4  * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *    * Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *
14  *    * Redistributions in binary form must reproduce the above
15  *      copyright notice, this list of conditions and the following
16  *      disclaimer in the documentation and/or other materials provided
17  *      with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #ifndef YUBIKEY_H
34 # define YUBIKEY_H
35 
36 # include <string.h>
37 
38 # define YUBIKEY_BLOCK_SIZE 16
39 # define YUBIKEY_KEY_SIZE 16
40 # define YUBIKEY_UID_SIZE 6
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 typedef struct
47 {
48   /* Unique (secret) ID. */
49   unsigned char uid[YUBIKEY_UID_SIZE];
50   /* Session counter (incremented by 1 at each startup).  High bit
51      indicates whether caps-lock triggered the token. */
52   unsigned short ctr;
53   /* Timestamp incremented by approx 8Hz (low part). */
54   unsigned short tstpl;
55   /* Timestamp (high part). */
56   unsigned char tstph;
57   /* Number of times used within session + activation flags. */
58   unsigned char use;
59   /* Pseudo-random value. */
60   unsigned short rnd;
61   /* CRC16 value of all fields. */
62   unsigned short crc;
63 } yubikey_token_st;
64 
65 typedef yubikey_token_st *yubikey_token_t;
66 
67 /* High-level functions. */
68 
69 /* Decrypt TOKEN using KEY and store output in OUT structure.  Note
70    that there is no error checking whether the output data is valid or
71    not, use yubikey_check_* for that. */
72 extern void yubikey_parse (const unsigned char token[YUBIKEY_BLOCK_SIZE],
73 			   const unsigned char key[YUBIKEY_KEY_SIZE],
74 			   yubikey_token_t out);
75 
76 # define yubikey_counter(ctr) ((ctr) & 0x7FFF)
77 # define yubikey_capslock(ctr) ((ctr) & 0x8000)
78 # define yubikey_crc_ok_p(tok) \
79   (yubikey_crc16 ((tok), YUBIKEY_BLOCK_SIZE) == YUBIKEY_CRC_OK_RESIDUE)
80 
81 /*
82  * Low-level functions; ModHex.
83  */
84 
85 # define YUBIKEY_MODHEX_MAP "cbdefghijklnrtuv"
86 
87 /* ModHex encode input string SRC of length SRCSIZE and put the zero
88    terminated output string in DST.  The size of the output string DST
89    must be at least 2*SRCSIZE+1.  The output string is always
90    2*SRCSIZE large plus the terminating zero.  */
91 extern void yubikey_modhex_encode (char *dst,
92 				   const char *src,
93 				   size_t srcsize);
94 
95 /* ModHex decode input string SRC of length DSTSIZE/2 into output
96    string DST.  The output string DST is always DSTSIZE/2 large plus
97    the terminating zero.  */
98 extern void yubikey_modhex_decode (char *dst,
99 				   const char *src,
100 				   size_t dstsize);
101 
102 /* Hex encode/decode data, same interface as modhex functions. */
103 extern void yubikey_hex_encode (char *dst, const char *src, size_t srcsize);
104 extern void yubikey_hex_decode (char *dst, const char *src, size_t dstsize);
105 
106 /* Return non-zero if zero-terminated input STR is a valid (mod)hex
107    string, and zero if any non-alphabetic characters are found. */
108 extern int yubikey_modhex_p (const char *str);
109 extern int yubikey_hex_p (const char *str);
110 
111 /*
112  * Low-level functions; CRC.
113  */
114 
115 # define YUBIKEY_CRC_OK_RESIDUE 0xf0b8
116 extern unsigned short yubikey_crc16 (const unsigned char * buf, size_t buf_size);
117 
118 /* Low-level functions; AES. */
119 
120 /* AES-decrypt one 16-byte block STATE using the 128-bit KEY, leaving
121    the decrypted output in the STATE buffer. */
122 extern void yubikey_aes_decrypt (unsigned char * state, const unsigned char * key);
123 
124 #ifdef __cplusplus
125 } // extern "C"
126 #endif
127 
128 
129 #endif
130