]> git.zerfleddert.de Git - proxmark3-svn/blob - client/emv/crypto.c
c8ced6d433443618598fd769b53b979b6fcc0b72
[proxmark3-svn] / client / emv / crypto.c
1 /*
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2015 Dmitry Eremin-Solenikov
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 #include "crypto.h"
21 #include "crypto_backend.h"
22
23 #include <string.h>
24
25 static struct crypto_backend *crypto_backend;
26
27 static bool crypto_init(void)
28 {
29 if (crypto_backend)
30 return true;
31
32 crypto_backend = crypto_polarssl_init();
33
34 if (!crypto_backend)
35 return false;
36
37 return true;
38 }
39
40 struct crypto_hash *crypto_hash_open(enum crypto_algo_hash hash)
41 {
42 struct crypto_hash *ch;
43
44 if (!crypto_init())
45 return NULL;
46
47 ch = crypto_backend->hash_open(hash);
48 if (ch)
49 ch->algo = hash;
50
51 return ch;
52 }
53
54 void crypto_hash_close(struct crypto_hash *ch)
55 {
56 ch->close(ch);
57 }
58
59 void crypto_hash_write(struct crypto_hash *ch, const unsigned char *buf, size_t len)
60 {
61 ch->write(ch, buf, len);
62 }
63
64 unsigned char *crypto_hash_read(struct crypto_hash *ch)
65 {
66 return ch->read(ch);
67 }
68
69 size_t crypto_hash_get_size(const struct crypto_hash *ch)
70 {
71 return ch->get_size(ch);
72 }
73
74 struct crypto_pk *crypto_pk_open(enum crypto_algo_pk pk, ...)
75 {
76 struct crypto_pk *cp;
77 va_list vl;
78
79 if (!crypto_init())
80 return NULL;
81
82 va_start(vl, pk);
83 cp = crypto_backend->pk_open(pk, vl);
84 va_end(vl);
85
86 if (cp)
87 cp->algo = pk;
88
89 return cp;
90 }
91
92 struct crypto_pk *crypto_pk_open_priv(enum crypto_algo_pk pk, ...)
93 {
94 struct crypto_pk *cp;
95 va_list vl;
96
97 if (!crypto_init())
98 return NULL;
99
100 if (!crypto_backend->pk_open_priv)
101 return NULL;
102
103 va_start(vl, pk);
104 cp = crypto_backend->pk_open_priv(pk, vl);
105 va_end(vl);
106
107 if (cp)
108 cp->algo = pk;
109
110 return cp;
111 }
112
113 struct crypto_pk *crypto_pk_genkey(enum crypto_algo_pk pk, ...)
114 {
115 struct crypto_pk *cp;
116 va_list vl;
117
118 if (!crypto_init())
119 return NULL;
120
121 if (!crypto_backend->pk_genkey)
122 return NULL;
123
124 va_start(vl, pk);
125 cp = crypto_backend->pk_genkey(pk, vl);
126 va_end(vl);
127
128 if (cp)
129 cp->algo = pk;
130
131 return cp;
132 }
133
134 void crypto_pk_close(struct crypto_pk *cp)
135 {
136 cp->close(cp);
137 }
138
139 unsigned char *crypto_pk_encrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen)
140 {
141 return cp->encrypt(cp, buf, len, clen);
142 }
143
144 unsigned char *crypto_pk_decrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen)
145 {
146 if (!cp->decrypt) {
147 *clen = 0;
148
149 return NULL;
150 }
151
152 return cp->decrypt(cp, buf, len, clen);
153 }
154
155 enum crypto_algo_pk crypto_pk_get_algo(const struct crypto_pk *cp)
156 {
157 if (!cp)
158 return PK_INVALID;
159
160 return cp->algo;
161 }
162
163 size_t crypto_pk_get_nbits(const struct crypto_pk *cp)
164 {
165 if (!cp->get_nbits)
166 return 0;
167
168 return cp->get_nbits(cp);
169 }
170
171 unsigned char *crypto_pk_get_parameter(const struct crypto_pk *cp, unsigned param, size_t *plen)
172 {
173 *plen = 0;
174
175 if (!cp->get_parameter)
176 return NULL;
177
178 return cp->get_parameter(cp, param, plen);
179 }
Impressum, Datenschutz