File: glome_test.c

package info (click to toggle)
glome 0.3.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: ansic: 2,468; python: 508; sh: 149; makefile: 20
file content (88 lines) | stat: -rw-r--r-- 2,947 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "glome.h"

#include <glib.h>
#include <stdint.h>
#include <stdio.h>

static void decode_hex(uint8_t *dst, const char *in) {
  size_t len = strlen(in);
  size_t i;
  for (i = 0; i < len / 2; i++) {
    sscanf(in + (i * 2), "%02hhX", dst + i);
  }
}

static void test_vector1(void) {
  uint8_t ka_priv[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
  uint8_t ka_pub[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
  uint8_t kb_pub[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
  uint8_t expected_tag[GLOME_MAX_TAG_LENGTH] = {0};
  uint8_t tag[GLOME_MAX_TAG_LENGTH] = {0};
  uint8_t counter = 0;
  const char *msg = "The quick brown fox";

  decode_hex(
      ka_priv,
      "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a");
  decode_hex(
      kb_pub,
      "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f");
  decode_hex(
      expected_tag,
      "9c44389f462d35d0672faf73a5e118f8b9f5c340bbe8d340e2b947c205ea4fa3");

  g_assert_true(glome_derive_key(ka_priv, ka_pub) == 0);
  g_assert_true(glome_tag(/* verify */ false, counter, ka_priv, kb_pub,
                          (const uint8_t *)msg, strlen(msg), tag) == 0);

  g_assert_cmpmem(tag, sizeof tag, expected_tag, sizeof expected_tag);
}

static void test_vector2(void) {
  uint8_t ka_pub[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
  uint8_t kb_priv[GLOME_MAX_PRIVATE_KEY_LENGTH] = {0};
  uint8_t kb_pub[GLOME_MAX_PUBLIC_KEY_LENGTH] = {0};
  uint8_t expected_tag[GLOME_MAX_TAG_LENGTH] = {0};
  uint8_t tag[GLOME_MAX_TAG_LENGTH] = {0};
  uint8_t counter = 100;
  const char *msg = "The quick brown fox";

  decode_hex(
      ka_pub,
      "872f435bb8b89d0e3ad62aa2e511074ee195e1c39ef6a88001418be656e3c376");
  decode_hex(
      kb_priv,
      "b105f00db105f00db105f00db105f00db105f00db105f00db105f00db105f00d");
  decode_hex(
      expected_tag,
      "06476f1f314b06c7f96e5dc62b2308268cbdb6140aefeeb55940731863032277");

  g_assert_true(glome_derive_key(kb_priv, kb_pub) == 0);
  g_assert_true(glome_tag(/* verify */ false, counter, kb_priv, ka_pub,
                          (const uint8_t *)msg, strlen(msg), tag) == 0);

  g_assert_cmpmem(tag, sizeof tag, expected_tag, sizeof expected_tag);
}

int main(int argc, char *argv[]) {
  g_test_init(&argc, &argv, NULL);

  g_test_add_func("/test-vector1", test_vector1);
  g_test_add_func("/test-vector2", test_vector2);

  return g_test_run();
}