BOLOS TEE
bolos_crypto.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 * BOLOS TEE
4 * (c) 2016, 2017 Ledger
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 ********************************************************************************/
18 
31 #ifndef __BOLOS_CRYPTO_H__
32 
33 #define __BOLOS_CRYPTO_H__
34 
35 /* ####################################################################### */
36 /* RAND */
37 /* ####################################################################### */
38 
44 uint8_t bls_rng_u8(void);
45 
46 
54 int bls_rng(uint8_t *buffer, size_t len);
55 
56 
57 /* ####################################################################### */
58 /* HASH/HMAC */
59 /* ####################################################################### */
60 
61 /* ======================================================================= */
62 /* HASH */
63 /* ======================================================================= */
64 
74 
83 int bls_sha1_init(bls_sha1_t *hash);
84 
93 int bls_sha256_init(bls_sha256_t *hash);
94 
103 int bls_sha512_init(bls_sha512_t *hash);
104 
116 int bls_sha3_init(bls_sha3_t *hash, int size);
117 
129 int bls_keccak_init(bls_sha3_t *hash, int size);
130 
161 int bls_hash(bls_hash_t *hash, int mode,
162  const uint8_t WIDE *in, size_t len, uint8_t *out);
163 
164 /* ======================================================================= */
165 /* HASH MAC */
166 /* ======================================================================= */
167 
183 int bls_hmac_ripemd160_init(bls_hmac_ripemd160_t *hmac, const uint8_t WIDE *key, size_t key_len);
184 
200 int bls_hmac_sha256_init(bls_hmac_sha256_t *hmac, const uint8_t WIDE *key, size_t key_len);
201 
217 int bls_hmac_sha512_init(bls_hmac_sha512_t *hmac, const uint8_t WIDE *key, size_t key_len);
218 
250 int bls_hmac(bls_hmac_t *hmac, int mode,
251  const uint8_t WIDE *in, size_t len, uint8_t *mac);
252 
253 /* ####################################################################### */
254 /* KEY STRETCHING */
255 /* ####################################################################### */
256 
281 int bls_pbkdf2(bls_md_t hash,
282  const bls_area_t *password,
283  const bls_area_t *salt,
284  int iterations,
285  uint8_t *out);
286 
287 /* ####################################################################### */
288 /* CIPHER/SIGNATURE */
289 /* ####################################################################### */
290 /* - DES
291  * - ECDSA
292  * - ECDH
293  * - RSA
294  */
295 
296 
297 /* ======================================================================= */
298 /* DES */
299 /* ======================================================================= */
300 
301 
322 int bls_des_init_key(const uint8_t WIDE *rawkey,
323  size_t key_len,
324  bls_des_key_t *key);
325 
326 
364 int bls_des(bls_des_key_t WIDE *key, int mode,
365  const bls_area_t *in, bls_area_t *out);
366 
367 
368 
412 int bls_des_iv(bls_des_key_t WIDE *key, int mode,
413  const bls_area_t *iv,
414  const bls_area_t *in, bls_area_t *out);
415 
416 
417 /* ======================================================================= */
418 /* AES */
419 /* ======================================================================= */
420 
421 
442 int bls_aes_init_key(const uint8_t WIDE *rawkey,
443  size_t key_len,
444  bls_aes_key_t *key);
445 
446 
488 int bls_aes(bls_aes_key_t WIDE *key, int mode,
489  const bls_area_t *in, bls_area_t *out);
490 
491 
538 int bls_aes_iv(bls_aes_key_t WIDE *key, int mode,
539  const bls_area_t *iv,
540  const bls_area_t *in, bls_area_t *out);
541 
542 
581 int bls_aes_iv_gcm(bls_aes_key_t WIDE *key, int mode,
582  const bls_area_t *in,
583  const bls_area_t *iv,
584  const bls_area_t WIDE *aadTag, // encrypt : aad in/tag out | decrypt : tag in
585  bls_area_t *out);
586 
587 
588 /* ======================================================================= */
589 /* RSA */
590 /* ======================================================================= */
591 
610 int bls_rsa_init_public_key(const bls_rsa_keypair_data_t WIDE *keyData,
612 
631 int bls_rsa_init_private_key(const bls_rsa_keypair_data_t WIDE *keyData,
633 
654 int bls_rsa_init_private_key_crt(const bls_rsa_crt_t WIDE *crtParameters,
656 
677 int bls_rsa_generate_keypair(int modulus_len, // in bits
678  bls_rsa_abstract_private_key_t *privateKey,
680  bls_rsa_keypair_data_t *generatedKeypairInfo
681  );
682 
697  bls_rsa_keypair_data_t *keyInfo);
698 
730 int bls_rsa_pub(bls_rsa_abstract_public_key_t WIDE *key, int mode,
731  bls_md_t hashID, const bls_area_t *src,
732  const bls_area_t *dest);
733 
765 int bls_rsa_priv(bls_rsa_abstract_private_key_t WIDE *key, int mode,
766  bls_md_t hashID, const bls_area_t *src,
767  const bls_area_t *dest);
768 
769 
770 /* ======================================================================= */
771 /* ECDSA */
772 /* ======================================================================= */
773 
774 
788 bls_curve_domain_t WIDE * bls_ecfp_get_domain(bls_curve_t curve);
789 
790 
809 int bls_ecfp_is_valid_point(const bls_curve_domain_t WIDE *domain, const uint8_t WIDE *point);
810 
842 int bls_ecfp_add_point(const bls_curve_domain_t WIDE *domain, uint8_t *R,
843  const uint8_t WIDE *P, const uint8_t WIDE *Q);
844 
872 int bls_ecdsa_init_public_key(bls_curve_t curve,
873  const uint8_t WIDE *rawkey, size_t key_len,
874  bls_ecfp_public_key_t *key);
875 
901 int bls_ecdsa_init_private_key(bls_curve_t curve,
902  const uint8_t WIDE *rawkey, size_t key_len,
923 int bls_ecfp_generate_pair(bls_curve_t curve,
924  bls_ecfp_public_key_t *public_key, bls_ecfp_private_key_t *private_key,
925  uint8_t *d);
926 
941  uint8_t *W);
942 
975 int bls_ecdsa_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID,
976  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1008 int bls_ecdsa_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID,
1009  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1010 
1041 int bls_schnorr_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID,
1042  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1074 int bls_schnorr_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID,
1075  const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig);
1076 
1077 
1078 /* ======================================================================= */
1079 /* ECDH */
1080 /* ======================================================================= */
1081 
1110 int bls_ecdh(bls_ecfp_private_key_t WIDE *key, int mode,
1111  const uint8_t WIDE *public_point, uint8_t *secret);
1112 
1113 
1114 /* ======================================================================= */
1115 /* CRC */
1116 /* ======================================================================= */
1117 
1134 uint16_t bls_crc16(const void WIDE *buffer, size_t len);
1135 
1136 
1156 uint16_t bls_crc16_update(unsigned short crc, const void WIDE *buffer, size_t len);
1157 
1158 
1159 /* ======================================================================= */
1160 /* MATH */
1161 /* ======================================================================= */
1162 
1163 
1178 void bls_math_addm(uint8_t* r, const uint8_t WIDE *a, const uint8_t WIDE *b, const uint8_t WIDE *m, size_t len);
1179 
1180 
1194 int bls_math_cmp(const uint8_t WIDE *a, const uint8_t WIDE *b, size_t len);
1195 
1206 int bls_math_is_zero(const uint8_t WIDE *a, size_t len);
1207 
1219 void bls_math_modm(uint8_t *v, size_t len_v,
1220  const uint8_t WIDE *m, size_t len_m);
1221 
1222 #endif //__BOLOS_CRYPTO_H__
1223 
void bls_math_modm(uint8_t *v, size_t len_v, const uint8_t WIDE *m, size_t len_m)
Reduce in place (left zero padded) the given value : v = v mod m.
int bls_math_cmp(const uint8_t WIDE *a, const uint8_t WIDE *b, size_t len)
Compare to unsigned long big-endian integer.
int bls_rsa_init_public_key(const bls_rsa_keypair_data_t WIDE *keyData, bls_rsa_abstract_public_key_t *key)
Initialize a public RSA Key.
int bls_ecfp_generate_pair(bls_curve_t curve, bls_ecfp_public_key_t *public_key, bls_ecfp_private_key_t *private_key, uint8_t *d)
Generate a ecfp key pair.
int bls_ecdsa_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Sign a hash message according to ECDSA scheme.
Definition: bolos_crypto_platform_tee.h:83
int bls_schnorr_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Verify a hash message signature according to Schnorr scheme.
uint8_t bls_rng_u8(void)
generate a random byte
int bls_aes_iv_gcm(bls_aes_key_t WIDE *key, int mode, const bls_area_t *in, const bls_area_t *iv, const bls_area_t WIDE *aadTag, bls_area_t *out)
Add data to an AES GCM operation.
int bls_des_iv(bls_des_key_t WIDE *key, int mode, const bls_area_t *iv, const bls_area_t *in, bls_area_t *out)
Add data to a DES operation using an Initialization Vector.
int bls_rsa_pub(bls_rsa_abstract_public_key_t WIDE *key, int mode, bls_md_t hashID, const bls_area_t *src, const bls_area_t *dest)
Perform a RSA public operation.
int bls_rsa_init_private_key(const bls_rsa_keypair_data_t WIDE *keyData, bls_rsa_abstract_private_key_t *key)
Initialize a private RSA Key.
int bls_sha512_init(bls_sha512_t *hash)
Initialize a sha512 context.
Definition: bolos_crypto_platform_tee.h:73
int bls_ecdsa_init_private_key(bls_curve_t curve, const uint8_t WIDE *rawkey, size_t key_len, bls_ecfp_private_key_t *key)
Initialize a private ECFP Key.
int bls_des(bls_des_key_t WIDE *key, int mode, const bls_area_t *in, bls_area_t *out)
Add data to a DES operation.
int bls_rsa_generate_keypair(int modulus_len, bls_rsa_abstract_private_key_t *privateKey, bls_rsa_abstract_public_key_t *publicKey, bls_rsa_keypair_data_t *generatedKeypairInfo)
Generate a RSA keypair.
Definition: bolos_crypto_platform_tee.h:145
uint16_t bls_crc16_update(unsigned short crc, const void WIDE *buffer, size_t len)
Update a 16 bits checksum value.
int bls_ecfp_add_point(const bls_curve_domain_t WIDE *domain, uint8_t *R, const uint8_t WIDE *P, const uint8_t WIDE *Q)
Add two affine point.
int bls_des_init_key(const uint8_t WIDE *rawkey, size_t key_len, bls_des_key_t *key)
Initialize a DES Key.
int bls_hmac_ripemd160_init(bls_hmac_ripemd160_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha512 context.
Definition: bolos_crypto_platform_tee.h:45
int bls_aes_init_key(const uint8_t WIDE *rawkey, size_t key_len, bls_aes_key_t *key)
Initialize an AES Key.
Definition: bolos_crypto_platform_tee.h:149
Definition: bolos_crypto_platform_tee.h:107
int bls_hmac(bls_hmac_t *hmac, int mode, const uint8_t WIDE *in, size_t len, uint8_t *mac)
Add more data to HMAC.
Definition: bolos_crypto_platform_tee.h:119
int bls_aes(bls_aes_key_t WIDE *key, int mode, const bls_area_t *in, bls_area_t *out)
Add data to an AES operation.
int bls_rng(uint8_t *buffer, size_t len)
generate a random buffer
int bls_math_is_zero(const uint8_t WIDE *a, size_t len)
Compare to unsigned long big-endian integer to zero.
Definition: bolos_crypto_platform_tee.h:130
Definition: bolos_crypto_platform_tee.h:50
Definition: bolos_crypto_common.h:218
int bls_rsa_init_private_key_crt(const bls_rsa_crt_t WIDE *crtParameters, bls_rsa_abstract_private_key_t *key)
Initialize a private RSA Key with CRT parameters.
Definition: bolos_crypto_platform_tee.h:135
Definition: bolos_crypto_common.h:204
int bls_ecdsa_init_public_key(bls_curve_t curve, const uint8_t WIDE *rawkey, size_t key_len, bls_ecfp_public_key_t *key)
Initialize a public ECFP Key.
Definition: bolos_crypto_platform_tee.h:40
int bls_schnorr_sign(bls_ecfp_private_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Sign a hash message according to Schnorr scheme.
int bls_ecdsa_verify(bls_ecfp_public_key_t WIDE *key, int mode, bls_md_t hashID, const uint8_t WIDE *hash, size_t hash_len, uint8_t *sig)
Verify a hash message signature according to ECDSA scheme.
int bls_ecfp_is_valid_point(const bls_curve_domain_t WIDE *domain, const uint8_t WIDE *point)
Verify that a given point is really on the specified curve.
int bls_rsa_get_public_key_data(bls_rsa_abstract_public_key_t *publicKey, bls_rsa_keypair_data_t *keyInfo)
Retrieve a RSA public key information.
Definition: bolos_crypto_platform_tee.h:153
int bls_hash(bls_hash_t *hash, int mode, const uint8_t WIDE *in, size_t len, uint8_t *out)
Add more data to hash.
int bls_hmac_sha256_init(bls_hmac_sha256_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha256 context.
Definition: bolos_crypto_platform_tee.h:55
int bls_hmac_sha512_init(bls_hmac_sha512_t *hmac, const uint8_t WIDE *key, size_t key_len)
Initialize a HMAC sha512 context.
int bls_ecfp_get_public_component(const bls_ecfp_public_key_t *public_key, uint8_t *W)
Return the uncompressed point of a public ECFP Key.
int bls_pbkdf2(bls_md_t hash, const bls_area_t *password, const bls_area_t *salt, int iterations, uint8_t *out)
Stretch a password using PBKDF2.
Definition: bolos_crypto_platform_tee.h:68
int bls_aes_iv(bls_aes_key_t WIDE *key, int mode, const bls_area_t *iv, const bls_area_t *in, bls_area_t *out)
Add data to an AES operation using an Initialization Vector.
int bls_sha3_init(bls_sha3_t *hash, int size)
Initialize a sha3 context.
int bls_rsa_priv(bls_rsa_abstract_private_key_t WIDE *key, int mode, bls_md_t hashID, const bls_area_t *src, const bls_area_t *dest)
Perform a RSA private operation.
uint16_t bls_crc16(const void WIDE *buffer, size_t len)
Compute a 16 bits checksum value.
Describe a buffer and length data structure.
Definition: bolos_core.h:38
int bls_ripemd160_init(bls_ripemd160_t *hash)
Initialize a ripmd160 context.
int bls_keccak_init(bls_sha3_t *hash, int size)
Initialize a Keccak (pre-release sha3) context.
bls_curve_domain_t WIDE * bls_ecfp_get_domain(bls_curve_t curve)
Retrieve the domain of the curve.
void bls_math_addm(uint8_t *r, const uint8_t WIDE *a, const uint8_t WIDE *b, const uint8_t WIDE *m, size_t len)
Modular addition of tow big integer of the size: r = a+b mod m.
int bls_ecdh(bls_ecfp_private_key_t WIDE *key, int mode, const uint8_t WIDE *public_point, uint8_t *secret)
Compute a shared secret according to ECDH specifiaction.
int bls_sha1_init(bls_sha1_t *hash)
Initialize a sha1 context.
int bls_sha256_init(bls_sha256_t *hash)
Initialize a sha256 context.
Definition: bolos_crypto_platform_tee.h:35
Definition: bolos_crypto_platform_tee.h:78