Skip to content

Commit 45f6b35

Browse files
committed
Fix RSA context initialization in rsa_alloc_wrap() plus some cleanups.
Print warnings on TTLS_ERR_BAD_INPUT_DATA in ttls_encrypt(): we have net reatelimited warnings, so this won't cause serious logging problems.
1 parent 618e7c6 commit 45f6b35

File tree

7 files changed

+98
-183
lines changed

7 files changed

+98
-183
lines changed

tls/config.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,6 @@
3535
#define ttls_calloc(n, s) kzalloc((n) * (s), GFP_ATOMIC)
3636
#define ttls_free(p) kfree(p)
3737

38-
/**
39-
* \def TTLS_AES_ALT
40-
*
41-
* TTLS__MODULE_NAME__ALT: Uncomment a macro to let mbed TLS use your
42-
* alternate core implementation of a symmetric crypto, an arithmetic or hash
43-
* module (e.g. platform specific assembly optimized implementations). Keep
44-
* in mind that the function prototypes should remain the same.
45-
*
46-
* This replaces the whole module. If you only want to replace one of the
47-
* functions, use one of the TTLS__FUNCTION_NAME__ALT flags.
48-
*
49-
* Example: In case you uncomment TTLS_AES_ALT, mbed TLS will no longer
50-
* provide the "struct ttls_aes_context" definition and omit the base
51-
* function declarations and implementations. "aes_alt.h" will be included from
52-
* "aes.h" to include the new function definitions.
53-
*
54-
* Uncomment a macro to enable alternate implementation of the corresponding
55-
* module.
56-
*
57-
* \warning MD2, MD4, MD5, DES and SHA-1 are considered weak and their
58-
* use constitutes a security risk. If possible, we recommend
59-
* avoiding dependencies on them, and considering stronger message
60-
* digests and ciphers instead.
61-
*
62-
*/
63-
//#define TTLS_RSA_ALT
6438
/*
6539
* When replacing the elliptic curve module, pleace consider, that it is
6640
* implemented with two .c files:

tls/ecdsa.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@
5757
*/
5858
typedef ttls_ecp_keypair ttls_ecdsa_context;
5959

60-
#ifdef __cplusplus
61-
extern "C" {
62-
#endif
63-
6460
/**
6561
* \brief This function computes the ECDSA signature of a
6662
* previously-hashed message.
@@ -189,8 +185,4 @@ void ttls_ecdsa_init(ttls_ecdsa_context *ctx);
189185
*/
190186
void ttls_ecdsa_free(ttls_ecdsa_context *ctx);
191187

192-
#ifdef __cplusplus
193-
}
194-
#endif
195-
196188
#endif /* ecdsa.h */

tls/pk_wrap.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ static int rsa_check_pair_wrap(const void *pub, const void *prv)
113113
static void *
114114
rsa_alloc_wrap(void)
115115
{
116-
void *ctx = kzalloc(sizeof(ttls_rsa_context), GFP_ATOMIC);
117-
if (!ctx)
116+
void *ctx;
117+
118+
might_sleep();
119+
if ((ctx = kzalloc(sizeof(ttls_rsa_context), GFP_KERNEL)))
118120
ttls_rsa_init((ttls_rsa_context *)ctx, 0, 0);
119121

120122
return ctx;

tls/rsa.c

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
*
44
* The RSA public-key cryptosystem.
55
*
6+
* TODO #1064: The Linux crypt API already has RSA implementation, so probably
7+
* the stuff below should be just thrown out. Fallback to GPU is necessary
8+
* however, so maybe not... A careful rethinking is requiered.
9+
*
10+
* The following sources were referenced in the design of this implementation
11+
* of the RSA algorithm:
12+
*
13+
* [1] A method for obtaining digital signatures and public-key cryptosystems
14+
* R Rivest, A Shamir, and L Adleman
15+
* http://people.csail.mit.edu/rivest/pubs.html#RSA78
16+
*
17+
* [2] Handbook of Applied Cryptography - 1997, Chapter 8
18+
* Menezes, van Oorschot and Vanstone
19+
*
20+
* [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
21+
* Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
22+
* Stefan Mangard
23+
* https://arxiv.org/abs/1702.08719v2
24+
*
625
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
726
* Copyright (C) 2015-2019 Tempesta Technologies, Inc.
827
* SPDX-License-Identifier: GPL-2.0
@@ -21,23 +40,6 @@
2140
* with this program; if not, write to the Free Software Foundation, Inc.,
2241
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2342
*/
24-
/*
25-
* The following sources were referenced in the design of this implementation
26-
* of the RSA algorithm:
27-
*
28-
* [1] A method for obtaining digital signatures and public-key cryptosystems
29-
* R Rivest, A Shamir, and L Adleman
30-
* http://people.csail.mit.edu/rivest/pubs.html#RSA78
31-
*
32-
* [2] Handbook of Applied Cryptography - 1997, Chapter 8
33-
* Menezes, van Oorschot and Vanstone
34-
*
35-
* [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
36-
* Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
37-
* Stefan Mangard
38-
* https://arxiv.org/abs/1702.08719v2
39-
*
40-
*/
4143
#include <linux/random.h>
4244

4345
#include "lib/str.h"
@@ -48,8 +50,6 @@
4850
#include "tls_internal.h"
4951
#include "oid.h"
5052

51-
#if !defined(TTLS_RSA_ALT)
52-
5353
/* constant-time buffer comparison */
5454
static inline int ttls_safer_memcmp(const void *a, const void *b, size_t n)
5555
{
@@ -429,36 +429,44 @@ int ttls_rsa_export_crt(const ttls_rsa_context *ctx,
429429
return 0;
430430
}
431431

432-
/*
433-
* Initialize an RSA context
434-
*/
435-
void ttls_rsa_init(ttls_rsa_context *ctx,
436-
int padding,
437-
int hash_id)
438-
{
439-
memset(ctx, 0, sizeof(ttls_rsa_context));
440-
441-
ttls_rsa_set_padding(ctx, padding, hash_id);
442-
443-
spin_lock_init(&ctx->mutex);
444-
}
445-
446-
/*
447-
* Set padding for an existing RSA context
432+
/**
433+
* Initialize an RSA context.
434+
*
435+
* TODO #1064: Set padding to #TTLS_RSA_PKCS_V21 for the RSAES-OAEP encryption
436+
* scheme and the RSASSA-PSS signature scheme. The choice of padding mode is
437+
* strictly enforced for private key operations, since there might be security
438+
* concerns in mixing padding modes. For public key operations it is a default
439+
* value, which can be overriden by calling specific rsa_rsaes_xxx or
440+
* rsa_rsassa_xxx functions.
441+
*
442+
* The hash selected in hash_id is always used for OEAP encryption. For PSS
443+
* signatures, it is always used for making signatures, but can be overriden
444+
* for verifying them. If set to TTLS_MD_NONE, it is always overriden.
448445
*/
449-
void ttls_rsa_set_padding(ttls_rsa_context *ctx, int padding, int hash_id)
446+
void
447+
ttls_rsa_init(ttls_rsa_context *ctx, int padding, int hash_id)
450448
{
449+
/*
450+
* TODO Select padding mode: TTLS_RSA_PKCS_V15 or TTLS_RSA_PKCS_V21.
451+
*/
451452
ctx->padding = padding;
453+
/*
454+
* TODO The hash identifier of ttls_md_type_t type, if padding is
455+
* TTLS_RSA_PKCS_V21. The hash_id parameter is ignored when using
456+
* TTLS_RSA_PKCS_V15 padding.
457+
*/
452458
ctx->hash_id = hash_id;
459+
460+
spin_lock_init(&ctx->mutex);
453461
}
454462

455-
/*
456-
* Get length in bytes of RSA modulus
463+
/**
464+
* Get length in bytes of RSA modulus.
457465
*/
458-
459-
size_t ttls_rsa_get_len(const ttls_rsa_context *ctx)
466+
size_t
467+
ttls_rsa_get_len(const ttls_rsa_context *ctx)
460468
{
461-
return(ctx->len);
469+
return ctx->len;
462470
}
463471

464472

@@ -2013,5 +2021,3 @@ void ttls_rsa_free(ttls_rsa_context *ctx)
20132021
ttls_mpi_free(&ctx->DP);
20142022
#endif /* TTLS_RSA_NO_CRT */
20152023
}
2016-
2017-
#endif /* !TTLS_RSA_ALT */

tls/rsa.h

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -106,35 +106,7 @@ typedef struct
106106
}
107107
ttls_rsa_context;
108108

109-
/**
110-
* \brief This function initializes an RSA context.
111-
*
112-
* \note Set padding to #TTLS_RSA_PKCS_V21 for the RSAES-OAEP
113-
* encryption scheme and the RSASSA-PSS signature scheme.
114-
*
115-
* \param ctx The RSA context to initialize.
116-
* \param padding Selects padding mode: #TTLS_RSA_PKCS_V15 or
117-
* #TTLS_RSA_PKCS_V21.
118-
* \param hash_id The hash identifier of #ttls_md_type_t type, if
119-
* \p padding is #TTLS_RSA_PKCS_V21.
120-
*
121-
* \note The \p hash_id parameter is ignored when using
122-
* #TTLS_RSA_PKCS_V15 padding.
123-
*
124-
* \note The choice of padding mode is strictly enforced for private key
125-
* operations, since there might be security concerns in
126-
* mixing padding modes. For public key operations it is
127-
* a default value, which can be overriden by calling specific
128-
* \c rsa_rsaes_xxx or \c rsa_rsassa_xxx functions.
129-
*
130-
* \note The hash selected in \p hash_id is always used for OEAP
131-
* encryption. For PSS signatures, it is always used for
132-
* making signatures, but can be overriden for verifying them.
133-
* If set to #TTLS_MD_NONE, it is always overriden.
134-
*/
135-
void ttls_rsa_init(ttls_rsa_context *ctx,
136-
int padding,
137-
int hash_id);
109+
void ttls_rsa_init(ttls_rsa_context *ctx, int padding, int hash_id);
138110

139111
/**
140112
* \brief This function imports a set of core parameters into an
@@ -350,18 +322,6 @@ int ttls_rsa_export_raw(const ttls_rsa_context *ctx,
350322
int ttls_rsa_export_crt(const ttls_rsa_context *ctx,
351323
ttls_mpi *DP, ttls_mpi *DQ, ttls_mpi *QP);
352324

353-
/**
354-
* \brief This function sets padding for an already initialized RSA
355-
* context. See ttls_rsa_init() for details.
356-
*
357-
* \param ctx The RSA context to be set.
358-
* \param padding Selects padding mode: #TTLS_RSA_PKCS_V15 or
359-
* #TTLS_RSA_PKCS_V21.
360-
* \param hash_id The #TTLS_RSA_PKCS_V21 hash identifier.
361-
*/
362-
void ttls_rsa_set_padding(ttls_rsa_context *ctx, int padding,
363-
int hash_id);
364-
365325
/**
366326
* \brief This function retrieves the length of RSA modulus in Bytes.
367327
*

tls/rsa_internal.h

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,52 @@
11
/**
2-
* \file rsa_internal.h
3-
*
4-
* \brief Context-independent RSA helper functions
5-
*/
6-
/*
7-
* Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
8-
* SPDX-License-Identifier: GPL-2.0
9-
*
10-
* This program is free software; you can redistribute it and/or modify
11-
* it under the terms of the GNU General Public License as published by
12-
* the Free Software Foundation; either version 2 of the License, or
13-
* (at your option) any later version.
14-
*
15-
* This program is distributed in the hope that it will be useful,
16-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18-
* GNU General Public License for more details.
19-
*
20-
* You should have received a copy of the GNU General Public License along
21-
* with this program; if not, write to the Free Software Foundation, Inc.,
22-
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23-
*
24-
* This file is part of mbed TLS (https://tls.mbed.org)
25-
*
26-
*
27-
* This file declares some RSA-related helper functions useful when
28-
* implementing the RSA interface. They are public and provided in a
29-
* separate compilation unit in order to make it easy for designers of
30-
* alternative RSA implementations to use them in their code, as it is
31-
* conceived that the functionality they provide will be necessary
32-
* for most complete implementations.
33-
*
34-
* End-users of Mbed TLS not intending to re-implement the RSA functionality
35-
* are not expected to get into the need of making use of these functions directly,
36-
* but instead should be able to use the functions declared in rsa.h.
37-
*
38-
* There are two classes of helper functions:
39-
* (1) Parameter-generating helpers. These are:
40-
* - ttls_rsa_deduce_primes
41-
* - ttls_rsa_deduce_private_exponent
42-
* - ttls_rsa_deduce_crt
43-
* Each of these functions takes a set of core RSA parameters
44-
* and generates some other, or CRT related parameters.
45-
* (2) Parameter-checking helpers. These are:
46-
* - ttls_rsa_validate_params
47-
* - ttls_rsa_validate_crt
48-
* They take a set of core or CRT related RSA parameters
49-
* and check their validity.
50-
*
2+
* Tempesta TLS
3+
*
4+
* Context-independent RSA helper functions.
5+
*
6+
* This file declares some RSA-related helper functions useful when
7+
* implementing the RSA interface. They are public and provided in a
8+
* separate compilation unit in order to make it easy for designers of
9+
* alternative RSA implementations to use them in their code, as it is
10+
* conceived that the functionality they provide will be necessary
11+
* for most complete implementations.
12+
*
13+
* There are two classes of helper functions:
14+
* (1) Parameter-generating helpers. These are:
15+
* - ttls_rsa_deduce_primes
16+
* - ttls_rsa_deduce_private_exponent
17+
* - ttls_rsa_deduce_crt
18+
* Each of these functions takes a set of core RSA parameters
19+
* and generates some other, or CRT related parameters.
20+
* (2) Parameter-checking helpers. These are:
21+
* - ttls_rsa_validate_params
22+
* - ttls_rsa_validate_crt
23+
* They take a set of core or CRT related RSA parameters
24+
* and check their validity.
25+
*
26+
* Copyright (C) 2006-2017, ARM Limited, All Rights Reserved
27+
* Copyright (C) 2015-2019 Tempesta Technologies, Inc.
28+
* SPDX-License-Identifier: GPL-2.0
29+
*
30+
* This program is free software; you can redistribute it and/or modify
31+
* it under the terms of the GNU General Public License as published by
32+
* the Free Software Foundation; either version 2 of the License, or
33+
* (at your option) any later version.
34+
*
35+
* This program is distributed in the hope that it will be useful,
36+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
37+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38+
* GNU General Public License for more details.
39+
*
40+
* You should have received a copy of the GNU General Public License along
41+
* with this program; if not, write to the Free Software Foundation, Inc.,
42+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5143
*/
52-
5344
#ifndef TTLS_RSA_INTERNAL_H
5445
#define TTLS_RSA_INTERNAL_H
5546

56-
#if !defined(TTLS_CONFIG_FILE)
5747
#include "config.h"
58-
#else
59-
#include TTLS_CONFIG_FILE
60-
#endif
61-
6248
#include "bignum.h"
6349

64-
#ifdef __cplusplus
65-
extern "C" {
66-
#endif
67-
68-
6950
/**
7051
* \brief Compute RSA prime moduli P, Q from public modulus N=PQ
7152
* and a pair of private and public key.

tls/ttls.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ ttls_encrypt(TlsCtx *tls, struct sg_table *sgt)
781781

782782
WARN_ON_ONCE(!ttls_xfrm_ready(tls));
783783
if (io->msglen > TLS_MAX_PAYLOAD_SIZE) {
784-
T_DBG("%s record content %u too large, maximum %lu\n",
785-
__func__, io->msglen, TLS_MAX_PAYLOAD_SIZE);
784+
T_WARN("cannot encrypt a record: content %u too large,"
785+
" maximum %lu\n", io->msglen, TLS_MAX_PAYLOAD_SIZE);
786786
return TTLS_ERR_BAD_INPUT_DATA;
787787
}
788788

@@ -810,7 +810,7 @@ ttls_encrypt(TlsCtx *tls, struct sg_table *sgt)
810810
min_t(size_t, 256, io->msglen + TLS_HEADER_SIZE));
811811

812812
if ((r = crypto_aead_encrypt(req))) {
813-
T_DBG2("encrypt failed: %d\n", r);
813+
T_WARN("AEAD encryption failed: %d\n", r);
814814
goto err;
815815
}
816816
T_DBG3_SL("encrypted buf (first 64 bytes)", sgt->sgl, sgt->nents, 0,

0 commit comments

Comments
 (0)