monotone

monotone Mtn Source Tree

Root/botan/prf_x942.cpp

1/*************************************************
2* X9.42 PRF Source File *
3* (C) 1999-2007 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/loadstor.h>
11#include <algorithm>
12#include <memory>
13
14namespace Botan {
15
16namespace {
17
18/*************************************************
19* Encode an integer as an OCTET STRING *
20*************************************************/
21MemoryVector<byte> encode_x942_int(u32bit n)
22 {
23 byte n_buf[4] = { 0 };
24 store_be(n, n_buf);
25 return DER_Encoder().encode(n_buf, 4, OCTET_STRING).get_contents();
26 }
27
28}
29
30/*************************************************
31* X9.42 PRF *
32*************************************************/
33SecureVector<byte> X942_PRF::derive(u32bit key_len,
34 const byte secret[], u32bit secret_len,
35 const byte salt[], u32bit salt_len) const
36 {
37 std::auto_ptr<HashFunction> hash(get_hash("SHA-1"));
38 const OID kek_algo(key_wrap_oid);
39
40 SecureVector<byte> key;
41 u32bit counter = 1;
42
43 while(key.size() != key_len && counter)
44 {
45 hash->update(secret, secret_len);
46
47 hash->update(
48 DER_Encoder().start_cons(SEQUENCE)
49
50 .start_cons(SEQUENCE)
51 .encode(kek_algo)
52 .raw_bytes(encode_x942_int(counter))
53 .end_cons()
54
55 .encode_if(salt_len != 0,
56 DER_Encoder()
57 .start_explicit(0)
58 .encode(salt, salt_len, OCTET_STRING)
59 .end_explicit()
60 )
61
62 .start_explicit(2)
63 .raw_bytes(encode_x942_int(8 * key_len))
64 .end_explicit()
65
66 .end_cons().get_contents()
67 );
68
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