monotone

monotone Mtn Source Tree

Root/botan/fips140.cpp

1/*************************************************
2* FIPS-140 Self Tests Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/fips140.h>
7#include <botan/lookup.h>
8
9namespace Botan {
10
11namespace FIPS140 {
12
13namespace {
14
15/*************************************************
16* Perform a Known Answer Test *
17*************************************************/
18void do_kat(const std::string& in, const std::string& out,
19 const std::string& algo_name, Filter* filter)
20 {
21 if(out.length())
22 {
23 Pipe pipe(new Hex_Decoder, filter, new Hex_Encoder);
24 pipe.process_msg(in);
25
26 if(out != pipe.read_all_as_string())
27 throw Self_Test_Failure("FIPS-140 " + algo_name + " test");
28 }
29 }
30
31/*************************************************
32* Perform a KAT for a cipher *
33*************************************************/
34void cipher_kat(const std::string& in, const std::string& out,
35 const std::string& key, const std::string& iv,
36 const std::string& cipher)
37 {
38 do_kat(in, out, cipher, get_cipher(cipher, key, iv, ENCRYPTION));
39 do_kat(out, in, cipher, get_cipher(cipher, key, iv, DECRYPTION));
40 }
41
42/*************************************************
43* Perform a KAT for a cipher *
44*************************************************/
45void cipher_kat(const std::string& cipher, const std::string& key,
46 const std::string& iv, const std::string& in,
47 const std::string& ecb_out, const std::string& cbc_out,
48 const std::string& cfb_out, const std::string& ofb_out,
49 const std::string& ctr_out)
50 {
51 if(!have_block_cipher(cipher))
52 return;
53
54 cipher_kat(in, ecb_out, key, "", cipher + "/ECB");
55 cipher_kat(in, cbc_out, key, iv, cipher + "/CBC/NoPadding");
56 cipher_kat(in, cfb_out, key, iv, cipher + "/CFB");
57 cipher_kat(in, ofb_out, key, iv, cipher + "/OFB");
58 cipher_kat(in, ctr_out, key, iv, cipher + "/CTR-BE");
59 }
60
61/*************************************************
62* Perform a KAT for a hash *
63*************************************************/
64void hash_kat(const std::string& hash, const std::string& in,
65 const std::string& out)
66 {
67 if(!have_hash(hash))
68 return;
69 do_kat(in, out, hash, new Hash_Filter(hash));
70 }
71
72/*************************************************
73* Perform a KAT for a MAC *
74*************************************************/
75void mac_kat(const std::string& mac, const std::string& in,
76 const std::string& out, const std::string& key)
77 {
78 if(!have_mac(mac))
79 return;
80 do_kat(in, out, mac, new MAC_Filter(mac, key));
81 }
82
83}
84
85/*************************************************
86* Perform FIPS 140 Self Tests *
87*************************************************/
88bool passes_self_tests()
89 {
90 try {
91 cipher_kat("DES", "0123456789ABCDEF", "1234567890ABCDEF",
92 "4E6F77206973207468652074696D6520666F7220616C6C20",
93 "3FA40E8A984D48156A271787AB8883F9893D51EC4B563B53",
94 "E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6",
95 "F3096249C7F46E51A69E839B1A92F78403467133898EA622",
96 "F3096249C7F46E5135F24A242EEB3D3F3D6D5BE3255AF8C3",
97 "F3096249C7F46E51163A8CA0FFC94C27FA2F80F480B86F75");
98
99 cipher_kat("TripleDES",
100 "385D7189A5C3D485E1370AA5D408082B5CCCCB5E19F2D90E",
101 "C141B5FCCD28DC8A",
102 "6E1BD7C6120947A464A6AAB293A0F89A563D8D40D3461B68",
103 "64EAAD4ACBB9CEAD6C7615E7C7E4792FE587D91F20C7D2F4",
104 "6235A461AFD312973E3B4F7AA7D23E34E03371F8E8C376C9",
105 "E26BA806A59B0330DE40CA38E77A3E494BE2B212F6DD624B",
106 "E26BA806A59B03307DE2BCC25A08BA40A8BA335F5D604C62",
107 "E26BA806A59B03303C62C2EFF32D3ACDD5D5F35EBCC53371");
108
109 cipher_kat("Skipjack", "1555E5531C3A169B2D65", "6EC9795701F49864",
110 "00AFA48E9621E52E8CBDA312660184EDDB1F33D9DACDA8DA",
111 "DBEC73562EFCAEB56204EB8AE9557EBF77473FBB52D17CD1",
112 "0C7B0B74E21F99B8F2C8DF37879F6C044967F42A796DCA8B",
113 "79FDDA9724E36CC2E023E9A5C717A8A8A7FDA465CADCBF63",
114 "79FDDA9724E36CC26CACBD83C1ABC06EAF5B249BE5B1E040",
115 "79FDDA9724E36CC211B0AEC607B95A96BCDA318440B82F49");
116
117 cipher_kat("AES",
118 "2B7E151628AED2A6ABF7158809CF4F3C",
119 "000102030405060708090A0B0C0D0E0F",
120 "6BC1BEE22E409F96E93D7E117393172A"
121 "AE2D8A571E03AC9C9EB76FAC45AF8E51",
122 "3AD77BB40D7A3660A89ECAF32466EF97"
123 "F5D3D58503B9699DE785895A96FDBAAF",
124 "7649ABAC8119B246CEE98E9B12E9197D"
125 "5086CB9B507219EE95DB113A917678B2",
126 "3B3FD92EB72DAD20333449F8E83CFB4A"
127 "C8A64537A0B3A93FCDE3CDAD9F1CE58B",
128 "3B3FD92EB72DAD20333449F8E83CFB4A"
129 "7789508D16918F03F53C52DAC54ED825",
130 "3B3FD92EB72DAD20333449F8E83CFB4A"
131 "010C041999E03F36448624483E582D0E");
132
133 hash_kat("SHA-1", "", "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709");
134 hash_kat("SHA-1", "616263", "A9993E364706816ABA3E25717850C26C9CD0D89D");
135 hash_kat("SHA-1",
136 "6162636462636465636465666465666765666768666768696768696A"
137 "68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F7071",
138 "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");
139
140 mac_kat("HMAC(SHA-1)", "4869205468657265",
141 "B617318655057264E28BC0B6FB378C8EF146BE00",
142 "0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B");
143
144 mac_kat("X9.19-MAC",
145 "31311C3931383237333634351C1C35383134333237361C1C3B3132333435"
146 "36373839303132333435363D3939313231303030303F1C30303031323530"
147 "301C393738363533343132343837363932331C", "C209CCB78EE1B606",
148 "0123456789ABCDEFFEDCBA9876543210");
149 }
150 catch(std::exception)
151 {
152 return false;
153 }
154
155 return true;
156 }
157
158/*************************************************
159* Check a SHA-1 EDC *
160*************************************************/
161bool good_edc(const std::string& filename, const std::string& edc)
162 {
163 if(filename == "" || edc == "")
164 return false;
165
166 Pipe pipe1(new Hash_Filter("SHA-1"));
167 Pipe pipe2(new Hex_Decoder);
168
169 DataSource_Stream in(filename, true);
170 pipe1.process_msg(in);
171 pipe2.process_msg(edc);
172
173 return (pipe1.read_all() == pipe2.read_all());
174 }
175
176}
177
178}

Archive Download this file

Branches

Tags

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