6f179e254429ef5c67d0e513acc43fbb5323186a
[openssl.git] / crypto / evp / e_aria.c
1 /*
2  * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2017, Oracle and/or its affiliates.  All rights reserved.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include "internal/cryptlib.h"
12 #ifndef OPENSSL_NO_ARIA
13 # include <openssl/evp.h>
14 # include <openssl/modes.h>
15 # include"internal/aria.h"
16 # include "internal/evp_int.h"
17
18 /* ARIA subkey Structure */
19 typedef struct {
20     ARIA_KEY ks;
21 } EVP_ARIA_KEY;
22
23 /* The subkey for ARIA is generated. */
24 static int aria_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
25                             const unsigned char *iv, int enc)
26 {
27     int ret;
28     int mode = EVP_CIPHER_CTX_mode(ctx);
29
30     if (enc || (mode != EVP_CIPH_ECB_MODE && mode != EVP_CIPH_CBC_MODE))
31         ret = aria_set_encrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
32                                         EVP_CIPHER_CTX_get_cipher_data(ctx));
33     else
34         ret = aria_set_decrypt_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8,
35                                         EVP_CIPHER_CTX_get_cipher_data(ctx));
36     if (ret < 0) {
37         EVPerr(EVP_F_ARIA_INIT_KEY,EVP_R_ARIA_KEY_SETUP_FAILED);
38         return 0;
39     }
40     return 1;
41 }
42
43 static void aria_cbc_encrypt(const unsigned char *in, unsigned char *out,
44                              size_t len, const ARIA_KEY *key,
45                              unsigned char *ivec, const int enc)
46 {
47
48     if (enc)
49         CRYPTO_cbc128_encrypt(in, out, len, key, ivec,
50                               (block128_f) aria_encrypt);
51     else
52         CRYPTO_cbc128_decrypt(in, out, len, key, ivec,
53                               (block128_f) aria_encrypt);
54 }
55
56 static void aria_cfb128_encrypt(const unsigned char *in, unsigned char *out,
57                                 size_t length, const ARIA_KEY *key,
58                                 unsigned char *ivec, int *num, const int enc)
59 {
60
61     CRYPTO_cfb128_encrypt(in, out, length, key, ivec, num, enc,
62                           (block128_f) aria_encrypt);
63 }
64
65 static void aria_cfb1_encrypt(const unsigned char *in, unsigned char *out,
66                               size_t length, const ARIA_KEY *key,
67                               unsigned char *ivec, int *num, const int enc)
68 {
69     CRYPTO_cfb128_1_encrypt(in, out, length, key, ivec, num, enc,
70                             (block128_f) aria_encrypt);
71 }
72
73 static void aria_cfb8_encrypt(const unsigned char *in, unsigned char *out,
74                               size_t length, const ARIA_KEY *key,
75                               unsigned char *ivec, int *num, const int enc)
76 {
77     CRYPTO_cfb128_8_encrypt(in, out, length, key, ivec, num, enc,
78                             (block128_f) aria_encrypt);
79 }
80
81 static void aria_ecb_encrypt(const unsigned char *in, unsigned char *out,
82                              const ARIA_KEY *key, const int enc)
83 {
84     aria_encrypt(in, out, key);
85 }
86
87 static void aria_ofb128_encrypt(const unsigned char *in, unsigned char *out,
88                              size_t length, const ARIA_KEY *key,
89                              unsigned char *ivec, int *num)
90 {
91     CRYPTO_ofb128_encrypt(in, out, length, key, ivec, num,
92                          (block128_f) aria_encrypt);
93 }
94
95 IMPLEMENT_BLOCK_CIPHER(aria_128, ks, aria, EVP_ARIA_KEY,
96                         NID_aria_128, 16, 16, 16, 128,
97                         0, aria_init_key, NULL,
98                         EVP_CIPHER_set_asn1_iv,
99                         EVP_CIPHER_get_asn1_iv,
100                         NULL)
101 IMPLEMENT_BLOCK_CIPHER(aria_192, ks, aria, EVP_ARIA_KEY,
102                         NID_aria_192, 16, 24, 16, 128,
103                         0, aria_init_key, NULL,
104                         EVP_CIPHER_set_asn1_iv,
105                         EVP_CIPHER_get_asn1_iv,
106                         NULL)
107 IMPLEMENT_BLOCK_CIPHER(aria_256, ks, aria, EVP_ARIA_KEY,
108                         NID_aria_256, 16, 32, 16, 128,
109                         0, aria_init_key, NULL,
110                         EVP_CIPHER_set_asn1_iv,
111                         EVP_CIPHER_get_asn1_iv,
112                         NULL)
113
114 # define IMPLEMENT_ARIA_CFBR(ksize,cbits) \
115                 IMPLEMENT_CFBR(aria,aria,EVP_ARIA_KEY,ks,ksize,cbits,16,0)
116 IMPLEMENT_ARIA_CFBR(128,1)
117 IMPLEMENT_ARIA_CFBR(192,1)
118 IMPLEMENT_ARIA_CFBR(256,1)
119 IMPLEMENT_ARIA_CFBR(128,8)
120 IMPLEMENT_ARIA_CFBR(192,8)
121 IMPLEMENT_ARIA_CFBR(256,8)
122
123 # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
124 static const EVP_CIPHER aria_##keylen##_##mode = { \
125         nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
126         flags|EVP_CIPH_##MODE##_MODE,   \
127         aria_init_key,                  \
128         aria_##mode##_cipher,           \
129         NULL,                           \
130         sizeof(EVP_ARIA_KEY),           \
131         NULL,NULL,NULL,NULL };          \
132 const EVP_CIPHER *EVP_aria_##keylen##_##mode(void) \
133 { return &aria_##keylen##_##mode; }
134
135 static int aria_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
136                                const unsigned char *in, size_t len)
137 {
138     unsigned int num = EVP_CIPHER_CTX_num(ctx);
139     EVP_ARIA_KEY *dat = EVP_C_DATA(EVP_ARIA_KEY,ctx);
140
141     CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
142                           EVP_CIPHER_CTX_iv_noconst(ctx),
143                           EVP_CIPHER_CTX_buf_noconst(ctx), &num,
144                           (block128_f) aria_encrypt);
145     EVP_CIPHER_CTX_set_num(ctx, num);
146     return 1;
147 }
148
149 BLOCK_CIPHER_generic(NID_aria, 128, 1, 16, ctr, ctr, CTR, 0)
150 BLOCK_CIPHER_generic(NID_aria, 192, 1, 16, ctr, ctr, CTR, 0)
151 BLOCK_CIPHER_generic(NID_aria, 256, 1, 16, ctr, ctr, CTR, 0)
152
153 #endif