1
1
mirror of https://github.com/BLAKE2/BLAKE2 synced 2024-11-08 14:59:19 +01:00
BLAKE2/sse/genkat-json.c

155 lines
10 KiB
C
Raw Permalink Normal View History

2016-04-22 22:12:08 +02:00
/*
BLAKE2 reference source code package - reference C implementations
2016-10-12 16:19:55 +02:00
2016-04-22 22:12:08 +02:00
Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the
terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at
your option. The terms of these licenses can be found at:
2016-10-12 16:19:55 +02:00
2016-04-22 22:12:08 +02:00
- CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
- OpenSSL license : https://www.openssl.org/source/license.html
- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
2016-10-12 16:19:55 +02:00
2016-04-22 22:12:08 +02:00
More information about the BLAKE2 hash function can be found at
https://blake2.net.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "blake2.h"
#define STR_(x) #x
#define STR(x) STR_(x)
#define LENGTH 256
2017-03-07 19:42:27 +01:00
#define MAKE_KAT(name, size_prefix, first) \
2016-10-12 16:19:55 +02:00
do { \
for (i = 0; i < LENGTH; ++i) { \
2017-03-07 19:42:27 +01:00
printf("%s\n{\n", i == 0 && first ? "" : ","); \
2016-10-12 16:19:55 +02:00
\
printf(" \"hash\": \"" #name "\",\n"); \
printf(" \"in\": \""); \
for (j = 0; j < i; ++j) \
printf("%02x", in[j]); \
\
printf("\",\n"); \
printf(" \"key\": \"\",\n"); \
printf(" \"out\": \""); \
\
name(hash, size_prefix##_OUTBYTES, in, i, NULL, 0); \
\
for (j = 0; j < size_prefix##_OUTBYTES; ++j) \
printf("%02x", hash[j]); \
printf("\"\n"); \
2017-03-07 19:42:27 +01:00
printf("}"); \
2016-10-12 16:19:55 +02:00
} \
} while (0)
2016-04-22 22:12:08 +02:00
2017-03-07 19:42:27 +01:00
#define MAKE_KEYED_KAT(name, size_prefix, first) \
2016-10-12 16:19:55 +02:00
do { \
for (i = 0; i < LENGTH; ++i) { \
2017-03-07 19:42:27 +01:00
printf("%s\n{\n", i == 0 && first ? "" : ","); \
2016-10-12 16:19:55 +02:00
\
printf(" \"hash\": \"" #name "\",\n"); \
printf(" \"in\": \""); \
for (j = 0; j < i; ++j) \
printf("%02x", in[j]); \
\
printf("\",\n"); \
printf(" \"key\": \""); \
for (j = 0; j < size_prefix##_KEYBYTES; ++j) \
printf("%02x", key[j]); \
printf("\",\n"); \
printf(" \"out\": \""); \
\
name(hash, size_prefix##_OUTBYTES, in, i, key, size_prefix##_KEYBYTES); \
\
for (j = 0; j < size_prefix##_OUTBYTES; ++j) \
printf("%02x", hash[j]); \
printf("\"\n"); \
2017-03-07 19:42:27 +01:00
printf("}"); \
2016-10-12 16:19:55 +02:00
} \
} while (0)
2016-10-11 22:55:35 +02:00
2017-03-07 19:42:27 +01:00
#define MAKE_XOF_KAT(name, first) \
2016-10-12 16:19:55 +02:00
do { \
for (i = 1; i <= LENGTH; ++i) { \
2017-03-07 19:42:27 +01:00
printf("%s\n{\n", i == 1 && first ? "" : ","); \
2016-10-12 16:19:55 +02:00
\
printf(" \"hash\": \"" #name "\",\n"); \
printf(" \"in\": \""); \
for (j = 0; j < LENGTH; ++j) \
printf("%02x", in[j]); \
\
printf("\",\n"); \
printf(" \"key\": \"\",\n"); \
printf(" \"out\": \""); \
\
name(hash, i, in, LENGTH, NULL, 0); \
\
for (j = 0; j < i; ++j) \
printf("%02x", hash[j]); \
printf("\"\n"); \
2017-03-07 19:42:27 +01:00
printf("}"); \
2016-10-12 16:19:55 +02:00
} \
} while (0)
2016-10-11 22:55:35 +02:00
2017-03-07 19:42:27 +01:00
#define MAKE_XOF_KEYED_KAT(name, size_prefix, first) \
2016-10-12 16:19:55 +02:00
do { \
for (i = 1; i <= LENGTH; ++i) { \
2017-03-07 19:42:27 +01:00
printf("%s\n{\n", i == 1 && first ? "" : ","); \
2016-10-12 16:19:55 +02:00
\
printf(" \"hash\": \"" #name "\",\n"); \
printf(" \"in\": \""); \
for (j = 0; j < LENGTH; ++j) \
printf("%02x", in[j]); \
\
printf("\",\n"); \
printf(" \"key\": \""); \
for (j = 0; j < size_prefix##_KEYBYTES; ++j) \
printf("%02x", key[j]); \
printf("\",\n"); \
printf(" \"out\": \""); \
\
name(hash, i, in, LENGTH, key, size_prefix##_KEYBYTES); \
\
for (j = 0; j < i; ++j) \
printf("%02x", hash[j]); \
printf("\"\n"); \
2017-03-07 19:42:27 +01:00
printf("}"); \
2016-10-12 16:19:55 +02:00
} \
} while (0)
2016-04-22 22:12:08 +02:00
2016-10-12 16:19:55 +02:00
int main() {
2016-04-22 22:12:08 +02:00
uint8_t key[64] = {0};
uint8_t in[LENGTH] = {0};
2016-10-11 22:55:35 +02:00
uint8_t hash[LENGTH] = {0};
2016-10-12 10:22:17 +02:00
size_t i, j;
2016-04-22 22:12:08 +02:00
2016-10-12 16:19:55 +02:00
for (i = 0; i < sizeof(in); ++i)
2016-04-22 22:12:08 +02:00
in[i] = i;
2016-10-12 16:19:55 +02:00
for (i = 0; i < sizeof(key); ++i)
2016-04-22 22:12:08 +02:00
key[i] = i;
2016-06-11 12:11:20 +02:00
printf("[");
2017-03-07 19:42:27 +01:00
MAKE_KAT(blake2s, BLAKE2S, 1);
MAKE_KEYED_KAT(blake2s, BLAKE2S, 0);
MAKE_KAT(blake2b, BLAKE2B, 0);
MAKE_KEYED_KAT(blake2b, BLAKE2B, 0);
MAKE_KAT(blake2sp, BLAKE2S, 0);
MAKE_KEYED_KAT(blake2sp, BLAKE2S, 0);
MAKE_KAT(blake2bp, BLAKE2B, 0);
MAKE_KEYED_KAT(blake2bp, BLAKE2B, 0);
MAKE_XOF_KAT(blake2xs, 0);
MAKE_XOF_KEYED_KAT(blake2xs, BLAKE2S, 0);
MAKE_XOF_KAT(blake2xb, 0);
MAKE_XOF_KEYED_KAT(blake2xb, BLAKE2B, 0);
2016-06-11 12:11:20 +02:00
printf("\n]\n");
2016-04-22 22:12:08 +02:00
fflush(stdout);
return 0;
}