00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "rsa.h"
00020 #include "pkcs1.h"
00021
00022
00023 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
00024 const u8 *in, size_t inlen,
00025 u8 *out, size_t *outlen)
00026 {
00027 size_t ps_len;
00028 u8 *pos;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
00041 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
00042 "lengths (modlen=%lu outlen=%lu inlen=%lu)",
00043 __func__, (unsigned long) modlen,
00044 (unsigned long) *outlen,
00045 (unsigned long) inlen);
00046 return -1;
00047 }
00048
00049 pos = out;
00050 *pos++ = 0x00;
00051 *pos++ = block_type;
00052 ps_len = modlen - inlen - 3;
00053 switch (block_type) {
00054 case 0:
00055 os_memset(pos, 0x00, ps_len);
00056 pos += ps_len;
00057 break;
00058 case 1:
00059 os_memset(pos, 0xff, ps_len);
00060 pos += ps_len;
00061 break;
00062 case 2:
00063 if (os_get_random(pos, ps_len) < 0) {
00064 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
00065 "random data for PS", __func__);
00066 return -1;
00067 }
00068 while (ps_len--) {
00069 if (*pos == 0x00)
00070 *pos = 0x01;
00071 pos++;
00072 }
00073 break;
00074 default:
00075 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
00076 "%d", __func__, block_type);
00077 return -1;
00078 }
00079 *pos++ = 0x00;
00080 os_memcpy(pos, in, inlen);
00081
00082 return 0;
00083 }
00084
00085
00086 int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
00087 int use_private, const u8 *in, size_t inlen,
00088 u8 *out, size_t *outlen)
00089 {
00090 size_t modlen;
00091
00092 modlen = crypto_rsa_get_modulus_len(key);
00093
00094 if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
00095 out, outlen) < 0)
00096 return -1;
00097
00098 return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
00099 }
00100
00101
00102 int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
00103 const u8 *in, size_t inlen,
00104 u8 *out, size_t *outlen)
00105 {
00106 int res;
00107 u8 *pos, *end;
00108
00109 res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
00110 if (res)
00111 return res;
00112
00113 if (*outlen < 2 || out[0] != 0 || out[1] != 2)
00114 return -1;
00115
00116
00117 pos = out + 2;
00118 end = out + *outlen;
00119 while (*pos && pos < end)
00120 pos++;
00121 if (pos == end)
00122 return -1;
00123 pos++;
00124
00125 *outlen -= pos - out;
00126
00127
00128 os_memmove(out, pos, *outlen);
00129
00130 return 0;
00131 }
00132
00133
00134 int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
00135 const u8 *crypt, size_t crypt_len,
00136 u8 *plain, size_t *plain_len)
00137 {
00138 size_t len;
00139 u8 *pos;
00140
00141 len = *plain_len;
00142 if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
00143 return -1;
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 if (len < 3 + 8 + 16 ||
00155 plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
00156 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
00157 "structure");
00158 return -1;
00159 }
00160
00161 pos = plain + 3;
00162 if (plain[1] == 0x00) {
00163
00164 if (plain[2] != 0x00) {
00165 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
00166 "PS (BT=00)");
00167 return -1;
00168 }
00169 while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
00170 pos++;
00171 } else {
00172
00173 if (plain[2] != 0xff) {
00174 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
00175 "PS (BT=01)");
00176 return -1;
00177 }
00178 while (pos < plain + len && *pos == 0xff)
00179 pos++;
00180 }
00181
00182 if (pos - plain - 2 < 8) {
00183
00184 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
00185 "padding");
00186 return -1;
00187 }
00188
00189 if (pos + 16 >= plain + len || *pos != 0x00) {
00190 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
00191 "structure (2)");
00192 return -1;
00193 }
00194 pos++;
00195 len -= pos - plain;
00196
00197
00198 os_memmove(plain, pos, len);
00199 *plain_len = len;
00200
00201 return 0;
00202 }
00203