2 * Copyright (C) 2017 Denys Vlasenko
4 * Licensed under GPLv2, see file LICENSE in this source tree.
8 /* The code below is taken from parts of
9 * matrixssl-3-7-2b-open/crypto/pubkey/pkcs.c
10 * matrixssl-3-7-2b-open/crypto/pubkey/rsa.c
11 * and (so far) almost not modified. Changes are flagged with ///bbox
14 #define pkcs1Pad(in, inlen, out, outlen, cryptType, userPtr) \
15 pkcs1Pad(in, inlen, out, outlen, cryptType)
17 int32 pkcs1Pad(unsigned char *in, uint32 inlen, unsigned char *out,
18 uint32 outlen, int32 cryptType, void *userPtr)
23 randomLen = outlen - 3 - inlen;
25 psTraceCrypto("pkcs1Pad failure\n");
31 *c = (unsigned char)cryptType;
33 if (cryptType == PUBKEY_TYPE) {
34 while (randomLen-- > 0) {
38 if (matrixCryptoGetPrngData(c, (uint32)randomLen, userPtr) < 0) {
39 return PS_PLATFORM_FAIL;
42 SECURITY: Read through the random data and change all 0x0 to 0x01.
43 This is per spec that no random bytes should be 0
45 while (randomLen-- > 0) {
59 #define psRsaCrypt(pool, in, inlen, out, outlen, key, type, data) \
60 psRsaCrypt(pool, in, inlen, out, outlen, key, type)
62 int32 psRsaCrypt(psPool_t *pool, const unsigned char *in, uint32 inlen,
63 unsigned char *out, uint32 *outlen, psRsaKey_t *key, int32 type,
66 pstm_int tmp, tmpa, tmpb;
70 if (in == NULL || out == NULL || outlen == NULL || key == NULL) {
71 psTraceCrypto("NULL parameter error in psRsaCrypt\n");
75 tmp.dp = tmpa.dp = tmpb.dp = NULL;
77 /* Init and copy into tmp */
78 if (pstm_init_for_read_unsigned_bin(pool, &tmp, inlen + sizeof(pstm_digit))
82 if (pstm_read_unsigned_bin(&tmp, (unsigned char *)in, inlen) != PS_SUCCESS){
86 /* Sanity check on the input */
87 if (pstm_cmp(&key->N, &tmp) == PSTM_LT) {
91 if (type == PRIVKEY_TYPE) {
93 if (pstm_init_size(pool, &tmpa, key->p.alloc) != PS_SUCCESS) {
97 if (pstm_init_size(pool, &tmpb, key->q.alloc) != PS_SUCCESS) {
102 if (pstm_exptmod(pool, &tmp, &key->dP, &key->p, &tmpa) !=
104 psTraceCrypto("decrypt error: pstm_exptmod dP, p\n");
107 if (pstm_exptmod(pool, &tmp, &key->dQ, &key->q, &tmpb) !=
109 psTraceCrypto("decrypt error: pstm_exptmod dQ, q\n");
112 if (pstm_sub(&tmpa, &tmpb, &tmp) != PS_SUCCESS) {
113 psTraceCrypto("decrypt error: sub tmpb, tmp\n");
116 if (pstm_mulmod(pool, &tmp, &key->qP, &key->p, &tmp) != PS_SUCCESS) {
117 psTraceCrypto("decrypt error: pstm_mulmod qP, p\n");
120 if (pstm_mul_comba(pool, &tmp, &key->q, &tmp, NULL, 0)
122 psTraceCrypto("decrypt error: pstm_mul q \n");
125 if (pstm_add(&tmp, &tmpb, &tmp) != PS_SUCCESS) {
126 psTraceCrypto("decrypt error: pstm_add tmp \n");
130 if (pstm_exptmod(pool, &tmp, &key->d, &key->N, &tmp) !=
132 psTraceCrypto("psRsaCrypt error: pstm_exptmod\n");
136 } else if (type == PUBKEY_TYPE) {
137 if (pstm_exptmod(pool, &tmp, &key->e, &key->N, &tmp) != PS_SUCCESS) {
138 psTraceCrypto("psRsaCrypt error: pstm_exptmod\n");
142 psTraceCrypto("psRsaCrypt error: invalid type param\n");
146 x = pstm_unsigned_bin_size(&key->N);
148 if ((uint32)x > *outlen) {
150 psTraceCrypto("psRsaCrypt error: pstm_unsigned_bin_size\n");
153 /* We want the encrypted value to always be the key size. Pad with 0x0 */
154 while ((uint32)x < (unsigned long)key->size) {
163 if (pstm_to_unsigned_bin(pool, &tmp, out+(x-pstm_unsigned_bin_size(&tmp)))
165 psTraceCrypto("psRsaCrypt error: pstm_to_unsigned_bin\n");
168 /* Clean up and return */
174 if (type == PRIVKEY_TYPE && key->optimized) {
175 pstm_clear_multi(&tmpa, &tmpb, NULL, NULL, NULL, NULL, NULL, NULL);
181 int32 psRsaEncryptPub(psPool_t *pool, psRsaKey_t *key,
182 unsigned char *in, uint32 inlen,
183 unsigned char *out, uint32 outlen, void *data)
190 psTraceCrypto("Error on bad outlen parameter to psRsaEncryptPub: outlen:%d < size:%d", outlen, size);
194 if ((err = pkcs1Pad(in, inlen, out, size, PRIVKEY_TYPE, data))
196 psTraceCrypto("Error padding psRsaEncryptPub. Likely data too long\n");
199 if ((err = psRsaCrypt(pool, out, size, out, (uint32*)&outlen, key,
200 PUBKEY_TYPE, data)) < PS_SUCCESS) {
201 psTraceCrypto("Error performing psRsaEncryptPub\n");
204 if (outlen != size) {
205 psTraceCrypto("Encrypted size error in psRsaEncryptPub\n");