rsa.h
Go to the documentation of this file.
1 /**
2  * @file rsa.h
3  * @brief RSA public-key cryptography standard
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneCrypto Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _RSA_H
30 #define _RSA_H
31 
32 //Dependencies
33 #include "core/crypto.h"
34 #include "mpi/mpi.h"
35 
36 //C++ guard
37 #ifdef __cplusplus
38  extern "C" {
39 #endif
40 
41 
42 /**
43  * @brief RSA public key
44  **/
45 
46 typedef struct
47 {
48  Mpi n; ///<Modulus
49  Mpi e; ///<Public exponent
50 } RsaPublicKey;
51 
52 
53 /**
54  * @brief RSA private key
55  **/
56 
57 typedef struct
58 {
59  Mpi n; ///<Modulus
60  Mpi e; ///<Public exponent
61  Mpi d; ///<Private exponent
62  Mpi p; ///<First factor
63  Mpi q; ///<Second factor
64  Mpi dp; ///<First factor's CRT exponent
65  Mpi dq; ///<second factor's CRT exponent
66  Mpi qinv; ///<CRT coefficient
68 
69 
70 //RSA related constants
71 extern const uint8_t PKCS1_OID[8];
72 extern const uint8_t RSA_ENCRYPTION_OID[9];
73 extern const uint8_t MD2_WITH_RSA_ENCRYPTION_OID[9];
74 extern const uint8_t MD5_WITH_RSA_ENCRYPTION_OID[9];
75 extern const uint8_t SHA1_WITH_RSA_ENCRYPTION_OID[9];
76 extern const uint8_t SHA224_WITH_RSA_ENCRYPTION_OID[9];
77 extern const uint8_t SHA256_WITH_RSA_ENCRYPTION_OID[9];
78 extern const uint8_t SHA384_WITH_RSA_ENCRYPTION_OID[9];
79 extern const uint8_t SHA512_WITH_RSA_ENCRYPTION_OID[9];
80 extern const uint8_t SHA512_256_WITH_RSA_ENCRYPTION_OID[9];
81 extern const uint8_t SHA512_224_WITH_RSA_ENCRYPTION_OID[9];
82 extern const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_224_OID[9];
83 extern const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_256_OID[9];
84 extern const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_384_OID[9];
85 extern const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_512_OID[9];
86 extern const uint8_t RSASSA_PSS_OID[9];
87 
88 //RSA related functions
93 
94 error_t rsaesPkcs1v15Encrypt(const PrngAlgo *prngAlgo, void *prngContext,
95  const RsaPublicKey *key, const uint8_t *message, size_t messageLen,
96  uint8_t *ciphertext, size_t *ciphertextLen);
97 
98 error_t rsaesPkcs1v15Decrypt(const RsaPrivateKey *key, const uint8_t *ciphertext,
99  size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen);
100 
101 error_t rsaesOaepEncrypt(const PrngAlgo *prngAlgo, void *prngContext,
102  const RsaPublicKey *key, const HashAlgo *hash, const char_t *label,
103  const uint8_t *message, size_t messageLen, uint8_t *ciphertext,
104  size_t *ciphertextLen);
105 
107  const char_t *label, const uint8_t *ciphertext, size_t ciphertextLen,
108  uint8_t *message, size_t messageSize, size_t *messageLen);
109 
111  const uint8_t *digest, uint8_t *signature, size_t *signatureLen);
112 
114  const uint8_t *digest, const uint8_t *signature, size_t signatureLen);
115 
116 error_t rsassaPssSign(const PrngAlgo *prngAlgo, void *prngContext,
117  const RsaPrivateKey *key, const HashAlgo *hash, size_t saltLen,
118  const uint8_t *digest, uint8_t *signature, size_t *signatureLen);
119 
121  size_t saltLen, const uint8_t *digest, const uint8_t *signature,
122  size_t signatureLen);
123 
124 error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c);
125 error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m);
126 
127 error_t rsasp1(const RsaPrivateKey *key, const Mpi *m, Mpi *s);
128 error_t rsavp1(const RsaPublicKey *key, const Mpi *s, Mpi *m);
129 
130 error_t emePkcs1v15Encode(const PrngAlgo *prngAlgo, void *prngContext,
131  const uint8_t *message, size_t messageLen, uint8_t *em, size_t k);
132 
133 error_t emePkcs1v15Decode(uint8_t *em, size_t k, uint8_t **message,
134  size_t *messageLen);
135 
136 error_t emeOaepEncode(const PrngAlgo *prngAlgo, void *prngContext,
137  const HashAlgo *hash, const char_t *label, const uint8_t *message,
138  size_t messageLen, uint8_t *em, size_t k);
139 
140 error_t emeOaepDecode(const HashAlgo *hash, const char_t *label, uint8_t *em,
141  size_t k, uint8_t **message, size_t *messageLen);
142 
144  const uint8_t *digest, uint8_t *em, size_t emLen);
145 
146 error_t emsaPkcs1v15Verify(const HashAlgo *hash, const uint8_t *digest,
147  const uint8_t *em, size_t emLen);
148 
149 error_t emsaPssEncode(const PrngAlgo *prngAlgo, void *prngContext,
150  const HashAlgo *hash, size_t saltLen, const uint8_t *digest,
151  uint8_t *em, uint_t emBits);
152 
153 error_t emsaPssVerify(const HashAlgo *hash, size_t saltLen,
154  const uint8_t *digest, uint8_t *em, uint_t emBits);
155 
156 void mgf1(const HashAlgo *hash, HashContext *hashContext, const uint8_t *seed,
157  size_t seedLen, uint8_t *data, size_t dataLen);
158 
159 //C++ guard
160 #ifdef __cplusplus
161  }
162 #endif
163 
164 #endif
error_t emsaPkcs1v15Verify(const HashAlgo *hash, const uint8_t *digest, const uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 verification operation.
Definition: rsa.c:1614
const uint8_t RSA_ENCRYPTION_OID[9]
Definition: rsa.c:55
void rsaFreePrivateKey(RsaPrivateKey *key)
Release a RSA private key.
Definition: rsa.c:145
error_t rsaesPkcs1v15Encrypt(const PrngAlgo *prngAlgo, void *prngContext, const RsaPublicKey *key, const uint8_t *message, size_t messageLen, uint8_t *ciphertext, size_t *ciphertextLen)
RSAES-PKCS1-v1_5 encryption operation.
Definition: rsa.c:171
error_t rsaep(const RsaPublicKey *key, const Mpi *m, Mpi *c)
RSA encryption primitive.
Definition: rsa.c:1118
Arbitrary precision integer.
Definition: mpi.h:67
char char_t
Definition: compiler_port.h:41
error_t rsaesPkcs1v15Decrypt(const RsaPrivateKey *key, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen)
RSAES-PKCS1-v1_5 decryption operation.
Definition: rsa.c:269
uint8_t c
Definition: ndp.h:510
Mpi p
First factor.
Definition: rsa.h:62
error_t rsaesOaepEncrypt(const PrngAlgo *prngAlgo, void *prngContext, const RsaPublicKey *key, const HashAlgo *hash, const char_t *label, const uint8_t *message, size_t messageLen, uint8_t *ciphertext, size_t *ciphertextLen)
RSAES-OAEP encryption operation.
Definition: rsa.c:402
Mpi n
Modulus.
Definition: rsa.h:48
uint8_t hash
Definition: tls.h:1363
Mpi d
Private exponent.
Definition: rsa.h:61
const uint8_t SHA384_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:68
uint8_t message[]
Definition: chap.h:150
General definitions for cryptographic algorithms.
error_t rsassaPssSign(const PrngAlgo *prngAlgo, void *prngContext, const RsaPrivateKey *key, const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
RSASSA-PSS signature generation operation.
Definition: rsa.c:884
error_t emePkcs1v15Decode(uint8_t *em, size_t k, uint8_t **message, size_t *messageLen)
EME-PKCS1-v1_5 decoding operation.
Definition: rsa.c:1322
void rsaFreePublicKey(RsaPublicKey *key)
Release a RSA public key.
Definition: rsa.c:113
const uint8_t SHA512_256_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:74
error_t rsavp1(const RsaPublicKey *key, const Mpi *s, Mpi *m)
RSA verification primitive.
Definition: rsa.c:1233
error_t rsadp(const RsaPrivateKey *key, const Mpi *c, Mpi *m)
RSA decryption primitive.
Definition: rsa.c:1145
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_256_OID[9]
Definition: rsa.c:79
const uint8_t RSASSA_PSS_OID[9]
Definition: rsa.c:86
Generic hash algorithm context.
Definition: crypto.h:1044
Mpi e
Public exponent.
Definition: rsa.h:49
uint8_t m
Definition: ndp.h:299
Mpi n
Modulus.
Definition: rsa.h:59
Mpi q
Second factor.
Definition: rsa.h:63
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_224_OID[9]
Definition: rsa.c:77
Mpi e
Public exponent.
Definition: rsa.h:60
RSA public key.
Definition: rsa.h:46
error_t rsassaPkcs1v15Verify(const RsaPublicKey *key, const HashAlgo *hash, const uint8_t *digest, const uint8_t *signature, size_t signatureLen)
RSASSA-PKCS1-v1_5 signature verification operation.
Definition: rsa.c:775
error_t emeOaepEncode(const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, const char_t *label, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
EME-OAEP encoding operation.
Definition: rsa.c:1379
void mgf1(const HashAlgo *hash, HashContext *hashContext, const uint8_t *seed, size_t seedLen, uint8_t *data, size_t dataLen)
MGF1 mask generation function.
Definition: rsa.c:1868
uint8_t signature
Definition: tls.h:1364
error_t emsaPssVerify(const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
EMSA-PSS verification operation.
Definition: rsa.c:1771
error_t emeOaepDecode(const HashAlgo *hash, const char_t *label, uint8_t *em, size_t k, uint8_t **message, size_t *messageLen)
EME-OAEP decoding operation.
Definition: rsa.c:1460
Mpi dq
second factor&#39;s CRT exponent
Definition: rsa.h:65
const uint8_t PKCS1_OID[8]
Definition: rsa.c:53
void rsaInitPrivateKey(RsaPrivateKey *key)
Initialize a RSA private key.
Definition: rsa.c:126
error_t
Error codes.
Definition: error.h:40
MPI (Multiple Precision Integer Arithmetic)
const uint8_t MD5_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:60
void rsaInitPublicKey(RsaPublicKey *key)
Initialize a RSA public key.
Definition: rsa.c:100
error_t emsaPkcs1v15Encode(const HashAlgo *hash, const uint8_t *digest, uint8_t *em, size_t emLen)
EMSA-PKCS1-v1_5 encoding operation.
Definition: rsa.c:1550
const uint8_t SHA512_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:70
unsigned int uint_t
Definition: compiler_port.h:43
Mpi qinv
CRT coefficient.
Definition: rsa.h:66
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_384_OID[9]
Definition: rsa.c:81
uint8_t data[]
Definition: dtls_misc.h:167
const uint8_t SHA1_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:62
Common interface for pseudo-random number generators.
Definition: crypto.h:1091
Mpi dp
First factor&#39;s CRT exponent.
Definition: rsa.h:64
error_t rsassaPkcs1v15Sign(const RsaPrivateKey *key, const HashAlgo *hash, const uint8_t *digest, uint8_t *signature, size_t *signatureLen)
RSASSA-PKCS1-v1_5 signature generation operation.
Definition: rsa.c:642
error_t rsaesOaepDecrypt(const RsaPrivateKey *key, const HashAlgo *hash, const char_t *label, const uint8_t *ciphertext, size_t ciphertextLen, uint8_t *message, size_t messageSize, size_t *messageLen)
RSAES-OAEP decryption operation.
Definition: rsa.c:508
const uint8_t SHA224_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:64
error_t rsasp1(const RsaPrivateKey *key, const Mpi *m, Mpi *s)
RSA signature primitive.
Definition: rsa.c:1212
error_t emsaPssEncode(const PrngAlgo *prngAlgo, void *prngContext, const HashAlgo *hash, size_t saltLen, const uint8_t *digest, uint8_t *em, uint_t emBits)
EMSA-PSS encoding operation.
Definition: rsa.c:1690
const uint8_t RSASSA_PKCS1_V1_5_WITH_SHA3_512_OID[9]
Definition: rsa.c:83
Common interface for hash algorithms.
Definition: crypto.h:1054
const uint8_t SHA512_224_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:72
RSA private key.
Definition: rsa.h:57
const uint8_t MD2_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:58
error_t rsassaPssVerify(const RsaPublicKey *key, const HashAlgo *hash, size_t saltLen, const uint8_t *digest, const uint8_t *signature, size_t signatureLen)
RSASSA-PSS signature verification operation.
Definition: rsa.c:1004
error_t emePkcs1v15Encode(const PrngAlgo *prngAlgo, void *prngContext, const uint8_t *message, size_t messageLen, uint8_t *em, size_t k)
EME-PKCS1-v1_5 encoding operation.
Definition: rsa.c:1253
const uint8_t SHA256_WITH_RSA_ENCRYPTION_OID[9]
Definition: rsa.c:66