monotone

monotone Mtn Source Tree

Root/botan/symkey.cpp

1/*************************************************
2* OctetString Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/symkey.h>
7#include <botan/bit_ops.h>
8#include <botan/pipe.h>
9#include <botan/hex.h>
10#include <botan/rng.h>
11
12namespace Botan {
13
14/*************************************************
15* Create an OctetString from RNG output *
16*************************************************/
17void OctetString::change(u32bit length, RNG_Quality level)
18 {
19 bits.create(length);
20 Global_RNG::randomize(bits, length, level);
21 }
22
23/*************************************************
24* Create an OctetString from a hex string *
25*************************************************/
26void OctetString::change(const std::string& hex_string)
27 {
28 SecureVector<byte> hex;
29 for(u32bit j = 0; j != hex_string.length(); j++)
30 if(Hex_Decoder::is_valid(hex_string[j]))
31 hex.append(hex_string[j]);
32
33 if(hex.size() % 2 != 0)
34 throw Invalid_Argument("OctetString: hex string must encode full bytes");
35 bits.create(hex.size() / 2);
36 for(u32bit j = 0; j != bits.size(); j++)
37 bits[j] = Hex_Decoder::decode(hex.begin() + 2*j);
38 }
39
40/*************************************************
41* Create an OctetString from a byte string *
42*************************************************/
43void OctetString::change(const byte in[], u32bit n)
44 {
45 bits.create(n);
46 bits.copy(in, n);
47 }
48
49/*************************************************
50* Set the parity of each key byte to odd *
51*************************************************/
52void OctetString::set_odd_parity()
53 {
54 const byte ODD_PARITY[256] = {
55 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, 0x08, 0x08, 0x0B, 0x0B,
56 0x0D, 0x0D, 0x0E, 0x0E, 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
57 0x19, 0x19, 0x1A, 0x1A, 0x1C, 0x1C, 0x1F, 0x1F, 0x20, 0x20, 0x23, 0x23,
58 0x25, 0x25, 0x26, 0x26, 0x29, 0x29, 0x2A, 0x2A, 0x2C, 0x2C, 0x2F, 0x2F,
59 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37, 0x38, 0x38, 0x3B, 0x3B,
60 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0xEC, 0xEC, 0xEF, 0xEF,
75 0xF1, 0xF1, 0xF2, 0xF2, 0xF4, 0xF4, 0xF7, 0xF7, 0xF8, 0xF8, 0xFB, 0xFB,
76 0xFD, 0xFD, 0xFE, 0xFE };
77
78 for(u32bit j = 0; j != bits.size(); j++)
79 bits[j] = ODD_PARITY[bits[j]];
80 }
81
82/*************************************************
83* Hex encode an OctetString *
84*************************************************/
85std::string OctetString::as_string() const
86 {
87 Pipe pipe(new Hex_Encoder);
88 pipe.process_msg(bits);
89 return pipe.read_all_as_string();
90 }
91
92/*************************************************
93* XOR Operation for OctetStrings *
94*************************************************/
95OctetString& OctetString::operator^=(const OctetString& k)
96 {
97 if(&k == this) { bits.clear(); return (*this); }
98 xor_buf(bits.begin(), k.begin(), std::min(length(), k.length()));
99 return (*this);
100 }
101
102/*************************************************
103* Equality Operation for OctetStrings *
104*************************************************/
105bool operator==(const OctetString& s1, const OctetString& s2)
106 {
107 return (s1.bits_of() == s2.bits_of());
108 }
109
110/*************************************************
111* Unequality Operation for OctetStrings *
112*************************************************/
113bool operator!=(const OctetString& s1, const OctetString& s2)
114 {
115 return !(s1 == s2);
116 }
117
118/*************************************************
119* Append Operation for OctetStrings *
120*************************************************/
121OctetString operator+(const OctetString& k1, const OctetString& k2)
122 {
123 return OctetString(SecureVector<byte>(k1.bits_of(), k2.bits_of()));
124 }
125
126/*************************************************
127* XOR Operation for OctetStrings *
128*************************************************/
129OctetString operator^(const OctetString& k1, const OctetString& k2)
130 {
131 SecureVector<byte> ret(std::max(k1.length(), k2.length()));
132 ret.copy(k1.begin(), k1.length());
133 xor_buf(ret, k2.begin(), k2.length());
134 return OctetString(ret);
135 }
136
137}

Archive Download this file

Branches

Tags

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