monotone

monotone Mtn Source Tree

Root/botan/prf_x942.cpp

1/*************************************************
2* X9.42 PRF Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/kdf.h>
7#include <botan/der_enc.h>
8#include <botan/oids.h>
9#include <botan/lookup.h>
10#include <botan/bit_ops.h>
11#include <memory>
12
13namespace Botan {
14
15namespace {
16
17/*************************************************
18* Encode an integer as an OCTET STRING *
19*************************************************/
20MemoryVector<byte> encode_x942_int(u32bit n)
21 {
22 byte n_buf[4];
23 for(u32bit j = 0; j != 4; j++)
24 n_buf[j] = get_byte(j, n);
25
26 DER_Encoder encoder;
27 DER::encode(encoder, n_buf, 4, OCTET_STRING);
28 return encoder.get_contents();
29 }
30
31}
32
33/*************************************************
34* X9.42 PRF *
35*************************************************/
36SecureVector<byte> X942_PRF::derive(u32bit key_len,
37 const byte secret[], u32bit secret_len,
38 const byte salt[], u32bit salt_len) const
39 {
40 std::auto_ptr<HashFunction> hash(get_hash("SHA-1"));
41 const OID kek_algo(key_wrap_oid);
42
43 SecureVector<byte> key;
44 u32bit counter = 1;
45
46 while(key.size() != key_len)
47 {
48 DER_Encoder encoder;
49 encoder.start_sequence();
50 encoder.start_sequence();
51 DER::encode(encoder, kek_algo);
52 encoder.add_raw_octets(encode_x942_int(counter));
53 encoder.end_sequence();
54
55 if(salt_len)
56 {
57 encoder.start_explicit(ASN1_Tag(0));
58 DER::encode(encoder, salt, salt_len, OCTET_STRING);
59 encoder.end_explicit(ASN1_Tag(0));
60 }
61
62 encoder.start_explicit(ASN1_Tag(2));
63 encoder.add_raw_octets(encode_x942_int(8 * key_len));
64 encoder.end_explicit(ASN1_Tag(2));
65 encoder.end_sequence();
66
67 hash->update(secret, secret_len);
68 hash->update(encoder.get_contents());
69 SecureVector<byte> digest = hash->final();
70 key.append(digest, std::min(digest.size(), key_len - key.size()));
71
72 counter++;
73 }
74
75 return key;
76 }
77
78/*************************************************
79* X9.42 Constructor *
80*************************************************/
81X942_PRF::X942_PRF(const std::string& oid)
82 {
83 if(OIDS::have_oid(oid))
84 key_wrap_oid = OIDS::lookup(oid).as_string();
85 else
86 key_wrap_oid = oid;
87 }
88
89}

Archive Download this file

Branches

Tags

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