textual
[RRG-proxmark3.git] / client / src / emv / crypto.c
blobf7fc60c8086645d27a438a03748f35708474b34d
1 /*
2 * libopenemv - a library to work with EMV family of smart cards
3 * Copyright (C) 2015 Dmitry Eremin-Solenikov
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.
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.
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
20 #include "crypto.h"
21 #include "crypto_backend.h"
23 static struct crypto_backend *crypto_backend;
25 static bool crypto_init(void) {
26 if (crypto_backend)
27 return true;
29 crypto_backend = crypto_polarssl_init();
31 if (!crypto_backend)
32 return false;
34 return true;
37 struct crypto_hash *crypto_hash_open(enum crypto_algo_hash hash) {
38 struct crypto_hash *ch;
40 if (!crypto_init())
41 return NULL;
43 ch = crypto_backend->hash_open(hash);
44 if (ch)
45 ch->algo = hash;
47 return ch;
50 void crypto_hash_close(struct crypto_hash *ch) {
51 ch->close(ch);
54 void crypto_hash_write(struct crypto_hash *ch, const unsigned char *buf, size_t len) {
55 ch->write(ch, buf, len);
58 unsigned char *crypto_hash_read(struct crypto_hash *ch) {
59 return ch->read(ch);
62 size_t crypto_hash_get_size(const struct crypto_hash *ch) {
63 return ch->get_size(ch);
66 struct crypto_pk *crypto_pk_open(enum crypto_algo_pk pk, ...) {
67 struct crypto_pk *cp;
68 va_list vl;
70 if (!crypto_init())
71 return NULL;
73 va_start(vl, pk);
74 cp = crypto_backend->pk_open(pk, vl);
75 va_end(vl);
77 if (cp)
78 cp->algo = pk;
80 return cp;
83 struct crypto_pk *crypto_pk_open_priv(enum crypto_algo_pk pk, ...) {
84 struct crypto_pk *cp;
85 va_list vl;
87 if (!crypto_init())
88 return NULL;
90 if (!crypto_backend->pk_open_priv)
91 return NULL;
93 va_start(vl, pk);
94 cp = crypto_backend->pk_open_priv(pk, vl);
95 va_end(vl);
97 if (cp)
98 cp->algo = pk;
100 return cp;
103 struct crypto_pk *crypto_pk_genkey(enum crypto_algo_pk pk, ...) {
104 struct crypto_pk *cp;
105 va_list vl;
107 if (!crypto_init())
108 return NULL;
110 if (!crypto_backend->pk_genkey)
111 return NULL;
113 va_start(vl, pk);
114 cp = crypto_backend->pk_genkey(pk, vl);
115 va_end(vl);
117 if (cp)
118 cp->algo = pk;
120 return cp;
123 void crypto_pk_close(struct crypto_pk *cp) {
124 cp->close(cp);
127 unsigned char *crypto_pk_encrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen) {
128 return cp->encrypt(cp, buf, len, clen);
131 unsigned char *crypto_pk_decrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen) {
132 if (!cp->decrypt) {
133 *clen = 0;
135 return NULL;
138 return cp->decrypt(cp, buf, len, clen);
141 enum crypto_algo_pk crypto_pk_get_algo(const struct crypto_pk *cp) {
142 if (!cp)
143 return PK_INVALID;
145 return cp->algo;
148 size_t crypto_pk_get_nbits(const struct crypto_pk *cp) {
149 if (!cp->get_nbits)
150 return 0;
152 return cp->get_nbits(cp);
155 unsigned char *crypto_pk_get_parameter(const struct crypto_pk *cp, unsigned param, size_t *plen) {
156 *plen = 0;
158 if (!cp->get_parameter)
159 return NULL;
161 return cp->get_parameter(cp, param, plen);