1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
* Parse DRBG CAVP test functions, for libreswan
*
* Copyright (C) 2016 Andrew Cagney <cagney@gnu.org>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <https://www.gnu.org/licenses/gpl2.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "lswalloc.h"
#include "ike_alg.h"
#include "ike_alg_hash.h"
#include "ike_alg_hash_ops.h"
#include "passert.h"
#include "crypt_symkey.h"
#include "ikev2_prf.h"
#include "cavp.h"
#include "cavp_entry.h"
#include "cavp_print.h"
#include "test_sha.h"
static unsigned long l;
static struct cavp_entry config[] = {
{ .key = "L", .op = op_unsigned_long, .unsigned_long = &l, },
{ .key = NULL, },
};
struct hash_desc ike_alg_hash_sha2_224 = {
.common = {
.fqn = "SHA2_224",
.algo_type = IKE_ALG_HASH,
.fips.approved = true,
},
.hash_digest_size = 28, /* 224/8 */
.hash_block_size = 64, /* from RFC 4868 */
.hash_ops = &ike_alg_hash_nss_ops,
};
static const struct hash_desc *hashes[] = {
#ifdef USE_SHA1
&ike_alg_hash_sha1,
#endif
#ifdef USE_SHA2
&ike_alg_hash_sha2_224,
&ike_alg_hash_sha2_256,
&ike_alg_hash_sha2_384,
&ike_alg_hash_sha2_512,
#endif
NULL,
};
static const struct hash_desc *hash_alg;
static void print_config(void)
{
for (int i = 0; hashes[i]; i++) {
if (hashes[i]->hash_digest_size == l) {
hash_alg = hashes[i];
break;
}
}
config_number("L", l);
if (hash_alg == NULL) {
fprintf(stderr, "SHA length %lu not recognised\n", l);
} else {
fprintf(stderr, "SHA %s with length %lu\n",
hash_alg->common.fqn, l);
}
}
static unsigned long len;
static chunk_t msg;
static struct cavp_entry msg_data[] = {
{ .key = "Len", .op = op_unsigned_long, .unsigned_long = &len, },
{ .key = "Msg", .op = op_chunk, .chunk = &msg, },
{ .key = "MD", .op = op_ignore, },
{ .key = NULL, },
};
static void msg_run_test(struct logger *logger_unused UNUSED)
{
print_number("Len", NULL, len);
/* byte aligned */
passert(len == (len & -4));
/* when len==0, msg may contain one byte :-/ */
passert((len == 0 && msg.len <= 1) || len == msg.len * BITS_IN_BYTE);
print_chunk("Msg", NULL, msg, 0);
struct hash_context *hash = hash_alg->hash_ops->init(hash_alg, "sha");
/* See above, use LEN, not MSG.LEN */
hash_alg->hash_ops->digest_bytes(hash, "msg", msg.ptr, len / BITS_IN_BYTE);
chunk_t bytes = alloc_chunk(l, "bytes");
hash_alg->hash_ops->final_bytes(&hash, bytes.ptr, bytes.len);
print_chunk("MD", NULL, bytes, 0);
free_chunk_content(&bytes);
}
const struct cavp test_sha_msg = {
.alias = "sha",
.description = "SHA Algorithms (message digest)",
.config = config,
.print_config = print_config,
.run_test = msg_run_test,
.data = msg_data,
.match = {
"SHA.*Msg",
NULL,
}
};
static chunk_t seed;
static unsigned long count;
static struct cavp_entry monte_data[] = {
{ .key = "Seed", .op = op_chunk, .chunk = &seed, },
{ .key = "COUNT", .op = op_unsigned_long, .unsigned_long = &count},
{ .key = "MD", .op = op_ignore, },
{ .key = NULL, },
};
static void monte_run_test(struct logger *logger_unused UNUSED)
{
print_chunk("Seed", NULL, seed, 0);
chunk_t MDi_3 = alloc_chunk(seed.len, "MDi_3");
chunk_t MDi_2 = alloc_chunk(seed.len, "MDi_2");
chunk_t MDi_1 = alloc_chunk(seed.len, "MDi_1");
chunk_t Mi = alloc_chunk(3 * seed.len, "Mi");
for (int j = 0; j < 100; j++) {
//MD[0] = MD[1] = MD[2] = Seed
memcpy(MDi_3.ptr, seed.ptr, seed.len);
memcpy(MDi_2.ptr, seed.ptr, seed.len);
memcpy(MDi_1.ptr, seed.ptr, seed.len);
for (int i = 3; i < 1003; i++) {
// shuffle
chunk_t tmp = MDi_3;
MDi_3 = MDi_2;
MDi_2 = MDi_1;
MDi_1 = seed;
seed = tmp;
// M[i] = MD[i-3] || MD[i-2] || MD[i-1];
memcpy(Mi.ptr + seed.len * 0, MDi_3.ptr, seed.len);
memcpy(Mi.ptr + seed.len * 1, MDi_2.ptr, seed.len);
memcpy(Mi.ptr + seed.len * 2, MDi_1.ptr, seed.len);
// MDi = SHA(Mi);
struct hash_context *hash = hash_alg->hash_ops->init(hash_alg,
"sha");
hash_alg->hash_ops->digest_bytes(hash, "msg", Mi.ptr, Mi.len);
hash_alg->hash_ops->final_bytes(&hash, seed.ptr, seed.len);
// printf("%d ", i);
// print_chunk("MDi", seed, 0);
}
print_line("");
print_number("COUNT", NULL, j);
// MDj = Seed = MD1002;
// OUTPUT: MDj; (aka seed)
print_chunk("MD", NULL, seed, 0);
}
free_chunk_content(&MDi_3);
free_chunk_content(&MDi_2);
free_chunk_content(&MDi_1);
free_chunk_content(&Mi);
print_line("");
exit(0);
}
const struct cavp test_sha_monte = {
.alias = "sha",
.description = "SHA Algorithms (monte carlo)",
.config = config,
.print_config = print_config,
.run_test = monte_run_test,
.data = monte_data,
.match = {
"SHA.*Monte",
NULL,
}
};
|