Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
91d2b9a
Revert "cardano-crypto-wallet: rename ed25519 C symbols from cardano_…
lehins Jun 4, 2026
7f98af1
Re-export `byteArrayFromShortByteString`, `byteArrayToShortByteString`.
lehins May 30, 2026
4397c90
Add `byteArrayFromByteString`
lehins May 30, 2026
b9010b0
Add `psbToByteArray`
lehins May 30, 2026
a94caf2
Add `psbFromByteStringM`
lehins May 30, 2026
a2c868d
Add the odd `output` file to gitignore
lehins May 30, 2026
6b5edc2
Introduce `SecretKey`
lehins May 30, 2026
b51bc91
Make `PublicKey` into a `newtype`
lehins May 30, 2026
2c72a01
Introduce `withDecryptedKeyMaterial`
lehins May 31, 2026
7165b0a
Introduce proper scoping in `withEncryptedKeyOutput`
lehins May 31, 2026
0fa54e9
Introduce `Validity` parameter to `KeyMaterial`
lehins May 31, 2026
1f55856
Improve `EncryptedKey` type safety
lehins May 31, 2026
dc49f21
Make `ChainCode` into a `newtype`
lehins May 31, 2026
0e52faa
Improve scoping of `decryptKeyMaterialV2`
lehins May 31, 2026
f93c85e
Rename "Encrypted" -> "Secret/Unencrypted"
lehins May 31, 2026
21c39fd
Simplify validation of `KeyMaterial`
lehins May 31, 2026
e67db24
Introduce `KeyMaterialBuffer`
lehins May 31, 2026
d922ac1
Avoid unnecessarily allocating `CKeyMaterialBuffer`
lehins May 31, 2026
750d89d
Remove confusing `encrypted_` suffix
lehins May 31, 2026
38445e0
Remove redundant `legacyKeySize`
lehins May 31, 2026
bee08e4
More type safety for `Nonce` and `Salt`
lehins May 31, 2026
db74c85
More type safety for `Tag`
lehins May 31, 2026
f05aa5f
WIP rename plaintext to secret_key
lehins May 31, 2026
627206e
Rename Ciphertext to EncSecretKey
lehins May 31, 2026
9f49b89
Remove another redundant argument to decrypt C function
lehins May 31, 2026
3c41781
Introduce `WrappingKey` and remove redundant parameters
lehins May 31, 2026
1387291
Rename `Envelope` and other minor cleanup
lehins May 31, 2026
7e1d046
Expose safe parts of the `Envelope`
lehins Jun 1, 2026
8f3169d
Consistent and unique naming for C functions and FFI
lehins Jun 4, 2026
55f8c52
Switch to using consistent `CCW` definition everywhere
lehins Jun 4, 2026
7587f86
TODO: confirm. Ensure more bytes than necessary is not copied over.
lehins Jun 4, 2026
599cbb6
Stop writing zeros into memory that is guaranteed to be overwritten
lehins Jun 4, 2026
610e241
Ensure sizes match up
lehins Jun 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ stack-local.yaml

# Test artefacts
/result-*
cardano-crypto-wallet/output
cardano-crypto-class/output
cardano-crypto-praos/output

Expand Down
5 changes: 3 additions & 2 deletions cardano-base/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog for `cardano-base`

## 0.1.5.1
## 0.1.6.0

*
* Re-export `byteArrayFromShortByteString`, `byteArrayToShortByteString`.
* Deprecate `byteStringToByteArray` in favor of newly added `byteArrayFromByteString`

## 0.1.5.0

Expand Down
2 changes: 1 addition & 1 deletion cardano-base/cardano-base.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-base
version: 0.1.5.0
version: 0.1.6.0
synopsis: Various utilities for Cardano
description: Various utilities for Cardano.
category:
Expand Down
8 changes: 8 additions & 0 deletions cardano-base/src/Cardano/Base/Bytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
{-# LANGUAGE TypeApplications #-}

module Cardano.Base.Bytes (
byteArrayFromByteString,
byteArrayToByteString,
byteStringToByteArray,
byteArrayFromShortByteString,
byteArrayToShortByteString,
slice,
splitsAt,
)
Expand All @@ -22,9 +25,14 @@ byteArrayToByteString :: ByteArray -> ByteString
byteArrayToByteString = SBS.fromShort . byteArrayToShortByteString
{-# INLINE byteArrayToByteString #-}

byteArrayFromByteString :: ByteString -> ByteArray
byteArrayFromByteString = byteArrayFromShortByteString . SBS.toShort
{-# INLINE byteArrayFromByteString #-}

byteStringToByteArray :: ByteString -> ByteArray
byteStringToByteArray = byteArrayFromShortByteString . SBS.toShort
{-# INLINE byteStringToByteArray #-}
{-# DEPRECATED byteStringToByteArray "In favor of more consistently named `byteArrayFromByteString`" #-}

slice :: Word -> Word -> ByteString -> ByteString
slice offset size =
Expand Down
5 changes: 3 additions & 2 deletions cardano-crypto-class/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog for `cardano-crypto-class`

## 2.5.0.1
## 2.5.1.0

*
* Add `psbToByteArray`
* Add `psbFromByteStringM`

## 2.5.0.0

Expand Down
4 changes: 2 additions & 2 deletions cardano-crypto-class/cardano-crypto-class.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: cardano-crypto-class
version: 2.5.0.0
version: 2.5.1.0
synopsis:
Type classes abstracting over cryptography primitives for Cardano

Expand Down Expand Up @@ -125,7 +125,7 @@ library
aeson,
base16-bytestring >=1,
bytestring,
cardano-base >=0.1.2,
cardano-base >=0.1.6,
cardano-binary >=1.7.3,
cardano-strict-containers,
cborg,
Expand Down
26 changes: 22 additions & 4 deletions cardano-crypto-class/src/Cardano/Crypto/PinnedSizedBytes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ module Cardano.Crypto.PinnedSizedBytes (
-- * Conversions
psbFromBytes,
psbToBytes,
psbToByteArray,
psbFromByteString,
psbFromByteStringM,
psbFromByteStringCheck,
psbToByteString,

Expand Down Expand Up @@ -76,6 +78,7 @@ import GHC.Exts (Int (..), copyAddrToByteArray#)
import GHC.Ptr (Ptr (..))

import qualified Data.ByteString as BS
import qualified Data.ByteString.Short as SBS
import qualified Data.Primitive as Prim

import Cardano.Crypto.Libsodium.C (c_sodium_compare)
Expand Down Expand Up @@ -186,8 +189,11 @@ instance KnownNat n => IsString (Code Q (PinnedSizedBytes n)) where
psbToBytes :: PinnedSizedBytes n -> [Word8]
psbToBytes (PSB ba) = foldrByteArray (:) [] ba

psbToByteArray :: PinnedSizedBytes n -> ByteArray
psbToByteArray (PSB ba) = ba

psbToByteString :: PinnedSizedBytes n -> BS.ByteString
psbToByteString = BS.pack . psbToBytes
psbToByteString = SBS.fromShort . byteArrayToShortByteString . psbToByteArray

psbToPackedBytes :: KnownNat n => PinnedSizedBytes n -> PackedBytes n
psbToPackedBytes (PSB ba) = packBytes (byteArrayToShortByteString ba) 0
Expand Down Expand Up @@ -227,16 +233,28 @@ psbFromByteString bs =
Just psb -> psb

psbFromByteStringCheck :: forall n. KnownNat n => BS.ByteString -> Maybe (PinnedSizedBytes n)
psbFromByteStringCheck bs
| BS.length bs == size = Just $
psbFromByteStringCheck = psbFromByteStringM

psbFromByteStringM ::
forall n m.
(KnownNat n, MonadFail m) =>
BS.ByteString -> m (PinnedSizedBytes n)
psbFromByteStringM bs
| n == size = pure $
unsafeDupablePerformIO $
BS.useAsCStringLen bs $ \(Ptr addr#, _) -> do
marr@(MutableByteArray marr#) <- newPinnedByteArray size
primitive_ $ copyAddrToByteArray# addr# marr# 0# (case size of I# s -> s)
arr <- unsafeFreezeByteArray marr
return (PSB arr)
| otherwise = Nothing
| otherwise =
fail $
"Supplied ByteString with size: "
<> show n
<> " did not match the expected number of bytes: "
<> show size
where
n = BS.length bs
size :: Int
size = fromInteger (natVal (Proxy :: Proxy n))

Expand Down
4 changes: 2 additions & 2 deletions cardano-crypto-class/src/Cardano/Crypto/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module Cardano.Crypto.Util (
)
where

import Cardano.Base.Bytes (byteStringToByteArray)
import Cardano.Base.Bytes (byteArrayFromByteString)
import Control.Monad (unless)
import Data.Array.Byte (ByteArray (..))
import Data.Bifunctor (first)
Expand Down Expand Up @@ -120,7 +120,7 @@ naturalToBytes = writeBinaryNatural
-- | The inverse of 'bytesToNatural'. Note that this is a naive implementation
-- and only suitable for tests.
naturalToByteArray :: Int -> Natural -> ByteArray
naturalToByteArray numBytes = byteStringToByteArray . writeBinaryNatural numBytes
naturalToByteArray numBytes = byteArrayFromByteString . writeBinaryNatural numBytes

-- | Create a 'Integer' out of a 'ByteString', in big endian.
bytesToInteger :: ByteString -> Integer
Expand Down
2 changes: 1 addition & 1 deletion cardano-crypto-wallet/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ main = do
, bench "sign-v2 (encryptedSign)" $
whnfIO (encryptedSign key testPass testMsg)
, bench "change-passphrase (encryptedChangePass)" $
whnfIO (encryptedChangePass testPass newPass key)
whnfIO (encryptedChangePassphrase testPass newPass key)
]
3 changes: 3 additions & 0 deletions cardano-crypto-wallet/cardano-crypto-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ library

build-depends:
bytestring,
cardano-binary,
cardano-crypto-class,
cborg,
deepseq,
FailT,
memory,

pkgconfig-depends: libsodium
Expand Down Expand Up @@ -97,6 +99,7 @@ test-suite tests
cardano-crypto-wallet:{cardano-crypto-wallet, testlib},
cborg,
hspec,
FailT,

benchmark bench
import: base, project-config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ ge25519_is_neutral_vartime(const ge25519 *p) {
}

int
ED25519_FN(ed25519_sign_open_batch) (const unsigned char **m, size_t *mlen, const unsigned char **pk, const unsigned char **RS, size_t num, int *valid) {
CCW_FN(ed25519_sign_open_batch) (const unsigned char **m, size_t *mlen, const unsigned char **pk, const unsigned char **RS, size_t num, int *valid) {
batch_heap ALIGN(16) batch;
ge25519 ALIGN(16) p;
bignum256modm *r_scalars;
Expand All @@ -217,7 +217,7 @@ ED25519_FN(ed25519_sign_open_batch) (const unsigned char **m, size_t *mlen, cons
batchsize = (num > max_batch_size) ? max_batch_size : num;

/* generate r (scalars[batchsize+1]..scalars[2*batchsize] */
ED25519_FN(ed25519_randombytes_unsafe) (batch.r, batchsize * 16);
CCW_FN(ed25519_randombytes_unsafe) (batch.r, batchsize * 16);
r_scalars = &batch.scalars[batchsize + 1];
for (i = 0; i < batchsize; i++)
expand256_modm(r_scalars[i], batch.r[i], 16);
Expand Down Expand Up @@ -252,7 +252,7 @@ ED25519_FN(ed25519_sign_open_batch) (const unsigned char **m, size_t *mlen, cons

fallback:
for (i = 0; i < batchsize; i++) {
valid[i] = ED25519_FN(ed25519_sign_open) (m[i], mlen[i], pk[i], RS[i]) ? 0 : 1;
valid[i] = CCW_FN(ed25519_sign_open) (m[i], mlen[i], pk[i], RS[i]) ? 0 : 1;
ret |= (valid[i] ^ 1);
}
}
Expand All @@ -266,7 +266,7 @@ ED25519_FN(ed25519_sign_open_batch) (const unsigned char **m, size_t *mlen, cons
}

for (i = 0; i < num; i++) {
valid[i] = ED25519_FN(ed25519_sign_open) (m[i], mlen[i], pk[i], RS[i]) ? 0 : 1;
valid[i] = CCW_FN(ed25519_sign_open) (m[i], mlen[i], pk[i], RS[i]) ? 0 : 1;
ret |= (valid[i] ^ 1);
}

Expand Down
2 changes: 1 addition & 1 deletion cardano-crypto-wallet/cbits/ed25519/ed25519-randombytes.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void
ED25519_FN(ed25519_randombytes_unsafe) (void *p, size_t len) {
CCW_FN(ed25519_randombytes_unsafe) (void *p, size_t len) {
exit(1);
}
14 changes: 6 additions & 8 deletions cardano-crypto-wallet/cbits/ed25519/ed25519.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/


#define ED25519_FN(fn) ccw_##fn

#include "ed25519-donna.h"
#include "ed25519.h"
#include "ed25519-randombytes.h"
Expand Down Expand Up @@ -37,7 +35,7 @@ ed25519_hram(hash_512bits hram, const ed25519_signature RS, const ed25519_public
}

void
ED25519_FN(ed25519_publickey) (const ed25519_secret_key sk, ed25519_public_key pk) {
CCW_FN(ed25519_publickey) (const ed25519_secret_key sk, ed25519_public_key pk) {
bignum256modm a;
ge25519 ALIGN(16) A;
hash_512bits extsk = { 0 };
Expand All @@ -55,7 +53,7 @@ ED25519_FN(ed25519_publickey) (const ed25519_secret_key sk, ed25519_public_key p
}

void
ED25519_FN(ed25519_sign) (const unsigned char *m, size_t mlen, const unsigned char *salt, size_t slen, const ed25519_secret_key sk, const ed25519_public_key pk, ed25519_signature RS) {
CCW_FN(ed25519_sign) (const unsigned char *m, size_t mlen, const unsigned char *salt, size_t slen, const ed25519_secret_key sk, const ed25519_public_key pk, ed25519_signature RS) {
ed25519_hash_context ctx;
bignum256modm r, S, a;
ge25519 ALIGN(16) R;
Expand Down Expand Up @@ -94,7 +92,7 @@ ED25519_FN(ed25519_sign) (const unsigned char *m, size_t mlen, const unsigned ch
}

int
ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS) {
CCW_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS) {
ge25519 ALIGN(16) R, A;
hash_512bits hash;
bignum256modm hram, S;
Expand All @@ -120,7 +118,7 @@ ED25519_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed2551

/* we only need the leftmost 32 bytes of the extended secret key */
int
ED25519_FN(ed25519_scalar_add) (const ed25519_secret_key sk1, const ed25519_secret_key sk2, ed25519_secret_key res)
CCW_FN(ed25519_scalar_add) (const ed25519_secret_key sk1, const ed25519_secret_key sk2, ed25519_secret_key res)
{
bignum256modm s1, s2;
expand256_modm(s1, sk1, 32);
Expand All @@ -131,7 +129,7 @@ ED25519_FN(ed25519_scalar_add) (const ed25519_secret_key sk1, const ed25519_secr
}

int
ED25519_FN(ed25519_point_add) (const ed25519_public_key pk1, const ed25519_public_key pk2, ed25519_public_key res)
CCW_FN(ed25519_point_add) (const ed25519_public_key pk1, const ed25519_public_key pk2, ed25519_public_key res)
{
ge25519 ALIGN(16) R, P, Q;

Expand All @@ -148,7 +146,7 @@ ED25519_FN(ed25519_point_add) (const ed25519_public_key pk1, const ed25519_publi
}

int
ED25519_FN(ed25519_extend) (const ed25519_unextended_secret_key seed, ed25519_secret_key secret)
CCW_FN(ed25519_extend) (const ed25519_unextended_secret_key seed, ed25519_secret_key secret)
{
ed25519_extsk(secret, seed);

Expand Down
14 changes: 8 additions & 6 deletions cardano-crypto-wallet/cbits/ed25519/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <stdlib.h>

#define CCW_FN(fn) cardano_crypto_wallet_##fn

#if defined(__cplusplus)
extern "C" {
#endif
Expand All @@ -12,12 +14,12 @@ typedef unsigned char ed25519_public_key[32];
typedef unsigned char ed25519_unextended_secret_key[32]; // this is the UNEXTENDED SECRET KEY
typedef unsigned char ed25519_secret_key[64]; // this is the EXTENDED SECRET KEY

void ccw_ed25519_publickey(const ed25519_secret_key sk, ed25519_public_key pk);
int ccw_ed25519_sign_open(const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS);
void ccw_ed25519_sign (const unsigned char *m, size_t mlen, const unsigned char *salt, size_t slen, const ed25519_secret_key sk, const ed25519_public_key pk, ed25519_signature RS);
int ccw_ed25519_scalar_add (const ed25519_secret_key sk1, const ed25519_secret_key sk2, ed25519_secret_key res);
int ccw_ed25519_point_add (const ed25519_public_key pk1, const ed25519_public_key pk2, ed25519_public_key res);
int ccw_ed25519_extend (const ed25519_unextended_secret_key seed, ed25519_secret_key secret);
void CCW_FN(ed25519_publickey) (const ed25519_secret_key sk, ed25519_public_key pk);
int CCW_FN(ed25519_sign_open) (const unsigned char *m, size_t mlen, const ed25519_public_key pk, const ed25519_signature RS);
void CCW_FN(ed25519_sign) (const unsigned char *m, size_t mlen, const unsigned char *salt, size_t slen, const ed25519_secret_key sk, const ed25519_public_key pk, ed25519_signature RS);
int CCW_FN(ed25519_scalar_add) (const ed25519_secret_key sk1, const ed25519_secret_key sk2, ed25519_secret_key res);
int CCW_FN(ed25519_point_add) (const ed25519_public_key pk1, const ed25519_public_key pk2, ed25519_public_key res);
int CCW_FN(ed25519_extend) (const ed25519_unextended_secret_key seed, ed25519_secret_key secret);

#if defined(__cplusplus)
}
Expand Down
Loading
Loading