Giter Site home page Giter Site logo

christophhagen / cryptokit25519 Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 3.0 114 KB

A Swift module for Curve25519 functions and AES-GCM encryption (compatible with Apple CryptoKit)

License: MIT License

Swift 100.00%
swift cryptokit signatures key-agreement encryption

cryptokit25519's Introduction

I'm a passionate programmer/engineer/scientist who likes to try new things, currently based in Munich, Germany.

My main interests in no particular order:

  • ๐Ÿš€ Space science and technology
  • ๐Ÿ„๐Ÿป Outdoor sports
  • ๐Ÿชš Different types of craftsmanship
  • ๐Ÿ“ท Photography
  • ๐Ÿ“บ Technological progress
  • ๐Ÿšฆ Ethics
  • ๐Ÿ’ป Computers and programming

You can find some of my other projects on my website christophhagen.de

Repositories

Most of my repositories are written in/for Swift, because it's a very modern, safe, and versatile language.

There are many more repositories, also in C++ for my embedded projects.

And who doesn't enjoy a few statistics?

Christoph Hagen's Github Statistics

cryptokit25519's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cryptokit25519's Issues

Crashes when generating keys

Hello, I tried to include CryptoKit25519 in a project that needs to support iOS 11+, but it seems to (randomly) crash whenever I generate keys (at least on iOS 13.4 where I am testing).
Here is the simplest sample project I could think of to reproduce the issue (using SwiftUI to be quicker):

import SwiftUI
import CryptoKit25519

struct ContentView: View {
    
    @State var privateKeyString = "n/a"
    @State var publicKeyString = "n/a"
    
    var body: some View {
        VStack {
            Text("Private key: \(privateKeyString)")
            Text("Public key: \(publicKeyString)")
            Button("Generate keys") {
                self.generateKeys()
            }
        }
    }
    
    func generateKeys() {
        let privateKey = try! Curve25519.Signing.PrivateKey()
        let privateData = privateKey.rawRepresentation
        privateKeyString = privateData.base64EncodedString()
        
        let publicKey = privateKey.publicKey
        let publicData = publicKey.rawRepresentation
        publicKeyString = publicData.base64EncodedString()
    }
    
}

Sometimes (usually - but not always - on the first try), keys are generated and displayed. But most often (almost always as of the second try) the app crashes. Unfortunately the error displayed is not always the same nor is very descriptive.

  • Sometimes I get something like

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1e378bfa4)

on the row return Data(bytes) in the rawRepresentation variable in PrivateKey.swift

  • Other times I get:

Thread 3: EXC_BAD_ACCESS (code=1, address=0xf5d65481e50b506b)

on the queue CA DispatchGroup

  • When I am "luckiest" I get an EXC_BREAKPOINT exception on the declaration of the AppDelegate class together with the most descriptive error:
CryptoTest(589,0x1050ae040) malloc: Heap corruption detected, free list is damaged at 0x2834a8d40
*** Incorrect guard value: 11272132526359900924
CryptoTest(589,0x1050ae040) malloc: *** set a breakpoint in malloc_error_break to debug

which would indicate something like... a memory leak perhaps?

There seem to be no discernible pattern among the various kinds of errors. Also, the situation is the same if I remove the part on the publicKey and just generate the privateKey.

Do you perhaps know what could be going on here? Thanks a lot!

Cannot recover the correct public key from private keys

Hi, I have been testing around with this library and found that the public key could not be correctly computed from the private keys.

import CryptoKit25519

let k1 = try! Curve25519.KeyAgreement.PrivateKey(rawRepresentation: Data([
  75, 157, 102, 134, 12, 57, 222, 49, 73, 43, 219, 59, 9, 5, 39, 191,
  102, 239, 30, 167, 95, 16, 91, 182, 248, 115, 40, 223, 187, 159, 227, 55]))
print("actual  ", Array(k1.publicKey.rawRepresentation))
print("expected", [30, 222, 35, 48, 128, 169, 48, 95, 101, 138, 236, 237, 7, 239, 4, 206, 211, 112, 181, 241, 187, 160, 153, 179, 171, 195, 158, 199, 180, 245, 168, 63])
// actual   [24, 233, 101, 77, 124, 166, 123, 38, 231, 73, 205, 242, 13, 207, 115, 34, 178, 98, 195, 39, 109, 223, 183, 78, 35, 64, 1, 165, 74, 213, 89, 46]
// expected [30, 222, 35, 48, 128, 169, 48, 95, 101, 138, 236, 237, 7, 239, 4, 206, 211, 112, 181, 241, 187, 160, 153, 179, 171, 195, 158, 199, 180, 245, 168, 63]

let k2 = try! Curve25519.KeyAgreement.PrivateKey(rawRepresentation: Data([
  0xc8, 0x06, 0x43, 0x9d, 0xc9, 0xd2, 0xc4, 0x76, 0xff, 0xed, 0x8f, 0x25, 0x80, 0xc0, 0x88, 0x8d,
  0x58, 0xab, 0x40, 0x6b, 0xf7, 0xae, 0x36, 0x98, 0x87, 0x90, 0x21, 0xb9, 0x6b, 0xb4, 0xbf, 0x59]))
print("actual  ", Array(k2.publicKey.rawRepresentation))
print("expected", [
  0x1b, 0xb7, 0x59, 0x66, 0xf2, 0xe9, 0x3a, 0x36, 0x91, 0xdf, 0xff, 0x94, 0x2b, 0xb2, 0xa4, 0x66,
  0xa1, 0xc0, 0x8b, 0x8d, 0x78, 0xca, 0x3f, 0x4d, 0x6d, 0xf8, 0xb8, 0xbf, 0xa2, 0xe4, 0xee, 0x28])
// actual   [27, 183, 89, 102, 242, 233, 58, 54, 145, 223, 255, 148, 43, 178, 164, 102, 161, 192, 139, 141, 120, 202, 63, 77, 109, 248, 184, 191, 162, 228, 238, 40]
// expected [27, 183, 89, 102, 242, 233, 58, 54, 145, 223, 255, 148, 43, 178, 164, 102, 161, 192, 139, 141, 120, 202, 63, 77, 109, 248, 184, 191, 162, 228, 238, 40]

The two test vectors are, respectively, extracted from test vector from Noise protocol, and the test case given in Curve25519 package.

(First test vector)
Private key: 4b9d66860c39de31492bdb3b090527bf66ef1ea75f105bb6f87328dfbb9fe337
Public key:  1ede233080a9305f658aeced07ef04ced370b5f1bba099b3abc39ec7b4f5a83f

The cause is, some of the bits in the secret key must be unset (and some otherwise need to be set) before computation:

func normalize(secretKey: [UInt8]) -> [UInt8] {
  var newSecretKey = secretKey
  newSecretKey[0] &= 0xf8
  newSecretKey[31] &= 0x3f
  newSecretKey[31] |= 0x40
  return newSecretKey
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.