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( 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;
71 // if (in == NULL || out == NULL || outlen == NULL || key == NULL) {
72 // psTraceCrypto("NULL parameter error in psRsaCrypt\n");
73 // return PS_ARG_FAIL;
76 tmp.dp = tmpa.dp = tmpb.dp = NULL;
78 /* Init and copy into tmp */
79 if (pstm_init_for_read_unsigned_bin(pool, &tmp, inlen + sizeof(pstm_digit))
83 if (pstm_read_unsigned_bin(&tmp, (unsigned char *)in, inlen) != PS_SUCCESS){
87 /* Sanity check on the input */
88 if (pstm_cmp(&key->N, &tmp) == PSTM_LT) {
92 if (type == PRIVKEY_TYPE) {
94 if (pstm_init_size(pool, &tmpa, key->p.alloc) != PS_SUCCESS) {
98 if (pstm_init_size(pool, &tmpb, key->q.alloc) != PS_SUCCESS) {
103 if (pstm_exptmod(pool, &tmp, &key->dP, &key->p, &tmpa) !=
105 psTraceCrypto("decrypt error: pstm_exptmod dP, p\n");
108 if (pstm_exptmod(pool, &tmp, &key->dQ, &key->q, &tmpb) !=
110 psTraceCrypto("decrypt error: pstm_exptmod dQ, q\n");
113 if (pstm_sub(&tmpa, &tmpb, &tmp) != PS_SUCCESS) {
114 psTraceCrypto("decrypt error: sub tmpb, tmp\n");
117 if (pstm_mulmod(pool, &tmp, &key->qP, &key->p, &tmp) != PS_SUCCESS) {
118 psTraceCrypto("decrypt error: pstm_mulmod qP, p\n");
121 if (pstm_mul_comba(pool, &tmp, &key->q, &tmp, NULL, 0)
123 psTraceCrypto("decrypt error: pstm_mul q \n");
126 if (pstm_add(&tmp, &tmpb, &tmp) != PS_SUCCESS) {
127 psTraceCrypto("decrypt error: pstm_add tmp \n");
131 if (pstm_exptmod(pool, &tmp, &key->d, &key->N, &tmp) !=
133 psTraceCrypto("psRsaCrypt error: pstm_exptmod\n");
137 } else if (type == PUBKEY_TYPE) {
138 if (pstm_exptmod(pool, &tmp, &key->e, &key->N, &tmp) != PS_SUCCESS) {
139 psTraceCrypto("psRsaCrypt error: pstm_exptmod\n");
143 psTraceCrypto("psRsaCrypt error: invalid type param\n");
147 x = pstm_unsigned_bin_size(&key->N);
149 if ((uint32)x > *outlen) {
151 psTraceCrypto("psRsaCrypt error: pstm_unsigned_bin_size\n");
154 /* We want the encrypted value to always be the key size. Pad with 0x0 */
155 while ((uint32)x < (unsigned long)key->size) {
164 if (pstm_to_unsigned_bin(pool, &tmp, out+(x-pstm_unsigned_bin_size(&tmp)))
166 psTraceCrypto("psRsaCrypt error: pstm_to_unsigned_bin\n");
169 /* Clean up and return */
175 if (type == PRIVKEY_TYPE && key->optimized) {
176 pstm_clear_multi(&tmpa, &tmpb, NULL, NULL, NULL, NULL, NULL, NULL);
182 int32 psRsaEncryptPub(psPool_t *pool, psRsaKey_t *key,
183 unsigned char *in, uint32 inlen,
184 unsigned char *out, uint32 outlen, void *data)
191 //bbox psTraceCrypto("Error on bad outlen parameter to psRsaEncryptPub\n");
192 bb_error_msg_and_die("RSA crypt outlen:%d < size:%d", outlen, size);
196 if ((err = pkcs1Pad(in, inlen, out, size, PRIVKEY_TYPE, data))
198 psTraceCrypto("Error padding psRsaEncryptPub. Likely data too long\n");
201 if ((err = psRsaCrypt(pool, out, size, out, (uint32*)&outlen, key,
202 PUBKEY_TYPE, data)) < PS_SUCCESS) {
203 psTraceCrypto("Error performing psRsaEncryptPub\n");
206 if (outlen != size) {
207 psTraceCrypto("Encrypted size error in psRsaEncryptPub\n");