/ ltc.c
ltc.c
  1  /*
  2   * Licensed under the Apache License, Version 2.0 (the "License");
  3   * you may not use this file except in compliance with the License.
  4   * See the NOTICE file distributed with this work for additional
  5   * information regarding copyright ownership.
  6   * You may obtain a copy of the License at
  7   *
  8   *     http://www.apache.org/licenses/LICENSE-2.0
  9   *
 10   * Unless required by applicable law or agreed to in writing, software
 11   * distributed under the License is distributed on an "AS IS" BASIS,
 12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13   * See the License for the specific language governing permissions and
 14   * limitations under the License.
 15   */
 16  
 17  #include <lscore/plugin.h>
 18  #include <lscore/registration.h>
 19  #include <tomcrypt.h>
 20  #include <lsplugin/mkobject.h>
 21  #include <lsplugin/mkoperator.h>
 22  #include <lsplugin/register.h>
 23  #include "ltc-methods.h"
 24  
 25  static const LSplugin_key_desc_t *ltc_key_implementations[] = {
 26    &ltc_dh_desc,
 27    &ltc_dsa_desc,
 28    &ltc_rsa_desc,
 29    &ltc_aes_desc,
 30    &ltc_des3_desc,
 31    &ltc_blowfish_desc,
 32    &ltc_twofish_desc,
 33    &ltc_cast5_desc,
 34    &ltc_kseed_desc,
 35    &ltc_camellia_desc,
 36    &ltc_chacha_desc,
 37    &ltc_rc4_desc,
 38    &ltc_poly1305_desc,
 39    &ltc_blake2b_desc,
 40    &ltc_blake2s_desc,
 41    NULL
 42  };
 43  
 44  static const LSplugin_signer_desc_t *ltc_sig_implementations[] = {
 45    &ltc_rsa_sha1_signer_desc,
 46    NULL
 47  };
 48  
 49  static const LSplugin_verifier_desc_t *ltc_ver_implementations[] = {
 50    &ltc_rsa_sha1_verifier_desc,
 51    NULL
 52  };
 53  
 54  static const LSplugin_encryptor_desc_t *ltc_enc_implementations[] = {
 55    &ltc_aes_ecb_encryptor_desc,
 56    &ltc_aes_cbc_encryptor_desc,
 57    &ltc_aes_ctr_encryptor_desc,
 58    &ltc_aes_cfb_encryptor_desc,
 59    &ltc_aes_ofb_encryptor_desc,
 60    &ltc_des3_ecb_encryptor_desc,
 61    &ltc_des3_cbc_encryptor_desc,
 62    &ltc_des3_ctr_encryptor_desc,
 63    &ltc_des3_cfb_encryptor_desc,
 64    &ltc_des3_ofb_encryptor_desc,
 65    &ltc_blowfish_ecb_encryptor_desc,
 66    &ltc_blowfish_cbc_encryptor_desc,
 67    &ltc_blowfish_ctr_encryptor_desc,
 68    &ltc_blowfish_cfb_encryptor_desc,
 69    &ltc_blowfish_ofb_encryptor_desc,
 70    &ltc_twofish_ecb_encryptor_desc,
 71    &ltc_twofish_cbc_encryptor_desc,
 72    &ltc_twofish_ctr_encryptor_desc,
 73    &ltc_twofish_cfb_encryptor_desc,
 74    &ltc_twofish_ofb_encryptor_desc,
 75    &ltc_cast5_ecb_encryptor_desc,
 76    &ltc_cast5_cbc_encryptor_desc,
 77    &ltc_cast5_ctr_encryptor_desc,
 78    &ltc_cast5_cfb_encryptor_desc,
 79    &ltc_cast5_ofb_encryptor_desc,
 80    &ltc_kseed_ecb_encryptor_desc,
 81    &ltc_kseed_cbc_encryptor_desc,
 82    &ltc_kseed_ctr_encryptor_desc,
 83    &ltc_kseed_cfb_encryptor_desc,
 84    &ltc_kseed_ofb_encryptor_desc,
 85    &ltc_camellia_ecb_encryptor_desc,
 86    &ltc_camellia_cbc_encryptor_desc,
 87    &ltc_camellia_ctr_encryptor_desc,
 88    &ltc_camellia_cfb_encryptor_desc,
 89    &ltc_camellia_ofb_encryptor_desc,
 90    &ltc_chacha_encryptor_desc,
 91    &ltc_rc4_encryptor_desc,
 92    NULL
 93  };
 94  
 95  static const LSplugin_decryptor_desc_t *ltc_dec_implementations[] = {
 96    &ltc_aes_ecb_decryptor_desc,
 97    &ltc_aes_cbc_decryptor_desc,
 98    &ltc_aes_ctr_decryptor_desc,
 99    &ltc_aes_cfb_decryptor_desc,
100    &ltc_aes_ofb_decryptor_desc,
101    &ltc_des3_ecb_decryptor_desc,
102    &ltc_des3_cbc_decryptor_desc,
103    &ltc_des3_ctr_decryptor_desc,
104    &ltc_des3_cfb_decryptor_desc,
105    &ltc_des3_ofb_decryptor_desc,
106    &ltc_blowfish_ecb_decryptor_desc,
107    &ltc_blowfish_cbc_decryptor_desc,
108    &ltc_blowfish_ctr_decryptor_desc,
109    &ltc_blowfish_cfb_decryptor_desc,
110    &ltc_blowfish_ofb_decryptor_desc,
111    &ltc_twofish_ecb_decryptor_desc,
112    &ltc_twofish_cbc_decryptor_desc,
113    &ltc_twofish_ctr_decryptor_desc,
114    &ltc_twofish_cfb_decryptor_desc,
115    &ltc_twofish_ofb_decryptor_desc,
116    &ltc_cast5_ecb_decryptor_desc,
117    &ltc_cast5_cbc_decryptor_desc,
118    &ltc_cast5_ctr_decryptor_desc,
119    &ltc_cast5_cfb_decryptor_desc,
120    &ltc_cast5_ofb_decryptor_desc,
121    &ltc_kseed_ecb_decryptor_desc,
122    &ltc_kseed_cbc_decryptor_desc,
123    &ltc_kseed_ctr_decryptor_desc,
124    &ltc_kseed_cfb_decryptor_desc,
125    &ltc_kseed_ofb_decryptor_desc,
126    &ltc_camellia_ecb_decryptor_desc,
127    &ltc_camellia_cbc_decryptor_desc,
128    &ltc_camellia_ctr_decryptor_desc,
129    &ltc_camellia_cfb_decryptor_desc,
130    &ltc_camellia_ofb_decryptor_desc,
131    &ltc_chacha_decryptor_desc,
132    &ltc_rc4_decryptor_desc,
133    NULL
134  };
135  
136  static const LSplugin_digester_desc_t *ltc_digest_implementations[] = {
137    &ltc_whirlpool_desc,
138    &ltc_sha3_512_desc,
139    &ltc_sha512_desc,
140    &ltc_blake2b_512_desc,
141    &ltc_sha3_384_desc,
142    &ltc_sha384_desc,
143    &ltc_rmd320_desc,
144    &ltc_sha512_256_desc,
145    &ltc_sha3_256_desc,
146    &ltc_sha256_desc,
147    &ltc_rmd256_desc,
148    &ltc_blake2s_256_desc,
149    &ltc_blake2b_256_desc,
150    &ltc_sha512_224_desc,
151    &ltc_sha3_224_desc,
152    &ltc_sha224_desc,
153    &ltc_blake2s_224_desc,
154    &ltc_blake2b_384_desc,
155    &ltc_tiger_desc,
156    &ltc_sha1_desc,
157    &ltc_rmd160_desc,
158    &ltc_blake2s_160_desc,
159    &ltc_blake2b_160_desc,
160    &ltc_rmd128_desc,
161    &ltc_md5_desc,
162    &ltc_md4_desc,
163    &ltc_md2_desc,
164    &ltc_blake2s_128_desc,
165    &ltc_shake128_desc,
166    &ltc_shake256_desc,
167    NULL
168  };
169  
170  static const LSplugin_mac_desc_t *ltc_mac_implementations[] = {
171    &ltc_poly1305_mac_desc,
172    &ltc_blake2b_mac_desc,
173    &ltc_blake2s_mac_desc,
174    NULL
175  };
176  
177  static const LSplugin_rbg_desc_t *ltc_rbg_implementations[] = {
178    &ltc_fortuna_desc,
179    NULL
180  };
181  
182  #ifdef LTC_STATIC_DEFINE
183  # define LSC_plugin_start ltc_plugin_start
184  # define LSC_plugin_stop ltc_plugin_stop
185  #endif
186  
187  LSC_EXPORT LE_STATUS LSC_plugin_start(LSC_plugin_t *plugin)
188  {
189    LSC_env_t *env;
190    LE_STATUS sts;
191  
192    if (!LE_status_is_OK(sts = LSC_get_plugin_parent_environment(plugin, &env)))
193      return sts;
194  
195  #define reg(T, N)                                                       \
196    do {                                                                  \
197      sts = LSplugin_register_##T##_implementations                       \
198        (env, plugin, ltc_##N##_implementations);                         \
199      if (!LE_status_is_OK(sts))                                          \
200        return sts;                                                       \
201    } while(0)
202  
203    reg(key, key);
204    reg(signer, sig);
205    reg(verifier, ver);
206    reg(encryptor, enc);
207    reg(decryptor, dec);
208    reg(digester, digest);
209    reg(mac, mac);
210    reg(rbg, rbg);
211  
212    /* Initialize LibTomCrypt */
213    /* Right now, we use standard libtommath */
214    ltc_mp = ltm_desc;
215  
216    return sts;
217  }
218  
219  LSC_EXPORT LE_STATUS LSC_plugin_stop(LSC_plugin_t *plugin)
220  {
221    LSC_env_t *env;
222    LE_STATUS sts;
223  
224    if (!LE_status_is_OK(sts = LSC_get_plugin_parent_environment(plugin, &env)))
225      return sts;
226  
227  #define dereg(T, N)                                                     \
228    do {                                                                  \
229      sts = LSplugin_deregister_##T##_implementations                     \
230        (env, plugin, ltc_##N##_implementations);                         \
231      if (!LE_status_is_OK(sts))                                          \
232        return sts;                                                       \
233    } while(0)
234  
235    dereg(rbg, rbg);
236    dereg(mac, mac);
237    dereg(digester, digest);
238    dereg(decryptor, dec);
239    dereg(encryptor, enc);
240    dereg(verifier, ver);
241    dereg(signer, sig);
242    dereg(key, key);
243  
244    return sts;
245  }