libssh  0.8.5
The SSH library
libmbedcrypto.h
1/*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2017 Sartura d.o.o.
5 *
6 * Author: Juraj Vijtiuk <juraj.vijtiuk@sartura.hr>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24#ifndef LIBMBEDCRYPTO_H_
25#define LIBMBEDCRYPTO_H_
26
27#include "config.h"
28
29#ifdef HAVE_LIBMBEDCRYPTO
30
31#include <mbedtls/md.h>
32#include <mbedtls/bignum.h>
33#include <mbedtls/pk.h>
34#include <mbedtls/cipher.h>
35#include <mbedtls/entropy.h>
36#include <mbedtls/ctr_drbg.h>
37
38typedef mbedtls_md_context_t *SHACTX;
39typedef mbedtls_md_context_t *SHA256CTX;
40typedef mbedtls_md_context_t *SHA384CTX;
41typedef mbedtls_md_context_t *SHA512CTX;
42typedef mbedtls_md_context_t *MD5CTX;
43typedef mbedtls_md_context_t *HMACCTX;
44typedef mbedtls_md_context_t *EVPCTX;
45
46#define SHA_DIGEST_LENGTH 20
47#define SHA_DIGEST_LEN SHA_DIGEST_LENGTH
48#define MD5_DIGEST_LEN 16
49#define SHA256_DIGEST_LENGTH 32
50#define SHA256_DIGEST_LEN SHA256_DIGEST_LENGTH
51#define SHA384_DIGEST_LENGTH 48
52#define SHA384_DIGEST_LEN SHA384_DIGEST_LENGTH
53#define SHA512_DIGEST_LENGTH 64
54#define SHA512_DIGEST_LEN SHA512_DIGEST_LENGTH
55
56#ifndef EVP_MAX_MD_SIZE
57#define EVP_MAX_MD_SIZE 64
58#endif
59
60#define EVP_DIGEST_LEN EVP_MAX_MD_SIZE
61
62typedef mbedtls_mpi *bignum;
63
64/* Constants for curves */
65#define NID_mbedtls_nistp256 0
66#define NID_mbedtls_nistp384 1
67#define NID_mbedtls_nistp521 2
68
69struct mbedtls_ecdsa_sig {
70 bignum r;
71 bignum s;
72};
73
74bignum ssh_mbedcry_bn_new(void);
75void ssh_mbedcry_bn_free(bignum num);
76char *ssh_mbedcry_bn2num(bignum num, int radix);
77int ssh_mbedcry_rand(bignum rnd, int bits, int top, int bottom);
78int ssh_mbedcry_is_bit_set(bignum num, size_t pos);
79
80#define bignum_new() ssh_mbedcry_bn_new()
81#define bignum_safe_free(num) do { \
82 if ((num) != NULL) { \
83 ssh_mbedcry_bn_free(num); \
84 (num)=NULL; \
85 } \
86 } while(0)
87#define bignum_set_word(bn, n) mbedtls_mpi_lset(bn, n) /* TODO fix
88 overflow/underflow */
89#define bignum_bin2bn(data, datalen, bn) mbedtls_mpi_read_binary(bn, data, \
90 datalen)
91#define bignum_bn2dec(num) ssh_mbedcry_bn2num(num, 10)
92#define bignum_dec2bn(data, bn) mbedtls_mpi_read_string(bn, 10, data)
93#define bignum_bn2hex(num) ssh_mbedcry_bn2num(num, 16)
94#define bignum_rand(rnd, bits) ssh_mbedcry_rand((rnd), (bits), 0, 1)
95#define bignum_mod_exp(dest, generator, exp, modulo, ctx) \
96 mbedtls_mpi_exp_mod(dest, generator, exp, modulo, NULL)
97#define bignum_num_bytes(num) mbedtls_mpi_size(num)
98#define bignum_num_bits(num) mbedtls_mpi_bitlen(num)
99#define bignum_is_bit_set(num, bit) ssh_mbedcry_is_bit_set(num, bit)
100#define bignum_bn2bin(num, ptr) mbedtls_mpi_write_binary(num, ptr, \
101 mbedtls_mpi_size(num))
102#define bignum_cmp(num1, num2) mbedtls_mpi_cmp_mpi(num1, num2)
103
104mbedtls_ctr_drbg_context *ssh_get_mbedtls_ctr_drbg_context(void);
105
106int ssh_mbedtls_random(void *where, int len, int strong);
107
108ssh_string make_ecpoint_string(const mbedtls_ecp_group *g, const
109 mbedtls_ecp_point *p);
110
111#endif /* HAVE_LIBMBEDCRYPTO */
112#endif /* LIBMBEDCRYPTO_H_ */
Definition: string.h:29