2017-12-24 17:26:47 +08:00
|
|
|
/*
|
|
|
|
* libopenemv - a library to work with EMV family of smart cards
|
|
|
|
* Copyright (C) 2015 Dmitry Eremin-Solenikov
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "crypto_backend.h"
|
|
|
|
|
|
|
|
static struct crypto_backend *crypto_backend;
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
static bool crypto_init(void) {
|
2019-03-10 06:35:06 +08:00
|
|
|
if (crypto_backend)
|
|
|
|
return true;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
crypto_backend = crypto_polarssl_init();
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_backend)
|
|
|
|
return false;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return true;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
struct crypto_hash *crypto_hash_open(enum crypto_algo_hash hash) {
|
2019-03-10 06:35:06 +08:00
|
|
|
struct crypto_hash *ch;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_init())
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
ch = crypto_backend->hash_open(hash);
|
|
|
|
if (ch)
|
|
|
|
ch->algo = hash;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return ch;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void crypto_hash_close(struct crypto_hash *ch) {
|
2019-03-10 06:35:06 +08:00
|
|
|
ch->close(ch);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void crypto_hash_write(struct crypto_hash *ch, const unsigned char *buf, size_t len) {
|
2019-03-10 06:35:06 +08:00
|
|
|
ch->write(ch, buf, len);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
unsigned char *crypto_hash_read(struct crypto_hash *ch) {
|
2019-03-10 06:35:06 +08:00
|
|
|
return ch->read(ch);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
size_t crypto_hash_get_size(const struct crypto_hash *ch) {
|
2019-03-10 06:35:06 +08:00
|
|
|
return ch->get_size(ch);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
struct crypto_pk *crypto_pk_open(enum crypto_algo_pk pk, ...) {
|
2019-03-10 06:35:06 +08:00
|
|
|
struct crypto_pk *cp;
|
|
|
|
va_list vl;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_init())
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
va_start(vl, pk);
|
|
|
|
cp = crypto_backend->pk_open(pk, vl);
|
|
|
|
va_end(vl);
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (cp)
|
|
|
|
cp->algo = pk;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
struct crypto_pk *crypto_pk_open_priv(enum crypto_algo_pk pk, ...) {
|
2019-03-10 06:35:06 +08:00
|
|
|
struct crypto_pk *cp;
|
|
|
|
va_list vl;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_init())
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_backend->pk_open_priv)
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
va_start(vl, pk);
|
|
|
|
cp = crypto_backend->pk_open_priv(pk, vl);
|
|
|
|
va_end(vl);
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (cp)
|
|
|
|
cp->algo = pk;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
struct crypto_pk *crypto_pk_genkey(enum crypto_algo_pk pk, ...) {
|
2019-03-10 06:35:06 +08:00
|
|
|
struct crypto_pk *cp;
|
|
|
|
va_list vl;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_init())
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!crypto_backend->pk_genkey)
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
va_start(vl, pk);
|
|
|
|
cp = crypto_backend->pk_genkey(pk, vl);
|
|
|
|
va_end(vl);
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (cp)
|
|
|
|
cp->algo = pk;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
void crypto_pk_close(struct crypto_pk *cp) {
|
2019-03-10 06:35:06 +08:00
|
|
|
cp->close(cp);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
unsigned char *crypto_pk_encrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen) {
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp->encrypt(cp, buf, len, clen);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
unsigned char *crypto_pk_decrypt(const struct crypto_pk *cp, const unsigned char *buf, size_t len, size_t *clen) {
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!cp->decrypt) {
|
|
|
|
*clen = 0;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp->decrypt(cp, buf, len, clen);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
enum crypto_algo_pk crypto_pk_get_algo(const struct crypto_pk *cp) {
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!cp)
|
|
|
|
return PK_INVALID;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp->algo;
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
size_t crypto_pk_get_nbits(const struct crypto_pk *cp) {
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!cp->get_nbits)
|
|
|
|
return 0;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp->get_nbits(cp);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|
|
|
|
|
2019-03-10 18:20:22 +08:00
|
|
|
unsigned char *crypto_pk_get_parameter(const struct crypto_pk *cp, unsigned param, size_t *plen) {
|
2019-03-10 06:35:06 +08:00
|
|
|
*plen = 0;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
if (!cp->get_parameter)
|
|
|
|
return NULL;
|
2017-12-24 17:26:47 +08:00
|
|
|
|
2019-03-10 06:35:06 +08:00
|
|
|
return cp->get_parameter(cp, param, plen);
|
2017-12-24 17:26:47 +08:00
|
|
|
}
|