YubiRuby 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  /* ykaes.c --- Implementation of AES-128.
2
2
  *
3
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
3
+ * Copyright (c) 2006-2012 Yubico AB
4
4
  * All rights reserved.
5
5
  *
6
6
  * Redistribution and use in source and binary forms, with or without
@@ -212,3 +212,103 @@ yubikey_aes_decrypt (uint8_t * state, const uint8_t * key)
212
212
 
213
213
  }
214
214
  }
215
+
216
+
217
+ /*******************************************************************
218
+ function aesEncrypt encrypts a single AES-128 block
219
+
220
+ void aesEncrypt(unsigned char *state, const unsigned char *key)
221
+
222
+ Where:
223
+ "state" is state buffer, i.e. plaintext in, ciphertext out
224
+ "key" is pointer to AES key
225
+
226
+ *************************************************************************/
227
+
228
+ void
229
+ yubikey_aes_encrypt (uint8_t *state, const uint8_t *key)
230
+ {
231
+ unsigned char i, j, k, tmp, round_key[0x10];
232
+
233
+ memcpy (round_key, key, sizeof (round_key));
234
+
235
+ for (i = 0; i < 16; i++)
236
+ state[i] ^= key[i];
237
+
238
+ for (i = 0; i < NUMBER_OF_ROUNDS; i++)
239
+ {
240
+
241
+ // byte_sub_shift_row(state);
242
+
243
+ /* First row: 0 shift, 0 4 8 12 */
244
+ state[0] = rijndael_sbox[state[0]];
245
+ state[4] = rijndael_sbox[state[4]];
246
+ state[8] = rijndael_sbox[state[8]];
247
+ state[12] = rijndael_sbox[state[12]];
248
+
249
+ /* Second row: 1 shift, 1 5 9 13 */
250
+ tmp = state[1];
251
+ state[1] = rijndael_sbox[state[5]];
252
+ state[5] = rijndael_sbox[state[9]];
253
+ state[9] = rijndael_sbox[state[13]];
254
+ state[13] = rijndael_sbox[tmp];
255
+
256
+ /* Third row: 2 shift, 2 6 10 14 */
257
+ tmp = state[2];
258
+ state[2] = rijndael_sbox[state[10]];
259
+ state[10] = rijndael_sbox[tmp];
260
+ tmp = state[6];
261
+ state[6] = rijndael_sbox[state[14]];
262
+ state[14] = rijndael_sbox[tmp];
263
+
264
+ /* Fourth row: 3 shift, 3 7 11 15 */
265
+ tmp = state[15];
266
+ state[15] = rijndael_sbox[state[11]];
267
+ state[11] = rijndael_sbox[state[7]];
268
+ state[7] = rijndael_sbox[state[3]];
269
+ state[3] = rijndael_sbox[tmp];
270
+
271
+ if (i != (NUMBER_OF_ROUNDS - 1))
272
+ {
273
+
274
+ // mix_column(state);
275
+
276
+ for (k = 0; k < 16; k += 4)
277
+ {
278
+
279
+ j = state[k] ^ state[k + 1];
280
+ tmp = j ^ state[k + 2] ^ state[k + 3];
281
+
282
+ j = xtime (j);
283
+
284
+ state[k] ^= (j ^ tmp);
285
+
286
+ j = state[k + 1] ^ state[k + 2];
287
+ j = xtime (j);
288
+
289
+ state[k + 1] ^= (j ^ tmp);
290
+
291
+ j = state[k + 2] ^ state[k + 3];
292
+ j = xtime (j);
293
+
294
+ state[k + 2] ^= (j ^ tmp);
295
+ state[k + 3] = state[k] ^ state[k + 1] ^ state[k + 2] ^ tmp;
296
+ }
297
+ }
298
+
299
+ round_key[0] ^= RC[i];
300
+
301
+ round_key[0] ^= rijndael_sbox[round_key[13]];
302
+ round_key[1] ^= rijndael_sbox[round_key[14]];
303
+ round_key[2] ^= rijndael_sbox[round_key[15]];
304
+ round_key[3] ^= rijndael_sbox[round_key[12]];
305
+
306
+ for (k = 4; k < 16; k++)
307
+ round_key[k] ^= round_key[k - 4];
308
+
309
+ // add_round_key(state, round_key);
310
+
311
+ for (j = 0; j < 16; j++)
312
+ state[j] ^= round_key[j];
313
+ }
314
+ }
@@ -1,7 +1,7 @@
1
- /* ykcrc.c --- Implementation of Yubikey CRC-16 function.
1
+ /* ykcrc.c --- Implementation of YubiKey CRC-16 function.
2
2
  *
3
3
  * Written by Simon Josefsson <simon@josefsson.org>.
4
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
4
+ * Copyright (c) 2006-2012 Yubico AB
5
5
  * All rights reserved.
6
6
  *
7
7
  * Redistribution and use in source and binary forms, with or without
@@ -33,7 +33,7 @@
33
33
  #include "yubikey.h"
34
34
 
35
35
  uint16_t
36
- yubikey_crc16 (const uint8_t *buf, size_t buf_size)
36
+ yubikey_crc16 (const uint8_t * buf, size_t buf_size)
37
37
  {
38
38
  uint16_t m_crc = 0xffff;
39
39
 
@@ -1,7 +1,7 @@
1
1
  /* ykhex.c --- Implementation of hex encoding/decoding
2
2
  *
3
3
  * Written by Simon Josefsson <simon@josefsson.org>.
4
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
4
+ * Copyright (c) 2006-2012 Yubico AB
5
5
  * All rights reserved.
6
6
  *
7
7
  * Redistribution and use in source and binary forms, with or without
@@ -32,8 +32,6 @@
32
32
 
33
33
  #include "yubikey.h"
34
34
 
35
- #include <stdbool.h>
36
-
37
35
  static const char trans[] = "0123456789abcdef";
38
36
 
39
37
  void
@@ -52,8 +50,8 @@ void
52
50
  yubikey_hex_decode (char *dst, const char *src, size_t dstSize)
53
51
  {
54
52
  char b;
55
- bool flag = false;
56
- char *p1;
53
+ int flag = 0;
54
+ const char *p1;
57
55
 
58
56
  for (; *src && dstSize > 0; src++)
59
57
  {
@@ -1,7 +1,7 @@
1
1
  /* ykmodhex.c --- Implementation of modhex encoding/decoding
2
2
  *
3
3
  * Written by Simon Josefsson <simon@josefsson.org>.
4
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
4
+ * Copyright (c) 2006-2012 Yubico AB
5
5
  * All rights reserved.
6
6
  *
7
7
  * Redistribution and use in source and binary forms, with or without
@@ -32,8 +32,6 @@
32
32
 
33
33
  #include "yubikey.h"
34
34
 
35
- #include <stdbool.h>
36
-
37
35
  static const char trans[] = YUBIKEY_MODHEX_MAP;
38
36
 
39
37
  void
@@ -52,8 +50,8 @@ void
52
50
  yubikey_modhex_decode (char *dst, const char *src, size_t dstSize)
53
51
  {
54
52
  char b;
55
- bool flag = false;
56
- char *p1;
53
+ int flag = 0;
54
+ const char *p1;
57
55
 
58
56
  for (; *src && dstSize > 0; src++)
59
57
  {
@@ -1,7 +1,7 @@
1
- /* ykparse.c --- Implementation of Yubikey token parser.
1
+ /* ykparse.c --- Example command line interface for authentication token.
2
2
  *
3
3
  * Written by Simon Josefsson <simon@josefsson.org>.
4
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
4
+ * Copyright (c) 2006-2012 Yubico AB
5
5
  * All rights reserved.
6
6
  *
7
7
  * Redistribution and use in source and binary forms, with or without
@@ -32,14 +32,120 @@
32
32
 
33
33
  #include "yubikey.h"
34
34
 
35
+ #include <stdio.h>
35
36
  #include <stdlib.h>
36
- #include <stdbool.h>
37
+ #include <stdint.h>
38
+ #include <string.h>
37
39
 
38
- void
39
- yubikey_parse (const uint8_t token[32],
40
- const uint8_t key[16], yubikey_token_t out)
40
+ int
41
+ main (int argc, char *argv[])
41
42
  {
42
- memset (out, 0, sizeof (*out));
43
- yubikey_modhex_decode ((void *) out, (char *) token, sizeof (*out));
44
- yubikey_aes_decrypt ((void *) out, key);
43
+ uint8_t buf[128];
44
+ uint8_t key[YUBIKEY_KEY_SIZE];
45
+ char *aeskey, *token;
46
+ yubikey_token_st tok;
47
+
48
+ /* Parse command line parameters. */
49
+ if (argc < 2)
50
+ {
51
+ printf ("Usage: %s <aeskey> <token>\n", argv[0]);
52
+ printf (" AESKEY:\tHex encoded AES-key.\n");
53
+ printf (" TOKEN:\t\tModHex encoded token.\n");
54
+ return EXIT_FAILURE;
55
+ }
56
+
57
+ aeskey = argv[1];
58
+ token = argv[2];
59
+
60
+ if (strlen (aeskey) != 32)
61
+ {
62
+ printf ("error: Hex encoded AES-key must be 32 characters.\n");
63
+ return EXIT_FAILURE;
64
+ }
65
+
66
+ if (strlen (token) > 32)
67
+ {
68
+ printf ("warning: overlong token, ignoring prefix: %.*s\n",
69
+ (int) strlen (token) - 32, token);
70
+ token = token + (strlen (token) - 32);
71
+ }
72
+
73
+ if (strlen (token) != 32)
74
+ {
75
+ printf ("error: ModHex encoded token must be 32 characters.\n");
76
+ return EXIT_FAILURE;
77
+ }
78
+
79
+ /* Debug. */
80
+ printf ("Input:\n");
81
+ printf (" token: %s\n", token);
82
+
83
+ yubikey_modhex_decode ((char *) key, token, YUBIKEY_KEY_SIZE);
84
+
85
+ {
86
+ size_t i;
87
+ printf (" ");
88
+ for (i = 0; i < YUBIKEY_KEY_SIZE; i++)
89
+ printf ("%02x ", key[i] & 0xFF);
90
+ printf ("\n");
91
+ }
92
+
93
+ printf (" aeskey: %s\n", aeskey);
94
+
95
+ yubikey_hex_decode ((char *) key, aeskey, YUBIKEY_KEY_SIZE);
96
+
97
+ {
98
+ size_t i;
99
+ printf (" ");
100
+ for (i = 0; i < YUBIKEY_KEY_SIZE; i++)
101
+ printf ("%02x ", key[i] & 0xFF);
102
+ printf ("\n");
103
+ }
104
+
105
+ /* Pack up dynamic password, decrypt it and verify checksum */
106
+ yubikey_parse ((uint8_t *) token, key, &tok);
107
+
108
+ printf ("Output:\n");
109
+ {
110
+ size_t i;
111
+ printf (" ");
112
+ for (i = 0; i < YUBIKEY_BLOCK_SIZE; i++)
113
+ printf ("%02x ", ((uint8_t *) & tok)[i] & 0xFF);
114
+ printf ("\n");
115
+ }
116
+
117
+ printf ("\nStruct:\n");
118
+ /* Debug */
119
+ {
120
+ size_t i;
121
+ printf (" uid: ");
122
+ for (i = 0; i < YUBIKEY_UID_SIZE; i++)
123
+ printf ("%02x ", tok.uid[i] & 0xFF);
124
+ printf ("\n");
125
+ }
126
+ printf (" counter: %d (0x%04x)\n", tok.ctr, tok.ctr);
127
+ printf (" timestamp (low): %d (0x%04x)\n", tok.tstpl, tok.tstpl);
128
+ printf (" timestamp (high): %d (0x%02x)\n", tok.tstph, tok.tstph);
129
+ printf (" session use: %d (0x%02x)\n", tok.use, tok.use);
130
+ printf (" random: %d (0x%02x)\n", tok.rnd, tok.rnd);
131
+ printf (" crc: %d (0x%04x)\n", tok.crc, tok.crc);
132
+
133
+ printf ("\nDerived:\n");
134
+ printf (" cleaned counter: %d (0x%04x)\n",
135
+ yubikey_counter (tok.ctr), yubikey_counter (tok.ctr));
136
+ yubikey_modhex_encode ((char *) buf, (char *) tok.uid, YUBIKEY_UID_SIZE);
137
+ printf (" modhex uid: %s\n", buf);
138
+ printf (" triggered by caps lock: %s\n",
139
+ yubikey_capslock (tok.ctr) ? "yes" : "no");
140
+ printf (" crc: %04X\n", yubikey_crc16 ((void *) &tok, YUBIKEY_KEY_SIZE));
141
+
142
+ printf (" crc check: ");
143
+ if (yubikey_crc_ok_p ((uint8_t *) & tok))
144
+ {
145
+ printf ("ok\n");
146
+ return EXIT_SUCCESS;
147
+ }
148
+
149
+ printf ("fail\n");
150
+ return EXIT_FAILURE;
45
151
  }
@@ -0,0 +1,52 @@
1
+ /* ykparse.c --- Implementation of YubiKey token parser.
2
+ *
3
+ * Written by Simon Josefsson <simon@josefsson.org>.
4
+ * Copyright (c) 2006-2012 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
+ #include "yubikey.h"
34
+
35
+ #include <stdlib.h>
36
+
37
+ void
38
+ yubikey_parse (const uint8_t token[32],
39
+ const uint8_t key[16], yubikey_token_t out)
40
+ {
41
+ memset (out, 0, sizeof (*out));
42
+ yubikey_modhex_decode ((char *) out, (const char *) token, sizeof (*out));
43
+ yubikey_aes_decrypt ((uint8_t *) out, key);
44
+ }
45
+
46
+ void
47
+ yubikey_generate (yubikey_token_t token,
48
+ const uint8_t key[YUBIKEY_KEY_SIZE], char out[32])
49
+ {
50
+ yubikey_aes_encrypt ((uint8_t *) token, key);
51
+ yubikey_modhex_encode (out, (const char *) token, YUBIKEY_KEY_SIZE);
52
+ }
@@ -1,7 +1,7 @@
1
- /* yubikey.h --- Prototypes for low-level Yubikey OTP functions.
1
+ /* yubikey.h --- Prototypes for low-level YubiKey OTP functions.
2
2
  *
3
3
  * Written by Simon Josefsson <simon@josefsson.org>.
4
- * Copyright (c) 2006, 2007, 2008, 2009 Yubico AB
4
+ * Copyright (c) 2006-2012 Yubico AB
5
5
  * All rights reserved.
6
6
  *
7
7
  * Redistribution and use in source and binary forms, with or without
@@ -36,9 +36,15 @@
36
36
  # include <stdint.h>
37
37
  # include <string.h>
38
38
 
39
+ # ifdef __cplusplus
40
+ extern "C"
41
+ {
42
+ # endif
43
+
39
44
  # define YUBIKEY_BLOCK_SIZE 16
40
45
  # define YUBIKEY_KEY_SIZE 16
41
46
  # define YUBIKEY_UID_SIZE 6
47
+ # define YUBIKEY_OTP_SIZE (2 * YUBIKEY_BLOCK_SIZE)
42
48
 
43
49
  typedef struct
44
50
  {
@@ -63,6 +69,7 @@ typedef yubikey_token_st *yubikey_token_t;
63
69
 
64
70
  /* High-level functions. */
65
71
 
72
+
66
73
  /* Decrypt TOKEN using KEY and store output in OUT structure. Note
67
74
  that there is no error checking whether the output data is valid or
68
75
  not, use yubikey_check_* for that. */
@@ -70,6 +77,11 @@ extern void yubikey_parse (const uint8_t token[YUBIKEY_BLOCK_SIZE],
70
77
  const uint8_t key[YUBIKEY_KEY_SIZE],
71
78
  yubikey_token_t out);
72
79
 
80
+ /* Generate OTP */
81
+ extern void yubikey_generate (yubikey_token_t token,
82
+ const uint8_t key[YUBIKEY_KEY_SIZE],
83
+ char out[YUBIKEY_OTP_SIZE]);
84
+
73
85
  # define yubikey_counter(ctr) ((ctr) & 0x7FFF)
74
86
  # define yubikey_capslock(ctr) ((ctr) & 0x8000)
75
87
  # define yubikey_crc_ok_p(tok) \
@@ -86,15 +98,13 @@ extern void yubikey_parse (const uint8_t token[YUBIKEY_BLOCK_SIZE],
86
98
  must be at least 2*SRCSIZE+1. The output string is always
87
99
  2*SRCSIZE large plus the terminating zero. */
88
100
  extern void yubikey_modhex_encode (char *dst,
89
- const char *src,
90
- size_t srcsize);
101
+ const char *src, size_t srcsize);
91
102
 
92
103
  /* ModHex decode input string SRC of length DSTSIZE/2 into output
93
104
  string DST. The output string DST is always DSTSIZE/2 large plus
94
105
  the terminating zero. */
95
106
  extern void yubikey_modhex_decode (char *dst,
96
- const char *src,
97
- size_t dstsize);
107
+ const char *src, size_t dstsize);
98
108
 
99
109
  /* Hex encode/decode data, same interface as modhex functions. */
100
110
  extern void yubikey_hex_encode (char *dst, const char *src, size_t srcsize);
@@ -114,8 +124,13 @@ extern uint16_t yubikey_crc16 (const uint8_t * buf, size_t buf_size);
114
124
 
115
125
  /* Low-level functions; AES. */
116
126
 
117
- /* AES-decrypt one 16-byte block STATE using the 128-bit KEY, leaving
118
- the decrypted output in the STATE buffer. */
127
+ /* AES-decrypt/encrypt one 16-byte block STATE using the 128-bit KEY,
128
+ leaving the decrypted/encrypted output in the STATE buffer. */
119
129
  extern void yubikey_aes_decrypt (uint8_t * state, const uint8_t * key);
130
+ extern void yubikey_aes_encrypt (uint8_t * state, const uint8_t * key);
131
+
132
+ # ifdef __cplusplus
133
+ }
134
+ # endif
120
135
 
121
136
  #endif