Coverage Report

Created: 2025-04-24 06:18

/src/hostap/src/crypto/crypto_internal-rsa.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Crypto wrapper for internal crypto implementation - RSA parts
3
 * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
4
 *
5
 * This software may be distributed under the terms of the BSD license.
6
 * See README for more details.
7
 */
8
9
#include "includes.h"
10
11
#include "common.h"
12
#include "crypto.h"
13
#include "tls/rsa.h"
14
#include "tls/pkcs1.h"
15
#include "tls/pkcs8.h"
16
17
/* Stub structures; these are just typecast to struct crypto_rsa_key */
18
struct crypto_public_key;
19
struct crypto_private_key;
20
21
22
struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
23
804
{
24
804
  return (struct crypto_public_key *)
25
804
    crypto_rsa_import_public_key(key, len);
26
804
}
27
28
29
struct crypto_public_key *
30
crypto_public_key_import_parts(const u8 *n, size_t n_len,
31
             const u8 *e, size_t e_len)
32
0
{
33
0
  return (struct crypto_public_key *)
34
0
    crypto_rsa_import_public_key_parts(n, n_len, e, e_len);
35
0
}
36
37
38
struct crypto_private_key * crypto_private_key_import(const u8 *key,
39
                  size_t len,
40
                  const char *passwd)
41
0
{
42
0
  struct crypto_private_key *res;
43
44
  /* First, check for possible PKCS #8 encoding */
45
0
  res = pkcs8_key_import(key, len);
46
0
  if (res)
47
0
    return res;
48
49
0
  if (passwd) {
50
    /* Try to parse as encrypted PKCS #8 */
51
0
    res = pkcs8_enc_key_import(key, len, passwd);
52
0
    if (res)
53
0
      return res;
54
0
  }
55
56
  /* Not PKCS#8, so try to import PKCS #1 encoded RSA private key */
57
0
  wpa_printf(MSG_DEBUG, "Trying to parse PKCS #1 encoded RSA private "
58
0
       "key");
59
0
  return (struct crypto_private_key *)
60
0
    crypto_rsa_import_private_key(key, len);
61
0
}
62
63
64
struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
65
                   size_t len)
66
2.29k
{
67
  /* No X.509 support in crypto_internal.c */
68
2.29k
  return NULL;
69
2.29k
}
70
71
72
int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
73
          const u8 *in, size_t inlen,
74
          u8 *out, size_t *outlen)
75
386
{
76
386
  return pkcs1_encrypt(2, (struct crypto_rsa_key *) key,
77
386
           0, in, inlen, out, outlen);
78
386
}
79
80
81
int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
82
           const u8 *in, size_t inlen,
83
           u8 *out, size_t *outlen)
84
0
{
85
0
  return pkcs1_v15_private_key_decrypt((struct crypto_rsa_key *) key,
86
0
               in, inlen, out, outlen);
87
0
}
88
89
90
int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
91
          const u8 *in, size_t inlen,
92
          u8 *out, size_t *outlen)
93
0
{
94
0
  return pkcs1_encrypt(1, (struct crypto_rsa_key *) key,
95
0
           1, in, inlen, out, outlen);
96
0
}
97
98
99
void crypto_public_key_free(struct crypto_public_key *key)
100
5.44k
{
101
5.44k
  crypto_rsa_free((struct crypto_rsa_key *) key);
102
5.44k
}
103
104
105
void crypto_private_key_free(struct crypto_private_key *key)
106
0
{
107
0
  crypto_rsa_free((struct crypto_rsa_key *) key);
108
0
}
109
110
111
int crypto_public_key_decrypt_pkcs1(struct crypto_public_key *key,
112
            const u8 *crypt, size_t crypt_len,
113
            u8 *plain, size_t *plain_len)
114
270
{
115
270
  return pkcs1_decrypt_public_key((struct crypto_rsa_key *) key,
116
270
          crypt, crypt_len, plain, plain_len);
117
270
}