test_aes128_ctr_decrypt.c 2.98 KB
Newer Older
gauthier's avatar
GPLv3    
gauthier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface 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 3 of the License, or
    (at your option) any later version.


    OpenAirInterface 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.

    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
gauthier's avatar
GPLv3    
gauthier committed
25
26
27
28

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

 *******************************************************************************/
Cedric Roux's avatar
 
Cedric Roux committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

#include "test_util.h"

#include "secu_defs.h"

#include <nettle/nettle-meta.h>
#include <nettle/aes.h>
#include <nettle/ctr.h>

static
void test_uncipher_ctr(const struct nettle_cipher *cipher, const uint8_t *key,
44
45
                       unsigned key_length, const uint8_t *cipheredtext,
                       unsigned length, const uint8_t *cleartext, const uint8_t *ictr)
Cedric Roux's avatar
 
Cedric Roux committed
46
{
47
48
49
  void *ctx = malloc(cipher->context_size);
  uint8_t *data = malloc(length);
  uint8_t *ctr = malloc(cipher->block_size);
Cedric Roux's avatar
 
Cedric Roux committed
50

51
  cipher->set_encrypt_key(ctx, key_length,  key);
52
  memcpy(ctr, ictr, cipher->block_size);
Cedric Roux's avatar
 
Cedric Roux committed
53

54
55
56
  ctr_crypt(ctx, cipher->encrypt,
            cipher->block_size, ctr,
            length, data, cipheredtext);
Cedric Roux's avatar
 
Cedric Roux committed
57

58
59
60
  if (compare_buffer(data, length, cleartext, length) != 0) {
    fail("Fail: test_uncipher_ctr\n");
  }
Cedric Roux's avatar
 
Cedric Roux committed
61

62
63
64
  free(ctx);
  free(data);
  free(ctr);
Cedric Roux's avatar
 
Cedric Roux committed
65
66
67
68
}

void doit (void)
{
69
70
71
72
73
74
75
76
  /* From NIST spec 800-38a on AES modes,
   *
   * http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38A.pdf
   *
   * F.5  CTR Example Vectors
   */
  /* F.5.1  CTR-AES128.Encrypt */
  test_uncipher_ctr(&nettle_aes128,
Cedric Roux's avatar
 
Cedric Roux committed
77
78
79
80
81
82
83
84
85
86
87
                    HL("2b7e151628aed2a6abf7158809cf4f3c"),
                    HL("6bc1bee22e409f96e93d7e117393172a"
                       "ae2d8a571e03ac9c9eb76fac45af8e51"
                       "30c81c46a35ce411e5fbc1191a0a52ef"
                       "f69f2445df4f9b17ad2b417be66c3710"),
                    H("874d6191b620e3261bef6864990db6ce"
                      "9806f66b7970fdff8617187bb9fffdff"
                      "5ae4df3edbd5d35e5b4f09020db03eab"
                      "1e031dda2fbe03d1792170a0f3009cee"),
                    H("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"));
}