monotone

monotone Mtn Source Tree

Root/botan/eme_pkcs.cpp

1/*************************************************
2* PKCS1 EME Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/eme.h>
7#include <botan/rng.h>
8
9namespace Botan {
10
11/*************************************************
12* PKCS1 Pad Operation *
13*************************************************/
14SecureVector<byte> EME_PKCS1v15::pad(const byte in[], u32bit inlen,
15 u32bit olen) const
16 {
17 olen /= 8;
18
19 if(olen < 10)
20 throw Encoding_Error("PKCS1: Output space too small");
21 if(inlen > olen - 10)
22 throw Encoding_Error("PKCS1: Input is too large");
23
24 SecureVector<byte> out(olen);
25
26 out[0] = 0x02;
27 for(u32bit j = 1; j != olen - inlen - 1; j++)
28 while(out[j] == 0)
29 out[j] = Global_RNG::random(Nonce);
30 out.copy(olen - inlen, in, inlen);
31
32 return out;
33 }
34
35/*************************************************
36* PKCS1 Unpad Operation *
37*************************************************/
38SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], u32bit inlen,
39 u32bit key_len) const
40 {
41 if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02)
42 throw Decoding_Error("PKCS1::unpad");
43
44 u32bit seperator = 0;
45 for(u32bit j = 0; j != inlen; j++)
46 if(in[j] == 0)
47 {
48 seperator = j;
49 break;
50 }
51 if(seperator < 9)
52 throw Decoding_Error("PKCS1::unpad");
53
54 return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1);
55 }
56
57/*************************************************
58* Return the max input size for a given key size *
59*************************************************/
60u32bit EME_PKCS1v15::maximum_input_size(u32bit keybits) const
61 {
62 if(keybits / 8 > 10)
63 return ((keybits / 8) - 10);
64 else
65 return 0;
66 }
67
68}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status