pkcs1.c
Go to the documentation of this file.
00001 /*
00002  * PKCS #1 (RSA Encryption)
00003  * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "includes.h"
00016 
00017 #include "common.h"
00018 #include "rsa.h"
00019 #include "pkcs1.h"
00020 
00021 
00022 static int pkcs1_generate_encryption_block(u8 block_type, size_t modlen,
00023                                            const u8 *in, size_t inlen,
00024                                            u8 *out, size_t *outlen)
00025 {
00026         size_t ps_len;
00027         u8 *pos;
00028 
00029         /*
00030          * PKCS #1 v1.5, 8.1:
00031          *
00032          * EB = 00 || BT || PS || 00 || D
00033          * BT = 00 or 01 for private-key operation; 02 for public-key operation
00034          * PS = k-3-||D||; at least eight octets
00035          * (BT=0: PS=0x00, BT=1: PS=0xff, BT=2: PS=pseudorandom non-zero)
00036          * k = length of modulus in octets (modlen)
00037          */
00038 
00039         if (modlen < 12 || modlen > *outlen || inlen > modlen - 11) {
00040                 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Invalid buffer "
00041                            "lengths (modlen=%lu outlen=%lu inlen=%lu)",
00042                            __func__, (unsigned long) modlen,
00043                            (unsigned long) *outlen,
00044                            (unsigned long) inlen);
00045                 return -1;
00046         }
00047 
00048         pos = out;
00049         *pos++ = 0x00;
00050         *pos++ = block_type; /* BT */
00051         ps_len = modlen - inlen - 3;
00052         switch (block_type) {
00053         case 0:
00054                 os_memset(pos, 0x00, ps_len);
00055                 pos += ps_len;
00056                 break;
00057         case 1:
00058                 os_memset(pos, 0xff, ps_len);
00059                 pos += ps_len;
00060                 break;
00061         case 2:
00062                 if (os_get_random(pos, ps_len) < 0) {
00063                         wpa_printf(MSG_DEBUG, "PKCS #1: %s - Failed to get "
00064                                    "random data for PS", __func__);
00065                         return -1;
00066                 }
00067                 while (ps_len--) {
00068                         if (*pos == 0x00)
00069                                 *pos = 0x01;
00070                         pos++;
00071                 }
00072                 break;
00073         default:
00074                 wpa_printf(MSG_DEBUG, "PKCS #1: %s - Unsupported block type "
00075                            "%d", __func__, block_type);
00076                 return -1;
00077         }
00078         *pos++ = 0x00;
00079         os_memcpy(pos, in, inlen); /* D */
00080 
00081         return 0;
00082 }
00083 
00084 
00085 int pkcs1_encrypt(int block_type, struct crypto_rsa_key *key,
00086                   int use_private, const u8 *in, size_t inlen,
00087                   u8 *out, size_t *outlen)
00088 {
00089         size_t modlen;
00090 
00091         modlen = crypto_rsa_get_modulus_len(key);
00092 
00093         if (pkcs1_generate_encryption_block(block_type, modlen, in, inlen,
00094                                             out, outlen) < 0)
00095                 return -1;
00096 
00097         return crypto_rsa_exptmod(out, modlen, out, outlen, key, use_private);
00098 }
00099 
00100 
00101 int pkcs1_v15_private_key_decrypt(struct crypto_rsa_key *key,
00102                                   const u8 *in, size_t inlen,
00103                                   u8 *out, size_t *outlen)
00104 {
00105         int res;
00106         u8 *pos, *end;
00107 
00108         res = crypto_rsa_exptmod(in, inlen, out, outlen, key, 1);
00109         if (res)
00110                 return res;
00111 
00112         if (*outlen < 2 || out[0] != 0 || out[1] != 2)
00113                 return -1;
00114 
00115         /* Skip PS (pseudorandom non-zero octets) */
00116         pos = out + 2;
00117         end = out + *outlen;
00118         while (*pos && pos < end)
00119                 pos++;
00120         if (pos == end)
00121                 return -1;
00122         pos++;
00123 
00124         *outlen -= pos - out;
00125 
00126         /* Strip PKCS #1 header */
00127         os_memmove(out, pos, *outlen);
00128 
00129         return 0;
00130 }
00131 
00132 
00133 int pkcs1_decrypt_public_key(struct crypto_rsa_key *key,
00134                              const u8 *crypt, size_t crypt_len,
00135                              u8 *plain, size_t *plain_len)
00136 {
00137         size_t len;
00138         u8 *pos;
00139 
00140         len = *plain_len;
00141         if (crypto_rsa_exptmod(crypt, crypt_len, plain, &len, key, 0) < 0)
00142                 return -1;
00143 
00144         /*
00145          * PKCS #1 v1.5, 8.1:
00146          *
00147          * EB = 00 || BT || PS || 00 || D
00148          * BT = 00 or 01
00149          * PS = k-3-||D|| times (00 if BT=00) or (FF if BT=01)
00150          * k = length of modulus in octets
00151          */
00152 
00153         if (len < 3 + 8 + 16 /* min hash len */ ||
00154             plain[0] != 0x00 || (plain[1] != 0x00 && plain[1] != 0x01)) {
00155                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
00156                            "structure");
00157                 return -1;
00158         }
00159 
00160         pos = plain + 3;
00161         if (plain[1] == 0x00) {
00162                 /* BT = 00 */
00163                 if (plain[2] != 0x00) {
00164                         wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
00165                                    "PS (BT=00)");
00166                         return -1;
00167                 }
00168                 while (pos + 1 < plain + len && *pos == 0x00 && pos[1] == 0x00)
00169                         pos++;
00170         } else {
00171                 /* BT = 01 */
00172                 if (plain[2] != 0xff) {
00173                         wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature "
00174                                    "PS (BT=01)");
00175                         return -1;
00176                 }
00177                 while (pos < plain + len && *pos == 0xff)
00178                         pos++;
00179         }
00180 
00181         if (pos - plain - 2 < 8) {
00182                 /* PKCS #1 v1.5, 8.1: At least eight octets long PS */
00183                 wpa_printf(MSG_INFO, "LibTomCrypt: Too short signature "
00184                            "padding");
00185                 return -1;
00186         }
00187 
00188         if (pos + 16 /* min hash len */ >= plain + len || *pos != 0x00) {
00189                 wpa_printf(MSG_INFO, "LibTomCrypt: Invalid signature EB "
00190                            "structure (2)");
00191                 return -1;
00192         }
00193         pos++;
00194         len -= pos - plain;
00195 
00196         /* Strip PKCS #1 header */
00197         os_memmove(plain, pos, len);
00198         *plain_len = len;
00199 
00200         return 0;
00201 }


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:35