text: /*
   * Copyright (c) 2015 Kaprica Security, Inc.
   *
   * Permission is hereby granted, free of charge, to any person obtaining a copy
   * of this software and associated documentation files (the "Software"), to deal
   * in the Software without restriction, including without limitation the rights
   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   * copies of the Software, and to permit persons to whom the Software is
   * furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   * THE SOFTWARE.
   *
   */
  #include "cgc_stdlib.h"
  #include "cgc_string.h"
  #include "cgc_common.h"
  #include "cgc_kx.h"
  
  #include "cgc_kxdata.h"
  
  void cgc_kx_init_common(kx_t *kx)
  {
      cgc_bn_init(&kx->P);
      cgc_bn_init(&kx->Q);
      cgc_bn_init(&kx->G);
      cgc_bn_init(&kx->a);
      cgc_bn_init(&kx->b);
      cgc_bn_init(&kx->S);
  }
  
  int cgc_kx_init_std(kx_t *kx, unsigned int group)
  {
      if (group >= KX_GROUP_END)
          return FAILURE;
  
      cgc_kx_init_common(kx);
  
      if (cgc_bn_from_bytes(&kx->P, groups[group].P, sizeof(groups[group].P)) != SUCCESS)
          goto fail;
      if (cgc_bn_from_bytes(&kx->Q, groups[group].Q, sizeof(groups[group].Q)) != SUCCESS)
          goto fail;
      if (cgc_bn_from_bytes(&kx->G, groups[group].G, sizeof(groups[group].G)) != SUCCESS)
          goto fail;
      return SUCCESS;
  
  fail:
      cgc_bn_destroy(&kx->P);
      cgc_bn_destroy(&kx->Q);
      cgc_bn_destroy(&kx->G);
      return FAILURE;
  }
  
  int cgc_kx_init(kx_t *kx, bn_t *P, bn_t *Q, bn_t *G)
  {
      cgc_kx_init_common(kx);
  
      if (cgc_bn_copy(&kx->P, P) != SUCCESS)
          goto fail;
      if (cgc_bn_copy(&kx->Q, Q) != SUCCESS)
          goto fail;
      if (cgc_bn_copy(&kx->G, G) != SUCCESS)
          goto fail;
      return SUCCESS;
  
  fail:
      cgc_bn_destroy(&kx->P);
      cgc_bn_destroy(&kx->Q);
      cgc_bn_destroy(&kx->G);
      return FAILURE;
  }
  
  void cgc_kx_destroy(kx_t *kx)
  {
      cgc_bn_destroy(&kx->P);
      cgc_bn_destroy(&kx->Q);
      cgc_bn_destroy(&kx->G);
      cgc_bn_destroy(&kx->a);
      cgc_bn_destroy(&kx->b);
      cgc_bn_destroy(&kx->S);
  }
  
  int cgc_kx_gen_a(kx_t *kx, rng_t *rng)
  {
      return cgc_bn_cgc_random(&kx->a, &kx->Q, rng);
  }
  
  int cgc_kx_get_A(kx_t *kx, bn_t *A)
  {
      return cgc_bn_modexp(A, &kx->G, &kx->a, &kx->P);
  }
  
  int cgc_kx_set_b(kx_t *kx, bn_t *b)
  {
      return cgc_bn_copy(&kx->b, b);
  }
  
  int cgc_kx_get_sk(kx_t *kx, unsigned char *out, unsigned int n)
  {
      if (n >= cgc_bn_length(&kx->Q))
          return FAILURE;
  
      if (cgc_bn_modexp(&kx->S, &kx->b, &kx->a, &kx->P) != SUCCESS)
          return FAILURE;
  
      cgc_bn_to_bytes(&kx->S, out, n / 8);
      return SUCCESS;
  }
  
  int cgc_kx_self_test()
  {
      const static unsigned char test_a[] = {0x2e, 0x4e, 0x3a, 0x05, 0xd8, 0xff, 0x06, 0x2b, 0x4e, 0x46, 0xda, 0x6e, 0x12, 0x9c, 0xf3, 0x40, 0xb3, 0x07, 0x66, 0x99, 0x8a, 0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x32, 0x7e, 0x10, 0x77};
      int result = FAILURE;
      kx_t kx;
      bn_t A, b, exp;
      unsigned char k[31];
      
      cgc_bn_init(&A);
      cgc_bn_init(&b);
      cgc_bn_init(&exp);
  
      if (cgc_kx_init_std(&kx, KX_GROUP_2048_256) != SUCCESS)
          goto fail2;
  
      if (cgc_bn_from_bytes(&kx.a, test_a, sizeof(test_a)) != SUCCESS)
          goto fail;
  
      if (cgc_kx_get_A(&kx, &A) != SUCCESS)
          goto fail;
  
      if (cgc_bn_from_bytes(&exp, exp_A, sizeof(exp_A)) != SUCCESS)
          goto fail;
  
      if (cgc_bn_cmp(&A, &exp) != 0)
          goto fail;
  
      if (cgc_bn_from_bytes(&b, test_b, sizeof(test_b)) != SUCCESS)
          goto fail;
  
      if (cgc_kx_set_b(&kx, &b) != SUCCESS)
          goto fail;
  
      if (cgc_kx_get_sk(&kx, k, sizeof(k) * 8) != SUCCESS)
          goto fail;
  
      if (cgc_memcmp(k, exp_k, sizeof(k)) != 0)
          goto fail;
  
      result = SUCCESS;
  
  fail:
      cgc_kx_destroy(&kx);
  fail2:
      if (result == FAILURE)
          cgc_fdprintf(STDERR, "KX self-test FAILED!\n");
      cgc_bn_destroy(&A);
      cgc_bn_destroy(&b);
      cgc_bn_destroy(&exp);
      return result;
  }
  