Bladeren bron

prefix tweetnacl `crypto_` API

Steffen Jaeckel 6 jaren geleden
bovenliggende
commit
0b06979b10

+ 6 - 6
src/headers/tomcrypt_private.h

@@ -302,19 +302,19 @@ int dsa_int_validate_primes(const dsa_key *key, int *stat);
 
 #ifdef LTC_CURVE25519
 
-int crypto_sign(
+int tweetnacl_crypto_sign(
   unsigned char *sm,unsigned long long *smlen,
   const unsigned char *m,unsigned long long mlen,
   const unsigned char *sk, const unsigned char *pk);
-int crypto_sign_open(
+int tweetnacl_crypto_sign_open(
   int *stat,
   unsigned char *m,unsigned long long *mlen,
   const unsigned char *sm,unsigned long long smlen,
   const unsigned char *pk);
-int crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
-int crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
-int crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
-int crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
+int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, unsigned char *pk,unsigned char *sk);
+int tweetnacl_crypto_sk_to_pk(unsigned char *pk, const unsigned char *sk);
+int tweetnacl_crypto_scalarmult(unsigned char *q, const unsigned char *n, const unsigned char *p);
+int tweetnacl_crypto_scalarmult_base(unsigned char *q,const unsigned char *n);
 
 typedef int (*sk_to_pk)(unsigned char *pk ,const unsigned char *sk);
 int ec25519_import_pkcs8(const unsigned char *in, unsigned long inlen,

+ 17 - 17
src/pk/ec25519/tweetnacl.c

@@ -39,7 +39,7 @@ static int vn(const u8 *x,const u8 *y,int n)
   return (1 & ((d - 1) >> 8)) - 1;
 }
 
-static int crypto_verify_32(const u8 *x,const u8 *y)
+static int tweetnacl_crypto_verify_32(const u8 *x,const u8 *y)
 {
   return vn(x,y,32);
 }
@@ -102,7 +102,7 @@ static int neq25519(const gf a, const gf b)
   u8 c[32],d[32];
   pack25519(c,a);
   pack25519(d,b);
-  return crypto_verify_32(c,d);
+  return tweetnacl_crypto_verify_32(c,d);
 }
 
 static u8 par25519(const gf a)
@@ -171,7 +171,7 @@ sv pow2523(gf o,const gf i)
   FOR(a,16) o[a]=c[a];
 }
 
-int crypto_scalarmult(u8 *q,const u8 *n,const u8 *p)
+int tweetnacl_crypto_scalarmult(u8 *q,const u8 *n,const u8 *p)
 {
   u8 z[32];
   i64 x[80],r,i;
@@ -222,12 +222,12 @@ int crypto_scalarmult(u8 *q,const u8 *n,const u8 *p)
   return 0;
 }
 
-int crypto_scalarmult_base(u8 *q,const u8 *n)
+int tweetnacl_crypto_scalarmult_base(u8 *q,const u8 *n)
 {
-  return crypto_scalarmult(q,n,_9);
+  return tweetnacl_crypto_scalarmult(q,n,_9);
 }
 
-static int crypto_hash(u8 *out,const u8 *m,u64 n)
+static int tweetnacl_crypto_hash(u8 *out,const u8 *m,u64 n)
 {
   unsigned long len;
   int err, hash_idx;
@@ -309,11 +309,11 @@ sv scalarbase(gf p[4],const u8 *s)
   scalarmult(p,q,s);
 }
 
-int crypto_sk_to_pk(u8 *pk, const u8 *sk)
+int tweetnacl_crypto_sk_to_pk(u8 *pk, const u8 *sk)
 {
   u8 d[64];
   gf p[4];
-  crypto_hash(d, sk, 32);
+  tweetnacl_crypto_hash(d, sk, 32);
   d[0] &= 248;
   d[31] &= 127;
   d[31] |= 64;
@@ -324,7 +324,7 @@ int crypto_sk_to_pk(u8 *pk, const u8 *sk)
   return 0;
 }
 
-int crypto_sign_keypair(prng_state *prng, int wprng, u8 *pk, u8 *sk)
+int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, u8 *pk, u8 *sk)
 {
   int err;
 
@@ -337,7 +337,7 @@ int crypto_sign_keypair(prng_state *prng, int wprng, u8 *pk, u8 *sk)
      return CRYPT_ERROR_READPRNG;
   }
 
-  if ((err = crypto_sk_to_pk(pk, sk)) != CRYPT_OK) {
+  if ((err = tweetnacl_crypto_sk_to_pk(pk, sk)) != CRYPT_OK) {
      return err;
   }
 
@@ -382,13 +382,13 @@ sv reduce(u8 *r)
   modL(r,x);
 }
 
-int crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk,const u8 *pk)
+int tweetnacl_crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk,const u8 *pk)
 {
   u8 d[64],h[64],r[64];
   i64 i,j,x[64];
   gf p[4];
 
-  crypto_hash(d, sk, 32);
+  tweetnacl_crypto_hash(d, sk, 32);
   d[0] &= 248;
   d[31] &= 127;
   d[31] |= 64;
@@ -397,13 +397,13 @@ int crypto_sign(u8 *sm,u64 *smlen,const u8 *m,u64 n,const u8 *sk,const u8 *pk)
   FOR(i,(i64)n) sm[64 + i] = m[i];
   FOR(i,32) sm[32 + i] = d[32 + i];
 
-  crypto_hash(r, sm+32, n+32);
+  tweetnacl_crypto_hash(r, sm+32, n+32);
   reduce(r);
   scalarbase(p,r);
   pack(sm,p);
 
   FOR(i,32) sm[i+32] = pk[i];
-  crypto_hash(h,sm,n + 64);
+  tweetnacl_crypto_hash(h,sm,n + 64);
   reduce(h);
 
   FOR(i,64) x[i] = 0;
@@ -450,7 +450,7 @@ static int unpackneg(gf r[4],const u8 p[32])
   return 0;
 }
 
-int crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
+int tweetnacl_crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
 {
   u64 i;
   u8 s[32],t[32],h[64];
@@ -466,7 +466,7 @@ int crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
   XMEMMOVE(m,sm,n);
   XMEMMOVE(s,m + 32,32);
   XMEMMOVE(m + 32,pk,32);
-  crypto_hash(h,m,n);
+  tweetnacl_crypto_hash(h,m,n);
   reduce(h);
   scalarmult(p,q,h);
 
@@ -475,7 +475,7 @@ int crypto_sign_open(int *stat, u8 *m,u64 *mlen,const u8 *sm,u64 n,const u8 *pk)
   pack(t,p);
 
   n -= 64;
-  if (crypto_verify_32(sm, t)) {
+  if (tweetnacl_crypto_verify_32(sm, t)) {
     FOR(i,n) m[i] = 0;
     zeromem(m, n);
     return CRYPT_OK;

+ 1 - 1
src/pk/ed25519/ed25519_import_pkcs8.c

@@ -28,7 +28,7 @@ int ed25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
                                   const void *pwd, unsigned long pwdlen,
                               curve25519_key *key)
 {
-   return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, PKA_ED25519, crypto_sk_to_pk, key);
+   return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, PKA_ED25519, tweetnacl_crypto_sk_to_pk, key);
 }
 
 #endif

+ 1 - 1
src/pk/ed25519/ed25519_make_key.c

@@ -29,7 +29,7 @@ int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key)
    LTC_ARGCHK(prng != NULL);
    LTC_ARGCHK(key  != NULL);
 
-   if ((err = crypto_sign_keypair(prng, wprng, key->pub, key->priv)) != CRYPT_OK) {
+   if ((err = tweetnacl_crypto_sign_keypair(prng, wprng, key->pub, key->priv)) != CRYPT_OK) {
       return err;
    }
 

+ 1 - 1
src/pk/ed25519/ed25519_set_key.c

@@ -37,7 +37,7 @@ int ed25519_set_key(const unsigned char *sk, unsigned long sklen,
    if (sk != NULL) {
       LTC_ARGCHK(sklen == 32uL);
       XMEMCPY(key->priv, sk, sizeof(key->priv));
-      crypto_sk_to_pk(key->pub, key->priv);
+      tweetnacl_crypto_sk_to_pk(key->pub, key->priv);
       if (pk != NULL) {
          LTC_ARGCHK(pklen == 32uL);
          if (XMEM_NEQ(pk, key->pub, sizeof(key->pub)) != 0) {

+ 3 - 3
src/pk/ed25519/ed25519_sign.c

@@ -48,9 +48,9 @@ int ed25519_sign(const unsigned char  *msg, unsigned long msglen,
    s = XMALLOC(smlen);
    if (s == NULL) return CRYPT_MEM;
 
-   err = crypto_sign(s, &smlen,
-                     msg, msglen,
-                     private_key->priv, private_key->pub);
+   err = tweetnacl_crypto_sign(s, &smlen,
+                               msg, msglen,
+                               private_key->priv, private_key->pub);
 
    XMEMCPY(sig, s, 64uL);
    *siglen = 64uL;

+ 4 - 4
src/pk/ed25519/ed25519_verify.c

@@ -51,10 +51,10 @@ int ed25519_verify(const  unsigned char *msg, unsigned long msglen,
    XMEMCPY(m, sig, siglen);
    XMEMCPY(m + siglen, msg, msglen);
 
-   err = crypto_sign_open(stat,
-                          m, &mlen,
-                          m, mlen,
-                          public_key->pub);
+   err = tweetnacl_crypto_sign_open(stat,
+                                    m, &mlen,
+                                    m, mlen,
+                                    public_key->pub);
 
 #ifdef LTC_CLEAN_STACK
    zeromem(m, mlen);

+ 1 - 1
src/pk/x25519/x25519_import_pkcs8.c

@@ -28,7 +28,7 @@ int x25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
                        const void *pwd, unsigned long pwdlen,
                        curve25519_key *key)
 {
-   return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, PKA_X25519, crypto_scalarmult_base, key);
+   return ec25519_import_pkcs8(in, inlen, pwd, pwdlen, PKA_X25519, tweetnacl_crypto_scalarmult_base, key);
 }
 
 #endif

+ 1 - 1
src/pk/x25519/x25519_make_key.c

@@ -37,7 +37,7 @@ int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key)
       return CRYPT_ERROR_READPRNG;
    }
 
-   crypto_scalarmult_base(key->pub, key->priv);
+   tweetnacl_crypto_scalarmult_base(key->pub, key->priv);
 
    key->type = PK_PRIVATE;
    key->algo = PKA_X25519;

+ 1 - 1
src/pk/x25519/x25519_set_key.c

@@ -37,7 +37,7 @@ int x25519_set_key(const unsigned char *k, unsigned long klen,
    if (k != NULL) {
       LTC_ARGCHK(klen == 32uL);
       XMEMCPY(key->priv, k, sizeof(key->priv));
-      crypto_scalarmult_base(key->pub, key->priv);
+      tweetnacl_crypto_scalarmult_base(key->pub, key->priv);
       if (u != NULL) {
          LTC_ARGCHK(ulen == 32uL);
          if (XMEM_NEQ(u, key->pub, sizeof(key->pub)) != 0) {

+ 1 - 1
src/pk/x25519/x25519_shared_secret.c

@@ -39,7 +39,7 @@ int x25519_shared_secret(const    curve25519_key *private_key,
       return CRYPT_BUFFER_OVERFLOW;
    }
 
-   crypto_scalarmult(out, private_key->priv, public_key->pub);
+   tweetnacl_crypto_scalarmult(out, private_key->priv, public_key->pub);
    *outlen = 32uL;
 
    return CRYPT_OK;

+ 1 - 1
tests/x25519_test.c

@@ -56,7 +56,7 @@ static int _rfc_7748_5_2_test(void)
    unsigned long n;
 
    for (n = 0; n < sizeof(rfc_7748_5_2)/sizeof(rfc_7748_5_2[0]); ++n) {
-      crypto_scalarmult(out, rfc_7748_5_2[n].scalar, rfc_7748_5_2[n].u_in);
+      tweetnacl_crypto_scalarmult(out, rfc_7748_5_2[n].scalar, rfc_7748_5_2[n].u_in);
       if (compare_testvector(out, sizeof(out), rfc_7748_5_2[n].u_out, sizeof(rfc_7748_5_2[n].u_out), "x25519 RFC 7748 Ch. 5.2", n) != 0) {
          return CRYPT_FAIL_TESTVECTOR;
       }