From a67d911d4abca595d34774105fb055ac8a711533 Mon Sep 17 00:00:00 2001 From: Nikos Mavrogiannopoulos Date: Fri, 14 Jul 2017 09:10:11 +0200 Subject: str: added function to append fixed-size MPI This is used in TLS 1.3 which introduces a new MPI over-the-wire format. Signed-off-by: Nikos Mavrogiannopoulos --- lib/str.c | 37 +++++++++++++++++++++++++++++++++++++ lib/str.h | 4 ++++ 2 files changed, 41 insertions(+) diff --git a/lib/str.c b/lib/str.c index e10ecd4802..1c87a277ab 100644 --- a/lib/str.c +++ b/lib/str.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2002-2012 Free Software Foundation, Inc. + * Copyright (C) 2016-2017 Red Hat, Inc. * * Author: Nikos Mavrogiannopoulos * @@ -869,6 +870,42 @@ int _gnutls_buffer_append_mpi(gnutls_buffer_st * buf, int pfx_size, return ret; } +/* Appends an MPI of fixed-size in bytes left-padded with zeros if necessary */ +int _gnutls_buffer_append_fixed_mpi(gnutls_buffer_st * buf, + bigint_t mpi, unsigned size) +{ + gnutls_datum_t dd; + unsigned pad, i; + int ret; + + ret = _gnutls_mpi_dprint(mpi, &dd); + if (ret < 0) + return gnutls_assert_val(ret); + + if (size < dd.size) { + ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); + goto cleanup; + } + + pad = size - dd.size; + for (i=0;i