Fossil

Check-in [bf16ab9b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Completed implementation of utility functions to encrypt and decrypt blobs.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | experimental
Files: files | file ages | folders
SHA1: bf16ab9b7bdcaa80d7fd868b997abaa018e372e7
User & Date: drh 2009-03-26 23:26:31
Context
2009-03-27
14:32
Infrastructure in place on the client side to encrypt sync traffic. This is mostly untested so far because we do not yet have a server that understands encrypted traffic. check-in: 9a23c348 user: drh tags: experimental
2009-03-26
23:26
Completed implementation of utility functions to encrypt and decrypt blobs. check-in: bf16ab9b user: drh tags: experimental
15:32
Incremental changes toward encrypting sync traffic. The changes are incomplete, but all legacy functionality appears to still works. check-in: 5468ec7c user: drh tags: experimental
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/blob.c.

904
905
906
907
908
909
910





911
912
913
914
915
916
917
...
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941







942
943
944
945
946
947
948
...
952
953
954
955
956
957
958
959






































960
961
962
963
964
965
966
    t = s[i];
    s[i] = s[j];
    s[j] = t;
    aOut[k] = aIn[k] ^ s[(s[i] + s[j])&255];
  }
}






/*
** Encrypt a blob using RC4.
**
** The key is constructed by taking the sha1 hash of the password
** and a random 20-byte nonce.  This nonce becomes the first 20 bytes
** of the encrypted output.  The first 1000 bytes of the RC4 byte stream
** are discarded.  The input is XORed against the remaining RC4 byte stream
................................................................................
** Because of the prepended nonce, the output will be 20 bytes larger
** than the input.
**
** pOut should be initialized prior to invoking this routine.  pOut might
** already contain other content.  The encryption is appended to pOut.
*/
void blob_encrypt(Blob *pIn, const char *zPassword, Blob *pOut){
  unsigned char aNonce[20];
  unsigned char *aIn;
  int nIn;
  unsigned char *aOut;

  sqlite3_randomness(sizeof(aNonce), aNonce);
  aIn = (unsigned char*)pIn->aData;
  aIn += pIn->iCursor;
  nIn = pIn->nUsed - pIn->iCursor;
  if( nIn<=0 ) return;
  







}

/*
** Decrypt a blob.
**
** This routine undoes the work of blob_encrypt.  pIn should be a blob
** that was generated by blob_encrypt.  Assuming the same password is
................................................................................
** extending to the end of the blob is decrypted.  Any content of pIn
** prior to the current cursor position is ignored.
**
** pOut should be initialized prior to invoking this routine.  pOut might
** already contain other content.  The encryption is appended to pOut.
*/
void blob_decrypt(Blob *pIn, const char *zPassword, Blob *pOut){
  /* TBD... */






































}

#ifdef __MINGW32__
/*
** Convert every \n character in the given blob into \r\n.
*/
void blob_add_cr(Blob *p){







>
>
>
>
>







 







|
|

|

<
|



<
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
...
929
930
931
932
933
934
935
936
937
938
939
940

941
942
943
944

945
946
947
948
949
950
951
952
953
954
955
956
957
958
...
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
    t = s[i];
    s[i] = s[j];
    s[j] = t;
    aOut[k] = aIn[k] ^ s[(s[i] + s[j])&255];
  }
}

/*
** Number of bytes of nonce for each encoding
*/
#define N_NONCE 20

/*
** Encrypt a blob using RC4.
**
** The key is constructed by taking the sha1 hash of the password
** and a random 20-byte nonce.  This nonce becomes the first 20 bytes
** of the encrypted output.  The first 1000 bytes of the RC4 byte stream
** are discarded.  The input is XORed against the remaining RC4 byte stream
................................................................................
** Because of the prepended nonce, the output will be 20 bytes larger
** than the input.
**
** pOut should be initialized prior to invoking this routine.  pOut might
** already contain other content.  The encryption is appended to pOut.
*/
void blob_encrypt(Blob *pIn, const char *zPassword, Blob *pOut){
  char *aNonce;
  char *aIn;
  int nIn;
  char *aOut;


  aIn = pIn->aData;
  aIn += pIn->iCursor;
  nIn = pIn->nUsed - pIn->iCursor;
  if( nIn<=0 ) return;

  blob_resize(pOut, pOut->nUsed+nIn+N_NONCE);
  aOut = pOut->aData;
  aOut += pOut->iCursor;
  aNonce = aOut;
  sqlite3_randomness(N_NONCE, aNonce);
  aOut += N_NONCE;
  rc4_coder(zPassword, aNonce, N_NONCE, aIn, nIn, aOut);
}

/*
** Decrypt a blob.
**
** This routine undoes the work of blob_encrypt.  pIn should be a blob
** that was generated by blob_encrypt.  Assuming the same password is
................................................................................
** extending to the end of the blob is decrypted.  Any content of pIn
** prior to the current cursor position is ignored.
**
** pOut should be initialized prior to invoking this routine.  pOut might
** already contain other content.  The encryption is appended to pOut.
*/
void blob_decrypt(Blob *pIn, const char *zPassword, Blob *pOut){
  char *aNonce;
  char *aIn;
  int nIn;
  char *aOut;

  aIn = pIn->aData;
  aNonce = aIn + pIn->iCursor;
  aIn = aNonce + N_NONCE;
  nIn = pIn->nUsed - pIn->iCursor - N_NONCE;
  blob_resize(pOut, pOut->iCursor + nIn);
  aOut = pOut->aData;
  aOut += pOut->iCursor;
  rc4_coder(zPassword, aNonce, N_NONCE, aIn, nIn, aOut);
}


/*
** COMMAND: test-encrypt PASSWORD PLAINTEXT CYPHERTEXT
*/
void encrypt_test_cmd(void){
  Blob in, out;
  if( g.argc!=5 ) usage("PASSWORD INPUTFILE OUTPUTFILE");
  blob_read_from_file(&in, g.argv[3]);
  blob_zero(&out);
  blob_encrypt(&in, g.argv[2], &out);
  blob_write_to_file(&out, g.argv[4]);
}


/*
** COMMAND: test-decrypt PASSWORD CYPHERTEXT PLAINTEXT
*/
void decrypt_test_cmd(void){
  Blob in, out;
  if( g.argc!=5 ) usage("PASSWORD INPUTFILE OUTPUTFILE");
  blob_read_from_file(&in, g.argv[3]);
  blob_zero(&out);
  blob_decrypt(&in, g.argv[2], &out);
  blob_write_to_file(&out, g.argv[4]);
}

#ifdef __MINGW32__
/*
** Convert every \n character in the given blob into \r\n.
*/
void blob_add_cr(Blob *p){