Botan  1.11.15
src/lib/pk_pad/eme_pkcs1/eme_pkcs.cpp
Go to the documentation of this file.
00001 /*
00002 * PKCS1 EME
00003 * (C) 1999-2007 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #include <botan/internal/pad_utils.h>
00009 #include <botan/eme_pkcs.h>
00010 
00011 namespace Botan {
00012 
00013 BOTAN_REGISTER_EME_NAMED_NOARGS(EME_PKCS1v15, "PKCS1v15");
00014 
00015 /*
00016 * PKCS1 Pad Operation
00017 */
00018 secure_vector<byte> EME_PKCS1v15::pad(const byte in[], size_t inlen,
00019                                      size_t olen,
00020                                      RandomNumberGenerator& rng) const
00021    {
00022    olen /= 8;
00023 
00024    if(olen < 10)
00025       throw Encoding_Error("PKCS1: Output space too small");
00026    if(inlen > olen - 10)
00027       throw Encoding_Error("PKCS1: Input is too large");
00028 
00029    secure_vector<byte> out(olen);
00030 
00031    out[0] = 0x02;
00032    for(size_t j = 1; j != olen - inlen - 1; ++j)
00033       while(out[j] == 0)
00034          out[j] = rng.next_byte();
00035    buffer_insert(out, olen - inlen, in, inlen);
00036 
00037    return out;
00038    }
00039 
00040 /*
00041 * PKCS1 Unpad Operation
00042 */
00043 secure_vector<byte> EME_PKCS1v15::unpad(const byte in[], size_t inlen,
00044                                        size_t key_len) const
00045    {
00046    if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02)
00047       throw Decoding_Error("PKCS1::unpad");
00048 
00049    size_t seperator = 0;
00050    for(size_t j = 0; j != inlen; ++j)
00051       if(in[j] == 0)
00052          {
00053          seperator = j;
00054          break;
00055          }
00056    if(seperator < 9)
00057       throw Decoding_Error("PKCS1::unpad");
00058 
00059    return secure_vector<byte>(&in[seperator + 1], &in[inlen]);
00060    }
00061 
00062 /*
00063 * Return the max input size for a given key size
00064 */
00065 size_t EME_PKCS1v15::maximum_input_size(size_t keybits) const
00066    {
00067    if(keybits / 8 > 10)
00068       return ((keybits / 8) - 10);
00069    else
00070       return 0;
00071    }
00072 
00073 }