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
yubikey_parse(const uint8_t token[32],const uint8_t key[16],yubikey_token_t out)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
yubikey_generate(yubikey_token_t token,const uint8_t key[YUBIKEY_KEY_SIZE],char out[32])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 }
53